From 43b95125f0832dcf99de37f57a54941c28bc45c7 Mon Sep 17 00:00:00 2001 From: karneaud Date: Sat, 17 Aug 2024 11:24:08 -0400 Subject: [PATCH] add logic for import payment entity --- app/Import/Providers/Quickbooks.php | 31 ++++-- .../Quickbooks/PaymentTransformer.php | 100 ++++++++++++++++++ .../Import/Quickbooks/PaymentRepository.php | 10 ++ app/Services/Import/Quickbooks/Service.php | 9 ++ 4 files changed, 144 insertions(+), 6 deletions(-) create mode 100644 app/Import/Transformer/Quickbooks/PaymentTransformer.php create mode 100644 app/Repositories/Import/Quickbooks/PaymentRepository.php diff --git a/app/Import/Providers/Quickbooks.php b/app/Import/Providers/Quickbooks.php index 60c64e039609..5bd968443b5c 100644 --- a/app/Import/Providers/Quickbooks.php +++ b/app/Import/Providers/Quickbooks.php @@ -15,16 +15,20 @@ use App\Models\Invoice; use App\Factory\ProductFactory; use App\Factory\ClientFactory; use App\Factory\InvoiceFactory; +use App\Factory\PaymentFactory; use Illuminate\Support\Facades\Cache; -use App\Http\Requests\Client\StoreClientRequest; -use App\Http\Requests\Product\StoreProductRequest; -use App\Http\Requests\Invoice\StoreInvoiceRequest; -use App\Import\Transformer\Quickbooks\ClientTransformer; -use App\Import\Transformer\Quickbooks\InvoiceTransformer; -use App\Import\Transformer\Quickbooks\ProductTransformer; use App\Repositories\ClientRepository; use App\Repositories\InvoiceRepository; use App\Repositories\ProductRepository; +use App\Repositories\PaymentRepository; +use App\Http\Requests\Client\StoreClientRequest; +use App\Http\Requests\Product\StoreProductRequest; +use App\Http\Requests\Invoice\StoreInvoiceRequest; +use App\Http\Requests\Payment\StorePaymentRequest; +use App\Import\Transformer\Quickbooks\ClientTransformer; +use App\Import\Transformer\Quickbooks\InvoiceTransformer; +use App\Import\Transformer\Quickbooks\ProductTransformer; +use App\Import\Transformer\Quickbooks\PaymentTransformer; class Quickbooks extends BaseImport { @@ -98,7 +102,22 @@ class Quickbooks extends BaseImport public function payment() { + $entity_type = 'payment'; + $data = $this->getData($entity_type); + if (empty($data)) { + $this->entity_count['payments'] = 0; + return; + } + + $this->request_name = StorePaymentRequest::class; + $this->repository_name = PaymentRepository::class; + $this->factory_name = PaymentFactory::class; + $this->repository = app()->make($this->repository_name); + $this->repository->import_mode = true; + $this->transformer = new PaymentTransformer($this->company); + $count = $this->ingest($data, $entity_type); + $this->entity_count['payments'] = $count; } public function invoice() diff --git a/app/Import/Transformer/Quickbooks/PaymentTransformer.php b/app/Import/Transformer/Quickbooks/PaymentTransformer.php new file mode 100644 index 000000000000..ff4b4f819b48 --- /dev/null +++ b/app/Import/Transformer/Quickbooks/PaymentTransformer.php @@ -0,0 +1,100 @@ + "PaymentRefNum", + 'amount' => "TotalAmt", + "client_id" => "CustomerRef", + "currency_id" => "CurrencyRef", + 'date' => "TxnDate", + "invoices" => "Line", + 'private_notes' => "PrivateNote", + 'created_at' => "CreateTime", + 'updated_at' => "LastUpdatedTime" + ]; + + public function __construct($company) + { + parent::__construct($company); + + $this->model = new Model; + } + + public function getTotalAmt($data, $field = null) { + return (float) $this->getString($data, $field); + } + + public function getTxnDate($data, $field = null) + { + return $this->parseDateOrNull($data, $field); + } + + public function getCustomerRef($data, $field = null ) + { + return $this->getClient($this->getString($data, 'CustomerRef.name'),null); + } + + public function getCurrencyRef($data, $field = null) + { + return $this->getCurrencyByCode($data['CurrencyRef'], 'value'); + } + + public function getLine($data, $field = null) + { + $invoices = []; + $invoice = $this->getString($data,'Line.LinkedTxn.TxnType'); + nlog(print_r([$invoice, $this->getString($data,'Line.LinkedTxn')], true)); + if(is_null($invoice) || $invoice !== 'Invoice') return $invoices; + if( is_null( ($invoice_id = $this->getInvoiceId($this->getString($data, 'Line.LinkedTxn.TxnId.value')))) ) return $invoices; + + return [[ + 'amount' => (float) $this->getString($data, 'Line.Amount'), + 'invoice_id' => $invoice_id + ]]; + } + + /** + * @param $invoice_number + * + * @return int|null + */ + public function getInvoiceId($invoice_number) + { + $invoice = Invoice::query()->where('company_id', $this->company->id) + ->where('is_deleted', false) + ->where("number", "LIKE", + "%-$invoice_number%", + ) + ->first(); + + return $invoice ? $invoice->id : null; + } + +} diff --git a/app/Repositories/Import/Quickbooks/PaymentRepository.php b/app/Repositories/Import/Quickbooks/PaymentRepository.php new file mode 100644 index 000000000000..86995fc18e34 --- /dev/null +++ b/app/Repositories/Import/Quickbooks/PaymentRepository.php @@ -0,0 +1,10 @@ +fetchRecords('Invoice', $max) ; } + /** + * fetch QuickBooks payment records + * @param int $max The maximum records to fetch. Default 100 + * @return Illuminate\Support\Collection; + */ + public function fetchPayments(int $max = 100): Collection + { + return $this->fetchRecords('Payment', $max) ; + } /** * fetch QuickBooks product records