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;
|
2024-09-22 23:55:53 +02:00
|
|
|
use App\DataMapper\QuickbooksSync;
|
2024-09-22 11:27:34 +02:00
|
|
|
use App\Factory\ClientContactFactory;
|
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;
|
2024-08-26 12:17:51 +02:00
|
|
|
use QuickBooksOnline\API\DataService\DataService;
|
2024-09-22 23:55:53 +02:00
|
|
|
use App\Services\Quickbooks\Jobs\QuickbooksImport;
|
2024-09-23 04:43:43 +02:00
|
|
|
use App\Services\Quickbooks\Models\QbClient;
|
2024-08-26 12:17:51 +02:00
|
|
|
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;
|
|
|
|
|
2024-09-23 04:43:43 +02:00
|
|
|
public QbClient $client;
|
|
|
|
|
2024-09-22 23:55:53 +02:00
|
|
|
public QuickbooksSync $settings;
|
2024-09-22 11:27:34 +02:00
|
|
|
|
2024-08-26 12:17:51 +02:00
|
|
|
private bool $testMode = true;
|
|
|
|
|
2024-09-23 04:43:43 +02:00
|
|
|
private bool $try_refresh = 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-23 04:43:43 +02:00
|
|
|
$this->checkToken();
|
|
|
|
|
2024-09-22 11:27:34 +02:00
|
|
|
$this->invoice = new QbInvoice($this);
|
|
|
|
|
|
|
|
$this->product = new QbProduct($this);
|
|
|
|
|
2024-09-23 04:43:43 +02:00
|
|
|
$this->client = new QbClient($this);
|
|
|
|
|
2024-09-22 11:27:34 +02:00
|
|
|
$this->settings = $this->company->quickbooks->settings;
|
|
|
|
|
2024-08-26 12:17:51 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2024-09-23 04:43:43 +02:00
|
|
|
private function checkToken(): self
|
|
|
|
{
|
2024-09-24 04:46:16 +02:00
|
|
|
nlog($this->company->quickbooks->accessTokenExpiresAt);
|
|
|
|
nlog(time());
|
|
|
|
|
|
|
|
if($this->company->quickbooks->accessTokenExpiresAt > time())
|
2024-09-23 04:43:43 +02:00
|
|
|
return $this;
|
|
|
|
|
|
|
|
if($this->company->quickbooks->accessTokenExpiresAt < time() && $this->try_refresh){
|
2024-09-24 04:46:16 +02:00
|
|
|
nlog('Refreshing token');
|
2024-09-23 04:43:43 +02:00
|
|
|
$this->sdk()->refreshToken($this->company->quickbooks->refresh_token);
|
|
|
|
$this->company = $this->company->fresh();
|
|
|
|
$this->try_refresh = false;
|
|
|
|
$this->init();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
nlog('Quickbooks token expired and could not be refreshed => ' .$this->company->company_key);
|
|
|
|
throw new \Exception('Quickbooks token expired and could not be refreshed');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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-09-22 23:55:53 +02:00
|
|
|
QuickbooksImport::dispatch($this->company->id, $this->company->db);
|
2024-08-26 12:17:51 +02:00
|
|
|
}
|
|
|
|
|
2024-09-23 04:43:43 +02:00
|
|
|
public function findEntityById(string $entity, string $id): mixed
|
|
|
|
{
|
|
|
|
return $this->sdk->FindById($entity, $id);
|
|
|
|
}
|
2024-09-24 06:15:11 +02:00
|
|
|
|
2024-09-23 04:57:27 +02:00
|
|
|
/**
|
2024-09-24 06:15:11 +02:00
|
|
|
* Flag to determine if a sync is allowed in either direction
|
2024-09-23 04:57:27 +02:00
|
|
|
*
|
|
|
|
* @param string $entity
|
2024-09-24 06:15:11 +02:00
|
|
|
* @param mixed $direction
|
2024-09-23 04:57:27 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2024-09-24 06:15:11 +02:00
|
|
|
public function syncable(string $entity, \App\Enum\SyncDirection $direction): bool
|
2024-09-23 04:57:27 +02:00
|
|
|
{
|
2024-09-24 06:15:11 +02:00
|
|
|
return $this->settings->{$entity}->direction === $direction || $this->settings->{$entity}->direction === \App\Enum\SyncDirection::BIDIRECTIONAL;
|
2024-09-23 04:57:27 +02:00
|
|
|
}
|
|
|
|
|
2024-08-26 12:17:51 +02:00
|
|
|
}
|