DeveloperTestSeeder.php 9.6 KB

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