parfum/database/seeders/VoucherSeeder.php

143 lines
5.3 KiB
PHP

<?php
namespace Database\Seeders;
use App\Enums\VoucherType;
use App\Models\Outlet;
use App\Models\Tier;
use App\Models\Voucher;
use Illuminate\Database\Seeder;
class VoucherSeeder extends Seeder
{
public function run(): void
{
$tiers = Tier::all();
$tierIds = $tiers->pluck('id')->toArray();
$vouchers = [
[
'name' => 'Diskon 10%',
'code' => 'DISC10',
'type' => VoucherType::PERCENTAGE,
'tags' => 'DISC 10%',
'tier_ids' => [], // Voucher umum untuk semua tier
'summary' => 'Diskon 10% untuk pembelian minimal Rp50.000',
'discount_amount' => 10,
'min_purchase' => 50000,
'max_discount' => 20000,
'quota' => 100,
'available_count' => 100,
'limit_per_user' => 2,
'start_date' => now()->subDays(5),
'end_date' => now()->addMonth(),
],
[
'name' => 'Diskon 20%',
'code' => 'DISC20',
'type' => VoucherType::PERCENTAGE,
'tags' => 'DISC 20%',
'tier_ids' => isset($tierIds[0]) ? [$tierIds[0]] : [], // Voucher khusus tier pertama
'summary' => 'Diskon 20% untuk pembelian minimal Rp100.000',
'discount_amount' => 20,
'min_purchase' => 100000,
'max_discount' => 50000,
'quota' => 50,
'available_count' => 50,
'limit_per_user' => 1,
'start_date' => now(),
'end_date' => now()->addDays(30),
],
[
'name' => 'Potongan Rp25K',
'code' => 'SAVE25K',
'type' => VoucherType::FIXED,
'tags' => 'SAVE 25K',
'tier_ids' => [], // Voucher umum
'summary' => 'Potongan Rp25K untuk pembelian minimal Rp100.000',
'discount_amount' => 25000,
'min_purchase' => 100000,
'max_discount' => null,
'quota' => 200,
'available_count' => 200,
'limit_per_user' => 3,
'start_date' => now(),
'end_date' => now()->addDays(15),
],
[
'name' => 'Potongan Rp50K',
'code' => 'SAVE50K',
'type' => VoucherType::FIXED,
'tags' => 'SAVE 50K',
'tier_ids' => isset($tierIds[1]) ? [$tierIds[1]] : [], // Voucher khusus tier kedua
'summary' => 'Potongan Rp50K untuk pembelian minimal Rp200.000',
'discount_amount' => 50000,
'min_purchase' => 200000,
'max_discount' => null,
'quota' => 80,
'available_count' => 80,
'limit_per_user' => 1,
'start_date' => now()->addDays(3),
'end_date' => now()->addDays(60),
],
[
'name' => 'Gratis Ongkir',
'code' => 'ONGKIR',
'type' => VoucherType::FIXED,
'tags' => 'GRATIS ONGKIR',
'tier_ids' => [], // Voucher umum
'summary' => 'Gratis Ongkir untuk pembelian minimal Rp50.000',
'discount_amount' => 15000,
'min_purchase' => 50000,
'max_discount' => 15000,
'quota' => 500,
'available_count' => 500,
'limit_per_user' => 5,
'start_date' => now(),
'end_date' => now()->addDays(90),
],
[
'name' => 'Flash Sale 30%',
'code' => 'FLASH30',
'type' => VoucherType::PERCENTAGE,
'tags' => 'FLASH 30%',
'tier_ids' => isset($tierIds[2]) ? [$tierIds[2]] : [], // Voucher khusus tier ketiga
'summary' => 'Diskon 30% untuk pembelian minimal Rp75.000',
'discount_amount' => 30,
'min_purchase' => 75000,
'max_discount' => 40000,
'quota' => 30,
'available_count' => 30,
'limit_per_user' => 1,
'start_date' => now()->subDay(),
'end_date' => now()->addDay(),
],
[
'name' => 'Spesial Member',
'code' => 'MEMBER15',
'type' => VoucherType::PERCENTAGE,
'tags' => 'Serial Member',
'tier_ids' => [], // Voucher umum untuk semua member
'summary' => 'Diskon 15% untuk pembelian minimal Rp50.000',
'discount_amount' => 15,
'min_purchase' => 50000,
'max_discount' => 25000,
'quota' => null,
'limit_per_user' => 1,
'start_date' => now(),
'end_date' => null,
],
];
foreach ($vouchers as $voucher) {
$tierIdsToAttach = $voucher['tier_ids'];
unset($voucher['tier_ids']);
$newVoucher = Voucher::create($voucher);
$newVoucher->tiers()->attach($tierIdsToAttach);
$newVoucher->outlets()->attach(Outlet::inRandomOrder()->first()->id);
}
}
}