Skip to content

Commit ffe17e8

Browse files
feat(descuentos globales): add descuentos globales (#179)
* feat(descuentos globales): add descuentos globales * Update snapshots * Fix quarkus snapshot * Fix quarkus snapshot * Add homologacion group4 tests
1 parent 1978ae2 commit ffe17e8

File tree

72 files changed

+3643
-45
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+3643
-45
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
3+
* and other contributors as indicated by the @author tags.
4+
*
5+
* Licensed under the Apache License - 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package io.github.project.openubl.xbuilder.content.catalogs;
18+
19+
public enum Catalog53_DescuentoGlobal implements Catalog {
20+
DESCUENTO_GLOBAL_AFECTA_BASE_IMPONIBLE_IGV_IVAP("02"),
21+
DESCUENTO_GLOBAL_NO_AFECTA_BASE_IMPONIBLE_IGV_IVAP("03");
22+
23+
private final String code;
24+
25+
Catalog53_DescuentoGlobal(String code) {
26+
this.code = code;
27+
}
28+
29+
@Override
30+
public String getCode() {
31+
return code;
32+
}
33+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
3+
* and other contributors as indicated by the @author tags.
4+
*
5+
* Licensed under the Apache License - 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package io.github.project.openubl.xbuilder.content.models.standard.general;
18+
19+
import io.swagger.v3.oas.annotations.media.Schema;
20+
import lombok.AllArgsConstructor;
21+
import lombok.Data;
22+
import lombok.NoArgsConstructor;
23+
import lombok.experimental.SuperBuilder;
24+
25+
import java.math.BigDecimal;
26+
27+
@Data
28+
@SuperBuilder
29+
@NoArgsConstructor
30+
@AllArgsConstructor
31+
public class Descuento {
32+
33+
@Schema(requiredMode = Schema.RequiredMode.REQUIRED, description = "Catalogo 53")
34+
private String tipoDescuento;
35+
36+
@Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED)
37+
private BigDecimal factor;
38+
39+
@Schema(requiredMode = Schema.RequiredMode.REQUIRED)
40+
private BigDecimal monto;
41+
42+
@Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED)
43+
private BigDecimal montoBase;
44+
45+
}

core/src/main/java/io/github/project/openubl/xbuilder/content/models/standard/general/Invoice.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,7 @@ public class Invoice extends SalesDocument {
6969

7070
@Singular
7171
private List<DocumentoRelacionado> otrosDocumentosTributariosRelacionados;
72+
73+
@Singular
74+
private List<Descuento> descuentos;
7275
}

core/src/main/java/io/github/project/openubl/xbuilder/content/models/standard/general/TotalImporteInvoice.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,7 @@ public class TotalImporteInvoice extends TotalImporte {
3434

3535
@Schema(minimum = "0", requiredMode = Schema.RequiredMode.REQUIRED)
3636
private BigDecimal anticipos;
37+
38+
@Schema(minimum = "0", requiredMode = Schema.RequiredMode.REQUIRED)
39+
private BigDecimal descuentos;
3740
}

