Search in sources :

Example 6 with MDistributionRunDetail

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);
}
Also used : MDistributionRunDetail(org.compiere.model.MDistributionRunDetail) MDistributionRunLine(org.compiere.model.MDistributionRunLine) BigDecimal(java.math.BigDecimal) AdempiereException(org.adempiere.exceptions.AdempiereException)

Aggregations

MDistributionRunDetail (org.compiere.model.MDistributionRunDetail)6 BigDecimal (java.math.BigDecimal)4 MDistributionRunLine (org.compiere.model.MDistributionRunLine)4 MProduct (org.compiere.model.MProduct)3 Query (org.compiere.model.Query)3 AdempiereException (org.adempiere.exceptions.AdempiereException)2 MBPartner (org.compiere.model.MBPartner)2 MOrg (org.compiere.model.MOrg)2 MOrgInfo (org.compiere.model.MOrgInfo)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 MLocator (org.compiere.model.MLocator)1 MOrder (org.compiere.model.MOrder)1 MOrderLine (org.compiere.model.MOrderLine)1 MWarehouse (org.compiere.model.MWarehouse)1 MDDOrder (org.eevolution.model.MDDOrder)1 MDDOrderLine (org.eevolution.model.MDDOrderLine)1