diff --git a/app/Http/Controllers/user/UserOrderController.php b/app/Http/Controllers/user/UserOrderController.php index da77e68..f58a935 100644 --- a/app/Http/Controllers/user/UserOrderController.php +++ b/app/Http/Controllers/user/UserOrderController.php @@ -21,23 +21,33 @@ class UserOrderController extends Controller } // ------------------------------------- - // Get all orders + // Get customer invoices with containers // ------------------------------------- - $orders = $user->orders()->with('invoice')->get(); + $invoices = $user->invoices()->with('container')->get(); + + // Unique containers for this customer + $containers = $invoices->pluck('container')->filter()->unique('id'); // ------------------------------------- - // Counts + // Counts based on container status // ------------------------------------- - $totalOrders = $orders->count(); - $delivered = $orders->where('status', 'delivered')->count(); - $inTransit = $orders->where('status', '!=', 'delivered')->count(); - $active = $totalOrders; + $totalOrders = $containers->count(); + + $delivered = $containers->where('status', 'delivered')->count(); + + $inTransit = $containers->whereNotIn('status', [ + 'delivered', + 'warehouse', + 'domestic-distribution' + ])->count(); + + $active = $totalOrders; // ------------------------------------- - // Total Amount = Invoice.total_with_gst + // Total Amount = sum of invoice totals // ------------------------------------- - $totalAmount = $orders->sum(function ($o) { - return $o->invoice->final_amount_with_gst ?? 0; + $totalAmount = $invoices->sum(function ($invoice) { + return $invoice->final_amount_with_gst ?? 0; }); // Format total amount in K, L, Cr @@ -45,13 +55,12 @@ class UserOrderController extends Controller return response()->json([ 'status' => true, - 'summary' => [ 'active_orders' => $active, 'in_transit_orders' => $inTransit, 'delivered_orders' => $delivered, - 'total_value' => $formattedAmount, // formatted value - 'total_raw' => $totalAmount // original value + 'total_value' => $formattedAmount, + 'total_raw' => $totalAmount ] ]); } @@ -90,20 +99,28 @@ class UserOrderController extends Controller ], 401); } - // Fetch orders for this user - $orders = $user->orders() - ->with(['invoice', 'shipments']) + // Get invoices with containers for this customer + $invoices = $user->invoices() + ->with('container') ->orderBy('id', 'desc') - ->get() - ->map(function ($o) { - return [ - 'order_id' => $o->order_id, - 'status' => $o->status, - 'amount' => $o->ttl_amount, - 'description'=> "Order from {$o->origin} to {$o->destination}", - 'created_at' => $o->created_at, - ]; - }); + ->get(); + + // Extract unique containers + $containers = $invoices->pluck('container') + ->filter() + ->unique('id') + ->values(); + + $orders = $containers->map(function ($container) { + + return [ + 'order_id' => $container->id, + 'container_number' => $container->container_number, + 'status' => $container->status, + 'container_date' => $container->container_date, + 'created_at' => $container->created_at, + ]; + }); return response()->json([ 'success' => true, @@ -115,45 +132,73 @@ public function orderDetails($order_id) { $user = JWTAuth::parseToken()->authenticate(); - $order = $user->orders() + if (!$user) { + return response()->json([ + 'success' => false, + 'message' => 'Unauthorized' + ], 401); + } + + // Find container first + $container = \App\Models\Container::find($order_id); + + if (!$container) { + return response()->json([ + 'success' => false, + 'message' => 'Container not found' + ], 404); + } + + // Find invoice belonging to this user for this container + $invoice = \App\Models\Invoice::where('customer_id', $user->id) + ->where('container_id', $container->id) ->with(['items']) - ->where('order_id', $order_id) ->first(); - if (!$order) { - return response()->json(['success' => false, 'message' => 'Order not found'], 404); + if (!$invoice) { + return response()->json([ + 'success' => false, + 'message' => 'Order not found for this user' + ], 404); } return response()->json([ 'success' => true, - 'order' => $order + 'order' => [ + 'container_id' => $container->id, + 'container_number' => $container->container_number, + 'container_date' => $container->container_date, + 'status' => $container->status, + 'invoice_id' => $invoice->id, + 'items' => $invoice->items + ] ]); } -public function orderShipment($order_id) -{ - $user = JWTAuth::parseToken()->authenticate(); +// public function orderShipment($order_id) +// { +// $user = JWTAuth::parseToken()->authenticate(); - // Get order - $order = $user->orders()->where('order_id', $order_id)->first(); +// // Get order +// $order = $user->orders()->where('order_id', $order_id)->first(); - if (!$order) { - return response()->json(['success' => false, 'message' => 'Order not found'], 404); - } +// if (!$order) { +// return response()->json(['success' => false, 'message' => 'Order not found'], 404); +// } - // Find shipment only for this order - $shipment = $order->shipments() - ->with(['items' => function ($q) use ($order) { - $q->where('order_id', $order->id); - }]) - ->first(); +// // Find shipment only for this order +// $shipment = $order->shipments() +// ->with(['items' => function ($q) use ($order) { +// $q->where('order_id', $order->id); +// }]) +// ->first(); - return response()->json([ - 'success' => true, - 'shipment' => $shipment - ]); -} +// return response()->json([ +// 'success' => true, +// 'shipment' => $shipment +// ]); +// } public function orderInvoice($order_id) @@ -179,23 +224,35 @@ public function trackOrder($order_id) { $user = JWTAuth::parseToken()->authenticate(); - $order = $user->orders() - ->with('shipments') - ->where('order_id', $order_id) - ->first(); - - if (!$order) { - return response()->json(['success' => false, 'message' => 'Order not found'], 404); + if (!$user) { + return response()->json([ + 'success' => false, + 'message' => 'Unauthorized' + ], 401); } - $shipment = $order->shipments()->first(); + // Ensure the container belongs to this customer via invoice + $invoice = \App\Models\Invoice::where('customer_id', $user->id) + ->where('container_id', $order_id) + ->with('container') + ->first(); + + if (!$invoice || !$invoice->container) { + return response()->json([ + 'success' => false, + 'message' => 'Order not found' + ], 404); + } + + $container = $invoice->container; return response()->json([ 'success' => true, 'track' => [ - 'order_id' => $order->order_id, - 'shipment_status' => $shipment->status ?? 'pending', - 'shipment_date' => $shipment->shipment_date ?? null, + 'order_id' => $container->id, + 'container_number' => $container->container_number, + 'status' => $container->status, + 'container_date' => $container->container_date, ] ]); } @@ -289,44 +346,44 @@ public function invoiceDetails($invoice_id) ]); } -public function confirmOrder($order_id) -{ - $user = JWTAuth::parseToken()->authenticate(); +// public function confirmOrder($order_id) +// { +// $user = JWTAuth::parseToken()->authenticate(); - if (! $user) { - return response()->json([ - 'success' => false, - 'message' => 'Unauthorized' - ], 401); - } +// if (! $user) { +// return response()->json([ +// 'success' => false, +// 'message' => 'Unauthorized' +// ], 401); +// } - $order = $user->orders() - ->where('order_id', $order_id) - ->first(); +// $order = $user->orders() +// ->where('order_id', $order_id) +// ->first(); - if (! $order) { - return response()->json([ - 'success' => false, - 'message' => 'Order not found' - ], 404); - } +// if (! $order) { +// return response()->json([ +// 'success' => false, +// 'message' => 'Order not found' +// ], 404); +// } - // 🚫 Only allow confirm from order_placed - if ($order->status !== 'order_placed') { - return response()->json([ - 'success' => false, - 'message' => 'Order cannot be confirmed' - ], 422); - } +// // 🚫 Only allow confirm from order_placed +// if ($order->status !== 'order_placed') { +// return response()->json([ +// 'success' => false, +// 'message' => 'Order cannot be confirmed' +// ], 422); +// } - $order->status = 'order_confirmed'; - $order->save(); +// $order->status = 'order_confirmed'; +// $order->save(); - return response()->json([ - 'success' => true, - 'message' => 'Order confirmed successfully' - ]); -} +// return response()->json([ +// 'success' => true, +// 'message' => 'Order confirmed successfully' +// ]); +// } diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index bf2e258..76b1c66 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -49,10 +49,10 @@ class Invoice extends Model return $this->hasMany(InvoiceItem::class)->orderBy('id', 'ASC'); } - public function container() - { - return $this->belongsTo(Container::class); - } + // public function container() + // { + // return $this->belongsTo(Container::class); + // } public function customer() { @@ -124,6 +124,10 @@ class Invoice extends Model { return $this->status === 'paid'; } - + + public function container() +{ + return $this->belongsTo(\App\Models\Container::class, 'container_id'); +} } diff --git a/app/Models/User.php b/app/Models/User.php index d8afb94..85acff5 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -89,10 +89,7 @@ class User extends Authenticatable implements JWTSubject { return []; } - public function invoices() - { - return $this->hasMany(\App\Models\Invoice::class, 'customer_id', 'customer_id'); - } + // App\Models\User.php @@ -108,6 +105,10 @@ public function invoiceInstallments() ); } +public function invoices() +{ + return $this->hasMany(\App\Models\Invoice::class, 'customer_id', 'id'); +} } diff --git a/public/invoices/invoice-INV-2026-000012.pdf b/public/invoices/invoice-INV-2026-000012.pdf new file mode 100644 index 0000000..5b1ad53 Binary files /dev/null and b/public/invoices/invoice-INV-2026-000012.pdf differ diff --git a/public/invoices/invoice-INV-2026-000013.pdf b/public/invoices/invoice-INV-2026-000013.pdf new file mode 100644 index 0000000..d0c97d7 Binary files /dev/null and b/public/invoices/invoice-INV-2026-000013.pdf differ diff --git a/public/invoices/invoice-INV-2026-000014.pdf b/public/invoices/invoice-INV-2026-000014.pdf new file mode 100644 index 0000000..fd33b6d Binary files /dev/null and b/public/invoices/invoice-INV-2026-000014.pdf differ diff --git a/public/invoices/invoice-INV-2026-000015.pdf b/public/invoices/invoice-INV-2026-000015.pdf new file mode 100644 index 0000000..365a30d Binary files /dev/null and b/public/invoices/invoice-INV-2026-000015.pdf differ