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;