Skip to content

Commit

Permalink
Add forma de pago tests
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosthe19916 committed Dec 4, 2023
1 parent f6f2c09 commit 6a38871
Show file tree
Hide file tree
Showing 21 changed files with 224 additions and 42 deletions.
18 changes: 9 additions & 9 deletions src/enricher/enrich.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand All @@ -175,23 +175,23 @@ where
+ InvoiceDescuentosEnrichRule
+ InvoiceFormaDePagoEnrichRule
+ InvoiceFormaDePagoTotalRule
+ InvoiceFormaDePagoTipoRule
+ InvoiceLeyendaDetraccionEnrichRule
+ InvoiceLeyendaDireccionEntregaEnrichRule
+ InvoiceLeyendaPercepcionEnrichRule
+ InvoiceFormaDePagoEnrichRule
+ InvoiceFormaDePagoTotalRule
+ InvoiceTipoComprobanteEnrichRule
+ InvoiceTipoOperacionEnrichRule,
{
fn enrich_invoice(&mut self, _: &Defaults) {
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),
Expand All @@ -212,7 +212,7 @@ where
let mut changed = true;

while changed {
let results = vec![
let results = [
NoteTipoComprobanteAfectadoEnrichRule::fill(self),
CreditNoteTipoEnrichRule::fill(self),
];
Expand All @@ -230,7 +230,7 @@ where
let mut changed = true;

while changed {
let results = vec![
let results = [
NoteTipoComprobanteAfectadoEnrichRule::fill(self),
DebitNoteTipoEnrichRule::fill(self),
];
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion src/enricher/rules/phase1fill/detalle/detalles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion src/enricher/rules/phase1fill/detalle/igvtasa.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down
4 changes: 2 additions & 2 deletions src/enricher/rules/phase1fill/invoice/anticipos.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,7 +19,7 @@ where
self.get_anticipos()
.iter_mut()
.map(|anticipo| {
let results = vec![
let results = [
AnticipoTipoRule::fill(anticipo),
AnticipoComprobanteTipoRule::fill(anticipo),
];
Expand Down
2 changes: 1 addition & 1 deletion src/enricher/rules/phase1fill/invoice/descuentos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
33 changes: 31 additions & 2 deletions src/enricher/rules/phase1fill/invoice/formadepago.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@ pub trait InvoiceFormaDePagoTotalRule {
fn fill(&mut self) -> bool;
}

pub trait InvoiceFormaDePagoTipoRule {
fn fill(&mut self) -> bool;
}

impl<T> 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,
});
Expand All @@ -32,6 +36,31 @@ where
}
}

impl<T> 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<T> InvoiceFormaDePagoTotalRule for T
where
T: InvoiceFormaDePagoGetter + InvoiceFormaDePagoSetter,
Expand Down
2 changes: 1 addition & 1 deletion src/enricher/rules/phase1fill/invoice/tipocomprobante.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/enricher/rules/phase2process/detalle/detalles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ where
self.get_detalles()
.iter_mut()
.map(|detalle| {
let results = vec![
let results = [
DetallePrecioProcessRule::process(detalle),
DetallePrecioConImpuestosProcessRule::process(detalle),
DetallePrecioReferenciaProcessRule::process(detalle),
];
if results.contains(&true) {
true
} else {
let results = vec![
let results = [
DetalleICBProcessRule::process(detalle),
DetalleICBAplicaProcessRule::process(detalle),
DetalleIGVProcessRule::process(detalle),
Expand Down
2 changes: 1 addition & 1 deletion src/enricher/rules/phase3summary/invoice/detraccion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/enricher/rules/phase3summary/invoice/percepcion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion src/enricher/rules/phase3summary/invoice/totalimporte.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/enricher/rules/phase3summary/invoice/totalimpuestos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/models/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub enum TipoFormaDePago {

#[derive(Clone, Debug, Serialize)]
pub struct FormaDePago {
pub tipo: TipoFormaDePago,
pub tipo: Option<TipoFormaDePago>,
pub cuotas: Vec<CuotaDePago>,
pub total: Option<Decimal>,
}
Expand Down
3 changes: 1 addition & 2 deletions src/prelude.rs
Original file line number Diff line number Diff line change
@@ -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;

29 changes: 27 additions & 2 deletions src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -69,6 +69,16 @@ pub fn currency(value: &Value, _: &HashMap<String, Value>) -> Result<Value, Erro
}
}

pub fn format03d(value: &Value, _: &HashMap<String, Value>) -> Result<Value, Error> {
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<bool> {
number_args_allowed("gt0", 0, params.len())?;
value_defined("gt0", value)?;
Expand All @@ -86,6 +96,19 @@ pub fn gt0(value: Option<&Value>, params: &[Value]) -> tera::Result<bool> {
}
}

pub fn credito(value: Option<&Value>, params: &[Value]) -> tera::Result<bool> {
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") {
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/templates/renderer/invoice.xml
Original file line number Diff line number Diff line change
@@ -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 -%}
<?xml version="1.0" encoding="ISO-8859-1"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
{%- include "ubl/standard/include/namespaces.xml" -%}
Expand Down Expand Up @@ -40,11 +41,10 @@
{%- include "ubl/common/signature.xml" %}
{% include "ubl/standard/include/supplier.xml" %}
{% include "ubl/standard/include/customer.xml" -%}
{%- if direccionEntrega %}
{%- if direccion_entrega %}
<cac:Delivery>
<cac:DeliveryLocation>
<cac:Address>
{% include "ubl/standard/include/address.xml" %}
<cac:Address>{{ macros_address::address(direccion=direccion_entrega) }}
</cac:Address>
</cac:DeliveryLocation>
</cac:Delivery>
Expand Down Expand Up @@ -117,7 +117,7 @@
{%- for it in detalles %}
<cac:InvoiceLine>
<cbc:ID>{{loop.index}}</cbc:ID>
<cbc:InvoicedQuantity unitCode="{{it.unidad_medida}}" unitCodeListAgencyName="United Nations Economic Commission for Europe" unitCodeListID="UN/ECE rec 20">{{it.cantidad}}</cbc:InvoicedQuantity>{{ macro::line(item=it) }}
<cbc:InvoicedQuantity unitCode="{{it.unidad_medida}}" unitCodeListAgencyName="United Nations Economic Commission for Europe" unitCodeListID="UN/ECE rec 20">{{it.cantidad}}</cbc:InvoicedQuantity>{{ macros_document_line::line(item=it) }}
</cac:InvoiceLine>
{%- endfor %}
</Invoice>
Loading

0 comments on commit 6a38871

Please sign in to comment.