public Tuple <string, string, int> CrearArchivoParaTimbrar(List <VENTA> Ventas, int IdNotaDeVenta, string Folio, string FormaDePago, string MetodoDePago, string Rfc, string Nombre, string Calle, string NumeroExterior, string NumeroInterior, string Colonia, string Municipio, string Estado, string Pais, string CodigoPostal) { string Fecha = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss"); System.IO.FileInfo fi = new System.IO.FileInfo(System.Web.HttpContext.Current.Server.MapPath("~/ARCHIVOS_GENERADOS") + "/PLANTILLAS/comprobanteSinTimbrar.xml"); XmlDocument doc = new XmlDocument(); doc.Load(fi.FullName); XmlNode root = doc.DocumentElement; root.Attributes["folio"].Value = Folio; root.Attributes["fecha"].Value = Fecha; root.Attributes["formaDePago"].Value = FormaDePago; root.Attributes["subTotal"].Value = Ventas.Sum(x => x.Importe).ToString(); root.Attributes["Moneda"].Value = "MXN"; root.Attributes["total"].Value = Ventas.Select(x => x.Importe + (x.Importe * x.P_IEPS) + (x.Importe * x.P_ISH) + (x.Importe * x.P_IVA)).Sum().ToString(); root.Attributes["tipoDeComprobante"].Value = "ingreso"; root.Attributes["metodoDePago"].Value = MetodoDePago; root.ChildNodes[1].Attributes["rfc"].Value = Rfc; root.ChildNodes[1].Attributes["nombre"].Value = Nombre; XmlElement element; foreach (var item in Ventas) { element = doc.CreateElement("cfdi:Concepto", "http://www.sat.gob.mx/cfd/3"); XmlAttribute attcantidad = doc.CreateAttribute("cantidad"); attcantidad.Value = item.Cantidad.ToString(); element.Attributes.Append(attcantidad); XmlAttribute attunidad = doc.CreateAttribute("unidad"); attunidad.Value = item.Unidad.ToString(); element.Attributes.Append(attunidad); XmlAttribute attnoIdentificacion = doc.CreateAttribute("noIdentificacion"); attnoIdentificacion.Value = item.Codigo.ToString(); element.Attributes.Append(attnoIdentificacion); XmlAttribute attdescripcion = doc.CreateAttribute("descripcion"); attdescripcion.Value = item.Descripcion.ToString(); element.Attributes.Append(attdescripcion); XmlAttribute attvalorUnitario = doc.CreateAttribute("valorUnitario"); attvalorUnitario.Value = item.Precio.ToString(); element.Attributes.Append(attvalorUnitario); XmlAttribute attimporte = doc.CreateAttribute("importe"); attimporte.Value = item.Importe.ToString(); element.Attributes.Append(attimporte); root.ChildNodes[2].AppendChild(element); } var elementtraslados = doc.CreateElement("cfdi:Impuestos", "http://www.sat.gob.mx/cfd/3"); root.AppendChild(elementtraslados); if (Ventas.Any(x => x.P_IEPS > 0 | x.P_IVA > 0 | x.P_ISH > 0)) { XmlAttribute atttotaltraslados = doc.CreateAttribute("totalImpuestosTrasladados"); atttotaltraslados.Value = Ventas.Select(x => x.P_IEPS + x.P_ISH + x.P_IVA).Sum().ToString(); elementtraslados.Attributes.Append(atttotaltraslados); var elementitemtraslados = doc.CreateElement("cfdi:Traslados", "http://www.sat.gob.mx/cfd/3"); elementtraslados.AppendChild(elementitemtraslados); foreach (var item in Ventas.Where(x => x.P_IEPS > 0).Select(x => new { x.P_IEPS, Total = x.Importe * x.P_IEPS }).Distinct()) { var elemenieps = doc.CreateElement("cfdi:Traslado", "http://www.sat.gob.mx/cfd/3"); XmlAttribute attriepsimpuesto = doc.CreateAttribute("impuesto"); attriepsimpuesto.Value = "IEPS"; elemenieps.Attributes.Append(attriepsimpuesto); XmlAttribute attriepstasa = doc.CreateAttribute("tasa"); attriepstasa.Value = (item.P_IEPS * 100).ToString(); elemenieps.Attributes.Append(attriepstasa); XmlAttribute attriepsimporte = doc.CreateAttribute("importe"); attriepsimporte.Value = item.Total.ToString(); elemenieps.Attributes.Append(attriepsimporte); elementitemtraslados.AppendChild(elemenieps); } foreach (var item in Ventas.Where(x => x.P_IVA > 0).Select(x => new { x.P_IVA, Total = x.Importe * x.P_IVA }).Distinct()) { var elemenieps = doc.CreateElement("cfdi", "Traslado"); XmlAttribute attriepsimpuesto = doc.CreateAttribute("impuesto"); attriepsimpuesto.Value = "IVA"; elemenieps.Attributes.Append(attriepsimpuesto); XmlAttribute attriepstasa = doc.CreateAttribute("tasa"); attriepstasa.Value = (item.P_IVA * 100).ToString(); elemenieps.Attributes.Append(attriepstasa); XmlAttribute attriepsimporte = doc.CreateAttribute("importe"); attriepsimporte.Value = item.Total.ToString(); elemenieps.Attributes.Append(attriepsimporte); elementitemtraslados.AppendChild(elemenieps); } foreach (var item in Ventas.Where(x => x.P_ISH > 0).Select(x => new { x.P_ISH, Total = x.Importe * x.P_ISH }).Distinct()) { var elemenieps = doc.CreateElement("cfdi", "Traslado"); XmlAttribute attriepsimpuesto = doc.CreateAttribute("impuesto"); attriepsimpuesto.Value = "ISH"; elemenieps.Attributes.Append(attriepsimpuesto); XmlAttribute attriepstasa = doc.CreateAttribute("tasa"); attriepstasa.Value = (item.P_ISH * 100).ToString(); elemenieps.Attributes.Append(attriepstasa); XmlAttribute attriepsimporte = doc.CreateAttribute("importe"); attriepsimporte.Value = item.Total.ToString(); elemenieps.Attributes.Append(attriepsimporte); elementitemtraslados.AppendChild(elemenieps); } } doc.Save(System.Web.HttpContext.Current.Server.MapPath("~/ARCHIVOS_GENERADOS") + "/" + Fecha.Replace(":", "_") + ".xml"); CONSULTAS sql = new CONSULTAS(); CONEXION C = new CONEXION(); string sentencia = string.Empty; int idfactura = (int)(C.EjecutarEscalar(sql.AGREGAR_FACTURA("", FormaDePago, Ventas.Select(x => (double)x.Importe).Sum(), IdNotaDeVenta, Rfc, Folio, Nombre, "MXN", Ventas.Select(x => x.Importe + (x.Importe * x.P_IEPS) + (x.Importe * x.P_ISH) + (x.Importe * x.P_IVA)).Sum(), MetodoDePago))); Nullable <int> creados = C.EjecutarSentencia(sql.AGREGAR_FACTURACION(IdNotaDeVenta, idfactura)); return(new Tuple <string, string, int>( (System.Web.HttpContext.Current.Server.MapPath("~/ARCHIVOS_GENERADOS") + "/" + Fecha.Replace(":", "_") + ".xml"), Folio, idfactura)); }
public object CrearNotaDeVenta(List <VENTA> Ventas, string Rfc, string FormaDePago, string DatosDePago) { CONFIGURACIONES configuracion = new CONFIGURACIONES(); CONSULTAS sql = new CONSULTAS(); CONEXION C = new CONEXION(); string sentencia = string.Empty; int idnotadeventa = int.Parse(C.EjecutarEscalar(sql.AGREGAR_NOTA_DE_VENTA()).ToString()); TIMBRADO timbradoactual = null; foreach (var venta in Ventas) { C.EjecutarSentencia(sql.AGREGAR_VENTAS(idnotadeventa, venta.Codigo, venta.Cantidad, venta.Unidad, venta.Descripcion, venta.Precio, venta.P_IEPS, venta.P_IVA, venta.P_ISH)); } if (!string.IsNullOrEmpty(Rfc)) { sentencia = sql.OBTENER_CLIENTE_POR_RFC(Rfc); DataTable dt = C.EjecutarConsulta(sentencia); var res = dt.AsEnumerable() .Select(x => new { RFC = x.Field <string>("RFC"), RAZON_SOCIAL = x.Field <string>("RAZON_SOCIAL"), CALLE = x.Field <string>("CALLE"), NUMERO_INTERIOR = x.Field <string>("NUMERO_INTERIOR"), NUMERO_EXTERIOR = x.Field <string>("NUMERO_EXTERIOR"), COLONIA = x.Field <string>("COLONIA"), DELEGACION_MUNICIPIO = x.Field <string>("DELEGACION_MUNICIPIO"), LOCALIDAD = x.Field <string>("LOCALIDAD"), ESTADO = x.Field <string>("ESTADO"), PAIS = x.Field <string>("PAIS"), CODIGO_POSTAL = x.Field <string>("CODIGO_POSTAL"), CORREO_ELECTRONICO = x.Field <string>("CORREO_ELECTRONICO") }).ToList(); if (res.Count > 0) { VALORACION_DE_TIMBRES t = new VALORACION_DE_TIMBRES(); Random mirandom = new Random(); timbradoactual = t.TimbrarVenta(Ventas, idnotadeventa, configuracion.Consumidor, "Pago en una sola exhibicion", FormaDePago, res[0].RFC, res[0].RAZON_SOCIAL, res[0].CALLE, res[0].NUMERO_EXTERIOR, res[0].NUMERO_INTERIOR, res[0].COLONIA, res[0].DELEGACION_MUNICIPIO, res[0].ESTADO, res[0].PAIS, res[0].CODIGO_POSTAL); int idfactura = (int)(C.EjecutarEscalar(sql.AGREGAR_FACTURA("", FormaDePago, Ventas.Select(x => (double)x.Importe).Sum(), idnotadeventa, Rfc, timbradoactual.Folio.ToString(), res[0].RAZON_SOCIAL, "MXN", Ventas.Select(x => x.Importe + (x.Importe * x.P_IEPS) + (x.Importe * x.P_ISH) + (x.Importe * x.P_IVA)).Sum(), FormaDePago))); //Nullable<int> creados = C.EjecutarSentencia(sql.AGREGAR_FACTURACION(IdNotaDeVenta, idfactura)); C.EjecutarSentencia(sql.ACTUALIZAR_fACTURA(idfactura, timbradoactual.Descripcion)); GENERACION_DE_REPORTES gr = new GENERACION_DE_REPORTES(); System.IO.FileInfo fi = new System.IO.FileInfo(timbradoactual.RutaDeArchivo); System.IO.File.WriteAllText(fi.FullName.Replace(fi.Extension, "_SAT.xml"), timbradoactual.Xml); System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); xmlDoc.LoadXml(timbradoactual.Xml); gr.GenerarFactura(fi.Name.Replace(fi.Extension, string.Empty), Ventas, res[0].RFC, res[0].RAZON_SOCIAL, string.Format("CALLE {0}, # EXTERIOR {1}, #INTERIOR {2}, COLONIA {3}", res[0].CALLE, res[0].NUMERO_EXTERIOR, res[0].NUMERO_INTERIOR, res[0].COLONIA), res[0].DELEGACION_MUNICIPIO, res[0].CODIGO_POSTAL, xmlDoc.ChildNodes[1].Attributes["folio"].Value, xmlDoc.ChildNodes[1].ChildNodes[4].ChildNodes[0].Attributes["UUID"].Value, xmlDoc.ChildNodes[1].ChildNodes[4].ChildNodes[0].Attributes["FechaTimbrado"].Value, xmlDoc.ChildNodes[1].Attributes["sello"].Value, xmlDoc.ChildNodes[1].ChildNodes[4].ChildNodes[0].Attributes["selloCFD"].Value, xmlDoc.ChildNodes[1].ChildNodes[4].ChildNodes[0].Attributes["noCertificadoSAT"].Value, "CODIGO RQ"); if (!string.IsNullOrEmpty(res[0].CORREO_ELECTRONICO)) { NOTIFIACIONES nt = new NOTIFIACIONES(); nt.NotificacionPorCorreo(res[0].CORREO_ELECTRONICO, new List <string>() { fi.FullName.Replace(fi.Extension, "_SAT.xml"), fi.FullName.Replace(fi.Extension, "_SAT.pdf") }); } } } return(new { ID_NOTA_DE_VENTA = idnotadeventa, ERROR = (timbradoactual == null ? false : (timbradoactual.NumeroError == "0" ? false : true)), RESULTADO = (timbradoactual == null ? "" : timbradoactual.Descripcion) }); }