741 lines
25 KiB
PHP
741 lines
25 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\Order;
|
|
use App\Models\OrderItem;
|
|
use App\Models\MarkList;
|
|
use App\Models\Invoice;
|
|
use App\Models\InvoiceItem;
|
|
use App\Models\User;
|
|
use PDF; // barryvdh/laravel-dompdf facade
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use App\Exports\OrdersExport;
|
|
|
|
|
|
class AdminOrderController extends Controller
|
|
{
|
|
// ---------------------------
|
|
// LIST / DASHBOARD
|
|
// ---------------------------
|
|
public function index()
|
|
{
|
|
// raw list for admin dashboard (simple)
|
|
$orders = Order::latest()->get();
|
|
$markList = MarkList::where('status', 'active')->get();
|
|
|
|
return view('admin.dashboard', compact('orders', 'markList'));
|
|
}
|
|
|
|
/**
|
|
* Orders list (detailed)
|
|
*/
|
|
// public function orderShow()
|
|
// {
|
|
// $orders = Order::with(['markList', 'shipments', 'invoice'])
|
|
// ->latest('id')
|
|
// ->get();
|
|
|
|
// return view('admin.orders', compact('orders'));
|
|
// }
|
|
|
|
// ---------------------------
|
|
// CREATE NEW ORDER (simple DB flow)
|
|
// ---------------------------
|
|
/**
|
|
* Show create form (you can place create UI on separate view or dashboard)
|
|
*/
|
|
public function create()
|
|
{
|
|
// return a dedicated create view - create it at resources/views/admin/orders_create.blade.php
|
|
// If you prefer create UI on dashboard, change this to redirect route('admin.orders.index') etc.
|
|
$markList = MarkList::where('status', 'active')->get();
|
|
return view('admin.orders_create', compact('markList'));
|
|
}
|
|
|
|
/**
|
|
* Store a new order and optionally create initial invoice
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$data = $request->validate([
|
|
'mark_no' => 'required|string',
|
|
'origin' => 'nullable|string',
|
|
'destination' => 'nullable|string',
|
|
// totals optional when creating without items
|
|
'ctn' => 'nullable|numeric',
|
|
'qty' => 'nullable|numeric',
|
|
'ttl_qty' => 'nullable|numeric',
|
|
'ttl_amount' => 'nullable|numeric',
|
|
'cbm' => 'nullable|numeric',
|
|
'ttl_cbm' => 'nullable|numeric',
|
|
'kg' => 'nullable|numeric',
|
|
'ttl_kg' => 'nullable|numeric',
|
|
]);
|
|
|
|
$order = Order::create([
|
|
'order_id' => $this->generateOrderId(),
|
|
'mark_no' => $data['mark_no'],
|
|
'origin' => $data['origin'] ?? null,
|
|
'destination' => $data['destination'] ?? null,
|
|
'ctn' => $data['ctn'] ?? 0,
|
|
'qty' => $data['qty'] ?? 0,
|
|
'ttl_qty' => $data['ttl_qty'] ?? 0,
|
|
'ttl_amount' => $data['ttl_amount'] ?? 0,
|
|
'cbm' => $data['cbm'] ?? 0,
|
|
'ttl_cbm' => $data['ttl_cbm'] ?? 0,
|
|
'kg' => $data['kg'] ?? 0,
|
|
'ttl_kg' => $data['ttl_kg'] ?? 0,
|
|
'status' => 'pending',
|
|
]);
|
|
|
|
//If you want to auto-create an invoice at order creation, uncomment:
|
|
$this->createInvoice($order);
|
|
|
|
return redirect()->route('admin.orders.show', $order->id)
|
|
->with('success', 'Order created successfully.');
|
|
}
|
|
|
|
// ---------------------------
|
|
// SHOW / POPUP
|
|
// ---------------------------
|
|
public function show($id)
|
|
{
|
|
$order = Order::with('items', 'markList')->findOrFail($id);
|
|
$user = $this->getCustomerFromOrder($order);
|
|
|
|
return view('admin.orders_show', compact('order', 'user'));
|
|
}
|
|
|
|
// public function popup($id)
|
|
// {
|
|
// $order = Order::with(['items', 'markList'])->findOrFail($id);
|
|
// $user = $this->getCustomerFromOrder($order);
|
|
|
|
// return view('admin.popup', compact('order', 'user'));
|
|
// }
|
|
|
|
// ---------------------------
|
|
// ORDER ITEM MANAGEMENT (DB)
|
|
// ---------------------------
|
|
/**
|
|
* Add an item to an existing order
|
|
*/
|
|
public function addItem(Request $request, $orderId)
|
|
{
|
|
$order = Order::findOrFail($orderId);
|
|
|
|
$data = $request->validate([
|
|
'description' => 'required|string',
|
|
'ctn' => 'nullable|numeric',
|
|
'qty' => 'nullable|numeric',
|
|
'ttl_qty' => 'nullable|numeric',
|
|
'unit' => 'nullable|string',
|
|
'price' => 'nullable|numeric',
|
|
'ttl_amount' => 'nullable|numeric',
|
|
'cbm' => 'nullable|numeric',
|
|
'ttl_cbm' => 'nullable|numeric',
|
|
'kg' => 'nullable|numeric',
|
|
'ttl_kg' => 'nullable|numeric',
|
|
'shop_no' => 'nullable|string',
|
|
]);
|
|
|
|
$data['order_id'] = $order->id;
|
|
|
|
OrderItem::create($data);
|
|
|
|
// recalc totals and save to order
|
|
$this->recalcTotals($order);
|
|
$this->updateInvoiceFromOrder($order); // <-- NEW
|
|
|
|
return redirect()->back()->with('success', 'Item added and totals updated.');
|
|
}
|
|
|
|
/**
|
|
* Soft-delete an order item and recalc totals
|
|
*/
|
|
public function deleteItem($id)
|
|
{
|
|
$item = OrderItem::findOrFail($id);
|
|
$order = $item->order;
|
|
|
|
$item->delete(); // soft delete
|
|
|
|
// recalc totals
|
|
$this->recalcTotals($order);
|
|
$this->updateInvoiceFromOrder($order);
|
|
|
|
|
|
return redirect()->back()->with('success', 'Item deleted and totals updated.');
|
|
}
|
|
|
|
/**
|
|
* Restore soft-deleted item and recalc totals
|
|
*/
|
|
public function restoreItem($id)
|
|
{
|
|
$item = OrderItem::withTrashed()->findOrFail($id);
|
|
$order = Order::findOrFail($item->order_id);
|
|
|
|
$item->restore();
|
|
|
|
// recalc totals
|
|
$this->recalcTotals($order);
|
|
$this->updateInvoiceFromOrder($order);
|
|
|
|
|
|
return redirect()->back()->with('success', 'Item restored and totals updated.');
|
|
}
|
|
|
|
// ---------------------------
|
|
// ORDER CRUD: update / destroy
|
|
// ---------------------------
|
|
public function update(Request $request, $id)
|
|
{
|
|
$order = Order::findOrFail($id);
|
|
|
|
$data = $request->validate([
|
|
'mark_no' => 'required|string',
|
|
'origin' => 'nullable|string',
|
|
'destination' => 'nullable|string',
|
|
]);
|
|
|
|
$order->update([
|
|
'mark_no' => $data['mark_no'],
|
|
'origin' => $data['origin'] ?? null,
|
|
'destination' => $data['destination'] ?? null,
|
|
]);
|
|
|
|
// optionally recalc totals (not necessary unless you change item-level fields here)
|
|
$this->recalcTotals($order);
|
|
|
|
return redirect()->route('admin.orders.show', $order->id)
|
|
->with('success', 'Order updated successfully.');
|
|
}
|
|
|
|
/**
|
|
* Soft-delete whole order and its items (soft-delete items first then order)
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
$order = Order::findOrFail($id);
|
|
|
|
// soft-delete items first (so they show up in onlyTrashed for restore)
|
|
OrderItem::where('order_id', $order->id)->delete();
|
|
|
|
// then soft-delete order
|
|
$order->delete();
|
|
|
|
return redirect()->route('admin.orders.index')
|
|
->with('success', 'Order deleted successfully.');
|
|
}
|
|
|
|
// ---------------------------
|
|
// HELPERS
|
|
// ---------------------------
|
|
/**
|
|
* Recalculate totals for the order from current (non-deleted) items
|
|
*/
|
|
private function recalcTotals(Order $order)
|
|
{
|
|
// make sure we re-query live items (non-deleted)
|
|
$items = $order->items()->get();
|
|
|
|
$order->update([
|
|
'ctn' => (int) $items->sum(fn($i) => (int) ($i->ctn ?? 0)),
|
|
'qty' => (int) $items->sum(fn($i) => (int) ($i->qty ?? 0)),
|
|
'ttl_qty' => (int) $items->sum(fn($i) => (int) ($i->ttl_qty ?? 0)),
|
|
'ttl_amount'=> (float) $items->sum(fn($i) => (float) ($i->ttl_amount ?? 0)),
|
|
'cbm' => (float) $items->sum(fn($i) => (float) ($i->cbm ?? 0)),
|
|
'ttl_cbm' => (float) $items->sum(fn($i) => (float) ($i->ttl_cbm ?? 0)),
|
|
'kg' => (float) $items->sum(fn($i) => (float) ($i->kg ?? 0)),
|
|
'ttl_kg' => (float) $items->sum(fn($i) => (float) ($i->ttl_kg ?? 0)),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Generate order id (keeps old format)
|
|
*/
|
|
private function generateOrderId()
|
|
{
|
|
$year = date('y');
|
|
$prefix = "KNT-$year-";
|
|
|
|
$lastOrder = Order::latest('id')->first();
|
|
$nextNumber = $lastOrder ? intval(substr($lastOrder->order_id, -8)) + 1 : 1;
|
|
|
|
return $prefix . str_pad($nextNumber, 8, '0', STR_PAD_LEFT);
|
|
}
|
|
|
|
// ---------------------------
|
|
// INVOICE CREATION (optional helper used by store/finish)
|
|
// ---------------------------
|
|
private function createInvoice(Order $order)
|
|
{
|
|
$invoiceNumber = $this->generateInvoiceNumber();
|
|
$customer = $this->getCustomerFromMarkList($order->mark_no);
|
|
$totalAmount = $order->ttl_amount;
|
|
|
|
$invoice = Invoice::create([
|
|
'order_id' => $order->id,
|
|
'customer_id' => $customer->id ?? null,
|
|
'mark_no' => $order->mark_no,
|
|
'invoice_number' => $invoiceNumber,
|
|
'invoice_date' => now(),
|
|
'due_date' => now()->addDays(10),
|
|
'payment_method' => null,
|
|
'reference_no' => null,
|
|
'status' => 'pending',
|
|
'final_amount' => $totalAmount,
|
|
'gst_percent' => 0,
|
|
'gst_amount' => 0,
|
|
'final_amount_with_gst' => $totalAmount,
|
|
'customer_name' => $customer->customer_name ?? null,
|
|
'company_name' => $customer->company_name ?? null,
|
|
'customer_email' => $customer->email ?? null,
|
|
'customer_mobile' => $customer->mobile_no ?? null,
|
|
'customer_address' => $customer->address ?? null,
|
|
'pincode' => $customer->pincode ?? null,
|
|
'notes' => null,
|
|
'pdf_path' => null,
|
|
]);
|
|
|
|
// clone order items into invoice items
|
|
foreach ($order->items as $item) {
|
|
InvoiceItem::create([
|
|
'invoice_id' => $invoice->id,
|
|
'description' => $item->description,
|
|
'ctn' => $item->ctn,
|
|
'qty' => $item->qty,
|
|
'ttl_qty' => $item->ttl_qty,
|
|
'unit' => $item->unit,
|
|
'price' => $item->price,
|
|
'ttl_amount' => $item->ttl_amount,
|
|
'cbm' => $item->cbm,
|
|
'ttl_cbm' => $item->ttl_cbm,
|
|
'kg' => $item->kg,
|
|
'ttl_kg' => $item->ttl_kg,
|
|
'shop_no' => $item->shop_no,
|
|
]);
|
|
}
|
|
}
|
|
|
|
private function generateInvoiceNumber()
|
|
{
|
|
$lastInvoice = Invoice::latest()->first();
|
|
$nextInvoice = $lastInvoice ? $lastInvoice->id + 1 : 1;
|
|
|
|
return 'INV-' . date('Y') . '-' . str_pad($nextInvoice, 6, '0', STR_PAD_LEFT);
|
|
}
|
|
|
|
private function getCustomerFromMarkList($markNo)
|
|
{
|
|
$markList = MarkList::where('mark_no', $markNo)->first();
|
|
|
|
if ($markList && $markList->customer_id) {
|
|
return User::where('customer_id', $markList->customer_id)->first();
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private function getCustomerFromOrder($order)
|
|
{
|
|
if ($order->markList && $order->markList->customer_id) {
|
|
return User::where('customer_id', $order->markList->customer_id)->first();
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function popup($id)
|
|
{
|
|
// Load order with items + markList
|
|
$order = Order::with(['items', 'markList'])->findOrFail($id);
|
|
|
|
// Fetch user based on markList customer_id (same as show method)
|
|
$user = null;
|
|
if ($order->markList && $order->markList->customer_id) {
|
|
$user = \App\Models\User::where('customer_id', $order->markList->customer_id)->first();
|
|
}
|
|
|
|
return view('admin.popup', compact('order', 'user'));
|
|
}
|
|
|
|
|
|
|
|
|
|
public function resetTemp()
|
|
{
|
|
session()->forget(['temp_order_items', 'mark_no', 'origin', 'destination']);
|
|
|
|
return redirect()->to(route('admin.orders.index') . '#createOrderForm')
|
|
->with('success', 'Order reset successfully.');
|
|
}
|
|
|
|
public function orderShow()
|
|
{
|
|
$orders = Order::with([
|
|
'markList', // company, customer, origin, destination, date
|
|
'shipments', // shipment_id, shipment_date, status
|
|
'invoice' // invoice number, dates, amounts, status
|
|
])
|
|
->latest('id') // show latest orders first
|
|
->get();
|
|
|
|
return view('admin.orders', compact('orders'));
|
|
}
|
|
|
|
// inside AdminOrderController
|
|
|
|
private function buildOrdersQueryFromRequest(Request $request)
|
|
{
|
|
$query = Order::with(['markList', 'invoice', 'shipments']);
|
|
|
|
// Search across order_id, markList.company_name, markList.customer_id, invoice.invoice_number
|
|
if ($request->filled('search')) {
|
|
$search = $request->search;
|
|
$query->where(function($q) use ($search) {
|
|
$q->where('order_id', 'like', "%{$search}%")
|
|
->orWhereHas('markList', function($q2) use ($search) {
|
|
$q2->where('company_name', 'like', "%{$search}%")
|
|
->orWhere('customer_id', 'like', "%{$search}%");
|
|
})
|
|
->orWhereHas('invoice', function($q3) use ($search) {
|
|
$q3->where('invoice_number', 'like', "%{$search}%");
|
|
});
|
|
});
|
|
}
|
|
|
|
// Invoice status filter
|
|
if ($request->filled('status')) {
|
|
$query->whereHas('invoice', function($q) use ($request) {
|
|
$q->where('status', $request->status);
|
|
});
|
|
}
|
|
|
|
// Shipment status filter
|
|
if ($request->filled('shipment')) {
|
|
$query->whereHas('shipments', function($q) use ($request) {
|
|
$q->where('status', $request->shipment);
|
|
});
|
|
}
|
|
|
|
// optional ordering
|
|
$query->latest('id');
|
|
|
|
return $query;
|
|
}
|
|
|
|
public function downloadPdf(Request $request)
|
|
{
|
|
// Build same filtered query used for table
|
|
$query = $this->buildOrdersQueryFromRequest($request);
|
|
|
|
$orders = $query->get();
|
|
|
|
// optional: pass filters to view for header
|
|
$filters = [
|
|
'search' => $request->search ?? null,
|
|
'status' => $request->status ?? null,
|
|
'shipment' => $request->shipment ?? null,
|
|
];
|
|
|
|
$pdf = PDF::loadView('admin.orders.pdf', compact('orders', 'filters'))
|
|
->setPaper('a4', 'landscape'); // adjust if needed
|
|
|
|
$fileName = 'orders-report'
|
|
. ($filters['status'] ? "-{$filters['status']}" : '')
|
|
. '-' . date('Y-m-d') . '.pdf';
|
|
|
|
return $pdf->download($fileName);
|
|
}
|
|
|
|
public function downloadExcel(Request $request)
|
|
{
|
|
// pass request to OrdersExport which will build Filtered query internally
|
|
return Excel::download(new OrdersExport($request), 'orders-report-' . date('Y-m-d') . '.xlsx');
|
|
}
|
|
|
|
|
|
public function addTempItem(Request $request)
|
|
{
|
|
// Validate item fields
|
|
$item = $request->validate([
|
|
'mark_no' => 'required',
|
|
'origin' => 'required',
|
|
'destination' => 'required',
|
|
'description' => 'required|string',
|
|
'ctn' => 'nullable|numeric',
|
|
'qty' => 'nullable|numeric',
|
|
'ttl_qty' => 'nullable|numeric',
|
|
'unit' => 'nullable|string',
|
|
'price' => 'nullable|numeric',
|
|
'ttl_amount' => 'nullable|numeric',
|
|
'cbm' => 'nullable|numeric',
|
|
'ttl_cbm' => 'nullable|numeric',
|
|
'kg' => 'nullable|numeric',
|
|
'ttl_kg' => 'nullable|numeric',
|
|
'shop_no' => 'nullable|string',
|
|
]);
|
|
|
|
// ❌ Prevent changing mark_no once first item added
|
|
if (session()->has('mark_no') && session('mark_no') != $request->mark_no) {
|
|
return redirect()->to(route('admin.orders.index') . '#createOrderForm')
|
|
->with('error', 'You must finish or clear the current order before changing Mark No.');
|
|
}
|
|
|
|
// Save mark, origin, destination ONLY ONCE
|
|
if (!session()->has('mark_no')) {
|
|
session([
|
|
'mark_no' => $request->mark_no,
|
|
'origin' => $request->origin,
|
|
'destination' => $request->destination
|
|
]);
|
|
}
|
|
|
|
// ❌ DO NOT overwrite these values again
|
|
// session(['mark_no' => $request->mark_no]);
|
|
// session(['origin' => $request->origin]);
|
|
// session(['destination' => $request->destination]);
|
|
|
|
// Add new sub-item into session
|
|
session()->push('temp_order_items', $item);
|
|
|
|
return redirect()->to(route('admin.orders.index') . '#createOrderForm')
|
|
|
|
->with('success', 'Item added.');
|
|
}
|
|
|
|
// -------------------------------------------------------------------------
|
|
// STEP 3 : FINISH ORDER
|
|
// -------------------------------------------------------------------------
|
|
|
|
public function finishOrder(Request $request)
|
|
{
|
|
$request->validate([
|
|
'mark_no' => 'required',
|
|
'origin' => 'required',
|
|
'destination' => 'required',
|
|
]);
|
|
|
|
$items = session('temp_order_items', []);
|
|
|
|
if (empty($items)) {
|
|
return redirect()->to(route('admin.orders.index') . '#createOrderForm')
|
|
->with('error', 'Add at least one item before finishing.');
|
|
}
|
|
|
|
// =======================
|
|
// GENERATE ORDER ID
|
|
// =======================
|
|
$year = date('y');
|
|
$prefix = "KNT-$year-";
|
|
|
|
$lastOrder = Order::latest('id')->first();
|
|
$nextNumber = $lastOrder ? intval(substr($lastOrder->order_id, -8)) + 1 : 1;
|
|
|
|
$orderId = $prefix . str_pad($nextNumber, 8, '0', STR_PAD_LEFT);
|
|
|
|
// =======================
|
|
// TOTAL SUMS
|
|
// =======================
|
|
$total_ctn = array_sum(array_column($items, 'ctn'));
|
|
$total_qty = array_sum(array_column($items, 'qty'));
|
|
$total_ttl_qty = array_sum(array_column($items, 'ttl_qty'));
|
|
$total_amount = array_sum(array_column($items, 'ttl_amount'));
|
|
$total_cbm = array_sum(array_column($items, 'cbm'));
|
|
$total_ttl_cbm = array_sum(array_column($items, 'ttl_cbm'));
|
|
$total_kg = array_sum(array_column($items, 'kg'));
|
|
$total_ttl_kg = array_sum(array_column($items, 'ttl_kg'));
|
|
|
|
// =======================
|
|
// CREATE ORDER
|
|
// =======================
|
|
$order = Order::create([
|
|
'order_id' => $orderId,
|
|
'mark_no' => $request->mark_no,
|
|
'origin' => $request->origin,
|
|
'destination' => $request->destination,
|
|
'ctn' => $total_ctn,
|
|
'qty' => $total_qty,
|
|
'ttl_qty' => $total_ttl_qty,
|
|
'ttl_amount' => $total_amount,
|
|
'cbm' => $total_cbm,
|
|
'ttl_cbm' => $total_ttl_cbm,
|
|
'kg' => $total_kg,
|
|
'ttl_kg' => $total_ttl_kg,
|
|
'status' => 'pending',
|
|
]);
|
|
|
|
// SAVE ORDER ITEMS
|
|
foreach ($items as $item) {
|
|
OrderItem::create([
|
|
'order_id' => $order->id,
|
|
'description' => $item['description'],
|
|
'ctn' => $item['ctn'],
|
|
'qty' => $item['qty'],
|
|
'ttl_qty' => $item['ttl_qty'],
|
|
'unit' => $item['unit'],
|
|
'price' => $item['price'],
|
|
'ttl_amount' => $item['ttl_amount'],
|
|
'cbm' => $item['cbm'],
|
|
'ttl_cbm' => $item['ttl_cbm'],
|
|
'kg' => $item['kg'],
|
|
'ttl_kg' => $item['ttl_kg'],
|
|
'shop_no' => $item['shop_no'],
|
|
]);
|
|
}
|
|
|
|
// =======================
|
|
// INVOICE CREATION START
|
|
// =======================
|
|
|
|
// 1. Auto-generate invoice number
|
|
$lastInvoice = \App\Models\Invoice::latest()->first();
|
|
$nextInvoice = $lastInvoice ? $lastInvoice->id + 1 : 1;
|
|
$invoiceNumber = 'INV-' . date('Y') . '-' . str_pad($nextInvoice, 6, '0', STR_PAD_LEFT);
|
|
|
|
// 2. Fetch customer (using mark list → customer_id)
|
|
$markList = MarkList::where('mark_no', $order->mark_no)->first();
|
|
$customer = null;
|
|
|
|
if ($markList && $markList->customer_id) {
|
|
$customer = \App\Models\User::where('customer_id', $markList->customer_id)->first();
|
|
}
|
|
|
|
// 3. Create Invoice Record
|
|
$invoice = \App\Models\Invoice::create([
|
|
'order_id' => $order->id,
|
|
'customer_id' => $customer->id ?? null,
|
|
'mark_no' => $order->mark_no,
|
|
|
|
'invoice_number' => $invoiceNumber,
|
|
'invoice_date' => now(),
|
|
'due_date' => now()->addDays(10),
|
|
|
|
'payment_method' => null,
|
|
'reference_no' => null,
|
|
'status' => 'pending',
|
|
|
|
'final_amount' => $total_amount,
|
|
'gst_percent' => 0,
|
|
'gst_amount' => 0,
|
|
'final_amount_with_gst' => $total_amount,
|
|
|
|
// snapshot customer fields
|
|
'customer_name' => $customer->customer_name ?? null,
|
|
'company_name' => $customer->company_name ?? null,
|
|
'customer_email' => $customer->email ?? null,
|
|
'customer_mobile' => $customer->mobile_no ?? null,
|
|
'customer_address' => $customer->address ?? null,
|
|
'pincode' => $customer->pincode ?? null,
|
|
|
|
'notes' => null,
|
|
]);
|
|
|
|
// 4. Clone order items into invoice_items
|
|
foreach ($order->items as $item) {
|
|
\App\Models\InvoiceItem::create([
|
|
'invoice_id' => $invoice->id,
|
|
'description' => $item->description,
|
|
'ctn' => $item->ctn,
|
|
'qty' => $item->qty,
|
|
'ttl_qty' => $item->ttl_qty,
|
|
'unit' => $item->unit,
|
|
'price' => $item->price,
|
|
'ttl_amount' => $item->ttl_amount,
|
|
'cbm' => $item->cbm,
|
|
'ttl_cbm' => $item->ttl_cbm,
|
|
'kg' => $item->kg,
|
|
'ttl_kg' => $item->ttl_kg,
|
|
'shop_no' => $item->shop_no,
|
|
]);
|
|
}
|
|
|
|
// 5. TODO: PDF generation (I will add this later)
|
|
$invoice->pdf_path = null; // placeholder for now
|
|
$invoice->save();
|
|
|
|
// =======================
|
|
// END INVOICE CREATION
|
|
// =======================
|
|
|
|
// CLEAR TEMP DATA
|
|
session()->forget(['temp_order_items', 'mark_no', 'origin', 'destination']);
|
|
|
|
return redirect()->route('admin.orders.index')
|
|
->with('success', 'Order + Invoice created successfully.');
|
|
}
|
|
|
|
// ---------------------------
|
|
// ORDER CRUD: update / destroy
|
|
// ---------------------------
|
|
public function updateItem(Request $request, $id)
|
|
{
|
|
$item = OrderItem::findOrFail($id);
|
|
$order = $item->order;
|
|
|
|
$item->update([
|
|
'description' => $request->description,
|
|
'ctn' => $request->ctn,
|
|
'qty' => $request->qty,
|
|
'ttl_qty' => $request->ttl_qty,
|
|
'unit' => $request->unit,
|
|
'price' => $request->price,
|
|
'ttl_amount' => $request->ttl_amount,
|
|
'cbm' => $request->cbm,
|
|
'ttl_cbm' => $request->ttl_cbm,
|
|
'kg' => $request->kg,
|
|
'ttl_kg' => $request->ttl_kg,
|
|
'shop_no' => $request->shop_no,
|
|
]);
|
|
|
|
$this->recalcTotals($order);
|
|
$this->updateInvoiceFromOrder($order); // <-- NEW
|
|
|
|
return back()->with('success', 'Item updated successfully');
|
|
}
|
|
|
|
|
|
private function updateInvoiceFromOrder(Order $order)
|
|
{
|
|
$invoice = Invoice::where('order_id', $order->id)->first();
|
|
|
|
if (!$invoice) {
|
|
return; // No invoice exists (should not happen normally)
|
|
}
|
|
|
|
// Update invoice totals
|
|
$invoice->final_amount = $order->ttl_amount;
|
|
$invoice->gst_percent = 0;
|
|
$invoice->gst_amount = 0;
|
|
$invoice->final_amount_with_gst = $order->ttl_amount;
|
|
$invoice->save();
|
|
|
|
// Delete old invoice items
|
|
InvoiceItem::where('invoice_id', $invoice->id)->delete();
|
|
|
|
// Re-create invoice items from updated order items
|
|
foreach ($order->items as $item) {
|
|
InvoiceItem::create([
|
|
'invoice_id' => $invoice->id,
|
|
'description' => $item->description,
|
|
'ctn' => $item->ctn,
|
|
'qty' => $item->qty,
|
|
'ttl_qty' => $item->ttl_qty,
|
|
'unit' => $item->unit,
|
|
'price' => $item->price,
|
|
'ttl_amount' => $item->ttl_amount,
|
|
'cbm' => $item->cbm,
|
|
'ttl_cbm' => $item->ttl_cbm,
|
|
'kg' => $item->kg,
|
|
'ttl_kg' => $item->ttl_kg,
|
|
'shop_no' => $item->shop_no,
|
|
]);
|
|
}
|
|
}
|
|
|
|
} |