KickJava   Java API By Example, From Geeks To Geeks.

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


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 Inventory Documents.
25  * <pre>
26  * Table: M_Inventory (321)
27  * Document Types: MMI
28  * </pre>
29  * @author Jorg Janke
30  * @version $Id: Doc_Inventory.java,v 1.8 2003/10/10 01:03:42 jjanke Exp $
31  */

32 public class Doc_Inventory extends Doc
33 {
34     /**
35      * Constructor
36      * @param AD_Client_ID client
37      */

38     public Doc_Inventory (int AD_Client_ID)
39     {
40         super (AD_Client_ID);
41     } // Doc_Inventory
42

43     /**
44      * Get AD_Table_ID
45      * @return 321
46      */

47     public int getAD_Table_ID()
48     {
49         return 321;
50     } // get_Table_ID
51

52     /**
53      * Table Name
54      * @return M_Inventory
55      */

56     public String JavaDoc getTableName()
57     {
58         return "M_Inventory";
59     } // getTableName
60

61     /**
62      * Load Document Details
63      * @param rs result
64      * @return true if loadDocumentType was set
65      */

66     protected boolean loadDocumentDetails(ResultSet rs)
67     {
68         p_vo.DocumentType = DocVO.DOCTYPE_MatInventory;
69         p_vo.C_Currency_ID = NO_CURRENCY;
70         try
71         {
72             p_vo.DateDoc = rs.getTimestamp("MovementDate");
73         }
74         catch (SQLException e)
75         {
76             log.error("loadDocumentDetails", e);
77         }
78         loadDocumentType(); // lines require doc type
79
// Contained Objects
80
p_lines = loadLines();
81         log.debug("Lines=" + p_lines.length);
82         return true;
83     } // loadDocumentDetails
84

85     /**
86      * Load Invoice Line
87      * @return DocLine Array
88      */

89     private DocLine[] loadLines()
90     {
91         ArrayList list = new ArrayList();
92         String JavaDoc sql = "SELECT * FROM M_InventoryLine WHERE M_Inventory_ID=? ORDER BY Line";
93         try
94         {
95             PreparedStatement pstmt = DB.prepareStatement(sql);
96             pstmt.setInt(1, p_vo.Record_ID);
97             ResultSet rs = pstmt.executeQuery();
98             //
99
while (rs.next())
100             {
101                 int Line_ID = rs.getInt("M_InventoryLine_ID");
102                 DocLine_Material docLine = new DocLine_Material (p_vo.DocumentType, p_vo.Record_ID, Line_ID);
103                 docLine.loadAttributes(rs, p_vo);
104                 BigDecimal QtyBook = rs.getBigDecimal("QtyBook");
105                 BigDecimal QtyCount = rs.getBigDecimal("QtyCount");
106                 docLine.setQty (QtyCount.subtract(QtyBook));
107                 docLine.setM_Locator_ID (rs.getInt("M_Locator_ID"));
108                 // Set Charge ID only when Inventory Type = Charge
109
if (!"C".equals(rs.getString("InventoryType")))
110                     docLine.setC_Charge_ID (0);
111                 //
112
log.debug(docLine.toString());
113                 list.add (docLine);
114             }
115             //
116
rs.close();
117             pstmt.close();
118         }
119         catch (SQLException e)
120         {
121             log.error ("loadLines", e);
122         }
123
124         // Return Array
125
DocLine[] dl = new DocLine[list.size()];
126         list.toArray(dl);
127         return dl;
128     } // loadLines
129

130     /**
131      * Get Balance
132      * @return Zero (always balanced)
133      */

134     public BigDecimal getBalance()
135     {
136         BigDecimal retValue = Env.ZERO;
137         return retValue;
138     } // getBalance
139

140     /**
141      * Create Facts (the accounting logic) for
142      * MMI.
143      * <pre>
144      * Inventory
145      * Inventory DR CR
146      * InventoryDiff DR CR (or Charge)
147      * </pre>
148      * @param as account schema
149      * @return Fact
150      */

151     public Fact createFact(AcctSchema as)
152     {
153     // Log.trace(Log.l4_Data, "Doc.Inventory.createFact");
154
p_vo.C_Currency_ID = as.getC_Currency_ID();
155         // create Fact Header
156
Fact fact = new Fact(this, as, Fact.POST_Actual);
157
158         // Line pointers
159
FactLine dr = null;
160         FactLine cr = null;
161
162         for (int i = 0; i < p_lines.length; i++)
163         {
164             DocLine_Material line = (DocLine_Material)p_lines[i];
165             BigDecimal costs = line.getProductCosts(as);
166             // Inventory DR CR
167
dr = fact.createLine(line,
168                 line.getAccount(ProductInfo.ACCTTYPE_P_Asset, as),
169                 as.getC_Currency_ID(), costs);
170             // may be zero difference - no line created.
171
if (dr == null)
172                 continue;
173             dr.setM_Locator_ID(line.getM_Locator_ID());
174             
175             // InventoryDiff DR CR
176
// or Charge
177
Account invDiff = line.getChargeAccount(as, costs.negate());
178             if (invDiff == null)
179                 invDiff = getAccount(Doc.ACCTTYPE_InvDifferences, as);
180             cr = fact.createLine(line, invDiff,
181                 as.getC_Currency_ID(), costs.negate());
182             cr.setM_Locator_ID(line.getM_Locator_ID());
183         }
184         return fact;
185     } // createFact
186

187 } // Doc_Inventory
188
Popular Tags