You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DiscountSeeder.php 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace Database\Seeders;
  3. use App\Models\Setting\Discount;
  4. use Illuminate\Database\Seeder;
  5. use Illuminate\Support\Facades\DB;
  6. class DiscountSeeder extends Seeder
  7. {
  8. /**
  9. * Run the database seeds.
  10. */
  11. public function run(): void
  12. {
  13. $companyId = DB::table('companies')->first()->id;
  14. $userId = DB::table('users')->first()->id;
  15. $salesDiscounts = [
  16. '4th of July Sale',
  17. 'End of Year Sale',
  18. 'Black Friday Sale',
  19. 'Cyber Monday Sale',
  20. 'Christmas Sale',
  21. ];
  22. $purchaseDiscounts = [
  23. 'Bulk Purchase Bargain',
  24. 'Early Payment Discount',
  25. 'First Time Buyer Special',
  26. 'Recurring Purchase Reward',
  27. 'Referral Program Discount',
  28. ];
  29. $shuffledDiscounts = [
  30. ...array_map(static fn($name) => ['name' => $name, 'type' => 'sales'], $salesDiscounts),
  31. ...array_map(static fn($name) => ['name' => $name, 'type' => 'purchase'], $purchaseDiscounts)
  32. ];
  33. shuffle($shuffledDiscounts);
  34. $allDiscounts = $shuffledDiscounts;
  35. foreach ($allDiscounts as $discount) {
  36. Discount::factory()->create([
  37. 'company_id' => $companyId,
  38. 'name' => $discount['name'],
  39. 'type' => $discount['type'],
  40. 'enabled' => false,
  41. 'created_by' => $userId,
  42. 'updated_by' => $userId,
  43. ]);
  44. }
  45. Discount::where('type', 'sales')
  46. ->where('company_id', $companyId)
  47. ->first()
  48. ->update(['enabled' => true]);
  49. Discount::where('type', 'purchase')
  50. ->where('company_id', $companyId)
  51. ->first()
  52. ->update(['enabled' => true]);
  53. }
  54. }