Ajustes para trasmitir expedientes
This commit is contained in:
@@ -30,7 +30,7 @@ namespace EFCDesk.Classes
|
||||
private readonly object _lockProceso = new object();
|
||||
|
||||
// Parámetros de robustez
|
||||
private readonly int _maxConcurrent = 2;
|
||||
private readonly int _maxConcurrent = 1;
|
||||
private readonly int _perFileTimeoutSeconds = 30;
|
||||
private readonly int _maxReintentos = 10; // reintentos de 1 minuto
|
||||
private readonly TimeSpan _reintentoDelay = TimeSpan.FromMinutes(1);
|
||||
@@ -225,126 +225,7 @@ namespace EFCDesk.Classes
|
||||
}
|
||||
}
|
||||
|
||||
//private async Task ProcesarCarpetaRaizAsync(string carpetaRaiz)
|
||||
//{
|
||||
// if (!Directory.Exists(carpetaRaiz))
|
||||
// {
|
||||
// Console.WriteLine($"[SKIP] No existe: {carpetaRaiz}");
|
||||
// return;
|
||||
// }
|
||||
|
||||
// Console.WriteLine($"➡️ Procesando raíz: {carpetaRaiz}");
|
||||
// var nombreRaiz = Path.GetFileName(carpetaRaiz);
|
||||
// var archivos = Directory.GetFiles(carpetaRaiz, "*.*", SearchOption.AllDirectories)
|
||||
// .Where(f => !EsTemporal(f))
|
||||
// .ToArray();
|
||||
|
||||
// if (archivos.Length == 0)
|
||||
// {
|
||||
// Console.WriteLine($"[INFO] Sin archivos en {carpetaRaiz}");
|
||||
// _repo.MarcarCarpetaProcesada(carpetaRaiz);
|
||||
// return;
|
||||
// }
|
||||
|
||||
// // Reintentos para archivos en uso
|
||||
// int intento = 0;
|
||||
// while (intento <= _maxReintentos)
|
||||
// {
|
||||
// var pendientes = archivos.Where(f => File.Exists(f) && !FileUtil.IsFileReady(f)).ToArray();
|
||||
// if (pendientes.Length == 0) break;
|
||||
|
||||
// if (intento == _maxReintentos)
|
||||
// {
|
||||
// Console.WriteLine($"[WARN] Archivos bloqueados tras {_maxReintentos} intentos. Se omite: {carpetaRaiz}");
|
||||
// return;
|
||||
// }
|
||||
|
||||
// Console.WriteLine($"[WAIT] {pendientes.Length} archivos bloqueados en {carpetaRaiz}. Reintento {intento + 1}/{_maxReintentos} en {_reintentoDelay.TotalMinutes} min.");
|
||||
// await Task.Delay(_reintentoDelay);
|
||||
// intento++;
|
||||
// }
|
||||
|
||||
// // Filtrar archivos ya procesados (persistente en SQLite)
|
||||
// var archivosAProcesar = new List<string>(archivos.Length);
|
||||
// foreach (var f in archivos)
|
||||
// {
|
||||
// if (_repo.EstaArchivoProcesado(f)) continue;
|
||||
// archivosAProcesar.Add(f);
|
||||
// }
|
||||
|
||||
// if (archivosAProcesar.Count == 0)
|
||||
// {
|
||||
// Console.WriteLine($"[INFO] Todos los archivos ya fueron procesados en {carpetaRaiz}");
|
||||
// _repo.MarcarCarpetaProcesada(carpetaRaiz);
|
||||
// return;
|
||||
// }
|
||||
|
||||
// // Crear ZIP aplanado
|
||||
// var rutaZip = Path.Combine(Path.GetDirectoryName(carpetaRaiz)!, $"{nombreRaiz}.zip");
|
||||
// if (File.Exists(rutaZip)) File.Delete(rutaZip);
|
||||
|
||||
// using (var zip = ZipFile.Open(rutaZip, ZipArchiveMode.Create))
|
||||
// {
|
||||
// foreach (var archivo in archivosAProcesar)
|
||||
// {
|
||||
// var nombreArchivo = Path.GetFileName(archivo);
|
||||
|
||||
// // Seguridad adicional: esperar per-file si justo se bloquea
|
||||
// if (!FileUtil.WaitAllFilesReady(new[] { archivo }, _perFileTimeoutSeconds))
|
||||
// {
|
||||
// Console.WriteLine($"[SKIP] Bloqueado: {archivo}");
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// // Registrar como procesado antes de subir, para evitar duplicados si se cae después
|
||||
// var sha1 = FileUtil.TryGetSHA1(archivo); // opcional, puede ser null
|
||||
// _repo.RegistrarArchivoProcesado(archivo, nombreArchivo, carpetaRaiz, $"{nombreRaiz}.zip", sha1);
|
||||
|
||||
// // Agregar al zip aplanado (sin directorios)
|
||||
// zip.CreateEntryFromFile(archivo, nombreArchivo, CompressionLevel.Optimal);
|
||||
// Console.WriteLine($" + {nombreArchivo}");
|
||||
// }
|
||||
// }
|
||||
|
||||
// Console.WriteLine($"ZIP creado: {rutaZip}");
|
||||
|
||||
// // 🔌 HOOK: Subir a FTP
|
||||
// try
|
||||
// {
|
||||
// SubirArchivoFTP(rutaZip);
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// Console.WriteLine($"[FTP ERR] {ex.Message}");
|
||||
// // según tu estrategia, podrías marcar la carpeta como pendiente otra vez
|
||||
// // y retornar aquí para reintentar más tarde.
|
||||
// }
|
||||
|
||||
// // 🔌 HOOK: Registrar en API
|
||||
// try
|
||||
// {
|
||||
// RegistrarArchivoEnAPI(rutaZip);
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// Console.WriteLine($"[API ERR] {ex.Message}");
|
||||
// }
|
||||
|
||||
// try
|
||||
// {
|
||||
// File.Delete(rutaZip);
|
||||
// Console.WriteLine($"🧹 ZIP eliminado: {rutaZip}");
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// Console.WriteLine($"[ZIP DEL ERR] {ex.Message}");
|
||||
// }
|
||||
|
||||
// _repo.MarcarCarpetaProcesada(carpetaRaiz);
|
||||
// Console.WriteLine($"✅ Completado: {carpetaRaiz}");
|
||||
//}
|
||||
|
||||
private async Task ProcesarCarpetaRaizAsync(string carpetaRaiz)
|
||||
private async Task ProcesarCarpetaRaizAsync1(string carpetaRaiz)
|
||||
{
|
||||
if (!Directory.Exists(carpetaRaiz))
|
||||
{
|
||||
@@ -359,16 +240,6 @@ namespace EFCDesk.Classes
|
||||
.Where(f => !EsTemporal(f))
|
||||
.ToArray();
|
||||
|
||||
// ==== Validar formato de nombre === //
|
||||
//string pattern = @"^\d{2}-\d{2}-\d{4}-\d{7}$";
|
||||
//bool esValido = System.Text.RegularExpressions.Regex.IsMatch(nombreRaiz, pattern);
|
||||
//if (!esValido)
|
||||
//{
|
||||
// _repo.MarcarCarpetaProcesada(carpetaRaiz, "Formato Incorrecto");
|
||||
// Console.WriteLine($"[SKIP] Formato Expediente Incorrecto: {nombreRaiz}");
|
||||
// return;
|
||||
//}
|
||||
|
||||
bool esValido = Helpers.EsValidaNomeclaturaCarpeta(nombreRaiz);
|
||||
if (!esValido)
|
||||
{
|
||||
@@ -471,6 +342,7 @@ namespace EFCDesk.Classes
|
||||
_dbSemaforo.Release();
|
||||
}
|
||||
|
||||
// Crear ZIP aplanado con solo ese archivo
|
||||
bool respuesa = false;
|
||||
// 🔌 HOOK: Registrar en API
|
||||
try
|
||||
@@ -515,6 +387,319 @@ namespace EFCDesk.Classes
|
||||
|
||||
}
|
||||
|
||||
private async Task ProcesarCarpetaRaizAsync(string carpetaRaiz)
|
||||
{
|
||||
if (!Directory.Exists(carpetaRaiz))
|
||||
{
|
||||
_repo.MarcarCarpetaProcesada(carpetaRaiz, "No existe");
|
||||
Console.WriteLine($"[SKIP] No existe: {carpetaRaiz}");
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine($"Procesando raíz (ZIP único): {carpetaRaiz}");
|
||||
var nombreRaiz = Path.GetFileName(carpetaRaiz);
|
||||
var archivos = Directory.GetFiles(carpetaRaiz, "*.*", SearchOption.AllDirectories)
|
||||
.Where(f => !EsTemporal(f))
|
||||
.ToArray();
|
||||
|
||||
bool esValido = Helpers.EsValidaNomeclaturaCarpeta(nombreRaiz);
|
||||
if (!esValido)
|
||||
{
|
||||
_repo.MarcarCarpetaProcesada(carpetaRaiz, "Formato Incorrecto");
|
||||
Console.WriteLine($"[SKIP] Formato Expediente Incorrecto: {nombreRaiz}");
|
||||
return;
|
||||
}
|
||||
|
||||
if (archivos.Length == 0)
|
||||
{
|
||||
Console.WriteLine($"[INFO] Sin archivos en {carpetaRaiz}");
|
||||
_repo.MarcarCarpetaProcesada(carpetaRaiz, "Sin Archivos");
|
||||
return;
|
||||
}
|
||||
|
||||
int intento = 0;
|
||||
while (intento <= _maxReintentos)
|
||||
{
|
||||
var pendientes = archivos.Where(f => File.Exists(f) && !FileUtil.IsFileReady(f)).ToArray();
|
||||
if (pendientes.Length == 0) break;
|
||||
|
||||
if (intento == _maxReintentos)
|
||||
{
|
||||
Console.WriteLine($"[WARN] Archivos bloqueados tras {_maxReintentos} intentos. Se omite: {carpetaRaiz}");
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine($"[WAIT] {pendientes.Length} archivos bloqueados en {carpetaRaiz}. Reintento {intento + 1}/{_maxReintentos} en {_reintentoDelay.TotalMinutes} min.");
|
||||
await Task.Delay(_reintentoDelay);
|
||||
intento++;
|
||||
}
|
||||
|
||||
var archivosAProcesar = new List<string>(archivos.Length);
|
||||
await _dbSemaforo.WaitAsync();
|
||||
try
|
||||
{
|
||||
foreach (var f in archivos)
|
||||
{
|
||||
if (_repo.EstaArchivoProcesado(f)) continue;
|
||||
archivosAProcesar.Add(f);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_dbSemaforo.Release();
|
||||
}
|
||||
|
||||
if (archivosAProcesar.Count == 0)
|
||||
{
|
||||
Console.WriteLine($"[INFO] Todos los archivos ya fueron procesados en {carpetaRaiz}");
|
||||
_repo.MarcarCarpetaProcesada(carpetaRaiz, "Procesado");
|
||||
return;
|
||||
}
|
||||
|
||||
//foreach (var archivo in archivosAProcesar)
|
||||
//{
|
||||
// if (!FileUtil.WaitAllFilesReady(new[] { archivo }, _perFileTimeoutSeconds))
|
||||
// {
|
||||
// Console.WriteLine($"[SKIP] Bloqueado: {archivo}");
|
||||
// archivosAProcesar.Remove(archivo);
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// if (Helpers.ObtenerTamanoEnBytes(archivo) <= 0)
|
||||
// {
|
||||
// Console.WriteLine($"[SKIP] Sin Informacion: {archivo}");
|
||||
// archivosAProcesar.Remove(archivo);
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// if (!Helpers.ValidarArchivo(archivo))
|
||||
// {
|
||||
// Console.WriteLine($"[SKIP] Extension no valida: {archivo}");
|
||||
// archivosAProcesar.Remove(archivo);
|
||||
// continue;
|
||||
// }
|
||||
//}
|
||||
|
||||
archivosAProcesar.RemoveAll(archivo =>
|
||||
{
|
||||
if (!FileUtil.WaitAllFilesReady(new[] { archivo }, _perFileTimeoutSeconds))
|
||||
{
|
||||
Globales.logger.LogWarning($"Archivo bloqueado tras múltiples intentos: {archivo}");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (Helpers.ObtenerTamanoEnBytes(archivo) <= 0)
|
||||
{
|
||||
Globales.logger.LogWarning($"Archivo sin información: {archivo}");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!Helpers.ValidarArchivo(archivo))
|
||||
{
|
||||
Globales.logger.LogWarning($"Archivo con extensión no válida: {archivo}");
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
|
||||
if (archivosAProcesar.Count == 0)
|
||||
{
|
||||
_repo.MarcarCarpetaProcesada(carpetaRaiz, "Procesado");
|
||||
return;
|
||||
}
|
||||
|
||||
string sfuente = "";
|
||||
int expedienteLogistico = Properties.Settings.Default.ExpLogistico;
|
||||
int expedienteWinsaai = Properties.Settings.Default.ExpWinsaai;
|
||||
|
||||
if (expedienteWinsaai > 0 && expedienteLogistico > 0)
|
||||
{
|
||||
sfuente = Properties.Settings.Default.ExpLogistico.ToString();
|
||||
}
|
||||
else if (expedienteWinsaai > 0)
|
||||
{
|
||||
sfuente = Properties.Settings.Default.ExpWinsaai.ToString();
|
||||
}
|
||||
else if (expedienteLogistico > 0)
|
||||
{
|
||||
sfuente = Properties.Settings.Default.ExpLogistico.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
sfuente = "5";
|
||||
}
|
||||
|
||||
Dictionary<string, string> metadatos = new Dictionary<string, string>
|
||||
{
|
||||
{ "fuente", sfuente },
|
||||
{ "clave_pedimento", "" },
|
||||
{ "pedimento", "" },
|
||||
{ "patente", "" },
|
||||
{ "aduana", "" },
|
||||
{ "tipo_operacion", "" },
|
||||
{ "contribuyente", "" },
|
||||
{ "curp_apoderado", "" },
|
||||
{ "fecha_pago", "" },
|
||||
{ "partidas", "" },
|
||||
};
|
||||
|
||||
bool metadatosCompletos = false;
|
||||
foreach (var archivo in archivosAProcesar)
|
||||
{
|
||||
if (Helpers.EsArchivoXMl(archivo) && !metadatosCompletos)
|
||||
{
|
||||
try
|
||||
{
|
||||
string xml = File.ReadAllText(archivo);
|
||||
var parser = new PedimentoXmlParser(xml);
|
||||
|
||||
var (tieneError, numeroOperacion) = parser.GetResultadoOperacion();
|
||||
|
||||
string numeroPedimento = parser.GetNumeroPedimento();
|
||||
var encabezado = parser.GetEncabezado();
|
||||
var importador = parser.GetImportador();
|
||||
var fechas = parser.GetFechas();
|
||||
var partidas = parser.GetPartidas();
|
||||
|
||||
if (!tieneError && encabezado.Count > 0)
|
||||
{
|
||||
string[] partes = nombreRaiz.Split("-");
|
||||
|
||||
metadatos["pedimento"] = numeroPedimento;
|
||||
metadatos["clave_pedimento"] = encabezado.ContainsKey("ClavePedimento") ? encabezado["ClavePedimento"] : "";
|
||||
metadatos["patente"] = partes.Length >= 3 ? partes[2] : "";
|
||||
metadatos["aduana"] = encabezado.ContainsKey("AduanaES") ? encabezado["AduanaES"] : "";
|
||||
metadatos["tipo_operacion"] = encabezado.ContainsKey("TipoOperacion") ? encabezado["TipoOperacion"] : "";
|
||||
metadatos["contribuyente"] = importador.ContainsKey("RFC") ? importador["RFC"] : "";
|
||||
metadatos["curp_apoderado"] = encabezado.ContainsKey("CurpApoderado") ? encabezado["CurpApoderado"] : "";
|
||||
metadatos["partidas"] = partidas.Count.ToString();
|
||||
|
||||
foreach (var fecha in fechas)
|
||||
{
|
||||
if (fecha.ContainsKey("TipoClave") && fecha["TipoClave"] == "2" && fecha.ContainsKey("Fecha"))
|
||||
{
|
||||
string[] partesFecha = fecha["Fecha"].Split("-");
|
||||
metadatos["fecha_pago"] = partesFecha[0] + "-" + partesFecha[1] + "-" + partesFecha[2];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
metadatosCompletos = true;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Console.WriteLine($"[WARN] Error al parsear XML {archivo}: {ex.Message}");
|
||||
Globales.logger.LogError($"Error al parsear XML {archivo}", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var rutaZip = Path.Combine(Path.GetDirectoryName(carpetaRaiz)!, $"{nombreRaiz}.zip");
|
||||
// if (File.Exists(rutaZip)) File.Delete(rutaZip);
|
||||
|
||||
_repo.MarcarCarpetaProcesada(carpetaRaiz, "Procesando");
|
||||
|
||||
string[] archivosParaZip = archivosAProcesar.ToArray();
|
||||
rutaZip = Helpers.CrearZipMultipleArchivos(archivosParaZip, $"{nombreRaiz}.zip");
|
||||
|
||||
if (string.IsNullOrEmpty(rutaZip) || !File.Exists(rutaZip))
|
||||
{
|
||||
Globales.logger.LogError($"No se pudo crear el ZIP para {carpetaRaiz}");
|
||||
_repo.MarcarCarpetaProcesada(carpetaRaiz, "Pendiente");
|
||||
return;
|
||||
}
|
||||
|
||||
bool respuesta = false;
|
||||
try
|
||||
{
|
||||
var apiClient = Globales.ApiClientLarge;
|
||||
string UrlPostArchivoZip = DominioEFC + ApiGetPedimentoDesk;
|
||||
|
||||
string respuestaJson = await apiClient.PostMultipartZipAsync(
|
||||
url: UrlPostArchivoZip,
|
||||
filePath: rutaZip,
|
||||
zipFileName: $"{nombreRaiz}.zip",
|
||||
metadatos: metadatos
|
||||
);
|
||||
|
||||
if (!apiClient.IsJson(respuestaJson) || !apiClient.HasJsonData(respuestaJson))
|
||||
{
|
||||
Globales.logger.LogError($"Respuesta inválida de API para {carpetaRaiz}: {respuestaJson}");
|
||||
_repo.MarcarCarpetaProcesada(carpetaRaiz, "Error API");
|
||||
return;
|
||||
}
|
||||
|
||||
if (JsonHelper.TryParse(respuestaJson, out var root))
|
||||
{
|
||||
var JsonData = JsonHelper.ToDictionary(root);
|
||||
|
||||
if (JsonData != null && JsonData.ContainsKey("tieneError"))
|
||||
{
|
||||
bool tieneError = Convert.ToBoolean(JsonData["tieneError"]);
|
||||
if (tieneError)
|
||||
{
|
||||
string mensaje = Convert.ToString(JsonData["mensaje"]) ?? "Error desconocido";
|
||||
Globales.logger.LogError($"API respondió con error para {carpetaRaiz}: {mensaje}");
|
||||
_repo.MarcarCarpetaProcesada(carpetaRaiz, "Error API");
|
||||
return;
|
||||
}
|
||||
|
||||
string taskId = Convert.ToString(JsonData["task_id"]) ?? "";
|
||||
if (!string.IsNullOrEmpty(taskId))
|
||||
{
|
||||
Globales.logger.LogInfo($"Archivo ZIP enviado a API para {carpetaRaiz}, task_id: {taskId}");
|
||||
_repo.GuardarTaskID(carpetaRaiz, taskId);
|
||||
}
|
||||
|
||||
respuesta = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Globales.logger.LogError($"Error al registrar en API para {carpetaRaiz}", ex);
|
||||
_repo.MarcarCarpetaProcesada(carpetaRaiz, "Error API");
|
||||
}
|
||||
finally
|
||||
{
|
||||
string tempFolder = Path.GetDirectoryName(rutaZip) ?? "";
|
||||
if (Directory.Exists(tempFolder))
|
||||
{
|
||||
try { Directory.Delete(tempFolder, true); } catch { }
|
||||
}
|
||||
}
|
||||
|
||||
if (respuesta)
|
||||
{
|
||||
await _dbSemaforo.WaitAsync();
|
||||
try
|
||||
{
|
||||
foreach (var archivo in archivosAProcesar)
|
||||
{
|
||||
var nombreArchivo = Path.GetFileName(archivo);
|
||||
var sha1 = FileUtil.TryGetSHA1(archivo);
|
||||
_repo.RegistrarArchivoProcesado(archivo, nombreArchivo, carpetaRaiz, $"{nombreRaiz}.zip", sha1);
|
||||
_repo.ActualizarEstadoArchivo(archivo, "Procesado");
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_dbSemaforo.Release();
|
||||
}
|
||||
|
||||
_repo.MarcarCarpetaProcesada(carpetaRaiz, "Procesado");
|
||||
Globales.logger.LogInfo($"Proceso Completado (ZIP único): {carpetaRaiz} - {archivosAProcesar.Count} archivos");
|
||||
}
|
||||
else
|
||||
{
|
||||
_repo.MarcarCarpetaProcesada(carpetaRaiz, "Pendiente");
|
||||
Globales.logger.LogError($"Proceso Fallido (ZIP único) para {carpetaRaiz}");
|
||||
}
|
||||
}
|
||||
|
||||
private static bool EsTemporal(string path)
|
||||
{
|
||||
var name = Path.GetFileName(path);
|
||||
@@ -529,15 +714,7 @@ namespace EFCDesk.Classes
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Implementa tus integraciones reales aquí
|
||||
private void SubirArchivoFTP(string rutaZip)
|
||||
{
|
||||
// TODO: tu implementación real de FTP
|
||||
Console.WriteLine($"[FTP] Subido: {rutaZip}");
|
||||
}
|
||||
|
||||
private async Task<bool> RegistrarArchivoEnAPI(string rutaArchivo, string pedimento)
|
||||
private async Task<bool> RegistrarArchivoEnAPI(string rutaArchivo, string pedimento, Dictionary<string, string>? metadatosPrecalculados = null)
|
||||
{
|
||||
// TODO: tu implementación real de API
|
||||
try
|
||||
@@ -554,7 +731,197 @@ namespace EFCDesk.Classes
|
||||
|
||||
if (!string.IsNullOrEmpty(Token))
|
||||
{
|
||||
using var apiClient = new ApiClient(timeout: TimeSpan.FromSeconds(600), maxRetries: 3, retryDelay: TimeSpan.FromSeconds(60));
|
||||
var apiClient = Globales.ApiClientLarge;
|
||||
|
||||
string sfuente = "";
|
||||
int expedienteLogistico = Properties.Settings.Default.ExpLogistico;
|
||||
int expedienteWinsaai = Properties.Settings.Default.ExpWinsaai;
|
||||
|
||||
if (expedienteWinsaai > 0 && expedienteLogistico > 0)
|
||||
{
|
||||
sfuente = Properties.Settings.Default.ExpLogistico.ToString();
|
||||
|
||||
}
|
||||
else if (expedienteWinsaai > 0)
|
||||
{
|
||||
sfuente = Properties.Settings.Default.ExpWinsaai.ToString();
|
||||
}
|
||||
else if (expedienteLogistico > 0)
|
||||
{
|
||||
sfuente = Properties.Settings.Default.ExpLogistico.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
sfuente = "5";
|
||||
}
|
||||
|
||||
// Verifica que el archivo exista
|
||||
if (!File.Exists(rutaArchivo))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Dictionary<string, string> metadatos;
|
||||
|
||||
if (metadatosPrecalculados != null)
|
||||
{
|
||||
metadatos = metadatosPrecalculados;
|
||||
}
|
||||
else
|
||||
{
|
||||
metadatos = new Dictionary<string, string>
|
||||
{
|
||||
{ "fuente", sfuente },
|
||||
{ "clave_pedimento", "" },
|
||||
{ "pedimento", "" },
|
||||
{ "patente", "" },
|
||||
{ "aduana", "" },
|
||||
{ "tipo_operacion", "" },
|
||||
{ "contribuyente", "" },
|
||||
{ "curp_apoderado", "" },
|
||||
{ "fecha_pago", "" },
|
||||
{ "partidas", "" },
|
||||
};
|
||||
|
||||
if (Helpers.EsArchivoXMl(rutaArchivo))
|
||||
{
|
||||
string xml = File.ReadAllText(rutaArchivo);
|
||||
var parser = new PedimentoXmlParser(xml);
|
||||
|
||||
var (tieneError, numeroOperacion) = parser.GetResultadoOperacion();
|
||||
|
||||
string numeroPedimento = parser.GetNumeroPedimento();
|
||||
var encabezado = parser.GetEncabezado();
|
||||
var importador = parser.GetImportador();
|
||||
var fechas = parser.GetFechas();
|
||||
var partidas = parser.GetPartidas();
|
||||
|
||||
if (!tieneError && encabezado.Count > 0)
|
||||
{
|
||||
|
||||
string[] partes = pedimento.Split("-");
|
||||
|
||||
metadatos["pedimento"] = numeroPedimento;
|
||||
metadatos["clave_pedimento"] = encabezado["ClavePedimento"];
|
||||
metadatos["patente"] = partes[2];
|
||||
metadatos["aduana"] = encabezado["AduanaES"];
|
||||
metadatos["tipo_operacion"] = encabezado["TipoOperacion"];
|
||||
metadatos["contribuyente"] = importador["RFC"];
|
||||
metadatos["curp_apoderado"] = encabezado["CurpApoderado"];
|
||||
metadatos["partidas"] = partidas.Count.ToString();
|
||||
|
||||
foreach (var fecha in fechas)
|
||||
{
|
||||
if (fecha["TipoClave"] == "2")
|
||||
{
|
||||
string[] partesFecha = fecha["Fecha"].Split("-");
|
||||
metadatos["fecha_pago"] = partesFecha[0] + "-" + partesFecha[1] + "-" + partesFecha[2];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
string nombreZip = $"{pedimento}.zip";
|
||||
string rutaZip = Helpers.CrearZipArchivoEnTemp(rutaArchivo, nombreZip);
|
||||
|
||||
string UrlPostArchivoZip = DominioEFC + ApiGetPedimentoDesk;
|
||||
string respuestaJson = await apiClient.PostMultipartZipAsync(
|
||||
url: UrlPostArchivoZip,
|
||||
filePath: rutaZip,
|
||||
zipFileName: nombreZip,
|
||||
metadatos: metadatos
|
||||
);
|
||||
|
||||
string tempFolder = Path.GetDirectoryName(rutaZip) ?? "";
|
||||
|
||||
// Borra toda la carpeta temporal
|
||||
if (Directory.Exists(tempFolder))
|
||||
{
|
||||
Directory.Delete(tempFolder, true);
|
||||
}
|
||||
|
||||
if (!apiClient.IsJson(respuestaJson))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!apiClient.HasJsonData(respuestaJson))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (JsonHelper.TryParse(respuestaJson, out var root))
|
||||
{
|
||||
var JsonData = JsonHelper.ToDictionary(root);
|
||||
|
||||
if (JsonData == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if (JsonData.ContainsKey("tieneError"))
|
||||
{
|
||||
bool tieneError = Convert.ToBoolean(JsonData["tieneError"]);
|
||||
if (tieneError)
|
||||
{
|
||||
string mensaje = Convert.ToString(JsonData["mensaje"]) ?? "Error desconocido";
|
||||
|
||||
logger.LogError($"Se presentan errores al intentar subir los documentos del pedimento {pedimento}");
|
||||
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogError("Error en la configuracion del Id de Usuario:" + DominioEFC, new Exception("Error Id Usuario"));
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError("Error inesperado SubirArchivoApiAsync", ex);
|
||||
return false;
|
||||
}
|
||||
|
||||
Console.WriteLine($"[API] Registrado: {rutaArchivo}");
|
||||
return true;
|
||||
}
|
||||
|
||||
private async Task<bool> RegistrarArchivoEnAPI_Original(string rutaArchivo, string pedimento)
|
||||
{
|
||||
// TODO: tu implementación real de API
|
||||
try
|
||||
{
|
||||
var config = ConfiguracionJSON.LoadFromJson();
|
||||
string Token = Globales.accesToken ?? "";
|
||||
|
||||
|
||||
if (string.IsNullOrWhiteSpace(DominioEFC))
|
||||
{
|
||||
logger.LogError("No se ha configurado el dominio", null);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(Token))
|
||||
{
|
||||
var apiClient = Globales.ApiClientLarge;
|
||||
|
||||
string sfuente = "";
|
||||
int expedienteLogistico = Properties.Settings.Default.ExpLogistico;
|
||||
@@ -598,91 +965,6 @@ namespace EFCDesk.Classes
|
||||
{ "partidas", "" },
|
||||
};
|
||||
|
||||
//if (Helpers.EsValidaNomeclaturaSAAIM3(rutaArchivo))
|
||||
//{
|
||||
// // Validar contenido del archivo SAAI M3
|
||||
|
||||
// var registros = SaaiM3Helper.LeerArchivo(rutaArchivo);
|
||||
// var resultados = SaaiM3Helper.BuscarPorTipo(registros, "501");
|
||||
|
||||
// foreach (var reg in resultados)
|
||||
// {
|
||||
// metadatos["clave_pedimento"] = reg.Campos[5];
|
||||
// metadatos["pedimento"] = reg.Campos[2];
|
||||
// metadatos["patente"] = reg.Campos[1];
|
||||
// metadatos["aduana"] = reg.Campos[3];
|
||||
// metadatos["tipo_operacion"] = reg.Campos[4];
|
||||
// metadatos["contribuyente"] = reg.Campos[8];
|
||||
// metadatos["curp_apoderado"] = reg.Campos[9];
|
||||
|
||||
// break;
|
||||
// }
|
||||
|
||||
// resultados = SaaiM3Helper.BuscarPorTipo(registros, "506");
|
||||
|
||||
// foreach (var reg in resultados)
|
||||
// {
|
||||
// string tipoFechaPago = reg.Campos[2];
|
||||
// if (tipoFechaPago == "2")
|
||||
// {
|
||||
// string fechaOriginal = reg.Campos[3];
|
||||
// DateTime fecha = DateTime.ParseExact(fechaOriginal, "ddMMyyyy", null);
|
||||
|
||||
// metadatos["fecha_pago"] = fecha.ToString("yyyy-MM-dd");
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
|
||||
// // Numero de partidas
|
||||
// resultados = SaaiM3Helper.BuscarPorTipo(registros, "551");
|
||||
// metadatos["partidas"] = resultados.Count.ToString();
|
||||
//}
|
||||
//else if (Helpers.EsArchivoXMl(rutaArchivo))
|
||||
//{
|
||||
|
||||
// //
|
||||
|
||||
// // Validar contenido del XML Pedimento Completo
|
||||
// string xml = File.ReadAllText(rutaArchivo);
|
||||
// var parser = new PedimentoXmlParser(xml);
|
||||
|
||||
// var (tieneError, numeroOperacion) = parser.GetResultadoOperacion();
|
||||
|
||||
// string numeroPedimento = parser.GetNumeroPedimento();
|
||||
// var encabezado = parser.GetEncabezado();
|
||||
// var importador = parser.GetImportador();
|
||||
// var fechas = parser.GetFechas();
|
||||
// var partidas = parser.GetPartidas();
|
||||
|
||||
// if (!tieneError && encabezado.Count > 0)
|
||||
// {
|
||||
|
||||
// string[] partes = pedimento.Split("-");
|
||||
|
||||
// metadatos["pedimento"] = numeroPedimento;
|
||||
// metadatos["clave_pedimento"] = encabezado["ClavePedimento"];
|
||||
// metadatos["patente"] = partes[2];
|
||||
// metadatos["aduana"] = encabezado["AduanaES"];
|
||||
// metadatos["tipo_operacion"] = encabezado["TipoOperacion"];
|
||||
// metadatos["contribuyente"] = importador["RFC"];
|
||||
// metadatos["curp_apoderado"] = encabezado["CurpApoderado"]; ;
|
||||
// metadatos["partidas"] = partidas.Count.ToString();
|
||||
|
||||
// foreach (var fecha in fechas)
|
||||
// {
|
||||
// if (fecha["TipoClave"] == "2")
|
||||
// {
|
||||
// string[] partesFecha = fecha["Fecha"].Split("-");
|
||||
// metadatos["fecha_pago"] = partesFecha[0] + "-" + partesFecha[1] + "-" + partesFecha[2];
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
|
||||
//}
|
||||
|
||||
if (Helpers.EsArchivoXMl(rutaArchivo))
|
||||
{
|
||||
|
||||
@@ -770,6 +1052,10 @@ namespace EFCDesk.Classes
|
||||
bool tieneError = Convert.ToBoolean(JsonData["tieneError"]);
|
||||
if (tieneError)
|
||||
{
|
||||
string mensaje = Convert.ToString(JsonData["mensaje"]) ?? "Error desconocido";
|
||||
|
||||
logger.LogError($"Se presentan errores al intentar subir los documentos del pedimento {pedimento}");
|
||||
|
||||
return false;
|
||||
}
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user