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_InOut extends Doc 33 { 34 35 public static final int AD_TABLE_ID = 319; 36 37 41 public Doc_InOut (int AD_Client_ID) 42 { 43 super (AD_Client_ID); 44 } 46 47 private int C_BPartner_Location_ID = 0; 48 49 53 public int getAD_Table_ID() 54 { 55 return AD_TABLE_ID; 56 } 58 62 public String getTableName() 63 { 64 return "M_InOut"; 65 } 67 72 protected boolean loadDocumentDetails(ResultSet rs) 73 { 74 p_vo.C_Currency_ID = NO_CURRENCY; 75 try 76 { 77 p_vo.DateDoc = rs.getTimestamp("MovementDate"); 78 C_BPartner_Location_ID = rs.getInt("C_BPartner_Location_ID"); 79 } 80 catch (SQLException e) 81 { 82 log.error("loadDocumentDetails", e); 83 } 84 85 loadDocumentType(); p_lines = loadLines(); 88 log.debug("Lines=" + p_lines.length); 89 return true; 90 } 92 96 private DocLine[] loadLines() 97 { 98 ArrayList list = new ArrayList(); 99 String sql = "SELECT * FROM M_InOutLine WHERE M_InOut_ID=? ORDER BY Line"; 100 try 101 { 102 PreparedStatement pstmt = DB.prepareStatement(sql); 103 pstmt.setInt(1, p_vo.Record_ID); 104 ResultSet rs = pstmt.executeQuery(); 105 while (rs.next()) 107 { 108 int Line_ID = rs.getInt("M_InOutLine_ID"); 109 DocLine_Material docLine = new DocLine_Material (p_vo.DocumentType, p_vo.Record_ID, Line_ID); 110 docLine.loadAttributes(rs, p_vo); 111 docLine.setQty (rs.getBigDecimal("MovementQty")); docLine.setM_Locator_ID (rs.getInt("M_Locator_ID")); 113 if (docLine.getM_Product_ID() == 0) 115 log.info(docLine.toString() + " - No Product - ignored"); 116 else 117 { 118 log.debug(docLine.toString()); 119 list.add (docLine); 120 } 121 } 122 rs.close(); 124 pstmt.close(); 125 } 126 catch (SQLException e) 127 { 128 log.error ("loadLines", e); 129 } 130 131 DocLine[] dl = new DocLine[list.size()]; 133 list.toArray(dl); 134 return dl; 135 } 137 141 public BigDecimal getBalance() 142 { 143 BigDecimal retValue = Env.ZERO; 144 return retValue; 145 } 147 164 public Fact createFact(AcctSchema as) 165 { 166 p_vo.C_Currency_ID = as.getC_Currency_ID(); 167 Fact fact = new Fact(this, as, Fact.POST_Actual); 169 170 FactLine dr = null; 172 FactLine cr = null; 173 174 if (p_vo.DocumentType.equals(DocVO.DOCTYPE_MatShipment)) 176 { 177 for (int i = 0; i < p_lines.length; i++) 178 { 179 DocLine_Material line = (DocLine_Material)p_lines[i]; 180 BigDecimal costs = line.getProductCosts(as); 181 dr = fact.createLine(line, 183 line.getAccount(ProductInfo.ACCTTYPE_P_Cogs, as), 184 as.getC_Currency_ID(), costs, null); 185 dr.setM_Locator_ID(line.getM_Locator_ID()); 186 dr.setLocationFromLocator(line.getM_Locator_ID(), true); dr.setLocationFromBPartner(C_BPartner_Location_ID, false); cr = fact.createLine(line, 190 line.getAccount(ProductInfo.ACCTTYPE_P_Asset, as), 191 as.getC_Currency_ID(), null, costs); 192 cr.setM_Locator_ID(line.getM_Locator_ID()); 193 cr.setLocationFromLocator(line.getM_Locator_ID(), true); cr.setLocationFromBPartner(C_BPartner_Location_ID, false); } 196 updateProductInfo(as.getC_AcctSchema_ID()); } 198 else if (p_vo.DocumentType.equals(DocVO.DOCTYPE_MatReceipt)) 200 { 201 for (int i = 0; i < p_lines.length; i++) 202 { 203 DocLine_Material line = (DocLine_Material)p_lines[i]; 204 BigDecimal costs = line.getProductCosts(as); 205 dr = fact.createLine(line, 207 line.getAccount(ProductInfo.ACCTTYPE_P_Asset, as), 208 as.getC_Currency_ID(), costs, null); 209 dr.setM_Locator_ID(line.getM_Locator_ID()); 210 dr.setLocationFromBPartner(C_BPartner_Location_ID, true); dr.setLocationFromLocator(line.getM_Locator_ID(), false); cr = fact.createLine(line, 214 getAccount(Doc.ACCTTYPE_NotInvoicedReceipts, as), 215 as.getC_Currency_ID(), null, costs); 216 cr.setM_Locator_ID(line.getM_Locator_ID()); 217 cr.setLocationFromBPartner(C_BPartner_Location_ID, true); cr.setLocationFromLocator(line.getM_Locator_ID(), false); } 220 } 221 else 222 { 223 p_vo.Error = "DocumentType unknown: " + p_vo.DocumentType; 224 log.error("createFact - " + p_vo.Error); 225 return null; 226 } 227 return fact; 229 } 231 232 240 private void updateProductInfo (int C_AcctSchema_ID) 241 { 242 log.debug("updateProductInfo - M_InOut_ID=" + p_vo.Record_ID); 243 244 StringBuffer sql = new StringBuffer ( 245 "UPDATE M_Product_Costing pc " 246 + "SET (CostAverageCumQty, CostAverageCumAmt)=" 247 + "(SELECT CostAverageCumQty - SUM(il.MovementQty)," 248 + " CostAverageCumAmt - SUM(il.MovementQty*CurrentCostPrice) " 249 + "FROM M_InOutLine il " 250 + "WHERE pc.M_Product_ID=il.M_Product_ID" 251 + " AND il.M_InOut_ID=").append(p_vo.Record_ID).append(") ") 252 .append("WHERE EXISTS (SELECT * " 253 + "FROM M_InOutLine il " 254 + "WHERE pc.M_Product_ID=il.M_Product_ID" 255 + " AND il.M_InOut_ID=").append(p_vo.Record_ID).append(")"); 256 int no = DB.executeUpdate(sql.toString()); 257 log.debug("M_Product_Costing - Updated=" + no); 258 } 260 } | Popular Tags |