feat: update factory and seeder implementations to include user associations and created_at timestamps for Expense, Order, Purchase, and PurchaseItem models

This commit is contained in:
Yoga Pangestu 2026-04-22 22:44:56 +07:00
parent cf4e2f5d10
commit b04ce914ad
9 changed files with 61 additions and 30 deletions

View File

@ -3,6 +3,7 @@
namespace Database\Factories;
use App\Models\Expense;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
@ -18,8 +19,10 @@ class ExpenseFactory extends Factory
public function definition(): array
{
return [
'name' => $this->faker->sentence(),
'user_id' => User::inRandomOrder()->first()?->id ?? User::factory(),
'name' => $this->faker->sentence(3),
'amount' => $this->faker->numberBetween(10000, 1000000),
'created_at' => $this->faker->dateTimeBetween('-1 year', 'now'),
];
}
}

View File

@ -23,9 +23,11 @@ class OrderFactory extends Factory
*/
public function definition(): array
{
$date = $this->faker->dateTimeBetween('-1 year', 'now');
return [
'user_id' => User::inRandomOrder()->first()?->id ?? User::factory(),
'invoice_number' => 'INV-'.now()->format('YmdHis').'-'.strtoupper($this->faker->bothify('??##')),
'invoice_number' => 'INV-'.$date->format('YmdHis').'-'.strtoupper($this->faker->bothify('??##')),
'customer_name' => $this->faker->name(),
'hpp' => 0,
'discount' => 0,
@ -34,6 +36,7 @@ public function definition(): array
'payment_method' => $this->faker->randomElement(PaymentMethod::cases()),
'order_status' => $this->faker->randomElement(OrderStatus::cases()),
'order_channel' => $this->faker->randomElement(OrderChannel::cases()),
'created_at' => $date,
];
}
}

View File

@ -17,10 +17,13 @@ class PurchaseFactory extends Factory
*/
public function definition(): array
{
$date = $this->faker->dateTimeBetween('-1 year', 'now');
return [
'purchase_date' => $this->faker->dateTimeBetween('-1 month', 'now')->format('Y-m-d'),
'purchase_date' => $date->format('Y-m-d'),
'total' => 0, // Calculated later in seeder
'note' => $this->faker->optional()->sentence(),
'created_at' => $date,
];
}
}

View File

@ -25,7 +25,7 @@ public function definition(): array
return [
'purchase_id' => Purchase::factory(),
'user_id' => User::factory(),
'user_id' => User::inRandomOrder()->first()?->id ?? User::factory(),
'product_id' => Product::factory(),
'quantity' => $quantity,
'unit_price' => $unitPrice,

View File

@ -13,16 +13,30 @@ class ExpenseSeeder extends Seeder
*/
public function run(): void
{
$user = User::first();
$users = User::all();
$expenses = [
['name' => 'Listrik Toko', 'amount' => 120000, 'user_id' => $user->id],
['name' => 'Wifi Bulanan', 'amount' => 300000, 'user_id' => $user->id],
['name' => 'Sampah Bulanan', 'amount' => 100000, 'user_id' => $user->id],
];
if ($users->isEmpty()) {
User::factory(5)->create();
$users = User::all();
}
foreach ($expenses as $expense) {
Expense::create($expense);
// Create 50 random expenses over the last year
Expense::factory(50)->create();
// Add some specific common expenses
$commonExpenses = ['Listrik Toko', 'Wifi Bulanan', 'Sampah Bulanan', 'Air'];
foreach (range(1, 12) as $month) {
$date = now()->subMonths($month)->startOfMonth();
foreach ($commonExpenses as $name) {
Expense::create([
'user_id' => $users->random()->id,
'name' => $name,
'amount' => rand(50000, 500000),
'created_at' => $date->copy()->addDays(rand(0, 28)),
]);
}
}
}
}

View File

@ -22,7 +22,7 @@ public function run(): void
return;
}
Order::factory(20)->create()->each(function ($order) use ($users, $products) {
Order::factory(100)->create()->each(function ($order) use ($users, $products) {
$itemsCount = rand(1, 4);
$totalOrderPrice = 0;
$totalOrderHpp = 0;
@ -44,6 +44,7 @@ public function run(): void
'qty' => $qty,
'price' => $price,
'total' => $itemTotal,
'created_at' => $order->created_at,
]);
$product->decrement('stock', $qty);

View File

@ -22,8 +22,8 @@ public function run(): void
$users = User::factory(5)->create();
}
// Generate payroll for the last 6 months
$months = collect(range(0, 5))->map(function ($i) {
// Generate payroll for the last 12 months
$months = collect(range(0, 11))->map(function ($i) {
return Carbon::now()->subMonths($i)->format('Y-m');
});

View File

@ -22,7 +22,7 @@ public function run(): void
return;
}
Purchase::factory(10)->create()->each(function ($purchase) use ($users, $products) {
Purchase::factory(50)->create()->each(function ($purchase) use ($users, $products) {
$itemsCount = rand(1, 5);
$total = 0;
@ -39,6 +39,7 @@ public function run(): void
'quantity' => $quantity,
'unit_price' => $unitPrice,
'total_price' => $totalPrice,
'created_at' => $purchase->created_at,
]);
$product->increment('stock', $quantity);

View File

@ -13,20 +13,26 @@ class UserSeeder extends Seeder
*/
public function run(): void
{
$user = User::create([
'username' => 'pangestu',
'email' => 'info.pangestuyoga@gmail.com',
'password' => Hash::make('Minimal8@'),
]);
$user = User::updateOrCreate(
['username' => 'pangestu'],
[
'email' => 'info.pangestuyoga@gmail.com',
'password' => Hash::make('Minimal8@'),
]
);
$user->profile()->create([
'nik' => '3213051307900001',
'full_name' => 'Yoga Pangestu',
'phone_number' => '082121495806',
'address' => 'Jl. Contoh No. 123',
'birth_place' => 'Jakarta',
'birth_date' => '1990-01-01',
'base_salary' => 1500000,
]);
$user->profile()->updateOrCreate(
['nik' => '3213051307900001'],
[
'full_name' => 'Yoga Pangestu',
'phone_number' => '082121495806',
'address' => 'Jl. Contoh No. 123',
'birth_place' => 'Jakarta',
'birth_date' => '1990-01-01',
'base_salary' => 1500000,
]
);
User::factory(10)->create();
}
}