/// <summary> /// Add up Allocations /// </summary> private void AddAllocations() { // Reset for (int j = 0; j < _runLines.Length; j++) { MDistributionRunLine runLine = _runLines[j]; runLine.ResetCalculations(); } // Add Up for (int i = 0; i < _details.Length; i++) { MDistributionRunDetail detail = _details[i]; for (int j = 0; j < _runLines.Length; j++) { MDistributionRunLine runLine = _runLines[j]; if (runLine.GetM_DistributionRunLine_ID() == detail.GetM_DistributionRunLine_ID()) { // Round detail.Round(runLine.GetUOMPrecision()); // Add runLine.AddActualMin(detail.GetMinQty()); runLine.AddActualQty(detail.GetQty()); runLine.AddActualAllocation(detail.GetActualAllocation()); runLine.SetMaxAllocation(detail.GetActualAllocation(), false); // log.Fine("RunLine=" + runLine.GetLine() + ": BP_ID=" + detail.GetC_BPartner_ID() + ", Min=" + detail.GetMinQty() + ", Qty=" + detail.GetQty() + ", Allocation=" + detail.GetActualAllocation()); continue; } } } // for all detail lines // Info for (int j = 0; j < _runLines.Length; j++) { MDistributionRunLine runLine = _runLines[j]; log.Fine("Run - " + runLine.GetInfo()); } }
/// <summary> /// Adjust Run Line Allocation /// </summary> /// <param name="index">run line index</param> private void AdjustAllocation(int index) { MDistributionRunLine runLine = _runLines[index]; Decimal difference = runLine.GetActualAllocationDiff(); if (difference.CompareTo(Env.ZERO) == 0) { return; } // Adjust when difference is -1->1 or last difference is the same bool adjustBiggest = Math.Abs(difference).CompareTo(Env.ONE) <= 0 || Math.Abs(difference).CompareTo(Math.Abs(runLine.GetLastDifference())) == 0; log.Fine("Line=" + runLine.GetLine() + ", Diff=" + difference + ", Adjust=" + adjustBiggest); // Adjust Biggest Amount if (adjustBiggest) { for (int i = 0; i < _details.Length; i++) { MDistributionRunDetail detail = _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.Save(); 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 Decimal ratioTotal = Env.ZERO; for (int i = 0; i < _details.Length; i++) { MDistributionRunDetail detail = _details[i]; if (runLine.GetM_DistributionRunLine_ID() == detail.GetM_DistributionRunLine_ID()) { if (detail.IsCanAdjust()) { ratioTotal = Decimal.Add(ratioTotal, 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 < _details.Length; i++) { MDistributionRunDetail detail = _details[i]; if (runLine.GetM_DistributionRunLine_ID() == detail.GetM_DistributionRunLine_ID()) { if (detail.IsCanAdjust()) { Decimal diffRatio = Decimal.Round(Decimal.Divide(Decimal.Multiply(detail.GetRatio(), difference), ratioTotal), MidpointRounding.AwayFromZero); log.Fine("Detail=" + detail.ToString() + ", Allocation=" + detail.GetActualAllocation() + ", DiffRatio=" + diffRatio); detail.AdjustQty(diffRatio); detail.Save(); } } } } runLine.SetLastDifference(difference); }