Ejemplo n.º 1
0
        static Comprobante InvoiceToCFDv33(FiscalDocument item)
        {
            var cer = item.Issuer.Certificates.SingleOrDefault(x => x.Id == item.IssuerCertificateNumber);
            var cfd = new Comprobante {
                TipoDeComprobante   = (c_TipoDeComprobante)FDT2TDC(item.Type),
                NoCertificado       = item.IssuerCertificateNumber.PadLeft(20, '0'),
                Serie               = item.Batch,
                Folio               = item.Serial.ToString(),
                Fecha               = item.Issued.GetValueOrDefault(),
                MetodoPago          = item.Terms == PaymentTerms.Immediate ? c_MetodoPago.PagoEnUnaSolaExhibicion : c_MetodoPago.PagoEnParcialidadesODiferido,
                MetodoPagoSpecified = true,
                FormaPago           = (c_FormaPago)(int)item.PaymentMethod,
                FormaPagoSpecified  = true,
                LugarExpedicion     = item.IssuedLocation,
                SubTotal            = item.Subtotal,
                Total               = item.Total,
                Moneda              = item.Currency.GetDisplayName(),
                TipoCambio          = item.ExchangeRate,
                TipoCambioSpecified = item.Currency != CurrencyCode.MXN,
                Sello               = item.IssuerDigitalSeal,
                Certificado         = (cer == null ? null : SecurityHelpers.EncodeBase64(cer.CertificateData)),
                Emisor              = new ComprobanteEmisor {
                    Rfc           = item.Issuer.Id,
                    Nombre        = item.IssuerName,
                    RegimenFiscal = (c_RegimenFiscal)int.Parse(item.IssuerRegime.Id),
                },
                Receptor = new ComprobanteReceptor {
                    Rfc     = item.Recipient,
                    Nombre  = item.RecipientName,
                    UsoCFDI = CfdiUsage2UsoCFDI(item.Usage.Id)
                },
                Conceptos = new ComprobanteConcepto [item.Details.Count]
            };

            int i = 0;

            foreach (var detail in item.Details)
            {
                cfd.Conceptos [i] = new ComprobanteConcepto {
                    Cantidad           = detail.Quantity,
                    ClaveUnidad        = detail.UnitOfMeasurement.Id,
                    Unidad             = detail.UnitOfMeasurementName,
                    NoIdentificacion   = detail.ProductCode,
                    ClaveProdServ      = detail.ProductService.Id,
                    Descripcion        = detail.ProductName,
                    ValorUnitario      = detail.NetPrice,
                    Importe            = detail.Subtotal,
                    Descuento          = detail.Discount,
                    DescuentoSpecified = detail.Discount > 0m
                };

                if (detail.Subtotal == detail.Discount)
                {
                    i++;
                    continue;
                }

                if (detail.TaxRate >= 0m)
                {
                    cfd.Conceptos [i].Impuestos = new ComprobanteConceptoImpuestos {
                        Traslados = new ComprobanteConceptoImpuestosTraslado [] {
                            new ComprobanteConceptoImpuestosTraslado {
                                Impuesto            = c_Impuesto.IVA,
                                TipoFactor          = c_TipoFactor.Tasa,
                                Base                = detail.TaxBase,
                                Importe             = detail.Taxes,
                                ImporteSpecified    = true,
                                TasaOCuota          = detail.TaxRate,
                                TasaOCuotaSpecified = true
                            }
                        },
                        Retenciones = item.RetentionRate <= 0m ? null : new ComprobanteConceptoImpuestosRetencion [] {
                            new ComprobanteConceptoImpuestosRetencion {
                                Impuesto   = c_Impuesto.IVA,
                                TipoFactor = c_TipoFactor.Tasa,
                                Base       = detail.TaxBase,
                                Importe    = detail.RetentionTaxes,
                                TasaOCuota = item.RetentionRate
                            }
                        }
                    };
                }
                else
                {
                    cfd.Conceptos [i].Impuestos = new ComprobanteConceptoImpuestos {
                        Traslados = new ComprobanteConceptoImpuestosTraslado [] {
                            new ComprobanteConceptoImpuestosTraslado {
                                Impuesto   = c_Impuesto.IVA,
                                TipoFactor = c_TipoFactor.Exento,
                                Base       = detail.TaxBase
                            }
                        }
                    };
                }

                i++;
            }

            if (item.Discount > 0)
            {
                cfd.Descuento          = item.Discount;
                cfd.DescuentoSpecified = true;
            }

            cfd.Impuestos = new ComprobanteImpuestos();

            var taxes = new List <ComprobanteImpuestosTraslado> ();

            if (cfd.Conceptos.Any(c => c.Impuestos != null && c.Impuestos.Traslados.Any(x => x.TasaOCuota == decimal.Zero)))
            {
                taxes.Add(new ComprobanteImpuestosTraslado {
                    Impuesto   = c_Impuesto.IVA,
                    TipoFactor = c_TipoFactor.Tasa,
                    TasaOCuota = 0.000000m,
                    Importe    = 0.00m
                });
            }

            if (cfd.Conceptos.Any(c => c.Impuestos != null && c.Impuestos.Traslados.Any(x => x.TasaOCuota == WebConfig.DefaultVAT)))
            {
                taxes.Add(new ComprobanteImpuestosTraslado {
                    Impuesto   = c_Impuesto.IVA,
                    TipoFactor = c_TipoFactor.Tasa,
                    TasaOCuota = WebConfig.DefaultVAT,
                    Importe    = cfd.Conceptos.Where(x => x.Impuestos != null).Sum(c => c.Impuestos.Traslados.Where(x => x.TasaOCuota == WebConfig.DefaultVAT).Sum(x => x.Importe))
                });
            }

            cfd.Impuestos.Traslados = taxes.ToArray();
            cfd.Impuestos.TotalImpuestosTrasladados          = cfd.Impuestos.Traslados.Sum(x => x.Importe);
            cfd.Impuestos.TotalImpuestosTrasladadosSpecified = true;

            if (item.RetentionRate > 0m)
            {
                cfd.Impuestos.Retenciones = new ComprobanteImpuestosRetencion [] {
                    new ComprobanteImpuestosRetencion {
                        Impuesto = c_Impuesto.IVA,
                        Importe  = item.RetentionTaxes,
                    }
                };

                cfd.Impuestos.TotalImpuestosRetenidos          = cfd.Impuestos.Retenciones.Sum(x => x.Importe);
                cfd.Impuestos.TotalImpuestosRetenidosSpecified = true;
            }

            if (item.Relations.Any())
            {
                cfd.CfdiRelacionados = new ComprobanteCfdiRelacionados {
                    TipoRelacion    = item.Type == FiscalDocumentType.AdvancePaymentsApplied ? c_TipoRelacion.AplicacionDeAnticipo : c_TipoRelacion.NotaDeCredito,
                    CfdiRelacionado = new ComprobanteCfdiRelacionadosCfdiRelacionado [item.Relations.Count]
                };

                i = 0;
                foreach (var relation in item.Relations)
                {
                    cfd.CfdiRelacionados.CfdiRelacionado [i++] = new ComprobanteCfdiRelacionadosCfdiRelacionado {
                        UUID = relation.Relation.StampId
                    };
                }
            }

            return(cfd);
        }
