refactor: Update order quantity, total amount, and discount fields to decimal types and integrate discount into the initial orders table migration.
This commit is contained in:
parent
22ef713f92
commit
f8c79ce688
@ -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')
|
||||
|
||||
@ -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',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -1,28 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('orders', function (Blueprint $table) {
|
||||
$table->unsignedInteger('discount')->default(0)->after('total_amount');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('orders', function (Blueprint $table) {
|
||||
$table->dropColumn('discount');
|
||||
});
|
||||
}
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user