Refactor for QB

This commit is contained in:
David Bomba 2024-08-26 20:17:51 +10:00
parent 5f32baceef
commit d074dd7edb
13 changed files with 171 additions and 98 deletions

View File

@ -15,7 +15,7 @@ use App\Http\Requests\Quickbooks\AuthorizedQuickbooksRequest;
use App\Libraries\MultiDB; use App\Libraries\MultiDB;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use App\Http\Requests\Quickbooks\AuthQuickbooksRequest; use App\Http\Requests\Quickbooks\AuthQuickbooksRequest;
use App\Services\Import\Quickbooks\QuickbooksService; use App\Services\Quickbooks\QuickbooksService;
class ImportQuickbooksController extends BaseController class ImportQuickbooksController extends BaseController
{ {

View File

@ -9,31 +9,38 @@
* @license https://www.elastic.co/licensing/elastic-license * @license https://www.elastic.co/licensing/elastic-license
*/ */
namespace App\Services\Import\Quickbooks; namespace App\Services\Quickbooks\Jobs;
use App\Factory\ClientContactFactory;
use App\Factory\ClientFactory;
use App\Factory\InvoiceFactory;
use App\Factory\ProductFactory;
use App\Models\Client; use App\Models\Client;
use App\Models\Company; use App\Models\Company;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Product; use App\Models\Product;
use QuickBooksOnline\API\Core\CoreConstants; use App\Libraries\MultiDB;
use QuickBooksOnline\API\DataService\DataService; use Illuminate\Bus\Queueable;
use App\Services\Import\Quickbooks\Transformers\ClientTransformer; use App\Factory\ClientFactory;
use App\Services\Import\Quickbooks\Transformers\InvoiceTransformer; use App\Factory\InvoiceFactory;
use App\Services\Import\Quickbooks\Transformers\PaymentTransformer; use App\Factory\ProductFactory;
use App\Services\Import\Quickbooks\Transformers\ProductTransformer; use App\Factory\ClientContactFactory;
use App\DataMapper\QuickbooksSettings;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Services\Quickbooks\QuickbooksService;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use App\Services\Quickbooks\Transformers\ClientTransformer;
use App\Services\Quickbooks\Transformers\InvoiceTransformer;
use App\Services\Quickbooks\Transformers\PaymentTransformer;
use App\Services\Quickbooks\Transformers\ProductTransformer;
// quickbooks_realm_id class QuickbooksSync implements ShouldQueue
// quickbooks_refresh_token
// quickbooks_refresh_expires
class QuickbooksService
{ {
public DataService $sdk; use Dispatchable;
use InteractsWithQueue;
private $entities = [ use Queueable;
use SerializesModels;
private array $entities = [
'product' => 'Item', 'product' => 'Item',
'client' => 'Customer', 'client' => 'Customer',
'invoice' => 'Invoice', 'invoice' => 'Invoice',
@ -42,73 +49,34 @@ class QuickbooksService
'payment' => 'Payment', 'payment' => 'Payment',
]; ];
private bool $testMode = true; private QuickbooksService $qbs;
private mixed $settings; private ?array $settings;
public function __construct(private Company $company) private Company $company;
public function __construct(public int $company_id, public string $db)
{ {
$this->init();
$this->settings = $this->company->quickbooks->settings;
} }
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',
'RedirectURI' => $this->testMode ? 'https://above-distinctly-teal.ngrok-free.app/quickbooks/authorized' : 'https://invoicing.co/quickbooks/authorized',
'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);
return $this;
}
private function ninjaAccessToken()
{
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);
}
/** /**
* //@todo - refactor to a job * Execute the job.
*
* @return void
*/ */
public function syncFromQb() public function handle()
{ {
//syncable_records. MultiDB::setDb($this->db);
foreach($this->entities as $key => $entity) $this->company = Company::find($this->company_id);
{ $this->qbs = new QuickbooksService($this->company);
if(!$this->syncGate($key, 'pull')) $this->settings = $this->company->quickbooks->settings;
nlog("here we go!");
foreach($this->entities as $key => $entity) {
if(!$this->syncGate($key, 'pull')) {
continue; continue;
}
$records = $this->sdk()->fetchRecords($entity); $records = $this->qbs->sdk()->fetchRecords($entity);
// nlog(json_encode($records));
$this->processEntitySync($key, $records); $this->processEntitySync($key, $records);
@ -126,10 +94,10 @@ class QuickbooksService
return (bool) $this->settings[$entity]['sync'] && $this->settings[$entity]['update_record']; return (bool) $this->settings[$entity]['sync'] && $this->settings[$entity]['update_record'];
} }
private function harvestQbEntityName(string $entity): string // private function harvestQbEntityName(string $entity): string
{ // {
return $this->entities[$entity]; // return $this->entities[$entity];
} // }
private function processEntitySync(string $entity, $records) private function processEntitySync(string $entity, $records)
{ {
@ -171,7 +139,7 @@ class QuickbooksService
foreach($payment_ids as $payment_id) foreach($payment_ids as $payment_id)
{ {
$payment = $this->sdk->FindById('Payment', $payment_id); $payment = $this->qbs->sdk->FindById('Payment', $payment_id);
$payment_transformer = new PaymentTransformer($this->company); $payment_transformer = new PaymentTransformer($this->company);
$transformed = $payment_transformer->qbToNinja($payment); $transformed = $payment_transformer->qbToNinja($payment);
@ -261,7 +229,6 @@ class QuickbooksService
foreach($records as $record) foreach($records as $record)
{ {
$ninja_data = $product_transformer->qbToNinja($record); $ninja_data = $product_transformer->qbToNinja($record);
// nlog($ninja_data);
if($product = $this->findProduct($ninja_data['hash'])) if($product = $this->findProduct($ninja_data['hash']))
{ {
@ -326,4 +293,16 @@ class QuickbooksService
} }
public function middleware()
{
return [new WithoutOverlapping("qbs-{$this->company_id}-{$this->db}")];
}
public function failed($exception)
{
nlog("QuickbooksSync failed => ".$exception->getMessage());
config(['queue.failed.driver' => null]);
}
} }

View File

@ -0,0 +1,94 @@
<?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\Factory\ClientContactFactory;
use App\Factory\ClientFactory;
use App\Factory\InvoiceFactory;
use App\Factory\ProductFactory;
use App\Models\Client;
use App\Models\Company;
use App\Models\Invoice;
use App\Models\Product;
use App\Services\Quickbooks\Jobs\QuickbooksSync;
use QuickBooksOnline\API\Core\CoreConstants;
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;
// quickbooks_realm_id
// quickbooks_refresh_token
// quickbooks_refresh_expires
class QuickbooksService
{
public DataService $sdk;
private bool $testMode = true;
public function __construct(private Company $company)
{
$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',
'RedirectURI' => $this->testMode ? 'https://above-distinctly-teal.ngrok-free.app/quickbooks/authorized' : 'https://invoicing.co/quickbooks/authorized',
'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);
return $this;
}
private function ninjaAccessToken()
{
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);
}
/**
* //@todo - refactor to a job
*
* @return void
*/
public function syncFromQb()
{
QuickbooksSync::dispatch($this->company);
}
}

View File

@ -9,7 +9,7 @@
* @license https://www.elastic.co/licensing/elastic-license * @license https://www.elastic.co/licensing/elastic-license
*/ */
namespace App\Services\Import\Quickbooks; namespace App\Services\Quickbooks;
use App\DataMapper\QuickbooksSettings; use App\DataMapper\QuickbooksSettings;
use Carbon\Carbon; use Carbon\Carbon;

View File

@ -10,7 +10,7 @@
* @license https://www.elastic.co/licensing/elastic-license * @license https://www.elastic.co/licensing/elastic-license
*/ */
namespace App\Services\Import\Quickbooks\Transformers; namespace App\Services\Quickbooks\Transformers;
use App\Models\Client; use App\Models\Client;
use App\Models\Company; use App\Models\Company;

View File

@ -10,7 +10,7 @@
* @license https://www.elastic.co/licensing/elastic-license * @license https://www.elastic.co/licensing/elastic-license
*/ */
namespace App\Services\Import\Quickbooks\Transformers; namespace App\Services\Quickbooks\Transformers;
use App\DataMapper\ClientSettings; use App\DataMapper\ClientSettings;

View File

@ -10,7 +10,7 @@
* @license https://www.elastic.co/licensing/elastic-license * @license https://www.elastic.co/licensing/elastic-license
*/ */
namespace App\Services\Import\Quickbooks\Transformers; namespace App\Services\Quickbooks\Transformers;
use App\Models\Client; use App\Models\Client;
use App\Models\Company; use App\Models\Company;

View File

@ -9,7 +9,7 @@
* @license https://www.elastic.co/licensing/elastic-license * @license https://www.elastic.co/licensing/elastic-license
*/ */
namespace App\Services\Import\Quickbooks\Transformers; namespace App\Services\Quickbooks\Transformers;
use App\Models\Company; use App\Models\Company;
use App\Models\Payment; use App\Models\Payment;

View File

@ -10,7 +10,7 @@
* @license https://www.elastic.co/licensing/elastic-license * @license https://www.elastic.co/licensing/elastic-license
*/ */
namespace App\Services\Import\Quickbooks\Transformers; namespace App\Services\Quickbooks\Transformers;
/** /**
* Class ProductTransformer. * Class ProductTransformer.

View File

@ -2,9 +2,9 @@
namespace Tests\Feature\Http\Controllers; namespace Tests\Feature\Http\Controllers;
use App\Services\Import\Quickbooks\Contracts\SdkInterface as QuickbooksInterface; use App\Services\Quickbooks\Contracts\SdkInterface as QuickbooksInterface;
use App\Services\Import\Quickbooks\Service as QuickbooksService; use App\Services\Quickbooks\Service as QuickbooksService;
use App\Services\Import\Quickbooks\SdkWrapper as QuickbooksSDK; use App\Services\Quickbooks\SdkWrapper as QuickbooksSDK;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Foundation\Testing\WithFaker;

View File

@ -2,9 +2,9 @@
namespace Tests\Integration\Services\Import\Quickbooks; namespace Tests\Integration\Services\Import\Quickbooks;
use App\Services\Import\Quickbooks\Contracts\SdkInterface as QuickbooksInterface; use App\Services\Quickbooks\Contracts\SdkInterface as QuickbooksInterface;
use App\Services\Import\Quickbooks\Service as QuickbooksService; use App\Services\Quickbooks\Service as QuickbooksService;
use App\Services\Import\Quickbooks\SdkWrapper as QuickbooksSDK; use App\Services\Quickbooks\SdkWrapper as QuickbooksSDK;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Tests\TestCase; use Tests\TestCase;

View File

@ -7,8 +7,8 @@ namespace Tests\Unit\Services\Import\Quickbooks;
use Mockery; use Mockery;
use Tests\TestCase; use Tests\TestCase;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use App\Services\Import\Quickbooks\Contracts\SdkInterface; use App\Services\Quickbooks\Contracts\SdkInterface;
use App\Services\Import\Quickbooks\SdkWrapper as QuickbookSDK; use App\Services\Quickbooks\SdkWrapper as QuickbookSDK;
class SdkWrapperTest extends TestCase class SdkWrapperTest extends TestCase
{ {

View File

@ -5,8 +5,8 @@ namespace Tests\Unit\Services\Import\Quickbooks;
use Mockery; use Mockery;
use Tests\TestCase; use Tests\TestCase;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use App\Services\Import\Quickbooks\Service as QuickbooksService; use App\Services\Quickbooks\Service as QuickbooksService;
use App\Services\Import\Quickbooks\Contracts\SdkInterface as QuickbooksInterface; use App\Services\Quickbooks\Contracts\SdkInterface as QuickbooksInterface;
class ServiceTest extends TestCase class ServiceTest extends TestCase
{ {