Skip to content

feat(descuentos globales): add descuentos globales #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License - 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.project.openubl.xbuilder.content.catalogs;

public enum Catalog53_DescuentoGlobal implements Catalog {
DESCUENTO_GLOBAL_AFECTA_BASE_IMPONIBLE_IGV_IVAP("02"),
DESCUENTO_GLOBAL_NO_AFECTA_BASE_IMPONIBLE_IGV_IVAP("03");

private final String code;

Catalog53_DescuentoGlobal(String code) {
this.code = code;
}

@Override
public String getCode() {
return code;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License - 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.project.openubl.xbuilder.content.models.standard.general;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.math.BigDecimal;

@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class Descuento {

@Schema(requiredMode = Schema.RequiredMode.REQUIRED, description = "Catalogo 53")
private String tipoDescuento;

@Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED)
private BigDecimal factor;

@Schema(requiredMode = Schema.RequiredMode.REQUIRED)
private BigDecimal monto;

@Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED)
private BigDecimal montoBase;

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,7 @@ public class Invoice extends SalesDocument {

@Singular
private List<DocumentoRelacionado> otrosDocumentosTributariosRelacionados;

@Singular
private List<Descuento> descuentos;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ public class TotalImporteInvoice extends TotalImporte {

@Schema(minimum = "0", requiredMode = Schema.RequiredMode.REQUIRED)
private BigDecimal anticipos;

@Schema(minimum = "0", requiredMode = Schema.RequiredMode.REQUIRED)
private BigDecimal descuentos;
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public void enrich(Invoice input) {
.build();
RuleUnit ruleUnitBody = new BodyRuleUnit(phaseType, defaults, ruleContextBody);
input.getDetalles().forEach(ruleUnitBody::modify);

input.getAnticipos().forEach(ruleUnitBody::modify);
input.getDescuentos().forEach(ruleUnitBody::modify);
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License - 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.project.openubl.xbuilder.enricher.kie.rules.enrich.body.descuento;

import io.github.project.openubl.xbuilder.content.models.standard.general.Descuento;
import io.github.project.openubl.xbuilder.enricher.kie.AbstractBodyRule;
import io.github.project.openubl.xbuilder.enricher.kie.RulePhase;

import java.math.BigDecimal;
import java.util.function.Consumer;

import static io.github.project.openubl.xbuilder.enricher.kie.rules.utils.Helpers.isDescuento;
import static io.github.project.openubl.xbuilder.enricher.kie.rules.utils.Helpers.whenDescuento;

@RulePhase(type = RulePhase.PhaseType.ENRICH)
public class FactorRule extends AbstractBodyRule {

@Override
public boolean test(Object object) {
return isDescuento.test(object) && whenDescuento.apply(object)
.map(descuento -> descuento.getFactor() == null)
.orElse(false);
}

@Override
public void modify(Object object) {
Consumer<Descuento> consumer = descuento -> {
descuento.setFactor(BigDecimal.ONE);
};
whenDescuento.apply(object).ifPresent(consumer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License - 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.project.openubl.xbuilder.enricher.kie.rules.enrich.body.descuento;

import io.github.project.openubl.xbuilder.content.models.standard.general.Descuento;
import io.github.project.openubl.xbuilder.enricher.kie.AbstractBodyRule;
import io.github.project.openubl.xbuilder.enricher.kie.RulePhase;

import java.math.BigDecimal;
import java.util.function.Consumer;

import static io.github.project.openubl.xbuilder.enricher.kie.rules.utils.Helpers.isDescuento;
import static io.github.project.openubl.xbuilder.enricher.kie.rules.utils.Helpers.whenDescuento;

@RulePhase(type = RulePhase.PhaseType.ENRICH)
public class MontoBaseRule extends AbstractBodyRule {

@Override
public boolean test(Object object) {
return isDescuento.test(object) && whenDescuento.apply(object)
.map(descuento -> descuento.getMontoBase() == null && descuento.getMonto() != null)
.orElse(false);
}

@Override
public void modify(Object object) {
Consumer<Descuento> consumer = descuento -> {
descuento.setMontoBase(descuento.getMonto());
};
whenDescuento.apply(object).ifPresent(consumer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License - 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.project.openubl.xbuilder.enricher.kie.rules.enrich.body.descuento;

import io.github.project.openubl.xbuilder.content.catalogs.Catalog;
import io.github.project.openubl.xbuilder.content.catalogs.Catalog53_DescuentoGlobal;
import io.github.project.openubl.xbuilder.content.models.standard.general.Descuento;
import io.github.project.openubl.xbuilder.enricher.kie.AbstractBodyRule;
import io.github.project.openubl.xbuilder.enricher.kie.RulePhase;

import java.util.function.Consumer;

import static io.github.project.openubl.xbuilder.enricher.kie.rules.utils.Helpers.isDescuento;
import static io.github.project.openubl.xbuilder.enricher.kie.rules.utils.Helpers.whenDescuento;

/**
* Rule for: {@link Descuento#tipoDescuento}
*
* @author <a href="mailto:[email protected]">Carlos Feria</a>
*/
@RulePhase(type = RulePhase.PhaseType.ENRICH)
public class TipoDescuentoRule extends AbstractBodyRule {

@Override
public boolean test(Object object) {
return isDescuento.test(object);
}

@Override
public void modify(Object object) {
Consumer<Descuento> consumer = descuento -> {
String tipoDescuento;
if (descuento.getTipoDescuento() == null) {
tipoDescuento = Catalog53_DescuentoGlobal.DESCUENTO_GLOBAL_NO_AFECTA_BASE_IMPONIBLE_IGV_IVAP.getCode();
} else {
Catalog53_DescuentoGlobal catalog53 = Catalog
.valueOfCode(Catalog53_DescuentoGlobal.class, descuento.getTipoDescuento())
.orElseThrow(Catalog.invalidCatalogValue);
tipoDescuento = catalog53.getCode();
}

descuento.setTipoDescuento(tipoDescuento);
};
whenDescuento.apply(object).ifPresent(consumer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@
*/
package io.github.project.openubl.xbuilder.enricher.kie.rules.summary.header.invoice;

import io.github.project.openubl.xbuilder.content.catalogs.Catalog;
import io.github.project.openubl.xbuilder.content.catalogs.Catalog53_DescuentoGlobal;
import io.github.project.openubl.xbuilder.content.models.standard.general.Anticipo;
import io.github.project.openubl.xbuilder.content.models.standard.general.Descuento;
import io.github.project.openubl.xbuilder.content.models.standard.general.Invoice;
import io.github.project.openubl.xbuilder.content.models.standard.general.TotalImporteInvoice;
import io.github.project.openubl.xbuilder.enricher.kie.AbstractHeaderRule;
import io.github.project.openubl.xbuilder.enricher.kie.RulePhase;
import io.github.project.openubl.xbuilder.enricher.kie.rules.utils.DetalleUtils;

import java.math.BigDecimal;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.stream.Collectors;

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

@Override
public boolean test(Object object) {
return (
isInvoice.test(object) &&
whenInvoice
.apply(object)
.map(invoice -> invoice.getTotalImporte() == null && invoice.getDetalles() != null)
.orElse(false)
return (isInvoice.test(object) && whenInvoice.apply(object)
.map(invoice -> invoice.getTotalImporte() == null && invoice.getDetalles() != null)
.orElse(false)
);
}

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

BigDecimal anticipos = invoice
.getAnticipos()
.stream()
// Anticipos
BigDecimal anticipos = invoice.getAnticipos().stream()
.map(Anticipo::getMonto)
.filter(Objects::nonNull)
.reduce(BigDecimal.ZERO, BigDecimal::add);

BigDecimal importeTotal = importeConImpuestos.subtract(anticipos);

invoice.setTotalImporte(
TotalImporteInvoice
.builder()
.importeSinImpuestos(importeSinImpuestos)
.importeConImpuestos(importeConImpuestos)
.anticipos(anticipos)
.importe(importeTotal)
.build()
// Descuentos
Map<Catalog53_DescuentoGlobal, BigDecimal> descuentos = invoice.getDescuentos().stream()
.filter(descuento -> descuento.getTipoDescuento() != null && descuento.getMonto() != null)
.collect(Collectors.groupingBy(
descuento -> Catalog.valueOfCode(Catalog53_DescuentoGlobal.class, descuento.getTipoDescuento()).orElseThrow(Catalog.invalidCatalogValue),
Collectors.reducing(BigDecimal.ZERO, Descuento::getMonto, BigDecimal::add)
));

BigDecimal descuentosQueAfectanBaseImponible_sinImpuestos = descuentos.getOrDefault(Catalog53_DescuentoGlobal.DESCUENTO_GLOBAL_AFECTA_BASE_IMPONIBLE_IGV_IVAP, BigDecimal.ZERO);
BigDecimal descuentosQueAfectanBaseImponible_conImpuestos = descuentosQueAfectanBaseImponible_sinImpuestos.multiply(invoice.getTasaIgv().add(BigDecimal.ONE));

BigDecimal descuentosQueNoAfectanBaseImponible_sinImpuestos = descuentos.getOrDefault(Catalog53_DescuentoGlobal.DESCUENTO_GLOBAL_NO_AFECTA_BASE_IMPONIBLE_IGV_IVAP, BigDecimal.ZERO);

//
importeSinImpuestos = importeSinImpuestos.subtract(descuentosQueAfectanBaseImponible_sinImpuestos);
importeConImpuestos = importeConImpuestos.subtract(descuentosQueAfectanBaseImponible_conImpuestos);
importeTotal = importeTotal
.subtract(descuentosQueAfectanBaseImponible_conImpuestos)
.subtract(descuentosQueNoAfectanBaseImponible_sinImpuestos);

// Set final values
invoice.setTotalImporte(TotalImporteInvoice.builder()
.importeSinImpuestos(importeSinImpuestos)
.importeConImpuestos(importeConImpuestos)
.descuentos(descuentosQueNoAfectanBaseImponible_sinImpuestos)
.anticipos(anticipos)
.importe(importeTotal)
.build()
);
};
whenInvoice.apply(object).ifPresent(consumer);
Expand Down
Loading