From 0ec2c8b12221ec14230d9c7bbdee6a179d512feb Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Mon, 4 Aug 2025 16:30:37 +0800 Subject: [PATCH] E-Invoice - Automapping Issues, Sales Invoice Report (Export) --- .../Services/ExportsSalesInvoiceReport.php | 132 +++++++++++++++--- 1 file changed, 114 insertions(+), 18 deletions(-) diff --git a/app/Classes/Modules/Exports/Services/ExportsSalesInvoiceReport.php b/app/Classes/Modules/Exports/Services/ExportsSalesInvoiceReport.php index d7cf1655..f3ff3ac6 100644 --- a/app/Classes/Modules/Exports/Services/ExportsSalesInvoiceReport.php +++ b/app/Classes/Modules/Exports/Services/ExportsSalesInvoiceReport.php @@ -2,18 +2,17 @@ namespace App\Classes\Modules\Exports\Services; -use App\Classes\ValueObjects\Constants\PaymentMethodType; use App\Classes\ValueObjects\Constants\TransactionType; use App\Classes\ValueObjects\Constants\ApprovalStatus; use App\Models\Booking; -use App\Models\Transaction; use Maatwebsite\Excel\Concerns\Exportable; use Maatwebsite\Excel\Concerns\FromQuery; use Maatwebsite\Excel\Concerns\ShouldAutoSize; use Maatwebsite\Excel\Concerns\WithHeadingRow; use Maatwebsite\Excel\Concerns\WithHeadings; use Maatwebsite\Excel\Concerns\WithMapping; -use Illuminate\Http\Request; +use App\Classes\Modules\Bookings\Services\CalculatesBookingRefundAmount; +use App\Classes\Modules\Bookings\Services\CalculatesBookingRefundServiceCharge; use Carbon\Carbon; use Illuminate\Support\Facades\Log; @@ -82,8 +81,14 @@ class ExportsSalesInvoiceReport implements FromQuery, WithHeadings, WithHeadingR return $records; } + $invoiceTransaction = $booking->transactions()->where('type', TransactionType::INVOICE)->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])->first(); + $currencyId = $booking->fix_currency_id; - $averageCurrencyRate = $lastPaymentTransaction->currency_rate; + + $subtotal = 0; + $displayedSubtotal = 0; + $totalPayment = 0; + $averageCurrencyRate = $invoiceTransaction->currency_rate; $paymentSum = $booking->transactions() ->where('type', TransactionType::PAYMENT) ->where('status', ApprovalStatus::COMPLETED) @@ -93,15 +98,20 @@ class ExportsSalesInvoiceReport implements FromQuery, WithHeadings, WithHeadingR }); if ($paymentSum){ $averageCurrencyRate = $booking->transactions() - ->where('type', TransactionType::PAYMENT) - ->where('status', ApprovalStatus::COMPLETED) - ->get() - ->sum(function ($transaction) { - return $transaction->currency_rate; - }) / $booking->transactions() - ->where('type', TransactionType::PAYMENT) - ->where('status', ApprovalStatus::COMPLETED) - ->count(); + ->where('type', TransactionType::PAYMENT) + ->where('status', ApprovalStatus::COMPLETED) + ->get() + ->sum(function ($transaction) { + return $transaction->currency_rate; + }) / $booking->transactions() + ->where('type', TransactionType::PAYMENT) + ->where('status', ApprovalStatus::COMPLETED) + ->count(); + + $refundedAmount = (App()->make(CalculatesBookingRefundAmount::class))->execute($booking, 1); + $refundedServiceCharge = (App()->make(CalculatesBookingRefundServiceCharge::class))->execute($booking, 1); + + $totalPayment = $paymentSum - $refundedAmount - $refundedServiceCharge; } $documentDate = $lastPaymentTransaction->created_at; @@ -117,14 +127,19 @@ class ExportsSalesInvoiceReport implements FromQuery, WithHeadings, WithHeadingR if($averageCurrencyRate && $currencyId){ $exactUnitPrice = ($currencyId) === 1 ? $detail->price : bcdiv($detail->price, $averageCurrencyRate, 7); $displayUnitPrice = round($exactUnitPrice, 2); - // $detailPrice = round(bcmul($displayUnitPrice, $detail->quantity, 7), 2); + $itemTotal = bcmul($exactUnitPrice, $detail->quantity, 5); + $displayedItemTotal = round(bcmul($displayUnitPrice, $detail->quantity, 7), 2); + $displayedSubtotal = bcadd($displayedSubtotal, $displayedItemTotal, 2); + $subtotal = bcadd($subtotal, $itemTotal, 5); } + + $records[] = [ $firstItem ? '<>' : '', - $formattedDocumentDate, - $company->debtor, - $booking->marking, - $booking->marking, + $firstItem ? $formattedDocumentDate : '', + $firstItem ? $company->debtor : '', + $firstItem ? $booking->marking : '', + $firstItem ? $booking->marking : '', '500-0000', 'PRODUCT NAME :', $detail->product_name, @@ -140,6 +155,87 @@ class ExportsSalesInvoiceReport implements FromQuery, WithHeadings, WithHeadingR $firstItem = false; } } + + + // Service Charge - Starts + $serviceCharge = 0; + if (!$totalPayment) { + $serviceCharge = $invoiceTransaction->service_charge; + } + else { + $serviceCharge = $booking->transactions() + ->where('type', TransactionType::PAYMENT) + ->where('status', ApprovalStatus::COMPLETED) + ->get() + ->sum(function ($transaction) { + return $transaction->service_charge; + }); + } + + $records[] = [ + '', + '', + '', + '', + '', + '500-0000', + 'PRODUCT NAME :', + 'Service Charge', + '022', + 'C', + '1', + $serviceCharge ? number_format($serviceCharge, 2): '0', + '', + $company->e_invoice ? 'F' : 'T' + ]; + // Service Charge - Ends + + // Adjustment - Starts + $adjustment = 0; + $voucherRedemption = $invoiceTransaction->voucherRedemption; + $voucherDiscount = $voucherRedemption ? bcmul((string)$voucherRedemption->value, "-1", 2) : "0"; + + $displayedSubtotal = is_numeric($displayedSubtotal) ? sprintf('%F', $displayedSubtotal) : '0'; + $serviceCharge = is_numeric($serviceCharge) ? sprintf('%F', $serviceCharge) : '0'; + $tax = is_numeric($invoiceTransaction->tax) ? sprintf('%F', $invoiceTransaction->tax) : '0'; + $voucherDiscount = is_numeric($voucherDiscount) ? sprintf('%F', $voucherDiscount) : '0'; + + $displayedTotal = bcadd( + bcadd( + bcadd($displayedSubtotal, $serviceCharge, 5), + $tax, + 5 + ), + $voucherDiscount, + 5 + ); + + $expectedTotal = bcadd(bcadd(bcadd($subtotal, $serviceCharge, 5), $invoiceTransaction->tax, 5), $voucherDiscount, 5); + $adjustment = bcsub($expectedTotal, $displayedTotal, 5); + + if ($totalPayment) { + $expectedTotal = $totalPayment; + $adjustment = bcsub($expectedTotal, $displayedTotal, 5); + } + + $records[] = [ + '', + '', + '', + '', + '', + '500-0000', + 'PRODUCT NAME :', + 'Adjustment', + '022', + 'C', + '1', + $adjustment ? number_format($adjustment, 2): '0', + '', + $company->e_invoice ? 'F' : 'T' + ]; + // Adjustment - Ends + return $records; } }