DeveloperTestSeeder.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace Database\Seeders;
  3. use App\Models\City;
  4. use App\Models\Permission;
  5. use App\Models\Unit;
  6. use App\Models\UnitPartner;
  7. use App\Models\UnitUser;
  8. use App\Models\User;
  9. use App\Models\UserType;
  10. use App\Models\UserTypePermission;
  11. use Illuminate\Database\Seeder;
  12. use Illuminate\Support\Facades\Cache;
  13. class DeveloperTestSeeder extends Seeder
  14. {
  15. public function run(): void
  16. {
  17. $cities = City::with('state')
  18. ->whereIn('name', ['São Paulo', 'Campinas', 'Santos'])
  19. ->get()
  20. ->keyBy('name');
  21. $fallbackCity = City::with('state')->firstOrFail();
  22. $accessPassword = 'S@ft2080.';
  23. $franchiseePermissions = Permission::where('scope', 'like', 'franchisee_%')->get();
  24. $units = [
  25. [
  26. 'city' => 'São Paulo',
  27. 'attributes' => [
  28. 'name' => 'Ginástica do Cérebro Paulista',
  29. 'fantasy_name' => 'GC Paulista',
  30. 'social_reason' => 'GC Paulista Desenvolvimento Cognitivo Ltda.',
  31. 'cnpj' => '11.723.853/0001-78',
  32. 'state_registration' => '110042490114',
  33. 'name_responsible' => 'Mariana Oliveira',
  34. 'street' => 'Avenida Paulista',
  35. 'address_number' => '1000',
  36. 'postal_code' => '01310-100',
  37. 'neighborhood' => 'Bela Vista',
  38. 'complement' => 'Conjunto 101',
  39. 'email' => 'paulista@dev.ginasticadocerebro.com.br',
  40. 'secondary_email' => 'administrativo.paulista@dev.ginasticadocerebro.com.br',
  41. 'phone_number' => '(11) 3251-1000',
  42. 'cell_number' => '(11) 99991-1000',
  43. ],
  44. 'partners' => [
  45. [
  46. 'name' => 'Mariana Oliveira',
  47. 'cpf' => '52998224725',
  48. 'role' => 'Sócia Administradora',
  49. 'participation' => 60,
  50. 'email' => 'mariana.oliveira@dev.ginasticadocerebro.com.br',
  51. 'cell_number' => '(11) 99991-1001',
  52. ],
  53. [
  54. 'name' => 'Carlos Henrique Lima',
  55. 'cpf' => '16899535009',
  56. 'role' => 'Sócio',
  57. 'participation' => 40,
  58. 'email' => 'carlos.lima@dev.ginasticadocerebro.com.br',
  59. ],
  60. ],
  61. ],
  62. [
  63. 'city' => 'Campinas',
  64. 'attributes' => [
  65. 'name' => 'Ginástica do Cérebro Campinas',
  66. 'fantasy_name' => 'GC Campinas',
  67. 'social_reason' => 'GC Campinas Treinamento Mental Ltda.',
  68. 'cnpj' => '45.997.418/0001-53',
  69. 'state_registration' => '244567890116',
  70. 'name_responsible' => 'Fernanda Souza',
  71. 'street' => 'Avenida José de Souza Campos',
  72. 'address_number' => '750',
  73. 'postal_code' => '13025-320',
  74. 'neighborhood' => 'Cambuí',
  75. 'complement' => 'Sala 12',
  76. 'email' => 'campinas@dev.ginasticadocerebro.com.br',
  77. 'secondary_email' => null,
  78. 'phone_number' => null,
  79. 'cell_number' => '(19) 99992-2000',
  80. ],
  81. 'partners' => [
  82. [
  83. 'name' => 'Fernanda Souza',
  84. 'cpf' => '11144477735',
  85. 'email' => 'fernanda.souza@dev.ginasticadocerebro.com.br',
  86. ],
  87. [
  88. 'name' => 'Ricardo Martins',
  89. 'cpf' => '12345678909',
  90. 'role' => 'Sócio',
  91. 'participation' => 35,
  92. 'email' => 'ricardo.martins@dev.ginasticadocerebro.com.br',
  93. 'cell_number' => '(19) 99992-2001',
  94. ],
  95. ],
  96. ],
  97. [
  98. 'city' => 'Santos',
  99. 'attributes' => [
  100. 'name' => 'Ginástica do Cérebro Santos',
  101. 'fantasy_name' => 'GC Santos',
  102. 'social_reason' => 'GC Santos Educação e Desenvolvimento Ltda.',
  103. 'cnpj' => '27.865.757/0001-02',
  104. 'state_registration' => null,
  105. 'name_responsible' => 'Ana Paula Ribeiro',
  106. 'street' => 'Avenida Ana Costa',
  107. 'address_number' => '450',
  108. 'postal_code' => '11060-002',
  109. 'neighborhood' => 'Gonzaga',
  110. 'complement' => null,
  111. 'email' => 'santos@dev.ginasticadocerebro.com.br',
  112. 'secondary_email' => 'administrativo.santos@dev.ginasticadocerebro.com.br',
  113. 'phone_number' => '(13) 3284-3000',
  114. 'cell_number' => '(13) 99993-3000',
  115. ],
  116. 'partners' => [
  117. [
  118. 'name' => 'Ana Paula Ribeiro',
  119. 'cpf' => '98765432100',
  120. 'role' => 'Sócia Administradora',
  121. 'participation' => 70,
  122. 'email' => 'ana.ribeiro@dev.ginasticadocerebro.com.br',
  123. ],
  124. [
  125. 'name' => 'Lucas Almeida',
  126. 'cpf' => '39053344705',
  127. 'role' => 'Sócio',
  128. 'participation' => 30,
  129. 'email' => 'lucas.almeida@dev.ginasticadocerebro.com.br',
  130. ],
  131. ],
  132. ],
  133. ];
  134. foreach ($units as $unitData) {
  135. $city = $cities->get($unitData['city']) ?? $fallbackCity;
  136. $attributes = array_merge($unitData['attributes'], [
  137. 'city_id' => $city->id,
  138. 'state_id' => $city->state_id,
  139. ]);
  140. $unit = Unit::withTrashed()->updateOrCreate(
  141. ['cnpj' => $attributes['cnpj']],
  142. $attributes,
  143. );
  144. $unit->restore();
  145. $userType = UserType::updateOrCreate(
  146. [
  147. 'scope_key' => "unit:{$unit->id}",
  148. 'slug' => 'ADMIN_FRANCHISEE',
  149. ],
  150. [
  151. 'unit_id' => $unit->id,
  152. 'system_key' => 'ADMIN_FRANCHISEE',
  153. 'label' => 'Administrador',
  154. 'is_system' => true,
  155. 'access_scope' => 'unit',
  156. ],
  157. );
  158. foreach ($franchiseePermissions as $permission) {
  159. $userTypePermission = UserTypePermission::withTrashed()->updateOrCreate(
  160. [
  161. 'user_type_id' => $userType->id,
  162. 'permission_id' => $permission->id,
  163. ],
  164. [
  165. 'user_type' => $userType->slug,
  166. 'bits' => Permission::ALL_PERMS,
  167. ],
  168. );
  169. if ($userTypePermission->trashed()) {
  170. $userTypePermission->restore();
  171. }
  172. }
  173. Cache::forget("permissions_role_id_{$userType->id}");
  174. foreach ($unitData['partners'] as $partnerData) {
  175. $partner = UnitPartner::withTrashed()->updateOrCreate(
  176. [
  177. 'unit_id' => $unit->id,
  178. 'cpf' => $partnerData['cpf'],
  179. ],
  180. array_merge($partnerData, [
  181. 'unit_id' => $unit->id,
  182. 'city_id' => $city->id,
  183. 'state_id' => $city->state_id,
  184. ]),
  185. );
  186. $partner->restore();
  187. $user = User::updateOrCreate(
  188. ['email' => $partnerData['email']],
  189. [
  190. 'name' => $partnerData['name'],
  191. 'cpf' => $partnerData['cpf'],
  192. 'password' => $accessPassword,
  193. 'status' => 'ACTIVE',
  194. 'user_type' => 'ADMIN_FRANCHISEE',
  195. 'access_scope' => 'unit',
  196. 'state_id' => $city->state_id,
  197. 'deleted_at' => null,
  198. ],
  199. );
  200. UnitUser::updateOrCreate(
  201. [
  202. 'unit_id' => $unit->id,
  203. 'user_id' => $user->id,
  204. ],
  205. [
  206. 'user_type_id' => $userType->id,
  207. 'deleted_at' => null,
  208. ],
  209. );
  210. }
  211. }
  212. }
  213. }