From 174a02b52a68dd9e9ff3b4259a5a5ca158ce3738 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 24 Sep 2024 07:22:53 +1000 Subject: [PATCH] QB SYNC --- app/Livewire/BillingPortalPurchasev2.php | 40 ++++---- .../CBAPowerBoard/CreditCard.php | 1 - .../EDocument/Gateway/Qvalia/Partner.php | 4 +- .../Quickbooks/Jobs/QuickbooksImport.php | 88 +---------------- app/Services/Quickbooks/Models/QbInvoice.php | 97 ++++++++++++++++++- 5 files changed, 119 insertions(+), 111 deletions(-) diff --git a/app/Livewire/BillingPortalPurchasev2.php b/app/Livewire/BillingPortalPurchasev2.php index cf03d07f75..0d365261a3 100644 --- a/app/Livewire/BillingPortalPurchasev2.php +++ b/app/Livewire/BillingPortalPurchasev2.php @@ -431,31 +431,31 @@ class BillingPortalPurchasev2 extends Component * @throws PresenterException * @throws InvalidArgumentException */ - private function createClientContact() - { - $company = $this->subscription->company; - $user = $this->subscription->user; - $user->setCompany($company); + // private function createClientContact() + // { + // $company = $this->subscription->company; + // $user = $this->subscription->user; + // $user->setCompany($company); - $client_repo = new ClientRepository(new ClientContactRepository()); - $data = [ - 'name' => '', - 'group_settings_id' => $this->subscription->group_id, - 'contacts' => [ - ['email' => $this->email], - ], - 'client_hash' => Str::random(40), - 'settings' => ClientSettings::defaults(), - ]; + // $client_repo = new ClientRepository(new ClientContactRepository()); + // $data = [ + // 'name' => '', + // 'group_settings_id' => $this->subscription->group_id, + // 'contacts' => [ + // ['email' => $this->email], + // ], + // 'client_hash' => Str::random(40), + // 'settings' => ClientSettings::defaults(), + // ]; - $client = $client_repo->save($data, ClientFactory::create($company->id, $user->id)); + // $client = $client_repo->save($data, ClientFactory::create($company->id, $user->id)); - $this->contact = $client->fresh()->contacts()->first(); + // $this->contact = $client->fresh()->contacts()->first(); - Auth::guard('contact')->loginUsingId($this->contact->id, true); + // Auth::guard('contact')->loginUsingId($this->contact->id, true); - return $this; - } + // return $this; + // } /** diff --git a/app/PaymentDrivers/CBAPowerBoard/CreditCard.php b/app/PaymentDrivers/CBAPowerBoard/CreditCard.php index aa0c6e8037..f7b9248620 100644 --- a/app/PaymentDrivers/CBAPowerBoard/CreditCard.php +++ b/app/PaymentDrivers/CBAPowerBoard/CreditCard.php @@ -451,7 +451,6 @@ class CreditCard implements LivewireMethodInterface match($error_object->error->code) { "UnfulfilledCondition" => $error_message = $error_object->error->details->messages[0] ?? $error_object->error->message ?? "Unknown error", "GatewayError" => $error_message = $error_object->error->message, - "UnfulfilledCondition" => $error_message = $error_object->error->message, "transaction_declined" => $error_message = $error_object->error->details[0]->status_code_description, default => $error_message = $error_object->error->message ?? "Unknown error", }; diff --git a/app/Services/EDocument/Gateway/Qvalia/Partner.php b/app/Services/EDocument/Gateway/Qvalia/Partner.php index 4cb82f65f5..e16f332172 100644 --- a/app/Services/EDocument/Gateway/Qvalia/Partner.php +++ b/app/Services/EDocument/Gateway/Qvalia/Partner.php @@ -107,7 +107,7 @@ class Partner { $uri = "/partner/{$this->partner_number}/account/{$accountRegNo}"; - return $this->qvalia->httpClient($uri, (\App\Enum\HttpVerb::DELETE)->value)->object(); + return $this->qvalia->httpClient($uri, (\App\Enum\HttpVerb::DELETE)->value),[])->object(); } /** @@ -138,7 +138,7 @@ class Partner { $uri = "/partner/{$this->partner_number}/account/{$accountRegNo}/peppol/{$peppolId}"; - return $this->qvalia->httpClient($uri, (\App\Enum\HttpVerb::DELETE)->value)->object(); + return $this->qvalia->httpClient($uri, (\App\Enum\HttpVerb::DELETE)->value), [])->object(); } } diff --git a/app/Services/Quickbooks/Jobs/QuickbooksImport.php b/app/Services/Quickbooks/Jobs/QuickbooksImport.php index a6ff0a385a..9901d388ec 100644 --- a/app/Services/Quickbooks/Jobs/QuickbooksImport.php +++ b/app/Services/Quickbooks/Jobs/QuickbooksImport.php @@ -109,8 +109,8 @@ class QuickbooksImport implements ShouldQueue match($entity){ 'client' => $this->qbs->client->syncToNinja($records), 'product' => $this->qbs->product->syncToNinja($records), - // 'invoice' => $this->syncQbToNinjaInvoices($records), - // 'sales' => $this->syncQbToNinjaInvoices($records), + 'invoice' => $this->qbs->invoice->syncToNinja($records), + 'sales' => $this->qbs->invoice->syncToNinja($records), // 'vendor' => $this->syncQbToNinjaVendors($records), // 'quote' => $this->syncInvoices($records), // 'expense' => $this->syncQbToNinjaExpenses($records), @@ -122,91 +122,11 @@ class QuickbooksImport implements ShouldQueue private function syncQbToNinjaInvoices($records): void { - nlog("invoice sync ". count($records)); - $invoice_transformer = new InvoiceTransformer($this->company); - - foreach($records as $record) - { - nlog($record); - - $ninja_invoice_data = $invoice_transformer->qbToNinja($record); - - nlog($ninja_invoice_data); - - $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()->createInvitations()->save(); - - foreach($payment_ids as $payment_id) - { - - $payment = $this->qbs->sdk->FindById('Payment', $payment_id); - - $payment_transformer = new PaymentTransformer($this->company); - - $transformed = $payment_transformer->qbToNinja($payment); - - $ninja_payment = $payment_transformer->buildPayment($payment); - $ninja_payment->service()->applyNumber()->save(); - - $paymentable = new \App\Models\Paymentable(); - $paymentable->payment_id = $ninja_payment->id; - $paymentable->paymentable_id = $invoice->id; - $paymentable->paymentable_type = 'invoices'; - $paymentable->amount = $transformed['applied'] + $ninja_payment->credits->sum('amount'); - $paymentable->created_at = $ninja_payment->date; //@phpstan-ignore-line - $paymentable->save(); - - $invoice->service()->applyPayment($ninja_payment, $paymentable->amount); - - } - - if($record instanceof IPPSalesReceipt) - { - $invoice->service()->markPaid()->save(); - } - - } - - $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']); - ->where('sync->qb_id', $ninja_invoice_data['id']); - - 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 syncQbToNinjaVendors(array $records): void { diff --git a/app/Services/Quickbooks/Models/QbInvoice.php b/app/Services/Quickbooks/Models/QbInvoice.php index c4ce204676..9f3ec1786c 100644 --- a/app/Services/Quickbooks/Models/QbInvoice.php +++ b/app/Services/Quickbooks/Models/QbInvoice.php @@ -11,20 +11,109 @@ namespace App\Services\Quickbooks\Models; - +use App\Models\Invoice; +use App\Factory\InvoiceFactory; +use App\Interfaces\SyncInterface; use App\Services\Quickbooks\QuickbooksService; +use App\Services\Quickbooks\Transformers\InvoiceTransformer; +use App\Services\Quickbooks\Transformers\PaymentTransformer; -class QbInvoice +class QbInvoice implements SyncInterface { - + protected InvoiceTransformer $transformer; + public function __construct(public QuickbooksService $service) { + $this->transformer = new InvoiceTransformer($this->service->company); } - public function find(int $id) + public function find(int $id): mixed { return $this->service->sdk->FindById('Invoice', $id); } + public function syncToNinja(array $records): void + { + + foreach ($records as $record) { + + $ninja_invoice_data = $this->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()->createInvitations()->save(); + + foreach ($payment_ids as $payment_id) { + + $payment = $this->service->sdk->FindById('Payment', $payment_id); + + $payment_transformer = new PaymentTransformer($this->service->company); + + $transformed = $payment_transformer->qbToNinja($payment); + + $ninja_payment = $payment_transformer->buildPayment($payment); + $ninja_payment->service()->applyNumber()->save(); + + $paymentable = new \App\Models\Paymentable(); + $paymentable->payment_id = $ninja_payment->id; + $paymentable->paymentable_id = $invoice->id; + $paymentable->paymentable_type = 'invoices'; + $paymentable->amount = $transformed['applied'] + $ninja_payment->credits->sum('amount'); + $paymentable->created_at = $ninja_payment->date; //@phpstan-ignore-line + $paymentable->save(); + + $invoice->service()->applyPayment($ninja_payment, $paymentable->amount); + + } + + if ($record instanceof IPPSalesReceipt) { + $invoice->service()->markPaid()->save(); + } + + } + + $ninja_invoice_data = false; + + } + + } + + public function syncToForeign(array $records): void + { + + } + + + private function findInvoice(array $ninja_invoice_data): ?Invoice + { + $search = Invoice::query() + ->withTrashed() + ->where('company_id', $this->service->company->id) + ->where('sync->qb_id', $ninja_invoice_data['id']); + + if($search->count() == 0) { + $invoice = InvoiceFactory::create($this->service->company->id, $this->service->company->owner()->id); + $invoice->client_id = $ninja_invoice_data['client_id']; + + return $invoice; + } elseif($search->count() == 1) { + return $this->service->settings->invoice->update_record ? $search->first() : null; + } + + return null; + + } }