1 17 package org.openbravo.erpCommon.businessUtility; 18 19 import java.util.Vector ; 20 import org.apache.log4j.Logger ; 21 import org.openbravo.database.ConnectionProvider; 22 import org.openbravo.base.secureApp.VariablesSecureApp; 23 import org.openbravo.erpCommon.utility.*; 24 import javax.servlet.*; 25 26 public class AccountTree { 27 static Logger log4j = Logger.getLogger(AccountTree.class); 28 private VariablesSecureApp vars; 29 private ConnectionProvider conn; 30 private AccountTreeData[] accounts; 31 private AccountTreeData[] elements; 32 private AccountTreeData[] resultantAccounts; 33 private String [] elementValueParent; 34 35 public AccountTree(VariablesSecureApp _vars, ConnectionProvider _conn, AccountTreeData[] _elements, AccountTreeData[] _accounts, String _elementValueParent) throws ServletException { 36 if (log4j.isDebugEnabled()) log4j.debug("AccountTree []"); 37 vars = _vars; 38 conn = _conn; 39 elements = _elements; 40 accounts = _accounts; 41 elementValueParent = new String [1]; 42 elementValueParent[0] = _elementValueParent; 43 resultantAccounts = updateTreeQuantitiesSign(null, 0, "D"); 44 if (resultantAccounts!=null && resultantAccounts.length>0) { 46 AccountTreeData[] forms = AccountTreeData.selectForms(conn, Utility.getContext(conn, vars, "#User_Org", "AccountTree"), Utility.getContext(conn, vars, "#User_Client", "AccountTree")); 47 resultantAccounts = calculateTree(forms, elementValueParent, new Vector <Object >()); 48 } 49 } 50 51 public AccountTree(VariablesSecureApp _vars, ConnectionProvider _conn, AccountTreeData[] _elements, AccountTreeData[] _accounts, String [] _elementValueParent) throws ServletException { 52 if (log4j.isDebugEnabled()) log4j.debug("AccountTree []"); 53 vars = _vars; 54 conn = _conn; 55 elements = _elements; 56 accounts = _accounts; 57 elementValueParent = _elementValueParent; 58 59 resultantAccounts = updateTreeQuantitiesSign(null, 0, "D"); 60 61 if (resultantAccounts!=null && resultantAccounts.length>0) { 62 AccountTreeData[] forms = AccountTreeData.selectForms(conn, Utility.getContext(conn, vars, "#User_Org", "AccountTree"), Utility.getContext(conn, vars, "#User_Client", "AccountTree")); 63 65 66 67 Vector <Object > vec = new Vector <Object >(); 68 AccountTreeData[] r; 69 70 for (int i=0; i<elementValueParent.length; i++) { 71 r = calculateTree(forms, elementValueParent[i], new Vector <Object >()); 72 for (int j=0; j<r.length; j++) 73 vec.addElement(r[j]); 74 } 75 76 resultantAccounts = new AccountTreeData[vec.size()]; 77 vec.copyInto(resultantAccounts); 78 } 79 80 81 104 } 105 106 public AccountTreeData[] getAccounts() { 107 return resultantAccounts; 108 } 109 110 111 private double applySign(double qty, String sign, boolean isSummary) { 112 double total=0.0; 113 if (isSummary && !sign.equalsIgnoreCase("A")) { 114 if (sign.equalsIgnoreCase("P")) total=((qty>total)?qty:0.0); 115 else if (sign.equalsIgnoreCase("N")) total=((qty<total)?qty:0.0); 116 else total=qty; 117 } else total=qty; 118 119 return total; 120 } 121 122 123 private AccountTreeData setDataQty(AccountTreeData element, String isDebitCredit) { 124 if (element==null || accounts==null || accounts.length==0) return element; 125 for (int i=0;i<accounts.length;i++) { 126 if (accounts[i].id.equals(element.id)) { 127 if (isDebitCredit.equals("C")) { 128 accounts[i].qty = accounts[i].qtycredit; 129 accounts[i].qtyRef = accounts[i].qtycreditRef; 130 } 131 element.qtyOperation = accounts[i].qty; 132 element.qtyOperationRef = accounts[i].qtyRef; 133 double dblQty = Double.valueOf(element.qtyOperation).doubleValue(); 134 double dblQtyRef = Double.valueOf(element.qtyOperationRef).doubleValue(); 135 element.qty = Double.toString(applySign(dblQty, element.showvaluecond, element.issummary.equals("Y"))); 136 element.qtyRef = Double.toString(applySign(dblQtyRef, element.showvaluecond, element.issummary.equals("Y"))); 137 break; 138 } 139 } 140 return element; 141 } 142 143 private AccountTreeData[] updateTreeQuantitiesSign(String indice, int level, String isDebitCredit) { 144 if (elements==null || elements.length==0) return elements; 145 AccountTreeData[] result = null; 146 Vector <Object > vec = new Vector <Object >(); 147 if (indice == null) indice="0"; 149 for (int i=0;i<elements.length;i++) { 150 if (elements[i].parentId.equals(indice)) { 151 if (level==0) isDebitCredit = elements[i].accountsign; 152 else if (isDebitCredit.equals("") || isDebitCredit.equalsIgnoreCase("N")) isDebitCredit = elements[i].accountsign; 153 AccountTreeData[] dataChilds = updateTreeQuantitiesSign(elements[i].nodeId, (level+1), isDebitCredit); 154 elements[i].elementLevel = Integer.toString(level); 155 elements[i] = setDataQty(elements[i], isDebitCredit); 156 vec.addElement(elements[i]); 157 if (dataChilds!=null && dataChilds.length>0) { 158 for (int j=0;j<dataChilds.length;j++) vec.addElement(dataChilds[j]); 159 } 160 } 161 } 162 result = new AccountTreeData[vec.size()]; 163 vec.copyInto(result); 164 return result; 165 } 166 167 private boolean hasForm(String indice, AccountTreeData[] forms) { 168 if (indice == null) { 169 log4j.error("AccountTree.hasForm - Missing index"); 170 return false; 171 } 172 for (int i=0;i<forms.length;i++) { 173 if (forms[i].id.equals(indice)) return true; 174 } 175 return false; 176 } 177 178 private void formsCalculate(Vector <Object > vecAll, AccountTreeData[] forms, String indice, Vector <Object > vecTotal) { 179 if (log4j.isDebugEnabled()) log4j.debug("AccountTree.formsCalculate"); 180 if (resultantAccounts==null || resultantAccounts.length==0) return; 181 if (indice == null) { 182 log4j.error("AccountTree.formsCalculate - Missing index"); 183 return; 184 } 185 if (vecTotal==null) vecTotal = new Vector <Object >(); 186 if (vecTotal.size()==0) { 187 vecTotal.addElement("0"); 188 vecTotal.addElement("0"); 189 } 190 double total = Double.valueOf((String ) vecTotal.elementAt(0)).doubleValue(); 191 double totalRef = Double.valueOf((String ) vecTotal.elementAt(1)).doubleValue(); 192 boolean encontrado=false; 193 for (int i=0;i<forms.length;i++) { 194 if (forms[i].id.equals(indice)) { 195 encontrado=false; 196 for (int j=0;j<vecAll.size();j++) { 197 AccountTreeData actual = (AccountTreeData) vecAll.elementAt(j); 198 if (actual.nodeId.equals(forms[i].nodeId)) { 199 encontrado=true; 200 total += (Double.valueOf(actual.qtyOperation).doubleValue() * Double.valueOf(forms[i].accountsign).doubleValue()); 201 totalRef += (Double.valueOf(actual.qtyOperationRef).doubleValue() * Double.valueOf(forms[i].accountsign).doubleValue()); 202 break; 204 } 205 } 206 if (!encontrado) { 207 Vector <Object > vecParcial = new Vector <Object >(); 209 vecParcial.addElement("0"); 210 vecParcial.addElement("0"); 211 calculateTree(forms, forms[i].nodeId, vecParcial, true); 212 double parcial = Double.valueOf((String ) vecParcial.elementAt(0)).doubleValue(); 213 double parcialRef = Double.valueOf((String ) vecParcial.elementAt(1)).doubleValue(); 214 parcial = (parcial * Double.valueOf(forms[i].accountsign).doubleValue()); 216 parcialRef = (parcialRef * Double.valueOf(forms[i].accountsign).doubleValue()); 217 total += parcial; 219 totalRef += parcialRef; 220 } 221 } 222 } 223 vecTotal.set(0, Double.toString(total)); 224 vecTotal.set(1, Double.toString(totalRef)); 225 } 226 227 private AccountTreeData[] calculateTree(AccountTreeData[] forms, String [] indice, Vector <Object > vecTotal) { 228 return calculateTree(forms, indice, vecTotal, true); 229 } 230 231 private AccountTreeData[] calculateTree(AccountTreeData[] forms, String indice, Vector <Object > vecTotal) { 232 String [] i=new String [1]; 233 i[0]=indice; 234 return calculateTree(forms, indice, vecTotal, true); 235 } 236 private boolean nodeIn(String node, String [] listOfNodes) { 237 for (int i=0;i<listOfNodes.length;i++) 238 if (node.equals(listOfNodes[i])) return true; 239 return false; 240 } 241 242 private AccountTreeData[] calculateTree(AccountTreeData[] forms, String indice, Vector <Object > vecTotal, boolean applysign) { 243 String [] i=new String [1]; 244 i[0]=indice; 245 246 return calculateTree(forms, i, vecTotal, applysign); 247 } 248 private AccountTreeData[] calculateTree(AccountTreeData[] forms, String [] indice, Vector <Object > vecTotal, boolean applysign) { 249 if (resultantAccounts==null || resultantAccounts.length==0) return resultantAccounts; 250 if (indice == null){ indice=new String [1]; indice[0]="0";} 251 AccountTreeData[] result = null; 252 Vector <Object > vec = new Vector <Object >(); 253 if (vecTotal==null) vecTotal = new Vector <Object >(); 255 if (vecTotal.size()==0) { 256 vecTotal.addElement("0"); 257 vecTotal.addElement("0"); 258 } 259 double total = Double.valueOf((String ) vecTotal.elementAt(0)).doubleValue(); 260 double totalRef = Double.valueOf((String ) vecTotal.elementAt(1)).doubleValue(); 261 for (int i=0;i<resultantAccounts.length;i++) { 262 if (resultantAccounts[i].nodeId.equals("1000510")&&resultantAccounts[i].parentId.equals(indice[0])) log4j.debug("CalculateTree Encontrado!!! indice"+indice[0]+" - parentId:"+resultantAccounts[i].parentId); 264 if (nodeIn(resultantAccounts[i].parentId, indice)) { 265 AccountTreeData[] dataChilds = null; 266 if (resultantAccounts[i].calculated.equals("N")) { 268 Vector <Object > vecParcial = new Vector <Object >(); 269 vecParcial.addElement("0"); 270 vecParcial.addElement("0"); 271 Vector <Object > vecAux = (Vector <Object >)vec.clone(); 272 dataChilds = calculateTree(forms, resultantAccounts[i].nodeId, vecParcial); 273 if (dataChilds!=null && dataChilds.length>0) for (int h=0;h<dataChilds.length;h++) vecAux.addElement(dataChilds[h]); 274 if (!hasForm(resultantAccounts[i].nodeId, forms)) { 275 double parcial = Double.valueOf((String ) vecParcial.elementAt(0)).doubleValue(); 276 double parcialRef = Double.valueOf((String ) vecParcial.elementAt(1)).doubleValue(); 277 resultantAccounts[i].qtyOperation = Double.toString(Double.valueOf(resultantAccounts[i].qtyOperation).doubleValue() + parcial); 278 resultantAccounts[i].qtyOperationRef = Double.toString(Double.valueOf(resultantAccounts[i].qtyOperationRef).doubleValue() + parcialRef); 279 } else { 280 vecParcial.set(0, "0"); 281 vecParcial.set(1, "0"); 282 formsCalculate(vecAux, forms, resultantAccounts[i].nodeId, vecParcial); 283 double parcial = Double.valueOf((String ) vecParcial.elementAt(0)).doubleValue(); 284 double parcialRef = Double.valueOf((String ) vecParcial.elementAt(1)).doubleValue(); 285 resultantAccounts[i].qtyOperation = Double.toString(Double.valueOf(resultantAccounts[i].qtyOperation).doubleValue() + parcial); 286 resultantAccounts[i].qtyOperationRef = Double.toString(Double.valueOf(resultantAccounts[i].qtyOperationRef).doubleValue() + parcialRef); 287 } 288 289 resultantAccounts[i].qty = Double.toString(applySign(Double.valueOf(resultantAccounts[i].qtyOperation).doubleValue(), resultantAccounts[i].showvaluecond, resultantAccounts[i].issummary.equals("Y"))); 290 resultantAccounts[i].qtyRef = Double.toString(applySign(Double.valueOf(resultantAccounts[i].qtyOperationRef).doubleValue(), resultantAccounts[i].showvaluecond, resultantAccounts[i].issummary.equals("Y"))); 291 resultantAccounts[i].calculated = "Y"; 292 } 293 vec.addElement(resultantAccounts[i]); 294 if (dataChilds!=null && dataChilds.length>0) { 295 for (int j=0;j<dataChilds.length;j++) vec.addElement(dataChilds[j]); 296 } 297 299 if (applysign) { 300 total += Double.valueOf(resultantAccounts[i].qty).doubleValue(); 301 totalRef += Double.valueOf(resultantAccounts[i].qtyRef).doubleValue(); 302 } else { 303 total += Double.valueOf(resultantAccounts[i].qtyOperation).doubleValue(); 304 totalRef += Double.valueOf(resultantAccounts[i].qtyOperationRef).doubleValue(); 305 } 306 } 307 } 308 vecTotal.set(0, Double.toString(total)); 309 vecTotal.set(1, Double.toString(totalRef)); 310 result = new AccountTreeData[vec.size()]; 311 vec.copyInto(result); 312 return result; 313 } 314 315 private AccountTreeData[] levelFilter(String [] indice, boolean found, String strLevel) { 316 if (resultantAccounts==null || resultantAccounts.length==0 || strLevel==null || strLevel.equals("")) return resultantAccounts; 317 AccountTreeData[] result = null; 318 Vector <Object > vec = new Vector <Object >(); 319 if (log4j.isDebugEnabled()) log4j.debug("AccountTree.levelFilter() - accounts: " + resultantAccounts.length); 320 321 if (indice == null){ indice=new String [1]; indice[0]="0";} 323 for (int i=0;i<resultantAccounts.length;i++) { 324 if (nodeIn(resultantAccounts[i].parentId, indice)&& (!found || resultantAccounts[i].elementlevel.equalsIgnoreCase(strLevel))) { 326 AccountTreeData[] dataChilds = levelFilter(resultantAccounts[i].nodeId, (found || resultantAccounts[i].elementlevel.equals(strLevel)), strLevel); 327 vec.addElement(resultantAccounts[i]); 328 if (dataChilds!=null && dataChilds.length>0) for (int j=0;j<dataChilds.length;j++) vec.addElement(dataChilds[j]); 329 } 330 } 331 result = new AccountTreeData[vec.size()]; 332 vec.copyInto(result); 333 vec.clear(); 334 return result; 335 } 336 337 private AccountTreeData[] levelFilter(String indice, boolean found, String strLevel) { 338 String [] i=new String [1]; 339 i[0]=indice; 340 if (log4j.isDebugEnabled()) log4j.debug("AccountTree.levelFilter1"); 341 return levelFilter(i, found, strLevel); 342 } 343 344 public AccountTreeData[] filterStructure(String [] indice, boolean notEmptyLines, String strLevel, boolean isLevel) { 345 if (log4j.isDebugEnabled()) log4j.debug("AccountTree.filterStructure() - accounts: " + resultantAccounts.length); 346 if (resultantAccounts==null || resultantAccounts.length==0) return resultantAccounts; 347 AccountTreeData[] result = null; 348 Vector <Object > vec = new Vector <Object >(); 349 350 351 double ZERO = 0.0; 352 353 AccountTreeData[] r = levelFilter(indice, false, strLevel); 354 355 for (int i=0;i<r.length;i++) { 356 if (r[i].nodeId.equals("1000510")) log4j.debug("filterStructure qty:" + r[i].qty+" - qtyRef:"+r[i].qtyRef+" - show:"+r[i].showelement); 357 if (r[i].showelement.equals("Y")) { 358 if (r[i].nodeId.equals("1000510")) log4j.debug("Encontrado!!! qty:" + r[i].qty+" - qtyRef:"+r[i].qtyRef+" - show:"+r[i].showelement); 359 r[i].qty = Double.toString(applySign(Double.valueOf(r[i].qty).doubleValue(), r[i].showvaluecond, true)); 360 r[i].qtyRef = Double.toString(applySign(Double.valueOf(r[i].qtyRef).doubleValue(), r[i].showvaluecond, true)); 361 if (!notEmptyLines || (Double.valueOf(r[i].qty).doubleValue()!=ZERO || Double.valueOf(r[i].qtyRef).doubleValue()!=ZERO)) { 362 vec.addElement(r[i]); 363 } 364 } 365 } 366 result = new AccountTreeData[vec.size()]; 367 vec.copyInto(result); 368 return result; 369 } 370 371 390 391 public void filter(boolean notEmptyLines, String strLevel, boolean isLevel) { 392 if (log4j.isDebugEnabled()) log4j.debug("filter"); 393 if (resultantAccounts==null) log4j.warn("No resultant Acct"); 394 resultantAccounts = filterStructure(elementValueParent, notEmptyLines, strLevel, isLevel); 395 } 396 } 397 | Popular Tags |