akuntansi-sia/database/seeders/PurchaseSaleSeeder.php

57 lines
1.6 KiB
PHP

<?php
namespace Database\Seeders;
use App\Models\Purchase;
use App\Models\Sale;
use App\Models\Product;
use Illuminate\Database\Seeder;
use Carbon\Carbon;
class PurchaseSaleSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$branches = ['Cabang 1', 'Cabang 2', 'Cabang 3'];
$products = Product::all();
if ($products->isEmpty()) {
$this->command->warn('Product table is empty. Please run ProductSeeder first.');
return;
}
foreach ($branches as $branch) {
// Seed Purchases
for ($i = 0; $i < 5; $i++) {
$product = $products->random();
Purchase::create([
'date' => Carbon::now()->subDays(rand(1, 10))->toDateString(),
'branch' => $branch,
'product_id' => $product->id,
'item' => $product->name,
'quantity' => rand(1, 10) . ' Pack',
'total_price' => rand(20000, 200000),
]);
}
// Seed Sales
for ($i = 0; $i < 10; $i++) {
$product = $products->random();
Sale::create([
'date' => Carbon::now()->subDays(rand(0, 5))->toDateString(),
'branch' => $branch,
'product_id' => $product->id,
'item' => $product->name,
'quantity' => rand(1, 5),
'total_price' => $product->selling_price * rand(1, 5),
]);
}
}
}
}