2026-03-15 17:08:23 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Modules\Business\App\Exports;
|
|
|
|
|
|
|
|
|
|
use App\Models\Party;
|
|
|
|
|
use Illuminate\Contracts\View\View;
|
|
|
|
|
use Maatwebsite\Excel\Concerns\FromView;
|
|
|
|
|
|
|
|
|
|
class ExportCustomerLedger implements FromView
|
|
|
|
|
{
|
|
|
|
|
public function view(): View
|
|
|
|
|
{
|
|
|
|
|
$customers = Party::with('sales')
|
|
|
|
|
->where('business_id', auth()->user()->business_id)
|
|
|
|
|
->where('type', '!=', 'Supplier')
|
2026-05-14 11:55:22 +07:00
|
|
|
->when(request('search'), function ($q) {
|
|
|
|
|
$q->where(function ($q) {
|
|
|
|
|
$q->where('name', 'like', '%' . request('search') . '%')
|
|
|
|
|
->orWhere('phone', 'like', '%' . request('search') . '%')
|
|
|
|
|
->orWhere('type', 'like', '%' . request('search') . '%');
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
->when(request('type'), function ($q) {
|
|
|
|
|
$q->where(function ($q) {
|
|
|
|
|
$q->where('type', request('type'));
|
|
|
|
|
});
|
|
|
|
|
})
|
2026-03-15 17:08:23 +07:00
|
|
|
->latest()
|
2026-05-14 11:55:22 +07:00
|
|
|
->limit(request('per_page') ?? 20)->get();
|
2026-03-15 17:08:23 +07:00
|
|
|
|
|
|
|
|
$totalAmount = $customers->sum(function ($customer) {
|
|
|
|
|
return $customer->sales?->sum('totalAmount') ?? 0;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$totalPaid = $customers->sum(function ($customer) {
|
|
|
|
|
return $customer->sales?->sum('paidAmount') ?? 0;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$totalDue = $customers->sum(function ($customer) {
|
|
|
|
|
return $customer->sales?->sum('dueAmount') ?? 0;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return view('business::party-reports.customer-ledger.excel-csv', compact('customers', 'totalAmount', 'totalPaid', 'totalDue'));
|
|
|
|
|
}
|
|
|
|
|
}
|