1 14 package org.compiere.acct; 15 16 import java.math.*; 17 import java.util.*; 18 import java.sql.*; 19 20 import org.compiere.util.*; 21 import org.compiere.model.*; 22 23 32 public class Doc_Cash extends Doc 33 { 34 38 protected Doc_Cash(int AD_Client_ID) 39 { 40 super(AD_Client_ID); 41 } 42 43 47 public String getTableName() 48 { 49 return "C_Cash"; 50 } 52 56 public int getAD_Table_ID() 57 { 58 return 407; 59 } 61 66 protected boolean loadDocumentDetails (ResultSet rs) 67 { 68 p_vo.DocumentType = DocVO.DOCTYPE_CashJournal; 69 try 70 { 71 p_vo.DateDoc = rs.getTimestamp("StatementDate"); 72 73 p_vo.Amounts[Doc.AMTTYPE_Gross] = rs.getBigDecimal("StatementDifference"); 75 if (p_vo.Amounts[Doc.AMTTYPE_Gross] == null) 76 p_vo.Amounts[Doc.AMTTYPE_Gross] = Env.ZERO; 77 } 78 catch (SQLException e) 79 { 80 log.error("loadDocumentDetails", e); 81 } 82 83 setCashBookInfo(); 85 loadDocumentType(); 87 p_lines = loadLines(); 89 log.debug("Lines=" + p_lines.length); 90 return true; 91 } 93 99 private void setCashBookInfo() 100 { 101 int retValue = 0; 102 String sql = "SELECT cb.C_CashBook_ID, cb.AD_Org_ID, cb.C_Currency_ID " 103 + "FROM C_Cash c, C_CashBook cb " 104 + "WHERE c.C_CashBook_ID=cb.C_CashBook_ID AND c.C_Cash_ID=?"; 105 try 106 { 107 PreparedStatement pstmt = DB.prepareStatement(sql); 108 pstmt.setInt(1, p_vo.Record_ID); 109 ResultSet rs = pstmt.executeQuery(); 110 if (rs.next()) 111 { 112 p_vo.C_CashBook_ID = rs.getInt(1); 113 p_vo.AD_Org_ID = rs.getInt(2); 114 p_vo.C_Currency_ID = rs.getInt(3); 115 } 116 rs.close(); 117 pstmt.close(); 118 } 119 catch (SQLException e) 120 { 121 log.error("setCashBookInfo", e); 122 } 123 } 125 129 private DocLine[] loadLines() 130 { 131 ArrayList list = new ArrayList(); 132 String sql = "SELECT * FROM C_CashLine WHERE C_Cash_ID=? ORDER BY Line"; 133 try 134 { 135 PreparedStatement pstmt = DB.prepareStatement(sql); 136 pstmt.setInt(1, p_vo.Record_ID); 137 ResultSet rs = pstmt.executeQuery(); 138 while (rs.next()) 140 { 141 int Line_ID = rs.getInt("C_CashLine_ID"); 142 DocLine_Cash docLine = new DocLine_Cash (p_vo.DocumentType, p_vo.Record_ID, Line_ID); 143 docLine.loadAttributes(rs, p_vo); 144 docLine.setCashType(rs.getString("CashType")); 145 docLine.setReference(rs.getInt("C_BankAccount_ID"), rs.getInt("C_Invoice_ID")); 146 docLine.setAmount (rs.getBigDecimal("Amount"), 147 rs.getBigDecimal("DiscountAmt"), rs.getBigDecimal("WriteOffAmt")); 148 list.add(docLine); 149 } 150 rs.close(); 152 pstmt.close(); 153 } 154 catch (SQLException e) 155 { 156 log.error ("loadLines", e); 157 } 158 159 DocLine[] dl = new DocLine[list.size()]; 161 list.toArray(dl); 162 return dl; 163 } 165 166 167 171 public BigDecimal getBalance() 172 { 173 BigDecimal retValue = Env.ZERO; 174 StringBuffer sb = new StringBuffer (" ["); 175 retValue = retValue.add(getAmount(Doc.AMTTYPE_Gross)); 177 sb.append(getAmount(Doc.AMTTYPE_Gross)); 178 for (int i = 0; i < p_lines.length; i++) 180 { 181 retValue = retValue.subtract(p_lines[i].getAmount()); 182 sb.append("-").append(p_lines[i].getAmount()); 183 } 184 sb.append("]"); 185 log.debug(toString() + " Balance=" + retValue + sb.toString()); 187 return Env.ZERO; } 191 217 public Fact createFact (AcctSchema as) 218 { 219 if (p_vo.C_CashBook_ID == 0) 221 { 222 p_vo.Error = "C_CashBook_ID not set"; 223 log.error("createFact - " + p_vo.Error); 224 return null; 225 } 226 227 Fact fact = new Fact(this, as, Fact.POST_Actual); 229 230 BigDecimal assetAmt = Env.ZERO; 233 234 for (int i = 0; i < p_lines.length; i++) 236 { 237 DocLine_Cash line = (DocLine_Cash)p_lines[i]; 238 String CashType = line.getCashType(); 239 240 if (CashType.equals(DocLine_Cash.CASHTYPE_EXPENSE)) 241 { fact.createLine(line, getAccount(Doc.ACCTTYPE_CashExpense, as), 245 p_vo.C_Currency_ID, line.getAmount().negate(), null); 246 assetAmt = assetAmt.subtract(line.getAmount().negate()); 249 } 250 else if (CashType.equals(DocLine_Cash.CASHTYPE_RECEIPT)) 251 { assetAmt = assetAmt.add(line.getAmount()); 257 fact.createLine(line, getAccount(Doc.ACCTTYPE_CashReceipt, as), 258 p_vo.C_Currency_ID, null, line.getAmount()); 259 } 260 else if (CashType.equals(DocLine_Cash.CASHTYPE_CHARGE)) 261 { fact.createLine(line, line.getChargeAccount(as, getAmount()), 265 p_vo.C_Currency_ID, line.getAmount().negate(), null); 266 assetAmt = assetAmt.subtract(line.getAmount().negate()); 269 } 270 else if (CashType.equals(DocLine_Cash.CASHTYPE_DIFFERENCE)) 271 { fact.createLine(line, getAccount(Doc.ACCTTYPE_CashDifference, as), 275 p_vo.C_Currency_ID, line.getAmount().negate()); 276 assetAmt = assetAmt.add(line.getAmount()); 279 } 280 else if (CashType.equals(DocLine_Cash.CASHTYPE_INVOICE)) 281 { if (line.getC_Currency_ID() == p_vo.C_Currency_ID) 285 assetAmt = assetAmt.add (line.getAmount()); 286 else 287 fact.createLine(line, 288 getAccount(Doc.ACCTTYPE_CashAsset, as), 289 line.getC_Currency_ID(), line.getAmount()); 290 fact.createLine(line, 291 getAccount(Doc.ACCTTYPE_CashTransfer, as), 292 line.getC_Currency_ID(), line.getAmount().negate()); 293 } 294 else if (CashType.equals(DocLine_Cash.CASHTYPE_TRANSFER)) 295 { int temp = p_vo.C_BankAccount_ID; 299 p_vo.C_BankAccount_ID = line.getC_BankAccount_ID(); 300 fact.createLine(line, 301 getAccount(Doc.ACCTTYPE_BankInTransit, as), 302 line.getC_Currency_ID(), line.getAmount().negate()); 303 p_vo.C_BankAccount_ID = temp; 304 if (line.getC_Currency_ID() == p_vo.C_Currency_ID) 305 assetAmt = assetAmt.add (line.getAmount()); 306 else 307 fact.createLine(line, 308 getAccount(Doc.ACCTTYPE_CashAsset, as), 309 line.getC_Currency_ID(), line.getAmount()); 310 } 311 } 313 fact.createLine(null, getAccount(Doc.ACCTTYPE_CashAsset, as), 315 p_vo.C_Currency_ID, assetAmt); 316 317 return fact; 318 } 320 } | Popular Tags |