fix booking rate average bug

This commit is contained in:
omair saleh
2021-09-24 18:11:34 +08:00
parent 6733778edf
commit 61605c8efa
@@ -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');
}
}