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_Order extends Doc 33 { 34 38 protected Doc_Order(int AD_Client_ID) 39 { 40 super(AD_Client_ID); 41 } 42 43 44 private DocTax[] m_taxes = null; 45 46 50 public String getTableName() 51 { 52 return "C_Order"; 53 } 55 59 public int getAD_Table_ID() 60 { 61 return 259; 62 } 64 69 protected boolean loadDocumentDetails (ResultSet rs) 70 { 71 try 72 { 73 p_vo.DateDoc = rs.getTimestamp("DateOrdered"); 74 p_vo.TaxIncluded = rs.getString("IsTaxIncluded").equals("Y"); 75 76 p_vo.Amounts[Doc.AMTTYPE_Gross] = rs.getBigDecimal("GrandTotal"); 78 if (p_vo.Amounts[Doc.AMTTYPE_Gross] == null) 79 p_vo.Amounts[Doc.AMTTYPE_Gross] = Env.ZERO; 80 p_vo.Amounts[Doc.AMTTYPE_Net] = rs.getBigDecimal("TotalLines"); 81 if (p_vo.Amounts[Doc.AMTTYPE_Net] == null) 82 p_vo.Amounts[Doc.AMTTYPE_Net] = Env.ZERO; 83 p_vo.Amounts[Doc.AMTTYPE_Charge] = rs.getBigDecimal("ChargeAmt"); 84 if (p_vo.Amounts[Doc.AMTTYPE_Charge] == null) 85 p_vo.Amounts[Doc.AMTTYPE_Charge] = Env.ZERO; 86 } 87 catch (SQLException e) 88 { 89 log.error("loadDocumentDetails", e); 90 } 91 loadDocumentType(); p_lines = loadLines(); 94 m_taxes = loadTaxes(); 95 return true; 97 } 99 100 104 private DocLine[] loadLines() 105 { 106 ArrayList list = new ArrayList(); 107 String sql = "SELECT * FROM C_OrderLine WHERE C_Order_ID=? ORDER BY Line"; 108 try 109 { 110 PreparedStatement pstmt = DB.prepareStatement(sql); 111 pstmt.setInt(1, p_vo.Record_ID); 112 ResultSet rs = pstmt.executeQuery(); 113 while (rs.next()) 115 { 116 int Line_ID = rs.getInt("C_OrderLine_ID"); 117 DocLine docLine = new DocLine (p_vo.DocumentType, p_vo.Record_ID, Line_ID); 118 docLine.loadAttributes(rs, p_vo); 119 BigDecimal Qty = rs.getBigDecimal("QtyOrdered"); 120 docLine.setQty(Qty); 121 BigDecimal LineNetAmt = rs.getBigDecimal("LineNetAmt"); 122 docLine.setAmount (LineNetAmt); 124 list.add(docLine); 125 } 126 rs.close(); 128 pstmt.close(); 129 } 130 catch (SQLException e) 131 { 132 log.error ("loadLines", e); 133 } 134 135 DocLine[] dl = new DocLine[list.size()]; 137 list.toArray(dl); 138 return dl; 139 } 141 145 private DocTax[] loadTaxes() 146 { 147 ArrayList list = new ArrayList(); 148 String sql = "SELECT it.C_Tax_ID, t.Name, t.Rate, it.TaxBaseAmt, it.TaxAmt " 149 + "FROM C_Tax t, C_OrderTax it " 150 + "WHERE t.C_Tax_ID=it.C_Tax_ID AND it.C_Order_ID=?"; 151 try 152 { 153 PreparedStatement pstmt = DB.prepareStatement(sql); 154 pstmt.setInt(1, p_vo.Record_ID); 155 ResultSet rs = pstmt.executeQuery(); 156 while (rs.next()) 158 { 159 int C_Tax_ID = rs.getInt(1); 160 String name = rs.getString(2); 161 BigDecimal rate = rs.getBigDecimal(3); 162 BigDecimal taxBaseAmt = rs.getBigDecimal(4); 163 BigDecimal amount = rs.getBigDecimal(5); 164 DocTax taxLine = new DocTax(C_Tax_ID, name, rate, taxBaseAmt, amount); 166 list.add(taxLine); 167 } 168 rs.close(); 170 pstmt.close(); 171 } 172 catch (SQLException e) 173 { 174 log.error ("loadTaxes", e); 175 } 176 177 DocTax[] tl = new DocTax[list.size()]; 179 list.toArray(tl); 180 return tl; 181 } 183 184 185 189 public BigDecimal getBalance() 190 { 191 BigDecimal retValue = new BigDecimal(0.0); 192 StringBuffer sb = new StringBuffer (" ["); 193 retValue = retValue.add(getAmount(Doc.AMTTYPE_Gross)); 195 sb.append(getAmount(Doc.AMTTYPE_Gross)); 196 retValue = retValue.subtract(getAmount(Doc.AMTTYPE_Charge)); 198 sb.append("-").append(getAmount(Doc.AMTTYPE_Charge)); 199 if (m_taxes != null) 201 { 202 for (int i = 0; i < m_taxes.length; i++) 203 { 204 retValue = retValue.subtract(m_taxes[i].getAmount()); 205 sb.append("-").append(m_taxes[i].getAmount()); 206 } 207 } 208 if (p_lines != null) 210 { 211 for (int i = 0; i < p_lines.length; i++) 212 { 213 retValue = retValue.subtract(p_lines[i].getAmount()); 214 sb.append("-").append(p_lines[i].getAmount()); 215 } 216 sb.append("]"); 217 } 218 log.debug(toString() + " Balance=" + retValue + sb.toString()); 220 return retValue; 221 } 223 231 public Fact createFact (AcctSchema as) 232 { 233 if (p_vo.DocumentType.equals(DocVO.DOCTYPE_POrder)) 235 updateProductInfo(as.getC_AcctSchema_ID()); 236 237 Fact fact = new Fact(this, as, Fact.POST_Actual); 239 return fact; 240 } 242 243 244 250 private void updateProductInfo (int C_AcctSchema_ID) 251 { 252 log.debug("updateProductInfo - C_Order_ID=" + p_vo.Record_ID); 253 254 256 257 StringBuffer sql = new StringBuffer ( 261 "UPDATE M_Product_PO po " 262 + "SET PriceLastPO = (SELECT C_Currency_Convert(ol.PriceActual,ol.C_Currency_ID,po.C_Currency_ID,o.DateOrdered,null,o.AD_Client_ID,o.AD_Org_ID) " 263 + "FROM C_Order o, C_OrderLine ol " 264 + "WHERE o.C_Order_ID=ol.C_Order_ID" 265 + " AND po.M_Product_ID=ol.M_Product_ID AND po.C_BPartner_ID=o.C_BPartner_ID" 266 + " AND ROWNUM=1 AND o.C_Order_ID=").append(p_vo.Record_ID).append(") ") 267 .append("WHERE EXISTS (SELECT * " 268 + "FROM C_Order o, C_OrderLine ol " 269 + "WHERE o.C_Order_ID=ol.C_Order_ID" 270 + " AND po.M_Product_ID=ol.M_Product_ID AND po.C_BPartner_ID=o.C_BPartner_ID" 271 + " AND o.C_Order_ID=").append(p_vo.Record_ID).append(")"); 272 int no = DB.executeUpdate(sql.toString()); 273 log.debug("M_Product_PO - Updated=" + no); 274 275 sql = new StringBuffer ( 279 "UPDATE M_Product_Costing pc " 280 + "SET PriceLastPO = " 281 + "(SELECT C_Currency_Convert(ol.PriceActual,ol.C_Currency_ID,a.C_Currency_ID,o.DateOrdered,null,o.AD_Client_ID,o.AD_Org_ID) " 282 + "FROM C_Order o, C_OrderLine ol, C_AcctSchema a " 283 + "WHERE o.C_Order_ID=ol.C_Order_ID" 284 + " AND pc.M_Product_ID=ol.M_Product_ID AND pc.C_AcctSchema_ID=a.C_AcctSchema_ID" 285 + " AND ROWNUM=1" 286 + " AND pc.C_AcctSchema_ID=").append(C_AcctSchema_ID).append(" AND o.C_Order_ID=").append(p_vo.Record_ID).append(") ") 287 .append("WHERE EXISTS (SELECT * " 288 + "FROM C_Order o, C_OrderLine ol, C_AcctSchema a " 289 + "WHERE o.C_Order_ID=ol.C_Order_ID" 290 + " AND pc.M_Product_ID=ol.M_Product_ID AND pc.C_AcctSchema_ID=a.C_AcctSchema_ID" 291 + " AND pc.C_AcctSchema_ID=").append(C_AcctSchema_ID).append(" AND o.C_Order_ID=").append(p_vo.Record_ID).append(")"); 292 no = DB.executeUpdate(sql.toString()); 293 log.debug("M_Product_Costing - Updated=" + no); 294 } 296 } | Popular Tags |