FIX: Permitir RFC nulo en solicitudes de cliente

This commit is contained in:
Juan Felipe Zapata Moreno 2025-11-19 17:05:22 -06:00
parent f310bcac51
commit 72f2e4f24d
3 changed files with 77 additions and 75 deletions

View File

@ -126,16 +126,40 @@ public function import(Request $request)
$sheet = $spreadsheet->getActiveSheet(); $sheet = $spreadsheet->getActiveSheet();
$rows = $sheet->toArray(); $rows = $sheet->toArray();
DB::beginTransaction();
try {
foreach ($rows as $index => $row) { foreach ($rows as $index => $row) {
if ($index === 0) continue; if ($index === 0) continue;
try {
$this->processRow([ $this->processRow([
'iccid' => $row[4] ?? null, 'iccid' => $row[4] ?? null,
'msisdn' => $row[5] ?? null, 'msisdn' => $row[5] ?? null,
'estado_de_la_sim' => $row[9] ?? null, 'estado_de_la_sim' => $row[9] ?? null,
'usuario' => $row[8] ?? 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([ return ApiResponse::OK->response([
'success' => true, 'success' => true,
@ -146,25 +170,26 @@ public function import(Request $request)
'price' => $p->price 'price' => $p->price
], $this->packageCache)), ], $this->packageCache)),
]); ]);
} catch (\Exception $e) {
DB::rollBack();
throw $e;
}
} catch (\Exception $e) { } catch (\Exception $e) {
return ApiResponse::BAD_REQUEST->response([ return ApiResponse::BAD_REQUEST->response([
'success' => false, 'success' => false,
'message' => 'Error en la importación', 'message' => 'Error en la importación',
'error' => $e->getMessage(), 'error' => $e->getMessage(),
'line' => $e->getLine(),
], 500); ], 500);
} }
} }
private function processRow(array $row) private function processRow(array $row, int $rowNumber = 0)
{ {
// Validar campos requeridos // Validar campos requeridos
if (empty($row['iccid']) || empty($row['msisdn'])) { if (empty($row['iccid']) || empty($row['msisdn'])) {
return; return;
} }
try {
DB::transaction(function () use ($row) {
// Buscar o crear la SIM // Buscar o crear la SIM
$sim = SimCard::where('iccid', $row['iccid'])->first(); $sim = SimCard::where('iccid', $row['iccid'])->first();
@ -182,13 +207,6 @@ private function processRow(array $row)
$this->processPackageFromText($sim, $row); $this->processPackageFromText($sim, $row);
// Asignar cliente // Asignar cliente
$this->assignToClient($sim, $row); $this->assignToClient($sim, $row);
});
} catch (\Exception $e) {
$this->stats['errors'][] = [
'iccid' => $row['iccid'] ?? 'N/A',
'error' => $e->getMessage(),
];
}
} }
private function processPackageFromText(SimCard $sim, array $row) private function processPackageFromText(SimCard $sim, array $row)
@ -283,7 +301,6 @@ private function assignToClient(SimCard $sim, array $row)
if (!$client) { if (!$client) {
$nameParts = $this->splitFullName($usuario); $nameParts = $this->splitFullName($usuario);
try {
$client = Client::create([ $client = Client::create([
'full_name' => $usuario, 'full_name' => $usuario,
'name' => $nameParts['name'], 'name' => $nameParts['name'],
@ -292,13 +309,6 @@ private function assignToClient(SimCard $sim, array $row)
]); ]);
$this->stats['clients_created']++; $this->stats['clients_created']++;
} catch (\Exception $e) {
$this->stats['errors'][] = [
'usuario' => $usuario,
'error' => 'Error al crear cliente: ' . $e->getMessage()
];
return;
}
} }
$existingRelation = ClientSim::where('client_id', $client->id) $existingRelation = ClientSim::where('client_id', $client->id)
@ -310,7 +320,6 @@ private function assignToClient(SimCard $sim, array $row)
return; return;
} }
try {
ClientSim::create([ ClientSim::create([
'client_id' => $client->id, 'client_id' => $client->id,
'sim_card_id' => $sim->id, 'sim_card_id' => $sim->id,
@ -321,13 +330,6 @@ private function assignToClient(SimCard $sim, array $row)
$sim->update(['status' => SimCardStatus::ASSIGNED]); $sim->update(['status' => SimCardStatus::ASSIGNED]);
$this->stats['assigned']++; $this->stats['assigned']++;
} catch (\Exception $e) {
$this->stats['errors'][] = [
'iccid' => $sim->iccid,
'usuario' => $usuario,
'error' => 'Error al asignar cliente: ' . $e->getMessage()
];
}
} }
private function splitFullName(string $fullName): array private function splitFullName(string $fullName): array

View File

@ -32,7 +32,7 @@ public function rules(): array
'maternal' => ['required', 'string'], 'maternal' => ['required', 'string'],
'email' => ['nullable', 'email'], 'email' => ['nullable', 'email'],
'phone' => ['nullable', 'string', 'max:10'], 'phone' => ['nullable', 'string', 'max:10'],
'rfc' => ['required', 'string', 'max:13'], 'rfc' => ['nullable', 'string', 'max:13'],
]; ];
} }

View File

@ -32,7 +32,7 @@ public function rules(): array
'maternal' => ['sometimes', 'string', 'max:100'], 'maternal' => ['sometimes', 'string', 'max:100'],
'email' => ['sometimes', 'email'], 'email' => ['sometimes', 'email'],
'phone' => ['nullable', 'string', 'max:20'], 'phone' => ['nullable', 'string', 'max:20'],
'rfc' => ['sometimes', 'string', 'max:13'], 'rfc' => ['nullable', 'string', 'max:13'],
]; ];
} }