/// <summary> /// Devuelve el cuerpo de una operación sujeta. /// </summary> /// <param name = "skipErrors" > Indica si hay que omitir las excepciones.</param> /// <returns>Operación sujeta correspondiente a la factura.</returns> internal DesgloseIVA GetDesgloseIVA(bool skipErrors = false) { DesgloseIVA desgloseIVA = new DesgloseIVA(); if (TaxesOutputs.Count > 0) { foreach (KeyValuePair <decimal, decimal[]> taxOut in TaxesOutputs) { DetalleIVA detalleIVA = new DetalleIVA() { TipoImpositivo = (taxOut.Value[2] == 0 && taxOut.Value[3] == 0) ? SIIParser.FromDecimal(taxOut.Key) : "0", BaseImponible = SIIParser.FromDecimal(taxOut.Value[0]), CuotaSoportada = SIIParser.FromDecimal(taxOut.Value[1]), }; if (Settings.Current.IDVersionSii.CompareTo("1.1") >= 0 && IsAsset) { detalleIVA.BienInversion = "S"; } if (taxOut.Value[2] != 0) { detalleIVA.TipoRecargoEquivalencia = SIIParser.FromDecimal(taxOut.Key); detalleIVA.CuotaRecargoEquivalencia = SIIParser.FromDecimal(taxOut.Value[2]); } if (taxOut.Value[3] != 0) { if (taxOut.Value[2] != 0 && !skipErrors) { throw new Exception("Only one value can be non zero: taxAmountCompensacionREAGYP != 0 and taxAmountRecargoEquivalencia != 0."); } if (ClaveRegimenEspecialOTrascendencia != ClaveRegimenEspecialOTrascendencia.ExportacionREAGYP && !skipErrors) { throw new Exception("ClaveRegimenEspecialOTrascendencia must be ClaveRegimenEspecialOTrascendencia.ExportacionREAGYP."); } detalleIVA.TipoImpositivo = detalleIVA.CuotaSoportada = null; detalleIVA.PorcentCompensacionREAGYP = SIIParser.FromDecimal(taxOut.Key); detalleIVA.ImporteCompensacionREAGYP = SIIParser.FromDecimal(taxOut.Value[3]); } desgloseIVA.DetalleIVA.Add(detalleIVA); } } else { desgloseIVA.DetalleIVA.Add(new DetalleIVA() { BaseImponible = SIIParser.FromDecimal(GrossAmount) }); } return(desgloseIVA); }
/// <summary> /// Rutina para añadir los desgloses de IVA correspondientes por cada factura. /// </summary> /// <param name="_CamposReg"></param> /// <param name="_FacturaActual"></param> /// <returns></returns> private RegistroLRFacturasEmitidas AgregarDesgloseIVA(string[] _CamposReg, RegistroLRFacturasEmitidas _FacturaActual) { RegistroLRFacturasEmitidas _FactActualWrk = _FacturaActual; TipoDesglose _TipoDesgloseTmp = new TipoDesglose(); DesgloseFactura _DesgloseFactTmp = new DesgloseFactura(); Sujeta _SujetaTmp = new Sujeta(); // Procedemos a tratar la factura actual. // En este caso añadiremos las líneas de fiscalidad que hayamos leido a la factura que estemos tratando en ese momento string _RegImpos = _CamposReg[1]; if (_RegImpos == "E ") { Exenta _ExentaWrk = new Exenta { CausaExencion = _CamposReg[7], BaseImponible = ((_CamposReg[4]).Trim()).Replace(',', '.') }; ; // La CausaExencion es opcional, de manera que no la informamos. _SujetaTmp.Exenta = _ExentaWrk; _DesgloseFactTmp.Sujeta = _SujetaTmp; _TipoDesgloseTmp.DesgloseFactura = _DesgloseFactTmp; } else { NoExenta _NoExentaWrk = new NoExenta { TipoNoExenta = _CamposReg[2] }; DesgloseIVA _DesgloseIVAWrk = new DesgloseIVA(); DetalleIVA _DetalleIVAWrk = new DetalleIVA { TipoImpositivo = ((_CamposReg[3]).Trim()).Replace(',', '.') }; ; _DetalleIVAWrk.BaseImponible = ((_CamposReg[4]).Trim()).Replace(',', '.');; _DetalleIVAWrk.CuotaRepercutida = ((_CamposReg[5]).Trim()).Replace(',', '.');; _DesgloseIVAWrk.DetalleIVA.Add(_DetalleIVAWrk); _NoExentaWrk.DesgloseIVA = _DesgloseIVAWrk; _SujetaTmp.NoExenta = _NoExentaWrk; _DesgloseFactTmp.Sujeta = _SujetaTmp; _TipoDesgloseTmp.DesgloseFactura = _DesgloseFactTmp; } _FactActualWrk.FacturaExpedida.TipoDesglose = _TipoDesgloseTmp; return(_FactActualWrk); }
/// <summary> /// Obtiene un objeto RegistroLRFacturasRecibidas, este objeto se utiliza /// para la serialización xml. /// </summary> /// <param name="updateInnerSII">Si es true, actualiza el objeto SII subyacente /// con el valor calculado.</param> /// <param name="skipErrors">Indica si hay que omitir las excepciones.</param> /// <returns>Nueva instancia del objeto para serialización /// xml RegistroLRFacturasEmitidas.</returns> public RegistroLRFacturasRecibidas ToSII(bool updateInnerSII = false, bool skipErrors = false) { if (InnerSII != null) { return(InnerSII); } RegistroLRFacturasRecibidas siiInvoice = new RegistroLRFacturasRecibidas(); if (IssueDate == null && !skipErrors) { throw new ArgumentNullException("IssueDate is null."); } if (PostingDate == null && !skipErrors) { throw new ArgumentNullException("PostingDate is null."); } if (RegisterDate == null && !skipErrors) { throw new ArgumentNullException("RegisterDate is null."); } if (!string.IsNullOrEmpty(ExternalReference) && !(Settings.Current.IDVersionSii.CompareTo("1.1") < 0)) { siiInvoice.FacturaRecibida.RefExterna = ExternalReference; } if (Settings.Current.IDVersionSii.CompareTo("1.1") < 0) { siiInvoice.PeriodoImpositivo.Ejercicio = (PostingDate ?? new DateTime(1, 1, 1)).ToString("yyyy"); siiInvoice.PeriodoImpositivo.Periodo = (PostingDate ?? new DateTime(1, 1, 1)).ToString("MM"); } else { siiInvoice.PeriodoLiquidacion.Ejercicio = (PostingDate ?? new DateTime(1, 1, 1)).ToString("yyyy"); siiInvoice.PeriodoLiquidacion.Periodo = (PostingDate ?? new DateTime(1, 1, 1)).ToString("MM"); } if (SellerParty == null && !skipErrors) { throw new ArgumentNullException("SellerParty is null."); } if (GrossAmount != 0) { siiInvoice.FacturaRecibida.ImporteTotal = SIIParser.FromDecimal(GrossAmount); } if (!(Settings.Current.IDVersionSii.CompareTo("1.1") < 0)) { if (GrossAmount > UpperLimit) { siiInvoice.FacturaRecibida.Macrodato = "S"; } } siiInvoice.IDFactura.NumSerieFacturaEmisor = InvoiceNumber; siiInvoice.IDFactura.FechaExpedicionFacturaEmisor = SIIParser.FromDate(IssueDate); /* B) Facturas Recibidas * * En un plazo de cuatro días naturales desde la fecha en que se produzca el registro * contable de la factura y, en todo caso, antes del día 16 del mes siguiente al periodo * de liquidación en que se hayan incluido las operaciones correspondientes * (período en que se deduce el IVA soportado). * Se entiende que el registro contable de la factura se produce en la fecha de entrada * en el sistema contable con independencia de la fecha reflejada en el asiento contable. */ siiInvoice.FacturaRecibida.FechaRegContable = SIIParser.FromDate(RegisterDate); if (OperationIssueDate != null) { siiInvoice.FacturaRecibida.FechaOperacion = SIIParser.FromDate(OperationIssueDate); } if (Settings.Current.IDVersionSii.CompareTo("1.1") >= 0 && TaxDate != null) { siiInvoice.FacturaRecibida.ADeducirEnPeriodoPosterior = "S"; siiInvoice.FacturaRecibida.EjercicioDeduccion = TaxDate?.ToString("yyyy"); siiInvoice.FacturaRecibida.PeriodoDeduccion = TaxDate?.ToString("MM");; } siiInvoice.FacturaRecibida.TipoFactura = InvoiceType.ToString(); siiInvoice.FacturaRecibida.ClaveRegimenEspecialOTrascendencia = ((int)ClaveRegimenEspecialOTrascendencia).ToString().PadLeft(2, '0'); siiInvoice.FacturaRecibida.DescripcionOperacion = InvoiceText; if (InvoiceType == InvoiceType.F5) { /* SII_Descripcion_ServicioWeb_v0.7.pdf (pag. 203) * 8.1.2.2.Ejemplo mensaje XML de alta de importación * En los datos identificativos correspondientes al proveedor se consignaran los del importador y titular del libro registro * Deberán consignarse, como número de factura y fecha de expedición, el número de referencia que figura en el propio DUA y la fecha de su * admisión por la Administración Aduanera respectivamente*/ SellerParty = BuyerParty; } siiInvoice.FacturaRecibida.Contraparte = GetContraparte(siiInvoice, skipErrors); // Tratamiento de las facturas rectificativas. if (InvoicesRectified.Count != 0) { siiInvoice.FacturaRecibida.TipoRectificativa = RectifiedType.ToString(); if (RectifiedBase != 0) { // Si consta el datos de importe rectificacion (tipo rectif 's'), lo ponemos ImporteRectificacion importeRectifica = new ImporteRectificacion(); importeRectifica.BaseRectificada = SIIParser.FromDecimal(RectifiedBase); importeRectifica.CuotaRectificada = SIIParser.FromDecimal(RectifiedAmount); importeRectifica.CuotaRecargoRectificado = SIIParser.FromDecimal(RectifiedEquivCharge); siiInvoice.FacturaRecibida.ImporteRectificacion = importeRectifica; } siiInvoice.FacturaRecibida.FacturasRectificadas = new List <IDFactura>(); foreach (var regRect in InvoicesRectified) { IDFactura FactRectificada = new IDFactura(); FactRectificada.NumSerieFacturaEmisor = regRect.RectifiedInvoiceNumber; FactRectificada.FechaExpedicionFacturaEmisor = SIIParser.FromDate(regRect.RectifiedIssueDate); // En este caso pongo a null IDEmisorFactura para que no serialice una etiqueta vacía. FactRectificada.IDEmisorFactura = null; siiInvoice.FacturaRecibida.FacturasRectificadas.Add(FactRectificada); } } else { if (InvoiceType.ToString().StartsWith("R") && !skipErrors) { throw new Exception("RectifiedInvoiceNumber for InvoiceType of kind 'R' must be not null."); } } // Desgloses DesgloseIVA desgloseIVA = GetDesgloseIVA(); if (IsInversionSujetoPasivo) { siiInvoice.FacturaRecibida.DesgloseFactura.InversionSujetoPasivo = desgloseIVA; } else { siiInvoice.FacturaRecibida.DesgloseFactura.DesgloseIVA = desgloseIVA; } decimal cuotaDeducible = 0; foreach (KeyValuePair <decimal, decimal[]> kvp in TaxesOutputs) { cuotaDeducible += kvp.Value[1]; } siiInvoice.FacturaRecibida.CuotaDeducible = SIIParser.FromDecimal(cuotaDeducible); if (updateInnerSII) { InnerSII = siiInvoice; } return(siiInvoice); }
/// <summary> /// Constructor de ARInvoice. /// </summary> /// <param name="registroLRFacturasRecibidas">Objeto serialización xml facturas emitidas.</param> public APInvoice(RegistroLRFacturasRecibidas registroLRFacturasRecibidas) { APInvoicePayments = new List <APInvoicePayment>(); InvoicesRectified = new List <InvoiceRectified>(); RegistroLRFacturasRecibidas siiInvoice = registroLRFacturasRecibidas; InvoiceNumber = siiInvoice.IDFactura.NumSerieFacturaEmisor; IssueDate = Convert.ToDateTime(siiInvoice.IDFactura.FechaExpedicionFacturaEmisor); PostingDate = Convert.ToDateTime(siiInvoice.FacturaRecibida.FechaRegContable); if (Settings.Current.IDVersionSii.CompareTo("1.1") >= 0 && siiInvoice.FacturaRecibida.ADeducirEnPeriodoPosterior == "S") { TaxDate = new DateTime(Convert.ToInt32(siiInvoice.FacturaRecibida.EjercicioDeduccion), Convert.ToInt32(siiInvoice.FacturaRecibida.PeriodoDeduccion), 1); } if (siiInvoice.FacturaRecibida.FechaOperacion != null) { OperationIssueDate = Convert.ToDateTime(siiInvoice.FacturaRecibida.FechaOperacion); } SellerParty = new Party() { TaxIdentificationNumber = siiInvoice.IDFactura.IDEmisorFactura.NIF, PartyName = siiInvoice.FacturaRecibida.Contraparte.NombreRazon }; if (siiInvoice.FacturaRecibida.Contraparte.IDOtro != null) { // Si no es un nif español IDOtroType = (IDOtroType)Convert.ToInt32( siiInvoice.FacturaRecibida.Contraparte.IDOtro.IDType); CountryCode = siiInvoice.FacturaRecibida.Contraparte.IDOtro.CodigoPais; SellerParty.TaxIdentificationNumber = siiInvoice.FacturaRecibida.Contraparte.IDOtro.ID; } if (SellerParty == null) { throw new ArgumentNullException("SellerParty is null."); } if (IssueDate == null) { throw new ArgumentNullException("IssueDate is null."); } if (PostingDate == null) { throw new ArgumentNullException("PostingDate is null."); } InvoiceType invoiceType; if (!Enum.TryParse <InvoiceType>(siiInvoice.FacturaRecibida.TipoFactura, out invoiceType)) { throw new InvalidOperationException($"Unknown invoice type {siiInvoice.FacturaRecibida.TipoFactura}"); } InvoiceType = invoiceType; ClaveRegimenEspecialOTrascendencia = (ClaveRegimenEspecialOTrascendencia)Convert.ToInt32( siiInvoice.FacturaRecibida.ClaveRegimenEspecialOTrascendencia); // Gestion rectificativa if (siiInvoice.FacturaRecibida.TipoRectificativa != null) { if (!siiInvoice.FacturaRecibida.TipoFactura.StartsWith("R")) { throw new ArgumentException("For TipoRectificativa not null TipoFactura must begin with 'R'."); } FacturaRecibida facturaRecibidaRectificativa = siiInvoice.FacturaRecibida; if (facturaRecibidaRectificativa.ImporteRectificacion != null) { RectifiedBase = SIIParser.ToDecimal(facturaRecibidaRectificativa.ImporteRectificacion.BaseRectificada); RectifiedAmount = SIIParser.ToDecimal(facturaRecibidaRectificativa.ImporteRectificacion.CuotaRectificada); } RectifiedType rectifiedType; if (Enum.TryParse <RectifiedType>(facturaRecibidaRectificativa.TipoRectificativa, out rectifiedType)) { RectifiedType = rectifiedType; } if (facturaRecibidaRectificativa.FacturasRectificadas.Count == 0) { throw new ArgumentException("FacturasRectificadas without elements."); } foreach (var regRect in facturaRecibidaRectificativa.FacturasRectificadas) { InvoiceRectified invoiceRectified = new InvoiceRectified(); invoiceRectified.RectifiedInvoiceNumber = regRect.NumSerieFacturaEmisor; invoiceRectified.RectifiedIssueDate = Convert.ToDateTime(regRect.FechaExpedicionFacturaEmisor); InvoicesRectified.Add(invoiceRectified); } } // Gestion desgloses decimal taxRate, taxBase, taxAmount, taxRateRE, taxAmountRE; if (siiInvoice.FacturaRecibida.DesgloseFactura != null) { DesgloseIVA desgloseIVA = null; if (siiInvoice.FacturaRecibida.DesgloseFactura.InversionSujetoPasivo != null && siiInvoice.FacturaRecibida.DesgloseFactura.InversionSujetoPasivo.DetalleIVA.Count > 0) { desgloseIVA = siiInvoice.FacturaRecibida.DesgloseFactura.InversionSujetoPasivo; } else { if (siiInvoice.FacturaRecibida.DesgloseFactura.DesgloseIVA == null) { throw new ArgumentException("For InversionSujetoPasivo DesgloseIVA must be present on DesgloseFactura."); } desgloseIVA = siiInvoice.FacturaRecibida.DesgloseFactura.DesgloseIVA; } foreach (var iva in desgloseIVA.DetalleIVA) { taxRate = SIIParser.ToDecimal(iva.TipoImpositivo); taxBase = SIIParser.ToDecimal(iva.BaseImponible); taxAmount = SIIParser.ToDecimal(iva.CuotaSoportada); taxRateRE = SIIParser.ToDecimal(iva.TipoRecargoEquivalencia); taxAmountRE = SIIParser.ToDecimal(iva.CuotaRecargoEquivalencia); if (Settings.Current.IDVersionSii.CompareTo("1.1") >= 0) { IsAsset = (iva.BienInversion == "S"); } TaxesOutputs.Add(taxRate, new decimal[] { taxBase, taxAmount, taxRateRE, taxAmountRE }); } } else { throw new ArgumentException("DesgloseFactura no present in FacturaRecibida."); } // Gestión cuota deducible GrossAmount = SIIParser.ToDecimal(siiInvoice.FacturaRecibida.ImporteTotal); InvoiceText = siiInvoice.FacturaRecibida.DescripcionOperacion; }
/// <summary> /// Constructor clase NoExenta. /// </summary> public NoExenta() { DesgloseIVA = new DesgloseIVA(); }