From 5dc6f395bd935b16a70b7ba57edc6c093aba3e40 Mon Sep 17 00:00:00 2001 From: Omair Saleh Date: Wed, 30 Aug 2023 11:49:29 +0800 Subject: [PATCH 1/6] show swift code in white form for usd --- app/Http/Resources/BankResource.php | 1 + .../bookings/forms/BookingPaymentQuotationComponent.vue | 2 +- resources/views/pages/pdfs/currency_vendor_order.blade.php | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/Http/Resources/BankResource.php b/app/Http/Resources/BankResource.php index 35fec923..f3de3038 100644 --- a/app/Http/Resources/BankResource.php +++ b/app/Http/Resources/BankResource.php @@ -21,6 +21,7 @@ class BankResource extends JsonResource 'reference' => $this->reference, 'bank_name' => $this->bank_name, 'bank_branch' => $this->bank_branch, + 'swift' => $this->swift, 'holder_name' => $this->holder_name, 'account_no' => $this->account_no, 'country_id' => $this->country_id, diff --git a/resources/assets/vue/components/bookings/forms/BookingPaymentQuotationComponent.vue b/resources/assets/vue/components/bookings/forms/BookingPaymentQuotationComponent.vue index 8171725d..b45ea1af 100644 --- a/resources/assets/vue/components/bookings/forms/BookingPaymentQuotationComponent.vue +++ b/resources/assets/vue/components/bookings/forms/BookingPaymentQuotationComponent.vue @@ -76,7 +76,7 @@
-
{{item.bank.bank_name}} ({{item.bank.bank_branch}})
+
{{item.bank.bank_name}} ({{item.bank.bank_branch}}) ({{item.bank.swift}})
diff --git a/resources/views/pages/pdfs/currency_vendor_order.blade.php b/resources/views/pages/pdfs/currency_vendor_order.blade.php index 1c92e205..987c42f2 100644 --- a/resources/views/pages/pdfs/currency_vendor_order.blade.php +++ b/resources/views/pages/pdfs/currency_vendor_order.blade.php @@ -42,7 +42,7 @@ {{$transaction->currency_rate}} {{$transaction->currency->short_code}} {{number_format((float)$transaction->amount, 2, '.', '')}} Account Holder Name: {{$transaction->owner->owner->bank->holder_name}}
{{$transaction->owner->owner->bank->bank_name}}: {{$transaction->owner->owner->bank->account_no}} -
Branch: {{$transaction->owner->owner->bank->bank_branch}}
Bank in Amount: {{$transaction->original_currency->short_code}} {{$transaction->original_amount}} +
Branch: {{$transaction->owner->owner->bank->bank_branch}}@if($transaction->original_currency->short_code === 'USD')
Swift Code: {{$transaction->owner->owner->bank->swift}}@endif
Bank in Amount: {{$transaction->original_currency->short_code}} {{$transaction->original_amount}} @endforeach From 85ca64ac392743e247d53d793af98355eb24dcd9 Mon Sep 17 00:00:00 2001 From: Omair Saleh Date: Fri, 1 Sep 2023 03:06:54 +0800 Subject: [PATCH 2/6] fix invoice bugs --- .../views/pages/pdfs/deliver_order.blade.php | 28 ++++++++++++----- resources/views/pages/pdfs/invoice.blade.php | 26 +++++++++++----- .../views/pages/pdfs/purchase_order.blade.php | 31 +++++++++++++------ 3 files changed, 61 insertions(+), 24 deletions(-) diff --git a/resources/views/pages/pdfs/deliver_order.blade.php b/resources/views/pages/pdfs/deliver_order.blade.php index 715244c7..e957c0f0 100644 --- a/resources/views/pages/pdfs/deliver_order.blade.php +++ b/resources/views/pages/pdfs/deliver_order.blade.php @@ -84,17 +84,22 @@ @php $subtotal = "0"; - $voucherDiscount = $voucher_redemption ? bcmul((string)$voucher_redemption->value, "-1", 2) : "0"; - $displayedSubtotal = 0; + $voucherDiscount = $voucher_redemption ? bcmul((string)$voucher_redemption->value, "-1", 5) : "0"; + $displayedSubtotal = "0"; + $exactTotal = "0"; @endphp @foreach ($po_order_transaction->transactionDetails as $key => $transaction_detail) @php $exactUnitPrice = bcdiv($transaction_detail->price, $transaction->currency_rate, 5); $itemTotal = bcmul($exactUnitPrice, $transaction_detail->quantity, 5); - $displayedItemTotal = bcmul($exactUnitPrice, $transaction_detail->quantity, 2); + + // Round half to even for displayed item total + $displayedItemTotal = round(bcmul($exactUnitPrice, $transaction_detail->quantity, 2), 2, PHP_ROUND_HALF_EVEN); + $displayedSubtotal = bcadd($displayedSubtotal, $displayedItemTotal, 2); - $subtotal = bcadd($subtotal, $displayedItemTotal, 2); + $subtotal = bcadd($subtotal, $itemTotal, 5); + $exactTotal = bcadd($exactTotal, $displayedItemTotal, 5); @endphp {{ $key + 1 }} @@ -112,7 +117,7 @@ @php - $subtotalWithDiscount = bcsub($subtotal, $voucherDiscount, 5); // Use bcsub to subtract + $subtotalWithDiscount = bcsub($subtotal, $voucherDiscount, 5); @endphp @@ -141,10 +146,17 @@ @endif @php - $displayedTotal = bcadd(bcadd(bcadd($displayedSubtotal, $transaction->service_charge, 2), $transaction->tax, 2), $voucherDiscount, 2); + // Calculate the totals with 5 decimal places $expectedTotal = bcadd(bcadd(bcadd($subtotal, $transaction->service_charge, 5), $transaction->tax, 5), $voucherDiscount, 5); - $discrepancy = bcsub($displayedTotal, $expectedTotal, 5); - $total = bcadd(bcadd(bcadd($subtotal, $transaction->service_charge, 5), $transaction->tax, 5), $voucherDiscount, 5); // Keep precision + + // Calculate the displayed totals with 2 decimal places + $displayedTotal = bcadd(bcadd(bcadd($displayedSubtotal, $transaction->service_charge, 2), $transaction->tax, 2), $voucherDiscount, 2); + + // Calculate the discrepancy + $discrepancy = bcsub($expectedTotal, $displayedTotal, 5); + + // Calculate the final total + $total = bcadd($expectedTotal, $discrepancy, 5); @endphp diff --git a/resources/views/pages/pdfs/invoice.blade.php b/resources/views/pages/pdfs/invoice.blade.php index db6c42c5..40706ce0 100644 --- a/resources/views/pages/pdfs/invoice.blade.php +++ b/resources/views/pages/pdfs/invoice.blade.php @@ -73,17 +73,22 @@ @php $subtotal = "0"; - $voucherDiscount = $voucher_redemption ? bcmul((string)$voucher_redemption->value, "-1", 2) : "0"; - $displayedSubtotal = 0; + $voucherDiscount = $voucher_redemption ? bcmul((string)$voucher_redemption->value, "-1", 5) : "0"; + $displayedSubtotal = "0"; + $exactTotal = "0"; @endphp @foreach ($po_order_transaction->transactionDetails as $key => $transaction_detail) @php - $exactUnitPrice = bcdiv($transaction_detail->price, $transaction->currency_rate, 7); + $exactUnitPrice = bcdiv($transaction_detail->price, $transaction->currency_rate, 5); $itemTotal = bcmul($exactUnitPrice, $transaction_detail->quantity, 5); - $displayedItemTotal = bcmul($exactUnitPrice, $transaction_detail->quantity, 2); + + // Round half to even for displayed item total + $displayedItemTotal = round(bcmul($exactUnitPrice, $transaction_detail->quantity, 2), 2, PHP_ROUND_HALF_EVEN); + $displayedSubtotal = bcadd($displayedSubtotal, $displayedItemTotal, 2); $subtotal = bcadd($subtotal, $itemTotal, 5); + $exactTotal = bcadd($exactTotal, $displayedItemTotal, 5); @endphp {{ $key + 1 }} @@ -130,10 +135,17 @@ @endif @php - $displayedTotal = bcadd(bcadd(bcadd($subtotal, $transaction->service_charge, 5), $transaction->tax, 5), $voucherDiscount, 5); + // Calculate the totals with 5 decimal places $expectedTotal = bcadd(bcadd(bcadd($subtotal, $transaction->service_charge, 5), $transaction->tax, 5), $voucherDiscount, 5); - $discrepancy = bcsub($displayedTotal, $expectedTotal, 5); - $total = bcadd(bcadd(bcadd($subtotal, $transaction->service_charge, 5), $transaction->tax, 5), $voucherDiscount, 5); + + // Calculate the displayed totals with 2 decimal places + $displayedTotal = bcadd(bcadd(bcadd($displayedSubtotal, $transaction->service_charge, 2), $transaction->tax, 2), $voucherDiscount, 2); + + // Calculate the discrepancy + $discrepancy = bcsub($expectedTotal, $displayedTotal, 5); + + // Calculate the final total + $total = bcadd($expectedTotal, $discrepancy, 5); @endphp diff --git a/resources/views/pages/pdfs/purchase_order.blade.php b/resources/views/pages/pdfs/purchase_order.blade.php index e0c9582f..3e79691a 100644 --- a/resources/views/pages/pdfs/purchase_order.blade.php +++ b/resources/views/pages/pdfs/purchase_order.blade.php @@ -91,15 +91,22 @@ @php $subtotal = "0"; - $displayedSubtotal = 0; + $voucherDiscount = $voucher_redemption ? bcmul((string)$voucher_redemption->value, "-1", 5) : "0"; + $displayedSubtotal = "0"; + $exactTotal = "0"; @endphp @foreach ($po_order_transaction->transactionDetails as $key => $transaction_detail) @php $exactUnitPrice = bcdiv($transaction_detail->price, $transaction->currency_rate, 5); $itemTotal = bcmul($exactUnitPrice, $transaction_detail->quantity, 5); - $displayedItemTotal = bcmul($exactUnitPrice, $transaction_detail->quantity, 2); + + // Round half to even for displayed item total + $displayedItemTotal = round(bcmul($exactUnitPrice, $transaction_detail->quantity, 2), 2, PHP_ROUND_HALF_EVEN); + $displayedSubtotal = bcadd($displayedSubtotal, $displayedItemTotal, 2); + $subtotal = bcadd($subtotal, $itemTotal, 5); + $exactTotal = bcadd($exactTotal, $displayedItemTotal, 5); @endphp {{ $key + 1 }} @@ -117,13 +124,7 @@ @php - $voucherDiscount = $voucher_redemption ? bcmul((string)$voucher_redemption->value, "-1", 2) : "0"; - $subtotalWithDiscount = bcsub($subtotal, $voucherDiscount, 5); // Use bcsub to subtract - $displayedTotal = bcadd(bcadd(bcadd($displayedSubtotal, $transaction->service_charge, 2), $transaction->tax, 2), $voucherDiscount, 2); - $expectedTotal = bcadd(bcadd(bcadd($subtotal, $transaction->service_charge, 5), $transaction->tax, 5), $voucherDiscount, 5); - $discrepancy = bcsub($displayedTotal, $expectedTotal, 5); - $total = bcadd(bcadd(bcadd($subtotal, $transaction->service_charge, 5), $transaction->tax, 5), $voucherDiscount, 5); // Keep precision - $subtotal = bcadd($subtotal, $displayedItemTotal, 2); + $subtotalWithDiscount = bcsub($subtotal, $voucherDiscount, 5); @endphp @@ -151,7 +152,19 @@ {{ number_format($transaction->tax, 2) }} @endif + @php + // Calculate the totals with 5 decimal places + $expectedTotal = bcadd(bcadd(bcadd($subtotal, $transaction->service_charge, 5), $transaction->tax, 5), $voucherDiscount, 5); + // Calculate the displayed totals with 2 decimal places + $displayedTotal = bcadd(bcadd(bcadd($displayedSubtotal, $transaction->service_charge, 2), $transaction->tax, 2), $voucherDiscount, 2); + + // Calculate the discrepancy + $discrepancy = bcsub($expectedTotal, $displayedTotal, 5); + + // Calculate the final total + $total = bcadd($expectedTotal, $discrepancy, 5); + @endphp Adjustment From 39d8ed2e9e102b60766148c7bf5b96fdfdc4ed7b Mon Sep 17 00:00:00 2001 From: Omair Saleh Date: Fri, 1 Sep 2023 04:32:37 +0800 Subject: [PATCH 3/6] fix invoice bugs --- .../GeneratesTransactionBillNumber.php | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/app/Classes/Modules/Transactions/Services/GeneratesTransactionBillNumber.php b/app/Classes/Modules/Transactions/Services/GeneratesTransactionBillNumber.php index 4505707e..943da696 100644 --- a/app/Classes/Modules/Transactions/Services/GeneratesTransactionBillNumber.php +++ b/app/Classes/Modules/Transactions/Services/GeneratesTransactionBillNumber.php @@ -3,6 +3,7 @@ namespace App\Classes\Modules\Transactions\Services; +use App\Classes\Exceptions\InternalServerErrorException; use Carbon\Carbon; class GeneratesTransactionBillNumber @@ -27,14 +28,21 @@ class GeneratesTransactionBillNumber * @return string */ public function execute(string $prefix, ?Carbon $date = null): string { - if(!$date){ - $date = carbon::now(); + if (!$date) { + $date = Carbon::now(); } - $billNumber = $prefix.$date->format('Y').$date->format('m').'-'.mt_rand(10000, 99999); - - return !$this->checksIfTransactionBillNumberExists->execute($billNumber) ? $billNumber : self::execute($prefix); + $attempt = 0; + while ($attempt < 10) { // Retry up to 10 times + $billNumber = $prefix . $date->format('Y') . $date->format('m') . '-' . microtime(true); + if (!$this->checksIfTransactionBillNumberExists->execute($billNumber)) { + return $billNumber; + } + $attempt++; + } + throw new InternalServerErrorException("Unable to generate unique bill number after {$attempt} attempts."); } -} \ No newline at end of file + +} From f2c008239b82563ba23ebc210af7f840bb054b1d Mon Sep 17 00:00:00 2001 From: Omair Saleh Date: Fri, 1 Sep 2023 05:15:41 +0800 Subject: [PATCH 4/6] fix invoice bugs --- routes/web.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routes/web.php b/routes/web.php index 5cc38fbd..7980b80a 100644 --- a/routes/web.php +++ b/routes/web.php @@ -121,7 +121,7 @@ Route::get('/transfer/{marking}/latest-invoice', function ($marking) { $transaction = $booking->transactions() ->where('type', TransactionType::PAYMENT) ->latest()->get()[0]; - + $supplier = Company::where('id', $transaction->receiver)->first(); $lowercaseDocumentType = strtolower(DocumentType::INVOICE); @@ -486,7 +486,7 @@ Route::get('/payments/manual', function(){ Route::get('/invoice/fix', function(){ - set_time_limit(1800); + set_time_limit(14400); $bookings = Booking::where('status', ApprovalStatus::COMPLETED)->whereDate('updated_at', '>=', Carbon::parse('01-01-2023'))->get(); foreach($bookings as $booking){ From 425dbef07241633998445906f89885cb3a73160b Mon Sep 17 00:00:00 2001 From: JiaSheng Date: Sat, 2 Sep 2023 19:36:31 +0800 Subject: [PATCH 5/6] fix a lot of transaction goes into unknown --- ...ankStatementTransactionOwnersProcessor.php | 82 ++++++++++++++++--- 1 file changed, 70 insertions(+), 12 deletions(-) diff --git a/app/Classes/Modules/Accounting/Processors/CreateBankStatementTransactionOwnersProcessor.php b/app/Classes/Modules/Accounting/Processors/CreateBankStatementTransactionOwnersProcessor.php index 90dd7048..a3c876d7 100644 --- a/app/Classes/Modules/Accounting/Processors/CreateBankStatementTransactionOwnersProcessor.php +++ b/app/Classes/Modules/Accounting/Processors/CreateBankStatementTransactionOwnersProcessor.php @@ -30,11 +30,12 @@ class CreateBankStatementTransactionOwnersProcessor // $transactions = StatementTransaction::whereDoesntHave('owners')->where('amount', '<', 0)->get(); foreach ($transactions as $transaction) { + $keywords = array_filter(explode(" ", $transaction->transaction_description . " " . $transaction->transaction_description_2)); if($transaction->amount > 0){ // Exchange Sales - $creditTransactions = $this->getTransactions($transaction->posting_date, $transaction->amount, TransactionType::PAYMENT, Booking::class, PaymentMethodType::WALLET, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + $creditTransactions = $this->getTransactions($transaction->posting_date, $transaction->amount, TransactionType::PAYMENT, Booking::class, PaymentMethodType::WALLET, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED], $keywords); foreach ($creditTransactions as $creditTransaction) { $transaction->owners()->firstOrCreate([ 'type' => StatementTransactionOwnerType::SALES, @@ -47,7 +48,7 @@ class CreateBankStatementTransactionOwnersProcessor // Shipping Portal Sales - $creditTransactions = $this->getTransactionsFromShippingPortal($transaction->amount, $this->getDateRange($transaction->posting_date), 2); + $creditTransactions = $this->getTransactionsFromShippingPortal($transaction->amount, $this->getDateRange($transaction->posting_date, 1), 2); foreach ($creditTransactions as $creditTransaction) { if($creditTransaction['owner_type'] === Wallet::class) continue; $transaction->owners()->firstOrCreate([ @@ -60,7 +61,7 @@ class CreateBankStatementTransactionOwnersProcessor } // Exchange Wallet Top Up - $creditTransactions = $this->getTransactions($transaction->posting_date, $transaction->amount, TransactionType::TOP_UP, Wallet::class, null, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + $creditTransactions = $this->getTransactions($transaction->posting_date, $transaction->amount, TransactionType::TOP_UP, Wallet::class, null, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED], $keywords); foreach ($creditTransactions as $creditTransaction) { $transaction->owners()->firstOrCreate([ 'type' => StatementTransactionOwnerType::WALLET_TOP_UP, @@ -71,7 +72,7 @@ class CreateBankStatementTransactionOwnersProcessor ]); } - $creditTransactions = $this->getTransactionsFromShippingPortal($transaction->amount, $this->getDateRange($transaction->posting_date), 5); + $creditTransactions = $this->getTransactionsFromShippingPortal($transaction->amount, $this->getDateRange($transaction->posting_date, 1), 5); foreach ($creditTransactions as $creditTransaction) { $transaction->owners()->firstOrCreate([ 'type' => StatementTransactionOwnerType::WALLET_TOP_UP, @@ -131,7 +132,7 @@ class CreateBankStatementTransactionOwnersProcessor } // Exchange Wallet Withdrawal - $debitTransactions = $this->getTransactions($transaction->posting_date, $transaction->amount, TransactionType::DEBIT_NOTE, Wallet::class, null, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + $debitTransactions = $this->getTransactions($transaction->posting_date, $transaction->amount, TransactionType::DEBIT_NOTE, Wallet::class, null, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED], $keywords); foreach ($debitTransactions as $debitTransaction) { $transaction->owners()->firstOrCreate([ 'type' => StatementTransactionOwnerType::WALLET_WITHDRAWAL, @@ -189,7 +190,8 @@ class CreateBankStatementTransactionOwnersProcessor } } - private function getTransactions($date, $amount, $type, $ownerType, $paymentMethod, $statuses, $model = Transaction::class) { + private function getTransactions($date, $amount, $type, $ownerType, $paymentMethod, $statuses, $keywords, $model = Transaction::class) { + $dateRange = $this->getDateRange($date, 4); $query = $model::whereIn('status', $statuses) ->where(function ($query) use ($ownerType, $paymentMethod, $type) { if ($ownerType) { @@ -204,9 +206,65 @@ class CreateBankStatementTransactionOwnersProcessor $query->where('type', $type); } }) - ->whereDate('created_at', $date->format('Y-m-d')) - ->where('amount', '>', ($amount - 0.01)) - ->where('amount', '<', ($amount + 0.01)); + ->whereDate('created_at', '>=', $dateRange['start_date']) + ->whereDate('created_at', '<=', $dateRange['end_date']) + ->where('amount', '>', ($amount - 0.05)) + ->where('amount', '<', ($amount + 0.05)); + + if ($query->count() > 1) { + $transactions = clone $query; + + $transactions->whereDate('created_at', $date->format('Y-m-d')); + + if ($transactions->count() === 1) { + return $transactions->get(); + } + + $transactions = clone $query; + + if ($ownerType === Booking::class) { + $transactions = $transactions->whereHasMorph('owner', [Booking::class], function ($q1) use ($keywords) { + return $q1->whereHas('company', function ($company) use ($keywords) { + return $company->where(function ($q2) use ($keywords) { + foreach($keywords as $keyword) { + $q2->orWhere('name', 'LIKE', '%'.$keyword.'%'); + } + $q2->orWhereHas('contacts', function ($contact) use($keywords) { + return $contact->where(function ($q3) use($keywords) { + foreach($keywords as $keyword) { + $q3->orWhere('reference', 'LIKE', '%'.$keyword.'%'); + } + }); + }); + }); + }); + }); + } + + + if ($ownerType === Wallet::class) { + $transactions = $transactions->whereHasMorph('owner', [Wallet::class], function ($q1) use ($keywords) { + return $q1->whereHasMorph('owner', [Company::class], function ($company) use($keywords) { + return $company->where(function ($q2) use ($keywords) { + foreach($keywords as $keyword) { + $q2->orWhere('name', 'LIKE', '%'.$keyword.'%'); + } + $q2->orWhereHas('contacts', function ($contact) use($keywords) { + return $contact->where(function ($q3) use($keywords) { + foreach($keywords as $keyword) { + $q3->orWhere('reference', 'LIKE', '%'.$keyword.'%'); + } + }); + }); + }); + }); + }); + } + + if ($transactions->count() === 1) { + return $transactions->get(); + } + } return $query->get(); } @@ -237,15 +295,15 @@ class CreateBankStatementTransactionOwnersProcessor } } - private function getDateRange(string $dateStr) { + private function getDateRange(string $dateStr, int $range) { // Create a DateTime object from the input string $date = strtotime($dateStr); // Get the first day of the month - $today = date('Y-m-d', strtotime('-1 day', $date)); + $today = date('Y-m-d', strtotime('-' . $range . ' day', $date)); // Get the first day of the next month - $nextDay = date('Y-m-d', strtotime('+1 day', $date)); + $nextDay = date('Y-m-d', strtotime('+' . $range . ' day', $date)); return [ 'start_date' => $today, From d8ff540f0dd532127287ec1f0e9f0240918182da Mon Sep 17 00:00:00 2001 From: edmondlang Date: Tue, 5 Sep 2023 11:52:45 +0800 Subject: [PATCH 6/6] fix refund booking --- app/Console/Commands/DeleteOrderCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Console/Commands/DeleteOrderCommand.php b/app/Console/Commands/DeleteOrderCommand.php index 849a0106..9bcefe47 100644 --- a/app/Console/Commands/DeleteOrderCommand.php +++ b/app/Console/Commands/DeleteOrderCommand.php @@ -41,8 +41,8 @@ class DeleteOrderCommand extends Command */ public function handle() { - // $bookings_reference = $this->argument('bookings_reference'); - $bookings_reference = '28546,38599,44487,71086,70133,58580,42831,96028,41188,33894,95877,86732,31894,50962,44215,92894,40968,30303,89762,74693,45271,27169'; + $bookings_reference = $this->argument('bookings_reference'); + // $bookings_reference = '28546,38599,44487,71086,70133,58580,42831,96028,41188,33894,95877,86732,31894,50962,44215,92894,40968,30303,89762,74693,45271,27169'; $bookings_reference = explode(',', $bookings_reference); $start = new Carbon();