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.

DatabaseSeeder.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Database\Seeders;
  3. use App\Models\User;
  4. use Illuminate\Database\Seeder;
  5. class DatabaseSeeder extends Seeder
  6. {
  7. /**
  8. * Seed the application's database.
  9. */
  10. public function run(): void
  11. {
  12. $startDate = today()->startOfYear();
  13. $endDate = today();
  14. // Change Company Name to ERPSAAS after Login
  15. $firstCompanyOwner = User::factory()
  16. ->withPersonalCompany()
  17. ->create([
  18. 'name' => 'Admin',
  19. 'email' => 'admin@gmail.com',
  20. 'password' => bcrypt('password'),
  21. 'current_company_id' => 1,
  22. 'created_at' => $startDate->copy(),
  23. ]);
  24. $firstCompanyOwner->ownedCompanies->first()->update(['created_at' => $startDate->copy()]);
  25. // Function to create employees for a company (also creates companies for the employees)
  26. $createUsers = static function ($company_id, $userCount, $minPercentage, $maxPercentage) use ($endDate, $startDate) {
  27. $users = User::factory($userCount)
  28. ->withPersonalCompany()
  29. ->create([
  30. 'password' => bcrypt('password'),
  31. 'current_company_id' => $company_id,
  32. ]);
  33. $dateRange = $endDate->diffInMinutes($startDate);
  34. $minOffset = (int) ($dateRange * $minPercentage);
  35. $maxOffset = (int) ($dateRange * $maxPercentage);
  36. for ($i = 0; $i < $userCount; $i++) {
  37. $increment = (int) ($minOffset + ($i * ($maxOffset - $minOffset) / $userCount));
  38. $userCreatedAt = $startDate->copy()->addMinutes($increment);
  39. $user = $users[$i];
  40. // Randomly assign a role to the user
  41. $roles = ['editor', 'admin'];
  42. $role = $roles[array_rand($roles)];
  43. $user->companies()->attach($company_id, compact('role'));
  44. $user->update(['created_at' => $userCreatedAt]);
  45. $user->ownedCompanies->first()?->update(['created_at' => $userCreatedAt]);
  46. // Generate random created_at date for the company_user pivot table (for employees)
  47. $user->companies->first()?->users()->updateExistingPivot($user->id, ['created_at' => $userCreatedAt]);
  48. }
  49. };
  50. // Users for the first company (excluding the first company owner)
  51. $createUsers(1, 5, 0, 0.1);
  52. // Second company owner
  53. $secondCompanyOwner = User::factory()
  54. ->withPersonalCompany()
  55. ->create([
  56. 'password' => bcrypt('admin2'),
  57. 'current_company_id' => 2,
  58. 'created_at' => $startDate->addMinutes($endDate->diffInMinutes($startDate) * 0.1),
  59. ]);
  60. $secondCompanyOwner->ownedCompanies->first()->update(['created_at' => $startDate->addMinutes($endDate->diffInMinutes($startDate) * 0.1)]);
  61. // Users for the second company (excluding the second company owner)
  62. $createUsers(2, 5, 0.1, 0.2);
  63. // Third company owner
  64. $thirdCompanyOwner = User::factory()
  65. ->withPersonalCompany()
  66. ->create([
  67. 'password' => bcrypt('admin3'),
  68. 'current_company_id' => 3,
  69. 'created_at' => $startDate->addMinutes($endDate->diffInMinutes($startDate) * 0.2),
  70. ]);
  71. $thirdCompanyOwner->ownedCompanies->first()->update(['created_at' => $startDate->addMinutes($endDate->diffInMinutes($startDate) * 0.2)]);
  72. // Users for the third company (excluding the third company owner)
  73. $createUsers(3, 5, 0.2, 0.3);
  74. // Create employees for each company (each employee has a company)
  75. $createUsers(4, 5, 0.3, 0.4);
  76. $createUsers(5, 5, 0.4, 0.5);
  77. $createUsers(6, 5, 0.5, 0.6);
  78. $createUsers(7, 5, 0.6, 0.7);
  79. $createUsers(8, 5, 0.7, 0.8);
  80. $createUsers(9, 5, 0.8, 0.9);
  81. $createUsers(10, 5, 0.9, 1);
  82. $this->call([
  83. //CurrencySeeder::class,
  84. //CategorySeeder::class,
  85. //TaxSeeder::class,
  86. //DiscountSeeder::class,
  87. ]);
  88. }
  89. }