refactor: Update EggCollectionResource to improve currency handling, enhance item display logic, and adjust database migration for decimal values in egg totals

This commit is contained in:
Yoga Pangestu 2026-03-07 10:59:47 +07:00
parent 547abb758e
commit c6e7119b94
2 changed files with 27 additions and 25 deletions

View File

@ -58,7 +58,7 @@ public static function form(Schema $schema): Schema
->required()
->searchable()
->preload()
->default(fn () => Warehouse::first()?->id),
->default(fn() => Warehouse::first()?->id),
Repeater::make('items')
->label('Rincian Produksi')
@ -76,8 +76,21 @@ public static function form(Schema $schema): Schema
->placeholder('0')
->autocomplete(false)
->required()
->currencyMask(thousandSeparator: '.', decimalSeparator: ',', precision: 1)
->dehydrateStateUsing(fn ($state) => (float) str()->replace(',', '.', str()->replace('.', '', (string) ($state ?? 0)))),
->currencyMask(thousandSeparator: '.', decimalSeparator: ',', precision: 2)
->formatStateUsing(fn($state) => $state === null ? null : (float) $state)
->dehydrateStateUsing(function ($state) {
if (blank($state)) {
return 0;
}
if (is_numeric($state)) {
return (float) $state;
}
return (float) str($state)
->replace('.', '')
->replace(',', '.')
->toString();
}),
Toggle::make('is_broken')
->label('Pecah?')
@ -115,27 +128,15 @@ public static function table(Table $table): Table
->label('Hasil Produksi')
->getStateUsing(function (EggCollection $record) {
return $record->items()
->where('is_broken', false)
->orderBy('is_broken')
->with('unit')
->get()
->map(fn ($item) => (float) $item->quantity." {$item->unit?->alias}")
->join(', ') ?: '-';
->map(fn($item) => str_replace('.', ',', (float) $item->quantity) . " {$item->unit?->alias}" . ($item->is_broken ? ' (Pecah)' : ''))
->toArray();
})
->badge()
->color('success'),
TextColumn::make('broken_summary')
->label('Telur Pecah')
->getStateUsing(function (EggCollection $record) {
return $record->items()
->where('is_broken', true)
->with('unit')
->get()
->map(fn ($item) => (float) $item->quantity." {$item->unit?->alias}")
->join(', ') ?: '-';
})
->badge()
->color('danger'),
->listWithLineBreaks()
->color(fn($state) => str_contains($state, '(Pecah)') ? 'danger' : 'success')
->placeholder('-'),
TextColumn::make('notes')
->label('Catatan')
@ -147,13 +148,13 @@ public static function table(Table $table): Table
->filters([
TrashedFilter::make()
->native(false)
->visible(fn (): bool => auth()->user()->hasRole(RoleEnum::DEVELOPER)),
->visible(fn(): bool => auth()->user()->hasRole(RoleEnum::DEVELOPER)),
])
->recordActions([
EditAction::make()
->label('Ubah')
->modalWidth(Width::FourExtraLarge)
->modalHeading(fn (EggCollection $record): string => 'Ubah '.$record->production_date->translatedFormat('l, d F Y H:i')),
->modalHeading(fn(EggCollection $record): string => 'Ubah ' . $record->production_date->translatedFormat('l, d F Y H:i')),
DeleteAction::make(),

View File

@ -11,8 +11,9 @@ public function up(): void
Schema::create('egg_collections', function (Blueprint $table) {
$table->id();
$table->dateTime('production_date');
$table->unsignedInteger('total_eggs')->default(0);
$table->unsignedInteger('total_broken_eggs')->default(0);
$table->foreignId('warehouse_id')->nullable()->constrained()->nullOnDelete();
$table->decimal('total_eggs', 12, 2)->default(0);
$table->decimal('total_broken_eggs', 12, 2)->default(0);
$table->text('notes')->nullable();
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->nullable()->useCurrentOnUpdate();