1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/Services/Quickbooks/QuickbooksService.php

107 lines
3.1 KiB
PHP
Raw Normal View History

2024-08-26 12:17:51 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Quickbooks;
use App\Models\Client;
use App\Models\Company;
use App\Models\Invoice;
use App\Models\Product;
2024-09-22 11:27:34 +02:00
use App\Factory\ClientFactory;
use App\Factory\InvoiceFactory;
use App\Factory\ProductFactory;
use App\Factory\ClientContactFactory;
use App\DataMapper\QuickbooksSettings;
2024-08-26 12:17:51 +02:00
use QuickBooksOnline\API\Core\CoreConstants;
2024-09-22 11:27:34 +02:00
use App\Services\Quickbooks\Models\QbInvoice;
use App\Services\Quickbooks\Models\QbProduct;
use App\Services\Quickbooks\Jobs\QuickbooksSync;
2024-08-26 12:17:51 +02:00
use QuickBooksOnline\API\DataService\DataService;
use App\Services\Quickbooks\Transformers\ClientTransformer;
use App\Services\Quickbooks\Transformers\InvoiceTransformer;
use App\Services\Quickbooks\Transformers\PaymentTransformer;
use App\Services\Quickbooks\Transformers\ProductTransformer;
class QuickbooksService
{
public DataService $sdk;
2024-09-22 11:27:34 +02:00
public QbInvoice $invoice;
public QbProduct $product;
public array $settings;
2024-08-26 12:17:51 +02:00
private bool $testMode = true;
2024-09-22 11:27:34 +02:00
public function __construct(public Company $company)
2024-08-26 12:17:51 +02:00
{
$this->init();
}
private function init(): self
{
$config = [
'ClientID' => config('services.quickbooks.client_id'),
'ClientSecret' => config('services.quickbooks.client_secret'),
'auth_mode' => 'oauth2',
'scope' => "com.intuit.quickbooks.accounting",
// 'RedirectURI' => 'https://developer.intuit.com/v2/OAuth2Playground/RedirectUrl',
2024-09-19 08:39:53 +02:00
'RedirectURI' => $this->testMode ? 'https://grok.romulus.com.au/quickbooks/authorized' : 'https://invoicing.co/quickbooks/authorized',
2024-08-26 12:17:51 +02:00
'baseUrl' => $this->testMode ? CoreConstants::SANDBOX_DEVELOPMENT : CoreConstants::QBO_BASEURL,
];
$merged = array_merge($config, $this->ninjaAccessToken());
$this->sdk = DataService::Configure($merged);
$this->sdk->setLogLocation(storage_path("logs/quickbooks.log"));
$this->sdk->enableLog();
$this->sdk->setMinorVersion("73");
$this->sdk->throwExceptionOnError(true);
2024-09-22 11:27:34 +02:00
$this->invoice = new QbInvoice($this);
$this->product = new QbProduct($this);
$this->settings = $this->company->quickbooks->settings;
2024-08-26 12:17:51 +02:00
return $this;
}
2024-09-19 08:39:53 +02:00
private function ninjaAccessToken(): array
2024-08-26 12:17:51 +02:00
{
return isset($this->company->quickbooks->accessTokenKey) ? [
'accessTokenKey' => $this->company->quickbooks->accessTokenKey,
'refreshTokenKey' => $this->company->quickbooks->refresh_token,
'QBORealmID' => $this->company->quickbooks->realmID,
] : [];
}
public function sdk(): SdkWrapper
{
return new SdkWrapper($this->sdk, $this->company);
}
/**
2024-09-19 08:39:53 +02:00
*
2024-08-26 12:17:51 +02:00
*
* @return void
*/
2024-09-19 08:39:53 +02:00
public function syncFromQb(): void
2024-08-26 12:17:51 +02:00
{
2024-08-27 03:49:01 +02:00
QuickbooksSync::dispatch($this->company->id, $this->company->db);
2024-08-26 12:17:51 +02:00
}
}