public void Run() { try { Console.WriteLine("----- Inicio del ejemplo PaymentComplementExample -----"); // Cfdi Incial (debe ser "PPD") // -------- Creacion del cfdi en su forma general (sin items / productos) asociados -------- Facturama.Models.Request.Cfdi cfdi = CreateModelCfdiGeneral(facturama); // -------- Agregar los items que lleva el cfdi ( para este ejemplo, se agregan con datos aleatorios) -------- cfdi = AddItemsToCfdi(facturama, cfdi); cfdi.PaymentMethod = "PPD"; // El método de pago del documento inicial debe ser "PPD" // Se manda timbrar mediante Facturama Facturama.Models.Response.Cfdi cfdiInicial = facturama.Cfdis.Create(cfdi); Console.WriteLine("Se creó exitosamente el cfdi Inicial (PPD) con el folio fiscal: " + cfdiInicial.Complement.TaxStamp.Uuid); // Descarga de los archivos del documento inicial String filePath = "factura" + cfdiInicial.Complement.TaxStamp.Uuid; facturama.Cfdis.SavePdf(filePath + ".pdf", cfdiInicial.Id); facturama.Cfdis.SaveXml(filePath + ".xml", cfdiInicial.Id); // Complemento de pago (debe ser "PUE") // Y no lleva "Items" solo especifica el "Complemento" Facturama.Models.Request.Cfdi paymentComplementModel = CreateModelCfdiPaymentComplement(facturama, cfdiInicial); // Se manda timbrar el complemento de pago mediante Facturama Facturama.Models.Response.Cfdi paymentComplement = facturama.Cfdis.Create(paymentComplementModel); Console.WriteLine("Se creó exitosamente el complemento de pago con el folio fiscal: " + cfdiInicial.Complement.TaxStamp.Uuid); // Descarga de los archivos del documento inicial String filePathPayment = "factura" + paymentComplement.Complement.TaxStamp.Uuid; facturama.Cfdis.SavePdf(filePath + ".pdf", paymentComplement.Id); facturama.Cfdis.SaveXml(filePath + ".xml", paymentComplement.Id); // Posibilidad de mandar los cfdis por coreo ( el cfdiInical y complemento de pago) Console.WriteLine(facturama.Cfdis.SendByMail(cfdiInicial.Id, "*****@*****.**")); Console.WriteLine(facturama.Cfdis.SendByMail(paymentComplement.Id, "*****@*****.**")); Console.WriteLine("----- Fin del ejemplo de PaymentComplementExample -----"); } catch (FacturamaException ex) { Console.WriteLine(ex.Message); foreach (var messageDetail in ex.Model.Details) { Console.WriteLine($"{messageDetail.Key}: {string.Join(",", messageDetail.Value)}"); } } catch (Exception ex) { Console.WriteLine($"Error inesperado: ", ex.Message); } }
private static void TestCreateCfdi(FacturamaApi facturama) { var products = facturama.Products.List().Where(p => p.Taxes.Any()).ToList(); var nameId = facturama.Catalogs.NameIds.ElementAt(1); //Nombre en el pdf: "Factura" var currency = facturama.Catalogs.Currencies.First(m => m.Value == "MXN"); var paymentMethod = facturama.Catalogs.PaymentMethods.First(p => p.Name == "Pago en una sola exhibición"); var paymentForm = facturama.Catalogs.PaymentForms.First(p => p.Name == "Efectivo"); var cliente = facturama.Clients.List().First(c => c.Rfc == "XAXX010101000"); var branchOffice = facturama.BranchOffices.List().First(); var random = new Random(); var nitems = random.Next(1, products.Count) % 10; // Cantidad de items para la factura var decimals = (int)currency.Decimals; var cfdi = new Cfdi { NameId = nameId.Value, CfdiType = CfdiType.Ingreso, PaymentForm = paymentForm.Value, PaymentMethod = paymentMethod.Value, Currency = currency.Value, Date = DateTime.Now, ExpeditionPlace = branchOffice.Address.ZipCode, Items = new List <Item>(), Receiver = new Receiver { CfdiUse = cliente.CfdiUse, Name = cliente.Name, Rfc = cliente.Rfc }, }; for (var i = products.Count - nitems; i < products.Count && i > 0; i++) { var product = products[i]; var quantity = random.Next(1, 5); //Una cantidad aleatoria var discount = product.Price % (product.Price == 0 ? 1 : random.Next(0, (int)product.Price)); //Un descuento aleatorio var subtotal = Math.Round(product.Price * quantity, decimals); var item = new Item { ProductCode = product.CodeProdServ, UnitCode = product.UnitCode, Unit = product.Unit, Description = product.Description, IdentificationNumber = product.IdentificationNumber, Quantity = quantity, Discount = Math.Round(discount, decimals), UnitPrice = Math.Round(product.Price, decimals), Subtotal = subtotal, Taxes = product.Taxes?.Select( t => { var baseAmount = Math.Round(subtotal - discount, decimals); return(new Tax { Name = t.Name, IsQuota = t.IsQuota, IsRetention = t.IsRetention, Rate = Math.Round(t.Rate, 6), Base = Math.Round(subtotal - discount, decimals), Total = Math.Round(baseAmount * t.Rate, decimals) }); }).ToList() }; var retenciones = item.Taxes?.Where(t => t.IsRetention).Sum(t => t.Total) ?? 0; var traslados = item.Taxes?.Where(t => !t.IsRetention).Sum(t => t.Total) ?? 0; item.Total = item.Subtotal - item.Discount + traslados - retenciones; cfdi.Items.Add(item); } try { var cfdiCreated = facturama.Cfdis.Create(cfdi); Console.WriteLine( $"Se creó exitosamente el cfdi con el folio fiscal: {cfdiCreated.Complement.TaxStamp.Uuid}"); facturama.Cfdis.SavePdf($"factura{cfdiCreated.Complement.TaxStamp.Uuid}.pdf", cfdiCreated.Id); facturama.Cfdis.SaveXml($"factura{cfdiCreated.Complement.TaxStamp.Uuid}.xml", cfdiCreated.Id); var list = facturama.Cfdis.List("Expresion en Software"); Console.WriteLine($"Se encontraron: {list.Length} elementos en la busqueda"); list = facturama.Cfdis.List(rfc: "ESO1202108R2"); //Atributo en especifico Console.WriteLine($"Se encontraron: {list.Length} elementos en la busqueda"); facturama.Cfdis.Remove(cfdiCreated.Id); Console.WriteLine( $"Se eliminó exitosamente el cfdi con el folio fiscal: {cfdiCreated.Complement.TaxStamp.Uuid}"); } catch (FacturamaException ex) { Console.WriteLine(ex.Message); foreach (var messageDetail in ex.Model.Details) { Console.WriteLine($"{messageDetail.Key}: {string.Join(",", messageDetail.Value)}"); } } catch (Exception ex) { Console.WriteLine($"Error inesperado: ", ex.Message); } Console.ReadKey(); }