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_Allocation extends Doc 33 { 34 38 protected Doc_Allocation(int AD_Client_ID) 39 { 40 super(AD_Client_ID); 41 } 43 private int m_C_Invoice_ID = 0; 45 private int m_C_Order_ID = 0; 46 private int m_C_Payment_ID = 0; 47 private int m_C_CashLine_ID = 0; 48 49 53 public String getTableName() 54 { 55 return "C_Allocation"; 56 } 58 62 public int getAD_Table_ID() 63 { 64 return 390; 65 } 67 72 protected boolean loadDocumentDetails (ResultSet rs) 73 { 74 p_vo.DocumentType = DocVO.DOCTYPE_Allocation; 75 try 76 { 77 p_vo.DateDoc = rs.getTimestamp("DateTrx"); 78 p_vo.DocumentNo = ""; 79 80 p_vo.Amounts[Doc.AMTTYPE_Allocation] = rs.getBigDecimal("Amount"); 82 if (p_vo.Amounts[Doc.AMTTYPE_Allocation] == null) 83 p_vo.Amounts[Doc.AMTTYPE_Allocation] = Env.ZERO; 84 p_vo.Amounts[Doc.AMTTYPE_Discount] = rs.getBigDecimal("DiscountAmt"); 85 if (p_vo.Amounts[Doc.AMTTYPE_Discount] == null) 86 p_vo.Amounts[Doc.AMTTYPE_Discount] = Env.ZERO; 87 p_vo.Amounts[Doc.AMTTYPE_WriteOff] = rs.getBigDecimal("WriteOffAmt"); 88 if (p_vo.Amounts[Doc.AMTTYPE_WriteOff] == null) 89 p_vo.Amounts[Doc.AMTTYPE_WriteOff] = Env.ZERO; 90 p_vo.Amounts[Doc.AMTTYPE_Invoice] = p_vo.Amounts[Doc.AMTTYPE_Allocation] 92 .add(p_vo.Amounts[Doc.AMTTYPE_Discount]) 93 .add(p_vo.Amounts[Doc.AMTTYPE_WriteOff]); 94 95 m_C_Invoice_ID = rs.getInt("C_Invoice_ID"); 97 m_C_Order_ID = rs.getInt("C_Order_ID"); 98 m_C_Payment_ID = rs.getInt("C_Payment_ID"); 99 m_C_CashLine_ID = rs.getInt("C_CashLine_ID"); 100 setReferenceInfo(); 101 } 102 catch (SQLException e) 103 { 104 log.error("loadDocumentDetails", e); 105 } 106 return false; 107 } 109 114 private void setReferenceInfo() 115 { 116 if (m_C_CashLine_ID != 0) 118 { 119 String sql = "SELECT c.C_CashBook_ID " 120 + "FROM C_Cash c, C_CashLine cl " 121 + "WHERE c.C_Cash_ID=cl.C_Cash_ID AND cl.C_CashLine_ID=?"; 122 try 123 { 124 PreparedStatement pstmt = DB.prepareStatement(sql); 125 pstmt.setInt(1, m_C_CashLine_ID); 126 ResultSet rs = pstmt.executeQuery(); 127 if (rs.next()) 128 p_vo.C_CashBook_ID = rs.getInt(1); 129 rs.close(); 130 pstmt.close(); 131 } 132 catch (SQLException e) 133 { 134 log.error("setReferenceInfo-1", e); 135 } 136 } 138 if (m_C_Payment_ID != 0) 140 { 141 String sql = "SELECT C_BankAccount_ID " 142 + "FROM C_Payment " 143 + "WHERE C_Payment_ID=?"; 144 try 145 { 146 PreparedStatement pstmt = DB.prepareStatement(sql); 147 pstmt.setInt(1, m_C_Payment_ID); 148 ResultSet rs = pstmt.executeQuery(); 149 if (rs.next()) 150 p_vo.C_BankAccount_ID = rs.getInt(1); 151 rs.close(); 152 pstmt.close(); 153 } 154 catch (SQLException e) 155 { 156 log.error("setReferenceInfo-1", e); 157 } 158 } } 161 162 163 167 public BigDecimal getBalance() 168 { 169 BigDecimal retValue = Env.ZERO; 170 return retValue; 171 } 173 206 public Fact createFact (AcctSchema as) 207 { 208 Fact fact = new Fact(this, as, Fact.POST_Actual); 210 211 if (m_C_Payment_ID != 0 && m_C_Invoice_ID == 0 && m_C_Order_ID == 0 213 && m_C_CashLine_ID == 0 && p_vo.C_BPartner_ID == 0 214 && Env.ZERO.equals(p_vo.Amounts[Doc.AMTTYPE_Discount]) 215 && Env.ZERO.equals(p_vo.Amounts[Doc.AMTTYPE_WriteOff])) 216 return fact; 217 if (m_C_Invoice_ID == 0) 219 return fact; 220 221 if (p_vo.C_BPartner_ID == 0) 223 { 224 p_vo.Error = "No Business Partner"; 227 log.error("createFact - " + p_vo.Error); 228 return null; 229 } 230 231 if (m_C_Payment_ID == 0 && m_C_CashLine_ID == 0) 233 { 234 p_vo.Error = "No Payment/Cash"; 235 log.error("createFact - " + p_vo.Error); 236 return null; 237 } 238 Boolean sales = isSales(); 240 if (sales == null) 241 { 242 p_vo.Error = "Cannot determine SO/PO"; 243 log.error("createFact - " + p_vo.Error); 244 return null; 245 } 246 else if (sales.booleanValue()) { 248 if (m_C_Payment_ID != 0) 249 fact.createLine(null, getAccount(Doc.ACCTTYPE_UnallocatedCash, as), 250 p_vo.C_Currency_ID, getAmount(Doc.AMTTYPE_Allocation), null); 251 else 252 fact.createLine(null, getAccount(Doc.ACCTTYPE_CashTransfer, as), 253 p_vo.C_Currency_ID, getAmount(Doc.AMTTYPE_Allocation), null); 254 fact.createLine(null, getAccount(Doc.ACCTTYPE_DiscountExp, as), 255 p_vo.C_Currency_ID, getAmount(Doc.AMTTYPE_Discount), null); 256 fact.createLine(null, getAccount(Doc.ACCTTYPE_WriteOff, as), 257 p_vo.C_Currency_ID, getAmount(Doc.AMTTYPE_WriteOff), null); 258 fact.createLine(null, getAccount(Doc.ACCTTYPE_C_Receivable, as), 259 p_vo.C_Currency_ID, null, getAmount(Doc.AMTTYPE_Invoice)); 260 } 261 else { 263 fact.createLine(null, getAccount(Doc.ACCTTYPE_V_Liability, as), 264 p_vo.C_Currency_ID, getAmount(Doc.AMTTYPE_Invoice), null); 265 fact.createLine(null, getAccount(Doc.ACCTTYPE_DiscountRev, as), 266 p_vo.C_Currency_ID, null, getAmount(Doc.AMTTYPE_Discount)); 267 fact.createLine(null, getAccount(Doc.ACCTTYPE_WriteOff, as), 268 p_vo.C_Currency_ID, null, getAmount(Doc.AMTTYPE_WriteOff)); 269 if (m_C_Payment_ID != 0) 270 fact.createLine(null, getAccount(Doc.ACCTTYPE_PaymentSelect, as), 271 p_vo.C_Currency_ID, null, getAmount(Doc.AMTTYPE_Allocation)); 272 else 273 fact.createLine(null, getAccount(Doc.ACCTTYPE_CashTransfer, as), 274 p_vo.C_Currency_ID, null, getAmount(Doc.AMTTYPE_Allocation)); 275 } 276 if (as.isDiscountCorrectTax()) 278 fact.createTaxCorrection(); 279 fact.createRealizedGainLoss(); 281 return fact; 282 } 284 288 private Boolean isSales() 289 { 290 Boolean retValue = null; 291 String sql = "SELECT IsSOTrx FROM C_Invoice WHERE C_Invoice_ID=?"; 292 try 293 { 294 PreparedStatement pstmt = DB.prepareStatement(sql); 295 pstmt.setInt(1, m_C_Invoice_ID); 296 ResultSet rs = pstmt.executeQuery(); 297 if (rs.next()) 298 retValue = new Boolean ("Y".equals(rs.getString(1))); 299 rs.close(); 300 pstmt.close(); 301 } 302 catch (SQLException e) 303 { 304 log.error("isSales", e); 305 } 306 return retValue; 307 } 309 } | Popular Tags |