mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 04:23:55 +00:00
81c81e3b48
-make refund function available for internal only, not for customer
32 lines
998 B
PHP
32 lines
998 B
PHP
<?php
|
|
|
|
namespace App\Classes\Modules\Bookings\Services;
|
|
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use App\Models\Booking;
|
|
|
|
class CalculatesBookingRefundAmount
|
|
{
|
|
public function execute(Booking $booking, int $type, ?string $payment_reference = null): float
|
|
{
|
|
$refundAmounts = $booking->transactions()->payments()->complete()->get()->map(function ($payment) use ($type) {
|
|
return $this->calculateRefundAmount($payment, $type);
|
|
});
|
|
|
|
$totalRefundAmount = $refundAmounts->sum();
|
|
|
|
return $totalRefundAmount;
|
|
}
|
|
|
|
public function calculateRefundAmount($payment, int $type): float
|
|
{
|
|
$refundTransactions = $payment->transactions()->refunds()->whereIn('status', [ApprovalStatus::APPROVED]);
|
|
|
|
if ($type === 1) {
|
|
return $refundTransactions->selectRaw('sum(amount - service_charge - tax) as sub_total')->get()->sum('sub_total');
|
|
}
|
|
|
|
return $refundTransactions->sum('original_amount');
|
|
}
|
|
}
|