This commit is contained in:
Utkarsh Khedkar
2026-03-13 23:06:51 +05:30
7 changed files with 164 additions and 102 deletions

View File

@@ -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(); $totalOrders = $containers->count();
$delivered = $orders->where('status', 'delivered')->count();
$inTransit = $orders->where('status', '!=', 'delivered')->count(); $delivered = $containers->where('status', 'delivered')->count();
$inTransit = $containers->whereNotIn('status', [
'delivered',
'warehouse',
'domestic-distribution'
])->count();
$active = $totalOrders; $active = $totalOrders;
// ------------------------------------- // -------------------------------------
// Total Amount = Invoice.total_with_gst // Total Amount = sum of invoice totals
// ------------------------------------- // -------------------------------------
$totalAmount = $orders->sum(function ($o) { $totalAmount = $invoices->sum(function ($invoice) {
return $o->invoice->final_amount_with_gst ?? 0; return $invoice->final_amount_with_gst ?? 0;
}); });
// Format total amount in K, L, Cr // Format total amount in K, L, Cr
@@ -45,13 +55,12 @@ class UserOrderController extends Controller
return response()->json([ return response()->json([
'status' => true, 'status' => true,
'summary' => [ 'summary' => [
'active_orders' => $active, 'active_orders' => $active,
'in_transit_orders' => $inTransit, 'in_transit_orders' => $inTransit,
'delivered_orders' => $delivered, 'delivered_orders' => $delivered,
'total_value' => $formattedAmount, // formatted value 'total_value' => $formattedAmount,
'total_raw' => $totalAmount // original value 'total_raw' => $totalAmount
] ]
]); ]);
} }
@@ -90,18 +99,26 @@ class UserOrderController extends Controller
], 401); ], 401);
} }
// Fetch orders for this user // Get invoices with containers for this customer
$orders = $user->orders() $invoices = $user->invoices()
->with(['invoice', 'shipments']) ->with('container')
->orderBy('id', 'desc') ->orderBy('id', 'desc')
->get() ->get();
->map(function ($o) {
// Extract unique containers
$containers = $invoices->pluck('container')
->filter()
->unique('id')
->values();
$orders = $containers->map(function ($container) {
return [ return [
'order_id' => $o->order_id, 'order_id' => $container->id,
'status' => $o->status, 'container_number' => $container->container_number,
'amount' => $o->ttl_amount, 'status' => $container->status,
'description'=> "Order from {$o->origin} to {$o->destination}", 'container_date' => $container->container_date,
'created_at' => $o->created_at, 'created_at' => $container->created_at,
]; ];
}); });
@@ -115,45 +132,73 @@ public function orderDetails($order_id)
{ {
$user = JWTAuth::parseToken()->authenticate(); $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']) ->with(['items'])
->where('order_id', $order_id)
->first(); ->first();
if (!$order) { if (!$invoice) {
return response()->json(['success' => false, 'message' => 'Order not found'], 404); return response()->json([
'success' => false,
'message' => 'Order not found for this user'
], 404);
} }
return response()->json([ return response()->json([
'success' => true, '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) // public function orderShipment($order_id)
{ // {
$user = JWTAuth::parseToken()->authenticate(); // $user = JWTAuth::parseToken()->authenticate();
// Get order // // Get order
$order = $user->orders()->where('order_id', $order_id)->first(); // $order = $user->orders()->where('order_id', $order_id)->first();
if (!$order) { // if (!$order) {
return response()->json(['success' => false, 'message' => 'Order not found'], 404); // return response()->json(['success' => false, 'message' => 'Order not found'], 404);
} // }
// Find shipment only for this order // // Find shipment only for this order
$shipment = $order->shipments() // $shipment = $order->shipments()
->with(['items' => function ($q) use ($order) { // ->with(['items' => function ($q) use ($order) {
$q->where('order_id', $order->id); // $q->where('order_id', $order->id);
}]) // }])
->first(); // ->first();
return response()->json([ // return response()->json([
'success' => true, // 'success' => true,
'shipment' => $shipment // 'shipment' => $shipment
]); // ]);
} // }
public function orderInvoice($order_id) public function orderInvoice($order_id)
@@ -179,23 +224,35 @@ public function trackOrder($order_id)
{ {
$user = JWTAuth::parseToken()->authenticate(); $user = JWTAuth::parseToken()->authenticate();
$order = $user->orders() if (!$user) {
->with('shipments') return response()->json([
->where('order_id', $order_id) 'success' => false,
->first(); 'message' => 'Unauthorized'
], 401);
if (!$order) {
return response()->json(['success' => false, 'message' => 'Order not found'], 404);
} }
$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([ return response()->json([
'success' => true, 'success' => true,
'track' => [ 'track' => [
'order_id' => $order->order_id, 'order_id' => $container->id,
'shipment_status' => $shipment->status ?? 'pending', 'container_number' => $container->container_number,
'shipment_date' => $shipment->shipment_date ?? null, 'status' => $container->status,
'container_date' => $container->container_date,
] ]
]); ]);
} }
@@ -289,44 +346,44 @@ public function invoiceDetails($invoice_id)
]); ]);
} }
public function confirmOrder($order_id) // public function confirmOrder($order_id)
{ // {
$user = JWTAuth::parseToken()->authenticate(); // $user = JWTAuth::parseToken()->authenticate();
if (! $user) { // if (! $user) {
return response()->json([ // return response()->json([
'success' => false, // 'success' => false,
'message' => 'Unauthorized' // 'message' => 'Unauthorized'
], 401); // ], 401);
} // }
$order = $user->orders() // $order = $user->orders()
->where('order_id', $order_id) // ->where('order_id', $order_id)
->first(); // ->first();
if (! $order) { // if (! $order) {
return response()->json([ // return response()->json([
'success' => false, // 'success' => false,
'message' => 'Order not found' // 'message' => 'Order not found'
], 404); // ], 404);
} // }
// 🚫 Only allow confirm from order_placed // // 🚫 Only allow confirm from order_placed
if ($order->status !== 'order_placed') { // if ($order->status !== 'order_placed') {
return response()->json([ // return response()->json([
'success' => false, // 'success' => false,
'message' => 'Order cannot be confirmed' // 'message' => 'Order cannot be confirmed'
], 422); // ], 422);
} // }
$order->status = 'order_confirmed'; // $order->status = 'order_confirmed';
$order->save(); // $order->save();
return response()->json([ // return response()->json([
'success' => true, // 'success' => true,
'message' => 'Order confirmed successfully' // 'message' => 'Order confirmed successfully'
]); // ]);
} // }

View File

@@ -49,10 +49,10 @@ class Invoice extends Model
return $this->hasMany(InvoiceItem::class)->orderBy('id', 'ASC'); return $this->hasMany(InvoiceItem::class)->orderBy('id', 'ASC');
} }
public function container() // public function container()
{ // {
return $this->belongsTo(Container::class); // return $this->belongsTo(Container::class);
} // }
public function customer() public function customer()
{ {
@@ -125,5 +125,9 @@ class Invoice extends Model
return $this->status === 'paid'; return $this->status === 'paid';
} }
public function container()
{
return $this->belongsTo(\App\Models\Container::class, 'container_id');
}
} }

View File

@@ -89,10 +89,7 @@ class User extends Authenticatable implements JWTSubject
{ {
return []; return [];
} }
public function invoices()
{
return $this->hasMany(\App\Models\Invoice::class, 'customer_id', 'customer_id');
}
// App\Models\User.php // App\Models\User.php
@@ -108,6 +105,10 @@ public function invoiceInstallments()
); );
} }
public function invoices()
{
return $this->hasMany(\App\Models\Invoice::class, 'customer_id', 'id');
}
} }

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.