2025-11-14 13:55:01 +05:30
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use App\Models\Shipment;
|
|
|
|
|
use App\Models\ShipmentItem;
|
|
|
|
|
use App\Models\Order;
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
|
|
|
|
|
class ShipmentController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Show shipment page (Create Shipment + Shipment List)
|
|
|
|
|
*/
|
2025-12-01 10:38:52 +05:30
|
|
|
public function index()
|
2025-11-14 13:55:01 +05:30
|
|
|
{
|
|
|
|
|
// 1) Get all used order IDs
|
|
|
|
|
$usedOrderIds = ShipmentItem::pluck('order_id')->toArray();
|
|
|
|
|
|
|
|
|
|
// 2) Load available orders (not used in any shipment)
|
|
|
|
|
$availableOrders = Order::whereNotIn('id', $usedOrderIds)->get();
|
|
|
|
|
|
|
|
|
|
// 3) Load all shipments for listing
|
|
|
|
|
$shipments = Shipment::latest()->get();
|
|
|
|
|
|
|
|
|
|
// Return your file: resources/views/admin/shipment.blade.php
|
|
|
|
|
return view('admin.shipments', compact('availableOrders', 'shipments'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Store new shipment
|
|
|
|
|
*/
|
|
|
|
|
public function store(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$request->validate([
|
|
|
|
|
'origin' => 'required|string',
|
|
|
|
|
'destination' => 'required|string',
|
|
|
|
|
'shipment_date' => 'required|date',
|
|
|
|
|
'order_ids' => 'required|array|min:1',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// -----------------------------
|
|
|
|
|
// PREVENT DUPLICATE ORDERS
|
|
|
|
|
// -----------------------------
|
|
|
|
|
foreach ($request->order_ids as $id) {
|
|
|
|
|
if (ShipmentItem::where('order_id', $id)->exists()) {
|
|
|
|
|
return back()->with('error', "Order ID $id is already assigned to a shipment.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------
|
|
|
|
|
// GENERATE UNIQUE SHIPMENT ID
|
|
|
|
|
// -----------------------------
|
|
|
|
|
$year = date('y');
|
|
|
|
|
$prefix = "SHIP-$year-";
|
|
|
|
|
|
|
|
|
|
$lastShipment = Shipment::latest('id')->first();
|
|
|
|
|
$nextNumber = $lastShipment ? intval(substr($lastShipment->shipment_id, -8)) + 1 : 1;
|
|
|
|
|
|
|
|
|
|
$newShipmentId = $prefix . str_pad($nextNumber, 8, '0', STR_PAD_LEFT);
|
|
|
|
|
|
|
|
|
|
// -----------------------------
|
|
|
|
|
// CALCULATE TOTALS
|
|
|
|
|
// -----------------------------
|
|
|
|
|
$orders = Order::whereIn('id', $request->order_ids)->get();
|
|
|
|
|
|
|
|
|
|
$total_ctn = $orders->sum('ctn');
|
|
|
|
|
$total_qty = $orders->sum('qty');
|
|
|
|
|
$total_ttl_qty = $orders->sum('ttl_qty');
|
|
|
|
|
$total_amount = $orders->sum('ttl_amount');
|
|
|
|
|
$total_cbm = $orders->sum('cbm');
|
|
|
|
|
$total_ttl_cbm = $orders->sum('ttl_cbm');
|
|
|
|
|
$total_kg = $orders->sum('kg');
|
|
|
|
|
$total_ttl_kg = $orders->sum('ttl_kg');
|
|
|
|
|
|
|
|
|
|
// -----------------------------
|
|
|
|
|
// CREATE SHIPMENT
|
|
|
|
|
//-------------------------------
|
|
|
|
|
$shipment = Shipment::create([
|
|
|
|
|
'shipment_id' => $newShipmentId,
|
|
|
|
|
'origin' => $request->origin,
|
|
|
|
|
'destination' => $request->destination,
|
|
|
|
|
'status' => Shipment::STATUS_PENDING,
|
|
|
|
|
'shipment_date' => $request->shipment_date,
|
|
|
|
|
|
|
|
|
|
'total_ctn' => $total_ctn,
|
|
|
|
|
'total_qty' => $total_qty,
|
|
|
|
|
'total_ttl_qty' => $total_ttl_qty,
|
|
|
|
|
'total_amount' => $total_amount,
|
|
|
|
|
'total_cbm' => $total_cbm,
|
|
|
|
|
'total_ttl_cbm' => $total_ttl_cbm,
|
|
|
|
|
'total_kg' => $total_kg,
|
|
|
|
|
'total_ttl_kg' => $total_ttl_kg,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// -----------------------------
|
|
|
|
|
// INSERT SHIPMENT ITEMS
|
|
|
|
|
// -----------------------------
|
|
|
|
|
foreach ($orders as $order) {
|
|
|
|
|
ShipmentItem::create([
|
|
|
|
|
'shipment_id' => $shipment->id,
|
|
|
|
|
'order_id' => $order->id,
|
|
|
|
|
'order_ctn' => $order->ctn,
|
|
|
|
|
'order_qty' => $order->qty,
|
|
|
|
|
'order_ttl_qty' => $order->ttl_qty,
|
|
|
|
|
'order_ttl_amount' => $order->ttl_amount,
|
|
|
|
|
'order_ttl_kg' => $order->ttl_kg,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return redirect()->back()->with('success', "Shipment $newShipmentId created successfully!");
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-04 11:21:46 +05:30
|
|
|
/**
|
|
|
|
|
* Show shipment details (for modal popup)
|
|
|
|
|
*/
|
2025-11-14 13:55:01 +05:30
|
|
|
public function show($id)
|
|
|
|
|
{
|
2025-12-04 11:21:46 +05:30
|
|
|
$shipment = Shipment::findOrFail($id);
|
2025-12-03 15:36:04 +05:30
|
|
|
|
2025-12-04 11:21:46 +05:30
|
|
|
// Load full order data from orders table
|
|
|
|
|
$orders = Order::whereIn('id',
|
|
|
|
|
ShipmentItem::where('shipment_id', $id)->pluck('order_id')
|
|
|
|
|
)->get();
|
2025-12-03 15:36:04 +05:30
|
|
|
|
2025-12-04 11:21:46 +05:30
|
|
|
return response()->json([
|
2025-11-14 13:55:01 +05:30
|
|
|
'shipment' => $shipment,
|
2025-12-04 11:21:46 +05:30
|
|
|
'orders' => $orders
|
2025-11-14 13:55:01 +05:30
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update Shipment status from action button
|
|
|
|
|
*/
|
|
|
|
|
public function updateStatus(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$request->validate([
|
|
|
|
|
'shipment_id' => 'required|exists:shipments,id',
|
|
|
|
|
'status' => 'required|string'
|
|
|
|
|
]);
|
|
|
|
|
|
2025-11-21 16:07:43 +05:30
|
|
|
// 1) Update shipment status
|
2025-11-14 13:55:01 +05:30
|
|
|
$shipment = Shipment::findOrFail($request->shipment_id);
|
|
|
|
|
$shipment->status = $request->status;
|
|
|
|
|
$shipment->save();
|
|
|
|
|
|
2025-11-21 16:07:43 +05:30
|
|
|
// 2) Update ALL related orders' status
|
|
|
|
|
foreach ($shipment->orders as $order) {
|
|
|
|
|
$order->status = $shipment->status; // status is string: pending, in_transit, dispatched, delivered
|
|
|
|
|
$order->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return redirect()->back()->with(
|
|
|
|
|
'success',
|
|
|
|
|
"Shipment status updated to {$shipment->statusLabel()} and related orders updated."
|
|
|
|
|
);
|
2025-11-14 13:55:01 +05:30
|
|
|
}
|
|
|
|
|
|
2025-12-01 10:38:52 +05:30
|
|
|
/**
|
|
|
|
|
* Update shipment details
|
|
|
|
|
*/
|
|
|
|
|
public function update(Request $request, $id)
|
|
|
|
|
{
|
|
|
|
|
$shipment = Shipment::findOrFail($id);
|
|
|
|
|
|
|
|
|
|
$data = $request->validate([
|
|
|
|
|
'origin' => 'required|string',
|
|
|
|
|
'destination' => 'required|string',
|
|
|
|
|
'shipment_date' => 'required|date',
|
|
|
|
|
'status' => 'required|string',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$shipment->update($data);
|
|
|
|
|
|
|
|
|
|
// If it's an AJAX request, return JSON response
|
|
|
|
|
if ($request->ajax() || $request->wantsJson()) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => 'Shipment updated successfully.'
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return redirect()->back()->with('success', 'Shipment updated successfully.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete shipment permanently
|
|
|
|
|
*/
|
|
|
|
|
public function destroy($id, Request $request)
|
|
|
|
|
{
|
|
|
|
|
$shipment = Shipment::findOrFail($id);
|
|
|
|
|
|
|
|
|
|
// Delete shipment items
|
|
|
|
|
ShipmentItem::where('shipment_id', $shipment->id)->delete();
|
|
|
|
|
|
|
|
|
|
// Delete shipment itself
|
|
|
|
|
$shipment->delete();
|
2025-11-21 16:07:43 +05:30
|
|
|
|
2025-12-01 10:38:52 +05:30
|
|
|
// If it's an AJAX request, return JSON response
|
|
|
|
|
if ($request->ajax() || $request->wantsJson()) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => 'Shipment deleted successfully.'
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return redirect()->route('admin.shipments')
|
|
|
|
|
->with('success', 'Shipment deleted successfully.');
|
|
|
|
|
}
|
2025-12-08 10:17:46 +05:30
|
|
|
|
|
|
|
|
public function dummy($id)
|
|
|
|
|
{
|
|
|
|
|
// Load shipment
|
|
|
|
|
$shipment = Shipment::with('orders')->findOrFail($id);
|
|
|
|
|
|
|
|
|
|
// Dummy data (you can modify anytime)
|
|
|
|
|
$dummyData = [
|
|
|
|
|
'title' => 'Dummy Shipment Preview',
|
|
|
|
|
'generated_on' => now()->format('d M Y h:i A'),
|
|
|
|
|
'note' => 'This is dummy shipment information for testing page layout.'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return view('admin.view_shipment', compact('shipment', 'dummyData'));
|
|
|
|
|
}
|
2025-12-19 11:00:34 +05:30
|
|
|
// App\Models\Shipment.php
|
|
|
|
|
|
|
|
|
|
public function orders()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(\App\Models\Order::class, 'shipment_items', 'shipment_id', 'order_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function removeOrder(Shipment $shipment, Order $order)
|
|
|
|
|
{
|
|
|
|
|
// Remove row from pivot table shipment_items
|
|
|
|
|
ShipmentItem::where('shipment_id', $shipment->id)
|
|
|
|
|
->where('order_id', $order->id)
|
|
|
|
|
->delete(); // removes link shipment <-> order [web:41][web:45]
|
|
|
|
|
|
|
|
|
|
// Recalculate totals on this shipment (optional but recommended)
|
|
|
|
|
$orders = Order::whereIn(
|
|
|
|
|
'id',
|
|
|
|
|
ShipmentItem::where('shipment_id', $shipment->id)->pluck('order_id')
|
|
|
|
|
)->get();
|
|
|
|
|
|
|
|
|
|
$shipment->total_ctn = $orders->sum('ctn');
|
|
|
|
|
$shipment->total_qty = $orders->sum('qty');
|
|
|
|
|
$shipment->total_ttl_qty = $orders->sum('ttl_qty');
|
|
|
|
|
$shipment->total_cbm = $orders->sum('cbm');
|
|
|
|
|
$shipment->total_ttl_cbm = $orders->sum('ttl_cbm');
|
|
|
|
|
$shipment->total_kg = $orders->sum('kg');
|
|
|
|
|
$shipment->total_ttl_kg = $orders->sum('ttl_kg');
|
|
|
|
|
$shipment->total_amount = $orders->sum('ttl_amount');
|
|
|
|
|
$shipment->save();
|
|
|
|
|
|
|
|
|
|
// Redirect back to preview page where your blade is loaded
|
|
|
|
|
return redirect()
|
|
|
|
|
->route('admin.shipments.dummy', $shipment->id)
|
|
|
|
|
->with('success', 'Order removed from shipment successfully.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function addOrders(Request $request, Shipment $shipment)
|
|
|
|
|
{
|
|
|
|
|
$request->validate([
|
|
|
|
|
'order_ids' => 'required|array|min:1',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// फक्त न वापरलेले orders घ्या
|
|
|
|
|
$orders = Order::whereIn('id', $request->order_ids)->get();
|
|
|
|
|
|
|
|
|
|
foreach ($orders as $order) {
|
|
|
|
|
// pivot मध्ये insert
|
|
|
|
|
ShipmentItem::create([
|
|
|
|
|
'shipment_id' => $shipment->id,
|
|
|
|
|
'order_id' => $order->id,
|
|
|
|
|
'order_ctn' => $order->ctn,
|
|
|
|
|
'order_qty' => $order->qty,
|
|
|
|
|
'order_ttl_qty' => $order->ttl_qty,
|
|
|
|
|
'order_ttl_amount' => $order->ttl_amount,
|
|
|
|
|
'order_ttl_kg' => $order->ttl_kg,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// totals
|
|
|
|
|
$orderIds = ShipmentItem::where('shipment_id', $shipment->id)->pluck('order_id');
|
|
|
|
|
$allOrders = Order::whereIn('id', $orderIds)->get();
|
|
|
|
|
|
|
|
|
|
$shipment->total_ctn = $allOrders->sum('ctn');
|
|
|
|
|
$shipment->total_qty = $allOrders->sum('qty');
|
|
|
|
|
$shipment->total_ttl_qty = $allOrders->sum('ttl_qty');
|
|
|
|
|
$shipment->total_cbm = $allOrders->sum('cbm');
|
|
|
|
|
$shipment->total_ttl_cbm = $allOrders->sum('ttl_cbm');
|
|
|
|
|
$shipment->total_kg = $allOrders->sum('kg');
|
|
|
|
|
$shipment->total_ttl_kg = $allOrders->sum('ttl_kg');
|
|
|
|
|
$shipment->total_amount = $allOrders->sum('ttl_amount');
|
|
|
|
|
$shipment->save();
|
|
|
|
|
|
|
|
|
|
return redirect()
|
|
|
|
|
->route('admin.shipments.dummy', $shipment->id)
|
|
|
|
|
->with('success', 'Orders added to shipment successfully.');
|
|
|
|
|
}
|
2025-12-08 10:17:46 +05:30
|
|
|
|
2025-12-01 10:38:52 +05:30
|
|
|
}
|