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:
parent
53cdb219d5
commit
9e973948d2
@ -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) {
|
||||||
|
|||||||
@ -22,44 +22,61 @@ public function run(): void
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Order::factory(100)->create()->each(function ($order) use ($users, $products) {
|
// Random Historical Data (Last 1 Year)
|
||||||
$itemsCount = rand(1, 4);
|
Order::factory(50)->create()->each(fn ($order) => $this->createOrderWithItems($order, $users, $products));
|
||||||
$totalOrderPrice = 0;
|
|
||||||
$totalOrderHpp = 0;
|
|
||||||
|
|
||||||
for ($i = 0; $i < $itemsCount; $i++) {
|
// Data Kemarin
|
||||||
$product = $products->random();
|
Order::factory(rand(5, 15))->create([
|
||||||
$qty = rand(1, 5);
|
'created_at' => now()->subDay()->setHour(rand(8, 20)),
|
||||||
$price = rand(100000, 300000);
|
])->each(fn ($order) => $this->createOrderWithItems($order, $users, $products));
|
||||||
$itemTotal = $qty * $price;
|
|
||||||
|
|
||||||
// Assuming hpp is 70% of price for simulation
|
// Data Hari Ini
|
||||||
$hpp = intval($price * 0.7);
|
Order::factory(rand(5, 15))->create([
|
||||||
$totalOrderHpp += ($hpp * $qty);
|
'created_at' => now()->setHour(rand(8, 20)),
|
||||||
|
])->each(fn ($order) => $this->createOrderWithItems($order, $users, $products));
|
||||||
|
}
|
||||||
|
|
||||||
OrderItem::factory()->create([
|
/**
|
||||||
'order_id' => $order->id,
|
* Helper method to create items for an order and update order totals.
|
||||||
'user_id' => $users->random()->id,
|
*/
|
||||||
'product_id' => $product->id,
|
private function createOrderWithItems(Order $order, $users, $products): void
|
||||||
'qty' => $qty,
|
{
|
||||||
'price' => $price,
|
$itemsCount = rand(1, 4);
|
||||||
'total' => $itemTotal,
|
$totalOrderPrice = 0;
|
||||||
'created_at' => $order->created_at,
|
$totalOrderHpp = 0;
|
||||||
]);
|
|
||||||
|
|
||||||
$product->decrement('stock', $qty);
|
for ($i = 0; $i < $itemsCount; $i++) {
|
||||||
$totalOrderPrice += $itemTotal;
|
$product = $products->random();
|
||||||
}
|
$qty = rand(1, 5);
|
||||||
|
$price = rand(100000, 300000);
|
||||||
|
$itemTotal = $qty * $price;
|
||||||
|
|
||||||
$discount = rand(0, 1) ? rand(5000, 20000) : 0;
|
// Assuming hpp is 70% of price for simulation
|
||||||
$finalTotal = max(0, $totalOrderPrice - $discount);
|
$hpp = intval($price * 0.7);
|
||||||
|
$totalOrderHpp += ($hpp * $qty);
|
||||||
|
|
||||||
$order->update([
|
OrderItem::factory()->create([
|
||||||
'hpp' => $totalOrderHpp,
|
'order_id' => $order->id,
|
||||||
'discount' => $discount,
|
'user_id' => $users->random()->id,
|
||||||
'total' => $finalTotal,
|
'product_id' => $product->id,
|
||||||
'payment' => $finalTotal, // Assume fully paid
|
'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
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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([
|
||||||
|
|||||||
@ -22,32 +22,42 @@ 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
|
||||||
$itemsCount = rand(1, 5);
|
for ($i = 0; $i < 6; $i++) {
|
||||||
$total = 0;
|
$monthDate = now()->subMonths($i);
|
||||||
|
|
||||||
for ($i = 0; $i < $itemsCount; $i++) {
|
// Two purchases per month
|
||||||
$product = $products->random();
|
for ($j = 0; $j < 2; $j++) {
|
||||||
$quantity = rand(1, 10);
|
$purchase = Purchase::factory()->create([
|
||||||
$unitPrice = rand(10000, 500000);
|
'created_at' => $monthDate->copy()->startOfMonth()->addDays(rand(0, 28))->setTime(rand(8, 17), rand(0, 59)),
|
||||||
$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);
|
$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]);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user