KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > acct > Doc_InOut


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

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 /**
24  * Post Shipment Documents.
25  * <pre>
26  * Table: M_InOut (319)
27  * Document Types: MMS, MMR
28  * </pre>
29  * @author Jorg Janke
30  * @version $Id: Doc_InOut.java,v 1.10 2003/10/10 01:03:41 jjanke Exp $
31  */

32 public class Doc_InOut extends Doc
33 {
34     /** AD_Table_ID */
35     public static final int AD_TABLE_ID = 319;
36
37     /**
38      * Constructor
39      * @param AD_Client_ID client
40      */

41     public Doc_InOut (int AD_Client_ID)
42     {
43         super (AD_Client_ID);
44     } // DocInOut
45

46     /** Location */
47     private int C_BPartner_Location_ID = 0;
48
49     /**
50      * Get AD_Table_ID
51      * @return 319
52      */

53     public int getAD_Table_ID()
54     {
55         return AD_TABLE_ID;
56     } // get_Table_ID
57

58     /**
59      * Table Name
60      * @return M_InOut
61      */

62     public String JavaDoc getTableName()
63     {
64         return "M_InOut";
65     } // getTableName
66

67     /**
68      * Load Document Details
69      * @param rs result set
70      * @return true if loadDocumentType was set
71      */

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(); // lines require doc type
86
// Contained Objects
87
p_lines = loadLines();
88         log.debug("Lines=" + p_lines.length);
89         return true;
90     } // loadDocumentDetails
91

92     /**
93      * Load Invoice Line
94      * @return DocLine Array
95      */

96     private DocLine[] loadLines()
97     {
98         ArrayList list = new ArrayList();
99         String JavaDoc 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             //
106
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")); // sets Trx and Storage Qty
112
docLine.setM_Locator_ID (rs.getInt("M_Locator_ID"));
113                 //
114
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             //
123
rs.close();
124             pstmt.close();
125         }
126         catch (SQLException e)
127         {
128             log.error ("loadLines", e);
129         }
130
131         // Return Array
132
DocLine[] dl = new DocLine[list.size()];
133         list.toArray(dl);
134         return dl;
135     } // loadLines
136

137     /**
138      * Get Balance
139      * @return Zero (always balanced)
140      */

141     public BigDecimal getBalance()
142     {
143         BigDecimal retValue = Env.ZERO;
144         return retValue;
145     } // getBalance
146

147     /**
148      * Create Facts (the accounting logic) for
149      * MMS, MMR.
150      * <pre>
151      * Shipment
152      * CoGS DR
153      * Inventory CR
154      * Shipment of Project Issue
155      * CoGS DR
156      * Project CR
157      * Receipt
158      * Inventory DR
159      * NotInvoicedReceipt CR
160      * </pre>
161      * @param as accounting schema
162      * @return Fact
163      */

164     public Fact createFact(AcctSchema as)
165     {
166         p_vo.C_Currency_ID = as.getC_Currency_ID();
167         // create Fact Header
168
Fact fact = new Fact(this, as, Fact.POST_Actual);
169
170         // Line pointers
171
FactLine dr = null;
172         FactLine cr = null;
173
174         // Sales
175
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                 // CoGS DR
182
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); // from Loc
187
dr.setLocationFromBPartner(C_BPartner_Location_ID, false); // to Loc
188
// Inventory CR
189
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); // from Loc
194
cr.setLocationFromBPartner(C_BPartner_Location_ID, false); // to Loc
195
}
196             updateProductInfo(as.getC_AcctSchema_ID()); // only for SO!
197
}
198         // Purchasing
199
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                 // Inventory DR
206
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); // from Loc
211
dr.setLocationFromLocator(line.getM_Locator_ID(), false); // to Loc
212
// NotInvoicedReceipt CR
213
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); // from Loc
218
cr.setLocationFromLocator(line.getM_Locator_ID(), false); // to Loc
219
}
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         //
228
return fact;
229     } // createFact
230

231
232     /**
233      * Update Sales Order Costing Product Info.
234      * Purchase side handeled in Invoice Matching.
235      * <br>
236      * decrease average cumulatives
237      *
238      * @param C_AcctSchema_ID accounting schema
239      */

240     private void updateProductInfo (int C_AcctSchema_ID)
241     {
242         log.debug("updateProductInfo - M_InOut_ID=" + p_vo.Record_ID);
243
244         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(
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     } // updateProductInfo
259

260 } // Doc_InOut
261
Popular Tags