feat: Remove EggPrice resource and related components from the application

This commit is contained in:
Yoga Pangestu 2026-02-26 07:56:22 +07:00
parent 75712c4f2a
commit 0210d08477
11 changed files with 129 additions and 573 deletions

View File

@ -2,9 +2,7 @@
namespace App\Filament\Resources\DelayedGoods\Schemas;
use App\Models\EggPrice;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
@ -23,56 +21,24 @@ public static function configure(Schema $schema): Schema
$set('total_amount', number_format($totalAmount, 0, ',', '.'));
};
return $schema->components([
Hidden::make('unit_id')
->required(),
$recalculateRemaining = function (callable $get, callable $set): void {
$paidAmount = (int) str()->replace('.', '', (string) ($get('paid_amount') ?? 0));
$totalAmount = (int) str()->replace('.', '', (string) ($get('total_amount') ?? 0));
$remaining = max(0, $totalAmount - $paidAmount);
$set('remaining_amount', number_format($remaining, 0, ',', '.'));
};
return $schema->components([
Grid::make(2)
->schema([
Select::make('egg_price_id')
->label('Harga Telur')
->options(
EggPrice::query()
->with('unit')
->orderBy('name')
->get()
->mapWithKeys(function ($item) {
return [$item->id => $item->name.' ('.($item->unit?->alias ?? '-').')'];
})
)
->searchable()
->preload()
DatePicker::make('stored_date')
->label('Tanggal Penyimpanan')
->default(now())
->required()
->native(false)
->live()
->afterStateHydrated(function ($state, callable $set, callable $get, $record): void {
if ($record && ! $state) {
$eggPrice = EggPrice::where('unit_id', $record->unit_id)
->where('price', $record->unit_price)
->first();
if ($eggPrice) {
$set('egg_price_id', $eggPrice->id);
}
}
})
->afterStateUpdated(function ($state, callable $set, callable $get) use ($recalculateTotal): void {
if (! $state) {
$set('unit_price', 0);
$set('unit_id', null);
$recalculateTotal($get, $set);
return;
}
$price = EggPrice::with('unit')->find($state);
$set('unit_price', number_format($price?->price ?? 0, 0, ',', '.'));
$set('unit_id', $price?->unit_id);
$recalculateTotal($get, $set);
})
->dehydrated(false)
->required(),
->displayFormat('l, d F Y'),
Select::make('customer_id')
->label('Pelanggan')
@ -81,43 +47,48 @@ public static function configure(Schema $schema): Schema
->preload()
->native(false)
->required(),
]),
DatePicker::make('stored_date')
->label('Tanggal Penyimpanan')
->default(now())
->required()
->native(false)
->displayFormat('l, d F Y'),
Grid::make(3)
->schema([
TextInput::make('quantity')
->label('Jumlah Barang')
->placeholder('0')
->autocomplete(false)
->required()
->live()
->afterStateUpdated(function ($state, callable $get, callable $set) use ($recalculateTotal): void {
->afterStateUpdated(function ($state, callable $get, callable $set) use ($recalculateTotal, $recalculateRemaining): void {
$recalculateTotal($get, $set);
$recalculateRemaining($get, $set);
})
->currencyMask(thousandSeparator: '.', decimalSeparator: ',', precision: 0)
->dehydrateStateUsing(fn ($state) => (int) str()->replace('.', '', (string) ($state ?? 0))),
]),
Grid::make(2)
->schema([
TextInput::make('unit_price')
->label('Harga per Satuan')
->placeholder('0')
->readOnly()
->autocomplete(false)
->prefix('Rp')
->required()
->formatStateUsing(
fn ($state) => number_format((int) ($state ?? 0), 0, ',', '.')
)
->live()
->currencyMask(thousandSeparator: '.', decimalSeparator: ',', precision: 0)
->dehydrateStateUsing(fn ($state) => (int) str()->replace('.', '', (string) ($state ?? 0)))
->afterStateUpdated(function ($state, callable $get, callable $set) use ($recalculateTotal): void {
->afterStateUpdated(function ($state, callable $get, callable $set) use ($recalculateTotal, $recalculateRemaining): void {
$recalculateTotal($get, $set);
$recalculateRemaining($get, $set);
}),
Select::make('unit_id')
->label('Satuan')
->relationship('unit', 'alias')
->searchable()
->preload()
->native(false)
->required(),
]),
Grid::make(3)
->schema([
TextInput::make('total_amount')
->label('Total')
->placeholder('0')
@ -129,10 +100,7 @@ public static function configure(Schema $schema): Schema
fn ($state) => number_format((int) ($state ?? 0), 0, ',', '.')
)
->dehydrateStateUsing(fn ($state) => (int) str()->replace('.', '', (string) ($state ?? 0))),
]),
Grid::make(3)
->schema([
TextInput::make('paid_amount')
->label('Jumlah yang Sudah Dibayar (DP)')
->placeholder('0')
@ -142,10 +110,12 @@ public static function configure(Schema $schema): Schema
->live()
->prefix('Rp')
->readOnly(fn (callable $get) => $get('is_paid'))
->afterStateUpdated(function ($state, callable $get, callable $set): void {
->afterStateUpdated(function ($state, callable $get, callable $set) use ($recalculateRemaining): void {
$paidAmount = (int) str()->replace('.', '', (string) ($state ?? 0));
$totalAmount = (int) str()->replace('.', '', (string) ($get('total_amount') ?? 0));
$recalculateRemaining($get, $set);
if (! $get('is_paid')) {
$set('is_paid', $paidAmount >= $totalAmount && $totalAmount > 0);
}
@ -167,6 +137,7 @@ public static function configure(Schema $schema): Schema
->readOnly()
->prefix('Rp')
->default(0)
->live()
->formatStateUsing(function ($state, callable $get) {
$paidAmount = (int) str()->replace('.', '', (string) ($get('paid_amount') ?? 0));
$totalAmount = (int) str()->replace('.', '', (string) ($get('total_amount') ?? 0));
@ -174,14 +145,13 @@ public static function configure(Schema $schema): Schema
return number_format($remaining, 0, ',', '.');
})
->dehydrated(false)
->live(),
->dehydrated(false),
Toggle::make('is_paid')
->label('Lunas')
->default(false)
->live()
->afterStateUpdated(function ($state, callable $get, callable $set): void {
->afterStateUpdated(function ($state, callable $get, callable $set) use ($recalculateRemaining): void {
$totalAmount = (int) str()->replace('.', '', (string) ($get('total_amount') ?? 0));
if ($state) {
@ -194,6 +164,8 @@ public static function configure(Schema $schema): Schema
$set('paid_amount', 0);
$set('payment_date', null);
}
$recalculateRemaining($get, $set);
}),
DatePicker::make('payment_date')

View File

@ -10,12 +10,10 @@
use App\Filament\Actions\DefaultBulkActions;
use App\Filament\Columns\TimestampColumns;
use App\Filament\Resources\Manage\Orders\Pages\ManageOrders;
use App\Models\EggPrice;
use App\Models\Order;
use BackedEnum;
use Filament\Actions\BulkActionGroup;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
@ -59,132 +57,90 @@ public static function form(Schema $schema): Schema
$set('total_amount', number_format($totalAmount, 0, ',', '.'));
};
return $schema->components([
Hidden::make('unit_id')
->required(),
return $schema
->components([
DatePicker::make('order_date')
->label('Tanggal Order')
->default(now())
->required()
->native(false)
->displayFormat('l, d F Y'),
Grid::make(2)
->schema([
Select::make('egg_price_id')
->label('Harga Telur')
->options(
EggPrice::query()
->with('unit')
->orderBy('name')
->get()
->mapWithKeys(function ($item) {
return [$item->id => $item->name.' ('.($item->unit?->alias ?? '-').')'];
})
)
->searchable()
->preload()
->native(false)
->live()
->afterStateHydrated(function ($state, callable $set, callable $get, $record): void {
if ($record && ! $state) {
$eggPrice = EggPrice::where('unit_id', $record->unit_id)
->where('price', $record->unit_price)
->first();
Select::make('customer_id')
->label('Pelanggan')
->relationship('customer', 'name')
->searchable()
->preload()
->native(false),
if ($eggPrice) {
$set('egg_price_id', $eggPrice->id);
}
}
})
->afterStateUpdated(function ($state, callable $set, callable $get) use ($recalculateTotal): void {
if (! $state) {
$set('unit_price', 0);
$set('unit_id', null);
Grid::make(3)
->schema([
TextInput::make('egg_quantity')
->label('Jumlah Telur')
->placeholder('0')
->autocomplete(false)
->helperText('Jumlah per satuan.')
->required()
->live()
->afterStateUpdated(function ($state, callable $get, callable $set) use ($recalculateTotal): void {
$recalculateTotal($get, $set);
})
->currencyMask(thousandSeparator: '.', decimalSeparator: ',', precision: 0)
->dehydrateStateUsing(fn ($state) => (int) str()->replace('.', '', (string) ($state ?? 0))),
return;
}
TextInput::make('unit_price')
->label('Harga per Satuan')
->placeholder('0')
->autocomplete(false)
->prefix('Rp')
->required()
->live()
->currencyMask(thousandSeparator: '.', decimalSeparator: ',', precision: 0)
->dehydrateStateUsing(fn ($state) => (int) str()->replace('.', '', (string) ($state ?? 0)))
->afterStateUpdated(function ($state, callable $get, callable $set) use ($recalculateTotal): void {
$recalculateTotal($get, $set);
}),
$price = EggPrice::with('unit')->find($state);
Select::make('unit_id')
->label('Satuan')
->relationship('unit', 'alias')
->searchable()
->preload()
->native(false)
->required(),
])
->columnSpanFull(),
$set('unit_price', number_format($price?->price ?? 0, 0, ',', '.'));
$set('unit_id', $price?->unit_id);
TextInput::make('discount')
->label('Diskon')
->placeholder('0')
->default(0)
->prefix('Rp')
->live()
->afterStateUpdated(function ($state, callable $get, callable $set) use ($recalculateTotal): void {
$recalculateTotal($get, $set);
})
->currencyMask(thousandSeparator: '.', decimalSeparator: ',', precision: 0)
->dehydrateStateUsing(fn ($state) => (int) str()->replace('.', '', (string) ($state ?? 0))),
$recalculateTotal($get, $set);
})
->dehydrated(false)
->required(),
TextInput::make('total_amount')
->label('Total')
->placeholder('0')
->readOnly()
->prefix('Rp')
->required()
->formatStateUsing(
fn ($state) => number_format((int) ($state ?? 0), 0, ',', '.')
)
->dehydrateStateUsing(fn ($state) => (int) str()->replace('.', '', (string) ($state ?? 0))),
Select::make('customer_id')
->label('Pelanggan')
->relationship('customer', 'name')
->searchable()
->preload()
->native(false),
DatePicker::make('order_date')
->label('Tanggal Order')
->default(now())
->required()
->native(false)
->displayFormat('l, d F Y'),
TextInput::make('egg_quantity')
->label('Jumlah Telur')
->placeholder('0')
->autocomplete(false)
->helperText('Jumlah per satuan.')
->required()
->live()
->afterStateUpdated(function ($state, callable $get, callable $set) use ($recalculateTotal): void {
$recalculateTotal($get, $set);
})
->currencyMask(thousandSeparator: '.', decimalSeparator: ',', precision: 0)
->dehydrateStateUsing(fn ($state) => (int) str()->replace('.', '', (string) ($state ?? 0))),
]),
Grid::make(3)
->schema([
TextInput::make('unit_price')
->label('Harga per Satuan')
->placeholder('0')
->readOnly()
->prefix('Rp')
->required()
->formatStateUsing(
fn ($state) => number_format((int) ($state ?? 0), 0, ',', '.')
)
->dehydrateStateUsing(fn ($state) => (int) str()->replace('.', '', (string) ($state ?? 0)))
->afterStateUpdated(function ($state, callable $get, callable $set) use ($recalculateTotal): void {
$recalculateTotal($get, $set);
}),
TextInput::make('discount')
->label('Diskon')
->placeholder('0')
->default(0)
->prefix('Rp')
->live()
->afterStateUpdated(function ($state, callable $get, callable $set) use ($recalculateTotal): void {
$recalculateTotal($get, $set);
})
->currencyMask(thousandSeparator: '.', decimalSeparator: ',', precision: 0)
->dehydrateStateUsing(fn ($state) => (int) str()->replace('.', '', (string) ($state ?? 0))),
TextInput::make('total_amount')
->label('Total')
->placeholder('0')
->readOnly()
->prefix('Rp')
->required()
->formatStateUsing(
fn ($state) => number_format((int) ($state ?? 0), 0, ',', '.')
)
->dehydrateStateUsing(fn ($state) => (int) str()->replace('.', '', (string) ($state ?? 0))),
]),
Textarea::make('notes')
->label('Catatan')
->placeholder('...')
->autocomplete(false)
->columnSpanFull(),
])
->columns(1);
Textarea::make('notes')
->label('Catatan')
->placeholder('...')
->autocomplete(false)
->columnSpanFull(),
])
->columns(2);
}
public static function table(Table $table): Table
@ -223,6 +179,12 @@ public static function table(Table $table): Table
->money('IDR', decimalPlaces: 0)
->sortable(),
TextColumn::make('notes')
->label('Catatan')
->sortable()
->searchable()
->toggleable(isToggledHiddenByDefault: true),
...TimestampColumns::make(),
])
->filters([

View File

@ -1,134 +0,0 @@
<?php
namespace App\Filament\Resources\Master\EggPrices;
use App\Enums\RoleEnum;
use App\Filament\Actions\Cheerful\DeleteAction;
use App\Filament\Actions\Cheerful\EditAction;
use App\Filament\Actions\Cheerful\ForceDeleteAction;
use App\Filament\Actions\Cheerful\RestoreAction;
use App\Filament\Actions\DefaultBulkActions;
use App\Filament\Columns\TimestampColumns;
use App\Filament\Resources\Master\EggPrices\Pages\ManageEggPrices;
use App\Models\EggPrice;
use BackedEnum;
use Filament\Actions\BulkActionGroup;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Enums\Width;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use UnitEnum;
class EggPriceResource extends Resource
{
protected static ?string $model = EggPrice::class;
protected static string|UnitEnum|null $navigationGroup = 'Master';
protected static string|BackedEnum|null $navigationIcon = Heroicon::CurrencyDollar;
protected static ?string $navigationLabel = 'Harga Telur';
protected static ?int $navigationSort = 5;
protected static ?string $recordTitleAttribute = 'name';
protected static ?string $slug = 'master/egg-prices';
public static function form(Schema $schema): Schema
{
return $schema
->components([
TextInput::make('name')
->label('Tipe Harga')
->placeholder('Ecer, Distributor, Grosir')
->required()
->maxLength(50)
->autofocus()
->autocomplete(false),
Select::make('unit_id')
->label('Satuan')
->relationship('unit', 'name')
->required()
->native(false)
->searchable()
->preload(),
TextInput::make('price')
->label('Harga')
->placeholder('0')
->autocomplete(false)
->required()
->currencyMask(thousandSeparator: '.', decimalSeparator: ',', precision: 0)
->prefix('Rp')
->dehydrateStateUsing(fn ($state) => (int) str()->replace(',', '', (string) ($state ?? 0))),
])
->columns(1);
}
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('name')
->label('Tipe Harga')
->searchable()
->sortable(),
TextColumn::make('price')
->label('Harga')
->money('IDR', decimalPlaces: 0)
->suffix(fn (EggPrice $record) => ' / '.$record->unit?->alias)
->sortable(),
...TimestampColumns::make(),
])
->filters([
TrashedFilter::make()
->native(false)
->visible(fn (): bool => auth()->user()->hasRole(RoleEnum::DEVELOPER)),
])
->recordActions([
EditAction::make()
->modalWidth(Width::Large),
DeleteAction::make(),
ForceDeleteAction::make(),
RestoreAction::make(),
])
->toolbarActions([
BulkActionGroup::make([
...DefaultBulkActions::make('Harga Telur'),
]),
])
->emptyStateIcon(Heroicon::CurrencyDollar)
->emptyStateDescription('Setelah Anda membuat data pertama, maka akan muncul disini.')
->defaultSort('created_at', 'desc')
->deferFilters(false);
}
public static function getPages(): array
{
return [
'index' => ManageEggPrices::route('/'),
];
}
public static function getRecordRouteBindingEloquentQuery(): Builder
{
return parent::getRecordRouteBindingEloquentQuery()
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}
}

View File

@ -1,31 +0,0 @@
<?php
namespace App\Filament\Resources\Master\EggPrices\Pages;
use App\Filament\Actions\Cheerful\CreateAction;
use App\Filament\Resources\Master\EggPrices\EggPriceResource;
use Filament\Resources\Pages\ManageRecords;
use Filament\Support\Enums\Width;
class ManageEggPrices extends ManageRecords
{
protected static string $resource = EggPriceResource::class;
protected static ?string $title = 'Harga Telur';
protected function getHeaderActions(): array
{
return [
CreateAction::make()
->label('Tambah')
->modalHeading('Tambah Harga Telur')
->modalSubmitActionLabel('Simpan')
->modalCancelActionLabel('Batal')
->extraModalFooterActions(fn (CreateAction $action): array => [
$action->makeModalSubmitAction('createAnother', arguments: ['another' => true])
->label('Simpan dan Tambah Lagi'),
])
->modalWidth(Width::Large),
];
}
}

View File

@ -1,27 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
class EggPrice extends Model
{
use HasFactory, SoftDeletes;
protected $guarded = ['id'];
protected function casts(): array
{
return [
'price' => 'integer',
];
}
public function unit(): BelongsTo
{
return $this->belongsTo(Unit::class);
}
}

View File

@ -1,69 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Policies;
use App\Models\EggPrice;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Foundation\Auth\User as AuthUser;
class EggPricePolicy
{
use HandlesAuthorization;
public function viewAny(AuthUser $authUser): bool
{
return $authUser->can('ViewAny:EggPrice');
}
public function view(AuthUser $authUser, EggPrice $eggPrice): bool
{
return $authUser->can('View:EggPrice');
}
public function create(AuthUser $authUser): bool
{
return $authUser->can('Create:EggPrice');
}
public function update(AuthUser $authUser, EggPrice $eggPrice): bool
{
return $authUser->can('Update:EggPrice');
}
public function delete(AuthUser $authUser, EggPrice $eggPrice): bool
{
return $authUser->can('Delete:EggPrice');
}
public function restore(AuthUser $authUser, EggPrice $eggPrice): bool
{
return $authUser->can('Restore:EggPrice');
}
public function forceDelete(AuthUser $authUser, EggPrice $eggPrice): bool
{
return $authUser->can('ForceDelete:EggPrice');
}
public function forceDeleteAny(AuthUser $authUser): bool
{
return $authUser->can('ForceDeleteAny:EggPrice');
}
public function restoreAny(AuthUser $authUser): bool
{
return $authUser->can('RestoreAny:EggPrice');
}
public function replicate(AuthUser $authUser, EggPrice $eggPrice): bool
{
return $authUser->can('Replicate:EggPrice');
}
public function reorder(AuthUser $authUser): bool
{
return $authUser->can('Reorder:EggPrice');
}
}

View File

@ -1,26 +0,0 @@
<?php
namespace Database\Factories;
use App\Models\EggPrice;
use App\Models\Unit;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories.Factory<\App\Models\EggPrice>
*/
class EggPriceFactory extends Factory
{
protected $model = EggPrice::class;
public function definition(): array
{
$unit = Unit::inRandomOrder()->first() ?? Unit::factory()->create();
return [
'name' => $this->faker->randomElement(['Ecer', 'Grosir', 'Distributor']).' '.$this->faker->randomElement(['Pagi', 'Siang', 'Malam']),
'unit_id' => $unit->id,
'price' => $this->faker->numberBetween(1500, 3000),
];
}
}

View File

@ -3,8 +3,8 @@
namespace Database\Factories;
use App\Models\Customer;
use App\Models\EggPrice;
use App\Models\Order;
use App\Models\Unit;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
@ -23,19 +23,19 @@ public function definition(): array
{
$date = $this->faker->dateTimeBetween('-3 months', 'now');
$eggPrice = EggPrice::inRandomOrder()->first() ?? EggPrice::factory()->create();
$eggPrice = $this->faker->numberBetween(28000, 35000);
$customer = Customer::inRandomOrder()->first() ?? Customer::factory()->create();
$eggQuantity = $this->faker->numberBetween(10, 500);
$unitPrice = $eggPrice->price;
$unitPrice = $eggPrice / 10;
$discount = $this->faker->optional(0.3)->numberBetween(5000, 50000) ?? 0;
$subtotal = $eggQuantity * $unitPrice;
$totalAmount = max(0, $subtotal - $discount);
return [
'customer_id' => $customer->id,
'unit_id' => $eggPrice->unit_id,
'unit_id' => Unit::inRandomOrder()->first()->id ?? Unit::factory()->create()->id,
'egg_quantity' => $eggQuantity,
'unit_price' => $unitPrice,
'order_date' => $date,

View File

@ -1,26 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('egg_prices', function (Blueprint $table) {
$table->id();
$table->string('name', 50);
$table->foreignId('unit_id')->constrained()->cascadeOnDelete();
$table->unsignedInteger('price');
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->nullable()->useCurrentOnUpdate();
$table->softDeletes();
});
}
public function down(): void
{
Schema::dropIfExists('egg_prices');
}
};

View File

@ -24,7 +24,6 @@ public function run(): void
FeedPurchaseSeeder::class,
EggCollectionSeeder::class,
CustomerSeeder::class,
EggPriceSeeder::class,
OrderSeeder::class,
DelayedGoodSeeder::class,
ExpenseSeeder::class,

View File

@ -1,64 +0,0 @@
<?php
namespace Database\Seeders;
use App\Models\EggPrice;
use App\Models\Unit;
use Illuminate\Database\Seeder;
class EggPriceSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$kgUnit = Unit::where('alias', 'kg')->first();
$butirUnit = Unit::where('alias', 'butir')->first();
$defaults = [
[
'name' => 'Ecer',
'price' => 28000,
'unit_id' => $kgUnit->id,
],
[
'name' => 'Grosir',
'price' => 26000,
'unit_id' => $kgUnit->id,
],
[
'name' => 'Distributor',
'price' => 25000,
'unit_id' => $kgUnit->id,
],
[
'name' => 'Ecer',
'price' => 2800,
'unit_id' => $butirUnit->id,
],
[
'name' => 'Grosir',
'price' => 2600,
'unit_id' => $butirUnit->id,
],
[
'name' => 'Distributor',
'price' => 2500,
'unit_id' => $butirUnit->id,
],
];
foreach ($defaults as $data) {
EggPrice::firstOrCreate(
[
'name' => $data['name'],
'unit_id' => $data['unit_id'],
],
[
'price' => $data['price'],
]
);
}
}
}