QB imports

This commit is contained in:
David Bomba 2024-09-19 20:12:56 +10:00
parent 6afd7a8970
commit 92ba61be51
4 changed files with 21 additions and 9 deletions

View File

@ -40,6 +40,7 @@ class QuickbooksSettings implements Castable
'client' => ['sync' => true, 'update_record' => true, 'direction' => 'bidirectional'], 'client' => ['sync' => true, 'update_record' => true, 'direction' => 'bidirectional'],
'vendor' => ['sync' => true, 'update_record' => true, 'direction' => 'bidirectional'], 'vendor' => ['sync' => true, 'update_record' => true, 'direction' => 'bidirectional'],
'invoice' => ['sync' => true, 'update_record' => true, 'direction' => 'bidirectional'], 'invoice' => ['sync' => true, 'update_record' => true, 'direction' => 'bidirectional'],
'sales' => ['sync' => true, 'update_record' => true, 'direction' => 'bidirectional'],
'quote' => ['sync' => true, 'update_record' => true, 'direction' => 'bidirectional'], 'quote' => ['sync' => true, 'update_record' => true, 'direction' => 'bidirectional'],
'purchase_order' => ['sync' => true, 'update_record' => true, 'direction' => 'bidirectional'], 'purchase_order' => ['sync' => true, 'update_record' => true, 'direction' => 'bidirectional'],
'product' => ['sync' => true, 'update_record' => true, 'direction' => 'bidirectional'], 'product' => ['sync' => true, 'update_record' => true, 'direction' => 'bidirectional'],

View File

@ -32,6 +32,7 @@ use App\Services\Quickbooks\Transformers\ClientTransformer;
use App\Services\Quickbooks\Transformers\InvoiceTransformer; use App\Services\Quickbooks\Transformers\InvoiceTransformer;
use App\Services\Quickbooks\Transformers\PaymentTransformer; use App\Services\Quickbooks\Transformers\PaymentTransformer;
use App\Services\Quickbooks\Transformers\ProductTransformer; use App\Services\Quickbooks\Transformers\ProductTransformer;
use QuickBooksOnline\API\Data\IPPSalesReceipt;
class QuickbooksSync implements ShouldQueue class QuickbooksSync implements ShouldQueue
{ {
@ -47,6 +48,7 @@ class QuickbooksSync implements ShouldQueue
'quote' => 'Estimate', 'quote' => 'Estimate',
'purchase_order' => 'PurchaseOrder', 'purchase_order' => 'PurchaseOrder',
'payment' => 'Payment', 'payment' => 'Payment',
'sales' => 'SalesReceipt',
]; ];
private QuickbooksService $qbs; private QuickbooksService $qbs;
@ -105,6 +107,7 @@ class QuickbooksSync implements ShouldQueue
'client' => $this->syncQbToNinjaClients($records), 'client' => $this->syncQbToNinjaClients($records),
'product' => $this->syncQbToNinjaProducts($records), 'product' => $this->syncQbToNinjaProducts($records),
'invoice' => $this->syncQbToNinjaInvoices($records), 'invoice' => $this->syncQbToNinjaInvoices($records),
'sales' => $this->syncQbToNinjaInvoices($records),
// 'vendor' => $this->syncQbToNinjaClients($records), // 'vendor' => $this->syncQbToNinjaClients($records),
// 'quote' => $this->syncInvoices($records), // 'quote' => $this->syncInvoices($records),
// 'purchase_order' => $this->syncInvoices($records), // 'purchase_order' => $this->syncInvoices($records),
@ -115,6 +118,7 @@ class QuickbooksSync implements ShouldQueue
private function syncQbToNinjaInvoices($records): void private function syncQbToNinjaInvoices($records): void
{ {
nlog("invoice sync ". count($records));
$invoice_transformer = new InvoiceTransformer($this->company); $invoice_transformer = new InvoiceTransformer($this->company);
foreach($records as $record) foreach($records as $record)
@ -164,6 +168,11 @@ class QuickbooksSync implements ShouldQueue
} }
if($record instanceof IPPSalesReceipt)
{
$invoice->service()->markPaid()->save();
}
} }
$ninja_invoice_data = false; $ninja_invoice_data = false;

View File

