layer-chicken/database/factories/OrderFactory.php

59 lines
1.9 KiB
PHP

<?php
namespace Database\Factories;
use App\Models\Customer;
use App\Models\Order;
use App\Models\Unit;
use App\Models\Warehouse;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Order>
*/
class OrderFactory extends Factory
{
protected $model = Order::class;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
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(),
];
}
}