'Generate Bookings E-Invoices', 'message' => sprintf( 'You have successfully submitted %d booking%s for E-Invoices processing.', $this->processedCount, $this->processedCount === 1 ? '' : 's' ), ]; } /** * @param Request $request * @return JsonResponse * @throws \App\Classes\Exceptions\AccessForbiddenException * @throws \App\Classes\Exceptions\MalformedRequestException * @throws \App\Classes\Exceptions\RequestValidationException */ public function logic(Request $request): JsonResponse { $validated = $request->validate([ 'startDate' => 'nullable|date_format:d-m-Y', 'endDate' => 'nullable|date_format:d-m-Y|after_or_equal:startDate', ]); $startDate = null; $endDate = null; if (isset($validated['startDate']) && $validated['startDate']) { $startDate = Carbon::createFromFormat('d-m-Y', $validated['startDate'])->startOfDay(); } else { $startDate = Carbon::now()->subMonths(1)->startOfDay(); } if (isset($validated['endDate']) && $validated['endDate']) { $endDate = Carbon::createFromFormat('d-m-Y', $validated['endDate'])->endOfDay(); } else { $endDate = Carbon::now()->endOfDay(); } $startDate = $startDate ? Carbon::parse($startDate)->startOfDay() : Carbon::now()->subMonths(1); $endDate = $endDate ? Carbon::parse($endDate)->endOfDay() : Carbon::now(); // $bookings = Booking::where('status', ApprovalStatus::COMPLETED) // // ->whereBetween('created_at', [$startDate, $endDate]) // ->whereHas('transactions', function ($query) use ($startDate, $endDate) { // $query->payments() // ->complete() // ->whereBetween('created_at', [$startDate, $endDate]) // ->latest('created_at'); // }) // ->whereHas('attributesKVP', function (Builder $query) { // $query->where('key', KVPKey::AUTOCOUNT_DOCNO_INVOICE); // }) // ->with(['attributesKVP' => function ($query) { // $query->where('key', KVPKey::AUTOCOUNT_DOCNO_INVOICE); // }]) // ->get(); $bookings = Booking::where('status', ApprovalStatus::COMPLETED) ->whereHas('transactions', function ($query) use ($startDate, $endDate) { $query->payments() ->complete() ->whereBetween('created_at', [$startDate, $endDate]) ->latest('created_at'); }) ->get(); foreach ($bookings as $booking) { // Log::info('Booking ID: ' . $booking->marking); ProcessBookingForEInvoiceV2CommandJob::dispatch($booking); } $this->processedCount = count($bookings); return $this->resourceResponse(JsonResource::collection(collect([]))); } }