} // validate /// <summary> /// Distribute Amount to Lines /// </summary> /// <param name="acct">account</param> /// <param name="Amt">amount</param> /// <param name="C_Currency_ID">currency</param> public void Distribute(MAccount acct, Decimal Amt, int C_Currency_ID) { log.Info("Amt=" + Amt + " - " + acct); GetLines(false); int precision = MCurrency.GetStdPrecision(GetCtx(), C_Currency_ID); // First Round Decimal total = Env.ZERO; int indexBiggest = -1; int indexZeroPercent = -1; for (int i = 0; i < _lines.Length; i++) { MDistributionLine dl = _lines[i]; if (!dl.IsActive()) { continue; } dl.SetAccount(acct); // Calculate Amount dl.CalculateAmt(Amt, precision); //total = total.add(dl.GetAmt()); total = Decimal.Add(total, dl.GetAmt()); // log.fine("distribute - Line=" + dl.getLine() + " - " + dl.getPercent() + "% " + dl.getAmt() + " - Total=" + total); // Remainder if (Env.Signum(dl.GetPercentDistribution()) == 0) { indexZeroPercent = i; } if (indexZeroPercent == -1) { if (indexBiggest == -1) { indexBiggest = i; } else if (dl.GetAmt().CompareTo(_lines[indexBiggest].GetAmt()) > 0) { indexBiggest = i; } } } // Adjust Remainder //Decimal difference = Amt.subtract(total); Decimal difference = Decimal.Subtract(Amt, total); if (difference.CompareTo(Env.ZERO) != 0) { if (indexZeroPercent != -1) { // log.fine("distribute - Difference=" + difference + " - 0%Line=" + _lines[indexZeroPercent]); _lines[indexZeroPercent].SetAmt(difference); } else if (indexBiggest != -1) { // log.fine("distribute - Difference=" + difference + " - MaxLine=" + _lines[indexBiggest] + " - " + _lines[indexBiggest].getAmt()); //_lines[indexBiggest].SetAmt (_lines[indexBiggest].GetAmt().add(difference)); _lines[indexBiggest].SetAmt(Decimal.Add(_lines[indexBiggest].GetAmt(), difference)); } else { log.Warning("Remaining Difference=" + difference); } } // if (VLogMgt.IsLevelFinest()) //if (CLogMgt.isLevelFinest()) { for (int i = 0; i < _lines.Length; i++) { if (_lines[i].IsActive()) { log.Fine("Amt=" + _lines[i].GetAmt() + " - " + _lines[i].GetAccount()); } } } } // distribute
/// <summary> /// Get Lines and calculate total /// </summary> /// <param name="reload">reload data</param> /// <returns>array of lines</returns> public MDistributionLine[] GetLines(Boolean reload) { if (_lines != null && !reload) { return(_lines); } Decimal PercentTotal = Env.ZERO; //ArrayList<MDistributionLine> list = new ArrayList<MDistributionLine>(); List <MDistributionLine> list = new List <MDistributionLine>(); String sql = "SELECT * FROM GL_DistributionLine " + "WHERE GL_Distribution_ID=@Param1 ORDER BY Line"; Boolean hasNullRemainder = false; SqlParameter[] Param = new SqlParameter[1]; IDataReader idr = null; DataTable dt = null; //PreparedStatement pstmt = null; try { //pstmt = DataBase.prepareStatement (sql, get_TrxName()); //pstmt.setInt (1, getGL_Distribution_ID()); Param[0] = new SqlParameter("@Param1", GetGL_Distribution_ID()); //ResultSet rs = pstmt.executeQuery (); idr = DataBase.DB.ExecuteReader(sql, Param, Get_TrxName()); dt = new DataTable(); dt.Load(idr); idr.Close(); foreach (DataRow dr in dt.Rows) { MDistributionLine dl = new MDistributionLine(GetCtx(), dr, Get_TrxName()); if (dl.IsActive()) { //PercentTotal = PercentTotal.add(dl.GetPercentDistribution()); PercentTotal = Decimal.Add(PercentTotal, dl.GetPercentDistribution()); //hasNullRemainder = dl.getPercentDistribution().signum() == 0; hasNullRemainder = Env.Signum(dl.GetPercentDistribution()) == 0; } dl.SetParent(this); list.Add(dl); } } catch (Exception e) { if (idr != null) { idr.Close(); } log.Log(Level.SEVERE, "getLines", e); } finally { dt = null; } // Update Ratio when saved and difference if (hasNullRemainder) { PercentTotal = Env.ONEHUNDRED; } if (Get_ID() != 0 && PercentTotal.CompareTo(GetPercentTotal()) != 0) { SetPercentTotal(PercentTotal); Save(); } // return _lines = new MDistributionLine[list.Count]; //list.toArray (_lines); _lines = list.ToArray(); return(_lines); } // getLines