#79 Add sums of columns for Customer Reports
This commit is contained in:
parent
6fc753fc19
commit
779d46d708
@ -4,15 +4,15 @@
|
||||
|
||||
use App\Enums\IconEnum;
|
||||
use App\Models\Customer;
|
||||
use App\Models\Invoice;
|
||||
use Filament\Forms\Components\DatePicker;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Support\Enums\FontWeight;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Columns\Summarizers\Summarizer;
|
||||
use Filament\Tables\Columns\Summarizers\Sum;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
|
||||
class CustomerReportResource extends Resource
|
||||
{
|
||||
@ -41,68 +41,66 @@ public static function table(Table $table): Table
|
||||
Tables\Columns\TextColumn::make('company_name')
|
||||
->label('Customer')
|
||||
->sortable()
|
||||
->searchable()
|
||||
// ->searchable()
|
||||
->extraHeaderAttributes(['class' => 'w-full']),
|
||||
|
||||
Tables\Columns\TextColumn::make('subtotal')
|
||||
Tables\Columns\TextColumn::make('invoices.subtotal')
|
||||
->label('Subtotal')
|
||||
->money()
|
||||
// ->summarize(Summarizer::make()->using(function ($query, Table $table) {
|
||||
// $createdAt = $table->getfilter('created_at')->getstate()['created_at'] ?? '1900-01-01';
|
||||
// $createdUntil = $table->getfilter('created_until')->getstate()['created_until'] ?? '2100-01-01';
|
||||
//
|
||||
// $invoiceSum = invoice::wherebetween('date', [$createdAt, $createdUntil])->sum('subtotal');
|
||||
//
|
||||
// return '$'.number_format(round($invoiceSum, 2), 2, '.', ',');
|
||||
// }))
|
||||
->alignRight()
|
||||
->getStateUsing(function (Table $table, Model $record) {
|
||||
return $record->getSubtotalAttribute(
|
||||
$table->getFilter('created_at')->getState()['created_at'],
|
||||
$table->getFilter('created_until')->getState()['created_until']
|
||||
);
|
||||
}),
|
||||
->getStateUsing(fn (Table $table, Model $record) => $record->invoices()->tap(fn ($q) => self::applyDateFilters($q, $table))->sum('subtotal'))
|
||||
->summarize(Sum::make('subtotal')
|
||||
->label('')
|
||||
->money()
|
||||
->using(fn (Table $table, $query) => $query->tap(fn ($q) => self::applyDateFilters($q, $table))->sum('subtotal'))
|
||||
),
|
||||
|
||||
Tables\Columns\TextColumn::make('gst')
|
||||
Tables\Columns\TextColumn::make('invoices.hst_amount')
|
||||
->label('HST')
|
||||
->money()
|
||||
->alignRight()
|
||||
->getStateUsing(fn (Table $table, Model $record) => $record->invoices()->tap(fn ($q) => self::applyDateFilters($q, $table))->sum('hst_amount'))
|
||||
->summarize(Sum::make('hst_amount')
|
||||
->label('')
|
||||
->money()
|
||||
->using(fn (Table $table, Builder $query) => $query->tap(fn ($q) => self::applyDateFilters($q, $table))->sum('hst_amount'))
|
||||
),
|
||||
|
||||
Tables\Columns\TextColumn::make('invoices.gst_amount')
|
||||
->label('GST')
|
||||
->money()
|
||||
->alignRight()
|
||||
->getStateUsing(function (Table $table, Model $record) {
|
||||
return $record->getGstAttribute(
|
||||
$table->getFilter('created_at')->getState()['created_at'],
|
||||
$table->getFilter('created_until')->getState()['created_until']
|
||||
);
|
||||
}),
|
||||
->getStateUsing(fn (Table $table, Model $record) => $record->invoices()->tap(fn ($q) => self::applyDateFilters($q, $table))->sum('gst_amount'))
|
||||
->summarize(Sum::make('gst_amount')
|
||||
->label('')
|
||||
->money()
|
||||
->using(fn (Table $table, $query) => $query->tap(fn ($q) => self::applyDateFilters($q, $table))->sum('gst_amount'))
|
||||
),
|
||||
|
||||
Tables\Columns\TextColumn::make('pst')
|
||||
Tables\Columns\TextColumn::make('invoices.pst_amount')
|
||||
->label('PST')
|
||||
->money()
|
||||
->alignRight()
|
||||
->getStateUsing(function (Table $table, Customer $record) {
|
||||
return $record->getPstAttribute(
|
||||
$table->getFilter('created_at')->getState()['created_at'],
|
||||
$table->getFilter('created_until')->getState()['created_until']
|
||||
);
|
||||
}),
|
||||
->getStateUsing(fn (Table $table, Model $record) => $record->invoices()->tap(fn ($q) => self::applyDateFilters($q, $table))->sum('pst_amount'))
|
||||
->summarize(Sum::make('pst_amount')
|
||||
->label('')
|
||||
->money()
|
||||
->using(fn (Table $table, $query) => $query->tap(fn ($q) => self::applyDateFilters($q, $table))->sum('pst_amount'))
|
||||
),
|
||||
|
||||
Tables\Columns\TextColumn::make('total')
|
||||
Tables\Columns\TextColumn::make('invoices.total')
|
||||
->label('Total')
|
||||
->money()
|
||||
// ->summarize(summarizer::make()->using(function ($query, table $table) {
|
||||
// $createdAt = $table->getfilter('created_at')->getstate()['created_at'] ?? '1900-01-01';
|
||||
// $createdUntil = $table->getfilter('created_until')->getstate()['created_until'] ?? '2100-01-01';
|
||||
//
|
||||
// $invoiceSum = invoice::wherebetween('date', [$createdAt, $createdUntil])->sum('total');
|
||||
//
|
||||
// return '$'.number_format(round($invoiceSum, 2), 2, '.', ',');
|
||||
// }))
|
||||
->weight(FontWeight::Bold)
|
||||
->alignRight()
|
||||
->getStateUsing(function (Table $table, Model $record) {
|
||||
return $record->getTotalAttribute(
|
||||
$table->getFilter('created_at')->getState()['created_at'],
|
||||
$table->getFilter('created_until')->getState()['created_until']
|
||||
);
|
||||
}),
|
||||
->getStateUsing(fn (Table $table, Model $record) => $record->invoices()->tap(fn ($q) => self::applyDateFilters($q, $table))->sum('total'))
|
||||
->summarize(Sum::make('total')
|
||||
->label('')
|
||||
->money()
|
||||
->using(fn (Table $table, $query) => $query->tap(fn ($q) => self::applyDateFilters($q, $table))->sum('total'))
|
||||
),
|
||||
])
|
||||
|
||||
->filters([
|
||||
Tables\Filters\Filter::make('created_at')
|
||||
->form([
|
||||
@ -115,11 +113,16 @@ public static function table(Table $table): Table
|
||||
DatePicker::make('created_until')
|
||||
->label('Until date'),
|
||||
]),
|
||||
])
|
||||
->actions([
|
||||
])
|
||||
->bulkActions([
|
||||
]);
|
||||
], layout: Tables\Enums\FiltersLayout::AboveContent);
|
||||
}
|
||||
|
||||
protected static function applyDateFilters($query, Table $table): void
|
||||
{
|
||||
$createdAt = $table->getFilter('created_at')?->getState()['created_at'] ?? null;
|
||||
$createdUntil = $table->getFilter('created_until')?->getState()['created_until'] ?? null;
|
||||
|
||||
$query->when($createdAt, fn ($q, $date) => $q->whereDate('created_at', '>=', $date));
|
||||
$query->when($createdUntil, fn ($q, $date) => $q->whereDate('created_at', '<=', $date));
|
||||
}
|
||||
|
||||
public static function canAccess(): bool
|
||||
|
@ -52,6 +52,7 @@ public static function table(Table $table): Table
|
||||
->extraHeaderAttributes(['class' => 'w-full']),
|
||||
|
||||
TextColumn::make('productServices.amount')
|
||||
->label('Quantity')
|
||||
->alignRight()
|
||||
->getStateUsing(function (Table $table, Model $record) {
|
||||
return $record->getQuantityAttribute(
|
||||
@ -71,6 +72,7 @@ public static function table(Table $table): Table
|
||||
),
|
||||
|
||||
TextColumn::make('productServices.amount_price')
|
||||
->label('Amount')
|
||||
->alignRight()
|
||||
->money()
|
||||
->getStateUsing(function (Table $table, Model $record) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user