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

View File

@ -10,12 +10,10 @@
use App\Filament\Actions\DefaultBulkActions; use App\Filament\Actions\DefaultBulkActions;
use App\Filament\Columns\TimestampColumns; use App\Filament\Columns\TimestampColumns;
use App\Filament\Resources\Manage\Orders\Pages\ManageOrders; use App\Filament\Resources\Manage\Orders\Pages\ManageOrders;
use App\Models\EggPrice;
use App\Models\Order; use App\Models\Order;
use BackedEnum; use BackedEnum;
use Filament\Actions\BulkActionGroup; use Filament\Actions\BulkActionGroup;
use Filament\Forms\Components\DatePicker; use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\Select; use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea; use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput; use Filament\Forms\Components\TextInput;
@ -59,132 +57,90 @@ public static function form(Schema $schema): Schema
$set('total_amount', number_format($totalAmount, 0, ',', '.')); $set('total_amount', number_format($totalAmount, 0, ',', '.'));
}; };
return $schema->components([ return $schema
Hidden::make('unit_id') ->components([
->required(), DatePicker::make('order_date')
->label('Tanggal Order')
->default(now())
->required()
->native(false)
->displayFormat('l, d F Y'),
Grid::make(2) Select::make('customer_id')
->schema([ ->label('Pelanggan')
Select::make('egg_price_id') ->relationship('customer', 'name')
->label('Harga Telur') ->searchable()
->options( ->preload()
EggPrice::query() ->native(false),
->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();
if ($eggPrice) { Grid::make(3)
$set('egg_price_id', $eggPrice->id); ->schema([
} TextInput::make('egg_quantity')
} ->label('Jumlah Telur')
}) ->placeholder('0')
->afterStateUpdated(function ($state, callable $set, callable $get) use ($recalculateTotal): void { ->autocomplete(false)
if (! $state) { ->helperText('Jumlah per satuan.')
$set('unit_price', 0); ->required()
$set('unit_id', null); ->live()
->afterStateUpdated(function ($state, callable $get, callable $set) use ($recalculateTotal): void {
$recalculateTotal($get, $set); $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, ',', '.')); TextInput::make('discount')
$set('unit_id', $price?->unit_id); ->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); TextInput::make('total_amount')
}) ->label('Total')
->dehydrated(false) ->placeholder('0')
->required(), ->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') Textarea::make('notes')
->label('Pelanggan') ->label('Catatan')
->relationship('customer', 'name') ->placeholder('...')
->searchable() ->autocomplete(false)
->preload() ->columnSpanFull(),
->native(false), ])
->columns(2);
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);
} }
public static function table(Table $table): Table public static function table(Table $table): Table
@ -223,6 +179,12 @@ public static function table(Table $table): Table
->money('IDR', decimalPlaces: 0) ->money('IDR', decimalPlaces: 0)
->sortable(), ->sortable(),
TextColumn::make('notes')
->label('Catatan')
->sortable()
->searchable()
->toggleable(isToggledHiddenByDefault: true),
...TimestampColumns::make(), ...TimestampColumns::make(),
]) ])
->filters([ ->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; namespace Database\Factories;
use App\Models\Customer; use App\Models\Customer;
use App\Models\EggPrice;
use App\Models\Order; use App\Models\Order;
use App\Models\Unit;
use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Factories\Factory;
/** /**
@ -23,19 +23,19 @@ public function definition(): array
{ {
$date = $this->faker->dateTimeBetween('-3 months', 'now'); $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(); $customer = Customer::inRandomOrder()->first() ?? Customer::factory()->create();
$eggQuantity = $this->faker->numberBetween(10, 500); $eggQuantity = $this->faker->numberBetween(10, 500);
$unitPrice = $eggPrice->price; $unitPrice = $eggPrice / 10;
$discount = $this->faker->optional(0.3)->numberBetween(5000, 50000) ?? 0; $discount = $this->faker->optional(0.3)->numberBetween(5000, 50000) ?? 0;
$subtotal = $eggQuantity * $unitPrice; $subtotal = $eggQuantity * $unitPrice;
$totalAmount = max(0, $subtotal - $discount); $totalAmount = max(0, $subtotal - $discount);
return [ return [
'customer_id' => $customer->id, 'customer_id' => $customer->id,
'unit_id' => $eggPrice->unit_id, 'unit_id' => Unit::inRandomOrder()->first()->id ?? Unit::factory()->create()->id,
'egg_quantity' => $eggQuantity, 'egg_quantity' => $eggQuantity,
'unit_price' => $unitPrice, 'unit_price' => $unitPrice,
'order_date' => $date, '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, FeedPurchaseSeeder::class,
EggCollectionSeeder::class, EggCollectionSeeder::class,
CustomerSeeder::class, CustomerSeeder::class,
EggPriceSeeder::class,
OrderSeeder::class, OrderSeeder::class,
DelayedGoodSeeder::class, DelayedGoodSeeder::class,
ExpenseSeeder::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'],
]
);
}
}
}