Pdf Changes Done
This commit is contained in:
@@ -8,6 +8,7 @@ use App\Models\InvoiceItem;
|
||||
use App\Models\InvoiceInstallment;
|
||||
use App\Models\InvoiceChargeGroup;
|
||||
use App\Models\InvoiceChargeGroupItem;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
@@ -20,9 +21,9 @@ class AdminInvoiceController extends Controller
|
||||
// -------------------------------------------------------------
|
||||
public function index(Request $request)
|
||||
{
|
||||
// Container relation सह invoices load करतो
|
||||
$query = Invoice::with(['items', 'customer', 'container']);
|
||||
|
||||
// Search
|
||||
|
||||
if ($request->filled('search')) {
|
||||
$search = $request->search;
|
||||
$query->where(function ($q) use ($search) {
|
||||
@@ -30,42 +31,51 @@ class AdminInvoiceController extends Controller
|
||||
->orWhere('customer_name', 'like', "%{$search}%");
|
||||
});
|
||||
}
|
||||
|
||||
// Status filter
|
||||
|
||||
if ($request->filled('status') && $request->status !== 'all') {
|
||||
$query->where('status', $request->status);
|
||||
}
|
||||
|
||||
// Date range filter (invoice_date वर)
|
||||
|
||||
if ($request->filled('start_date')) {
|
||||
$query->whereDate('invoice_date', '>=', $request->start_date);
|
||||
}
|
||||
|
||||
|
||||
if ($request->filled('end_date')) {
|
||||
$query->whereDate('invoice_date', '<=', $request->end_date);
|
||||
}
|
||||
|
||||
// Latest first
|
||||
|
||||
$invoices = $query->latest()->get();
|
||||
|
||||
|
||||
return view('admin.invoice', compact('invoices'));
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// POPUP VIEW (AJAX)
|
||||
// POPUP VIEW + CUSTOMER DATA SYNC
|
||||
// -------------------------------------------------------------
|
||||
public function popup($id)
|
||||
{
|
||||
$invoice = Invoice::with([
|
||||
'items',
|
||||
'customer',
|
||||
'container',
|
||||
'chargeGroups.items.item',
|
||||
'chargeGroups.items',
|
||||
])->findOrFail($id);
|
||||
|
||||
// demo update असेल तर ठेव/काढ
|
||||
$invoice->update([
|
||||
'customer_email' => 'test@demo.com',
|
||||
'customer_address' => 'TEST ADDRESS',
|
||||
'pincode' => '999999',
|
||||
]);
|
||||
|
||||
$shipment = null;
|
||||
|
||||
return view('admin.popup_invoice', compact('invoice', 'shipment'));
|
||||
// आधीच group मध्ये असलेले item ids
|
||||
$groupedItemIds = $invoice->chargeGroups
|
||||
->flatMap(fn($group) => $group->items->pluck('invoice_item_id'))
|
||||
->unique()
|
||||
->values()
|
||||
->toArray();
|
||||
|
||||
return view('admin.popup_invoice', compact('invoice', 'shipment', 'groupedItemIds'));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
@@ -134,10 +144,10 @@ class AdminInvoiceController extends Controller
|
||||
$data['igst_percent'] = $taxPercent;
|
||||
}
|
||||
|
||||
$gstAmount = ($finalAmount * $taxPercent) / 100;
|
||||
$data['gst_amount'] = $gstAmount;
|
||||
$gstAmount = ($finalAmount * $taxPercent) / 100;
|
||||
$data['gst_amount'] = $gstAmount;
|
||||
$data['final_amount_with_gst'] = $finalAmount + $gstAmount;
|
||||
$data['gst_percent'] = $taxPercent;
|
||||
$data['gst_percent'] = $taxPercent;
|
||||
|
||||
Log::info('📌 Final Calculated Invoice Values', [
|
||||
'invoice_id' => $invoice->id,
|
||||
@@ -177,7 +187,7 @@ class AdminInvoiceController extends Controller
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// 🔹 UPDATE INVOICE ITEMS (price + ttl_amount)
|
||||
// UPDATE INVOICE ITEMS
|
||||
// -------------------------------------------------------------
|
||||
public function updateItems(Request $request, Invoice $invoice)
|
||||
{
|
||||
@@ -230,10 +240,16 @@ class AdminInvoiceController extends Controller
|
||||
$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->final_amount = $newBaseAmount;
|
||||
$invoice->gst_amount = $gstAmount;
|
||||
$invoice->final_amount_with_gst = $finalWithGst;
|
||||
$invoice->gst_percent = $gstPercent;
|
||||
$invoice->save();
|
||||
|
||||
// ⭐ Total Charges (groups समावेत) पुन्हा कॅलक्युलेट
|
||||
$chargeGroupsTotal = $invoice->chargeGroups()->sum('total_charge');
|
||||
$invoice->charge_groups_total = $chargeGroupsTotal;
|
||||
$invoice->grand_total_with_charges = $invoice->final_amount_with_gst + $chargeGroupsTotal;
|
||||
$invoice->save();
|
||||
|
||||
Log::info('✅ Invoice items updated & totals recalculated', [
|
||||
@@ -245,13 +261,15 @@ class AdminInvoiceController extends Controller
|
||||
'cgst_percent' => $invoice->cgst_percent,
|
||||
'sgst_percent' => $invoice->sgst_percent,
|
||||
'igst_percent' => $invoice->igst_percent,
|
||||
'charge_groups_total' => $invoice->charge_groups_total,
|
||||
'grand_total_with_charges' => $invoice->grand_total_with_charges,
|
||||
]);
|
||||
|
||||
return back()->with('success', 'Invoice items updated successfully.');
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// PDF GENERATION USING mPDF
|
||||
// PDF GENERATION
|
||||
// -------------------------------------------------------------
|
||||
public function generateInvoicePDF($invoice)
|
||||
{
|
||||
@@ -302,7 +320,10 @@ class AdminInvoiceController extends Controller
|
||||
|
||||
$invoice = Invoice::findOrFail($invoice_id);
|
||||
$paidTotal = $invoice->installments()->sum('amount');
|
||||
$remaining = $invoice->final_amount_with_gst - $paidTotal;
|
||||
|
||||
// 👇 Total Charges (grand_total_with_charges) वरून remaining
|
||||
$grandTotal = $invoice->grand_total_with_charges;
|
||||
$remaining = $grandTotal - $paidTotal;
|
||||
|
||||
if ($request->amount > $remaining) {
|
||||
return response()->json([
|
||||
@@ -331,11 +352,12 @@ class AdminInvoiceController extends Controller
|
||||
'installment' => $installment,
|
||||
'totalPaid' => $newPaid,
|
||||
'gstAmount' => $invoice->gst_amount,
|
||||
'finalAmountWithGst' => $invoice->final_amount_with_gst,
|
||||
'finalAmountWithGst' => $grandTotal, // इथे grand total पाठव
|
||||
'baseAmount' => $invoice->final_amount,
|
||||
'remaining' => max(0, $invoice->final_amount_with_gst - $newPaid),
|
||||
'isCompleted' => $newPaid >= $invoice->final_amount_with_gst,
|
||||
'remaining' => max(0, $grandTotal - $newPaid),
|
||||
'isCompleted' => $newPaid >= $grandTotal,
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
@@ -348,34 +370,32 @@ class AdminInvoiceController extends Controller
|
||||
|
||||
$installment->delete();
|
||||
|
||||
$paidTotal = $invoice->installments()->sum('amount');
|
||||
$remaining = $invoice->final_amount_with_gst - $paidTotal;
|
||||
|
||||
if ($remaining > 0 && $invoice->status === 'paid') {
|
||||
$invoice->update(['status' => 'pending']);
|
||||
}
|
||||
|
||||
$paidTotal = $invoice->installments()->sum('amount');
|
||||
$grandTotal = $invoice->grand_total_with_charges;
|
||||
$remaining = $grandTotal - $paidTotal;
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Installment deleted.',
|
||||
'totalPaid' => $paidTotal,
|
||||
'gstAmount' => $invoice->gst_amount,
|
||||
'finalAmountWithGst' => $invoice->final_amount_with_gst,
|
||||
'finalAmountWithGst' => $grandTotal, // इथेही
|
||||
'baseAmount' => $invoice->final_amount,
|
||||
'remaining' => $remaining,
|
||||
'isZero' => $paidTotal == 0,
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// CHARGE GROUP SAVE (NEW)
|
||||
// CHARGE GROUP SAVE (no AJAX branch)
|
||||
// -------------------------------------------------------------
|
||||
public function storeChargeGroup(Request $request, $invoiceId)
|
||||
{
|
||||
$invoice = Invoice::with('items')->findOrFail($invoiceId);
|
||||
|
||||
|
||||
$data = $request->validate([
|
||||
'group_name' => 'nullable|string|max:255',
|
||||
'group_name' => 'required|string|max:255',
|
||||
'basis_type' => 'required|in:ttl_qty,amount,ttl_cbm,ttl_kg',
|
||||
'basis_value' => 'required|numeric',
|
||||
'rate' => 'required|numeric|min:0.0001',
|
||||
@@ -383,62 +403,47 @@ class AdminInvoiceController extends Controller
|
||||
'item_ids' => 'required|array',
|
||||
'item_ids.*' => 'integer|exists:invoice_items,id',
|
||||
]);
|
||||
|
||||
|
||||
$exists = InvoiceChargeGroup::where('invoice_id', $invoice->id)
|
||||
->where('group_name', $data['group_name'])
|
||||
->exists();
|
||||
|
||||
if ($exists) {
|
||||
return back()
|
||||
->withErrors(['group_name' => 'This group name is already used for this invoice.'])
|
||||
->withInput();
|
||||
}
|
||||
|
||||
$group = InvoiceChargeGroup::create([
|
||||
'invoice_id' => $invoice->id,
|
||||
'group_name' => $data['group_name'] ?? null,
|
||||
'group_name' => $data['group_name'],
|
||||
'basis_type' => $data['basis_type'],
|
||||
'basis_value' => $data['basis_value'],
|
||||
'rate' => $data['rate'],
|
||||
'total_charge' => $data['auto_total'],
|
||||
]);
|
||||
|
||||
|
||||
foreach ($data['item_ids'] as $itemId) {
|
||||
InvoiceChargeGroupItem::create([
|
||||
'group_id' => $group->id,
|
||||
'invoice_item_id' => $itemId,
|
||||
]);
|
||||
}
|
||||
|
||||
if ($request->ajax()) {
|
||||
// load items with invoice item relation
|
||||
$group->load(['items.item']);
|
||||
|
||||
// prepare simple array for JS
|
||||
$itemsPayload = $group->items->map(function ($gi) {
|
||||
$it = $gi->item;
|
||||
return [
|
||||
'description' => $it->description,
|
||||
'qty' => $it->qty,
|
||||
'ttlqty' => $it->ttl_qty,
|
||||
'cbm' => $it->cbm,
|
||||
'ttlcbm' => $it->ttl_cbm,
|
||||
'kg' => $it->kg,
|
||||
'ttlkg' => $it->ttl_kg,
|
||||
'amount' => $it->ttl_amount,
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Charge group created successfully.',
|
||||
'group' => [
|
||||
'id' => $group->id,
|
||||
'group_name' => $group->group_name,
|
||||
'basis_type' => $group->basis_type,
|
||||
'basis_value' => $group->basis_value,
|
||||
'rate' => $group->rate,
|
||||
'total_charge'=> $group->total_charge,
|
||||
'items' => $itemsPayload,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
// ⭐ Charge groups नुसार Total Charges सेट करा
|
||||
$chargeGroupsTotal = $invoice->chargeGroups()->sum('total_charge');
|
||||
$grandTotal = $invoice->final_amount_with_gst + $chargeGroupsTotal;
|
||||
|
||||
$invoice->update([
|
||||
'charge_groups_total' => $chargeGroupsTotal,
|
||||
'grand_total_with_charges' => $grandTotal,
|
||||
]);
|
||||
|
||||
return redirect()
|
||||
->back()
|
||||
->with('success', 'Charge group saved successfully.');
|
||||
}
|
||||
|
||||
|
||||
public function download(Invoice $invoice)
|
||||
{
|
||||
if (!$invoice->pdf_path || !Storage::exists($invoice->pdf_path)) {
|
||||
|
||||
Reference in New Issue
Block a user