From 6a388711c270d9901f8873aff0fc4198b5e5e915 Mon Sep 17 00:00:00 2001 From: carlosthe19916 <2582866+carlosthe19916@users.noreply.github.com> Date: Mon, 4 Dec 2023 21:35:57 +0100 Subject: [PATCH] Add forma de pago tests --- src/enricher/enrich.rs | 18 ++--- .../rules/phase1fill/detalle/detalles.rs | 2 +- .../rules/phase1fill/detalle/igvtasa.rs | 2 +- .../detalle/precioreferenciatipo.rs | 2 +- .../rules/phase1fill/invoice/anticipos.rs | 4 +- .../rules/phase1fill/invoice/descuentos.rs | 2 +- .../rules/phase1fill/invoice/formadepago.rs | 33 ++++++++- .../phase1fill/invoice/tipocomprobante.rs | 2 +- .../note/tipocomprobanteafectado.rs | 2 +- .../rules/phase2process/detalle/detalles.rs | 4 +- .../rules/phase3summary/invoice/detraccion.rs | 2 +- .../rules/phase3summary/invoice/percepcion.rs | 2 +- .../phase3summary/invoice/totalimporte.rs | 2 +- .../phase3summary/invoice/totalimpuestos.rs | 6 +- src/models/general.rs | 2 +- src/prelude.rs | 3 +- src/renderer/mod.rs | 29 +++++++- src/templates/renderer/invoice.xml | 10 +-- .../ubl/standard/include/payment-terms.xml | 10 +-- tests/invoice_direccion_entrega.rs | 67 +++++++++++++++++++ tests/invoice_forma_pago.rs | 62 +++++++++++++++++ 21 files changed, 224 insertions(+), 42 deletions(-) create mode 100644 tests/invoice_direccion_entrega.rs create mode 100644 tests/invoice_forma_pago.rs diff --git a/src/enricher/enrich.rs b/src/enricher/enrich.rs index e1f73826..4741457d 100644 --- a/src/enricher/enrich.rs +++ b/src/enricher/enrich.rs @@ -9,7 +9,7 @@ use crate::enricher::rules::phase1fill::igvtasa::IGVTasaEnrichRule; use crate::enricher::rules::phase1fill::invoice::anticipos::InvoiceAnticiposEnrichRule; use crate::enricher::rules::phase1fill::invoice::descuentos::InvoiceDescuentosEnrichRule; use crate::enricher::rules::phase1fill::invoice::formadepago::{ - InvoiceFormaDePagoEnrichRule, InvoiceFormaDePagoTotalRule, + InvoiceFormaDePagoEnrichRule, InvoiceFormaDePagoTipoRule, InvoiceFormaDePagoTotalRule, }; use crate::enricher::rules::phase1fill::invoice::leyenda::{ InvoiceLeyendaDetraccionEnrichRule, InvoiceLeyendaDireccionEntregaEnrichRule, @@ -153,7 +153,7 @@ where let mut changed = true; while changed { - let results = vec![ + let results = [ FechaEmisionEnrichRule::fill(self, defaults), FirmanteEnrichRule::fill(self), ICBTasaEnrichRule::fill(self, defaults), @@ -175,11 +175,10 @@ where + InvoiceDescuentosEnrichRule + InvoiceFormaDePagoEnrichRule + InvoiceFormaDePagoTotalRule + + InvoiceFormaDePagoTipoRule + InvoiceLeyendaDetraccionEnrichRule + InvoiceLeyendaDireccionEntregaEnrichRule + InvoiceLeyendaPercepcionEnrichRule - + InvoiceFormaDePagoEnrichRule - + InvoiceFormaDePagoTotalRule + InvoiceTipoComprobanteEnrichRule + InvoiceTipoOperacionEnrichRule, { @@ -187,11 +186,12 @@ where let mut changed = true; while changed { - let results = vec![ + let results = [ InvoiceAnticiposEnrichRule::fill(self), InvoiceDescuentosEnrichRule::fill(self), InvoiceFormaDePagoEnrichRule::fill(self), InvoiceFormaDePagoTotalRule::fill(self), + InvoiceFormaDePagoTipoRule::fill(self), InvoiceLeyendaDetraccionEnrichRule::fill(self), InvoiceLeyendaDireccionEntregaEnrichRule::fill(self), InvoiceLeyendaPercepcionEnrichRule::fill(self), @@ -212,7 +212,7 @@ where let mut changed = true; while changed { - let results = vec![ + let results = [ NoteTipoComprobanteAfectadoEnrichRule::fill(self), CreditNoteTipoEnrichRule::fill(self), ]; @@ -230,7 +230,7 @@ where let mut changed = true; while changed { - let results = vec![ + let results = [ NoteTipoComprobanteAfectadoEnrichRule::fill(self), DebitNoteTipoEnrichRule::fill(self), ]; @@ -268,7 +268,7 @@ where let mut changed = true; while changed { - let results = vec![DetallesProcessRule::process(self)]; + let results = [DetallesProcessRule::process(self)]; changed = results.contains(&true); } @@ -307,7 +307,7 @@ where let mut changed = true; while changed { - let results = vec![ + let results = [ InvoiceTotalImpuestosSummaryRule::summary(self), InvoiceTotalImporteSummaryRule::summary(self), LeyendaIVAPSummaryRule::summary(self), diff --git a/src/enricher/rules/phase1fill/detalle/detalles.rs b/src/enricher/rules/phase1fill/detalle/detalles.rs index c008008e..d88e8eaa 100644 --- a/src/enricher/rules/phase1fill/detalle/detalles.rs +++ b/src/enricher/rules/phase1fill/detalle/detalles.rs @@ -36,7 +36,7 @@ where self.get_detalles() .iter_mut() .map(|detalle| { - let results = vec![ + let results = [ DetalleICBTasaEnrichRule::fill(detalle, defaults), DetalleIGVTasaEnrichRule::fill(detalle, defaults), DetalleIGVTipoEnrichRule::fill(detalle, defaults), diff --git a/src/enricher/rules/phase1fill/detalle/igvtasa.rs b/src/enricher/rules/phase1fill/detalle/igvtasa.rs index b84ed8b6..1c0184f7 100644 --- a/src/enricher/rules/phase1fill/detalle/igvtasa.rs +++ b/src/enricher/rules/phase1fill/detalle/igvtasa.rs @@ -1,6 +1,6 @@ use rust_decimal::Decimal; -use crate::catalogs::{Catalog7, catalog7_value_of_code, Catalog7Group}; +use crate::catalogs::{catalog7_value_of_code, Catalog7, Catalog7Group}; use crate::enricher::rules::phase1fill::detalle::detalles::DetalleDefaults; use crate::models::traits::detalle::igvtasa::{DetalleIGVTasaGetter, DetalleIGVTasaSetter}; use crate::models::traits::detalle::igvtipo::DetalleIGVTipoGetter; diff --git a/src/enricher/rules/phase1fill/detalle/precioreferenciatipo.rs b/src/enricher/rules/phase1fill/detalle/precioreferenciatipo.rs index 520a5449..0e23d0fc 100644 --- a/src/enricher/rules/phase1fill/detalle/precioreferenciatipo.rs +++ b/src/enricher/rules/phase1fill/detalle/precioreferenciatipo.rs @@ -1,4 +1,4 @@ -use crate::catalogs::{Catalog, Catalog16, catalog7_value_of_code}; +use crate::catalogs::{catalog7_value_of_code, Catalog, Catalog16}; use crate::enricher::rules::phase1fill::detalle::detalles::DetalleDefaults; use crate::models::traits::detalle::igvtipo::DetalleIGVTipoGetter; use crate::models::traits::detalle::precioreferenciatipo::{ diff --git a/src/enricher/rules/phase1fill/invoice/anticipos.rs b/src/enricher/rules/phase1fill/invoice/anticipos.rs index 62a35739..6b059876 100644 --- a/src/enricher/rules/phase1fill/invoice/anticipos.rs +++ b/src/enricher/rules/phase1fill/invoice/anticipos.rs @@ -1,11 +1,11 @@ use log::warn; use regex::Regex; -use crate::{BOLETA_SERIE_REGEX, FACTURA_SERIE_REGEX}; use crate::catalogs::{Catalog, Catalog12, Catalog53}; use crate::models::traits::invoice::anticipos::{ AnticipoGetter, AnticipoSetter, InvoiceAnticiposGetter, }; +use crate::{BOLETA_SERIE_REGEX, FACTURA_SERIE_REGEX}; pub trait InvoiceAnticiposEnrichRule { fn fill(&mut self) -> bool; @@ -19,7 +19,7 @@ where self.get_anticipos() .iter_mut() .map(|anticipo| { - let results = vec![ + let results = [ AnticipoTipoRule::fill(anticipo), AnticipoComprobanteTipoRule::fill(anticipo), ]; diff --git a/src/enricher/rules/phase1fill/invoice/descuentos.rs b/src/enricher/rules/phase1fill/invoice/descuentos.rs index 9189a9d5..855b19d3 100644 --- a/src/enricher/rules/phase1fill/invoice/descuentos.rs +++ b/src/enricher/rules/phase1fill/invoice/descuentos.rs @@ -17,7 +17,7 @@ where self.get_descuentos() .iter_mut() .map(|descuento| { - let results = vec![ + let results = [ DescuentoFactorRule::fill(descuento), DescuentoMontoBaseRule::fill(descuento), DescuentoTipoRule::fill(descuento), diff --git a/src/enricher/rules/phase1fill/invoice/formadepago.rs b/src/enricher/rules/phase1fill/invoice/formadepago.rs index 8e6519d8..1edfa92a 100644 --- a/src/enricher/rules/phase1fill/invoice/formadepago.rs +++ b/src/enricher/rules/phase1fill/invoice/formadepago.rs @@ -13,16 +13,20 @@ pub trait InvoiceFormaDePagoTotalRule { fn fill(&mut self) -> bool; } +pub trait InvoiceFormaDePagoTipoRule { + fn fill(&mut self) -> bool; +} + impl InvoiceFormaDePagoEnrichRule for T where T: InvoiceFormaDePagoGetter + InvoiceFormaDePagoSetter, { fn fill(&mut self) -> bool { match &self.get_formadepago() { - Some(..) => false, + Some(_) => false, None => { self.set_formadepago(FormaDePago { - tipo: TipoFormaDePago::Contado, + tipo: Some(TipoFormaDePago::Contado), cuotas: vec![], total: None, }); @@ -32,6 +36,31 @@ where } } +impl InvoiceFormaDePagoTipoRule for T +where + T: InvoiceFormaDePagoGetter + InvoiceFormaDePagoSetter, +{ + fn fill(&mut self) -> bool { + if let Some(forma_de_pago) = self.get_formadepago() { + if forma_de_pago.tipo.is_none() { + let tipo = if forma_de_pago.cuotas.is_empty() { + TipoFormaDePago::Contado + } else { + TipoFormaDePago::Credito + }; + self.set_formadepago(FormaDePago { + tipo: Some(tipo), + ..forma_de_pago.clone() + }); + + return true; + }; + }; + + false + } +} + impl InvoiceFormaDePagoTotalRule for T where T: InvoiceFormaDePagoGetter + InvoiceFormaDePagoSetter, diff --git a/src/enricher/rules/phase1fill/invoice/tipocomprobante.rs b/src/enricher/rules/phase1fill/invoice/tipocomprobante.rs index 869d55d9..30d5e5a4 100644 --- a/src/enricher/rules/phase1fill/invoice/tipocomprobante.rs +++ b/src/enricher/rules/phase1fill/invoice/tipocomprobante.rs @@ -1,11 +1,11 @@ use regex::Regex; -use crate::{BOLETA_SERIE_REGEX, FACTURA_SERIE_REGEX}; use crate::catalogs::{Catalog, Catalog1}; use crate::models::traits::invoice::tipocomprobante::{ InvoiceTipoComprobanteGetter, InvoiceTipoComprobanteSetter, }; use crate::models::traits::serienumero::SerieNumeroGetter; +use crate::{BOLETA_SERIE_REGEX, FACTURA_SERIE_REGEX}; pub trait InvoiceTipoComprobanteEnrichRule { fn fill(&mut self) -> bool; diff --git a/src/enricher/rules/phase1fill/note/tipocomprobanteafectado.rs b/src/enricher/rules/phase1fill/note/tipocomprobanteafectado.rs index aa92cc92..2b2122b7 100644 --- a/src/enricher/rules/phase1fill/note/tipocomprobanteafectado.rs +++ b/src/enricher/rules/phase1fill/note/tipocomprobanteafectado.rs @@ -1,12 +1,12 @@ use log::warn; use regex::Regex; -use crate::{BOLETA_SERIE_REGEX, FACTURA_SERIE_REGEX}; use crate::catalogs::{Catalog, Catalog1}; use crate::models::traits::note::tipocomprobanteafectado::{ NoteTipoComprobanteAfectadoGetter, NoteTipoComprobanteAfectadoSetter, }; use crate::models::traits::serienumero::SerieNumeroGetter; +use crate::{BOLETA_SERIE_REGEX, FACTURA_SERIE_REGEX}; pub trait NoteTipoComprobanteAfectadoEnrichRule { fn fill(&mut self) -> bool; diff --git a/src/enricher/rules/phase2process/detalle/detalles.rs b/src/enricher/rules/phase2process/detalle/detalles.rs index ffcd8886..efc50981 100644 --- a/src/enricher/rules/phase2process/detalle/detalles.rs +++ b/src/enricher/rules/phase2process/detalle/detalles.rs @@ -22,7 +22,7 @@ where self.get_detalles() .iter_mut() .map(|detalle| { - let results = vec![ + let results = [ DetallePrecioProcessRule::process(detalle), DetallePrecioConImpuestosProcessRule::process(detalle), DetallePrecioReferenciaProcessRule::process(detalle), @@ -30,7 +30,7 @@ where if results.contains(&true) { true } else { - let results = vec![ + let results = [ DetalleICBProcessRule::process(detalle), DetalleICBAplicaProcessRule::process(detalle), DetalleIGVProcessRule::process(detalle), diff --git a/src/enricher/rules/phase3summary/invoice/detraccion.rs b/src/enricher/rules/phase3summary/invoice/detraccion.rs index df32845c..7496808e 100644 --- a/src/enricher/rules/phase3summary/invoice/detraccion.rs +++ b/src/enricher/rules/phase3summary/invoice/detraccion.rs @@ -16,7 +16,7 @@ where fn summary(&mut self) -> bool { match (self.get_totalimporte().clone(), self.get_detraccion()) { (Some(total_importe), Some(detraccion)) => { - let results = vec![DetraccionMontoRule::summary(detraccion, &total_importe)]; + let results = [DetraccionMontoRule::summary(detraccion, &total_importe)]; results.contains(&true) } _ => false, diff --git a/src/enricher/rules/phase3summary/invoice/percepcion.rs b/src/enricher/rules/phase3summary/invoice/percepcion.rs index 727a874e..03d1b63e 100644 --- a/src/enricher/rules/phase3summary/invoice/percepcion.rs +++ b/src/enricher/rules/phase3summary/invoice/percepcion.rs @@ -19,7 +19,7 @@ where { fn summary(&mut self) -> bool { match (self.get_totalimporte().clone(), self.get_percepcion()) { - (Some(total_importe), Some(percepcion)) => vec![ + (Some(total_importe), Some(percepcion)) => [ PerceptionPorcentajeBaseRule::summary(percepcion), PerceptionMontoBaseRule::summary(percepcion, &total_importe), PerceptionMontoRule::summary(percepcion), diff --git a/src/enricher/rules/phase3summary/invoice/totalimporte.rs b/src/enricher/rules/phase3summary/invoice/totalimporte.rs index 41f2aa97..daa41309 100644 --- a/src/enricher/rules/phase3summary/invoice/totalimporte.rs +++ b/src/enricher/rules/phase3summary/invoice/totalimporte.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use rust_decimal::Decimal; use rust_decimal_macros::dec; -use crate::catalogs::{Catalog5, Catalog53, catalog53_value_of_code, catalog7_value_of_code}; +use crate::catalogs::{catalog53_value_of_code, catalog7_value_of_code, Catalog5, Catalog53}; use crate::models::general::TotalImporte; use crate::models::traits::detalle::DetallesGetter; use crate::models::traits::igv::IGVTasaGetter; diff --git a/src/enricher/rules/phase3summary/invoice/totalimpuestos.rs b/src/enricher/rules/phase3summary/invoice/totalimpuestos.rs index ad4b7df1..72edbdb1 100644 --- a/src/enricher/rules/phase3summary/invoice/totalimpuestos.rs +++ b/src/enricher/rules/phase3summary/invoice/totalimpuestos.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use rust_decimal::Decimal; use rust_decimal_macros::dec; -use crate::catalogs::{Catalog5, Catalog53, catalog53_value_of_code, catalog7_value_of_code}; +use crate::catalogs::{catalog53_value_of_code, catalog7_value_of_code, Catalog5, Catalog53}; use crate::models::general::{Detalle, TotalImpuestos}; use crate::models::traits::detalle::DetallesGetter; use crate::models::traits::invoice::anticipos::InvoiceAnticiposGetter; @@ -34,7 +34,7 @@ where let gratuito = cal_impuesto_by_tipo(self.get_detalles(), Catalog5::Gratuito); // ICB - let icb_importe = vec![ + let icb_importe = [ ivap.importe_icb, exportacion.importe_icb, gravado.importe_icb, @@ -46,7 +46,7 @@ where .fold(dec!(0), |a, b| a + b); // ISC - let isc_importe = vec![ + let isc_importe = [ ivap.importe_isc, exportacion.importe_isc, gravado.importe_isc, diff --git a/src/models/general.rs b/src/models/general.rs index 075c8210..718c1e2b 100644 --- a/src/models/general.rs +++ b/src/models/general.rs @@ -24,7 +24,7 @@ pub enum TipoFormaDePago { #[derive(Clone, Debug, Serialize)] pub struct FormaDePago { - pub tipo: TipoFormaDePago, + pub tipo: Option, pub cuotas: Vec, pub total: Option, } diff --git a/src/prelude.rs b/src/prelude.rs index 07fc39ee..af3fa667 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -1,8 +1,7 @@ pub use crate::catalogs::*; pub use crate::enricher::enrich::{Defaults, EnrichTrait}; -pub use crate::models::*; pub use crate::models::common::*; pub use crate::models::general::*; pub use crate::models::invoice::*; +pub use crate::models::*; pub use crate::renderer::render_invoice; - diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 08db8a72..6e6de395 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -3,11 +3,11 @@ use std::str::FromStr; use rust_decimal::Decimal; use rust_decimal_macros::dec; -use tera::{Context, Error, from_value, Function, Tera, to_value, Value}; use tera::helpers::tests::{number_args_allowed, value_defined}; +use tera::{from_value, to_value, Context, Error, Function, Tera, Value}; use crate::models::invoice::Invoice; -use crate::prelude::{Catalog, catalog7_value_of_code}; +use crate::prelude::{catalog7_value_of_code, Catalog}; fn catalog7_taxcategory() -> impl Function { Box::new( @@ -69,6 +69,16 @@ pub fn currency(value: &Value, _: &HashMap) -> Result) -> Result { + match value.as_u64() { + Some(number) => { + let result = format!("{:03}", number); + Ok(to_value(result).unwrap()) + } + None => Err("format03d could not find a string".into()), + } +} + pub fn gt0(value: Option<&Value>, params: &[Value]) -> tera::Result { number_args_allowed("gt0", 0, params.len())?; value_defined("gt0", value)?; @@ -86,6 +96,19 @@ pub fn gt0(value: Option<&Value>, params: &[Value]) -> tera::Result { } } +pub fn credito(value: Option<&Value>, params: &[Value]) -> tera::Result { + number_args_allowed("credito", 0, params.len())?; + value_defined("credito", value)?; + + match value.and_then(|v| v.as_str()) { + Some("Credito") => Ok(true), + Some("Contado") => Ok(false), + _ => Err(Error::msg( + "Tester `gt0` was called on a variable that isn't a number", + )), + } +} + lazy_static::lazy_static! { pub static ref TEMPLATES: Tera = { let mut tera = match Tera::new("src/templates/**/*.xml") { @@ -99,8 +122,10 @@ lazy_static::lazy_static! { tera.register_filter("multiply100", multiply100); tera.register_filter("round_decimal", round_decimal); tera.register_filter("currency", currency); + tera.register_filter("format03d", format03d); tera.register_tester("gt0", gt0); + tera.register_tester("credito", credito); // tera.autoescape_on(vec![".xml"]); tera diff --git a/src/templates/renderer/invoice.xml b/src/templates/renderer/invoice.xml index 4fa3f38d..e6c78477 100644 --- a/src/templates/renderer/invoice.xml +++ b/src/templates/renderer/invoice.xml @@ -1,4 +1,5 @@ -{%- import "ubl/standard/include/document-line.xml" as macro -%} +{%- import "ubl/standard/include/document-line.xml" as macros_document_line -%} +{%- import "ubl/standard/include/address.xml" as macros_address -%} - - {% include "ubl/standard/include/address.xml" %} + {{ macros_address::address(direccion=direccion_entrega) }} @@ -117,7 +117,7 @@ {%- for it in detalles %} {{loop.index}} - {{it.cantidad}}{{ macro::line(item=it) }} + {{it.cantidad}}{{ macros_document_line::line(item=it) }} {%- endfor %} diff --git a/src/templates/ubl/standard/include/payment-terms.xml b/src/templates/ubl/standard/include/payment-terms.xml index 6bd99a95..37e25d96 100644 --- a/src/templates/ubl/standard/include/payment-terms.xml +++ b/src/templates/ubl/standard/include/payment-terms.xml @@ -2,15 +2,15 @@ FormaPago {{forma_de_pago.tipo}} - {%- if forma_de_pago.tipo == 'Credito' %} - {item.total | round_decimal} + {%- if forma_de_pago.tipo is credito %} + {{forma_de_pago.total | round_decimal}} {%- endif %} {%- for it in forma_de_pago.cuotas %} FormaPago - Cuota{it_index.add(1).format('%03d')} - {it.importe | round_decimal} - {it.fechaPago} + Cuota{{loop.index | format03d}} + {{it.importe | round_decimal}} + {{it.fecha_pago}} {%- endfor %} \ No newline at end of file diff --git a/tests/invoice_direccion_entrega.rs b/tests/invoice_direccion_entrega.rs new file mode 100644 index 00000000..3cb547b2 --- /dev/null +++ b/tests/invoice_direccion_entrega.rs @@ -0,0 +1,67 @@ +use rust_decimal_macros::dec; +use xbuilder::prelude::*; + +use crate::common::invoice_base; +use crate::common::{assert_invoice, detalle_base}; + +mod common; + +const BASE: &str = "tests/resources/e2e/renderer/invoice/InvoiceDireccionEntregaTest"; + +#[test] +fn invoice_direccion_entrega_min() { + let mut invoice = Invoice { + detalles: vec![ + Detalle { + precio: Some(dec!(100)), + ..detalle_base("Item1", dec!(2)) + }, + Detalle { + precio: Some(dec!(100)), + ..detalle_base("Item2", dec!(2)) + }, + ], + direccion_entrega: Some(Direccion { + direccion: Some("Jr. las flores 123"), + codigo_local: None, + ubigeo: None, + departamento: None, + provincia: None, + distrito: None, + urbanizacion: None, + codigo_pais: None, + }), + ..invoice_base() + }; + + assert_invoice(&mut invoice, &format!("{BASE}/direccionEntregaMin.xml")); +} + +#[test] +fn invoice_direccion_entrega_full() { + let mut invoice = Invoice { + detalles: vec![ + Detalle { + precio: Some(dec!(100)), + ..detalle_base("Item1", dec!(2)) + }, + Detalle { + precio: Some(dec!(100)), + ..detalle_base("Item2", dec!(2)) + }, + ], + direccion_entrega: Some(Direccion { + ubigeo: Some("050101"), + departamento: Some("Ayacucho"), + provincia: Some("Huamanga"), + distrito: Some("Jesus Nazareno"), + codigo_local: Some("0101"), + urbanizacion: Some("000000"), + direccion: Some("Jr. Las piedras 123"), + codigo_pais: Some("PE"), + }), + ..invoice_base() + }; + + assert_invoice(&mut invoice, &format!("{BASE}/direccionEntregaFull.xml")); +} diff --git a/tests/invoice_forma_pago.rs b/tests/invoice_forma_pago.rs new file mode 100644 index 00000000..08091637 --- /dev/null +++ b/tests/invoice_forma_pago.rs @@ -0,0 +1,62 @@ +use chrono::NaiveDate; +use rust_decimal_macros::dec; +use xbuilder::prelude::*; + +use crate::common::invoice_base; +use crate::common::{assert_invoice, detalle_base}; + +mod common; + +const BASE: &str = "tests/resources/e2e/renderer/invoice/InvoiceFormaPagoTest"; + +#[test] +fn invoice_forma_pago_contado_defecto() { + let mut invoice = Invoice { + detalles: vec![ + Detalle { + precio: Some(dec!(100)), + ..detalle_base("Item1", dec!(10)) + }, + Detalle { + precio: Some(dec!(100)), + ..detalle_base("Item2", dec!(10)) + }, + ], + ..invoice_base() + }; + + assert_invoice(&mut invoice, &format!("{BASE}/sinFormaPago.xml")); +} + +#[test] +fn invoice_forma_pago_credito() { + let mut invoice = Invoice { + detalles: vec![ + Detalle { + precio: Some(dec!(100)), + ..detalle_base("Item1", dec!(10)) + }, + Detalle { + precio: Some(dec!(100)), + ..detalle_base("Item2", dec!(10)) + }, + ], + forma_de_pago: Some(FormaDePago { + tipo: None, + total: None, + cuotas: vec![ + CuotaDePago { + importe: dec!(10), + fecha_pago: NaiveDate::from_ymd_opt(2022, 1, 20).unwrap(), + }, + CuotaDePago { + importe: dec!(20), + fecha_pago: NaiveDate::from_ymd_opt(2022, 2, 20).unwrap(), + }, + ], + }), + ..invoice_base() + }; + + assert_invoice(&mut invoice, &format!("{BASE}/conFormaPago.xml")); +}