AssociadoImportService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. $firstName = $this->extractFirstName($name);
  58. $password = "{$firstName}2026";
  59. User::create([
  60. 'name' => $name,
  61. 'password' => Hash::make($password),
  62. 'type' => UserTypeEnum::ASSOCIADO,
  63. 'status' => UserStatusEnum::ACTIVE,
  64. 'registration' => $registration,
  65. 'admission_date' => $today,
  66. 'expiry_date' => $expiryDate,
  67. ]);
  68. $created++;
  69. }
  70. }
  71. $inactivated = User::where('type', UserTypeEnum::ASSOCIADO)
  72. ->where('status', UserStatusEnum::ACTIVE)
  73. ->whereNotNull('registration')
  74. ->whereNotIn('registration', $importedRegistrations)
  75. ->update([
  76. 'status' => UserStatusEnum::INACTIVE,
  77. 'excluded_at' => now(),
  78. ]);
  79. return [
  80. 'total' => $rows->count(),
  81. 'created' => $created,
  82. 'updated' => $updated,
  83. 'inactivated' => $inactivated,
  84. ];
  85. }
  86. private function extractFirstName(string $fullName): string
  87. {
  88. $firstName = explode(' ', trim($fullName))[0];
  89. return strtolower(Str::ascii($firstName));
  90. }
  91. }