diff --git a/app/Filament/Resources/ServiceTypeResource.php b/app/Filament/Resources/ServiceTypeResource.php index 3d5eb59..39d3a20 100644 --- a/app/Filament/Resources/ServiceTypeResource.php +++ b/app/Filament/Resources/ServiceTypeResource.php @@ -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([ ]) diff --git a/app/Models/ServiceType.php b/app/Models/ServiceType.php index e42fddd..8f8a46d 100644 --- a/app/Models/ServiceType.php +++ b/app/Models/ServiceType.php @@ -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 diff --git a/database/factories/ProductServiceFactory.php b/database/factories/ProductServiceFactory.php index 64465de..1e3c3b8 100644 --- a/database/factories/ProductServiceFactory.php +++ b/database/factories/ProductServiceFactory.php @@ -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), diff --git a/time.ods b/time.ods index 2e09882..26ed7c0 100644 Binary files a/time.ods and b/time.ods differ