From f8c79ce6881caf9fc8d0fe79379a2ccbe181ddeb Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Thu, 26 Feb 2026 13:52:21 +0700 Subject: [PATCH] refactor: Update order quantity, total amount, and discount fields to decimal types and integrate discount into the initial orders table migration. --- .../Resources/Manage/Orders/OrderResource.php | 25 +++++++++++------ app/Models/Order.php | 6 ++-- database/factories/OrderFactory.php | 2 +- .../2026_02_18_000004_create_orders_table.php | 5 ++-- ...19_020225_add_discount_to_orders_table.php | 28 ------------------- 5 files changed, 24 insertions(+), 42 deletions(-) delete mode 100644 database/migrations/2026_02_19_020225_add_discount_to_orders_table.php diff --git a/app/Filament/Resources/Manage/Orders/OrderResource.php b/app/Filament/Resources/Manage/Orders/OrderResource.php index e866c99..5110de6 100644 --- a/app/Filament/Resources/Manage/Orders/OrderResource.php +++ b/app/Filament/Resources/Manage/Orders/OrderResource.php @@ -47,9 +47,16 @@ class OrderResource extends Resource public static function form(Schema $schema): Schema { $recalculateTotal = function (callable $get, callable $set): void { - $quantity = (int) str()->replace('.', '', (string) ($get('egg_quantity') ?? 0)); - $unitPrice = (int) str()->replace('.', '', (string) ($get('unit_price') ?? 0)); - $discount = (int) str()->replace('.', '', (string) ($get('discount') ?? 0)); + $qty = $get('egg_quantity') ?? 0; + $quantity = str_contains((string) $qty, ',') + ? (float) str_replace(',', '.', str_replace('.', '', (string) $qty)) + : (float) $qty; + + $price = $get('unit_price') ?? 0; + $unitPrice = (int) str_replace('.', '', (string) $price); + + $discountRaw = $get('discount') ?? 0; + $discount = (int) str_replace('.', '', (string) $discountRaw); $subtotal = $quantity * $unitPrice; $totalAmount = max(0, $subtotal - $discount); @@ -85,8 +92,10 @@ public static function form(Schema $schema): Schema ->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))), + ->currencyMask(thousandSeparator: '.', decimalSeparator: ',', precision: 2) + ->dehydrateStateUsing(fn ($state) => str_contains((string) $state, ',') + ? (float) str_replace(',', '.', str_replace('.', '', (string) $state)) + : (float) $state), TextInput::make('unit_price') ->label('Harga per Satuan') @@ -130,9 +139,9 @@ public static function form(Schema $schema): Schema ->prefix('Rp') ->required() ->formatStateUsing( - fn ($state) => number_format((int) ($state ?? 0), 0, ',', '.') + fn ($state) => number_format((float) ($state ?? 0), 0, ',', '.') ) - ->dehydrateStateUsing(fn ($state) => (int) str()->replace('.', '', (string) ($state ?? 0))), + ->dehydrateStateUsing(fn ($state) => (float) str_replace('.', '', (string) ($state ?? 0))), Textarea::make('notes') ->label('Catatan') @@ -160,7 +169,7 @@ public static function table(Table $table): Table TextColumn::make('egg_quantity') ->label('Jumlah') ->suffix(fn ($record) => ' '.$record->unit?->alias) - ->numeric() + ->numeric(decimalPlaces: 2, decimalSeparator: ',', thousandsSeparator: '.') ->sortable(), TextColumn::make('unit_price') diff --git a/app/Models/Order.php b/app/Models/Order.php index bfb43d2..20e7c19 100644 --- a/app/Models/Order.php +++ b/app/Models/Order.php @@ -17,10 +17,10 @@ protected function casts(): array { return [ 'order_date' => 'date', - 'egg_quantity' => 'integer', + 'egg_quantity' => 'decimal:2', 'unit_price' => 'integer', - 'total_amount' => 'integer', - 'discount' => 'integer', + 'total_amount' => 'decimal:2', + 'discount' => 'decimal:2', ]; } diff --git a/database/factories/OrderFactory.php b/database/factories/OrderFactory.php index b7ebb70..da5cfd2 100644 --- a/database/factories/OrderFactory.php +++ b/database/factories/OrderFactory.php @@ -27,7 +27,7 @@ public function definition(): array $customer = Customer::inRandomOrder()->first() ?? Customer::factory()->create(); - $eggQuantity = $this->faker->numberBetween(10, 500); + $eggQuantity = $this->faker->randomFloat(2, 10, 500); $unitPrice = $eggPrice / 10; $discount = $this->faker->optional(0.3)->numberBetween(5000, 50000) ?? 0; $subtotal = $eggQuantity * $unitPrice; diff --git a/database/migrations/2026_02_18_000004_create_orders_table.php b/database/migrations/2026_02_18_000004_create_orders_table.php index 9f5545c..6f53823 100644 --- a/database/migrations/2026_02_18_000004_create_orders_table.php +++ b/database/migrations/2026_02_18_000004_create_orders_table.php @@ -12,11 +12,12 @@ public function up(): void $table->id(); $table->foreignId('customer_id')->nullable()->constrained()->cascadeOnDelete(); $table->foreignId('unit_id')->constrained()->cascadeOnDelete(); - $table->unsignedInteger('egg_quantity'); + $table->decimal('egg_quantity', 10, 2); $table->unsignedInteger('unit_price'); $table->date('order_date'); $table->string('status', 20)->default('pending'); - $table->unsignedInteger('total_amount'); + $table->decimal('total_amount', 15, 2); + $table->decimal('discount', 15, 2)->default(0); $table->text('notes')->nullable(); $table->timestamp('created_at')->useCurrent(); $table->timestamp('updated_at')->nullable()->useCurrentOnUpdate(); diff --git a/database/migrations/2026_02_19_020225_add_discount_to_orders_table.php b/database/migrations/2026_02_19_020225_add_discount_to_orders_table.php deleted file mode 100644 index 9dc010c..0000000 --- a/database/migrations/2026_02_19_020225_add_discount_to_orders_table.php +++ /dev/null @@ -1,28 +0,0 @@ -unsignedInteger('discount')->default(0)->after('total_amount'); - }); - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - Schema::table('orders', function (Blueprint $table) { - $table->dropColumn('discount'); - }); - } -};