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,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

View File

@ -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'],
];
}

View File

@ -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'],
];
}