/// <summary>
        /// Allocate Landed Cost - Enforce Rounding
        /// </summary>
        /// <param name="_expectedDistributionlines">expected allocation lines</param>
        private void AllocateLandedCostRounding(MExpectedCostDistribution[] _expectedDistributionlines)
        {
            if (_expectedDistributionlines == null)
            {
                return;
            }
            MExpectedCostDistribution largestAmtAllocation = null;
            Decimal allocationAmt = Env.ZERO;

            for (int i = 0; i < _expectedDistributionlines.Length; i++)
            {
                MExpectedCostDistribution allocation = _expectedDistributionlines[i];
                if (largestAmtAllocation == null ||
                    allocation.GetAmt().CompareTo(largestAmtAllocation.GetAmt()) > 0)
                {
                    largestAmtAllocation = allocation;
                }
                allocationAmt = Decimal.Add(allocationAmt, allocation.GetAmt());
            }
            Decimal difference = Decimal.Subtract(GetAmt(), allocationAmt);

            if (Env.Signum(difference) != 0)
            {
                largestAmtAllocation.SetAmt(Decimal.Add(largestAmtAllocation.GetAmt(), difference));
                largestAmtAllocation.Save();
                log.Config("Difference=" + difference
                           + ", C_ExpectedCostDistribution_ID=" + largestAmtAllocation.GetC_ExpectedCostDistribution_ID()
                           + ", Amt" + largestAmtAllocation.GetAmt());
            }
        }
        /// <summary>
        /// Distribute Cost
        /// </summary>
        /// <returns>if success then empty string else message</returns>
        public String DistributeLandedCost()
        {
            // default distribution line
            MExpectedCostDistribution[] _expectedDistributionlines = null;

            // Delete expected distribution lines
            String sql = "DELETE FROM C_ExpectedCostDistribution WHERE  C_ExpectedCost_ID = " + GetC_ExpectedCost_ID();
            int    no  = DataBase.DB.ExecuteQuery(sql, null, Get_Trx());

            if (no != 0)
            {
                _log.Info(" C_ExpectedCostDistribution - Deleted #" + no);
            }

            // get order lines having only product
            MOrderLine[] orderLines = GetLinesItemProduct(GetC_Order_ID());

            // create expected cost distribution lines
            if (orderLines != null && orderLines.Length > 0)
            {
                List <MExpectedCostDistribution> list = new List <MExpectedCostDistribution>();
                for (int i = 0; i < orderLines.Length; i++)
                {
                    MExpectedCostDistribution allocation = new MExpectedCostDistribution(GetCtx(), 0, Get_Trx());
                    allocation.SetC_ExpectedCost_ID(GetC_ExpectedCost_ID());
                    allocation.SetC_OrderLine_ID(orderLines[i].GetC_OrderLine_ID());
                    allocation.SetClientOrg(GetAD_Client_ID(), GetAD_Org_ID());
                    allocation.SetAmt(Env.ZERO);
                    allocation.SetBase(Env.ZERO);
                    allocation.SetQty(Env.ZERO);
                    if (!allocation.Save())
                    {
                        pp = VLogger.RetrieveError();
                        if (pp != null && !string.IsNullOrEmpty(pp.GetName()))
                        {
                            return(Msg.GetMsg(GetCtx(), "ExpectedAllocationNotSaved") + ", " + pp.GetName());
                        }
                        else
                        {
                            return(Msg.GetMsg(GetCtx(), "ExpectedAllocationNotSaved"));
                        }
                    }
                    else
                    {
                        list.Add(allocation);
                    }
                }
                if (list.Count > 0)
                {
                    _expectedDistributionlines = new MExpectedCostDistribution[list.Count];
                    _expectedDistributionlines = list.ToArray();
                }
            }

            if (_expectedDistributionlines != null && _expectedDistributionlines.Length == 1)
            {
                Decimal baseValue = orderLines[0].GetBase(GetLandedCostDistribution());
                if (baseValue == 0)
                {
                    StringBuilder msgreturn = new StringBuilder("Total of Base values is 0 - ").Append(GetLandedCostDistribution());
                    return(msgreturn.ToString());
                }
                _expectedDistributionlines[0].SetBase(baseValue);
                _expectedDistributionlines[0].SetQty(orderLines[0].GetQtyOrdered());
                _expectedDistributionlines[0].SetAmt(GetAmt(), orderLines[0].GetPrecision());
                if (!_expectedDistributionlines[0].Save())
                {
                    pp = VLogger.RetrieveError();
                    if (pp != null && !string.IsNullOrEmpty(pp.GetName()))
                    {
                        return(Msg.GetMsg(GetCtx(), "ExpectedAllocationNotSaved") + ", " + pp.GetName());
                    }
                    else
                    {
                        return(Msg.GetMsg(GetCtx(), "ExpectedAllocationNotSaved"));
                    }
                }
            }
            else if (_expectedDistributionlines != null && _expectedDistributionlines.Length > 1)
            {
                // get total
                Decimal total = Env.ZERO;
                for (int i = 0; i < orderLines.Length; i++)
                {
                    total = Decimal.Add(total, orderLines[i].GetBase(GetLandedCostDistribution()));
                }

                if (total == 0)
                {
                    StringBuilder msgreturn = new StringBuilder("Total of Base values is 0 - ").Append(GetLandedCostDistribution());
                    return(msgreturn.ToString());
                }

                //	Create Allocations
                for (int i = 0; i < _expectedDistributionlines.Length; i++)
                {
                    MOrderLine orderLine = new MOrderLine(GetCtx(), _expectedDistributionlines[i].GetC_OrderLine_ID(), Get_Trx());
                    Decimal    baseValue = orderLine.GetBase(GetLandedCostDistribution());
                    _expectedDistributionlines[i].SetBase(baseValue);
                    _expectedDistributionlines[i].SetQty(orderLine.GetQtyOrdered());
                    if (baseValue != 0)
                    {
                        Decimal result = Decimal.Multiply(GetAmt(), baseValue);
                        result = Decimal.Round(Decimal.Divide(result, total), orderLine.GetPrecision(), MidpointRounding.AwayFromZero);
                        _expectedDistributionlines[i].SetAmt(result);
                    }
                    if (!_expectedDistributionlines[i].Save())
                    {
                        pp = VLogger.RetrieveError();
                        if (pp != null && !string.IsNullOrEmpty(pp.GetName()))
                        {
                            return(Msg.GetMsg(GetCtx(), "ExpectedAllocationNotSaved") + ", " + pp.GetName());
                        }
                        else
                        {
                            return(Msg.GetMsg(GetCtx(), "ExpectedAllocationNotSaved"));
                        }
                    }
                }
            }
            AllocateLandedCostRounding(_expectedDistributionlines);
            return("");
        }