From 726ea70f7593f5a73591eb89818c067c3c7d87d8 Mon Sep 17 00:00:00 2001 From: edmondlang Date: Tue, 18 Jul 2023 12:54:32 +0800 Subject: [PATCH] update export invoice accounts --- .../ListShippingPortalTransactions.php | 3 +- .../Services/ExportsInvoiceTransactions.php | 62 +++++++++++-------- 2 files changed, 39 insertions(+), 26 deletions(-) diff --git a/app/Classes/Modules/Accounting/Processors/ListShippingPortalTransactions.php b/app/Classes/Modules/Accounting/Processors/ListShippingPortalTransactions.php index 5e0d4756..f80c3c0e 100644 --- a/app/Classes/Modules/Accounting/Processors/ListShippingPortalTransactions.php +++ b/app/Classes/Modules/Accounting/Processors/ListShippingPortalTransactions.php @@ -21,7 +21,8 @@ class ListShippingPortalTransactions return $data['payload']['data']; } catch (\Exception $exception) { - dd($exception->getMessage()); + // dd($exception->getMessage()); + throw new MalformedRequestException($exception->getMessage()); // preg_match('/\{.*\}/s', $exception->getMessage(), $matches); // $jsonError = json_decode($matches[0]); // Retrieved Transactions failed diff --git a/app/Classes/Modules/Exports/Services/ExportsInvoiceTransactions.php b/app/Classes/Modules/Exports/Services/ExportsInvoiceTransactions.php index 2474df77..eebcaad9 100644 --- a/app/Classes/Modules/Exports/Services/ExportsInvoiceTransactions.php +++ b/app/Classes/Modules/Exports/Services/ExportsInvoiceTransactions.php @@ -55,11 +55,9 @@ class ExportsInvoiceTransactions implements FromQuery, WithHeadings, WithHeading */ public function query() { - $query = StatementTransactionOwner::whereNull('invoice_reference') + return StatementTransactionOwner::whereNull('invoice_reference') ->whereIn('type', [StatementTransactionOwnerType::SALES, StatementTransactionOwnerType::WALLET_TOP_UP]) ->whereIn('status', [ApprovalStatus::COMPLETED, ApprovalStatus::APPROVED]); - - return $query; } /** @@ -69,20 +67,21 @@ class ExportsInvoiceTransactions implements FromQuery, WithHeadings, WithHeading */ public function map($transaction): array { - $row = null; - - $logArray = []; - $logArray['counter'] = $this->counter; + $logArray = [ + 'counter' => $this->counter, + 'system' => $transaction->system, + 'StatementTransactionOwner_id' => $transaction->id, + 'transaction_table_id' => $transaction->owner_id, + ]; $this->counter += 1; - $logArray['system'] = $transaction->system; - $logArray['StatementTransactionOwner_id'] = $transaction->id; $logArray = json_encode($logArray); - $filePath = storage_path('logs/exports_invoice_transactions.log'); + $filePath = storage_path('logs/exports_invoice_transactions.log'); + $errorFilePath = storage_path('logs/exports_invoice_transactions_error.log'); $textToAppend = Carbon::now()->format('[Y-m-d H:i:s]') . ' ' . $logArray . PHP_EOL; file_put_contents($filePath, $textToAppend, FILE_APPEND); - if($transaction->system == 'EXCHANGE') { + if ($transaction->system == 'EXCHANGE') { $row = (App()->make($transaction->owner_type))->where('id', $transaction->owner_id)->first(); $company = $row->type === TransactionType::PAYMENT ? $row->owner->company : $row->owner->owner; @@ -104,30 +103,43 @@ class ExportsInvoiceTransactions implements FromQuery, WithHeadings, WithHeading '500-0000', 'CIEF' ]; - } - else { + } else { $row = (App()->make(ListShippingPortalTransactions::class))->execute([ - 'id' => $transaction->owner_id, - 'with_company' => true, - ])[0]; + 'id' => $transaction->owner_id, + 'with_company' => true, + ]); + if (empty($row) || $row[0]['status'] != 'success') { + $textToAppend = Carbon::now()->format('[Y-m-d H:i:s]') . ' Fetch Shipping Transaction Fail ' . json_encode([ + 'id' => $transaction->owner_id, + 'with_company' => true, + ]) . PHP_EOL; + file_put_contents($errorFilePath, $textToAppend, FILE_APPEND); + + $textToAppend = Carbon::now()->format('[Y-m-d H:i:s]') . ' Shipping Portal Respnose ' . json_encode($row) . PHP_EOL; + file_put_contents($errorFilePath, $textToAppend, FILE_APPEND); + + return []; + } + + $row = $row[0]; return [ - '<>', + '<>', $row['updated_at'], $row['debtor_code'], $row['type'] === ShippingTransactionType::PAYMENT ? $row['order_reference'] : $row['marking'], '', - 'MYR', + 'MYR', $row['type'] === ShippingTransactionType::PAYMENT ? $row['order_reference'] : $row['bill_no'], - $row['type'] === ShippingTransactionType::PAYMENT ? '' : 'W1', - $row['type'] === ShippingTransactionType::PAYMENT ? 'PLEASE REFER TO THE ATTACHED APPENDIX REF `' . $row['order_reference'] : 'CREDIT SALES', + $row['type'] === ShippingTransactionType::PAYMENT ? '' : 'W1', + $row['type'] === ShippingTransactionType::PAYMENT ? 'PLEASE REFER TO THE ATTACHED APPENDIX REF `' . $row['order_reference'] : 'CREDIT SALES', '', - 1, - round($row['amount'], 2), - '500-0000', + 1, + round($row['amount'], 2), + '500-0000', 'CIEF' - ]; + ]; } } -} \ No newline at end of file +}