updatesBookingStatus = $updatesBookingStatus; } /** * Execute the console command. * * @return int */ public function handle() { // 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, ApprovalStatus::REFUNDED]); }); }); })->get(); foreach ($bookings as $booking) { $this->updatesBookingStatus->execute($booking, ApprovalStatus::EXPIRED); $this->info(Carbon::now() . " : Expired Booking without payment & purchase order, booking id: " . $booking->id); $transactions = $booking->transactions; foreach ($transactions as $transaction) { $prevStatus = $transaction->status; $transaction->status = ApprovalStatus::EXPIRED; $transaction->save(); $this->info(Carbon::now() . " : Expired Transaction id: {$transaction->id} from Booking id: {$booking->id}. Status before update: {$prevStatus}"); } } // 2. Cancel booking without payment but with purchase order (2 month) $bookings = Booking::where('status', ApprovalStatus::APPROVED) ->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, ApprovalStatus::REFUNDED]); })->whereHas('transactions', function($transaction) { return $transaction->where('type', TransactionType::PURCHASE_ORDER); }); })->get(); foreach ($bookings as $booking) { $this->updatesBookingStatus->execute($booking, ApprovalStatus::EXPIRED); $this->info(Carbon::now() . " : Expired Booking without payment but with purchase order, booking id: " . $booking->id); $transactions = $booking->transactions; foreach ($transactions as $transaction) { $prevStatus = $transaction->status; $transaction->status = ApprovalStatus::EXPIRED; $transaction->save(); $this->info(Carbon::now() . " : Expired Transaction id: {$transaction->id} from Booking id: {$booking->id}. Status before update: {$prevStatus}"); } } } }