65 lines
1.7 KiB
PHP
65 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace Database\Factories;
|
|
|
|
use App\Models\Bottle;
|
|
use App\Models\Order;
|
|
use App\Models\OrderItem;
|
|
use App\Models\Perfume;
|
|
use App\Models\Product;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
|
|
|
class OrderItemFactory extends Factory
|
|
{
|
|
protected $model = OrderItem::class;
|
|
|
|
public function definition(): array
|
|
{
|
|
$unitPrice = $this->faker->numberBetween(20000, 150000);
|
|
$quantity = $this->faker->numberBetween(1, 5);
|
|
|
|
return [
|
|
'user_id' => User::factory(),
|
|
'cogs' => (int) ($unitPrice * 0.4),
|
|
'unit_price' => $unitPrice,
|
|
'quantity' => $quantity,
|
|
];
|
|
}
|
|
|
|
public function forOrder(?Order $order = null): Factory
|
|
{
|
|
return $this->state(fn () => ['order_id' => $order?->id ?? Order::factory()]);
|
|
}
|
|
|
|
public function forProduct(?Product $product = null): Factory
|
|
{
|
|
$product = $product ?? Product::factory()->create();
|
|
|
|
return $this->state(fn () => [
|
|
'orderable_type' => $product->getMorphClass(),
|
|
'orderable_id' => $product->id,
|
|
]);
|
|
}
|
|
|
|
public function forPerfume(?Perfume $perfume = null): Factory
|
|
{
|
|
$perfume = $perfume ?? Perfume::factory()->create();
|
|
|
|
return $this->state(fn () => [
|
|
'orderable_type' => $perfume->getMorphClass(),
|
|
'orderable_id' => $perfume->id,
|
|
]);
|
|
}
|
|
|
|
public function forBottle(?Bottle $bottle = null): Factory
|
|
{
|
|
$bottle = $bottle ?? Bottle::factory()->create();
|
|
|
|
return $this->state(fn () => [
|
|
'orderable_type' => $bottle->getMorphClass(),
|
|
'orderable_id' => $bottle->id,
|
|
]);
|
|
}
|
|
}
|