From c34bacccfd50b2d9d1733c285a1c8cae3b5f4362 Mon Sep 17 00:00:00 2001 From: Jia Sheng Date: Mon, 24 Jun 2024 22:45:21 +0800 Subject: [PATCH] -fix expired booking command to ignore 1688 as 1688 may not have PO initally -create command to revert those fully refunded payment transaction from status EXPIRED to REFUNDED --- .../Commands/ExpiredBookingCommand.php | 5 +- ...gWithRefundedPaymentTransactionCommand.php | 67 +++++++++++++++++++ app/Console/Kernel.php | 8 +-- 3 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 app/Console/Commands/FixExpiredBookingWithRefundedPaymentTransactionCommand.php diff --git a/app/Console/Commands/ExpiredBookingCommand.php b/app/Console/Commands/ExpiredBookingCommand.php index fe4f260e..17a0158e 100644 --- a/app/Console/Commands/ExpiredBookingCommand.php +++ b/app/Console/Commands/ExpiredBookingCommand.php @@ -51,11 +51,12 @@ class ExpiredBookingCommand extends Command // 1. Cancel booking without payment & purchase order (1 month) $bookings = Booking::where('status', ApprovalStatus::APPROVED) ->where('created_at', '<', now()->subDays(30)->endOfDay()) + ->where('service_id', '!=', 4) ->where(function ($query) { $query->whereDoesntHave('transactions') ->orWhereDoesntHave('transactions', function($transaction) { return $transaction->where('type', TransactionType::PURCHASE_ORDER)->orWhere(function ($q) { - $q->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + $q->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED, ApprovalStatus::REFUNDED]); }); }); })->get(); @@ -78,7 +79,7 @@ class ExpiredBookingCommand extends Command ->where('created_at', '<', now()->subDays(60)->endOfDay()) ->where(function ($query) { $query->whereDoesntHave('transactions', function($transaction) { - return $transaction->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + return $transaction->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED, ApprovalStatus::REFUNDED]); })->whereHas('transactions', function($transaction) { return $transaction->where('type', TransactionType::PURCHASE_ORDER); }); diff --git a/app/Console/Commands/FixExpiredBookingWithRefundedPaymentTransactionCommand.php b/app/Console/Commands/FixExpiredBookingWithRefundedPaymentTransactionCommand.php new file mode 100644 index 00000000..b8e27996 --- /dev/null +++ b/app/Console/Commands/FixExpiredBookingWithRefundedPaymentTransactionCommand.php @@ -0,0 +1,67 @@ +whereHas('transactions', function ($q) { + $q->where('type', TransactionType::PAYMENT)->where('status', ApprovalStatus::EXPIRED)->whereHas('transactions', function ($q2) { + $q2->where('type', TransactionType::REFUND)->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + }); + })->get(); + + foreach ($bookings as $booking) { + $payment_transactions = $booking->transactions()->where('type', TransactionType::PAYMENT)->whereHas('transactions', function ($q2) { + $q2->where('type', TransactionType::REFUND)->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + })->get(); + + foreach ($payment_transactions as $payment) { + $payment_original_amount = $payment->original_amount; + $refund_original_amount = $payment->transactions()->where('type', TransactionType::REFUND)->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])->sum('original_amount'); + + if ($payment_original_amount - $refund_original_amount < 0.01) { + $this->info("Updated booking id: $booking->id, payment transaction id: $payment->id, from EXPIRED to REFUNDED"); + + $payment->status = ApprovalStatus::REFUNDED; + $payment->save(); + } + } + } + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 09b96687..e455eea1 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -44,10 +44,10 @@ class Kernel extends ConsoleKernel ->appendOutputTo(storage_path().'/logs/delete-bulk-download-files.log') ->withoutOverlapping(); - // $schedule->command('booking:expired') - // ->dailyAt('02:00') - // ->appendOutputTo(storage_path().'/logs/expire-booking.log') - // ->withoutOverlapping(); + $schedule->command('booking:expired') + ->dailyAt('02:00') + ->appendOutputTo(storage_path().'/logs/expire-booking.log') + ->withoutOverlapping(); // $schedule->command('purchaseOrder:autoFill') // ->dailyAt('03:00')