KickJava   Java API By Example, From Geeks To Geeks.

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


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 Order Documents.
25  * <pre>
26  * Table: C_Order (259)
27  * Document Types: SOO, POO, POR (Requisition)
28  * </pre>
29  * @author Jorg Janke
30  * @version $Id: Doc_Order.java,v 1.15 2003/03/19 06:47:32 jjanke Exp $
31  */

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

38     protected Doc_Order(int AD_Client_ID)
39     {
40         super(AD_Client_ID);
41     }
42
43     /** Contained Optional Tax Lines */
44     private DocTax[] m_taxes = null;
45
46     /**
47      * Return TableName of Document
48      * @return C_Order
49      */

50     public String JavaDoc getTableName()
51     {
52         return "C_Order";
53     } // getTableName
54

55     /**
56      * Get Table ID
57      * @return 259
58      */

59     public int getAD_Table_ID()
60     {
61         return 259;
62     } // getAD_Table_ID
63

64     /**
65      * Load Specific Document Details
66      * @param rs result set
67      * @return true if loadDocumentType was set
68      */

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             // Amounts
77
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(); // lines require doc type
92
// Contained Objects
93
p_lines = loadLines();
94         m_taxes = loadTaxes();
95     // Log.trace(Log.l5_DData, "Lines=" + p_lines.length + ", Taxes=" + m_taxes.length);
96
return true;
97     } // loadDocumentDetails
98

99
100     /**
101      * Load Invoice Line
102      * @return DocLine Array
103      */

104     private DocLine[] loadLines()
105     {
106         ArrayList list = new ArrayList();
107         String JavaDoc 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             //
114
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             // BigDecimal PriceList = rs.getBigDecimal("PriceList");
123
docLine.setAmount (LineNetAmt);
124                 list.add(docLine);
125             }
126             //
127
rs.close();
128             pstmt.close();
129         }
130         catch (SQLException e)
131         {
132             log.error ("loadLines", e);
133         }
134
135         // Return Array
136
DocLine[] dl = new DocLine[list.size()];
137         list.toArray(dl);
138         return dl;
139     } // loadLines
140

141     /**
142      * Load Invoice Taxes
143      * @return DocTax Array
144      */

145     private DocTax[] loadTaxes()
146     {
147         ArrayList list = new ArrayList();
148         String JavaDoc 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             //
157
while (rs.next())
158             {
159                 int C_Tax_ID = rs.getInt(1);
160                 String JavaDoc name = rs.getString(2);
161                 BigDecimal rate = rs.getBigDecimal(3);
162                 BigDecimal taxBaseAmt = rs.getBigDecimal(4);
163                 BigDecimal amount = rs.getBigDecimal(5);
164                 //
165
DocTax taxLine = new DocTax(C_Tax_ID, name, rate, taxBaseAmt, amount);
166                 list.add(taxLine);
167             }
168             //
169
rs.close();
170             pstmt.close();
171         }
172         catch (SQLException e)
173         {
174             log.error ("loadTaxes", e);
175         }
176
177         // Return Array
178
DocTax[] tl = new DocTax[list.size()];
179         list.toArray(tl);
180         return tl;
181     } // loadTaxes
182

183     /*************************************************************************/
184
185     /**
186      * Get Source Currency Balance - subtracts line and tax amounts from total - no rounding
187      * @return positive amount, if total invoice is bigger than lines
188      */

189     public BigDecimal getBalance()
190     {
191         BigDecimal retValue = new BigDecimal(0.0);
192         StringBuffer JavaDoc sb = new StringBuffer JavaDoc (" [");
193         // Total
194
retValue = retValue.add(getAmount(Doc.AMTTYPE_Gross));
195         sb.append(getAmount(Doc.AMTTYPE_Gross));
196         // - Charge
197
retValue = retValue.subtract(getAmount(Doc.AMTTYPE_Charge));
198         sb.append("-").append(getAmount(Doc.AMTTYPE_Charge));
199         // - Tax
200
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         // - Lines
209
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         //
219
log.debug(toString() + " Balance=" + retValue + sb.toString());
220         return retValue;
221     } // getBalance
222

223     /**
224      * Create Facts (the accounting logic) for
225      * SOO, POO, POR.
226      * <pre>
227      * </pre>
228      * @param as accounting schema
229      * @return Fact
230      */

231     public Fact createFact (AcctSchema as)
232     {
233         // Purchase Order
234
if (p_vo.DocumentType.equals(DocVO.DOCTYPE_POrder))
235             updateProductInfo(as.getC_AcctSchema_ID());
236
237         // create Fact Header
238
Fact fact = new Fact(this, as, Fact.POST_Actual);
239         return fact;
240     } // createFact
241

242     /*************************************************************************/
243
244     /**
245      * Update Product Info.
246      * - Costing (PriceLastPO)
247      * - PO (PriceLastPO)
248      * @param C_AcctSchema_ID accounting schema
249      */

250     private void updateProductInfo (int C_AcctSchema_ID)
251     {
252         log.debug("updateProductInfo - C_Order_ID=" + p_vo.Record_ID);
253
254         /** @todo Last.. would need to compare document/last updated date
255          * would need to maintain LastPriceUpdateDate on _PO and _Costing */

256
257         // update Product PO info
258
// should only be once, but here for every AcctSchema
259
// ignores multiple lines with same product - just uses first
260
StringBuffer JavaDoc sql = new StringBuffer JavaDoc (
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         // update Product Costing
276
// requires existence of currency conversion !!
277
// if there are multiple lines of the same product last price uses first
278
sql = new StringBuffer JavaDoc (
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     } // updateProductInfo
295

296 } // Doc_Order
297
Popular Tags