1 17 package org.openbravo.erpCommon.ad_forms; 18 19 import org.openbravo.base.secureApp.VariablesSecureApp; 20 import java.math.*; 21 import java.util.*; 22 import javax.servlet.*; 23 import org.apache.log4j.Logger ; 24 import org.openbravo.database.ConnectionProvider; 26 import java.sql.Connection ; 27 28 29 30 public class Fact { 31 static Logger log4jFact = Logger.getLogger(Fact.class); 32 33 34 private AcctServer m_doc = null; 35 36 37 private AcctSchema m_acctSchema = null; 38 39 40 private String m_postingType = null; 41 42 43 public final BigDecimal ZERO = new BigDecimal("0"); 44 45 46 47 public static final String POST_Actual = "A"; 48 49 public static final String POST_Budget = "B"; 50 51 public static final String POST_Commitment = "C"; 52 53 54 private ArrayList<Object > m_lines = new ArrayList<Object >(); 55 56 57 58 64 public Fact (AcctServer document, AcctSchema acctSchema, String defaultPostingType) { 65 m_doc = document; 66 m_acctSchema = acctSchema; 67 m_postingType = defaultPostingType; 68 log4jFact.debug ("Fact[" + m_doc.DocumentNo + "," + "AcctSchema[" + m_acctSchema.m_C_AcctSchema_ID + "-" + m_acctSchema.m_Name 70 + ",PostType=" + m_postingType + "]"); 71 } 73 76 public void dispose(){ 77 for (int i = 0; i < m_lines.size(); i++) 78 ((FactLine)m_lines.get(i)).dispose(); 79 m_lines.clear(); 80 m_lines = null; 81 } 83 94 public FactLine createLine (DocLine docLine, Account account, 95 String C_Currency_ID, String debitAmt, String creditAmt, String Fact_Acct_Group_ID, String SeqNo, String DocBaseType,ConnectionProvider conn){ 96 log4jFact.debug ("createLine - " + account + " - Dr=" + debitAmt + ", Cr=" + creditAmt); 97 log4jFact.debug("Starting createline"); 98 if (account == null){ 100 log4jFact.debug("end of create line"); 101 return null; 102 } 103 log4jFact.debug("createLine - Fact_Acct_Group_ID = " + Fact_Acct_Group_ID); 105 FactLine line = new FactLine (m_doc.AD_Table_ID, m_doc.Record_ID, 106 docLine == null ? "" : docLine.m_TrxLine_ID, Fact_Acct_Group_ID, SeqNo, DocBaseType); 107 log4jFact.debug("createLine - line.m_Fact_Acct_Group_ID = " + line.m_Fact_Acct_Group_ID); 108 log4jFact.debug("Object created"); 109 line.setDocumentInfo(m_doc, docLine); 110 line.setAD_Org_ID(m_doc.AD_Org_ID); 111 log4jFact.debug("document info set"); 113 line.setAccount(m_acctSchema, account); 114 log4jFact.debug("account set"); 115 116 log4jFact.debug("C_Currency_ID: " + C_Currency_ID + " - debitAmt: " + debitAmt + " - creditAmt: " + creditAmt); 117 if (!line.setAmtSource(C_Currency_ID, debitAmt, creditAmt)) 119 return null; 120 log4jFact.debug("C_Currency_ID: " + m_acctSchema.getC_Currency_ID() + " - DateAcct: " + m_doc.DateAcct + " - CurrencyRateType: " + m_acctSchema.getCurrencyRateType()); 121 line.convert(m_acctSchema.getC_Currency_ID(), m_doc.DateAcct, m_acctSchema.getCurrencyRateType(),conn); 123 if (docLine != null && !docLine.m_AmtAcctDr.equals("") && !docLine.m_AmtAcctCr.equals("")) 125 line.setAmtAcct(docLine.m_AmtAcctDr, docLine.m_AmtAcctCr); 126 line.setJournalInfo(m_doc.GL_Budget_ID, m_doc.GL_Category_ID); 128 line.setPostingType(m_postingType); 129 line.setDocumentInfo(m_doc, docLine); 131 log4jFact.debug ("createLine - " + m_doc.DocumentNo); 133 log4jFact.debug("********************* Fact - createLine - DocumentNo - " + m_doc.DocumentNo + " - m_lines.size() - " + m_lines.size()); 134 m_lines.add(line); 135 return line; 136 } 138 139 143 void add (FactLine line){ 144 m_lines.add(line); 145 } 147 158 public FactLine createLine (DocLine docLine, Account accountDr, Account accountCr,String C_Currency_ID, String Amt, String Fact_Acct_Group_ID, String SeqNo, String DocBaseType, ConnectionProvider conn){ 159 BigDecimal m_Amt = ZERO; 160 try { 161 if(!Amt.equals("")) m_Amt = new BigDecimal(Amt); 162 } catch (Exception ex) { 163 ex.printStackTrace(); 164 } 166 if (m_Amt.compareTo(ZERO) < 0) 167 return createLine (docLine, accountCr, C_Currency_ID, "", m_Amt.abs().toString(), Fact_Acct_Group_ID, SeqNo, DocBaseType,conn); 168 else 169 return createLine (docLine, accountDr, C_Currency_ID, m_Amt.toString(), "", Fact_Acct_Group_ID, SeqNo, DocBaseType,conn); 170 } 172 182 public FactLine createLine (DocLine docLine, Account account,String C_Currency_ID, String Amt, String Fact_Acct_Group_ID, String SeqNo, String DocBaseType, ConnectionProvider conn){ 183 BigDecimal m_Amt = ZERO; 184 try { 185 if(!Amt.equals("")) m_Amt = new BigDecimal(Amt); 186 } catch (Exception ex) { 187 ex.printStackTrace(); 188 } 190 if (m_Amt.compareTo(ZERO) < 0) 191 return createLine (docLine, account, C_Currency_ID, "", m_Amt.abs().toString(), Fact_Acct_Group_ID, SeqNo, DocBaseType,conn); 192 else 193 return createLine (docLine, account, C_Currency_ID, Amt.toString(), "", Fact_Acct_Group_ID, SeqNo, DocBaseType,conn); 194 } 196 200 public boolean isSourceBalanced(){ 201 log4jFact.debug ("Starting isSourceBalanced"); 202 if (m_lines == null || m_lines.size() == 0) 204 return true; 205 BigDecimal balance = getSourceBalance(); 206 boolean retValue = balance.compareTo(ZERO) == 0; 207 if (retValue) 208 log4jFact.debug ("isSourceBalanced - "); 209 else 210 log4jFact.warn ("isSourceBalanced NO - Balance=" + balance); 211 return retValue; 212 } 214 218 protected BigDecimal getSourceBalance(){ 219 BigDecimal result = new BigDecimal("0"); 220 for (int i = 0; i < m_lines.size(); i++){ 221 FactLine line = (FactLine)m_lines.get(i); 222 result = result.add (line.getSourceBalance()); 223 } 224 return result; 226 } 228 235 public FactLine balanceSource(ConnectionProvider conn){ 236 if (!m_acctSchema.isSuspenseBalancing() || m_doc.MultiCurrency) 237 return null; 238 if (m_lines.size()==0) { 239 log4jFact.error("balanceSouce failed."); 240 return null; 241 } 242 FactLine fl = (FactLine)m_lines.get(0); 243 BigDecimal diff = getSourceBalance(); 244 log4jFact.debug ("balanceSource = " + diff); 245 FactLine line = new FactLine (m_doc.AD_Table_ID, m_doc.Record_ID, "", fl.m_Fact_Acct_Group_ID, fl.m_SeqNo, fl.m_DocBaseType); line.setDocumentInfo(m_doc, null); 248 line.setJournalInfo(m_doc.GL_Budget_ID, m_doc.GL_Category_ID); 249 line.setPostingType(m_postingType); 250 if (diff.compareTo(ZERO) < 0) line.setAmtSource(m_doc.C_Currency_ID, diff.abs().toString(), ZERO.toString()); 253 else line.setAmtSource(m_doc.C_Currency_ID, ZERO.toString(), diff.toString()); 255 line.convert(m_acctSchema.getC_Currency_ID(), m_doc.DateAcct, m_acctSchema.getCurrencyRateType(),conn); 257 line.setAccount(m_acctSchema, m_acctSchema.getSuspenseBalancing_Acct()); 258 log4jFact.debug ("balanceSource - "); 260 log4jFact.debug("****************** fact - balancesource - m_lines.size() - " + m_lines.size()); 261 m_lines.add(line); 262 return line; 263 } 265 266 267 271 public FactLine[] getLines(){ 272 FactLine[] temp = new FactLine[m_lines.size()]; 273 m_lines.toArray(temp); 274 return temp; 275 } 277 282 public boolean save (Connection con,ConnectionProvider conn,VariablesSecureApp vars)throws ServletException{ 283 log4jFact.debug(" Fact - save() - m_lines.size - " + m_lines.size()); 285 if(m_lines.size()==0) return true; 286 for (int i = 0; i < m_lines.size(); i++){ 287 FactLine fl = (FactLine)m_lines.get(i); 288 if (!fl.save(con, conn, vars)){ log4jFact.warn("Save (fact): aborted. i=" + i); 290 return false; 291 } 292 } 293 return true; 294 } 296 300 public boolean isSegmentBalanced(ConnectionProvider conn){ 301 if (m_lines.size() == 0) 302 return true; 303 304 ArrayList<Object > elementList = m_acctSchema.m_elementList; 305 int size = elementList.size(); 306 for (int i = 0; i < size; i++){ 308 AcctSchemaElement ase = (AcctSchemaElement)elementList.get(i); 309 if (ase.m_balanced.equals("Y") && !isSegmentBalanced (ase.m_segmentType, conn)) 310 return false; 311 } 312 return true; 313 } 315 322 public boolean isSegmentBalanced (String segmentType, ConnectionProvider conn){ 323 if (segmentType.equals(AcctSchemaElement.SEGMENT_Org)){ 324 log4jFact.debug("Starting isSegmentBalanced"); 325 HashMap<Integer , BigDecimal> map = new HashMap<Integer , BigDecimal>(); 326 for (int i = 0; i < m_lines.size(); i++){ 328 FactLine line = (FactLine)m_lines.get(i); 329 Integer key = new Integer (line.getAD_Org_ID(conn)); 330 BigDecimal bal = line.getSourceBalance(); 331 BigDecimal oldBal = map.get(key); 332 if (oldBal != null) 333 bal = bal.add(oldBal); 334 map.put(key, bal); 335 } 337 Iterator values = map.values().iterator(); 339 while (values.hasNext()){ 340 BigDecimal bal = (BigDecimal)values.next(); 341 if (bal.compareTo(ZERO) != 0){ 342 map.clear(); 343 log4jFact.warn ("isSegmentBalanced (" + segmentType + ") NO - " + toString() + ", Balance=" + bal); 344 return false; 345 } 346 } 347 map.clear(); 348 log4jFact.debug ("isSegmentBalanced (" + segmentType + ") - " + toString()); 349 return true; 350 } 351 log4jFact.debug ("isSegmentBalanced (" + segmentType + ") (not checked) - " + toString()); 352 return true; 353 } 355 362 public void balanceSegments(ConnectionProvider conn){ 363 log4jFact.debug("balanceSegments"); 364 ArrayList<Object > elementList = m_acctSchema.m_elementList; 366 int size = elementList.size(); 367 for (int i = 0; i < size; i++){ 369 AcctSchemaElement ase = (AcctSchemaElement)elementList.get(i); 370 if (ase.m_balanced.equals("Y")) balanceSegment (ase.m_segmentType, conn); 371 } 372 } 374 378 private void balanceSegment (String segmentType, ConnectionProvider conn){ 379 if (m_lines.size() == 0) return; 381 log4jFact.debug ("balanceSegment (" + segmentType + ") - "); 382 if (segmentType.equals(AcctSchemaElement.SEGMENT_Org)){ 384 HashMap<Integer , BigDecimal> map = new HashMap<Integer , BigDecimal>(); 385 for (int i = 0; i < m_lines.size(); i++){ 387 FactLine line = (FactLine)m_lines.get(i); 388 Integer key = new Integer (line.getAD_Org_ID(conn)); 389 BigDecimal bal = line.getSourceBalance(); 390 BigDecimal oldBal = map.get(key); 391 if (oldBal != null) 392 bal = bal.add(oldBal); 393 map.put(key, bal); 394 } 395 Iterator keys = map.keySet().iterator(); 397 while (keys.hasNext()){ 398 Integer key = (Integer )keys.next(); 399 BigDecimal diff = map.get(key); 400 if (diff.compareTo(ZERO) != 0){ 402 if (m_lines.size()==0) { 404 log4jFact.error("balanceSegment failed."); 405 return; 406 } 407 FactLine fl = (FactLine)m_lines.get(0); 408 FactLine line = new FactLine (m_doc.AD_Table_ID, m_doc.Record_ID, "", fl.m_Fact_Acct_Group_ID, fl.m_SeqNo, fl.m_DocBaseType); 409 line.setDocumentInfo(m_doc, null); 410 line.setJournalInfo(m_doc.GL_Budget_ID, m_doc.GL_Category_ID); 411 line.setPostingType(m_postingType); 412 if (diff.compareTo(ZERO) < 0){ 414 line.setAmtSource(m_doc.C_Currency_ID, diff.abs().toString(), ZERO.toString()); 415 line.setAccount(m_acctSchema, m_acctSchema.m_DueFrom_Acct); 416 }else { 417 line.setAmtSource(m_doc.C_Currency_ID, ZERO.toString(), diff.abs().toString()); 418 line.setAccount(m_acctSchema, m_acctSchema.m_DueTo_Acct); 419 } 420 line.convert(m_acctSchema.getC_Currency_ID(), m_doc.DateAcct, m_acctSchema.getCurrencyRateType(),conn); 421 line.setAD_Org_ID(key.toString()); 422 log4jFact .debug ("balanceSegment (" + segmentType + ") - "); 423 log4jFact.debug("************* fact - balanceSegment - m_lines.size() - " + m_lines.size() + " - line.ad_org_id - " + line.getAD_Org_ID(conn)); 424 m_lines.add(line); 425 } 426 } 427 map.clear(); 428 } 429 } 431 435 public boolean isAcctBalanced(){ 436 if (m_lines == null || m_lines.size() == 0) 438 return true; 439 BigDecimal balance = getAcctBalance(); 440 boolean retValue = balance.compareTo(ZERO) == 0; 441 if (retValue) 442 log4jFact.debug("isAcctBalanced - "); 443 else 444 log4jFact.warn("isAcctBalanced NO - Balance=" + balance); 445 return retValue; 446 } 448 452 protected BigDecimal getAcctBalance(){ 453 BigDecimal result = ZERO; 454 for (int i = 0; i < m_lines.size(); i++){ 455 FactLine line = (FactLine)m_lines.get(i); 456 BigDecimal balance = new BigDecimal(line.getAcctBalance()); 457 result = result.add(balance); 458 } 459 return result; 460 } 462 472 public FactLine balanceAccounting(ConnectionProvider conn){ 473 BigDecimal diff = getAcctBalance(); 474 log4jFact.debug ("balanceAccounting - Balance=" + diff); 475 FactLine line = null; 476 if (m_acctSchema.isCurrencyBalancing()){ 478 if (m_lines.size()==0) { 479 log4jFact.error("balanceAccounting failed."); 480 return null; 481 } 482 FactLine fl = (FactLine)m_lines.get(0); 483 line = new FactLine (m_doc.AD_Table_ID, m_doc.Record_ID, "", fl.m_Fact_Acct_Group_ID, fl.m_SeqNo, fl.m_DocBaseType); 484 line.setDocumentInfo(m_doc, null); 485 line.setJournalInfo(m_doc.GL_Budget_ID, m_doc.GL_Category_ID); 486 line.setPostingType(m_postingType); 487 488 line.setAmtSource(m_doc.C_Currency_ID, ZERO.toString(), ZERO.toString()); 490 line.convert(m_acctSchema.getC_Currency_ID(), m_doc.DateAcct, m_acctSchema.getCurrencyRateType(),conn); 491 if (diff.compareTo(ZERO) < 0) line.setAmtAcct(diff.abs().toString(), ZERO.toString()); 492 else line.setAmtAcct(ZERO.toString(), diff.abs().toString()); 493 line.setAccount(m_acctSchema, m_acctSchema.getCurrencyBalancing_Acct()); 494 log4jFact.debug ("balanceAccounting - " + line.toString()); 495 log4jFact.debug("************* fact - balanceAccounting - m_lines.size() - " + m_lines.size()); 496 m_lines.add(line); 497 } 498 else{ BigDecimal BSamount = ZERO; 500 FactLine BSline = null; 501 BigDecimal PLamount = ZERO; 502 FactLine PLline = null; 503 504 for (int i = 0; i < m_lines.size(); i++){ 506 FactLine l = (FactLine)m_lines.get(i); 507 BigDecimal amt = new BigDecimal(l.getAcctBalance()); 508 amt = amt.abs(); 509 if (l.isBalanceSheet() && amt.compareTo(BSamount) > 0){ 510 BSamount = amt; 511 BSline = l; 512 }else if (!l.isBalanceSheet() && amt.compareTo(PLamount) > 0){ 513 PLamount = amt; 514 PLline = l; 515 } 516 } 517 if (BSline != null) line = BSline; 518 else line = PLline; 519 520 if (line == null) log4jFact.error ("balanceAccounting - No Line found"); 521 else{ 522 log4jFact.debug ("Adjusting Amt=" + diff.toString() + "; Line=" + line.toString()); 523 line.currencyCorrect(diff); 524 log4jFact.debug ("balanceAccounting - " + line.toString()); 525 } 526 } 528 this.isAcctBalanced(); 530 531 return line; 532 } 534 535 } 536 | Popular Tags |