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",
|
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);
|
|
|
|
|
2024-09-25 06:20:49 +02:00
|
|
|
// $this->sdk->setLogLocation(storage_path("logs/quickbooks.log"));
|
2024-08-26 12:17:51 +02:00
|
|
|
$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-09-25 12:16:16 +02:00
|
|
|
$this->checkDefaultAccounts();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function checkDefaultAccounts(): self
|
|
|
|
{
|
|
|
|
|
|
|
|
$accountQuery = "SELECT * FROM Account WHERE AccountType IN ('Income', 'Cost of Goods Sold')";
|
|
|
|
|
|
|
|
if(strlen($this->settings->default_income_account) == 0 || strlen($this->settings->default_expense_account) == 0){
|
|
|
|
|
|
|
|
nlog("Checking default accounts for company {$this->company->company_key}");
|
|
|
|
$accounts = $this->sdk->Query($accountQuery);
|
|
|
|
|
|
|
|
nlog($accounts);
|
|
|
|
|
|
|
|
$find_income_account = true;
|
|
|
|
$find_expense_account = true;
|
|
|
|
|
|
|
|
foreach ($accounts as $account) {
|
|
|
|
if ($account->AccountType->value == 'Income' && $find_income_account) {
|
|
|
|
$this->settings->default_income_account = $account->Id->value;
|
|
|
|
$find_income_account = false;
|
|
|
|
} elseif ($account->AccountType->value == 'Cost of Goods Sold' && $find_expense_account) {
|
|
|
|
$this->settings->default_expense_account = $account->Id->value;
|
|
|
|
$find_expense_account = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nlog($this->settings);
|
|
|
|
|
|
|
|
$this->company->quickbooks->settings = $this->settings;
|
|
|
|
$this->company->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2024-09-25 06:20:49 +02:00
|
|
|
if($this->company->quickbooks->accessTokenExpiresAt == 0 || $this->company->quickbooks->accessTokenExpiresAt > time())
|
2024-09-23 04:43:43 +02:00
|
|
|
return $this;
|
|
|
|
|
2024-09-25 06:20:49 +02:00
|
|
|
if($this->company->quickbooks->accessTokenExpiresAt && $this->company->quickbooks->accessTokenExpiresAt < time() && $this->try_refresh){
|
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
|
|
|
{
|
2024-09-25 06:20:49 +02:00
|
|
|
return $this->company->quickbooks->accessTokenExpiresAt > 0 ? [
|
2024-08-26 12:17:51 +02:00
|
|
|
'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-29 06:12:54 +02:00
|
|
|
|
|
|
|
public function query(string $query)
|
|
|
|
{
|
|
|
|
return $this->sdk->Query($query);
|
|
|
|
}
|
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:49:19 +02:00
|
|
|
* @param \App\Enum\SyncDirection $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
|
|
|
}
|