core/src/main/java/io/github/project/openubl/xbuilder/enricher/ContentEnricher.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ public void enrich(Invoice input) {
6666
.build();
6767
RuleUnit ruleUnitBody = new BodyRuleUnit(phaseType, defaults, ruleContextBody);
6868
input.getDetalles().forEach(ruleUnitBody::modify);
69+
6970
input.getAnticipos().forEach(ruleUnitBody::modify);
71+
input.getDescuentos().forEach(ruleUnitBody::modify);
7072
});
7173
}
7274

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
3+
* and other contributors as indicated by the @author tags.
4+
*
5+
* Licensed under the Apache License - 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package io.github.project.openubl.xbuilder.enricher.kie.rules.enrich.body.descuento;
18+
19+
import io.github.project.openubl.xbuilder.content.models.standard.general.Descuento;
20+
import io.github.project.openubl.xbuilder.enricher.kie.AbstractBodyRule;
21+
import io.github.project.openubl.xbuilder.enricher.kie.RulePhase;
22+
23+
import java.math.BigDecimal;
24+
import java.util.function.Consumer;
25+
26+
import static io.github.project.openubl.xbuilder.enricher.kie.rules.utils.Helpers.isDescuento;
27+
import static io.github.project.openubl.xbuilder.enricher.kie.rules.utils.Helpers.whenDescuento;
28+
29+
@RulePhase(type = RulePhase.PhaseType.ENRICH)
30+
public class FactorRule extends AbstractBodyRule {
31+
32+
@Override
33+
public boolean test(Object object) {
34+
return isDescuento.test(object) && whenDescuento.apply(object)
35+
.map(descuento -> descuento.getFactor() == null)
36+
.orElse(false);
37+
}
38+
39+
@Override
40+
public void modify(Object object) {
41+
Consumer<Descuento> consumer = descuento -> {
42+
descuento.setFactor(BigDecimal.ONE);
43+
};
44+
whenDescuento.apply(object).ifPresent(consumer);
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
3+
* and other contributors as indicated by the @author tags.
4+
*
5+
* Licensed under the Apache License - 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package io.github.project.openubl.xbuilder.enricher.kie.rules.enrich.body.descuento;
18+
19+
import io.github.project.openubl.xbuilder.content.models.standard.general.Descuento;
20+
import io.github.project.openubl.xbuilder.enricher.kie.AbstractBodyRule;
21+
import io.github.project.openubl.xbuilder.enricher.kie.RulePhase;
22+
23+
import java.math.BigDecimal;
24+
import java.util.function.Consumer;
25+
26+
import static io.github.project.openubl.xbuilder.enricher.kie.rules.utils.Helpers.isDescuento;
27+
import static io.github.project.openubl.xbuilder.enricher.kie.rules.utils.Helpers.whenDescuento;
28+
29+
@RulePhase(type = RulePhase.PhaseType.ENRICH)
30+
public class MontoBaseRule extends AbstractBodyRule {
31+
32+
@Override
33+
public boolean test(Object object) {
34+
return isDescuento.test(object) && whenDescuento.apply(object)
35+
.map(descuento -> descuento.getMontoBase() == null && descuento.getMonto() != null)
36+
.orElse(false);
37+
}
38+
39+
@Override
40+
public void modify(Object object) {
41+
Consumer<Descuento> consumer = descuento -> {
42+
descuento.setMontoBase(descuento.getMonto());
43+
};
44+
whenDescuento.apply(object).ifPresent(consumer);
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
3+
* and other contributors as indicated by the @author tags.
4+
*
5+
* Licensed under the Apache License - 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package io.github.project.openubl.xbuilder.enricher.kie.rules.enrich.body.descuento;
18+
19+
import io.github.project.openubl.xbuilder.content.catalogs.Catalog;
20+
import io.github.project.openubl.xbuilder.content.catalogs.Catalog53_DescuentoGlobal;
21+
import io.github.project.openubl.xbuilder.content.models.standard.general.Descuento;
22+
import io.github.project.openubl.xbuilder.enricher.kie.AbstractBodyRule;
23+
import io.github.project.openubl.xbuilder.enricher.kie.RulePhase;
24+
25+
import java.util.function.Consumer;
26+
27+
import static io.github.project.openubl.xbuilder.enricher.kie.rules.utils.Helpers.isDescuento;
28+
import static io.github.project.openubl.xbuilder.enricher.kie.rules.utils.Helpers.whenDescuento;
29+
30+
/**
31+
* Rule for: {@link Descuento#tipoDescuento}
32+
*
33+
* @author <a href="mailto:carlosthe19916@gmail.com">Carlos Feria</a>
34+
*/
35+
@RulePhase(type = RulePhase.PhaseType.ENRICH)
36+
public class TipoDescuentoRule extends AbstractBodyRule {
37+
38+
@Override
39+
public boolean test(Object object) {
40+
return isDescuento.test(object);
41+
}
42+
43+
@Override
44+
public void modify(Object object) {
45+
Consumer<Descuento> consumer = descuento -> {
46+
String tipoDescuento;
47+
if (descuento.getTipoDescuento() == null) {
48+
tipoDescuento = Catalog53_DescuentoGlobal.DESCUENTO_GLOBAL_NO_AFECTA_BASE_IMPONIBLE_IGV_IVAP.getCode();
49+
} else {
50+
Catalog53_DescuentoGlobal catalog53 = Catalog
51+
.valueOfCode(Catalog53_DescuentoGlobal.class, descuento.getTipoDescuento())
52+
.orElseThrow(Catalog.invalidCatalogValue);
53+
tipoDescuento = catalog53.getCode();
54+
}
55+
56+
descuento.setTipoDescuento(tipoDescuento);
57+
};
58+
whenDescuento.apply(object).ifPresent(consumer);
59+
}
60+
}

core/src/main/java/io/github/project/openubl/xbuilder/enricher/kie/rules/summary/header/invoice/TotalImporteRule.java

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,21 @@
1616
*/
1717
package io.github.project.openubl.xbuilder.enricher.kie.rules.summary.header.invoice;
1818

19+
import io.github.project.openubl.xbuilder.content.catalogs.Catalog;
20+
import io.github.project.openubl.xbuilder.content.catalogs.Catalog53_DescuentoGlobal;
1921
import io.github.project.openubl.xbuilder.content.models.standard.general.Anticipo;
22+
import io.github.project.openubl.xbuilder.content.models.standard.general.Descuento;
2023
import io.github.project.openubl.xbuilder.content.models.standard.general.Invoice;
2124
import io.github.project.openubl.xbuilder.content.models.standard.general.TotalImporteInvoice;
2225
import io.github.project.openubl.xbuilder.enricher.kie.AbstractHeaderRule;
2326
import io.github.project.openubl.xbuilder.enricher.kie.RulePhase;
2427
import io.github.project.openubl.xbuilder.enricher.kie.rules.utils.DetalleUtils;
2528

2629
import java.math.BigDecimal;
30+
import java.util.Map;
2731
import java.util.Objects;
2832
import java.util.function.Consumer;
33+
import java.util.stream.Collectors;
2934

3035
import static io.github.project.openubl.xbuilder.enricher.kie.rules.utils.Helpers.isInvoice;
3136
import static io.github.project.openubl.xbuilder.enricher.kie.rules.utils.Helpers.whenInvoice;
@@ -35,12 +40,9 @@ public class TotalImporteRule extends AbstractHeaderRule {
3540

3641
@Override
3742
public boolean test(Object object) {
38-
return (
39-
isInvoice.test(object) &&
40-
whenInvoice
41-
.apply(object)
42-
.map(invoice -> invoice.getTotalImporte() == null && invoice.getDetalles() != null)
43-
.orElse(false)
43+
return (isInvoice.test(object) && whenInvoice.apply(object)
44+
.map(invoice -> invoice.getTotalImporte() == null && invoice.getDetalles() != null)
45+
.orElse(false)
4446
);
4547
}
4648

@@ -52,23 +54,42 @@ public void modify(Object object) {
5254
BigDecimal importeSinImpuestos = DetalleUtils.getImporteSinImpuestos(invoice.getDetalles());
5355
BigDecimal importeConImpuestos = importeSinImpuestos.add(totalImpuestos);
5456

55-
BigDecimal anticipos = invoice
56-
.getAnticipos()
57-
.stream()
57+
// Anticipos
58+
BigDecimal anticipos = invoice.getAnticipos().stream()
5859
.map(Anticipo::getMonto)
5960
.filter(Objects::nonNull)
6061
.reduce(BigDecimal.ZERO, BigDecimal::add);
6162

6263
BigDecimal importeTotal = importeConImpuestos.subtract(anticipos);
6364

64-
invoice.setTotalImporte(
65-
TotalImporteInvoice
66-
.builder()
67-
.importeSinImpuestos(importeSinImpuestos)
68-
.importeConImpuestos(importeConImpuestos)
69-
.anticipos(anticipos)
70-
.importe(importeTotal)
71-
.build()
65+
// Descuentos
66+
Map<Catalog53_DescuentoGlobal, BigDecimal> descuentos = invoice.getDescuentos().stream()
67+
.filter(descuento -> descuento.getTipoDescuento() != null && descuento.getMonto() != null)
68+
.collect(Collectors.groupingBy(
69+
descuento -> Catalog.valueOfCode(Catalog53_DescuentoGlobal.class, descuento.getTipoDescuento()).orElseThrow(Catalog.invalidCatalogValue),
70+
Collectors.reducing(BigDecimal.ZERO, Descuento::getMonto, BigDecimal::add)
71+
));
72+
73+
BigDecimal descuentosQueAfectanBaseImponible_sinImpuestos = descuentos.getOrDefault(Catalog53_DescuentoGlobal.DESCUENTO_GLOBAL_AFECTA_BASE_IMPONIBLE_IGV_IVAP, BigDecimal.ZERO);
74+
BigDecimal descuentosQueAfectanBaseImponible_conImpuestos = descuentosQueAfectanBaseImponible_sinImpuestos.multiply(invoice.getTasaIgv().add(BigDecimal.ONE));
75+
76+
BigDecimal descuentosQueNoAfectanBaseImponible_sinImpuestos = descuentos.getOrDefault(Catalog53_DescuentoGlobal.DESCUENTO_GLOBAL_NO_AFECTA_BASE_IMPONIBLE_IGV_IVAP, BigDecimal.ZERO);
77+
78+
//
79+
importeSinImpuestos = importeSinImpuestos.subtract(descuentosQueAfectanBaseImponible_sinImpuestos);
80+
importeConImpuestos = importeConImpuestos.subtract(descuentosQueAfectanBaseImponible_conImpuestos);
81+
importeTotal = importeTotal
82+
.subtract(descuentosQueAfectanBaseImponible_conImpuestos)
83+
.subtract(descuentosQueNoAfectanBaseImponible_sinImpuestos);
84+
85+
// Set final values
86+
invoice.setTotalImporte(TotalImporteInvoice.builder()
87+
.importeSinImpuestos(importeSinImpuestos)
88+
.importeConImpuestos(importeConImpuestos)
89+
.descuentos(descuentosQueNoAfectanBaseImponible_sinImpuestos)
90+
.anticipos(anticipos)
91+
.importe(importeTotal)
92+
.build()
7293
);
7394
};
7495
whenInvoice.apply(object).ifPresent(consumer);

0 commit comments

Comments
 (0)