diff --git a/app/Classes/Modules/Bookings/Services/CalculatesBookingCurrencyAverageRate.php b/app/Classes/Modules/Bookings/Services/CalculatesBookingCurrencyAverageRate.php index 235d897a..2a6de8d2 100644 --- a/app/Classes/Modules/Bookings/Services/CalculatesBookingCurrencyAverageRate.php +++ b/app/Classes/Modules/Bookings/Services/CalculatesBookingCurrencyAverageRate.php @@ -9,27 +9,29 @@ use Carbon\Carbon; class CalculatesBookingCurrencyAverageRate { + /** @var CalculatesBookingPayableAmount */ + private $calculatesBookingPayableAmount; + + /** + * CalculatesBookingCurrencyAverageRate constructor. + * @param CalculatesBookingPayableAmount $calculatesBookingPayableAmount + */ + public function __construct(CalculatesBookingPayableAmount $calculatesBookingPayableAmount) + { + $this->calculatesBookingPayableAmount = $calculatesBookingPayableAmount; + } + + public function execute(Booking $booking, $type){ if ($type == TransactionType::PAYMENT) { - - $amount_without_service = $booking->transactions() - ->where('type', TransactionType::PAYMENT) - ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]) - ->selectRaw('ROUND(amount,2)-ROUND(service_charge,2) AS amount_without_service') - ->get()->sum('amount_without_service'); + $totalPayment = $booking->fix_currency_id === 1 ? $booking->transactions()->payments()->complete()->sum('original_amount') : + $booking->transactions()->payments()->complete()->selectRaw('sum(amount - service_charge - tax) as sub_total')->get()->sum('sub_total'); + return $this->calculatesBookingPayableAmount->execute($booking, $booking->fix_currency_id) / $totalPayment; - return number_format($booking->fix_amount/$amount_without_service,2); } else if ($type == TransactionType::BILL) { - - $amount_without_service = $booking->transactions() - ->where('type', TransactionType::BILL) - ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]) - ->selectRaw('ROUND(amount,2)-ROUND(service_charge,2) AS amount_without_service') - ->get()->sum('amount_without_service'); - - return number_format($booking->fix_amount/$amount_without_service,2); + return $booking->transactions()->bills()->complete()->sum('original_amount') / $booking->transactions()->bills()->complete()->sum('amount'); } }