93 lines
2.8 KiB
PHP
93 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Models\Item;
|
|
use App\Models\Kategori;
|
|
use App\Models\Nampan;
|
|
use App\Models\Produk;
|
|
use App\Models\Sales;
|
|
use App\Models\Transaksi;
|
|
use App\Models\User;
|
|
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
|
use Illuminate\Database\Seeder;
|
|
|
|
class DatabaseSeeder extends Seeder
|
|
{
|
|
/**
|
|
* Seed the application's database.
|
|
*/
|
|
public function run(): void
|
|
{
|
|
User::factory()->create([
|
|
'nama' => 'Test User',
|
|
'role' => 'owner',
|
|
'password' => bcrypt('123123123'),
|
|
]);
|
|
|
|
User::factory(2)->create();
|
|
Sales::factory(5)->create();
|
|
|
|
$kodeNampan = ['A', 'B'];
|
|
foreach ($kodeNampan as $kode) {
|
|
for ($i=0; $i < 4; $i++) {
|
|
Nampan::factory()->create([
|
|
'nama' => $kode . ($i + 1) // A1, A2, ... B4
|
|
]);
|
|
}
|
|
}
|
|
|
|
$kategoriList = ['cincin', 'gelang rantai', 'gelang bulat', 'kalung', 'liontin', 'anting', 'giwang'];
|
|
foreach ($kategoriList as $kategori) {
|
|
Kategori::factory()->create([
|
|
'nama' => $kategori
|
|
]);
|
|
}
|
|
|
|
Produk::factory(10)->create()->each(function ($produk) {
|
|
// setiap produk punya 1-3 foto
|
|
$jumlah_foto = rand(1, 3);
|
|
$fotoData = [];
|
|
for ($i = 0; $i < $jumlah_foto; $i++) {
|
|
$fotoData[] = [
|
|
'url' => 'https://random-image-pepebigotes.vercel.app/api/random-image'
|
|
];
|
|
}
|
|
$produk->foto()->createMany($fotoData);
|
|
|
|
$jumlah_item = rand(1, 20);
|
|
Item::factory($jumlah_item)->create([
|
|
'id_produk' => $produk->id,
|
|
'is_sold' => false,
|
|
]);
|
|
});
|
|
|
|
// 30% peluang item masuk nampan, sisanya di brankas
|
|
$nampans = Nampan::all()->pluck('id')->toArray();
|
|
$jumlahNampan = count($nampans);
|
|
$counter = 0;
|
|
|
|
foreach (Item::all() as $item) {
|
|
if (rand(1, 100) <= 30) {
|
|
$item->update([
|
|
'id_nampan' => $nampans[$counter % $jumlahNampan],
|
|
]);
|
|
$counter++;
|
|
}
|
|
}
|
|
|
|
Transaksi::factory(20)->create()->each(function ($transaksi) {
|
|
$jumlah_item = rand(1, 5);
|
|
$items = Item::where('is_sold', false)->inRandomOrder()->limit($jumlah_item)->get();
|
|
if ($items->isEmpty()) return;
|
|
foreach ($items as $item) {
|
|
$transaksi->itemTransaksi()->create([
|
|
'id_item' => $item->id,
|
|
'harga_deal' => $item->produk->harga_jual,
|
|
]);
|
|
$item->update(['is_sold' => true]);
|
|
}
|
|
});
|
|
}
|
|
}
|