#98 Product Services Sum / Totals
This commit is contained in:
parent
e45041566a
commit
cab2b2c478
@ -9,6 +9,8 @@
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Columns\Summarizers\Sum;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
@ -42,44 +44,65 @@ public static function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
Tables\Columns\TextColumn::make('name')
|
||||
TextColumn::make('name')
|
||||
->label('Code'),
|
||||
|
||||
Tables\Columns\TextColumn::make('value')
|
||||
TextColumn::make('value')
|
||||
->label('Long Name')
|
||||
->extraHeaderAttributes(['class' => 'w-full']),
|
||||
|
||||
Tables\Columns\TextColumn::make('quantity')
|
||||
TextColumn::make('productServices.amount')
|
||||
->alignRight()
|
||||
->getStateUsing(function (Table $table, Model $record) {
|
||||
return $record->getQuantityAttribute(
|
||||
$table->getFilter('created_at')->getState()['created_at'],
|
||||
$table->getFilter('created_until')->getState()['created_until']
|
||||
);
|
||||
}),
|
||||
})
|
||||
->summarize(Sum::make('amount')
|
||||
->label('')
|
||||
->using(fn (Table $table, $query) => $query
|
||||
->when($createdAt = $table->getFilter('created_at')->getState()['created_at'] ?? null,
|
||||
fn ($q) => $q->whereDate('created_at', '>=', $createdAt))
|
||||
->when($createdUntil = $table->getFilter('created_until')->getState()['created_until'] ?? null,
|
||||
fn ($q) => $q->whereDate('created_at', '<=', $createdUntil))
|
||||
->sum('amount')
|
||||
)
|
||||
),
|
||||
|
||||
Tables\Columns\TextColumn::make('amount')
|
||||
TextColumn::make('productServices.amount_price')
|
||||
->alignRight()
|
||||
->money()
|
||||
->getStateUsing(function (Table $table, Model $record) {
|
||||
return $record->getAmountAttribute(
|
||||
$table->getFilter('created_at')->getState()['created_at'],
|
||||
$table->getFilter('created_until')->getState()['created_until']
|
||||
);
|
||||
})
|
||||
->money('usd'),
|
||||
->summarize(Sum::make('amount_price')
|
||||
->label('')
|
||||
->money()
|
||||
->using(fn (Table $table, $query) => $query
|
||||
->when($createdAt = $table->getFilter('created_at')->getState()['created_at'] ?? null,
|
||||
fn ($q) => $q->whereDate('created_at', '>=', $createdAt))
|
||||
->when($createdUntil = $table->getFilter('created_until')->getState()['created_until'] ?? null,
|
||||
fn ($q) => $q->whereDate('created_at', '<=', $createdUntil))
|
||||
->sum('amount_price')
|
||||
)
|
||||
),
|
||||
|
||||
Tables\Columns\TextColumn::make('salesPercentage')
|
||||
TextColumn::make('salesPercentage')
|
||||
->alignRight()
|
||||
->suffix('%')
|
||||
->label('% sales')
|
||||
->getStateUsing(function (Table $table, Model $record) {
|
||||
return $record->getSalesPercentageAttribute(
|
||||
$table->getFilter('created_at')->getState()['created_at'],
|
||||
$table->getFilter('created_until')->getState()['created_until']
|
||||
);
|
||||
})
|
||||
->suffix('%')
|
||||
->label('% sales'),
|
||||
}),
|
||||
|
||||
Tables\Columns\TextColumn::make('averagePrice')
|
||||
TextColumn::make('averagePrice')
|
||||
->getStateUsing(function (Table $table, Model $record) {
|
||||
return $record->getAveragePriceAttribute(
|
||||
$table->getFilter('created_at')->getState()['created_at'],
|
||||
|
@ -42,7 +42,7 @@ public function getSalesPercentageAttribute($created_at = null, $created_until =
|
||||
{
|
||||
$query = ProductService::query()
|
||||
->when($created_at, fn ($query) => $query->whereDate('created_at', '>=', $created_at))
|
||||
->when($created_until, fn ($query) => $query->whereDate('created_until', '<=', $created_until));
|
||||
->when($created_until, fn ($query) => $query->whereDate('created_at', '<=', $created_until));
|
||||
|
||||
$total = $query->count();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user