amount update

This commit is contained in:
Abhishek Mali
2025-12-22 19:22:01 +05:30
parent cdb6cab57d
commit 1bce2be826
9 changed files with 152 additions and 27 deletions

View File

@@ -19,32 +19,37 @@ class AdminCustomerController extends Controller
$search = $request->search;
$status = $request->status;
$query = User::with(['marks', 'orders'])->orderBy('id', 'desc');
$query = User::with([
'marks',
'orders',
'invoices.installments' // 🔥 IMPORTANT
])->orderBy('id', 'desc');
// SEARCH FILTER
if (!empty($search)) {
$query->where(function ($q) use ($search) {
$q->where('customer_name', 'like', "%$search%")
->orWhere('email', 'like', "%$search%")
->orWhere('mobile_no', 'like', "%$search%")
->orWhere('customer_id', 'like', "%$search%");
->orWhere('email', 'like', "%$search%")
->orWhere('mobile_no', 'like', "%$search%")
->orWhere('customer_id', 'like', "%$search%");
});
}
// STATUS FILTER
if (!empty($status) && in_array($status, ['active', 'inactive'])) {
$query->where('status', $status);
}
// Get all customers for statistics (without pagination)
$allCustomers = $query->get();
// Get paginated customers for the table (10 per page)
$customers = $query->paginate(10);
return view('admin.customers', compact('customers', 'allCustomers', 'search', 'status'));
return view('admin.customers', compact(
'customers',
'allCustomers',
'search',
'status'
));
}
// ---------------------------------------------------------
// SHOW ADD CUSTOMER FORM
// ---------------------------------------------------------
@@ -106,20 +111,36 @@ class AdminCustomerController extends Controller
// VIEW CUSTOMER FULL DETAILS
// ---------------------------------------------------------
public function view($id)
{
$customer = User::with(['marks', 'orders'])->findOrFail($id);
{
$customer = User::with([
'marks',
'orders',
'invoices.installments'
])->findOrFail($id);
$totalOrders = $customer->orders->count();
$totalAmount = $customer->orders->sum('ttl_amount');
$recentOrders = $customer->orders()->latest()->take(5)->get();
// Orders
$totalOrders = $customer->orders->count();
$totalOrderAmount = $customer->orders->sum('ttl_amount');
// Invoices (PAYABLE)
$totalPayable = $customer->invoices->sum('final_amount_with_gst');
// Paid via installments
$totalPaid = $customer->invoiceInstallments->sum('amount');
// Remaining
$totalRemaining = max($totalPayable - $totalPaid, 0);
return view('admin.customers_view', compact(
'customer',
'totalOrders',
'totalOrderAmount',
'totalPayable',
'totalPaid',
'totalRemaining'
));
}
return view('admin.customers_view', compact(
'customer',
'totalOrders',
'totalAmount',
'recentOrders'
));
}
// ---------------------------------------------------------
// TOGGLE STATUS ACTIVE / INACTIVE

View File

@@ -92,5 +92,20 @@ class Invoice extends Model
return $this->hasMany(InvoiceInstallment::class);
}
// App\Models\Invoice.php
public function totalPaid()
{
return $this->installments()->sum('amount');
}
public function remainingAmount()
{
return max(
($this->final_amount_with_gst ?? 0) - $this->totalPaid(),
0
);
}
}

View File

@@ -94,6 +94,19 @@ public function invoices()
return $this->hasMany(\App\Models\Invoice::class, 'customer_id', 'id');
}
// App\Models\User.php
public function invoiceInstallments()
{
return $this->hasManyThrough(
InvoiceInstallment::class,
Invoice::class,
'customer_id', // FK on invoices
'invoice_id' // FK on installments
);
}
}