| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- namespace App\Services;
- use App\Enums\UserStatusEnum;
- use App\Enums\UserTypeEnum;
- use App\Imports\AssociadoImport;
- use App\Models\User;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Hash;
- use Illuminate\Support\Str;
- use Maatwebsite\Excel\Facades\Excel;
- class AssociadoImportService
- {
- public function syncFromExcel(string $filePath): array
- {
- $import = new AssociadoImport();
- Excel::import($import, $filePath);
- $rows = $import->rows ?? collect();
- $today = Carbon::today()->toDateString();
- $expiryDate = Carbon::now()->endOfMonth()->toDateString();
- $existing = User::where('type', UserTypeEnum::ASSOCIADO)
- ->whereNotNull('registration')
- ->get()
- ->keyBy('registration');
- $importedRegistrations = [];
- $seenRegistrations = [];
- $created = 0;
- $updated = 0;
- foreach ($rows as $row) {
- $registration = $row['registration'];
- $name = $row['name'];
- if (isset($seenRegistrations[$registration])) {
- continue;
- }
- $seenRegistrations[$registration] = true;
- $importedRegistrations[] = $registration;
- if ($existing->has($registration)) {
- $user = $existing->get($registration);
- $changed = false;
- if ($user->name !== $name) {
- $user->name = $name;
- $changed = true;
- }
- if ($user->status !== UserStatusEnum::ACTIVE) {
- $user->status = UserStatusEnum::ACTIVE;
- $user->excluded_at = null;
- $changed = true;
- }
- if ($user->expiry_date?->toDateString() !== $expiryDate) {
- $user->expiry_date = $expiryDate;
- $changed = true;
- }
- if ($changed) {
- $user->save();
- $updated++;
- }
- } else {
- User::create([
- 'name' => $name,
- 'password' => Hash::make(Str::random(32)),
- 'type' => UserTypeEnum::ASSOCIADO,
- 'status' => UserStatusEnum::ACTIVE,
- 'registration' => $registration,
- 'admission_date' => $today,
- 'expiry_date' => $expiryDate,
- ]);
- $created++;
- }
- }
- $inactivated = User::where('type', UserTypeEnum::ASSOCIADO)
- ->where('status', UserStatusEnum::ACTIVE)
- ->whereNotNull('registration')
- ->whereNotIn('registration', $importedRegistrations)
- ->update([
- 'status' => UserStatusEnum::INACTIVE,
- 'excluded_at' => now(),
- ]);
- return [
- 'total' => $rows->count(),
- 'created' => $created,
- 'updated' => $updated,
- 'inactivated' => $inactivated,
- ];
- }
- public function syncAfastamentosFromExcel(string $filePath, int $byUserId): array
- {
- $import = new AssociadoImport();
- Excel::import($import, $filePath);
- $rows = $import->rows ?? collect();
- $today = Carbon::today()->toDateString();
- $expiryDate = Carbon::now()->endOfMonth()->toDateString();
- $existing = User::where('type', UserTypeEnum::ASSOCIADO)
- ->whereNotNull('registration')
- ->get()
- ->keyBy('registration');
- $seenRegistrations = [];
- $created = 0;
- $onLeave = 0;
- foreach ($rows as $row) {
- $registration = $row['registration'];
- $name = $row['name'];
- if (isset($seenRegistrations[$registration])) {
- continue;
- }
- $seenRegistrations[$registration] = true;
- if ($existing->has($registration)) {
- $user = $existing->get($registration);
- if ($user->status !== UserStatusEnum::ON_LEAVE) {
- $user->status = UserStatusEnum::ON_LEAVE;
- $user->on_leave_at = now();
- $user->on_leave_by_user_id = $byUserId;
- $user->save();
- $onLeave++;
- }
- } else {
- User::create([
- 'name' => $name,
- 'password' => Hash::make(Str::random(32)),
- 'type' => UserTypeEnum::ASSOCIADO,
- 'status' => UserStatusEnum::ON_LEAVE,
- 'registration' => $registration,
- 'admission_date' => $today,
- 'expiry_date' => $expiryDate,
- 'on_leave_at' => now(),
- 'on_leave_by_user_id' => $byUserId,
- ]);
- $created++;
- }
- }
- return [
- 'total' => $rows->count(),
- 'created' => $created,
- 'on_leave' => $onLeave,
- ];
- }
- }
|