feat: Add DelayedGood model factory and seeder, and integrate it into the main DatabaseSeeder.

This commit is contained in:
Yoga Pangestu 2026-02-19 09:52:04 +07:00
parent 556618c929
commit a1d01654d5
3 changed files with 73 additions and 0 deletions

View File

@ -0,0 +1,41 @@
<?php
namespace Database\Factories;
use App\Models\Customer;
use App\Models\Unit;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\DelayedGood>
*/
class DelayedGoodFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
$quantity = fake()->numberBetween(1, 100);
$unitPrice = fake()->numberBetween(5000, 20000);
$totalAmount = $quantity * $unitPrice;
$isPaid = fake()->boolean();
$paidAmount = $isPaid ? $totalAmount : fake()->numberBetween(0, $totalAmount);
$storedDate = fake()->dateTimeBetween('-1 month', 'now');
return [
'customer_id' => Customer::factory(),
'unit_id' => Unit::factory(),
'quantity' => $quantity,
'unit_price' => $unitPrice,
'total_amount' => $totalAmount,
'paid_amount' => $paidAmount,
'stored_date' => $storedDate,
'is_paid' => $isPaid,
'payment_date' => $isPaid ? fake()->dateTimeBetween($storedDate, 'now') : null,
'notes' => fake()->optional()->sentence(),
];
}
}

View File

@ -25,6 +25,7 @@ public function run(): void
CustomerSeeder::class,
EggPriceSeeder::class,
OrderSeeder::class,
DelayedGoodSeeder::class,
]);
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DelayedGoodSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$customers = \App\Models\Customer::all();
$units = \App\Models\Unit::all();
if ($customers->isEmpty()) {
$customers = \App\Models\Customer::factory()->count(10)->create();
}
if ($units->isEmpty()) {
$units = \App\Models\Unit::factory()->count(5)->create();
}
\App\Models\DelayedGood::factory()
->count(50)
->recycle($customers)
->recycle($units)
->create();
}
}