From 72f2e4f24dadd9e8123fc452d801fee0231b7188 Mon Sep 17 00:00:00 2001 From: Juan Felipe Zapata Moreno Date: Wed, 19 Nov 2025 17:05:22 -0600 Subject: [PATCH] FIX: Permitir RFC nulo en solicitudes de cliente --- .../Controllers/Netbien/SimCardController.php | 148 +++++++++--------- .../Requests/Netbien/ClientStoreRequest.php | 2 +- .../Requests/Netbien/ClientUpdateRequest.php | 2 +- 3 files changed, 77 insertions(+), 75 deletions(-) diff --git a/app/Http/Controllers/Netbien/SimCardController.php b/app/Http/Controllers/Netbien/SimCardController.php index 50db6cd..6cc528b 100644 --- a/app/Http/Controllers/Netbien/SimCardController.php +++ b/app/Http/Controllers/Netbien/SimCardController.php @@ -126,69 +126,87 @@ public function import(Request $request) $sheet = $spreadsheet->getActiveSheet(); $rows = $sheet->toArray(); - foreach ($rows as $index => $row) { - if ($index === 0) continue; + DB::beginTransaction(); - $this->processRow([ - 'iccid' => $row[4] ?? null, - 'msisdn' => $row[5] ?? null, - 'estado_de_la_sim' => $row[9] ?? null, - 'usuario' => $row[8] ?? null, + try { + foreach ($rows as $index => $row) { + if ($index === 0) continue; + + try { + $this->processRow([ + 'iccid' => $row[4] ?? null, + 'msisdn' => $row[5] ?? null, + 'estado_de_la_sim' => $row[9] ?? null, + 'usuario' => $row[8] ?? null, + ], $index + 1); + } catch (\Exception $e) { + // Capturar información detallada del error antes de revertir + DB::rollBack(); + + return ApiResponse::BAD_REQUEST->response([ + 'success' => false, + 'message' => 'Error en la importación', + 'error' => $e->getMessage(), + 'fila' => $index + 1, + 'datos_fila' => [ + 'iccid' => $row[4] ?? null, + 'msisdn' => $row[5] ?? null, + 'estado_de_la_sim' => $row[9] ?? null, + 'usuario' => $row[8] ?? null, + ], + 'stats' => $this->stats, + ], 500); + } + } + + DB::commit(); + + return ApiResponse::OK->response([ + 'success' => true, + 'message' => 'Importación completada', + 'stats' => $this->stats, + 'packages_created' => array_values(array_map(fn($p) => [ + 'name' => $p->name, + 'price' => $p->price + ], $this->packageCache)), ]); + } catch (\Exception $e) { + DB::rollBack(); + throw $e; } - - return ApiResponse::OK->response([ - 'success' => true, - 'message' => 'Importación completada', - 'stats' => $this->stats, - 'packages_created' => array_values(array_map(fn($p) => [ - 'name' => $p->name, - 'price' => $p->price - ], $this->packageCache)), - ]); } catch (\Exception $e) { return ApiResponse::BAD_REQUEST->response([ 'success' => false, 'message' => 'Error en la importación', 'error' => $e->getMessage(), - 'line' => $e->getLine(), ], 500); } } - private function processRow(array $row) + private function processRow(array $row, int $rowNumber = 0) { // Validar campos requeridos if (empty($row['iccid']) || empty($row['msisdn'])) { return; } - try { - DB::transaction(function () use ($row) { - // Buscar o crear la SIM - $sim = SimCard::where('iccid', $row['iccid'])->first(); + // Buscar o crear la SIM + $sim = SimCard::where('iccid', $row['iccid'])->first(); - if (!$sim) { - // No existe, crearla - $sim = SimCard::create([ - 'iccid' => $row['iccid'], - 'msisdn' => $row['msisdn'], - 'status' => SimCardStatus::AVAILABLE, - ]); + if (!$sim) { + // No existe, crearla + $sim = SimCard::create([ + 'iccid' => $row['iccid'], + 'msisdn' => $row['msisdn'], + 'status' => SimCardStatus::AVAILABLE, + ]); - $this->stats['created']++; - } - - $this->processPackageFromText($sim, $row); - // Asignar cliente - $this->assignToClient($sim, $row); - }); - } catch (\Exception $e) { - $this->stats['errors'][] = [ - 'iccid' => $row['iccid'] ?? 'N/A', - 'error' => $e->getMessage(), - ]; + $this->stats['created']++; } + + $this->processPackageFromText($sim, $row); + // Asignar cliente + $this->assignToClient($sim, $row); } private function processPackageFromText(SimCard $sim, array $row) @@ -283,22 +301,14 @@ private function assignToClient(SimCard $sim, array $row) if (!$client) { $nameParts = $this->splitFullName($usuario); - try { - $client = Client::create([ - 'full_name' => $usuario, - 'name' => $nameParts['name'], - 'paternal' => $nameParts['paternal'], - 'maternal' => $nameParts['maternal'], - ]); + $client = Client::create([ + 'full_name' => $usuario, + 'name' => $nameParts['name'], + 'paternal' => $nameParts['paternal'], + 'maternal' => $nameParts['maternal'], + ]); - $this->stats['clients_created']++; - } catch (\Exception $e) { - $this->stats['errors'][] = [ - 'usuario' => $usuario, - 'error' => 'Error al crear cliente: ' . $e->getMessage() - ]; - return; - } + $this->stats['clients_created']++; } $existingRelation = ClientSim::where('client_id', $client->id) @@ -310,24 +320,16 @@ private function assignToClient(SimCard $sim, array $row) return; } - try { - ClientSim::create([ - 'client_id' => $client->id, - 'sim_card_id' => $sim->id, - 'assigned_at' => now(), - 'is_active' => true, - ]); + ClientSim::create([ + 'client_id' => $client->id, + 'sim_card_id' => $sim->id, + 'assigned_at' => now(), + 'is_active' => true, + ]); - $sim->update(['status' => SimCardStatus::ASSIGNED]); + $sim->update(['status' => SimCardStatus::ASSIGNED]); - $this->stats['assigned']++; - } catch (\Exception $e) { - $this->stats['errors'][] = [ - 'iccid' => $sim->iccid, - 'usuario' => $usuario, - 'error' => 'Error al asignar cliente: ' . $e->getMessage() - ]; - } + $this->stats['assigned']++; } private function splitFullName(string $fullName): array diff --git a/app/Http/Requests/Netbien/ClientStoreRequest.php b/app/Http/Requests/Netbien/ClientStoreRequest.php index 2bfaee5..7c4f86a 100644 --- a/app/Http/Requests/Netbien/ClientStoreRequest.php +++ b/app/Http/Requests/Netbien/ClientStoreRequest.php @@ -32,7 +32,7 @@ public function rules(): array 'maternal' => ['required', 'string'], 'email' => ['nullable', 'email'], 'phone' => ['nullable', 'string', 'max:10'], - 'rfc' => ['required', 'string', 'max:13'], + 'rfc' => ['nullable', 'string', 'max:13'], ]; } diff --git a/app/Http/Requests/Netbien/ClientUpdateRequest.php b/app/Http/Requests/Netbien/ClientUpdateRequest.php index f81c367..7de9d77 100644 --- a/app/Http/Requests/Netbien/ClientUpdateRequest.php +++ b/app/Http/Requests/Netbien/ClientUpdateRequest.php @@ -32,7 +32,7 @@ public function rules(): array 'maternal' => ['sometimes', 'string', 'max:100'], 'email' => ['sometimes', 'email'], 'phone' => ['nullable', 'string', 'max:20'], - 'rfc' => ['sometimes', 'string', 'max:13'], + 'rfc' => ['nullable', 'string', 'max:13'], ]; }