1 14 package org.compiere.acct; 15 16 import java.util.*; 17 import java.math.*; 18 import java.sql.*; 19 20 import org.apache.log4j.Logger; 21 import org.compiere.util.Env; 22 import org.compiere.model.*; 23 24 30 public final class Fact 31 { 32 38 public Fact (Doc document, AcctSchema acctSchema, String defaultPostingType) 39 { 40 m_doc = document; 41 m_acctSchema = acctSchema; 42 m_postingType = defaultPostingType; 43 m_docVO = m_doc.p_vo; 44 log.debug (toString()); 46 } 48 49 private Logger log = Logger.getLogger(getClass()); 50 51 52 private Doc m_doc = null; 53 54 private AcctSchema m_acctSchema = null; 55 56 57 private String m_postingType = null; 58 59 60 public static final String POST_Actual = "A"; 61 62 public static final String POST_Budget = "B"; 63 64 public static final String POST_Commitment = "C"; 65 66 67 private DocVO m_docVO = null; 68 69 70 private boolean m_converted = false; 71 72 73 private ArrayList m_lines = new ArrayList(); 74 75 76 79 public void dispose() 80 { 81 for (int i = 0; i < m_lines.size(); i++) 82 ((FactLine)m_lines.get(i)).dispose(); 83 m_lines.clear(); 84 m_lines = null; 85 } 87 98 public FactLine createLine (DocLine docLine, Account account, 99 int C_Currency_ID, BigDecimal debitAmt, BigDecimal creditAmt) 100 { 101 103 if (account == null) 105 return null; 106 FactLine line = new FactLine (m_docVO.AD_Table_ID, m_docVO.Record_ID, 108 docLine == null ? 0 : docLine.getTrxLine_ID()); 109 line.setDocumentInfo(m_docVO, docLine); 111 line.setAccount(m_acctSchema, account); 112 113 if (!line.setAmtSource(C_Currency_ID, debitAmt, creditAmt)) 115 return null; 116 line.convert(m_acctSchema.getC_Currency_ID(), m_docVO.DateAcct, m_acctSchema.getCurrencyRateType()); 118 if (docLine != null && docLine.getAmtAcctDr() != null && docLine.getAmtAcctDr() != null) 120 line.setAmtAcct(docLine.getAmtAcctDr(), docLine.getAmtAcctCr()); 121 line.setJournalInfo(m_docVO.GL_Budget_ID, m_docVO.GL_Category_ID); 123 line.setPostingType(m_postingType); 124 log.debug ("createLine - " + line.toString()); 126 add(line); 127 return line; 128 } 130 134 void add (FactLine line) 135 { 136 m_lines.add(line); 137 } 139 150 public FactLine createLine (DocLine docLine, Account accountDr, Account accountCr, 151 int C_Currency_ID, BigDecimal Amt) 152 { 153 if (Amt.compareTo(Env.ZERO) < 0) 154 return createLine (docLine, accountCr, C_Currency_ID, null, Amt.abs()); 155 else 156 return createLine (docLine, accountDr, C_Currency_ID, Amt, null); 157 } 159 169 public FactLine createLine (DocLine docLine, Account account, 170 int C_Currency_ID, BigDecimal Amt) 171 { 172 if (Amt.compareTo(Env.ZERO) < 0) 173 return createLine (docLine, account, C_Currency_ID, null, Amt.abs()); 174 else 175 return createLine (docLine, account, C_Currency_ID, Amt, null); 176 } 178 183 public boolean isPostingType (String PostingType) 184 { 185 return m_postingType.equals(PostingType); 186 } 188 192 public boolean isConverted() 193 { 194 return m_converted; 195 } 197 201 public AcctSchema getAcctSchema() 202 { 203 return m_acctSchema; 204 } 206 207 208 212 public boolean isSourceBalanced() 213 { 214 if (m_lines.size() == 0) 216 return true; 217 BigDecimal balance = getSourceBalance(); 218 boolean retValue = balance.compareTo(Env.ZERO) == 0; 219 if (retValue) 220 log.debug ("isSourceBalanced - " + toString()); 221 else 222 log.warn ("isSourceBalanced NO - " + toString() + ", Balance=" + balance); 223 return retValue; 224 } 226 230 protected BigDecimal getSourceBalance() 231 { 232 BigDecimal result = Env.ZERO; 233 for (int i = 0; i < m_lines.size(); i++) 234 { 235 FactLine line = (FactLine)m_lines.get(i); 236 result = result.add (line.getSourceBalance()); 237 } 238 return result; 240 } 242 249 public FactLine balanceSource() 250 { 251 if (!m_acctSchema.isSuspenseBalancing() || m_docVO.MultiCurrency) 252 return null; 253 BigDecimal diff = getSourceBalance(); 254 log.debug ("balanceSource = " + diff); 255 256 FactLine line = new FactLine (m_docVO.AD_Table_ID, m_docVO.Record_ID, 0); 258 line.setDocumentInfo(m_docVO, null); 259 line.setJournalInfo(m_docVO.GL_Budget_ID, m_docVO.GL_Category_ID); 260 line.setPostingType(m_postingType); 261 262 if (diff.compareTo(Env.ZERO) < 0) line.setAmtSource(m_docVO.C_Currency_ID, diff.abs(), Env.ZERO); 265 else line.setAmtSource(m_docVO.C_Currency_ID, Env.ZERO, diff); 267 line.convert(m_acctSchema.getC_Currency_ID(), m_docVO.DateAcct, m_acctSchema.getCurrencyRateType()); 269 line.setAccount(m_acctSchema, m_acctSchema.getSuspenseBalancing_Acct()); 270 log.debug ("balanceSource - " + line.toString()); 272 m_lines.add(line); 273 return line; 274 } 276 277 278 282 public boolean isSegmentBalanced() 283 { 284 if (m_lines.size() == 0) 285 return true; 286 287 ArrayList elementList = m_acctSchema.getAcctSchemaElementList(); 288 int size = elementList.size(); 289 for (int i = 0; i < size; i++) 291 { 292 AcctSchemaElement ase = (AcctSchemaElement)elementList.get(i); 293 if (ase.isBalanced() && !isSegmentBalanced (ase.getSegmentType())) 294 return false; 295 } 296 return true; 297 } 299 306 public boolean isSegmentBalanced (String segmentType) 307 { 308 if (segmentType.equals(AcctSchemaElement.SEGMENT_Org)) 309 { 310 HashMap map = new HashMap(); 311 for (int i = 0; i < m_lines.size(); i++) 313 { 314 FactLine line = (FactLine)m_lines.get(i); 315 Integer key = new Integer (line.getAD_Org_ID()); 316 BigDecimal bal = line.getSourceBalance(); 317 BigDecimal oldBal = (BigDecimal)map.get(key); 318 if (oldBal != null) 319 bal = bal.add(oldBal); 320 map.put(key, bal); 321 } 323 Iterator values = map.values().iterator(); 325 while (values.hasNext()) 326 { 327 BigDecimal bal = (BigDecimal)values.next(); 328 if (bal.compareTo(Env.ZERO) != 0) 329 { 330 map.clear(); 331 log.warn ("isSegmentBalanced (" + segmentType + ") NO - " + toString() + ", Balance=" + bal); 332 return false; 333 } 334 } 335 map.clear(); 336 log.debug ("isSegmentBalanced (" + segmentType + ") - " + toString()); 337 return true; 338 } 339 log.debug ("isSegmentBalanced (" + segmentType + ") (not checked) - " + toString()); 340 return true; 341 } 343 350 public void balanceSegments() 351 { 352 log.debug("balanceSegments"); 353 ArrayList elementList = m_acctSchema.getAcctSchemaElementList(); 355 int size = elementList.size(); 356 for (int i = 0; i < size; i++) 358 { 359 AcctSchemaElement ase = (AcctSchemaElement)elementList.get(i); 360 if (ase.isBalanced()) 361 balanceSegment (ase.getSegmentType()); 362 } 363 } 365 369 private void balanceSegment (String segmentType) 370 { 371 if (m_lines.size() == 0) 373 return; 374 375 log.debug ("balanceSegment (" + segmentType + ") - " + toString()); 376 377 if (segmentType.equals(AcctSchemaElement.SEGMENT_Org)) 379 { 380 HashMap map = new HashMap(); 381 for (int i = 0; i < m_lines.size(); i++) 383 { 384 FactLine line = (FactLine)m_lines.get(i); 385 Integer key = new Integer (line.getAD_Org_ID()); 386 BigDecimal bal = line.getSourceBalance(); 387 BigDecimal oldBal = (BigDecimal)map.get(key); 388 if (oldBal != null) 389 bal = bal.add(oldBal); 390 map.put(key, bal); 391 } 392 393 Iterator keys = map.keySet().iterator(); 395 while (keys.hasNext()) 396 { 397 Integer key = (Integer )keys.next(); 398 BigDecimal diff = (BigDecimal)map.get(key); 399 if (diff.compareTo(Env.ZERO) != 0) 401 { 402 FactLine line = new FactLine (m_docVO.AD_Table_ID, m_docVO.Record_ID, 0); 404 line.setDocumentInfo(m_docVO, null); 405 line.setJournalInfo(m_docVO.GL_Budget_ID, m_docVO.GL_Category_ID); 406 line.setPostingType(m_postingType); 407 if (diff.compareTo(Env.ZERO) < 0) 409 { 410 line.setAmtSource(m_docVO.C_Currency_ID, diff.abs(), Env.ZERO); 411 line.setAccount(m_acctSchema, m_acctSchema.getDueFrom_Acct(segmentType)); 412 } 413 else 414 { 415 line.setAmtSource(m_docVO.C_Currency_ID, Env.ZERO, diff.abs()); 416 line.setAccount(m_acctSchema, m_acctSchema.getDueTo_Acct(segmentType)); 417 } 418 line.convert(m_acctSchema.getC_Currency_ID(), m_docVO.DateAcct, m_acctSchema.getCurrencyRateType()); 419 line.setAD_Org_ID(key.intValue()); 420 log.debug ("balanceSegment (" + segmentType + ") - " + toString()); 421 m_lines.add(line); 422 } 423 } 424 map.clear(); 425 } 426 } 428 429 430 434 public boolean isAcctBalanced() 435 { 436 if (m_lines.size() == 0) 438 return true; 439 BigDecimal balance = getAcctBalance(); 440 boolean retValue = balance.compareTo(Env.ZERO) == 0; 441 if (retValue) 442 log.debug("isAcctBalanced - " + toString()); 443 else 444 log.warn("isAcctBalanced NO - " + toString() + ", Balance=" + balance); 445 return retValue; 446 } 448 452 protected BigDecimal getAcctBalance() 453 { 454 BigDecimal result = Env.ZERO; 455 for (int i = 0; i < m_lines.size(); i++) 456 { 457 FactLine line = (FactLine)m_lines.get(i); 458 result = result.add(line.getAcctBalance()); 459 } 460 return result; 462 } 464 474 public FactLine balanceAccounting() 475 { 476 BigDecimal diff = getAcctBalance(); 477 log.debug ("balanceAccounting - " + toString() + ", Balance=" + diff); 478 FactLine line = null; 479 480 if (m_acctSchema.isCurrencyBalancing()) 482 { 483 line = new FactLine (m_docVO.AD_Table_ID, m_docVO.Record_ID, 0); 484 line.setDocumentInfo(m_docVO, null); 485 line.setJournalInfo(m_docVO.GL_Budget_ID, m_docVO.GL_Category_ID); 486 line.setPostingType(m_postingType); 487 488 line.setAmtSource(m_docVO.C_Currency_ID, Env.ZERO, Env.ZERO); 490 line.convert(m_acctSchema.getC_Currency_ID(), m_docVO.DateAcct, m_acctSchema.getCurrencyRateType()); 491 if (diff.compareTo(Env.ZERO) < 0) 492 line.setAmtAcct(diff.abs(), Env.ZERO); 493 else 494 line.setAmtAcct(Env.ZERO, diff.abs()); 495 line.setAccount(m_acctSchema, m_acctSchema.getCurrencyBalancing_Acct()); 496 log.debug ("balanceAccounting - " + line.toString()); 497 m_lines.add(line); 498 } 499 else 500 { 502 BigDecimal BSamount = Env.ZERO; 503 FactLine BSline = null; 504 BigDecimal PLamount = Env.ZERO; 505 FactLine PLline = null; 506 507 for (int i = 0; i < m_lines.size(); i++) 509 { 510 FactLine l = (FactLine)m_lines.get(i); 511 BigDecimal amt = l.getAcctBalance().abs(); 512 if (l.isBalanceSheet() && amt.compareTo(BSamount) > 0) 513 { 514 BSamount = amt; 515 BSline = l; 516 } 517 else if (!l.isBalanceSheet() && amt.compareTo(PLamount) > 0) 518 { 519 PLamount = amt; 520 PLline = l; 521 } 522 } 523 if (BSline != null) 524 line = BSline; 525 else 526 line = PLline; 527 if (line == null) 528 log.error ("balanceAccounting - No Line found"); 529 else 530 { 531 log.debug ("Adjusting Amt=" + diff.toString() + "; Line=" + line.toString()); 532 line.currencyCorrect(diff); 533 log.debug ("balanceAccounting - " + line.toString()); 534 } 535 } 537 this.isAcctBalanced(); 539 540 return line; 541 } 543 544 545 548 public void createTaxCorrection() 549 { 550 log.debug ("createTaxCorrection - (NIY)" + toString()); 551 } 553 556 public void createRealizedGainLoss() 557 { 558 log.debug ("createRealizedGainLoss - (NIY)" + toString()); 559 } 561 565 public String toString() 566 { 567 StringBuffer sb = new StringBuffer ("Fact["); 568 sb.append(m_doc.toString()); 569 sb.append(",").append(m_acctSchema.toString()); 570 sb.append(",PostType=").append(m_postingType); 571 sb.append("]"); 572 return sb.toString(); 573 } 575 579 public FactLine[] getLines() 580 { 581 FactLine[] temp = new FactLine[m_lines.size()]; 582 m_lines.toArray(temp); 583 return temp; 584 } 586 591 public boolean save (Connection con) 592 { 593 for (int i = 0; i < m_lines.size(); i++) 595 { 596 FactLine fl = (FactLine)m_lines.get(i); 597 if (!fl.save(con)) return false; 599 } 600 return true; 601 } 603 } | Popular Tags |