From 334dfd7b92c7b160f12b064905436bcdf199b830 Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Fri, 31 Oct 2025 13:40:03 +0800 Subject: [PATCH] E-Invoice - Include voucherify data into the report --- .../Services/ExportsSalesInvoiceReport.php | 37 ++++++++++++++++--- .../ExportsSalesInvoiceWithRefundReport.php | 34 +++++++++++++++-- ...dd_category_to_voucher_campaigns_table.php | 32 ++++++++++++++++ 3 files changed, 94 insertions(+), 9 deletions(-) create mode 100644 database/migrations/2025_10_31_095130_add_category_to_voucher_campaigns_table.php diff --git a/app/Classes/Modules/Exports/Services/ExportsSalesInvoiceReport.php b/app/Classes/Modules/Exports/Services/ExportsSalesInvoiceReport.php index c8e25404..3142441b 100644 --- a/app/Classes/Modules/Exports/Services/ExportsSalesInvoiceReport.php +++ b/app/Classes/Modules/Exports/Services/ExportsSalesInvoiceReport.php @@ -68,7 +68,8 @@ class ExportsSalesInvoiceReport implements FromQuery, WithHeadings, WithHeadingR return Booking::with([ 'company', 'transactions.transactionDetails', - 'transactions.voucherRedemption' + 'transactions.voucherRedemption', + 'transactions.voucherRedemption.voucher.campaign' ]) ->where('status', ApprovalStatus::COMPLETED) ->whereHas('transactions', function ($query) use ($startDate, $endDate) { @@ -138,10 +139,12 @@ class ExportsSalesInvoiceReport implements FromQuery, WithHeadings, WithHeadingR ->where('status', ApprovalStatus::COMPLETED) ->count(); - $refundedAmount = (App()->make(CalculatesBookingRefundAmount::class))->execute($booking, 1); - $refundedServiceCharge = (App()->make(CalculatesBookingRefundServiceCharge::class))->execute($booking, 1); + // $refundedAmount = (App()->make(CalculatesBookingRefundAmount::class))->execute($booking, 1); + // $refundedServiceCharge = (App()->make(CalculatesBookingRefundServiceCharge::class))->execute($booking, 1); - $totalPayment = $paymentSum - $refundedAmount - $refundedServiceCharge; + // $totalPayment = $paymentSum - $refundedAmount - $refundedServiceCharge; + + $totalPayment = $paymentSum; } $formattedDocumentDate = Carbon::parse($documentDate)->format('m/d/Y'); @@ -194,6 +197,30 @@ class ExportsSalesInvoiceReport implements FromQuery, WithHeadings, WithHeadingR } } + // Voucherify - Starts + $voucherRedemption = $lastPaymentTransaction->voucherRedemption; + if($voucherRedemption){ + $voucherDiscount = $voucherRedemption ? bcmul((string)$voucherRedemption->value, "-1", 2) : "0"; + $voucher = $voucherRedemption->voucher; + $category = $voucher->campaign ? $voucher->campaign->category : null; + $records[] = [ + '', + $formattedDocumentDate, + $company->debtor, + $invoiceTransaction ? $invoiceTransaction->bill_no : '', + $booking->marking, + $category === 'Compensation Voucher' ? '1000-000' : '949-2000', + 'PRODUCT NAME :', + $voucher->code, + '022', + 'C', + '1', + $voucherDiscount ? number_format($voucherDiscount, 2): '0', + '', + '' + ]; + } + // Voucherify - Ends // Service Charge - Starts $serviceCharge = 0; @@ -229,7 +256,7 @@ class ExportsSalesInvoiceReport implements FromQuery, WithHeadings, WithHeadingR // Adjustment - Starts $adjustment = 0; - $voucherRedemption = $invoiceTransaction->voucherRedemption; + $voucherRedemption = $lastPaymentTransaction->voucherRedemption; $voucherDiscount = $voucherRedemption ? bcmul((string)$voucherRedemption->value, "-1", 2) : "0"; $displayedSubtotal = is_numeric($displayedSubtotal) ? sprintf('%F', $displayedSubtotal) : '0'; diff --git a/app/Classes/Modules/Exports/Services/ExportsSalesInvoiceWithRefundReport.php b/app/Classes/Modules/Exports/Services/ExportsSalesInvoiceWithRefundReport.php index 36e71484..c6ac49cf 100644 --- a/app/Classes/Modules/Exports/Services/ExportsSalesInvoiceWithRefundReport.php +++ b/app/Classes/Modules/Exports/Services/ExportsSalesInvoiceWithRefundReport.php @@ -146,10 +146,11 @@ class ExportsSalesInvoiceWithRefundReport implements FromQuery, WithHeadings, Wi ->where('status', ApprovalStatus::COMPLETED) ->count(); - $refundedAmount = (App()->make(CalculatesBookingRefundAmount::class))->execute($booking, 1); - $refundedServiceCharge = (App()->make(CalculatesBookingRefundServiceCharge::class))->execute($booking, 1); + // $refundedAmount = (App()->make(CalculatesBookingRefundAmount::class))->execute($booking, 1); + // $refundedServiceCharge = (App()->make(CalculatesBookingRefundServiceCharge::class))->execute($booking, 1); + // $totalPayment = $paymentSum - $refundedAmount - $refundedServiceCharge; - $totalPayment = $paymentSum - $refundedAmount - $refundedServiceCharge; + $totalPayment = $paymentSum; } $formattedDocumentDate = Carbon::parse($documentDate)->format('m/d/Y'); @@ -203,6 +204,31 @@ class ExportsSalesInvoiceWithRefundReport implements FromQuery, WithHeadings, Wi } } + // Voucherify - Starts + $voucherRedemption = $lastPaymentTransaction->voucherRedemption; + if($voucherRedemption){ + $voucherDiscount = $voucherRedemption ? bcmul((string)$voucherRedemption->value, "-1", 2) : "0"; + $voucher = $voucherRedemption->voucher; + $category = $voucher->campaign ? $voucher->campaign->category : null; + $records[] = [ + '', + $formattedDocumentDate, + $company->debtor, + $invoiceTransaction ? $invoiceTransaction->bill_no : '', + $booking->marking, + $category === 'Compensation Voucher' ? '1000-000' : '949-2000', + 'PRODUCT NAME :', + $voucher->code, + '022', + 'C', + '1', + $voucherDiscount ? number_format($voucherDiscount, 2): '0', + '', + '' + ]; + } + // Voucherify - Ends + // Service Charge - Starts $serviceCharge = 0; if (!$totalPayment && $invoiceTransaction) { @@ -239,7 +265,7 @@ class ExportsSalesInvoiceWithRefundReport implements FromQuery, WithHeadings, Wi // Adjustment - Starts if($invoiceTransaction){ $adjustment = 0; - $voucherRedemption = $invoiceTransaction->voucherRedemption; + $voucherRedemption = $lastPaymentTransaction->voucherRedemption; $voucherDiscount = $voucherRedemption ? bcmul((string)$voucherRedemption->value, "-1", 2) : "0"; $displayedSubtotal = is_numeric($displayedSubtotal) ? sprintf('%F', $displayedSubtotal) : '0'; diff --git a/database/migrations/2025_10_31_095130_add_category_to_voucher_campaigns_table.php b/database/migrations/2025_10_31_095130_add_category_to_voucher_campaigns_table.php new file mode 100644 index 00000000..9a719286 --- /dev/null +++ b/database/migrations/2025_10_31_095130_add_category_to_voucher_campaigns_table.php @@ -0,0 +1,32 @@ +string('category')->nullable()->after('description'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('voucher_campaigns', function (Blueprint $table) { + $table->dropColumn('category'); + }); + } +}