354 lines
13 KiB
PHP
354 lines
13 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Invoice;
|
|
use App\Models\InvoiceItem;
|
|
use App\Models\InvoiceInstallment;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Mpdf\Mpdf;
|
|
|
|
class AdminInvoiceController extends Controller
|
|
{
|
|
// -------------------------------------------------------------
|
|
// INVOICE LIST PAGE
|
|
// -------------------------------------------------------------
|
|
public function index()
|
|
{
|
|
$invoices = Invoice::with(['items', 'customer', 'container'])
|
|
->latest()
|
|
->get();
|
|
|
|
return view('admin.invoice', compact('invoices'));
|
|
}
|
|
|
|
// -------------------------------------------------------------
|
|
// POPUP VIEW (AJAX)
|
|
// -------------------------------------------------------------
|
|
public function popup($id)
|
|
{
|
|
$invoice = Invoice::with(['items', 'customer', 'container'])->findOrFail($id);
|
|
$shipment = null;
|
|
|
|
return view('admin.popup_invoice', compact('invoice', 'shipment'));
|
|
}
|
|
|
|
// -------------------------------------------------------------
|
|
// EDIT INVOICE PAGE
|
|
// -------------------------------------------------------------
|
|
public function edit($id)
|
|
{
|
|
$invoice = Invoice::with(['items', 'customer', 'container'])->findOrFail($id);
|
|
$shipment = null;
|
|
|
|
// ADD THIS SECTION: Calculate customer's total due across all invoices
|
|
$customerTotalDue = Invoice::where('customer_id', $invoice->customer_id)
|
|
->where('status', '!=', 'cancelled')
|
|
->where('status', '!=', 'void')
|
|
->sum('final_amount_with_gst');
|
|
|
|
// Pass the new variable to the view
|
|
return view('admin.invoice_edit', compact('invoice', 'shipment', 'customerTotalDue'));
|
|
}
|
|
|
|
// -------------------------------------------------------------
|
|
// UPDATE INVOICE (HEADER LEVEL)
|
|
// -------------------------------------------------------------
|
|
// -------------------------------------------------------------
|
|
// UPDATE INVOICE (HEADER LEVEL)
|
|
// -------------------------------------------------------------
|
|
public function update(Request $request, $id)
|
|
{
|
|
Log::info('🟡 Invoice Update Request Received', [
|
|
'invoice_id' => $id,
|
|
'request' => $request->all(),
|
|
]);
|
|
|
|
$invoice = Invoice::findOrFail($id);
|
|
|
|
// 1) VALIDATION
|
|
$data = $request->validate([
|
|
'invoice_date' => 'required|date',
|
|
'due_date' => 'required|date|after_or_equal:invoice_date',
|
|
'final_amount' => 'required|numeric|min:0',
|
|
'tax_type' => 'required|in:gst,igst',
|
|
'tax_percent' => 'required|numeric|min:0|max:28',
|
|
'status' => 'required|in:pending,paid,overdue',
|
|
'notes' => 'nullable|string',
|
|
]);
|
|
|
|
Log::info('✅ Validated Invoice Update Data', $data);
|
|
|
|
// 2) CALCULATE GST / TOTALS
|
|
$finalAmount = (float) $data['final_amount'];
|
|
$taxPercent = (float) $data['tax_percent'];
|
|
|
|
if ($data['tax_type'] === 'gst') {
|
|
Log::info('🟢 GST Selected', compact('taxPercent'));
|
|
|
|
$data['cgst_percent'] = $taxPercent / 2;
|
|
$data['sgst_percent'] = $taxPercent / 2;
|
|
$data['igst_percent'] = 0;
|
|
} else {
|
|
Log::info('🔵 IGST Selected', compact('taxPercent'));
|
|
|
|
$data['cgst_percent'] = 0;
|
|
$data['sgst_percent'] = 0;
|
|
$data['igst_percent'] = $taxPercent;
|
|
}
|
|
|
|
$gstAmount = ($finalAmount * $taxPercent) / 100;
|
|
$data['gst_amount'] = $gstAmount;
|
|
$data['final_amount_with_gst'] = $finalAmount + $gstAmount;
|
|
$data['gst_percent'] = $taxPercent;
|
|
|
|
Log::info('📌 Final Calculated Invoice Values', [
|
|
'invoice_id' => $invoice->id,
|
|
'final_amount' => $finalAmount,
|
|
'gst_amount' => $data['gst_amount'],
|
|
'final_amount_with_gst' => $data['final_amount_with_gst'],
|
|
'tax_type' => $data['tax_type'],
|
|
'cgst_percent' => $data['cgst_percent'],
|
|
'sgst_percent' => $data['sgst_percent'],
|
|
'igst_percent' => $data['igst_percent'],
|
|
]);
|
|
|
|
// 3) UPDATE DB
|
|
$invoice->update($data);
|
|
|
|
Log::info('✅ Invoice Updated Successfully', [
|
|
'invoice_id' => $invoice->id,
|
|
]);
|
|
|
|
// 4) LOG ACTUAL DB VALUES
|
|
$invoice->refresh();
|
|
Log::info('🔍 Invoice AFTER UPDATE (DB values)', [
|
|
'invoice_id' => $invoice->id,
|
|
'final_amount' => $invoice->final_amount,
|
|
'gst_percent' => $invoice->gst_percent,
|
|
'gst_amount' => $invoice->gst_amount,
|
|
'final_amount_with_gst' => $invoice->final_amount_with_gst,
|
|
'tax_type' => $invoice->tax_type,
|
|
'cgst_percent' => $invoice->cgst_percent,
|
|
'sgst_percent' => $invoice->sgst_percent,
|
|
'igst_percent' => $invoice->igst_percent,
|
|
]);
|
|
|
|
// 5) REGENERATE PDF
|
|
$this->generateInvoicePDF($invoice);
|
|
|
|
return redirect()
|
|
->route('admin.invoices.index')
|
|
->with('success', 'Invoice updated & PDF generated successfully.');
|
|
}
|
|
|
|
|
|
// -------------------------------------------------------------
|
|
// 🔹 UPDATE INVOICE ITEMS (price + ttl_amount)
|
|
// -------------------------------------------------------------
|
|
public function updateItems(Request $request, Invoice $invoice)
|
|
{
|
|
Log::info('🟡 Invoice Items Update Request', [
|
|
'invoice_id' => $invoice->id,
|
|
'payload' => $request->all(),
|
|
]);
|
|
|
|
$data = $request->validate([
|
|
'items' => ['required', 'array'],
|
|
'items.*.price' => ['required', 'numeric', 'min:0'],
|
|
'items.*.ttl_amount' => ['required', 'numeric', 'min:0'],
|
|
]);
|
|
|
|
$itemsInput = $data['items'];
|
|
|
|
foreach ($itemsInput as $itemId => $itemData) {
|
|
$item = InvoiceItem::where('id', $itemId)
|
|
->where('invoice_id', $invoice->id)
|
|
->first();
|
|
|
|
if (!$item) {
|
|
Log::warning('Invoice item not found or mismatched invoice', [
|
|
'invoice_id' => $invoice->id,
|
|
'item_id' => $itemId,
|
|
]);
|
|
continue;
|
|
}
|
|
|
|
$item->price = $itemData['price'];
|
|
$item->ttl_amount = $itemData['ttl_amount'];
|
|
$item->save();
|
|
}
|
|
|
|
$newBaseAmount = InvoiceItem::where('invoice_id', $invoice->id)
|
|
->sum('ttl_amount');
|
|
|
|
$taxType = $invoice->tax_type;
|
|
$cgstPercent = (float) ($invoice->cgst_percent ?? 0);
|
|
$sgstPercent = (float) ($invoice->sgst_percent ?? 0);
|
|
$igstPercent = (float) ($invoice->igst_percent ?? 0);
|
|
|
|
$gstPercent = 0;
|
|
if ($taxType === 'gst') {
|
|
$gstPercent = $cgstPercent + $sgstPercent;
|
|
} elseif ($taxType === 'igst') {
|
|
$gstPercent = $igstPercent;
|
|
}
|
|
|
|
$gstAmount = $newBaseAmount * $gstPercent / 100;
|
|
$finalWithGst = $newBaseAmount + $gstAmount;
|
|
|
|
$invoice->final_amount = $newBaseAmount;
|
|
$invoice->gst_amount = $gstAmount;
|
|
$invoice->final_amount_with_gst = $finalWithGst;
|
|
$invoice->gst_percent = $gstPercent;
|
|
$invoice->save();
|
|
|
|
Log::info('✅ Invoice items updated & totals recalculated', [
|
|
'invoice_id' => $invoice->id,
|
|
'final_amount' => $invoice->final_amount,
|
|
'gst_amount' => $invoice->gst_amount,
|
|
'final_amount_with_gst' => $invoice->final_amount_with_gst,
|
|
'tax_type' => $invoice->tax_type,
|
|
'cgst_percent' => $invoice->cgst_percent,
|
|
'sgst_percent' => $invoice->sgst_percent,
|
|
'igst_percent' => $invoice->igst_percent,
|
|
]);
|
|
|
|
return back()->with('success', 'Invoice items updated successfully.');
|
|
}
|
|
|
|
// -------------------------------------------------------------
|
|
// PDF GENERATION USING mPDF
|
|
// -------------------------------------------------------------
|
|
public function generateInvoicePDF($invoice)
|
|
{
|
|
$invoice->load(['items', 'customer', 'container']);
|
|
$shipment = null;
|
|
|
|
$fileName = 'invoice-' . $invoice->invoice_number . '.pdf';
|
|
$folder = public_path('invoices/');
|
|
|
|
if (!file_exists($folder)) {
|
|
mkdir($folder, 0777, true);
|
|
}
|
|
|
|
$filePath = $folder . $fileName;
|
|
|
|
if (file_exists($filePath)) {
|
|
unlink($filePath);
|
|
}
|
|
|
|
$mpdf = new Mpdf([
|
|
'mode' => 'utf-8',
|
|
'format' => 'A4',
|
|
'default_font' => 'sans-serif',
|
|
]);
|
|
|
|
$html = view('admin.pdf.invoice', [
|
|
'invoice' => $invoice,
|
|
'shipment' => $shipment,
|
|
])->render();
|
|
|
|
$mpdf->WriteHTML($html);
|
|
$mpdf->Output($filePath, 'F');
|
|
|
|
$invoice->update(['pdf_path' => 'invoices/' . $fileName]);
|
|
}
|
|
|
|
public function downloadInvoice($id)
|
|
{
|
|
$invoice = Invoice::findOrFail($id);
|
|
|
|
// ALWAYS regenerate to reflect latest HTML/CSS
|
|
$this->generateInvoicePDF($invoice);
|
|
$invoice->refresh();
|
|
|
|
return response()->download(public_path($invoice->pdf_path));
|
|
}
|
|
|
|
// -------------------------------------------------------------
|
|
// INSTALLMENTS (ADD)
|
|
// -------------------------------------------------------------
|
|
public function storeInstallment(Request $request, $invoice_id)
|
|
{
|
|
$request->validate([
|
|
'installment_date' => 'required|date',
|
|
'payment_method' => 'required|string',
|
|
'reference_no' => 'nullable|string',
|
|
'amount' => 'required|numeric|min:1',
|
|
]);
|
|
|
|
$invoice = Invoice::findOrFail($invoice_id);
|
|
$paidTotal = $invoice->installments()->sum('amount');
|
|
$remaining = $invoice->final_amount_with_gst - $paidTotal;
|
|
|
|
if ($request->amount > $remaining) {
|
|
return response()->json([
|
|
'status' => 'error',
|
|
'message' => 'Installment amount exceeds remaining balance.',
|
|
], 422);
|
|
}
|
|
|
|
$installment = InvoiceInstallment::create([
|
|
'invoice_id' => $invoice_id,
|
|
'installment_date' => $request->installment_date,
|
|
'payment_method' => $request->payment_method,
|
|
'reference_no' => $request->reference_no,
|
|
'amount' => $request->amount,
|
|
]);
|
|
|
|
$newPaid = $paidTotal + $request->amount;
|
|
|
|
if ($newPaid >= $invoice->final_amount_with_gst) {
|
|
$invoice->update(['status' => 'paid']);
|
|
|
|
$this->generateInvoicePDF($invoice);
|
|
}
|
|
|
|
return response()->json([
|
|
'status' => 'success',
|
|
'message' => 'Installment added successfully.',
|
|
'installment' => $installment,
|
|
'totalPaid' => $newPaid,
|
|
'gstAmount' => $invoice->gst_amount,
|
|
'finalAmountWithGst' => $invoice->final_amount_with_gst,
|
|
'baseAmount' => $invoice->final_amount,
|
|
'remaining' => max(0, $invoice->final_amount_with_gst - $newPaid),
|
|
'isCompleted' => $newPaid >= $invoice->final_amount_with_gst,
|
|
]);
|
|
}
|
|
|
|
// -------------------------------------------------------------
|
|
// INSTALLMENTS (DELETE)
|
|
// -------------------------------------------------------------
|
|
public function deleteInstallment($id)
|
|
{
|
|
$installment = InvoiceInstallment::findOrFail($id);
|
|
$invoice = $installment->invoice;
|
|
|
|
$installment->delete();
|
|
|
|
$paidTotal = $invoice->installments()->sum('amount');
|
|
$remaining = $invoice->final_amount_with_gst - $paidTotal;
|
|
|
|
if ($remaining > 0 && $invoice->status === 'paid') {
|
|
$invoice->update(['status' => 'pending']);
|
|
|
|
$this->generateInvoicePDF($invoice);
|
|
}
|
|
|
|
return response()->json([
|
|
'status' => 'success',
|
|
'message' => 'Installment deleted.',
|
|
'totalPaid' => $paidTotal,
|
|
'gstAmount' => $invoice->gst_amount,
|
|
'finalAmountWithGst' => $invoice->final_amount_with_gst,
|
|
'baseAmount' => $invoice->final_amount,
|
|
'remaining' => $remaining,
|
|
'isZero' => $paidTotal == 0,
|
|
]);
|
|
}
|
|
}
|