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

170 lines
5.0 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;
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-23 04:57:27 +02:00
/**
2024-09-23 05:36:12 +02:00
* Updates the gate for a given entity
2024-09-23 04:57:27 +02:00
*
* @param string $entity
* @return bool
*/
public function updateGate(string $entity): bool
{
2024-09-24 04:46:16 +02:00
nlog($this->settings->{$entity}->sync);
nlog($this->settings->{$entity}->update_record);
return $this->settings->{$entity}->sync && $this->settings->{$entity}->update_record;
2024-09-23 04:57:27 +02:00
}
/**
* Determines whether a sync is allowed based on the settings
*
* @param string $entity
* @param string $direction
* @return bool
*/
public function syncGate(string $entity, string $direction): bool
{
return (bool) $this->settings->{$entity}->sync && in_array($this->settings->{$entity}->direction->value, [$direction, 'bidirectional']);
}
2024-08-26 12:17:51 +02:00
}