container and invoice api done small api like view invoice traking in order section is remaning
This commit is contained in:
@@ -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();
|
||||||
$active = $totalOrders;
|
|
||||||
|
$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) {
|
$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,20 +99,28 @@ 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) {
|
|
||||||
return [
|
// Extract unique containers
|
||||||
'order_id' => $o->order_id,
|
$containers = $invoices->pluck('container')
|
||||||
'status' => $o->status,
|
->filter()
|
||||||
'amount' => $o->ttl_amount,
|
->unique('id')
|
||||||
'description'=> "Order from {$o->origin} to {$o->destination}",
|
->values();
|
||||||
'created_at' => $o->created_at,
|
|
||||||
];
|
$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([
|
return response()->json([
|
||||||
'success' => true,
|
'success' => true,
|
||||||
@@ -115,45 +132,63 @@ public function orderDetails($order_id)
|
|||||||
{
|
{
|
||||||
$user = JWTAuth::parseToken()->authenticate();
|
$user = JWTAuth::parseToken()->authenticate();
|
||||||
|
|
||||||
$order = $user->orders()
|
if (!$user) {
|
||||||
->with(['items'])
|
return response()->json([
|
||||||
->where('order_id', $order_id)
|
'success' => false,
|
||||||
|
'message' => 'Unauthorized'
|
||||||
|
], 401);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find invoice for this customer and container
|
||||||
|
$invoice = \App\Models\Invoice::where('customer_id', $user->id)
|
||||||
|
->where('container_id', $order_id)
|
||||||
|
->with(['items', 'container'])
|
||||||
->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'
|
||||||
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'success' => true,
|
'success' => true,
|
||||||
'order' => $order
|
'order' => [
|
||||||
|
'container_id' => $invoice->container->id ?? null,
|
||||||
|
'container_number' => $invoice->container->container_number ?? null,
|
||||||
|
'container_date' => $invoice->container->container_date ?? null,
|
||||||
|
'status' => $invoice->container->status ?? null,
|
||||||
|
'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 +214,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 +336,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'
|
||||||
]);
|
// ]);
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -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');
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
public/invoices/invoice-INV-2026-000012.pdf
Normal file
BIN
public/invoices/invoice-INV-2026-000012.pdf
Normal file
Binary file not shown.
BIN
public/invoices/invoice-INV-2026-000013.pdf
Normal file
BIN
public/invoices/invoice-INV-2026-000013.pdf
Normal file
Binary file not shown.
BIN
public/invoices/invoice-INV-2026-000014.pdf
Normal file
BIN
public/invoices/invoice-INV-2026-000014.pdf
Normal file
Binary file not shown.
BIN
public/invoices/invoice-INV-2026-000015.pdf
Normal file
BIN
public/invoices/invoice-INV-2026-000015.pdf
Normal file
Binary file not shown.
Reference in New Issue
Block a user