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; namespace Database\Factories;
use App\Models\Expense; use App\Models\Expense;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Factories\Factory;
/** /**
@ -18,8 +19,10 @@ class ExpenseFactory extends Factory
public function definition(): array public function definition(): array
{ {
return [ return [
'name' => $this->faker->sentence(), 'user_id' => User::inRandomOrder()->first()?->id ?? User::factory(),
'name' => $this->faker->sentence(3),
'amount' => $this->faker->numberBetween(10000, 1000000), '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 public function definition(): array
{ {
$date = $this->faker->dateTimeBetween('-1 year', 'now');
return [ return [
'user_id' => User::inRandomOrder()->first()?->id ?? User::factory(), '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(), 'customer_name' => $this->faker->name(),
'hpp' => 0, 'hpp' => 0,
'discount' => 0, 'discount' => 0,
@ -34,6 +36,7 @@ public function definition(): array
'payment_method' => $this->faker->randomElement(PaymentMethod::cases()), 'payment_method' => $this->faker->randomElement(PaymentMethod::cases()),
'order_status' => $this->faker->randomElement(OrderStatus::cases()), 'order_status' => $this->faker->randomElement(OrderStatus::cases()),
'order_channel' => $this->faker->randomElement(OrderChannel::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 public function definition(): array
{ {
$date = $this->faker->dateTimeBetween('-1 year', 'now');
return [ 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 'total' => 0, // Calculated later in seeder
'note' => $this->faker->optional()->sentence(), 'note' => $this->faker->optional()->sentence(),
'created_at' => $date,
]; ];
} }
} }

View File

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

View File

@ -13,16 +13,30 @@ class ExpenseSeeder extends Seeder
*/ */
public function run(): void public function run(): void
{ {
$user = User::first(); $users = User::all();
$expenses = [ if ($users->isEmpty()) {
['name' => 'Listrik Toko', 'amount' => 120000, 'user_id' => $user->id], User::factory(5)->create();
['name' => 'Wifi Bulanan', 'amount' => 300000, 'user_id' => $user->id], $users = User::all();
['name' => 'Sampah Bulanan', 'amount' => 100000, 'user_id' => $user->id], }
];
foreach ($expenses as $expense) { // Create 50 random expenses over the last year
Expense::create($expense); 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; 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); $itemsCount = rand(1, 4);
$totalOrderPrice = 0; $totalOrderPrice = 0;
$totalOrderHpp = 0; $totalOrderHpp = 0;
@ -44,6 +44,7 @@ public function run(): void
'qty' => $qty, 'qty' => $qty,
'price' => $price, 'price' => $price,
'total' => $itemTotal, 'total' => $itemTotal,
'created_at' => $order->created_at,
]); ]);
$product->decrement('stock', $qty); $product->decrement('stock', $qty);

View File

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

View File

@ -22,7 +22,7 @@ public function run(): void
return; 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); $itemsCount = rand(1, 5);
$total = 0; $total = 0;
@ -39,6 +39,7 @@ public function run(): void
'quantity' => $quantity, 'quantity' => $quantity,
'unit_price' => $unitPrice, 'unit_price' => $unitPrice,
'total_price' => $totalPrice, 'total_price' => $totalPrice,
'created_at' => $purchase->created_at,
]); ]);
$product->increment('stock', $quantity); $product->increment('stock', $quantity);

View File

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