KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > erpCommon > ad_forms > DocOrder


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

17 package org.openbravo.erpCommon.ad_forms;
18
19 import org.openbravo.base.secureApp.VariablesSecureApp;
20 import java.math.*;
21 import java.util.*;
22 import javax.servlet.*;
23 import org.apache.log4j.Logger ;
24 // imports for transactions
25
import org.openbravo.database.ConnectionProvider;
26 import java.sql.Connection JavaDoc;
27 import org.openbravo.data.FieldProvider;
28
29
30
31 public class DocOrder extends AcctServer {
32     static Logger log4jDocOrder = Logger.getLogger(DocOrder.class);
33
34 /** Contained Optional Tax Lines */
35 public DocTax[] m_taxes = null;
36
37
38 /**
39  * Constructor
40  * @param AD_Client_ID client
41  */

42 public DocOrder(String JavaDoc AD_Client_ID){
43     super(AD_Client_ID);
44 }
45
46
47 public void loadObjectFieldProvider(ConnectionProvider conn, String JavaDoc AD_Client_ID, String JavaDoc Id) throws ServletException{
48     setObjectFieldProvider(DocOrderData.selectRegistro(conn, AD_Client_ID, Id));
49 }
50
51 /**
52  * Load Specific Document Details
53  * @param rs result set
54  * @return true if loadDocumentType was set
55  */

56 public boolean loadDocumentDetails (FieldProvider [] data,ConnectionProvider conn){
57     DateDoc = data[0].getField("DateOrdered");
58     TaxIncluded = data[0].getField("IsTaxIncluded");
59
60     // Amounts
61
Amounts[AcctServer.AMTTYPE_Gross] = data[0].getField("GrandTotal");
62     if (Amounts[AcctServer.AMTTYPE_Gross] == null)
63         Amounts[AcctServer.AMTTYPE_Gross] = ZERO.toString();
64     Amounts[AcctServer.AMTTYPE_Net] = data[0].getField("TotalLines");
65     if (Amounts[AcctServer.AMTTYPE_Net] == null)
66         Amounts[AcctServer.AMTTYPE_Net] = ZERO.toString();
67     Amounts[AcctServer.AMTTYPE_Charge] = data[0].getField("ChargeAmt");
68     if (Amounts[AcctServer.AMTTYPE_Charge] == null)
69         Amounts[AcctServer.AMTTYPE_Charge] = ZERO.toString();
70     loadDocumentType(); // lines require doc type
71
// Contained Objects
72
p_lines = loadLines(conn);
73     m_taxes = loadTaxes(conn);
74 // Log.trace(Log.l5_DData, "Lines=" + p_lines.length + ", Taxes=" + m_taxes.length);
75
return true;
76 } // loadDocumentDetails
77

78
79 /**
80  * Load Invoice Line
81  * @return DocLine Array
82  */

83 public DocLine[] loadLines(ConnectionProvider conn){
84     ArrayList<Object JavaDoc> list = new ArrayList<Object JavaDoc>();
85     DocLineOrderData [] data = null;
86     try{
87         data = DocLineOrderData.select(conn, Record_ID);
88     }
89     catch (ServletException e){
90         log4jDocOrder.warn(e);
91     }
92     //
93
for(int i=0;i<data.length;i++){
94         String JavaDoc Line_ID = data[i].getField("cOrderlineId");
95         DocLine docLine = new DocLine (DocumentType, Record_ID, Line_ID);
96         docLine.loadAttributes(data[i], this);
97         String JavaDoc Qty = data[i].getField("qtyordered");
98         docLine.setQty(Qty);
99         String JavaDoc LineNetAmt = data[i].getField("linenetamt");
100     // BigDecimal PriceList = rs.getBigDecimal("PriceList");
101
docLine.setAmount (LineNetAmt);
102         list.add(docLine);
103     }
104     //
105

106     // Return Array
107
DocLine[] dl = new DocLine[list.size()];
108     list.toArray(dl);
109     return dl;
110 } // loadLines
111

112 /**
113  * Load Invoice Taxes
114  * @return DocTax Array
115  */

116 public DocTax[] loadTaxes(ConnectionProvider conn){
117     ArrayList<Object JavaDoc> list = new ArrayList<Object JavaDoc>();
118
119     DocOrderData [] data = null;
120     try{
121         data = DocOrderData.select(conn, Record_ID);
122     }
123     catch (ServletException e){
124         log4jDocOrder.warn(e);
125     }
126
127     //
128
for(int i=0; i<data.length; i++){
129         String JavaDoc C_Tax_ID = data[i].getField("cTaxId");
130         String JavaDoc name = data[i].getField("name");
131         String JavaDoc rate = data[i].getField("rate");
132         String JavaDoc taxBaseAmt = data[i].getField("taxbaseamt");
133         String JavaDoc amount = data[i].getField("taxamt");
134         //
135
DocTax taxLine = new DocTax(C_Tax_ID, name, rate, taxBaseAmt, amount);
136         list.add(taxLine);
137     }
138
139     // Return Array
140
DocTax[] tl = new DocTax[list.size()];
141     list.toArray(tl);
142     return tl;
143 } // loadTaxes
144

145 /**
146  * Get Source Currency Balance - subtracts line and tax amounts from total - no rounding
147  * @return positive amount, if total invoice is bigger than lines
148  */

149 public BigDecimal getBalance(){
150     BigDecimal retValue = new BigDecimal("0");
151     StringBuffer JavaDoc sb = new StringBuffer JavaDoc (" [");
152     // Total
153
retValue = retValue.add(new BigDecimal(getAmount(AcctServer.AMTTYPE_Gross)));
154     sb.append(getAmount(AcctServer.AMTTYPE_Gross));
155     // - Charge
156
retValue = retValue.subtract(new BigDecimal(getAmount(AcctServer.AMTTYPE_Charge)));
157     sb.append("-").append(getAmount(AcctServer.AMTTYPE_Charge));
158     // - Tax
159
if (m_taxes != null){
160         for (int i = 0; i < m_taxes.length; i++){
161             retValue = retValue.subtract(new BigDecimal(m_taxes[i].getAmount()));
162             sb.append("-").append(m_taxes[i].getAmount());
163         }
164     }
165     // - Lines
166
if (p_lines != null){
167         for (int i = 0; i < p_lines.length; i++){
168             retValue = retValue.subtract(new BigDecimal(p_lines[i].getAmount()));
169             sb.append("-").append(p_lines[i].getAmount());
170         }
171         sb.append("]");
172     }
173     //
174
log4jDocOrder.debug(" Balance=" + retValue + sb.toString());
175     return retValue;
176 } // getBalance
177

178 /**
179  * Create Facts (the accounting logic) for
180  * SOO, POO, POR.
181  * <pre>
182  * </pre>
183  * @param as accounting schema
184  * @return Fact
185  */

186 public Fact createFact (AcctSchema as,ConnectionProvider conn,Connection JavaDoc con,VariablesSecureApp vars) throws ServletException{
187     // Purchase Order
188
if (DocumentType.equals(AcctServer.DOCTYPE_POrder))
189         updateProductInfo(as.getC_AcctSchema_ID(), conn, con);
190
191     // create Fact Header
192
Fact fact = new Fact(this, as, Fact.POST_Actual);
193     return fact;
194 } // createFact
195

196     /**
197      * Update Product Info.
198      * - Costing (PriceLastPO)
199      * - PO (PriceLastPO)
200      * @param C_AcctSchema_ID accounting schema
201      */

202     private void updateProductInfo (String JavaDoc C_AcctSchema_ID,ConnectionProvider conn,Connection JavaDoc con){
203         log4jDocOrder.debug("updateProductInfo - C_Order_ID=" + Record_ID);
204
205         /** @todo Last.. would need to compare document/last updated date
206          * would need to maintain LastPriceUpdateDate on _PO and _Costing */

207
208
209         try{
210             // update Product PO info
211
// should only be once, but here for every AcctSchema
212
// ignores multiple lines with same product - just uses first
213
int no = DocOrderData.updateProductPO(con, conn, Record_ID);
214             log4jDocOrder.debug("M_Product_PO - Updated=" + no);
215
216             // update Product Costing
217
// requires existence of currency conversion !!
218
// if there are multiple lines of the same product last price uses first
219
no = DocOrderData.updateProductCosting(con, conn, C_AcctSchema_ID, Record_ID);
220             log4jDocOrder.debug("M_Product_Costing - Updated=" + no);
221         }
222         catch (ServletException e){
223             log4jDocOrder.warn(e);
224         }
225
226     } // updateProductInfo
227

228   /**
229    * Get Document Confirmation
230    * @not used
231    */

232   public boolean getDocumentConfirmation(ConnectionProvider conn, String JavaDoc strRecordId) {
233     return true;
234   }
235
236     public String JavaDoc getServletInfo() {
237     return "Servlet for the accounting";
238   } // end of getServletInfo() method
239
}
240
Popular Tags