diff --git a/app/Classes/Jobs/GenerateInvoice.php b/app/Classes/Jobs/GenerateInvoice.php new file mode 100644 index 00000000..83191e25 --- /dev/null +++ b/app/Classes/Jobs/GenerateInvoice.php @@ -0,0 +1,43 @@ +booking = $booking; + } + + + public function handle() + { + $this->booking->transactions()->whereIn('transactions.type', [TransactionType::INVOICE, TransactionType::SUPPLIER_DELIVER])->delete(); + $this->booking->documents()->whereIn('document_type', [DocumentType::INVOICE, DocumentType::PURCHASE_ORDER, DocumentType::DELIVER_ORDER, DocumentType::SUPPLIER_DELIVER_ORDER])->delete(); + $this->booking->status = ApprovalStatus::APPROVED; + $this->booking->save(); + + (App()->make(CreateInvoiceTransactionProcessor::class))->execute($this->booking); + } +} diff --git a/app/Classes/Modules/Transactions/ControllersLogic/FetchCompanyTransactionStatementLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/FetchCompanyTransactionStatementLogic.php index c16ac453..a078a01d 100644 --- a/app/Classes/Modules/Transactions/ControllersLogic/FetchCompanyTransactionStatementLogic.php +++ b/app/Classes/Modules/Transactions/ControllersLogic/FetchCompanyTransactionStatementLogic.php @@ -55,6 +55,15 @@ class FetchCompanyTransactionStatementLogic extends AbstractControllerLogic ->where('owner_type', Wallet::class) ->where('type', '!=', TransactionType::PAYMENT); }) + ->orWhere(function ($query) use ($companyId) { + $query + ->where('type', TransactionType::INVOICE) + ->where('owner_type', Booking::class) + ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]) + ->whereHas('booking', function ($query) use ($companyId) { + $query->where('company_id', $companyId); + }); + }) ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]) ->orderBy('created_at', 'desc') ->get(); diff --git a/composer.json b/composer.json index 47acc131..b8b505d5 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,8 @@ "staudenmeir/eloquent-has-many-deep": "^1.7", "timehunter/laravel-google-recaptcha-v3": "~2.5", "tymon/jwt-auth": "^1.0", - "webklex/laravel-pdfmerger": "^1.3" + "webklex/laravel-pdfmerger": "^1.3", + "ext-bcmath": "*" }, "require-dev": { "facade/ignition": "^2.3.6", diff --git a/resources/assets/vue/components/bookings/elements/CustomerTransactionHistorySectionComponent.vue b/resources/assets/vue/components/bookings/elements/CustomerTransactionHistorySectionComponent.vue index 7b0aee28..026a76c5 100644 --- a/resources/assets/vue/components/bookings/elements/CustomerTransactionHistorySectionComponent.vue +++ b/resources/assets/vue/components/bookings/elements/CustomerTransactionHistorySectionComponent.vue @@ -24,7 +24,8 @@
{{item.created_at}}
{{ convertTransactionType(item.type) }} {{ item.payment_reference ? ' - ' + item.payment_reference : '' }}
{{item.booking_marking}}
-
{{[5, 9].includes(parseFloat(item.type)) ? (Math.round((parseFloat(item.amount) + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") : ''}}
+
{{[5, 9].includes(parseFloat(item.type)) ? (Math.round((parseFloat(item.amount) + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") : ''}}
+
{{(Math.round(((parseFloat(item.amount) / parseFloat(item.currency_rate) + parseFloat(item.service_charge)) + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}}
{{[1, 11].includes(parseFloat(item.type)) ? '- ' + (Math.round((parseFloat(item.amount) + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") : ''}}
{{remainingBalance(index)}}
@@ -104,7 +105,11 @@ export default { if(this.transaction){ let transactions = this.transaction.slice().reverse(); transactions.slice(0, transactions.length - index).map(function(transaction) { - [1, 11].includes(transaction.type) ? tempBalance -= (transaction.amount) : tempBalance += (transaction.amount); + if (transaction.type === 2) { + tempBalance += (transaction.amount / transaction.currency_rate + transaction.service_charge); + } else { + [1, 11].includes(transaction.type) ? tempBalance -= (transaction.amount) : tempBalance += (transaction.amount); + } return tempBalance }, 0); } diff --git a/resources/views/layouts/base_pdf.blade.php b/resources/views/layouts/base_pdf.blade.php index 99eb376a..e88d541d 100644 --- a/resources/views/layouts/base_pdf.blade.php +++ b/resources/views/layouts/base_pdf.blade.php @@ -149,4 +149,4 @@ @yield('inner_content') - \ No newline at end of file + diff --git a/resources/views/pages/pdfs/deliver_order.blade.php b/resources/views/pages/pdfs/deliver_order.blade.php index c59cc5a9..715244c7 100644 --- a/resources/views/pages/pdfs/deliver_order.blade.php +++ b/resources/views/pages/pdfs/deliver_order.blade.php @@ -33,7 +33,8 @@
EDO: {{ $transaction->bill_no }}
REF: {{ $transaction->booking->marking }}
-
Date: {{ $supplier->segments->whereIn('id', [23])->first() ? \Carbon\Carbon::now() : $po_order_transaction->created_at }}
+
Date: {{ + $supplier->segments->whereIn('id', [23])->first() ? \Carbon\Carbon::now() : $po_order_transaction->created_at }}
 
@@ -82,8 +83,8 @@ @php - $subtotal = calculateDoSubtotal($po_order_transaction, $transaction->currency_rate); - $voucherDiscount = getDoVoucherDiscount($voucher_redemption); + $subtotal = "0"; + $voucherDiscount = $voucher_redemption ? bcmul((string)$voucher_redemption->value, "-1", 2) : "0"; $displayedSubtotal = 0; @endphp @@ -93,6 +94,7 @@ $itemTotal = bcmul($exactUnitPrice, $transaction_detail->quantity, 5); $displayedItemTotal = bcmul($exactUnitPrice, $transaction_detail->quantity, 2); $displayedSubtotal = bcadd($displayedSubtotal, $displayedItemTotal, 2); + $subtotal = bcadd($subtotal, $displayedItemTotal, 2); @endphp {{ $key + 1 }} @@ -110,7 +112,6 @@ @php - $voucherDiscount = getDoVoucherDiscount($voucher_redemption); $subtotalWithDiscount = bcsub($subtotal, $voucherDiscount, 5); // Use bcsub to subtract @endphp @@ -148,7 +149,7 @@ Adjustment - {{ roundUpDo($discrepancy,2) }} + {{number_format($discrepancy, 5)}} @@ -167,28 +168,4 @@ - @php - function calculateDoSubtotal($po_order_transaction, $currencyRate) { - $subtotal = "0"; - foreach ($po_order_transaction->transactionDetails as $transaction_detail) { - $unitPrice = bcdiv((string)$transaction_detail->price, (string)$currencyRate, 5); - $itemTotal = bcmul($unitPrice, (string)$transaction_detail->quantity, 5); - $subtotal = bcadd($subtotal, $itemTotal, 5); - } - return $subtotal; - } - - function getDoVoucherDiscount($voucher_redemption) { - return $voucher_redemption ? bcmul((string)$voucher_redemption->value, "-1", 2) : "0"; - } - function roundUpDo($number, $decimals) { - $factor = pow(10, $decimals); - if ($number > 0) { - return ceil($number * $factor) / $factor; - } else { - return floor($number * $factor) / $factor; - } - } - - @endphp @endsection diff --git a/resources/views/pages/pdfs/invoice.blade.php b/resources/views/pages/pdfs/invoice.blade.php index 066fb971..db6c42c5 100644 --- a/resources/views/pages/pdfs/invoice.blade.php +++ b/resources/views/pages/pdfs/invoice.blade.php @@ -72,17 +72,18 @@ @php - $subtotal = calculateSubtotal($po_order_transaction, $transaction->currency_rate); - $voucherDiscount = getVoucherDiscount($voucher_redemption); + $subtotal = "0"; + $voucherDiscount = $voucher_redemption ? bcmul((string)$voucher_redemption->value, "-1", 2) : "0"; $displayedSubtotal = 0; @endphp @foreach ($po_order_transaction->transactionDetails as $key => $transaction_detail) @php - $exactUnitPrice = bcdiv($transaction_detail->price, $transaction->currency_rate, 5); + $exactUnitPrice = bcdiv($transaction_detail->price, $transaction->currency_rate, 7); $itemTotal = bcmul($exactUnitPrice, $transaction_detail->quantity, 5); $displayedItemTotal = bcmul($exactUnitPrice, $transaction_detail->quantity, 2); $displayedSubtotal = bcadd($displayedSubtotal, $displayedItemTotal, 2); + $subtotal = bcadd($subtotal, $itemTotal, 5); @endphp {{ $key + 1 }} @@ -100,8 +101,7 @@ @php - $voucherDiscount = getVoucherDiscount($voucher_redemption); - $subtotalWithDiscount = bcsub($subtotal, $voucherDiscount, 5); // Use bcsub to subtract + $subtotalWithDiscount = bcsub($subtotal, $voucherDiscount, 5); @endphp @@ -130,15 +130,15 @@ @endif @php - $displayedTotal = bcadd(bcadd(bcadd($displayedSubtotal, $transaction->service_charge, 2), $transaction->tax, 2), $voucherDiscount, 2); + $displayedTotal = bcadd(bcadd(bcadd($subtotal, $transaction->service_charge, 5), $transaction->tax, 5), $voucherDiscount, 5); $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 + $total = bcadd(bcadd(bcadd($subtotal, $transaction->service_charge, 5), $transaction->tax, 5), $voucherDiscount, 5); @endphp Adjustment - {{ roundUp($discrepancy,2) }} + {{number_format($discrepancy, 5)}} @@ -160,28 +160,4 @@ Account Name: CIEF Worldwide Sdn Bhd
Account No: 564892103405
- @php - function calculateSubtotal($po_order_transaction, $currencyRate) { - $subtotal = "0"; - foreach ($po_order_transaction->transactionDetails as $transaction_detail) { - $unitPrice = bcdiv((string)$transaction_detail->price, (string)$currencyRate, 5); - $itemTotal = bcmul($unitPrice, (string)$transaction_detail->quantity, 5); - $subtotal = bcadd($subtotal, $itemTotal, 5); - } - return $subtotal; - } - - function getVoucherDiscount($voucher_redemption) { - return $voucher_redemption ? bcmul((string)$voucher_redemption->value, "-1", 2) : "0"; - } - function roundUp($number, $decimals) { - $factor = pow(10, $decimals); - if ($number > 0) { - return ceil($number * $factor) / $factor; - } else { - return floor($number * $factor) / $factor; - } - } - - @endphp @endsection diff --git a/resources/views/pages/pdfs/purchase_order.blade.php b/resources/views/pages/pdfs/purchase_order.blade.php index e9d1c8fd..e0c9582f 100644 --- a/resources/views/pages/pdfs/purchase_order.blade.php +++ b/resources/views/pages/pdfs/purchase_order.blade.php @@ -24,9 +24,9 @@ @php - $subtotal = calculatePoSubtotal($po_order_transaction, $transaction->currency_rate); - $voucherDiscount = getPoVoucherDiscount($voucher_redemption); + $subtotal = "0"; $displayedSubtotal = 0; @endphp @@ -118,8 +117,13 @@ @php - $voucherDiscount = getPoVoucherDiscount($voucher_redemption); + $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); @endphp @@ -147,16 +151,11 @@ @endif - @php - $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 - @endphp + - + @@ -175,28 +174,4 @@
- - Buyer - + + Buyer +
@@ -34,34 +34,34 @@
- {{ $supplier->name }} - + {{ $supplier->name }} + - {{-- {{ $supplier }} --}} - + {{-- {{ $supplier }} --}} +
- @php - $billingAddress = $supplier->addresses()->where('billing', '=', true)->first(); - @endphp + @php + $billingAddress = $supplier->addresses()->where('billing', '=', true)->first(); + @endphp {{ $billingAddress->street_one }} {{ $billingAddress->street_two }} {{ $billingAddress->state()->first()->name }} {{ $billingAddress->district()->first()->name }} - +
- Phone: {{ $supplier->contacts()->first()->phone }} - + Phone: {{ $supplier->contacts()->first()->phone }} +
- - Seller - + + Seller +
- CIEF Worldwide Sdn Bhd (1134596-M) - + CIEF Worldwide Sdn Bhd (1134596-M) +
No. 72-3, Jalan Jalil 1,
The Earth Bukit Jalil,
@@ -90,8 +90,7 @@
{{ number_format($transaction->tax, 2) }}
Adjustment{{ roundUpPo($discrepancy,2) }}{{number_format($discrepancy, 5)}}
- @php - function calculatePoSubtotal($po_order_transaction, $currencyRate) { - $subtotal = "0"; - foreach ($po_order_transaction->transactionDetails as $transaction_detail) { - $unitPrice = bcdiv((string)$transaction_detail->price, (string)$currencyRate, 5); - $itemTotal = bcmul($unitPrice, (string)$transaction_detail->quantity, 5); - $subtotal = bcadd($subtotal, $itemTotal, 5); - } - return $subtotal; - } - - function getPoVoucherDiscount($voucher_redemption) { - return $voucher_redemption ? bcmul((string)$voucher_redemption->value, "-1", 2) : "0"; - } - function roundUpPo($number, $decimals) { - $factor = pow(10, $decimals); - if ($number > 0) { - return ceil($number * $factor) / $factor; - } else { - return floor($number * $factor) / $factor; - } - } - - @endphp @endsection diff --git a/routes/web.php b/routes/web.php index cff4a06d..5cc38fbd 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,5 +1,6 @@ $marking]); })->name('booking.details'); +Route::get('/transfer/{marking}/latest-invoice', function ($marking) { + $booking= Booking::where('marking', $marking)->first(); + + $purchaseOrder = $booking->transactions() + ->where('type', TransactionType::PURCHASE_ORDER) + ->complete() + ->first(); + + $transaction = $booking->transactions() + ->where('type', TransactionType::PAYMENT) + ->latest()->get()[0]; + + $supplier = Company::where('id', $transaction->receiver)->first(); + + $lowercaseDocumentType = strtolower(DocumentType::INVOICE); + + $voucherRedemption = $transaction->voucherRedemption; + + return view('pages.pdfs.' . $lowercaseDocumentType, ['transaction' => $transaction, 'po_order_transaction' => $purchaseOrder, 'supplier' => $supplier, 'voucher_redemption' => $voucherRedemption]); +})->name('booking.details.latest_invoice'); + Route::get('/transfer/merge/{marking}', function ($marking) { return view('pages.bookings.merge', ['marking' => $marking]); })->name('booking.merge'); @@ -462,6 +484,21 @@ Route::get('/payments/manual', function(){ +Route::get('/invoice/fix', function(){ + + set_time_limit(1800); + $bookings = Booking::where('status', ApprovalStatus::COMPLETED)->whereDate('updated_at', '>=', Carbon::parse('01-01-2023'))->get(); + + foreach($bookings as $booking){ + $booking->transactions()->whereIn('transactions.type', [TransactionType::INVOICE, TransactionType::SUPPLIER_DELIVER])->delete(); + $booking->documents()->whereIn('document_type', [DocumentType::INVOICE, DocumentType::PURCHASE_ORDER, DocumentType::DELIVER_ORDER, DocumentType::SUPPLIER_DELIVER_ORDER])->delete(); + $booking->status = ApprovalStatus::APPROVED; + $booking->save(); + + (App()->make(CreateInvoiceTransactionProcessor::class))->execute($booking); + } +})->name('invoice.fix'); + Route::get('/1688/fix/{reference}', function($reference){ $booking = Booking::where('marking', $reference)->first();