KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > process > ExpenseSOrder


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-2003 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.process;
15
16 import java.sql.*;
17 import java.math.*;
18 import java.net.*;
19
20 import org.compiere.model.*;
21 import org.compiere.util.DB;
22 import org.compiere.util.Env;
23
24 /**
25  * Create Sales Orders from Expense Reports
26  *
27  * @author Jorg Janke
28  * @version $Id: ExpenseSOrder.java,v 1.12 2003/09/05 04:59:48 jjanke Exp $
29  */

30 public class ExpenseSOrder extends SvrProcess
31 {
32     /**
33      * Constructor
34      */

35     public ExpenseSOrder()
36     {
37         super();
38         log.info("ExpenseSOrder - Start");
39     } // ExpenseSOrder
40

41     private int m_C_BPartner_ID = 0;
42     private Timestamp m_DateFrom = null;
43     private Timestamp m_DateTo = null;
44     private int m_noOrders = 0;
45
46     /**
47      * Prepare - e.g., get Parameters.
48      */

49     protected void prepare()
50     {
51         ProcessInfoParameter[] para = getParameter();
52         for (int i = 0; i < para.length; i++)
53         {
54             String JavaDoc name = para[i].getParameterName();
55             if (para[i].getParameter() == null)
56                 ;
57             else if (name.equals("C_BPartner_ID"))
58                 m_C_BPartner_ID = ((BigDecimal)para[i].getParameter()).intValue();
59             else if (name.equals("DateReport"))
60             {
61                 m_DateFrom = (Timestamp)para[i].getParameter();
62                 m_DateTo = (Timestamp)para[i].getParameter_To();
63             }
64             else
65                 log.error("prepare - Unknown Parameter: " + name);
66         }
67     } // prepare
68

69
70     /**
71      * Perform process.
72      * @return Message to be translated
73      * @throws Exception
74      */

75     protected String JavaDoc doIt() throws java.lang.Exception JavaDoc
76     {
77         StringBuffer JavaDoc sql = new StringBuffer JavaDoc("SELECT e.AD_Client_ID, e.AD_Org_ID," // 1..2
78
+ "el.C_BPartner_ID, e.M_Warehouse_ID," // 3..4
79
+ "el.C_Project_ID, el.C_Activity_ID, el.C_Campaign_ID," // 5..7
80
+ "el.M_Product_ID, el.S_ResourceAssignment_ID, el.C_UOM_ID," // 8..10
81
+ "el.Qty, el.Description, el.Note, el.InvoicePrice," // 11..14
82
+ "COALESCE(bp.M_PriceList_ID, e.M_PriceList_ID), " // 15
83
+ "e.DocumentNo, el.S_TimeExpenseLine_ID " // 16..17
84
+ "FROM S_TimeExpense e"
85             + " INNER JOIN S_TimeExpenseLine el ON (e.S_TimeExpense_ID=el.S_TimeExpense_ID)"
86             + " INNER JOIN C_BPartner bp ON (el.C_BPartner_ID=bp.C_BPartner_ID)"
87             + " LEFT OUTER JOIN S_ResourceAssignment ra ON (el.S_ResourceAssignment_ID=ra.S_ResourceAssignment_ID) "
88             + "WHERE el.C_OrderLine_ID IS NULL AND el.IsInvoiced='Y' AND e.Processed='Y'"
89             + " AND e.AD_Client_ID=?"); // #1
90
if (m_C_BPartner_ID != 0)
91             sql.append(" AND el.C_BPartner_ID=?"); // #2
92
if (m_DateFrom != null)
93             sql.append(" AND e.DateReport >= ?"); // #3
94
if (m_DateTo != null)
95             sql.append(" AND e.DateReport <= ?"); // #4
96
sql.append(" ORDER BY el.C_BPartner_ID, el.C_Project_ID, e.S_TimeExpense_ID, el.Line");
97         //
98
int old_BPartner_ID = -1;
99         int old_Project_ID = -1;
100         MOrder order = null;
101         //
102
PreparedStatement pstmt = null;
103         try
104         {
105             pstmt = DB.prepareStatement(sql.toString());
106             int par = 1;
107             pstmt.setInt(par++, getAD_Client_ID());
108             if (m_C_BPartner_ID != 0)
109                 pstmt.setInt(par++, m_C_BPartner_ID);
110             if (m_DateFrom != null)
111                 pstmt.setTimestamp(par++, m_DateFrom);
112             if (m_DateTo != null)
113                 pstmt.setTimestamp(par++, m_DateTo);
114             ResultSet rs = pstmt.executeQuery();
115             while (rs.next()) // ********* Expense Line Loop
116
{
117                 // e.AD_Client_ID, e.AD_Org_ID, // 1..2
118
int AD_Client_ID = rs.getInt(1);
119                 int AS_org_ID = rs.getInt(2);
120                 // el.C_BPartner_ID, e.M_Warehouse_ID, // 3..4
121
int C_BPartner_ID = rs.getInt(3);
122                 int M_Warehouse_ID = rs.getInt(4);
123                 // el.C_Project_ID, el.C_Activity_ID, el.C_Campaign_ID, // 5..7
124
int C_Project_ID = rs.getInt(5);
125                 int C_Activity_ID = rs.getInt(6);
126                 int C_Campaign_ID = rs.getInt(7);
127                 // el.M_Product_ID, el.S_ResourceAssignment_ID, el.C_UOM_ID, // 8..10
128
int M_Product_ID = rs.getInt(8);
129                 int S_ResourceAssignment_ID = rs.getInt(9);
130                 int C_UOM_ID = rs.getInt(10);
131                 // el.Qty, el.Description, el.Note, el.InvoicePrice, // 11..14
132
BigDecimal Qty = rs.getBigDecimal(11);
133                 String JavaDoc Description = rs.getString(12);
134                 String JavaDoc Note = rs.getString(13);
135                 BigDecimal InvoicePrice = rs.getBigDecimal(14);
136                 // COALESCE(bp.M_PriceList_ID, e.M_PriceList_ID) // 15
137
int M_PriceList_ID = rs.getInt(15);
138                 // e.DocumentNo, el.S_TimeExpenseLine_ID // 16..17
139
String JavaDoc DocumentNo = rs.getString(16);
140                 int S_TimeExpenseLine_ID = rs.getInt(17);
141
142
143                 // New BPartner or Project - New Order
144
if (C_BPartner_ID != old_BPartner_ID || C_Project_ID != old_Project_ID)
145                 {
146                     completeOrder (order);
147                     MBPartner bp = new MBPartner (getCtx(), C_BPartner_ID);
148                     //
149
log.info("doIt - New Order for " + bp + ", Project=" + C_Project_ID);
150                     order = new MOrder (getCtx(), 0);
151                     order.setC_DocTypeTarget_ID(MOrder.DocSubTypeSO_OnCredit);
152                     //
153
order.setBPartner(bp);
154                     order.setM_Warehouse_ID(M_Warehouse_ID);
155                     if (C_Activity_ID != 0)
156                         order.setC_Activity_ID(C_Activity_ID);
157                     if (C_Campaign_ID != 0)
158                         order.setC_Campaign_ID(C_Campaign_ID);
159                     if (C_Project_ID != 0)
160                         order.setC_Project_ID(C_Project_ID);
161                     //
162
order.setM_PriceList_ID (M_PriceList_ID);
163                     //
164
if (!order.save())
165                     {
166                         log.error("doIt - cannot save Order");
167                         order = null;
168                         break;
169                     }
170                     old_BPartner_ID = C_BPartner_ID;
171                     old_Project_ID = C_Project_ID;
172                 }
173                 // OrderLine
174
MOrderLine ol = new MOrderLine (order);
175                 //
176
if (M_Product_ID != 0)
177                     ol.setM_Product_ID(M_Product_ID);
178                 if (S_ResourceAssignment_ID != 0)
179                     ol.setS_ResourceAssignment_ID(S_ResourceAssignment_ID);
180                 ol.setQtyOrdered(Qty);
181                 ol.setDescription(Description);
182                 //
183
if (InvoicePrice != null && InvoicePrice.compareTo(Env.ZERO) != 0)
184                     ol.setPriceActual(InvoicePrice);
185                 else
186                     ol.setPrice();
187                 if (C_UOM_ID != 0 && ol.getC_UOM_ID() == 0)
188                     ol.setC_UOM_ID(C_UOM_ID);
189                 ol.setTax();
190                 if (!ol.save())
191                 {
192                     log.error ("doIt - cannot save OrderLine");
193                     order = null;
194                     break;
195                 }
196                 // Update TimeExpense Line
197
sql = new StringBuffer JavaDoc ("UPDATE S_TimeExpenseLine SET C_OrderLine_ID=")
198                     .append(ol.getID())
199                     .append(" WHERE S_TimeExpenseLine_ID=").append(S_TimeExpenseLine_ID);
200                 int no = DB.executeUpdate(sql.toString());
201                 if (no == 1)
202                     log.debug("doIt Updated - S_TimeExpenseLine_ID=" + S_TimeExpenseLine_ID
203                         + " with C_OrderLine_ID=" + ol.getID());
204                 else
205                     log.error("doIt Not Updated #" + no
206                         + " - S_TimeExpenseLine_ID=" + S_TimeExpenseLine_ID
207                         + " with C_OrderLine_ID=" + ol.getID());
208             } // ********* Expense Line Loop
209
rs.close();
210             pstmt.close();
211             pstmt = null;
212         }
213         catch (Exception JavaDoc e)
214         {
215             log.error("doIt", e);
216         }
217         finally
218         {
219             try
220             {
221                 if (pstmt != null)
222                     pstmt.close ();
223             }
224             catch (Exception JavaDoc e)
225             {}
226             pstmt = null;
227         }
228         completeOrder (order);
229
230         return "@Created@=" + m_noOrders;
231     } // doIt
232

233     /**
234      * Complete Order
235      * @param order Order
236      */

237     private void completeOrder (MOrder order)
238     {
239         if (order == null)
240             return;
241         m_noOrders++;
242         addLog (order.getID(), order.getDateOrdered(), order.getGrandTotal(), order.getDocumentNo());
243     } // completeOrder
244

245 } // ExpenseSOrder
246
Popular Tags