AssociadoImportService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace App\Services;
  3. use App\Enums\UserStatusEnum;
  4. use App\Enums\UserTypeEnum;
  5. use App\Imports\AssociadoImport;
  6. use App\Models\User;
  7. use Carbon\Carbon;
  8. use Illuminate\Support\Facades\Hash;
  9. use Illuminate\Support\Str;
  10. use Maatwebsite\Excel\Facades\Excel;
  11. class AssociadoImportService
  12. {
  13. public function syncFromExcel(string $filePath): array
  14. {
  15. $import = new AssociadoImport();
  16. Excel::import($import, $filePath);
  17. $rows = $import->rows ?? collect();
  18. $today = Carbon::today()->toDateString();
  19. $expiryDate = Carbon::now()->endOfMonth()->toDateString();
  20. $existing = User::where('type', UserTypeEnum::ASSOCIADO)
  21. ->whereNotNull('registration')
  22. ->get()
  23. ->keyBy('registration');
  24. $importedRegistrations = [];
  25. $seenRegistrations = [];
  26. $created = 0;
  27. $updated = 0;
  28. foreach ($rows as $row) {
  29. $registration = $row['registration'];
  30. $name = $row['name'];
  31. if (isset($seenRegistrations[$registration])) {
  32. continue;
  33. }
  34. $seenRegistrations[$registration] = true;
  35. $importedRegistrations[] = $registration;
  36. if ($existing->has($registration)) {
  37. $user = $existing->get($registration);
  38. $changed = false;
  39. if ($user->name !== $name) {
  40. $user->name = $name;
  41. $changed = true;
  42. }
  43. if ($user->status !== UserStatusEnum::ACTIVE) {
  44. $user->status = UserStatusEnum::ACTIVE;
  45. $user->excluded_at = null;
  46. $changed = true;
  47. }
  48. if ($user->expiry_date?->toDateString() !== $expiryDate) {
  49. $user->expiry_date = $expiryDate;
  50. $changed = true;
  51. }
  52. if ($changed) {
  53. $user->save();
  54. $updated++;
  55. }
  56. } else {
  57. User::create([
  58. 'name' => $name,
  59. 'password' => Hash::make(Str::random(32)),
  60. 'type' => UserTypeEnum::ASSOCIADO,
  61. 'status' => UserStatusEnum::ACTIVE,
  62. 'registration' => $registration,
  63. 'admission_date' => $today,
  64. 'expiry_date' => $expiryDate,
  65. ]);
  66. $created++;
  67. }
  68. }
  69. $inactivated = User::where('type', UserTypeEnum::ASSOCIADO)
  70. ->where('status', UserStatusEnum::ACTIVE)
  71. ->whereNotNull('registration')
  72. ->whereNotIn('registration', $importedRegistrations)
  73. ->update([
  74. 'status' => UserStatusEnum::INACTIVE,
  75. 'excluded_at' => now(),
  76. ]);
  77. return [
  78. 'total' => $rows->count(),
  79. 'created' => $created,
  80. 'updated' => $updated,
  81. 'inactivated' => $inactivated,
  82. ];
  83. }
  84. public function syncAfastamentosFromExcel(string $filePath, int $byUserId): array
  85. {
  86. $import = new AssociadoImport();
  87. Excel::import($import, $filePath);
  88. $rows = $import->rows ?? collect();
  89. $today = Carbon::today()->toDateString();
  90. $expiryDate = Carbon::now()->endOfMonth()->toDateString();
  91. $existing = User::where('type', UserTypeEnum::ASSOCIADO)
  92. ->whereNotNull('registration')
  93. ->get()
  94. ->keyBy('registration');
  95. $seenRegistrations = [];
  96. $created = 0;
  97. $onLeave = 0;
  98. foreach ($rows as $row) {
  99. $registration = $row['registration'];
  100. $name = $row['name'];
  101. if (isset($seenRegistrations[$registration])) {
  102. continue;
  103. }
  104. $seenRegistrations[$registration] = true;
  105. if ($existing->has($registration)) {
  106. $user = $existing->get($registration);
  107. if ($user->status !== UserStatusEnum::ON_LEAVE) {
  108. $user->status = UserStatusEnum::ON_LEAVE;
  109. $user->on_leave_at = now();
  110. $user->on_leave_by_user_id = $byUserId;
  111. $user->save();
  112. $onLeave++;
  113. }
  114. } else {
  115. User::create([
  116. 'name' => $name,
  117. 'password' => Hash::make(Str::random(32)),
  118. 'type' => UserTypeEnum::ASSOCIADO,
  119. 'status' => UserStatusEnum::ON_LEAVE,
  120. 'registration' => $registration,
  121. 'admission_date' => $today,
  122. 'expiry_date' => $expiryDate,
  123. 'on_leave_at' => now(),
  124. 'on_leave_by_user_id' => $byUserId,
  125. ]);
  126. $created++;
  127. }
  128. }
  129. return [
  130. 'total' => $rows->count(),
  131. 'created' => $created,
  132. 'on_leave' => $onLeave,
  133. ];
  134. }
  135. }