Skip to content

Commit

Permalink
changed AveragePOCostingMethodHandler to consider currentCost if no c…
Browse files Browse the repository at this point in the history
…ost was provided (#6908)

#6900
  • Loading branch information
pvpurcarcosmin committed Jun 25, 2020
1 parent 83ea199 commit 0d6c7e3
Showing 1 changed file with 37 additions and 23 deletions.
@@ -1,27 +1,6 @@
package de.metas.costing.methods;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

import org.adempiere.ad.trx.api.ITrx;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.exceptions.DBException;
import org.adempiere.service.ClientId;
import org.compiere.model.I_C_InvoiceLine;
import org.compiere.model.I_M_InOutLine;
import org.compiere.model.I_M_MatchInv;
import org.compiere.util.DB;
import org.compiere.util.TimeUtil;
import org.springframework.stereotype.Component;

import com.google.common.collect.ImmutableList;

import de.metas.acct.api.AcctSchema;
import de.metas.costing.CostAmount;
import de.metas.costing.CostDetail;
Expand Down Expand Up @@ -50,6 +29,25 @@
import de.metas.uom.UomId;
import de.metas.util.Services;
import lombok.NonNull;
import org.adempiere.ad.trx.api.ITrx;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.exceptions.DBException;
import org.adempiere.service.ClientId;
import org.compiere.model.I_C_InvoiceLine;
import org.compiere.model.I_M_InOutLine;
import org.compiere.model.I_M_MatchInv;
import org.compiere.util.DB;
import org.compiere.util.TimeUtil;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

/*
* #%L
Expand Down Expand Up @@ -145,9 +143,15 @@ private CostDetailCreateResult createCostDetailAndAdjustCurrentCosts(final CostD
final CostDetailCreateResult result;
if (isInboundTrx || request.isReversal())
{
result = utils.createCostDetailRecordWithChangedCosts(request, currentCosts);
//in case the amount was not provided but there is a positive qty incoming
//use the current cost price to calculate the amount.
final CostDetailCreateRequest requestEffective = request.getAmt().isZero()
? request.withAmount(calculateAmountBasedOnExistingPrice(request, currentCosts))
: request;

result = utils.createCostDetailRecordWithChangedCosts(requestEffective, currentCosts);

currentCosts.addWeightedAverage(request.getAmt(), qty, utils.getQuantityUOMConverter());
currentCosts.addWeightedAverage(requestEffective.getAmt(), qty, utils.getQuantityUOMConverter());
}
else
{
Expand Down Expand Up @@ -374,4 +378,14 @@ private CostDetailAdjustment recalculateCostDetailAmount(final CostDetail costDe
.previousAmounts(previousAmounts)
.build();
}

private CostAmount calculateAmountBasedOnExistingPrice(final CostDetailCreateRequest request, final CurrentCost currentCosts)
{
final CostPrice price = currentCosts.getCostPrice();

//make sure we are multiplying the cost price with the qty in the correct UOM, i.e the UOM of the cost price.
final Quantity qtyInCostUOM = utils.getQuantityUOMConverter().convertQuantityTo(request.getQty(), request.getProductId(), currentCosts.getUomId());

return price.multiply(qtyInCostUOM).roundToPrecisionIfNeeded(currentCosts.getPrecision());
}
}

0 comments on commit 0d6c7e3

Please sign in to comment.