From 1bb0a73bd9b2ea368d762482f7c2b862b1e15a07 Mon Sep 17 00:00:00 2001
From: Edmond Lang
Date: Fri, 25 Jul 2025 18:59:02 +0800
Subject: [PATCH] add route preview-unfinished-payment-orders
---
.../Reports/UnfinishedPaymentOrders.php | 155 ++++++++++++++++++
routes/web.php | 52 +-----
2 files changed, 162 insertions(+), 45 deletions(-)
create mode 100644 app/Http/Controllers/Reports/UnfinishedPaymentOrders.php
diff --git a/app/Http/Controllers/Reports/UnfinishedPaymentOrders.php b/app/Http/Controllers/Reports/UnfinishedPaymentOrders.php
new file mode 100644
index 00000000..c3bda14a
--- /dev/null
+++ b/app/Http/Controllers/Reports/UnfinishedPaymentOrders.php
@@ -0,0 +1,155 @@
+input('page', 1);
+ $perPage = 5000;
+ $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}
+🔄 Processing... Total 0
+
+
+
+
+
+
+
+ | Order Ref |
+ Booking Amount |
+ Paid Amount |
+ Outstanding Amount |
+ Customer |
+ Order Created Date |
+
+
+
+
+
+
+HTML;
+ }
+}
diff --git a/routes/web.php b/routes/web.php
index 521d91d1..b5cb1767 100644
--- a/routes/web.php
+++ b/routes/web.php
@@ -36,7 +36,7 @@ use App\Classes\Modules\Transactions\Services\DeletesTransaction;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Classes\General\AWSS3Helper;
-use App\Classes\Modules\Bookings\Services\CalculatesBookingPaidAmount;
+use App\Http\Controllers\Reports\UnfinishedPaymentOrders;
use Illuminate\Support\Facades\File;
@@ -1371,49 +1371,11 @@ Route::get('/maintenance', function () {
return response()->view('errors.503', [], 503);
});
+// web route to view the result
Route::get('/preview-unfinished-payment-orders', function (Request $request) {
- $cutOffDate = $request->cut_off_date ?? null;
-
- if ($cutOffDate) {
- echo 'Cut Off Date: ' . $cutOffDate;
- echo '
';
-
- $cutOffDate = Carbon::parse($cutOffDate);
- }
-
- echo '';
- echo '
- | Order Ref |
- Booking Amount |
- Paid Amount |
- Outstanding Amount |
- Customer |
- Order Created Date |
-
';
- echo '';
- $bookings = Booking::where('status', '<', ApprovalStatus::COMPLETED);
- if ($cutOffDate) {
- $bookings->where('created_at', '<=', $cutOffDate);
- }
- $bookings = $bookings->orderBy('id', 'desc')->get();
-
- $calculateBookingPaidAmount = new CalculatesBookingPaidAmount();
-
- foreach ($bookings as $booking) {
- $paid = $calculateBookingPaidAmount->executeUntilDate($booking, $cutOffDate);
- $outstanding = $booking->fix_amount - $paid;
-
- if ($paid > 0 && $outstanding > 0) {
- echo '';
- echo '| '.$booking->marking.' | ';
- echo '' . number_format($booking->fix_amount, 2) . ' | ';
- echo '' . number_format($paid, 2) . ' | ';
- echo '' . number_format($outstanding, 2) . ' | ';
- echo ''.$booking->company->reference.' | ';
- echo '' . $booking->created_at->format('d-m-Y') . ' | ';
- echo '
';
- }
- }
-
- echo '
';
+ return (new UnfinishedPaymentOrders())->loadView($request);
+});
+// web route to run the logic
+Route::get('/run-batch-unfinished-payment-orders', function (Request $request) {
+ return (new UnfinishedPaymentOrders())->execute($request);
});
\ No newline at end of file