From 4ceeb6a3178b0fdb7f0b41fb4d0892192f0dc279 Mon Sep 17 00:00:00 2001 From: Steve Ng Date: Mon, 16 Oct 2023 10:25:02 +0800 Subject: [PATCH] code for import receipts --- .../Services/ExportsInvoiceTransactions.php | 22 +++++-- .../ExportCustomersToExcelController.php | 4 +- .../ImportStatementReceiptsController.php | 64 ++++++++++++++++++- .../ImportedInvoiceMappedComponent.vue | 42 +++++++----- .../sections/TransactionsMappingComponent.vue | 29 ++++++--- 5 files changed, 124 insertions(+), 37 deletions(-) diff --git a/app/Classes/Modules/Exports/Services/ExportsInvoiceTransactions.php b/app/Classes/Modules/Exports/Services/ExportsInvoiceTransactions.php index 87cf35a2..79ca4380 100644 --- a/app/Classes/Modules/Exports/Services/ExportsInvoiceTransactions.php +++ b/app/Classes/Modules/Exports/Services/ExportsInvoiceTransactions.php @@ -33,12 +33,19 @@ class ExportsInvoiceTransactions implements FromQuery, WithHeadings, WithHeading public function headings(): array { - return [ - 'DocNo', - 'DocDate', - 'DebtorCode', - 'Ref', - 'DebtorName', + $header = []; + if ($this->request->input('type') == 'invoices') { + $header[] = 'DocNo'; + $header[] = 'DocDate'; + $header[] = 'DebtorCode'; + } else { + $header[] = 'OrNo'; + $header[] = 'OrDate'; + $header[] = 'CreditorCode'; + } + $header[] = 'Ref'; + $header[] = ($this->request->input('type') == 'invoices' ? 'DebtorName' : 'CreditorName'); + $header = array_merge($header, [ 'CurrencyCode', 'ShipInfo', 'ItemCode', @@ -48,7 +55,8 @@ class ExportsInvoiceTransactions implements FromQuery, WithHeadings, WithHeading 'UnitPrice', 'AccNo', 'DeptNo' - ]; + ]); + return $header; } /** diff --git a/app/Http/Controllers/Exports/ExportCustomersToExcelController.php b/app/Http/Controllers/Exports/ExportCustomersToExcelController.php index eebfeed7..7dfe0485 100644 --- a/app/Http/Controllers/Exports/ExportCustomersToExcelController.php +++ b/app/Http/Controllers/Exports/ExportCustomersToExcelController.php @@ -61,7 +61,7 @@ class ExportCustomersToExcelController public function invoiceTransactions(Request $request){ $exportsTransactions = new ExportsInvoiceTransactions($request); - $response = $exportsTransactions->download('invoice-transactions.xls', Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); + $response = $exportsTransactions->download(($request->input('type') == 'invoices' ? 'invoice-transactions' : 'receipt-transactions').'.xls', Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); ob_end_clean(); return $response; } @@ -79,7 +79,7 @@ class ExportCustomersToExcelController } public function importedInvoiceMapped(ExportsImportedInvoiceMappeds $exportsImportedInvoiceMappeds, Request $request) { - $response = $exportsImportedInvoiceMappeds->download('InvoiceMapped.xls', Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); + $response = $exportsImportedInvoiceMappeds->download($request->input('fileName').'.xls', Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); ob_end_clean(); return $response; } diff --git a/app/Http/Controllers/Imports/ImportStatementReceiptsController.php b/app/Http/Controllers/Imports/ImportStatementReceiptsController.php index 22111881..8021e3a9 100644 --- a/app/Http/Controllers/Imports/ImportStatementReceiptsController.php +++ b/app/Http/Controllers/Imports/ImportStatementReceiptsController.php @@ -17,9 +17,21 @@ use App\Classes\Modules\Companies\Processors\AssignSegmentProcessor; use App\Models\Company; use App\Models\SeasonalSegment; use App\Models\Transaction; +use App\Models\TransactionMappingLog; +use App\Classes\ValueObjects\Response\ApiResponseObject; +use App\Classes\ValueObjects\Constants\HttpStatus; +use Illuminate\Http\JsonResponse; class ImportStatementReceiptsController { + private $responseTitle; + private $responseMessage; + + public function __construct() { + $this->responseTitle = 'Import Receipt Mapping'; + $this->responseMessage = 'You have successfully imported receipt mapping'; + } + /** * @param Request $request * @return array @@ -27,6 +39,7 @@ class ImportStatementReceiptsController */ public function import(Request $request) { + $importDate = date('Y-m-d H:i:s'); $object = new DocumentObject('', $request->input('files'), '', ApprovalStatus::APPROVED, 'imports'); $file = json_decode($object->getFiles()[0])->file_info->original->file; @@ -35,10 +48,57 @@ class ImportStatementReceiptsController $excelRows = $import->rows; $excelRows = $excelRows->toArray(); + $data = []; foreach ($excelRows as $row) { - // if has date column - // $transactionDate = $this->changeExcelDate($row['date']); + $row['mapped_result_reference'] = null; + $row['mapped_status'] = 'failed'; + $row['date'] = $this->changeExcelDate($row['doc_date']); + + $returnReference = $this->mappingExchange($row); + if ($returnReference) { + $row['mapped_result_reference'] = $returnReference; + $row['mapped_status'] = 'success'; + } + + TransactionMappingLog::create([ + 'imported_date'=>$importDate, + 'data'=>$row, + ]); + array_push($data, $row); } + + return $this->response(['data'=>$data,'importedDate'=>$importDate]); + } + + public function response(?array $data = []) : JsonResponse { + return (new ApiResponseObject($this->responseTitle, + $this->responseMessage, + HttpStatus::OK_WITH_MESSAGE, $data))->handler(); + } + + private function mappingExchange(Array $row) { + $date = $row['date']; + $transactions = Transaction::where('original_amount', $row['local_payment_amount'])->whereRaw("DATE(created_at) = '$date'") + ->whereHas('issuerCompany', function($q) use($row) { + $q->where('debtor',$row['debtor_code']); + }) + ->get(); + + if ($transactions && $transactions->count() == 1) { + foreach ($transactions as $key => $transaction) { + return $this->updateTransactionOwnerReference($transaction, $row['doc_no']); + } + } + return false; + } + + public function updateTransactionOwnerReference($transaction, String $docNo) { + $transactionOwner = $transaction->transaction_owner; + if ($transactionOwner) { + $transactionOwner->update(['receipt_reference'=>$docNo]); + return $transactionOwner->owner_reference; + } + return false; } public function changeExcelDate($date) diff --git a/resources/assets/vue/components/accounting/sections/ImportedInvoiceMappedComponent.vue b/resources/assets/vue/components/accounting/sections/ImportedInvoiceMappedComponent.vue index a8df27f7..3e52b70b 100644 --- a/resources/assets/vue/components/accounting/sections/ImportedInvoiceMappedComponent.vue +++ b/resources/assets/vue/components/accounting/sections/ImportedInvoiceMappedComponent.vue @@ -7,7 +7,7 @@
-

Imported Invoice Mapped

+

{{ componentTitle }}