|
@@ -102,6 +102,71 @@ class AssociadoImportService
|
|
|
];
|
|
];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ 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 {
|
|
|
|
|
+ $firstName = $this->extractFirstName($name);
|
|
|
|
|
+ $password = "{$firstName}2026";
|
|
|
|
|
+
|
|
|
|
|
+ User::create([
|
|
|
|
|
+ 'name' => $name,
|
|
|
|
|
+ 'password' => Hash::make($password),
|
|
|
|
|
+ '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,
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private function extractFirstName(string $fullName): string
|
|
private function extractFirstName(string $fullName): string
|
|
|
{
|
|
{
|
|
|
$firstName = explode(' ', trim($fullName))[0];
|
|
$firstName = explode(' ', trim($fullName))[0];
|