Optimized product services reports code

orders
Nisse Lommerde 2 weeks ago
parent 7740160b4f
commit 1f1f783aa9

@ -154,6 +154,7 @@ class InvoiceResource extends Resource
->actions([
Tables\Actions\EditAction::make(),
//todo: generate report pdf
])
->bulkActions([

@ -6,7 +6,6 @@ use App\Filament\Resources\ServiceTypeResource\Pages;
use App\Filament\Resources\ServiceTypeResource\Widgets\ServiceTypeOverview;
use App\Models\ServiceType;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Split;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
@ -47,16 +46,16 @@ class ServiceTypeResource extends Resource
Tables\Columns\TextColumn::make('quantity')
->getStateUsing(function (Table $table, Model $record) {
return $record->getQuantityAttribute(
$table->getFilter('dates')->getState()['created_from'],
$table->getFilter('dates')->getState()['created_until']
$table->getFilter('created_at')->getState()['created_at'],
$table->getFilter('created_until')->getState()['created_until']
);
}),
Tables\Columns\TextColumn::make('amount')
->getStateUsing(function (Table $table, Model $record) {
return $record->getAmountAttribute(
$table->getFilter('dates')->getState()['created_from'],
$table->getFilter('dates')->getState()['created_until']
$table->getFilter('created_at')->getState()['created_at'],
$table->getFilter('created_until')->getState()['created_until']
);
})
->prefix('$'),
@ -64,36 +63,38 @@ class ServiceTypeResource extends Resource
Tables\Columns\TextColumn::make('salesPercentage')
->getStateUsing(function (Table $table, Model $record) {
return $record->getSalesPercentageAttribute(
$table->getFilter('dates')->getState()['created_from'],
$table->getFilter('dates')->getState()['created_until']
$table->getFilter('created_at')->getState()['created_at'],
$table->getFilter('created_until')->getState()['created_until']
);
})
->suffix('%')
->label('Percentage'),
->label('Sales percentage'),
Tables\Columns\TextColumn::make('averagePrice')
->getStateUsing(function (Table $table, Model $record) {
return $record->getAveragePriceAttribute(
$table->getFilter('dates')->getState()['created_from'],
$table->getFilter('dates')->getState()['created_until']
$table->getFilter('created_at')->getState()['created_at'],
$table->getFilter('created_until')->getState()['created_until']
);
})
->prefix('$'),
])
->filters([
Tables\Filters\Filter::make('dates')
Tables\Filters\Filter::make('created_at')
->form([
Split::make([
DatePicker::make('created_from')
DatePicker::make('created_at')
->label('From date'),
]),
Tables\Filters\Filter::make('created_until')
->form([
DatePicker::make('created_until')
->reactive()
->label('Until date'),
]),
]),
])
], layout: Tables\Enums\FiltersLayout::AboveContentCollapsible)
->actions([
])
->bulkActions([
]);
}

@ -2,7 +2,6 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
@ -23,17 +22,11 @@ class ServiceType extends Model
'average_price',
];
//to do: date between?
public function getQuantityAttribute($created_at = null, $created_until = null): int
{
return $this->productServices()
->when($created_at, function (Builder $query) use ($created_at) {
return $query->whereDate('created_at', '>=', $created_at);
})
->when($created_until, function (Builder $query) use ($created_until) {
return $query->whereDate('created_at', '<=', $created_until);
})
->when($created_at, fn ($query) => $query->whereDate('created_at', '>=', $created_at))
->when($created_until, fn ($query) => $query->whereDate('created_at', '<=', $created_until))
->sum('amount');
}
@ -41,12 +34,8 @@ class ServiceType extends Model
{
return number_format(
$this->productServices()
->when($created_at, function (Builder $query) use ($created_at) {
return $query->whereDate('created_at', '>=', $created_at);
})
->when($created_until, function (Builder $query) use ($created_until) {
return $query->whereDate('created_at', '<=', $created_until);
})
->when($created_at, fn ($query) => $query->whereDate('created_at', '>=', $created_at))
->when($created_until, fn ($query) => $query->whereDate('created_at', '<=', $created_until))
->sum('amount_price'),
2
);
@ -54,30 +43,31 @@ class ServiceType extends Model
public function getSalesPercentageAttribute($created_at = null, $created_until = null): float
{
$total = ProductService::when($created_at, function (Builder $query) use ($created_at) {
return $query->whereDate('created_at', '>=', $created_at);
})
->when($created_until, function (Builder $query) use ($created_until) {
return $query->whereDate('created_at', '<=', $created_until);
})
->count();
$part = ProductService::where('service_type_id', $this->id)
->when($created_at, function (Builder $query) use ($created_at) {
return $query->whereDate('created_at', '>=', $created_at);
})
->when($created_until, function (Builder $query) use ($created_until) {
return $query->whereDate('created_at', '<=', $created_until);
})
->count();
return round(($part / $total) * 100, 2);
$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));
$total = $query->count();
$part = $query->where('service_type_id', $this->id)->count();
if ($total == 0) {
return 0.0;
}
return round(($part / $total) * 100, 1);
}
public function getAveragePriceAttribute($created_at = null, $created_until = null): string
{
return number_format(floatval($this->getAmountAttribute($created_at, $created_until)) /
$this->getQuantityAttribute($created_at, $created_until), 2);
$quantity = $this->getQuantityAttribute($created_at, $created_until);
$amount = $this->getAmountAttribute($created_at, $created_until);
if ($quantity == 0) {
return '0.0';
}
return number_format($quantity / $amount, 2);
}
public function productServices(): HasMany

@ -0,0 +1,5 @@
todo
- finish invoice styling
- add to invoice button on order page
- customer name to invoice title / filename
Loading…
Cancel
Save