AssociadoImportService.php 5.3 KB

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