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

309 lines
9.7 KiB
PHP
Raw Normal View History

2024-08-21 05:22:46 +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
*/
2024-08-26 12:17:51 +02:00
namespace App\Services\Quickbooks\Jobs;
2024-08-21 05:22:46 +02:00
2024-08-26 00:24:51 +02:00
use App\Models\Client;
2024-08-21 05:22:46 +02:00
use App\Models\Company;
2024-08-26 07:48:48 +02:00
use App\Models\Invoice;
use App\Models\Product;
2024-08-26 12:17:51 +02:00
use App\Libraries\MultiDB;
use Illuminate\Bus\Queueable;
use App\Factory\ClientFactory;
use App\Factory\InvoiceFactory;
use App\Factory\ProductFactory;
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;
class QuickbooksSync implements ShouldQueue
2024-08-22 08:45:06 +02:00
{
2024-08-26 12:17:51 +02:00
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
private array $entities = [
2024-08-26 07:48:48 +02:00
'product' => 'Item',
2024-08-26 00:24:51 +02:00
'client' => 'Customer',
'invoice' => 'Invoice',
'quote' => 'Estimate',
'purchase_order' => 'PurchaseOrder',
'payment' => 'Payment',
];
2024-08-21 05:22:46 +02:00
2024-08-26 12:17:51 +02:00
private QuickbooksService $qbs;
2024-08-21 05:22:46 +02:00
2024-08-26 12:17:51 +02:00
private ?array $settings;
2024-08-26 00:24:51 +02:00
2024-08-26 12:17:51 +02:00
private Company $company;
2024-08-21 05:22:46 +02:00
2024-08-26 12:17:51 +02:00
public function __construct(public int $company_id, public string $db)
2024-08-21 05:22:46 +02:00
{
}
2024-08-26 00:24:51 +02:00
/**
2024-08-26 12:17:51 +02:00
* Execute the job.
2024-08-26 00:24:51 +02:00
*/
2024-08-26 12:17:51 +02:00
public function handle()
2024-08-26 00:24:51 +02:00
{
2024-08-26 12:17:51 +02:00
MultiDB::setDb($this->db);
2024-08-27 03:49:01 +02:00
$this->company = Company::query()->find($this->company_id);
2024-08-26 12:17:51 +02:00
$this->qbs = new QuickbooksService($this->company);
$this->settings = $this->company->quickbooks->settings;
nlog("here we go!");
foreach($this->entities as $key => $entity) {
if(!$this->syncGate($key, 'pull')) {
2024-08-26 01:49:22 +02:00
continue;
2024-08-26 12:17:51 +02:00
}
2024-08-26 00:24:51 +02:00
2024-08-26 12:17:51 +02:00
$records = $this->qbs->sdk()->fetchRecords($entity);
2024-08-26 01:49:22 +02:00
$this->processEntitySync($key, $records);
2024-08-26 00:24:51 +02:00
}
}
2024-08-26 01:49:22 +02:00
private function syncGate(string $entity, string $direction): bool
{
return (bool) $this->settings[$entity]['sync'] && in_array($this->settings[$entity]['direction'], [$direction,'bidirectional']);
}
private function updateGate(string $entity): bool
{
return (bool) $this->settings[$entity]['sync'] && $this->settings[$entity]['update_record'];
}
2024-08-26 12:17:51 +02:00
// private function harvestQbEntityName(string $entity): string
// {
// return $this->entities[$entity];
// }
2024-08-26 01:49:22 +02:00
2024-08-26 00:24:51 +02:00
private function processEntitySync(string $entity, $records)
2024-08-21 05:22:46 +02:00
{
2024-08-26 00:24:51 +02:00
match($entity){
2024-08-30 06:45:16 +02:00
'client' => $this->syncQbToNinjaClients($records),
2024-08-26 08:14:54 +02:00
'product' => $this->syncQbToNinjaProducts($records),
2024-08-30 06:45:16 +02:00
'invoice' => $this->syncQbToNinjaInvoices($records),
2024-08-26 00:24:51 +02:00
// 'vendor' => $this->syncQbToNinjaClients($records),
// 'quote' => $this->syncInvoices($records),
// 'purchase_order' => $this->syncInvoices($records),
// 'payment' => $this->syncPayment($records),
2024-08-26 01:49:22 +02:00
default => false,
2024-08-26 00:24:51 +02:00
};
}
2024-08-26 07:48:48 +02:00
private function syncQbToNinjaInvoices($records): void
2024-08-26 00:24:51 +02:00
{
2024-08-26 07:48:48 +02:00
$invoice_transformer = new InvoiceTransformer($this->company);
foreach($records as $record)
{
$ninja_invoice_data = $invoice_transformer->qbToNinja($record);
$payment_ids = $ninja_invoice_data['payment_ids'] ?? [];
$client_id = $ninja_invoice_data['client_id'] ?? null;
if(is_null($client_id))
continue;
unset($ninja_invoice_data['payment_ids']);
if($invoice = $this->findInvoice($ninja_invoice_data))
{
$invoice->fill($ninja_invoice_data);
$invoice->saveQuietly();
$invoice = $invoice->calc()->getInvoice()->service()->markSent()->save();
foreach($payment_ids as $payment_id)
{
2024-08-26 12:17:51 +02:00
$payment = $this->qbs->sdk->FindById('Payment', $payment_id);
2024-08-26 07:48:48 +02:00
$payment_transformer = new PaymentTransformer($this->company);
$transformed = $payment_transformer->qbToNinja($payment);
$ninja_payment = $payment_transformer->buildPayment($payment);
$ninja_payment->service()->applyNumber()->save();
2024-08-26 01:49:22 +02:00
2024-08-26 07:48:48 +02:00
$paymentable = new \App\Models\Paymentable();
$paymentable->payment_id = $ninja_payment->id;
$paymentable->paymentable_id = $invoice->id;
$paymentable->paymentable_type = 'invoices';
$paymentable->amount = $transformed['applied'];
$paymentable->save();
$invoice->service()->applyPayment($ninja_payment, $transformed['applied']);
}
}
$ninja_invoice_data = false;
}
}
private function findInvoice(array $ninja_invoice_data): ?Invoice
{
$search = Invoice::query()
->withTrashed()
->where('company_id', $this->company->id)
->where('number', $ninja_invoice_data['number']);
if($search->count() == 0) {
//new invoice
$invoice = InvoiceFactory::create($this->company->id, $this->company->owner()->id);
$invoice->client_id = $ninja_invoice_data['client_id'];
return $invoice;
} elseif($search->count() == 1) {
return $this->settings['invoice']['update_record'] ? $search->first() : null;
}
return null;
}
private function syncQbToNinjaClients(array $records): void
{
$client_transformer = new ClientTransformer($this->company);
2024-08-26 01:49:22 +02:00
2024-08-26 00:24:51 +02:00
foreach($records as $record)
{
2024-08-26 01:49:22 +02:00
$ninja_client_data = $client_transformer->qbToNinja($record);
2024-08-26 00:24:51 +02:00
if($client = $this->findClient($ninja_client_data))
{
$client->fill($ninja_client_data[0]);
2024-08-26 01:49:22 +02:00
$client->saveQuietly();
$contact = $client->contacts()->where('email', $ninja_client_data[1]['email'])->first();
if(!$contact)
{
$contact = ClientContactFactory::create($this->company->id, $this->company->owner()->id);
$contact->client_id = $client->id;
$contact->send_email = true;
$contact->is_primary = true;
$contact->fill($ninja_client_data[1]);
$contact->saveQuietly();
}
elseif($this->updateGate('client')){
$contact->fill($ninja_client_data[1]);
$contact->saveQuietly();
}
2024-08-26 00:24:51 +02:00
}
}
}
2024-08-26 07:48:48 +02:00
private function syncQbToNinjaProducts($records): void
{
$product_transformer = new ProductTransformer($this->company);
foreach($records as $record)
{
$ninja_data = $product_transformer->qbToNinja($record);
2024-08-26 08:14:54 +02:00
if($product = $this->findProduct($ninja_data['hash']))
2024-08-26 07:48:48 +02:00
{
$product->fill($ninja_data);
$product->save();
}
}
}
2024-08-26 01:49:22 +02:00
private function findClient(array $qb_data) :?Client
2024-08-26 00:24:51 +02:00
{
$client = $qb_data[0];
$contact = $qb_data[1];
$client_meta = $qb_data[2];
$search = Client::query()
->withTrashed()
2024-08-26 01:49:22 +02:00
->where('company_id', $this->company->id)
2024-08-26 00:24:51 +02:00
->where(function ($q) use ($client, $client_meta, $contact){
$q->where('client_hash', $client_meta['client_hash'])
->orWhere('id_number', $client['id_number'])
->orWhereHas('contacts', function ($q) use ($contact){
$q->where('email', $contact['email']);
});
});
if($search->count() == 0) {
//new client
$client = ClientFactory::create($this->company->id, $this->company->owner()->id);
$client->client_hash = $client_meta['client_hash'];
$client->settings = $client_meta['settings'];
return $client;
}
elseif($search->count() == 1) {
2024-08-26 01:49:22 +02:00
return $this->settings['client']['update_record'] ? $search->first() : null;
2024-08-26 00:24:51 +02:00
}
2024-08-26 07:48:48 +02:00
return null;
}
private function findProduct(string $key): ?Product
{
$search = Product::query()
->withTrashed()
->where('company_id', $this->company->id)
2024-08-26 08:14:54 +02:00
->where('hash', $key);
2024-08-26 07:48:48 +02:00
if($search->count() == 0) {
//new product
$product = ProductFactory::create($this->company->id, $this->company->owner()->id);
2024-08-26 08:25:46 +02:00
$product->hash = $key;
2024-08-26 07:48:48 +02:00
return $product;
} elseif($search->count() == 1) {
return $this->settings['product']['update_record'] ? $search->first() : null;
2024-08-26 00:24:51 +02:00
}
2024-08-26 07:48:48 +02:00
return null;
2024-08-21 05:22:46 +02:00
}
2024-08-26 12:17:51 +02:00
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]);
}
2024-08-21 05:22:46 +02:00
}