Product Services filtering seems to work

orders
Nisse Lommerde 2 weeks ago
parent fcb1eda56f
commit 7740160b4f

@ -5,13 +5,18 @@ namespace App\Filament\Resources;
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;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;
class ServiceTypeResource extends Resource
{
private ?string $quantity = 'priv';
protected static ?string $model = ServiceType::class;
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
@ -38,21 +43,54 @@ class ServiceTypeResource extends Resource
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->getStateUsing(function () {
return 'test';
Tables\Columns\TextColumn::make('name'),
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']
);
}),
Tables\Columns\TextColumn::make('quantity'),
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']
);
})
->prefix('$'),
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']
);
})
->suffix('%')
->label('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']
);
})
->prefix('$'),
])
->filters([
//
Tables\Filters\Filter::make('dates')
->form([
Split::make([
DatePicker::make('created_from')
->label('From date'),
DatePicker::make('created_until')
->reactive()
->label('Until date'),
]),
]),
])
->actions([
])

@ -2,6 +2,7 @@
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;
@ -24,27 +25,59 @@ class ServiceType extends Model
//to do: date between?
public function getQuantityAttribute(): int
public function getQuantityAttribute($created_at = null, $created_until = null): int
{
return $this->productServices()->sum('amount');
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);
})
->sum('amount');
}
public function getAmountAttribute(): string
public function getAmountAttribute($created_at = null, $created_until = null): string
{
return number_format($this->productServices()->sum('amount_price'), 2);
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);
})
->sum('amount_price'),
2
);
}
public function getSalesPercentageAttribute(): float
public function getSalesPercentageAttribute($created_at = null, $created_until = null): float
{
$total = ProductService::all()->count();
$part = ProductService::where('service_type_id', $this->id)->count();
$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);
}
public function getAveragePriceAttribute(): string
public function getAveragePriceAttribute($created_at = null, $created_until = null): string
{
return number_format(floatval($this->amount) / $this->quantity, 2);
return number_format(floatval($this->getAmountAttribute($created_at, $created_until)) /
$this->getQuantityAttribute($created_at, $created_until), 2);
}
public function productServices(): HasMany

@ -13,7 +13,7 @@ class ProductServiceFactory extends Factory
public function definition(): array
{
return [
'created_at' => Carbon::now(),
'created_at' => Carbon::now()->subDays(rand(0, 31)),
'updated_at' => Carbon::now(),
'placement' => $this->faker->randomElement(['L/C', 'C/F', 'F/B', 'R/C']),
'amount' => $this->faker->randomNumber(1),

Binary file not shown.
Loading…
Cancel
Save