Skip to content

Commit

Permalink
Refactor catalogs
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosthe19916 committed Dec 5, 2023
1 parent e0bf83a commit 4dcbd14
Show file tree
Hide file tree
Showing 20 changed files with 111 additions and 217 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
![CI](https://github.com/project-openubl/xbuilder/workflows/CI/badge.svg)

[![Project Chat](https://img.shields.io/badge/zulip-join_chat-brightgreen.svg?style=for-the-badge&logo=zulip)](https://projectopenubl.zulipchat.com/)
[![Supported JVM Versions](https://img.shields.io/badge/JVM-11--17-brightgreen.svg?style=for-the-badge&logo=Java)](https://github.com/project-openubl/xbuilder/actions/runs/472762588/)

| Artifact | Version |
|----------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| XBuilder | [![Maven Central](https://img.shields.io/maven-central/v/io.github.project-openubl/xbuilder)](https://search.maven.org/artifact/io.github.project-openubl/xbuilder/) |
| XBuilder Quarkus extension | [![Maven Central](https://img.shields.io/maven-central/v/io.github.project-openubl/quarkus-xbuilder)](https://search.maven.org/artifact/io.github.project-openubl/quarkus-xbuilder/) |

# XBuilder

Expand Down
122 changes: 67 additions & 55 deletions src/catalogs/mod.rs → src/catalogs.rs
Original file line number Diff line number Diff line change
@@ -1,67 +1,19 @@
use std::fmt;
use std::fmt::{Display, Formatter};

/// A trait for providing the 'code' of a catalog
pub trait Catalog {
fn code(&self) -> &str;
}

pub trait Label {
fn label(&self) -> &str;
}

pub fn catalog7_value_of_code(code: &str) -> Option<Catalog7> {
match code {
"10" => Some(Catalog7::GravadoOperacionOnerosa),
"11" => Some(Catalog7::GravadoRetiroPorPremio),
"12" => Some(Catalog7::GravadoRetiroPorDonacion),
"13" => Some(Catalog7::GravadoRetiro),
"14" => Some(Catalog7::GravadoRetiroPorPublicidad),
"15" => Some(Catalog7::GravadoBonificaciones),
"16" => Some(Catalog7::GravadoRetiroPorEntregaATrabajadores),
"17" => Some(Catalog7::GravadoIvap),

"20" => Some(Catalog7::ExoneradoOperacionOnerosa),
"21" => Some(Catalog7::ExoneradoTransferenciaGratuita),

"30" => Some(Catalog7::InafectoOperacionOnerosa),
"31" => Some(Catalog7::InafectoRetiroPorBonificacion),
"32" => Some(Catalog7::InafectoRetiro),
"33" => Some(Catalog7::InafectoRetiroPorMuestrasMedicas),
"34" => Some(Catalog7::InafectoRetiroPorConvenioColectivo),
"35" => Some(Catalog7::InafectoRetiroPorPremio),
"36" => Some(Catalog7::InafectoRetiroPorPublicidad),

"40" => Some(Catalog7::Exportacion),
_ => None,
}
/// A trait for getting a catalog from its 'code'
pub trait FromCode: Sized {
fn from_code(code: &str) -> Result<Self, String>;
}

pub fn catalog53_value_of_code(code: &'static str) -> Option<Catalog53> {
match code {
"00" => Some(Catalog53::DescuentoAfectaBaseImponibleIgvIvap),
"01" => Some(Catalog53::DescuentoNoAfectaBaseImponibleIgvIvap),
"02" => Some(Catalog53::DescuentoGlobalAfectaBaseImponibleIgvIvap),
"03" => Some(Catalog53::DescuentoGlobalNoAfectaBaseImponibleIgvIvap),
"04" => Some(Catalog53::DescuentoGlobalPorAnticiposGravadosAfectaBaseImponibleIgvIvap),
"05" => Some(Catalog53::DescuentoGlobalPorAnticiposExonerados),
"06" => Some(Catalog53::DescuentoGlobalPorAnticiposInafectos),
"07" => Some(Catalog53::FactorDeCompensacion),
"20" => Some(Catalog53::AnticipoDeIsc),
"45" => Some(Catalog53::FISE),
"46" => Some(Catalog53::RecargoAlConsumoYOPropinas),
"47" => Some(Catalog53::CargosQueAfectanBaseImponibleIgvIvap),
"48" => Some(Catalog53::CargosQueNoAfectanBaseImponibleIgvIvap),
"49" => Some(Catalog53::CargosGlobalesQueAfectanBaseImponibleIgvIvap),
"50" => Some(Catalog53::CargosGlobalesQueNoAfectanBaseImponibleIgvIvap),
"51" => Some(Catalog53::PercepcionVentaInterna),
"52" => Some(Catalog53::PercepcionALaAdquisicionDeCombustible),
"53" => Some(Catalog53::PercepcionRealizadaAlAgenteDePercepcionConTasaEspecial),
"61" => Some(Catalog53::FactorDeAportacion),
"62" => Some(Catalog53::RetencionDeRentaPorAnticipos),
"63" => Some(Catalog53::RetencionDelIgv),
"64" => Some(Catalog53::RetencionDeRentaDeSegundaCategoria),
_ => None,
}
/// A trait for providing a 'label' representation
pub trait Label {
fn label(&self) -> &str;
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -367,6 +319,36 @@ impl Display for Catalog7 {
}
}

impl FromCode for Catalog7 {
fn from_code(code: &str) -> Result<Self, String> {
match code {
"10" => Ok(Catalog7::GravadoOperacionOnerosa),
"11" => Ok(Catalog7::GravadoRetiroPorPremio),
"12" => Ok(Catalog7::GravadoRetiroPorDonacion),
"13" => Ok(Catalog7::GravadoRetiro),
"14" => Ok(Catalog7::GravadoRetiroPorPublicidad),
"15" => Ok(Catalog7::GravadoBonificaciones),
"16" => Ok(Catalog7::GravadoRetiroPorEntregaATrabajadores),
"17" => Ok(Catalog7::GravadoIvap),

"20" => Ok(Catalog7::ExoneradoOperacionOnerosa),
"21" => Ok(Catalog7::ExoneradoTransferenciaGratuita),

"30" => Ok(Catalog7::InafectoOperacionOnerosa),
"31" => Ok(Catalog7::InafectoRetiroPorBonificacion),
"32" => Ok(Catalog7::InafectoRetiro),
"33" => Ok(Catalog7::InafectoRetiroPorMuestrasMedicas),
"34" => Ok(Catalog7::InafectoRetiroPorConvenioColectivo),
"35" => Ok(Catalog7::InafectoRetiroPorPremio),
"36" => Ok(Catalog7::InafectoRetiroPorPublicidad),

"40" => Ok(Catalog7::Exportacion),

_ => Err(format!("code {code} did not match")),
}
}
}

#[derive(Clone, Debug)]
pub enum Catalog8 {
SistemaAlValor,
Expand Down Expand Up @@ -582,6 +564,36 @@ impl Catalog for Catalog53 {
}
}

impl FromCode for Catalog53 {
fn from_code(code: &str) -> Result<Self, String> {
match code {
"00" => Ok(Catalog53::DescuentoAfectaBaseImponibleIgvIvap),
"01" => Ok(Catalog53::DescuentoNoAfectaBaseImponibleIgvIvap),
"02" => Ok(Catalog53::DescuentoGlobalAfectaBaseImponibleIgvIvap),
"03" => Ok(Catalog53::DescuentoGlobalNoAfectaBaseImponibleIgvIvap),
"04" => Ok(Catalog53::DescuentoGlobalPorAnticiposGravadosAfectaBaseImponibleIgvIvap),
"05" => Ok(Catalog53::DescuentoGlobalPorAnticiposExonerados),
"06" => Ok(Catalog53::DescuentoGlobalPorAnticiposInafectos),
"07" => Ok(Catalog53::FactorDeCompensacion),
"20" => Ok(Catalog53::AnticipoDeIsc),
"45" => Ok(Catalog53::FISE),
"46" => Ok(Catalog53::RecargoAlConsumoYOPropinas),
"47" => Ok(Catalog53::CargosQueAfectanBaseImponibleIgvIvap),
"48" => Ok(Catalog53::CargosQueNoAfectanBaseImponibleIgvIvap),
"49" => Ok(Catalog53::CargosGlobalesQueAfectanBaseImponibleIgvIvap),
"50" => Ok(Catalog53::CargosGlobalesQueNoAfectanBaseImponibleIgvIvap),
"51" => Ok(Catalog53::PercepcionVentaInterna),
"52" => Ok(Catalog53::PercepcionALaAdquisicionDeCombustible),
"53" => Ok(Catalog53::PercepcionRealizadaAlAgenteDePercepcionConTasaEspecial),
"61" => Ok(Catalog53::FactorDeAportacion),
"62" => Ok(Catalog53::RetencionDeRentaPorAnticipos),
"63" => Ok(Catalog53::RetencionDelIgv),
"64" => Ok(Catalog53::RetencionDeRentaDeSegundaCategoria),
_ => Err(format!("code {code} did not match")),
}
}
}

#[derive(Clone, Debug)]
pub enum Catalog54 {
Azucar,
Expand Down
4 changes: 2 additions & 2 deletions 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_value_of_code, Catalog7, Catalog7Group};
use crate::catalogs::{Catalog7, Catalog7Group, FromCode};
use crate::enricher::rules::phase1fill::detalle::detalles::DetalleDefaults;
use crate::models::traits::detalle::igvtasa::{DetalleIGVTasaGetter, DetalleIGVTasaSetter};
use crate::models::traits::detalle::igvtipo::DetalleIGVTipoGetter;
Expand All @@ -16,7 +16,7 @@ where
fn fill(&mut self, defaults: &DetalleDefaults) -> bool {
match (self.get_igvtasa(), *self.get_igvtipo()) {
(None, Some(igv_tipo)) => {
if let Some(catalog) = catalog7_value_of_code(igv_tipo) {
if let Ok(catalog) = Catalog7::from_code(igv_tipo) {
let tasa = match catalog {
Catalog7::GravadoIvap => defaults.ivap_tasa,
_ => match catalog.group() {
Expand Down
4 changes: 2 additions & 2 deletions src/enricher/rules/phase1fill/detalle/isctasa.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use log::trace;
use rust_decimal::Decimal;

use crate::catalogs::{catalog7_value_of_code, Catalog7Group};
use crate::catalogs::{Catalog7, Catalog7Group, FromCode};
use crate::enricher::rules::phase1fill::detalle::detalles::DetalleDefaults;
use crate::models::traits::detalle::igvtipo::DetalleIGVTipoGetter;
use crate::models::traits::detalle::isctasa::{DetalleISCTasaGetter, DetalleISCTasaSetter};
Expand All @@ -17,7 +17,7 @@ where
fn fill(&mut self, _: &DetalleDefaults) -> bool {
match (&self.get_isctasa(), &self.get_igvtipo()) {
(Some(isc_tasa), Some(igv_tipo)) => {
if let Some(catalog) = catalog7_value_of_code(igv_tipo) {
if let Ok(catalog) = Catalog7::from_code(igv_tipo) {
let tasa = if !catalog.onerosa() {
Decimal::ZERO
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/enricher/rules/phase1fill/detalle/precioreferenciatipo.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::catalogs::{catalog7_value_of_code, Catalog, Catalog16};
use crate::catalogs::{Catalog, Catalog16, Catalog7, FromCode};
use crate::enricher::rules::phase1fill::detalle::detalles::DetalleDefaults;
use crate::models::traits::detalle::igvtipo::DetalleIGVTipoGetter;
use crate::models::traits::detalle::precioreferenciatipo::{
Expand All @@ -16,7 +16,7 @@ where
fn fill(&mut self, _: &DetalleDefaults) -> bool {
match (self.get_precioreferenciatipo(), *self.get_igvtipo()) {
(None, Some(igv_tipo)) => {
if let Some(catalog) = catalog7_value_of_code(igv_tipo) {
if let Ok(catalog) = Catalog7::from_code(igv_tipo) {
let catalog16 = if catalog.onerosa() {
&Catalog16::PrecioUnitarioIncluyeIgv
} else {
Expand Down
2 changes: 1 addition & 1 deletion 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 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use log::warn;

use crate::catalogs::catalog7_value_of_code;
use crate::catalogs::FromCode;
use crate::models::traits::detalle::cantidad::DetalleCantidadGetter;
use crate::models::traits::detalle::igvbaseimponible::{
DetalleIGVBaseImponibleGetter, DetalleIGVBaseImponibleSetter,
Expand All @@ -9,6 +9,7 @@ use crate::models::traits::detalle::igvtipo::DetalleIGVTipoGetter;
use crate::models::traits::detalle::isc::DetalleISCGetter;
use crate::models::traits::detalle::precio::DetallePrecioGetter;
use crate::models::traits::detalle::precioreferencia::DetallePrecioReferenciaGetter;
use crate::prelude::Catalog7;

pub trait DetalleIGVBaseImponibleProcessRule {
fn process(&mut self) -> bool;
Expand All @@ -33,7 +34,7 @@ where
&self.get_isc(),
) {
(None, Some(igv_tipo), Some(precio), Some(precio_referencia), Some(isc)) => {
if let Some(catalog) = catalog7_value_of_code(igv_tipo) {
if let Ok(catalog) = Catalog7::from_code(igv_tipo) {
let base_imponible = if catalog.onerosa() {
self.get_cantidad() * precio
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/enricher/rules/phase2process/detalle/isc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rust_decimal::Decimal;

use crate::catalogs::{catalog7_value_of_code, Catalog7Group};
use crate::catalogs::{Catalog7, Catalog7Group, FromCode};
use crate::models::traits::detalle::igvtipo::DetalleIGVTipoGetter;
use crate::models::traits::detalle::isc::{DetalleISCGetter, DetalleISCSetter};
use crate::models::traits::detalle::iscbaseimponible::DetalleISCBaseImponibleGetter;
Expand All @@ -26,7 +26,7 @@ where
&self.get_igvtipo(),
) {
(None, Some(isc_base_imponible), Some(isc_tasa), Some(igv_tipo)) => {
if let Some(catalog) = catalog7_value_of_code(igv_tipo) {
if let Ok(catalog) = Catalog7::from_code(igv_tipo) {
let tasa = if catalog.onerosa() {
match catalog.group() {
Catalog7Group::Gravado => *isc_tasa,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::catalogs::catalog7_value_of_code;
use crate::catalogs::{Catalog7, FromCode};
use crate::models::traits::detalle::cantidad::DetalleCantidadGetter;
use crate::models::traits::detalle::igvtipo::DetalleIGVTipoGetter;
use crate::models::traits::detalle::iscbaseimponible::{
Expand Down Expand Up @@ -28,7 +28,7 @@ where
&self.get_precioreferencia(),
) {
(None, Some(igv_tipo), Some(precio), Some(precio_referencia)) => {
if let Some(catalog) = catalog7_value_of_code(igv_tipo) {
if let Ok(catalog) = Catalog7::from_code(igv_tipo) {
let base_imponible = if catalog.onerosa() {
self.get_cantidad() * *precio
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/enricher/rules/phase2process/detalle/precio.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rust_decimal::Decimal;

use crate::catalogs::catalog7_value_of_code;
use crate::catalogs::{Catalog7, FromCode};
use crate::models::traits::detalle::igvtasa::DetalleIGVTasaGetter;
use crate::models::traits::detalle::igvtipo::DetalleIGVTipoGetter;
use crate::models::traits::detalle::isctasa::DetalleISCTasaGetter;
Expand Down Expand Up @@ -29,7 +29,7 @@ where
&self.get_precioconimpuestos(),
) {
(None, Some(igv_tipo), Some(igv_tasa), Some(isc_tasa), Some(precio_con_impuestos)) => {
if let Some(catalog) = catalog7_value_of_code(igv_tipo) {
if let Ok(catalog) = Catalog7::from_code(igv_tipo) {
let precio = if catalog.onerosa() {
precio_con_impuestos
/ (Decimal::ONE + *igv_tasa)
Expand All @@ -46,7 +46,7 @@ where
}
// Si operacion onerosa y precio es diferente de cero => modificarlo
(Some(precio), Some(igv_tipo), Some(_), Some(_), Some(_)) => {
if let Some(catalog) = catalog7_value_of_code(igv_tipo) {
if let Ok(catalog) = Catalog7::from_code(igv_tipo) {
if !catalog.onerosa() && precio > &Decimal::ZERO {
self.set_precio(Decimal::ZERO);
true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rust_decimal_macros::dec;

use crate::catalogs::catalog7_value_of_code;
use crate::catalogs::{Catalog7, FromCode};
use crate::models::traits::detalle::igvtasa::DetalleIGVTasaGetter;
use crate::models::traits::detalle::igvtipo::DetalleIGVTipoGetter;
use crate::models::traits::detalle::isctasa::DetalleISCTasaGetter;
Expand Down Expand Up @@ -31,7 +31,7 @@ where
&self.get_precio(),
) {
(None, Some(igv_tipo), Some(igv_tasa), Some(isc_tasa), Some(precio)) => {
if let Some(catalog) = catalog7_value_of_code(igv_tipo) {
if let Ok(catalog) = Catalog7::from_code(igv_tipo) {
let precio_con_impuestos = if catalog.onerosa() {
precio * (dec!(1) + *igv_tasa) * (dec!(1) + *isc_tasa)
} else {
Expand All @@ -46,7 +46,7 @@ where
}
// Si operacion no es onerosa y precio es diferente de cero => modificarlo
(Some(precio_con_impuestos), Some(igv_tipo), Some(_), Some(_), Some(_)) => {
if let Some(catalog) = catalog7_value_of_code(igv_tipo) {
if let Ok(catalog) = Catalog7::from_code(igv_tipo) {
if !catalog.onerosa() && precio_con_impuestos > &dec!(0) {
self.set_precioconimpuestos(dec!(0));
true
Expand Down
5 changes: 3 additions & 2 deletions src/enricher/rules/phase2process/detalle/precioreferencia.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::catalogs::catalog7_value_of_code;
use crate::catalogs::FromCode;
use crate::models::traits::detalle::igvtipo::DetalleIGVTipoGetter;
use crate::models::traits::detalle::precio::DetallePrecioGetter;
use crate::models::traits::detalle::precioconimpuestos::DetallePrecioConImpuestosGetter;
use crate::models::traits::detalle::precioreferencia::{
DetallePrecioReferenciaGetter, DetallePrecioReferenciaSetter,
};
use crate::prelude::Catalog7;

pub trait DetallePrecioReferenciaProcessRule {
fn process(&mut self) -> bool;
Expand All @@ -26,7 +27,7 @@ where
&self.get_precioconimpuestos(),
) {
(None, Some(igv_tipo), Some(precio), Some(precio_con_impuestos)) => {
if let Some(catalog) = catalog7_value_of_code(igv_tipo) {
if let Ok(catalog) = Catalog7::from_code(igv_tipo) {
let precio_referencia = if catalog.onerosa() {
precio_con_impuestos
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/enricher/rules/phase2process/detalle/totalimpuestos.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rust_decimal_macros::dec;

use crate::catalogs::catalog7_value_of_code;
use crate::catalogs::{Catalog7, FromCode};
use crate::models::traits::detalle::icb::DetalleICBGetter;
use crate::models::traits::detalle::igv::DetalleIGVGetter;
use crate::models::traits::detalle::igvtipo::DetalleIGVTipoGetter;
Expand Down Expand Up @@ -31,7 +31,7 @@ where
&self.get_isc(),
) {
(None, Some(igv_tipo), Some(igv), Some(icb), Some(isc)) => {
if let Some(catalog) = catalog7_value_of_code(igv_tipo) {
if let Ok(catalog) = Catalog7::from_code(igv_tipo) {
let igv_isc = if catalog.onerosa() {
(*igv, *isc)
} else {
Expand Down
Loading

0 comments on commit 4dcbd14

Please sign in to comment.