diff --git a/app/Classes/Modules/Bookings/Services/CalculatesBookingOutstanding.php b/app/Classes/Modules/Bookings/Services/CalculatesBookingOutstanding.php index 31a9ebd3..f45a5749 100644 --- a/app/Classes/Modules/Bookings/Services/CalculatesBookingOutstanding.php +++ b/app/Classes/Modules/Bookings/Services/CalculatesBookingOutstanding.php @@ -3,6 +3,7 @@ namespace App\Classes\Modules\Bookings\Services; use App\Models\Booking; +use Illuminate\Support\Facades\Log; class CalculatesBookingOutstanding { @@ -31,10 +32,17 @@ class CalculatesBookingOutstanding public function execute(Booking $booking){ - return $booking->fix_amount - $this->calculatesBookingFloatingAmount->execute($booking, $booking->fix_currency_id) - $this->calculatesBookingPayableAmount->execute($booking, $booking->fix_currency_id) + $this->calculatesBookingRefundAmount->execute($booking, $booking->fix_currency_id); + $originalBookingAmount = $booking->fix_amount; + $refundedAmount = $this->calculatesBookingRefundAmount->execute($booking, $booking->fix_currency_id); + if($originalBookingAmount === $refundedAmount){ + return $originalBookingAmount - $this->calculatesBookingFloatingAmount->execute($booking, $booking->fix_currency_id) - $this->calculatesBookingPayableAmount->execute($booking, $booking->fix_currency_id); + } + else{ + return $originalBookingAmount - $this->calculatesBookingFloatingAmount->execute($booking, $booking->fix_currency_id) - $this->calculatesBookingPayableAmount->execute($booking, $booking->fix_currency_id) + $refundedAmount; + } } public function executeWithoutFloatingAmount(Booking $booking){ return $booking->fix_amount - $this->calculatesBookingPayableAmount->execute($booking, $booking->fix_currency_id) + $this->calculatesBookingRefundAmount->execute($booking, $booking->fix_currency_id); } -} \ No newline at end of file +} diff --git a/app/Classes/Modules/Bookings/Services/CalculatesBookingPayableAmount.php b/app/Classes/Modules/Bookings/Services/CalculatesBookingPayableAmount.php index c60debdb..c361c13a 100644 --- a/app/Classes/Modules/Bookings/Services/CalculatesBookingPayableAmount.php +++ b/app/Classes/Modules/Bookings/Services/CalculatesBookingPayableAmount.php @@ -7,6 +7,7 @@ use App\Classes\ValueObjects\Constants\ApprovalStatus; use App\Classes\ValueObjects\Constants\TransactionType; use App\Models\Booking; use Carbon\Carbon; +use Illuminate\Support\Facades\Log; class CalculatesBookingPayableAmount { @@ -19,10 +20,21 @@ class CalculatesBookingPayableAmount ->selectRaw('sum(amount - service_charge - tax) as sub_total')->get()->sum('sub_total') : $booking->transactions()->payments()->where('status', ApprovalStatus::REFUNDED)->sum('original_amount'); } - return $type === 1 ? - $booking->transactions()->payments()->complete() - ->selectRaw('sum(amount - service_charge - tax) as sub_total')->get()->sum('sub_total') : - $booking->transactions()->payments()->complete()->sum('original_amount'); + else{ + $returnAmount = $type === 1 ? + $booking->transactions()->payments()->complete() + ->selectRaw('sum(amount - service_charge - tax) as sub_total')->get()->sum('sub_total') : + $booking->transactions()->payments()->complete()->sum('original_amount'); + + if($returnAmount === 0){ + $returnAmount = $type === 1 ? + $booking->transactions()->payments()->where('status', ApprovalStatus::REFUNDED) + ->selectRaw('sum(amount - service_charge - tax) as sub_total')->get()->sum('sub_total') : + $booking->transactions()->payments()->where('status', ApprovalStatus::REFUNDED)->sum('original_amount'); + } + + return $returnAmount; + } } } diff --git a/app/Classes/Modules/Bookings/Services/CalculatesBookingRefundAmount.php b/app/Classes/Modules/Bookings/Services/CalculatesBookingRefundAmount.php index 8792aaa6..5f3fd8b6 100644 --- a/app/Classes/Modules/Bookings/Services/CalculatesBookingRefundAmount.php +++ b/app/Classes/Modules/Bookings/Services/CalculatesBookingRefundAmount.php @@ -13,6 +13,12 @@ class CalculatesBookingRefundAmount return $this->calculateRefundAmount($payment, $type); }); + if ($refundAmounts->isEmpty()) { + $refundAmounts = $booking->transactions()->payments()->whereIn('status', [ApprovalStatus::REFUNDED])->get()->map(function ($payment) use ($type) { + return $this->calculateRefundAmount($payment, $type); + }); + } + $totalRefundAmount = $refundAmounts->sum(); return $totalRefundAmount; 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/app/Http/Resources/BookingResource.php b/app/Http/Resources/BookingResource.php index 5d8b3836..9c6edf96 100644 --- a/app/Http/Resources/BookingResource.php +++ b/app/Http/Resources/BookingResource.php @@ -49,7 +49,7 @@ class BookingResource extends JsonResource 'marking' => $this->marking, 'amount' => $this->fix_amount, 'floating_amount' => floatval((App()->make(CalculatesBookingFloatingAmount::class))->execute($this->resource, $this->fix_currency_id)), - 'paid_amount_with_refund' => floatval((App()->make(CalculatesBookingPayableAmount::class))->execute($this->resource, $this->fix_currency_id)) - floatval((App()->make(CalculatesBookingRefundAmount::class))->execute($this->resource, $this->fix_currency_id)), + // 'paid_amount_with_refund' => floatval((App()->make(CalculatesBookingPayableAmount::class))->execute($this->resource, $this->fix_currency_id)) - floatval((App()->make(CalculatesBookingRefundAmount::class))->execute($this->resource, $this->fix_currency_id)), 'paid_amount' => floatval((App()->make(CalculatesBookingPayableAmount::class))->execute($this->resource, $this->fix_currency_id)), 'outstanding_amount_with_refund' => $outStandingAmountWithRefund > 0 ? $outStandingAmountWithRefund : 0, 'refunded_amount' => floatval((App()->make(CalculatesBookingRefundAmount::class))->execute($this->resource, $this->fix_currency_id)), 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'); + }); + } +} diff --git a/resources/assets/vue/components/bookings/sections/BookingDetailsSectionComponent.vue b/resources/assets/vue/components/bookings/sections/BookingDetailsSectionComponent.vue index bc5f3dca..0e702efc 100644 --- a/resources/assets/vue/components/bookings/sections/BookingDetailsSectionComponent.vue +++ b/resources/assets/vue/components/bookings/sections/BookingDetailsSectionComponent.vue @@ -5,8 +5,8 @@
| No | -Stock Code | -Description | -Quantity | -Unit Price (RM) | -Total Amount (RM) |
-
|---|---|---|---|---|---|
| {{ $key + 1 }} | -{{ $transaction_detail->product_code }} | -{{ $transaction_detail->product_name }} | -{{ $transaction_detail->quantity }} | -- {{ number_format($displayUnitPrice, 2) }} - | -- {{ number_format($displayedItemTotal, 2) }} - | -
| - | - | CANCEL FULL ORDER | -- | - | - |
| - | Subtotal | -{{ number_format($displayedSubtotal, 2) }} | -|||
| - | Service Charges | -{{ number_format($serviceCharge, 2) }} | -|||
| - | Voucher ({{ $voucher_redemption->voucher->code }}) | -{{ number_format($voucherDiscount, 2) }} | -|||
| - | Tax | -{{ number_format($transaction->tax, 2) }} | -|||
| - | Adjustment | -{{number_format($discrepancy, 5)}} | -|||
| - | Total | -- {{ number_format($total, 2) }} - | -|||