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();
}
// Create 50 random expenses over the last year
Expense::factory(50)->create();
// Create 30 random expenses over the last 6 months
Expense::factory(30)->create([
'created_at' => now()->subMonths(rand(0, 5))->subDays(rand(0, 28)),
]);
// Add some specific common expenses
$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();
foreach ($commonExpenses as $name) {

View File

@ -22,44 +22,61 @@ public function run(): void
return;
}
Order::factory(100)->create()->each(function ($order) use ($users, $products) {
$itemsCount = rand(1, 4);
$totalOrderPrice = 0;
$totalOrderHpp = 0;
// Random Historical Data (Last 1 Year)
Order::factory(50)->create()->each(fn ($order) => $this->createOrderWithItems($order, $users, $products));
for ($i = 0; $i < $itemsCount; $i++) {
$product = $products->random();
$qty = rand(1, 5);
$price = rand(100000, 300000);
$itemTotal = $qty * $price;
// Data Kemarin
Order::factory(rand(5, 15))->create([
'created_at' => now()->subDay()->setHour(rand(8, 20)),
])->each(fn ($order) => $this->createOrderWithItems($order, $users, $products));
// Assuming hpp is 70% of price for simulation
$hpp = intval($price * 0.7);
$totalOrderHpp += ($hpp * $qty);
// Data Hari Ini
Order::factory(rand(5, 15))->create([
'created_at' => now()->setHour(rand(8, 20)),
])->each(fn ($order) => $this->createOrderWithItems($order, $users, $products));
}
OrderItem::factory()->create([
'order_id' => $order->id,
'user_id' => $users->random()->id,
'product_id' => $product->id,
'qty' => $qty,
'price' => $price,
'total' => $itemTotal,
'created_at' => $order->created_at,
]);
/**
* Helper method to create items for an order and update order totals.
*/
private function createOrderWithItems(Order $order, $users, $products): void
{
$itemsCount = rand(1, 4);
$totalOrderPrice = 0;
$totalOrderHpp = 0;
$product->decrement('stock', $qty);
$totalOrderPrice += $itemTotal;
}
for ($i = 0; $i < $itemsCount; $i++) {
$product = $products->random();
$qty = rand(1, 5);
$price = rand(100000, 300000);
$itemTotal = $qty * $price;
$discount = rand(0, 1) ? rand(5000, 20000) : 0;
$finalTotal = max(0, $totalOrderPrice - $discount);
// Assuming hpp is 70% of price for simulation
$hpp = intval($price * 0.7);
$totalOrderHpp += ($hpp * $qty);
$order->update([
'hpp' => $totalOrderHpp,
'discount' => $discount,
'total' => $finalTotal,
'payment' => $finalTotal, // Assume fully paid
OrderItem::factory()->create([
'order_id' => $order->id,
'user_id' => $users->random()->id,
'product_id' => $product->id,
'qty' => $qty,
'price' => $price,
'total' => $itemTotal,
'created_at' => $order->created_at,
]);
});
$product->decrement('stock', $qty);
$totalOrderPrice += $itemTotal;
}
$discount = rand(0, 1) ? rand(5000, 20000) : 0;
$finalTotal = max(0, $totalOrderPrice - $discount);
$order->update([
'hpp' => $totalOrderHpp,
'discount' => $discount,
'total' => $finalTotal,
'payment' => $finalTotal, // Assume fully paid
]);
}
}

View File

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

View File

@ -22,32 +22,42 @@ public function run(): void
return;
}
Purchase::factory(50)->create()->each(function ($purchase) use ($users, $products) {
$itemsCount = rand(1, 5);
$total = 0;
// Generate purchases for the last 6 months, twice a month
for ($i = 0; $i < 6; $i++) {
$monthDate = now()->subMonths($i);
for ($i = 0; $i < $itemsCount; $i++) {
$product = $products->random();
$quantity = rand(1, 10);
$unitPrice = rand(10000, 500000);
$totalPrice = $quantity * $unitPrice;
PurchaseItem::create([
'purchase_id' => $purchase->id,
'user_id' => $users->random()->id,
'product_id' => $product->id,
'quantity' => $quantity,
'unit_price' => $unitPrice,
'total_price' => $totalPrice,
'created_at' => $purchase->created_at,
// 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)),
]);
$product->increment('stock', $quantity);
$itemsCount = rand(1, 5);
$total = 0;
$total += $totalPrice;
for ($k = 0; $k < $itemsCount; $k++) {
$product = $products->random();
$quantity = rand(1, 10);
$unitPrice = rand(10000, 500000);
$totalPrice = $quantity * $unitPrice;
PurchaseItem::create([
'purchase_id' => $purchase->id,
'user_id' => $users->random()->id,
'product_id' => $product->id,
'quantity' => $quantity,
'unit_price' => $unitPrice,
'total_price' => $totalPrice,
'created_at' => $purchase->created_at,
]);
$product->increment('stock', $quantity);
$total += $totalPrice;
}
$purchase->update(['total' => $total]);
}
$purchase->update(['total' => $total]);
});
}
}
}