Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

DatabaseSeeder.php 4.3KB

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