store/database/seeders/ProductSeeder.php

124 lines
4.8 KiB
PHP

<?php
namespace Database\Seeders;
use App\Enums\PriceType;
use App\Models\Category;
use App\Models\Product;
use App\Models\ProductPrice;
use App\Models\ProductVariant;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class ProductSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$categories = Category::query()->pluck('id', 'slug');
$products = [
[
'name' => 'Daster Batik Parang',
'description' => 'Daster batik motif parang dengan bahan katun nyaman untuk pemakaian sehari-hari.',
'category_slugs' => ['daster'],
'variants' => [
['name' => 'S', 'stock' => 25, 'base_price' => 65000],
['name' => 'M', 'stock' => 40, 'base_price' => 65000],
['name' => 'L', 'stock' => 35, 'base_price' => 65000],
['name' => 'XL', 'stock' => 20, 'base_price' => 70000],
],
],
[
'name' => 'Setelan Celana Batik Modern',
'description' => 'Setelan atasan dan celana batik dengan potongan modern untuk acara formal maupun kasual.',
'category_slugs' => ['setelan-celana'],
'variants' => [
['name' => 'S', 'stock' => 15, 'base_price' => 185000],
['name' => 'M', 'stock' => 20, 'base_price' => 185000],
['name' => 'L', 'stock' => 18, 'base_price' => 185000],
['name' => 'XL', 'stock' => 12, 'base_price' => 195000],
],
],
[
'name' => 'Blouse Batik Lengan Panjang',
'description' => 'Blouse batik lengan panjang dengan detail kerah mandarin.',
'category_slugs' => ['atasan'],
'variants' => [
['name' => 'S', 'stock' => 30, 'base_price' => 95000],
['name' => 'M', 'stock' => 35, 'base_price' => 95000],
['name' => 'L', 'stock' => 28, 'base_price' => 95000],
],
],
[
'name' => 'Rok Lilit Batik',
'description' => 'Rok lilit batik dengan motif klasik, mudah disesuaikan dengan berbagai ukuran.',
'category_slugs' => ['bawahan'],
'variants' => [
['name' => 'All Size', 'stock' => 50, 'base_price' => 85000],
],
],
[
'name' => 'Daster Busui Nursing',
'description' => 'Daster busui dengan akses bukaan menyusui praktis dan bahan adem.',
'category_slugs' => ['busui', 'daster'],
'variants' => [
['name' => 'M', 'stock' => 22, 'base_price' => 78000],
['name' => 'L', 'stock' => 26, 'base_price' => 78000],
['name' => 'XL', 'stock' => 18, 'base_price' => 82000],
],
],
];
DB::transaction(function () use ($products, $categories): void {
foreach ($products as $productData) {
$product = Product::factory()->create([
'name' => $productData['name'],
'slug' => str()->slug($productData['name']),
'description' => $productData['description'],
'is_active' => true,
]);
$product->categories()->sync(
collect($productData['category_slugs'])
->map(fn (string $slug) => $categories[$slug])
->all()
);
foreach ($productData['variants'] as $variantData) {
$variant = ProductVariant::factory()->create([
'product_id' => $product->id,
'name' => $variantData['name'],
'stock' => $variantData['stock'],
]);
$this->seedVariantPrices($variant, $variantData['base_price']);
}
}
});
}
private function seedVariantPrices(ProductVariant $variant, int $basePrice): void
{
$multipliers = [
PriceType::DISTRIBUTOR->value => 1.00,
PriceType::AGENT->value => 1.05,
PriceType::SUB_AGENT->value => 1.10,
PriceType::GROSIR->value => 1.15,
PriceType::ECER->value => 1.35,
PriceType::TIKTOK->value => 1.40,
PriceType::SHOPEE->value => 1.40,
];
foreach ($multipliers as $type => $multiplier) {
ProductPrice::factory()->create([
'variant_id' => $variant->id,
'type' => $type,
'price' => (int) round($basePrice * $multiplier),
]);
}
}
}