<?php

namespace App\Classes\Modules\Bookings\Services;


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
{

    public function execute(Booking $booking, int $type, bool $generateEInvoiceRefund = false){

        if($generateEInvoiceRefund){
            return $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');
        }
        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;
        }
    }

}
