Files
EFC-DESK-V2/Parsers/PedimentoXmlParser.cs
2026-02-09 10:55:45 -07:00

213 lines
7.5 KiB
C#

using EFCDesk.HelpersXml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Xml.Linq;
namespace EFCDesk.Parsers
{
public class PedimentoXmlParser
{
private readonly XDocument _doc;
private readonly XmlNamespaces _ns = new();
public PedimentoXmlParser(string xml)
{
_doc = XDocument.Parse(xml);
}
private XElement? Pedimento => _doc
.Element(_ns.S + "Envelope")
?.Element(_ns.S + "Body")
?.Element(_ns.ns2 + "consultarPedimentoCompletoRespuesta")
?.Element(_ns.ns2 + "pedimento");
// NUmero Pedimento
public string GetNumeroPedimento()
{
if (Pedimento == null)
return string.Empty;
return Pedimento.Element(_ns.ns2 + "pedimento")?.Value ?? string.Empty;
}
// Respuesta
public (bool TieneError, string NumeroOperacion) GetResultadoOperacion()
{
var respuesta =
_doc
.Element(_ns.S + "Envelope")
?.Element(_ns.S + "Body")
?.Element(_ns.ns2 + "consultarPedimentoCompletoRespuesta");
if (respuesta == null)
return (false, string.Empty);
bool tieneError =
bool.TryParse(
respuesta.Element(_ns.ns3 + "tieneError")?.Value,
out var error
) && error;
string numeroOperacion =
respuesta.Element(_ns.ns2 + "numeroOperacion")?.Value?.Trim()
?? string.Empty;
return (tieneError, numeroOperacion);
}
// Encabezado
public Dictionary<string, string> GetEncabezado()
{
var enc = Pedimento?.Element(_ns.ns2 + "encabezado");
if (enc == null) return new();
return new Dictionary<string, string>
{
["TipoOperacion"] =
XmlHelper.Value(enc.Element(_ns.ns2 + "tipoOperacion"), _ns.ns2 + "clave"),
["ClavePedimento"] =
XmlHelper.Value(enc.Element(_ns.ns2 + "claveDocumento"), _ns.ns2 + "clave"),
["Destino"] =
XmlHelper.Value(enc.Element(_ns.ns2 + "destino"), _ns.ns2 + "descripcion"),
["AduanaES"] =
XmlHelper.Value(enc.Element(_ns.ns2 + "aduanaEntradaSalida"), _ns.ns2 + "clave"),
["TipoCambio"] =
XmlHelper.Value(enc, _ns.ns2 + "tipoCambio"),
["PesoBruto"] =
XmlHelper.Value(enc, _ns.ns2 + "pesoBruto"),
["ValorDolares"] =
XmlHelper.Value(enc, _ns.ns2 + "valorDolares"),
["ValorAduanalTotal"] =
XmlHelper.Value(enc, _ns.ns2 + "valorAduanalTotal"),
["ValorComercialTotal"] =
XmlHelper.Value(enc, _ns.ns2 + "valorComercialTotal"),
["CurpApoderado"] =
XmlHelper.Value(enc, _ns.ns2 + "curpApoderadomandatario")
};
}
// Importador / Exportador
public Dictionary<string, string> GetImportador()
{
var imp = Pedimento?.Element(_ns.ns2 + "importadorExportador");
if (imp == null) return new();
var dom = imp.Element(_ns.ns2 + "domicilio");
return new Dictionary<string, string>
{
["RFC"] = XmlHelper.Value(imp, _ns.ns2 + "rfc"),
["RazonSocial"] = XmlHelper.Value(imp, _ns.ns2 + "razonSocial"),
["Calle"] = XmlHelper.Value(dom, _ns.ns2 + "calle"),
["NumeroExterior"] = XmlHelper.Value(dom, _ns.ns2 + "numeroExterior"),
["Ciudad"] = XmlHelper.Value(dom, _ns.ns2 + "ciudadMunicipio"),
["CodigoPostal"] = XmlHelper.Value(dom, _ns.ns2 + "codigoPostal"),
["Pais"] = XmlHelper.Value(imp.Element(_ns.ns2 + "pais"), "descripcion")
};
}
// Fechas
public List<Dictionary<string, string>> GetFechas()
{
var imp = Pedimento?.Element(_ns.ns2 + "importadorExportador");
if (imp == null) return new();
var fechas = imp.Elements(_ns.ns2 + "fechas");
return fechas.Select(f => new Dictionary<string, string>
{
["Fecha"] = XmlHelper.Value(f, _ns.ns2 + "fecha"),
["TipoClave"] = XmlHelper.Value(f.Element(_ns.ns2 + "tipo"), _ns.ns2 + "clave"),
["TipoDescripcion"] = XmlHelper.Value(f.Element(_ns.ns2 + "tipo"), _ns.ns2 + "descripcion")
}).ToList();
}
// Tasas
public List<Dictionary<string, string>> GetTasas()
{
var tasas = XmlHelper.Elements(Pedimento, _ns.ns2 + "tasas");
return tasas.Select(t => new Dictionary<string, string>
{
["ContribucionClave"] =
XmlHelper.Value(t.Element(_ns.ns2 + "contribucion"), _ns.ns2 + "clave"),
["ContribucionDescripcion"] =
XmlHelper.Value(t.Element(_ns.ns2 + "contribucion"), _ns.ns2 + "descripcion"),
["TipoTasa"] =
XmlHelper.Value(t.Element(_ns.ns2 + "tipoTasa"), "descripcion"),
["TasaAplicable"] =
XmlHelper.Value(t, _ns.ns2 + "tasaAplicable"),
["FormaPago"] =
XmlHelper.Value(t.Element(_ns.ns2 + "formaPago"), "descripcion"),
["Importe"] =
XmlHelper.Value(t, _ns.ns2 + "importe")
}).ToList();
}
// Facturas
public List<Dictionary<string, string>> GetFacturas()
{
var facturas = XmlHelper.Elements(Pedimento, _ns.ns2 + "facturas");
return facturas.Select(f => new Dictionary<string, string>
{
["Fecha"] = XmlHelper.Value(f, _ns.ns2 + "fecha"),
["Numero"] = XmlHelper.Value(f, _ns.ns2 + "numero"),
["Moneda"] = XmlHelper.Value(f.Element(_ns.ns2 + "moneda"), _ns.ns2 + "descripcion"),
["ValorDolares"] = XmlHelper.Value(f, _ns.ns2 + "valorDolares"),
["Proveedor"] = XmlHelper.Value(f, _ns.ns2 + "proveedorComprador")
}).ToList();
}
// Identificadores
public List<Dictionary<string, string>> GetIdentificadores()
{
var root = Pedimento?.Element(_ns.ns2 + "identificadores");
if (root == null) return new();
return root.Elements(_ns.ns2 + "identificadores")
.Select(i => new Dictionary<string, string>
{
["Clave"] =
XmlHelper.Value(i.Element("claveIdentificador"), "clave"),
["Descripcion"] =
XmlHelper.Value(i.Element("claveIdentificador"), "descripcion"),
["Complemento1"] =
XmlHelper.Value(i, "complemento1")
}).ToList();
}
// Partidas
public List<string> GetPartidas()
{
if (Pedimento == null)
return new List<string>();
return Pedimento
.Elements(_ns.ns2 + "partidas")
.Select(p => p.Value?.Trim())
.Where(v => !string.IsNullOrEmpty(v))
.ToList()!;
}
}
}