-
Notifications
You must be signed in to change notification settings - Fork 0
/
PurchaseOrderFactory.php
50 lines (44 loc) · 1.43 KB
/
PurchaseOrderFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace Database\Factories;
use App\Models\Category;
use App\Models\PurchaseOrder;
use App\Models\PurchaseOrderItem;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<PurchaseOrder>
*/
class PurchaseOrderFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'po_number' => fake()->numberBetween('10000','1000000000'),
'buyer_name' => fake()->company,
'total' => 0,
];
}
public function configure(): static
{
return $this->afterCreating(function (PurchaseOrder $purchaseOrder) {
// Create three purchase order items with unique attributes
for ($i = 0; $i < 3; $i++) {
PurchaseOrderItem::factory()->create([
'purchase_order_id' => $purchaseOrder->id,
'description' => fake()->sentence,
'quantity' => $this->faker->randomNumber(1, 100),
'unit_price' => $this->faker->randomFloat('2',1,1000),
]);
}
// Calculate total and update the purchase order
$total = $purchaseOrder->items->sum(function ($item) {
return $item->quantity * $item->unit_price;
});
$purchaseOrder->update(['total' => $total]);
});
}
}