refactor: update seeders to generate data for the last 6 months and optimize expense, order, payroll, and purchase creation logic

This commit is contained in:
Yoga Pangestu 2026-04-24 19:10:26 +07:00
parent 53cdb219d5
commit 9e973948d2
4 changed files with 91 additions and 59 deletions

View File

@ -20,13 +20,15 @@ public function run(): void
$users = User::all(); $users = User::all();
} }
// Create 50 random expenses over the last year // Create 30 random expenses over the last 6 months
Expense::factory(50)->create(); Expense::factory(30)->create([
'created_at' => now()->subMonths(rand(0, 5))->subDays(rand(0, 28)),
]);
// Add some specific common expenses // Add some specific common expenses
$commonExpenses = ['Listrik Toko', 'Wifi Bulanan', 'Sampah Bulanan', 'Air']; $commonExpenses = ['Listrik Toko', 'Wifi Bulanan', 'Sampah Bulanan', 'Air'];
foreach (range(1, 12) as $month) { foreach (range(1, 6) as $month) {
$date = now()->subMonths($month)->startOfMonth(); $date = now()->subMonths($month)->startOfMonth();
foreach ($commonExpenses as $name) { foreach ($commonExpenses as $name) {

View File

@ -22,7 +22,25 @@ public function run(): void
return; return;
} }
Order::factory(100)->create()->each(function ($order) use ($users, $products) { // Random Historical Data (Last 1 Year)
Order::factory(50)->create()->each(fn ($order) => $this->createOrderWithItems($order, $users, $products));
// Data Kemarin
Order::factory(rand(5, 15))->create([
'created_at' => now()->subDay()->setHour(rand(8, 20)),
])->each(fn ($order) => $this->createOrderWithItems($order, $users, $products));
// Data Hari Ini
Order::factory(rand(5, 15))->create([
'created_at' => now()->setHour(rand(8, 20)),
])->each(fn ($order) => $this->createOrderWithItems($order, $users, $products));
}
/**
* Helper method to create items for an order and update order totals.
*/
private function createOrderWithItems(Order $order, $users, $products): void
{
$itemsCount = rand(1, 4); $itemsCount = rand(1, 4);
$totalOrderPrice = 0; $totalOrderPrice = 0;
$totalOrderHpp = 0; $totalOrderHpp = 0;
@ -60,6 +78,5 @@ public function run(): void
'total' => $finalTotal, 'total' => $finalTotal,
'payment' => $finalTotal, // Assume fully paid 'payment' => $finalTotal, // Assume fully paid
]); ]);
});
} }
} }

View File

@ -22,11 +22,13 @@ public function run(): void
$users = User::factory(5)->create(); $users = User::factory(5)->create();
} }
// Generate payroll for the last 12 months // Generate payroll for the last 6 months
$months = collect(range(0, 11))->map(function ($i) { $months = collect(range(0, 5))->map(function ($i) {
return Carbon::now()->subMonths($i)->format('Y-m'); return Carbon::now()->subMonths($i)->format('Y-m');
}); });
$currentMonth = Carbon::now()->format('Y-m');
foreach ($users as $user) { foreach ($users as $user) {
foreach ($months as $month) { foreach ($months as $month) {
if (Payroll::where('user_id', $user->id)->where('period_month', $month)->exists()) { if (Payroll::where('user_id', $user->id)->where('period_month', $month)->exists()) {
@ -36,6 +38,7 @@ public function run(): void
$payroll = Payroll::factory()->create([ $payroll = Payroll::factory()->create([
'user_id' => $user->id, 'user_id' => $user->id,
'period_month' => $month, 'period_month' => $month,
'is_paid' => $month !== $currentMonth,
]); ]);
PayrollAdjustment::factory(rand(1, 3))->create([ PayrollAdjustment::factory(rand(1, 3))->create([

View File

@ -22,11 +22,20 @@ public function run(): void
return; return;
} }
Purchase::factory(50)->create()->each(function ($purchase) use ($users, $products) { // Generate purchases for the last 6 months, twice a month
for ($i = 0; $i < 6; $i++) {
$monthDate = now()->subMonths($i);
// Two purchases per month
for ($j = 0; $j < 2; $j++) {
$purchase = Purchase::factory()->create([
'created_at' => $monthDate->copy()->startOfMonth()->addDays(rand(0, 28))->setTime(rand(8, 17), rand(0, 59)),
]);
$itemsCount = rand(1, 5); $itemsCount = rand(1, 5);
$total = 0; $total = 0;
for ($i = 0; $i < $itemsCount; $i++) { for ($k = 0; $k < $itemsCount; $k++) {
$product = $products->random(); $product = $products->random();
$quantity = rand(1, 10); $quantity = rand(1, 10);
$unitPrice = rand(10000, 500000); $unitPrice = rand(10000, 500000);
@ -48,6 +57,7 @@ public function run(): void
} }
$purchase->update(['total' => $total]); $purchase->update(['total' => $total]);
}); }
}
} }
} }