Files
Kent-logistics-Laravel/app/Http/Controllers/user/UserOrderController.php

392 lines
10 KiB
PHP
Raw Normal View History

2025-12-01 11:44:43 +05:30
<?php
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use PHPOpenSourceSaver\JWTAuth\Facades\JWTAuth;
class UserOrderController extends Controller
{
public function orderSummary()
{
// Authenticate user via JWT
$user = JWTAuth::parseToken()->authenticate();
if (!$user) {
return response()->json([
'status' => false,
'message' => 'Unauthorized'
], 401);
}
// -------------------------------------
// Get customer invoices with containers
2025-12-01 11:44:43 +05:30
// -------------------------------------
$invoices = $user->invoices()->with('container')->get();
// Unique containers for this customer
$containers = $invoices->pluck('container')->filter()->unique('id');
2025-12-01 11:44:43 +05:30
// -------------------------------------
// Counts based on container status
2025-12-01 11:44:43 +05:30
// -------------------------------------
$totalOrders = $containers->count();
$delivered = $containers->where('status', 'delivered')->count();
$inTransit = $containers->whereNotIn('status', [
'delivered',
'warehouse',
'domestic-distribution'
])->count();
$active = $totalOrders;
2025-12-01 11:44:43 +05:30
// -------------------------------------
// Total Amount = sum of invoice totals
2025-12-01 11:44:43 +05:30
// -------------------------------------
$totalAmount = $invoices->sum(function ($invoice) {
return $invoice->final_amount_with_gst ?? 0;
2025-12-01 11:44:43 +05:30
});
// Format total amount in K, L, Cr
$formattedAmount = $this->formatIndianNumber($totalAmount);
return response()->json([
'status' => true,
'summary' => [
'active_orders' => $active,
'in_transit_orders' => $inTransit,
'delivered_orders' => $delivered,
'total_value' => $formattedAmount,
'total_raw' => $totalAmount
2025-12-01 11:44:43 +05:30
]
]);
}
/**
* Convert number into Indian Format:
* 1000 -> 1K
* 100000 -> 1L
* 10000000 -> 1Cr
*/
private function formatIndianNumber($num)
{
if ($num >= 10000000) {
return round($num / 10000000, 1) . 'Cr';
}
if ($num >= 100000) {
return round($num / 100000, 1) . 'L';
}
if ($num >= 1000) {
return round($num / 1000, 1) . 'K';
}
return (string)$num;
}
public function allOrders()
{
$user = JWTAuth::parseToken()->authenticate();
if (!$user) {
return response()->json([
'success' => false,
'message' => 'Unauthorized'
], 401);
}
// Get invoices with containers for this customer
$invoices = $user->invoices()
->with('container')
2025-12-01 11:44:43 +05:30
->orderBy('id', 'desc')
->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,
];
});
2025-12-01 11:44:43 +05:30
return response()->json([
'success' => true,
'orders' => $orders
]);
}
public function orderDetails($order_id)
{
$user = JWTAuth::parseToken()->authenticate();
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'])
2025-12-01 11:44:43 +05:30
->first();
if (!$invoice) {
return response()->json([
'success' => false,
'message' => 'Order not found for this user'
], 404);
2025-12-01 11:44:43 +05:30
}
return response()->json([
'success' => true,
'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
]
2025-12-01 11:44:43 +05:30
]);
}
// public function orderShipment($order_id)
// {
// $user = JWTAuth::parseToken()->authenticate();
2025-12-01 11:44:43 +05:30
// // Get order
// $order = $user->orders()->where('order_id', $order_id)->first();
2025-12-01 11:44:43 +05:30
// if (!$order) {
// return response()->json(['success' => false, 'message' => 'Order not found'], 404);
// }
2025-12-01 11:44:43 +05:30
// // Find shipment only for this order
// $shipment = $order->shipments()
// ->with(['items' => function ($q) use ($order) {
// $q->where('order_id', $order->id);
// }])
// ->first();
2025-12-01 11:44:43 +05:30
// return response()->json([
// 'success' => true,
// 'shipment' => $shipment
// ]);
// }
2025-12-01 11:44:43 +05:30
public function orderInvoice($order_id)
{
$user = JWTAuth::parseToken()->authenticate();
$order = $user->orders()
->with('invoice.items')
->where('order_id', $order_id)
->first();
if (!$order) {
return response()->json(['success' => false, 'message' => 'Order not found'], 404);
}
return response()->json([
'success' => true,
'invoice' => $order->invoice
]);
}
public function trackOrder($order_id)
{
$user = JWTAuth::parseToken()->authenticate();
if (!$user) {
return response()->json([
'success' => false,
'message' => 'Unauthorized'
], 401);
}
// 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')
2025-12-01 11:44:43 +05:30
->first();
if (!$invoice || !$invoice->container) {
return response()->json([
'success' => false,
'message' => 'Order not found'
], 404);
2025-12-01 11:44:43 +05:30
}
$container = $invoice->container;
2025-12-01 11:44:43 +05:30
return response()->json([
'success' => true,
'track' => [
'order_id' => $container->id,
'container_number' => $container->container_number,
'status' => $container->status,
'container_date' => $container->container_date,
2025-12-01 11:44:43 +05:30
]
]);
}
public function allInvoices()
{
$user = JWTAuth::parseToken()->authenticate();
if (!$user) {
return response()->json([
'success' => false,
'message' => 'Unauthorized'
], 401);
}
// Fetch all invoices of customer
$invoices = $user->invoices()
->withCount('installments')
->orderBy('id', 'desc')
->get()
->map(function ($invoice) {
return [
'invoice_id' => $invoice->id,
'invoice_number' => $invoice->invoice_number,
'invoice_date' => $invoice->invoice_date,
'status' => $invoice->status,
'amount' => $invoice->final_amount_with_gst,
'formatted_amount' => $this->formatIndianNumber($invoice->final_amount_with_gst),
'pdf_url' => $invoice->pdf_path ? url($invoice->pdf_path) : null,
'installment_count' => $invoice->installments_count,
];
});
return response()->json([
'success' => true,
'invoices' => $invoices
]);
}
public function invoiceInstallmentsById($invoice_id)
{
$user = \PHPOpenSourceSaver\JWTAuth\Facades\JWTAuth::parseToken()->authenticate();
if (! $user) {
return response()->json(['success' => false, 'message' => 'Unauthorized'], 401);
}
// Find invoice by numeric id and ensure it belongs to logged-in user (invoice.customer_id = user.id)
$invoice = \App\Models\Invoice::where('id', (int)$invoice_id)
->where('customer_id', $user->id)
->with(['installments' => function($q){
$q->orderBy('installment_date', 'ASC')->orderBy('id', 'ASC');
}])
->first();
if (! $invoice) {
return response()->json([
'success' => false,
'message' => 'Invoice not found for this customer'
], 404);
}
return response()->json([
'success' => true,
'invoice_id' => $invoice->id,
'invoice_number' => $invoice->invoice_number,
'installments' => $invoice->installments
]);
}
public function invoiceDetails($invoice_id)
{
$user = JWTAuth::parseToken()->authenticate();
if (! $user) {
return response()->json(['success' => false, 'message' => 'Unauthorized'], 401);
}
$invoice = \App\Models\Invoice::where('id', $invoice_id)
->where('customer_id', $user->id)
->with('items')
->first();
if (! $invoice) {
return response()->json(['success' => false, 'message' => 'Invoice not found'], 404);
}
return response()->json([
'success' => true,
'invoice' => $invoice
]);
}
// public function confirmOrder($order_id)
// {
// $user = JWTAuth::parseToken()->authenticate();
// if (! $user) {
// return response()->json([
// 'success' => false,
// 'message' => 'Unauthorized'
// ], 401);
// }
// $order = $user->orders()
// ->where('order_id', $order_id)
// ->first();
// 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);
// }
// $order->status = 'order_confirmed';
// $order->save();
// return response()->json([
// 'success' => true,
// 'message' => 'Order confirmed successfully'
// ]);
// }
2025-12-01 11:44:43 +05:30
}