Ejemplo n.º 2
0
        static Comprobante PaymentReceiptToCFDv33(FiscalDocument item)
        {
            var cer = item.Issuer.Certificates.SingleOrDefault(x => x.Id == item.IssuerCertificateNumber);
            var cfd = new Comprobante {
                Serie             = item.Batch,
                Folio             = item.Serial.ToString(),
                Fecha             = item.Issued.GetValueOrDefault(),
                NoCertificado     = item.IssuerCertificateNumber.PadLeft(20, '0'),
                Certificado       = (cer == null ? null : SecurityHelpers.EncodeBase64(cer.CertificateData)),
                SubTotal          = 0,
                Moneda            = "XXX",
                Total             = 0,
                TipoDeComprobante = c_TipoDeComprobante.Pago,
                LugarExpedicion   = item.IssuedLocation,
                Emisor            = new ComprobanteEmisor {
                    Rfc           = item.Issuer.Id,
                    Nombre        = item.IssuerName,
                    RegimenFiscal = (c_RegimenFiscal)int.Parse(item.IssuerRegime.Id),
                },
                Receptor = new ComprobanteReceptor {
                    Rfc     = item.Recipient,
                    Nombre  = item.RecipientName,
                    UsoCFDI = c_UsoCFDI.PorDefinir
                },
                Conceptos = new ComprobanteConcepto [] {
                    new ComprobanteConcepto {
                        ClaveProdServ = "84111506",
                        Cantidad      = 1,
                        ClaveUnidad   = "ACT",
                        Descripcion   = "Pago",
                        ValorUnitario = 0,
                        Importe       = 0
                    }
                },
                Complemento = new List <object> ()
            };
            var pagos = new Pagos {
                Pago = new PagosPago [] {
                    new PagosPago {
                        FechaPago            = item.PaymentDate.GetValueOrDefault(),
                        FormaDePagoP         = (c_FormaPago)(int)item.PaymentMethod,
                        MonedaP              = item.Currency.GetDisplayName(),
                        TipoCambioP          = item.ExchangeRate,
                        TipoCambioPSpecified = item.Currency != CurrencyCode.MXN,
                        Monto            = item.PaymentAmount,
                        NumOperacion     = string.IsNullOrWhiteSpace(item.PaymentReference) ? null : item.PaymentReference,
                        NomBancoOrdExt   = string.IsNullOrWhiteSpace(item.Reference) ? null : item.Reference,
                        DoctoRelacionado = new PagosPagoDoctoRelacionado [item.Relations.Count]
                    }
                }
            };

            int i = 0;

            foreach (var relation in item.Relations)
            {
                pagos.Pago [0].DoctoRelacionado [i++] = new PagosPagoDoctoRelacionado {
                    IdDocumento               = relation.Relation.StampId,
                    Serie                     = relation.Relation.Batch,
                    Folio                     = relation.Relation.Serial.ToString(),
                    MonedaDR                  = relation.Relation.Currency.GetDisplayName(),
                    TipoCambioDR              = relation.ExchangeRate,
                    TipoCambioDRSpecified     = relation.Relation.Currency != item.Currency,
                    MetodoDePagoDR            = c_MetodoPago.PagoEnParcialidadesODiferido,
                    NumParcialidad            = relation.Installment.ToString(),
                    ImpSaldoAnt               = relation.PreviousBalance,
                    ImpSaldoAntSpecified      = true,
                    ImpPagado                 = relation.Amount,
                    ImpPagadoSpecified        = true,
                    ImpSaldoInsoluto          = relation.OutstandingBalance,
                    ImpSaldoInsolutoSpecified = true
                };
            }

            cfd.Complemento.Add(pagos);

            return(cfd);
        }