FIX: Permitir RFC nulo en solicitudes de cliente
This commit is contained in:
parent
f310bcac51
commit
72f2e4f24d
@ -126,16 +126,40 @@ public function import(Request $request)
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
$rows = $sheet->toArray();
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
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,
|
||||
@ -146,25 +170,26 @@ public function import(Request $request)
|
||||
'price' => $p->price
|
||||
], $this->packageCache)),
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
throw $e;
|
||||
}
|
||||
} 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();
|
||||
|
||||
@ -182,13 +207,6 @@ private function processRow(array $row)
|
||||
$this->processPackageFromText($sim, $row);
|
||||
// Asignar cliente
|
||||
$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)
|
||||
@ -283,7 +301,6 @@ private function assignToClient(SimCard $sim, array $row)
|
||||
if (!$client) {
|
||||
$nameParts = $this->splitFullName($usuario);
|
||||
|
||||
try {
|
||||
$client = Client::create([
|
||||
'full_name' => $usuario,
|
||||
'name' => $nameParts['name'],
|
||||
@ -292,13 +309,6 @@ private function assignToClient(SimCard $sim, array $row)
|
||||
]);
|
||||
|
||||
$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)
|
||||
@ -310,7 +320,6 @@ private function assignToClient(SimCard $sim, array $row)
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
ClientSim::create([
|
||||
'client_id' => $client->id,
|
||||
'sim_card_id' => $sim->id,
|
||||
@ -321,13 +330,6 @@ private function assignToClient(SimCard $sim, array $row)
|
||||
$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()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
private function splitFullName(string $fullName): array
|
||||
|
||||
@ -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'],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@ -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'],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user