Ejemplo n.º 1
0
        /// <summary>
        /// Genera un documento XML para la emision de un comprobante.
        /// </summary>
        /// <param name="invoiceHeaderEntity">Entidad Invoice</param>
        /// <returns>Retorna el XML generado.</returns>
        public XmlFileResult GeneraDocumentoInvoice(InvoiceHeader invoiceHeaderEntity)
        {
            try
            {
                #region Filename
                var xmlFilename =
                    $"{invoiceHeaderEntity.RucEmisor}-{(int) invoiceHeaderEntity.TipoDocumento:00}-" +
                    $"{invoiceHeaderEntity.SerieDocumento}-{invoiceHeaderEntity.CorrelativoDocumento}";
                #endregion

                #region Gen Invoice
                AmountType.TlsDefaultCurrencyID = invoiceHeaderEntity.CodigoMoneda;

                var invoice = new InvoiceType
                {
                    ID                   = $"{invoiceHeaderEntity.SerieDocumento}-{invoiceHeaderEntity.CorrelativoDocumento}",
                    IssueDate            = invoiceHeaderEntity.FechaEmision,
                    InvoiceTypeCode      = ((int)invoiceHeaderEntity.TipoDocumento).ToString("00"),
                    DocumentCurrencyCode = invoiceHeaderEntity.CodigoMoneda,
                    CustomizationID      = "1.0",
                    OrderReference       = invoiceHeaderEntity.Compra != null ? new OrderReferenceType {
                        ID = invoiceHeaderEntity.Compra
                    } : null,
                    DespatchDocumentReference   = UtilsXmlDoc.DevuelveGuiasRemisionReferenciadas(invoiceHeaderEntity.GuiaRemisionReferencia),
                    AdditionalDocumentReference = UtilsXmlDoc.DevuelveDocumentosReferencia(invoiceHeaderEntity),
                    UBLExtensions = new[]
                    { new UBLExtensionType
                      {
                          ExtensionContent = new AdditionalsInformationType   //(mas informacion en el catalogo no 14 del manual.)
                          {
                              AdditionalInformation = new AdditionalInformationType {
                                  AdditionalMonetaryTotal    = UtilsXmlDoc.DevuelveTributosAdicionales(invoiceHeaderEntity.TotalTributosAdicionales),
                                  AdditionalProperty         = invoiceHeaderEntity.InfoAddicional.ToArray(),
                                  SUNATEmbededDespatchAdvice = invoiceHeaderEntity.GuiaEmbebida,
                                  SUNATTransaction           = invoiceHeaderEntity.TipoOperacion.HasValue ? new SUNATTransactionType
                                  {
                                      ID = ((int)invoiceHeaderEntity.TipoOperacion).ToString("00")
                                  }: null
                              }
                          }
                      },
                      new UBLExtensionType
                      {
                          ExtensionContent = new AdditionalsInformationType(),
                      } },
                    Signature = UtilsXmlDoc.GetSignature(invoiceHeaderEntity),
                    AccountingSupplierParty = UtilsXmlDoc.GetInfoEmisor(invoiceHeaderEntity),
                    SellerSupplierParty     = invoiceHeaderEntity.VendedorCodigo != null || invoiceHeaderEntity.VendedorNombre != null ? new SupplierPartyType
                    {
                        CustomerAssignedAccountID = invoiceHeaderEntity.VendedorCodigo,
                        Party = new PartyType
                        {
                            PartyName = new PartyNameType[] { invoiceHeaderEntity.VendedorNombre }
                        }
                    }: null,
                    AccountingCustomerParty = new CustomerPartyType
                    {
                        CustomerAssignedAccountID = invoiceHeaderEntity.NroDocCliente,
                        AdditionalAccountID       =
                            new IdentifierType[] { ((int)invoiceHeaderEntity.TipoDocumentoIdentidadCliente).ToString() },
                        Party = new PartyType
                        {
                            PostalAddress = string.IsNullOrWhiteSpace(invoiceHeaderEntity.DireccionCliente) ? null : new AddressType
                            {
                                StreetName = invoiceHeaderEntity.DireccionCliente
                            },
                            PartyLegalEntity = new[]
                            {
                                new PartyLegalEntityType
                                {
                                    RegistrationName = invoiceHeaderEntity.NombreRazonSocialCliente,
                                }
                            }
                        },
                        BuyerContact = invoiceHeaderEntity.CorreoCliente != null ? new ContactType {
                            ElectronicMail = invoiceHeaderEntity.CorreoCliente
                        } : null
                    },
                    PaymentMeans = invoiceHeaderEntity.Vencimiento != null ? new []
                    {
                        new PaymentMeansType
                        {
                            PaymentMeansCode = "-",
                            PaymentDueDate   = invoiceHeaderEntity.Vencimiento.Value
                        }
                    }: null,
                    PrepaidPayment     = UtilsXmlDoc.GetAnticipos(invoiceHeaderEntity.Anticipos),
                    LegalMonetaryTotal = new MonetaryTotalType
                    {
                        AllowanceTotalAmount = invoiceHeaderEntity.DescuentoGlobal > 0 ? new AmountType {
                            Value = invoiceHeaderEntity.DescuentoGlobal
                        } : null,
                        PrepaidAmount = invoiceHeaderEntity.TotalAnticipos.HasValue ? new AmountType {
                            Value = invoiceHeaderEntity.TotalAnticipos.Value
                        } : null,
                        PayableAmount = invoiceHeaderEntity.TotalVenta
                    },
                    InvoiceLine = UtilsXmlDoc.DevuelveDetallesDelComprobante(invoiceHeaderEntity.DetallesDocumento),
                    TaxTotal    = UtilsXmlDoc.DevuelveSubTotalImpuestos(invoiceHeaderEntity.Impuesto)
                };
                invoice.AccountingSupplierParty.Party.PostalAddress = UtilsXmlDoc.ObtenerDireccion(invoiceHeaderEntity.DireccionEmisor);
                #endregion

                return(FromDocument(invoice, xmlFilename));
            }
            catch (Exception ex)
            {
                return(FromException(ex));
            }
        }