input('page', 1); $perPage = 2500; $offset = ($page - 1) * $perPage; $cutOffDate = $request->cut_off_date ? Carbon::parse($request->cut_off_date) : null; $baseQuery = Booking::with('company') ->when($cutOffDate, fn($q) => $q->where('created_at', '<=', $cutOffDate)) ->orderByDesc('id'); $total = $baseQuery->count(); $bookings = $baseQuery->offset($offset)->limit($perPage)->get(); $calculator = new CalculatesBookingPaidAmount(); $filtered = $bookings->filter(function ($b) use ($calculator, $cutOffDate) { $paid = $calculator->executeUntilDate($b, $cutOffDate); $outstanding = $b->fix_amount - $paid; return $paid > 0 && $outstanding > 0; }); return response()->json([ 'success' => true, 'current_page' => $page, 'next_page' => ($offset + $perPage < $total) ? $page + 1 : null, 'count' => $filtered->count(), 'data' => $filtered->map(function ($b) use ($calculator, $cutOffDate) { $paid = $calculator->executeUntilDate($b, $cutOffDate); return [ 'id' => $b->id, 'order_ref' => $b->marking ?? $b->id, 'booking_amount' => number_format($b->fix_amount, 2), 'paid_amount' => number_format($paid, 2), 'outstanding_amount' => number_format($b->fix_amount - $paid, 2), 'customer' => optional($b->company)->reference, 'created_at' => $b->created_at->toDateTimeString(), ]; })->values(), ]); } public function loadView(Request $request) { $cutOffDateString = $request->cut_off_date ?? ''; $cutOffDateParsed = $cutOffDateString ? Carbon::parse($cutOffDateString)->toDateString() : '-'; echo <<Cut Off Date: {$cutOffDateParsed}
| Order Ref | Booking Amount | Paid Amount | Outstanding Amount | Customer | Order Created Date |
|---|