@ -21,7 +21,7 @@ class SdkWrapper
{ {
public const MAXRESULTS = 10000; public const MAXRESULTS = 10000;
private $entities = ['Customer','Invoice','Item']; private $entities = ['Customer','Invoice','Item','SalesReceipt'];
private OAuth2AccessToken $token; private OAuth2AccessToken $token;
@ -176,12 +176,18 @@ class SdkWrapper
$start = 0; $start = 0;
$limit = 1000; $limit = 1000;
try { try {
$total = $this->totalRecords($entity); $total = $this->totalRecords($entity);
$total = min($max, $total); $total = min($max, $total);
nlog("total = {$total}");
// Step 3 & 4: Get chunks of records until the total required records are retrieved // Step 3 & 4: Get chunks of records until the total required records are retrieved
do { do {
$limit = min(self::MAXRESULTS, $total - $start); $limit = min(self::MAXRESULTS, $total - $start);
nlog("limit = {$limit}");
$recordsChunk = $this->queryData("select * from $entity", $start, $limit); $recordsChunk = $this->queryData("select * from $entity", $start, $limit);
if(empty($recordsChunk)) { if(empty($recordsChunk)) {
break; break;
@ -189,6 +195,8 @@ class SdkWrapper
$records = array_merge($records, $recordsChunk); $records = array_merge($records, $recordsChunk);
$start += $limit; $start += $limit;
nlog("start = {$start}");
} while ($start < $total); } while ($start < $total);
if(empty($records)) { if(empty($records)) {
throw new \Exception("No records retrieved!"); throw new \Exception("No records retrieved!");
@ -198,7 +206,8 @@ class SdkWrapper
nlog("Fetch Quickbooks API Error: {$th->getMessage()}"); nlog("Fetch Quickbooks API Error: {$th->getMessage()}");
} }
nlog($records); // nlog($records);
return $records; return $records;
} }
} }

View File

@ -58,8 +58,6 @@ class InvoiceTransformer extends BaseTransformer
{ {
$payments = []; $payments = [];
nlog("get payments");
$qb_payments = data_get($qb_data, 'LinkedTxn', false); $qb_payments = data_get($qb_data, 'LinkedTxn', false);
if(!$qb_payments) { if(!$qb_payments) {
@ -67,7 +65,6 @@ class InvoiceTransformer extends BaseTransformer
} }
if(!is_array($qb_payments) && data_get($qb_payments, 'TxnType', false) == 'Payment'){ if(!is_array($qb_payments) && data_get($qb_payments, 'TxnType', false) == 'Payment'){
nlog([data_get($qb_payments, 'TxnId.value', false)]);
return [data_get($qb_payments, 'TxnId.value', false)]; return [data_get($qb_payments, 'TxnId.value', false)];
} }
@ -80,8 +77,6 @@ class InvoiceTransformer extends BaseTransformer
} }
} }
nlog($payments);
return $payments; return $payments;
} }
@ -102,8 +97,6 @@ class InvoiceTransformer extends BaseTransformer
$item->type_id = stripos(data_get($qb_item, 'ItemAccountRef.name') ?? '', 'Service') !== false ? '2' : '1'; $item->type_id = stripos(data_get($qb_item, 'ItemAccountRef.name') ?? '', 'Service') !== false ? '2' : '1';
$item->tax_id = data_get($qb_item, 'TaxCodeRef.value', '') == 'NON' ? Product::PRODUCT_TYPE_EXEMPT : $item->type_id; $item->tax_id = data_get($qb_item, 'TaxCodeRef.value', '') == 'NON' ? Product::PRODUCT_TYPE_EXEMPT : $item->type_id;
$item->tax_rate1 = data_get($qb_item, 'TxnTaxDetail.TaxLine.TaxLineDetail.TaxPercent', 0); $item->tax_rate1 = data_get($qb_item, 'TxnTaxDetail.TaxLine.TaxLineDetail.TaxPercent', 0);
// $item->tax_rate1 = data_get($qb_item, 'TaxLineDetail.TaxRateRef.TaxPercent', 0);
$item->tax_name1 = $item->tax_rate1 > 0 ? "Sales Tax" : ""; $item->tax_name1 = $item->tax_rate1 > 0 ? "Sales Tax" : "";
$items[] = (object)$item; $items[] = (object)$item;
} }