Files
exchange-2.0/app/Classes/Modules/Bookings/Services/CalculatesBookingRefundAmount.php
T
JiaSheng 81c81e3b48 -show remark and categorized in export refund booking
-make refund function available for internal only, not for customer
2024-03-15 10:34:21 +08:00

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');
}
}