Optimized product services reports code

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

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

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

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