*/ class OrderFactory extends Factory { protected $model = Order::class; /** * Define the model's default state. * * @return array */ public function definition(): array { $date = $this->faker->dateTimeBetween('-3 months', 'now'); $unit = Unit::whereIn('alias', ['butir', 'kg'])->inRandomOrder()->first() ?? Unit::where('alias', 'butir')->first() ?? Unit::factory()->create(); $customer = Customer::inRandomOrder()->first() ?? Customer::factory()->create(); if ($unit->alias === 'butir') { $eggQuantity = $this->faker->numberBetween(30, 300); $unitPrice = $this->faker->numberBetween(1500, 2500); // Price per egg } else { $eggQuantity = $this->faker->randomFloat(2, 5, 50); $unitPrice = $this->faker->numberBetween(25000, 32000); // Price per kg } $discount = $this->faker->optional(0.3)->numberBetween(5000, 25000) ?? 0; $subtotal = $eggQuantity * $unitPrice; $totalAmount = max(0, $subtotal - $discount); return [ 'customer_id' => $customer->id, 'unit_id' => $unit->id, 'warehouse_id' => Warehouse::inRandomOrder()->first()?->id ?? Warehouse::factory(), 'egg_quantity' => $eggQuantity, 'unit_price' => $unitPrice, 'order_date' => $date, 'status' => $this->faker->randomElement(['pending', 'completed', 'cancelled']), 'discount' => $discount, 'total_amount' => $totalAmount, 'notes' => $this->faker->optional(0.4)->sentence(), ]; } }