diff --git a/app/Classes/Modules/Transactions/Processors/CreateInvoiceTransactionProcessor.php b/app/Classes/Modules/Transactions/Processors/CreateInvoiceTransactionProcessor.php index 799032ab..afacb35f 100644 --- a/app/Classes/Modules/Transactions/Processors/CreateInvoiceTransactionProcessor.php +++ b/app/Classes/Modules/Transactions/Processors/CreateInvoiceTransactionProcessor.php @@ -22,6 +22,7 @@ use App\Classes\ValueObjects\Constants\KVPKey; use App\Models\Booking; use App\Models\SegmentConstant; use Carbon\Carbon; +use Exception; use Illuminate\Support\Facades\Log; class CreateInvoiceTransactionProcessor @@ -214,8 +215,23 @@ class CreateInvoiceTransactionProcessor $booking_currency_average_rate = $this->calculatesBookingCurrencyAverageRate->execute($booking, TransactionType::BILL); - $transaction = $booking->transactions()->payments()->where('status', ApprovalStatus::COMPLETED)->first() - ->transactions()->where('type', TransactionType::BILL)->first(); + $paymentTransaction = $booking->transactions()->payments()->where('status', ApprovalStatus::COMPLETED)->first(); + + $transaction = null; + if($paymentTransaction){ + $transaction = $paymentTransaction->transactions()->where('type', TransactionType::BILL)->first(); + } + else{ // Special handling for refund cases (When a refund is deleted via DeleteRefundTransactionLogic, a booking payment transaction is set to ApprovalStatus::APPROVED) + $temp = $booking->transactions()->payments()->where('status', ApprovalStatus::APPROVED)->first(); + // Lets check if there is a refund case + $refund = $temp->transactions()->refunds()->where('status', ApprovalStatus::APPROVED)->first(); + if($refund){ + $transaction = $temp; + } + else{ + throw new Exception("No payment found for booking '$booking->id'."); + } + } $transaction_object = new TransactionObject( $billNumber,