xbuilder/enricher/rules/phase2process/detalle/
isc.rs

1use rust_decimal::Decimal;
2
3use crate::catalogs::{Catalog7, Catalog7Group, FromCode};
4use crate::enricher::bounds::detalle::igv_tipo::DetalleIgvTipoGetter;
5use crate::enricher::bounds::detalle::isc::{DetalleISCSetter, DetalleIscGetter};
6use crate::enricher::bounds::detalle::isc_base_imponible::DetalleIscBaseImponibleGetter;
7use crate::enricher::bounds::detalle::isc_tasa::DetalleIscTasaGetter;
8
9pub trait DetalleISCProcessRule {
10    fn process(&mut self) -> bool;
11}
12
13impl<T> DetalleISCProcessRule for T
14where
15    T: DetalleIscGetter
16        + DetalleISCSetter
17        + DetalleIscBaseImponibleGetter
18        + DetalleIscTasaGetter
19        + DetalleIgvTipoGetter,
20{
21    fn process(&mut self) -> bool {
22        match (
23            &self.get_isc(),
24            &self.get_isc_base_imponible(),
25            &self.get_isc_tasa(),
26            &self.get_igv_tipo(),
27        ) {
28            (None, Some(isc_base_imponible), Some(isc_tasa), Some(igv_tipo)) => {
29                if let Ok(catalog) = Catalog7::from_code(igv_tipo) {
30                    let tasa = if catalog.onerosa() {
31                        match catalog.group() {
32                            Catalog7Group::Gravado => *isc_tasa,
33                            Catalog7Group::Exonerado => Decimal::ZERO,
34                            Catalog7Group::Inafecto => Decimal::ZERO,
35                            Catalog7Group::Exportacion => Decimal::ZERO,
36                            Catalog7Group::Gratuita => *isc_tasa,
37                        }
38                    } else {
39                        Decimal::ZERO
40                    };
41
42                    let isc = isc_base_imponible * tasa;
43                    self.set_isc(isc);
44                    true
45                } else {
46                    false
47                }
48            }
49            _ => false,
50        }
51    }
52}