diff --git a/app/Http/Controllers/Admin/AdminCustomerController.php b/app/Http/Controllers/Admin/AdminCustomerController.php index aab2cd1..4afccda 100644 --- a/app/Http/Controllers/Admin/AdminCustomerController.php +++ b/app/Http/Controllers/Admin/AdminCustomerController.php @@ -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 diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index b59b456..d9dc394 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -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 + ); +} + } diff --git a/app/Models/User.php b/app/Models/User.php index e67ea2f..48c963c 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -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 + ); +} + } diff --git a/config/jwt.php b/config/jwt.php index fcaf30a..3119694 100644 --- a/config/jwt.php +++ b/config/jwt.php @@ -89,7 +89,7 @@ return [ | */ - 'ttl' => (int) env('JWT_TTL', 15), + 'ttl' => (int) env('JWT_TTL', 1440), /* |-------------------------------------------------------------------------- @@ -108,7 +108,7 @@ return [ | */ - 'refresh_ttl' => (int) env('JWT_REFRESH_TTL', 60), + 'refresh_ttl' => (int) env('JWT_REFRESH_TTL', 64800), /* |-------------------------------------------------------------------------- diff --git a/public/invoices/invoice-INV-2025-000024.pdf b/public/invoices/invoice-INV-2025-000024.pdf index 68bc8aa..0494642 100644 Binary files a/public/invoices/invoice-INV-2025-000024.pdf and b/public/invoices/invoice-INV-2025-000024.pdf differ diff --git a/public/invoices/invoice-INV-2025-000033.pdf b/public/invoices/invoice-INV-2025-000033.pdf index 7cd4e84..12229f7 100644 Binary files a/public/invoices/invoice-INV-2025-000033.pdf and b/public/invoices/invoice-INV-2025-000033.pdf differ diff --git a/public/invoices/invoice-INV-2025-000034.pdf b/public/invoices/invoice-INV-2025-000034.pdf new file mode 100644 index 0000000..2257043 Binary files /dev/null and b/public/invoices/invoice-INV-2025-000034.pdf differ diff --git a/resources/views/admin/customers.blade.php b/resources/views/admin/customers.blade.php index 0cd4604..31db1f2 100644 --- a/resources/views/admin/customers.blade.php +++ b/resources/views/admin/customers.blade.php @@ -827,14 +827,29 @@ Customer Info Customer ID Orders - Total + Order Total + Total Payable {{-- NEW --}} + Remaining {{-- NEW --}} Create Date Status Actions + @forelse($customers as $c) + @php + // Invoice total (with GST) + $totalPayable = $c->invoices->sum('final_amount_with_gst'); + + // Total paid via installments + $totalPaid = $c->invoices + ->flatMap(fn($inv) => $inv->installments) + ->sum('amount'); + + // Remaining amount + $remainingAmount = max($totalPayable - $totalPaid, 0); + @endphp @@ -869,9 +884,33 @@ - ₹{{ number_format($c->orders->sum('ttl_amount'), 2) }} + + ₹{{ number_format($c->orders->sum('ttl_amount'), 2) }} + + + + ₹{{ number_format($totalPayable, 2) }} + + + + + @if($remainingAmount > 0) + + ₹{{ number_format($remainingAmount, 2) }} + + @else + + ₹0.00 + + @endif + + + + + + {{ $c->created_at ? $c->created_at->format('d-m-Y') : '-' }} diff --git a/resources/views/admin/customers_view.blade.php b/resources/views/admin/customers_view.blade.php index dc56665..d4dbb66 100644 --- a/resources/views/admin/customers_view.blade.php +++ b/resources/views/admin/customers_view.blade.php @@ -698,11 +698,48 @@
-
₹{{ number_format($totalAmount, 2) }}
+
₹{{ number_format($totalOrderAmount, 2) }}
Total Amount Spent
+ +
+
+
+ +
+
₹{{ number_format($totalPayable, 2) }}
+
Total Payable
+
+
+
+
+
+ +
+
₹{{ number_format($totalRemaining, 2) }}
+
Remaining Amount
+
+
+ + {{-- Mark Count --}}