From 1fe81ae8a875ba9d0752ead6e574a45fe00d0e81 Mon Sep 17 00:00:00 2001 From: Carlos Parada Date: Fri, 9 Aug 2024 11:16:33 -0400 Subject: [PATCH] Se agrega soporte a matener el porcentaje de descuento de la orden al copiar valores convertidos desde la orden hasta la factura --- .../erpya/lve/model/OrganizationRules.java | 4 +- .../erpya/lve/util/OrganizationRulesUtil.java | 42 ++++++++++++++++--- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/base/org/erpya/lve/model/OrganizationRules.java b/core/src/main/java/base/org/erpya/lve/model/OrganizationRules.java index 443bd22..fa57283 100644 --- a/core/src/main/java/base/org/erpya/lve/model/OrganizationRules.java +++ b/core/src/main/java/base/org/erpya/lve/model/OrganizationRules.java @@ -85,7 +85,7 @@ public String modelChange(PO entity, int type) throws Exception { if(!invoice.isReversal() && documentType.get_ValueAsBoolean(LVEUtil.COLUMNNAME_IsInvoicedWithOrgCurrency) && invoiceCurrencyId > 0) { - OrganizationRulesUtil.recalculateInvoiceLineRate(invoice, invoiceLine); + OrganizationRulesUtil.recalculateInvoiceLineRate(invoice, invoiceLine, true); } } } else if(entity.get_TableName().equals(I_C_Invoice.Table_Name)) { @@ -109,7 +109,7 @@ public String modelChange(PO entity, int type) throws Exception { invoice.setC_Currency_ID(newCurrency.getC_Currency_ID()); // Recalculate Lines Arrays.asList(invoice.getLines(true)).forEach(invoiceLine -> { - OrganizationRulesUtil.recalculateInvoiceLineRate(invoice, invoiceLine); + OrganizationRulesUtil.recalculateInvoiceLineRate(invoice, invoiceLine, true); invoiceLine.saveEx(); }); } diff --git a/core/src/main/java/base/org/erpya/lve/util/OrganizationRulesUtil.java b/core/src/main/java/base/org/erpya/lve/util/OrganizationRulesUtil.java index 693f80b..12bfdb7 100644 --- a/core/src/main/java/base/org/erpya/lve/util/OrganizationRulesUtil.java +++ b/core/src/main/java/base/org/erpya/lve/util/OrganizationRulesUtil.java @@ -18,6 +18,7 @@ package org.erpya.lve.util; import java.math.BigDecimal; +import java.math.MathContext; import java.math.RoundingMode; import java.util.Optional; @@ -28,6 +29,7 @@ import org.compiere.model.MInvoiceLine; import org.compiere.model.MOrder; import org.compiere.model.MOrderLine; +import org.compiere.model.MUOMConversion; import org.compiere.util.Env; /** @@ -37,12 +39,22 @@ public class OrganizationRulesUtil { /** - * Recaulculate Invoice Line from Order + * Recalculate Invoice Line from Order * @param invoice * @param invoiceLine * @return */ public static final void recalculateInvoiceLineRate(MInvoice invoice, MInvoiceLine invoiceLine) { + recalculateInvoiceLineRate(invoice, invoiceLine, false); + } + + /** + * Recalculate Invoice Line from Order + * @param invoice + * @param invoiceLine + * @param maintainPriceDiscount + */ + public static final void recalculateInvoiceLineRate(MInvoice invoice, MInvoiceLine invoiceLine, boolean maintainPriceDiscount) { if(invoiceLine.getC_OrderLine_ID() > 0) { if(!invoiceLine.isProcessed()) { MOrderLine orderLine = (MOrderLine) invoiceLine.getC_OrderLine(); @@ -68,10 +80,30 @@ public static final void recalculateInvoiceLineRate(MInvoice invoice, MInvoiceLi BigDecimal invoicePriceList = orderPriceList.multiply(conversionRate).setScale(currencyTo.getStdPrecision(), RoundingMode.HALF_UP); BigDecimal invoicePriceActual = orderPriceActual.multiply(conversionRate).setScale(currencyTo.getStdPrecision(), RoundingMode.HALF_UP); BigDecimal invoicePriceEntered = orderPriceEntered.multiply(conversionRate).setScale(currencyTo.getStdPrecision(), RoundingMode.HALF_UP); - // Set Price - invoiceLine.setPriceList(invoicePriceList); - invoiceLine.setPriceActual(invoicePriceActual); - invoiceLine.setPriceEntered(invoicePriceEntered); + + if (maintainPriceDiscount + && orderLine.getDiscount().compareTo(Env.ZERO) !=0 + && orderLine.getM_Product_ID() > 0) { + //Set Price + BigDecimal discount = invoicePriceList.multiply(orderLine.getDiscount().divide(Env.ONEHUNDRED, MathContext.DECIMAL128), MathContext.DECIMAL128); + BigDecimal priceWithDiscount = invoicePriceList.subtract(discount); + invoiceLine.setPriceList(invoicePriceList); + + if (invoiceLine.getQtyEntered().compareTo(invoiceLine.getQtyInvoiced()) == 0) + invoiceLine.setPrice(priceWithDiscount); + else { + invoiceLine.setPriceActual(priceWithDiscount); + BigDecimal priceEntered = MUOMConversion.convertProductFrom (invoiceLine.getCtx(), invoiceLine.getM_Product_ID(), + invoiceLine.getC_UOM_ID(), invoiceLine.getPriceActual()); + invoiceLine.setPriceEntered(priceEntered); + } + }else { + //Set Price + invoiceLine.setPriceList(invoicePriceList); + invoiceLine.setPriceActual(invoicePriceActual); + invoiceLine.setPriceEntered(invoicePriceEntered); + } + invoiceLine.setLineNetAmt(); invoiceLine.setTaxAmt(); }