use of org.compiere.model.MDistributionRunDetail in project adempiere by adempiere.
the class DistributionRun method adjustAllocation.
// adjustAllocation
/**
* Adjust Run Line Allocation
* @param index run line index
* @throws Exception
*/
private void adjustAllocation(int index) throws Exception {
MDistributionRunLine runLine = m_runLines[index];
BigDecimal difference = runLine.getActualAllocationDiff();
if (difference.compareTo(Env.ZERO) == 0)
return;
// Adjust when difference is -1->1 or last difference is the same
boolean adjustBiggest = difference.abs().compareTo(Env.ONE) <= 0 || difference.abs().compareTo(runLine.getLastDifference().abs()) == 0;
log.fine("Line=" + runLine.getLine() + ", Diff=" + difference + ", Adjust=" + adjustBiggest);
// Adjust Biggest Amount
if (adjustBiggest) {
for (int i = 0; i < m_details.length; i++) {
MDistributionRunDetail detail = m_details[i];
if (runLine.getM_DistributionRunLine_ID() == detail.getM_DistributionRunLine_ID()) {
log.fine("Biggest - DetailAllocation=" + detail.getActualAllocation() + ", MaxAllocation=" + runLine.getMaxAllocation() + ", Qty Difference=" + difference);
if (detail.getActualAllocation().compareTo(runLine.getMaxAllocation()) == 0 && detail.isCanAdjust()) {
detail.adjustQty(difference);
detail.saveEx();
return;
}
}
}
// for all detail lines
throw new Exception("Cannot adjust Difference = " + difference + " - You need to change Total Qty or Min Qty");
} else // Distibute
{
// New Total Ratio
BigDecimal ratioTotal = Env.ZERO;
for (int i = 0; i < m_details.length; i++) {
MDistributionRunDetail detail = m_details[i];
if (runLine.getM_DistributionRunLine_ID() == detail.getM_DistributionRunLine_ID()) {
if (detail.isCanAdjust())
ratioTotal = ratioTotal.add(detail.getRatio());
}
}
if (ratioTotal.compareTo(Env.ZERO) == 0)
throw new Exception("Cannot distribute Difference = " + difference + " - You need to change Total Qty or Min Qty");
// Distribute
for (int i = 0; i < m_details.length; i++) {
MDistributionRunDetail detail = m_details[i];
if (runLine.getM_DistributionRunLine_ID() == detail.getM_DistributionRunLine_ID()) {
if (detail.isCanAdjust()) {
BigDecimal diffRatio = detail.getRatio().multiply(difference).divide(ratioTotal, // precision from total
BigDecimal.ROUND_HALF_UP);
log.fine("Detail=" + detail.toString() + ", Allocation=" + detail.getActualAllocation() + ", DiffRatio=" + diffRatio);
detail.adjustQty(diffRatio);
detail.saveEx();
}
}
}
}
runLine.setLastDifference(difference);
}
Aggregations