KickJava   Java API By Example, From Geeks To Geeks.

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


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 Smart 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.math.*;
17 import java.util.*;
18 import java.sql.*;
19
20 import org.compiere.model.*;
21 import org.compiere.util.*;
22
23 /**
24  * Issue to Project.
25  *
26  * @author Jorg Janke
27  * @version $Id: ProjectIssue.java,v 1.2 2003/10/11 05:20:32 jjanke Exp $
28  */

29 public class ProjectIssue extends SvrProcess
30 {
31     /** Project - Mandatory Parameter */
32     private int m_C_Project_ID = 0;
33     /** Receipt - Option 1 */
34     private int m_M_InOut_ID = 0;
35     /** Expenses - Option 2 */
36     private int m_S_TimeExpense_ID = 0;
37     /** Locator - Option 3,4 */
38     private int m_M_Locator_ID = 0;
39     /** Project Line - Option 3 */
40     private int m_C_ProjectLine_ID = 0;
41     /** Product - Option 4 */
42     private int m_M_Product_ID = 0;
43     /** Attribute - Option 4 */
44     private int m_M_AttributeSetInstance_ID = 0;
45     /** Qty - Option 4 */
46     private BigDecimal m_MovementQty = null;
47     /** Date - Option */
48     private Timestamp m_MovementDate = null;
49     /** Description - Option */
50     private String JavaDoc m_Description = null;
51
52     /** The Project to be received */
53     private MProject m_project = null;
54     /** The Project to be received */
55     private MProjectIssue[] m_projectIssues = null;
56
57
58     /**
59      * Prepare - e.g., get Parameters.
60      */

61     protected void prepare()
62     {
63         ProcessInfoParameter[] para = getParameter();
64         for (int i = 0; i < para.length; i++)
65         {
66             String JavaDoc name = para[i].getParameterName();
67             if (para[i].getParameter() == null)
68                 ;
69             else if (name.equals("C_Project_ID"))
70                 m_C_Project_ID = ((BigDecimal)para[i].getParameter()).intValue();
71             else if (name.equals("M_InOut_ID"))
72                 m_M_InOut_ID = ((BigDecimal)para[i].getParameter()).intValue();
73             else if (name.equals("S_TimeExpense_ID"))
74                 m_S_TimeExpense_ID = ((BigDecimal)para[i].getParameter()).intValue();
75             else if (name.equals("M_Locator_ID"))
76                 m_M_Locator_ID = ((BigDecimal)para[i].getParameter()).intValue();
77             else if (name.equals("C_ProjectLine_ID"))
78                 m_C_ProjectLine_ID = ((BigDecimal)para[i].getParameter()).intValue();
79             else if (name.equals("M_Product_ID"))
80                 m_M_Product_ID = ((BigDecimal)para[i].getParameter()).intValue();
81             else if (name.equals("M_AttributeSetInstance_ID"))
82                 m_M_AttributeSetInstance_ID = ((BigDecimal)para[i].getParameter()).intValue();
83             else if (name.equals("MovementQty"))
84                 m_MovementQty = (BigDecimal)para[i].getParameter();
85             else if (name.equals("MovementDate"))
86                 m_MovementDate = (Timestamp)para[i].getParameter();
87             else if (name.equals("Description"))
88                 m_Description = (String JavaDoc)para[i].getParameter();
89             else
90                 log.error("prepare - Unknown Parameter: " + name);
91           }
92     } // prepare
93

94     /**
95      * Perrform process.
96      * @return Message (clear text)
97      * @throws Exception if not successful
98      */

99     protected String JavaDoc doIt() throws Exception JavaDoc
100     {
101         // Check Parameter
102
m_project = new MProject (getCtx(), m_C_Project_ID);
103         if (!(MProject.PROJECTCATEGORY_WorkOrderJob.equals(m_project.getProjectCategory())
104             || MProject.PROJECTCATEGORY_AssetProject.equals(m_project.getProjectCategory())))
105             throw new IllegalArgumentException JavaDoc("Project not Work Order or Asset =" + m_project.getProjectCategory());
106         log.info("doIt - " + m_project);
107         //
108
if (m_M_InOut_ID != 0)
109             return issueReceipt();
110         if (m_S_TimeExpense_ID != 0)
111             return issueExpense();
112         if (m_M_Locator_ID == 0)
113             throw new IllegalArgumentException JavaDoc("Locator missing");
114         if (m_C_ProjectLine_ID != 0)
115             return issueProjectLine();
116         return issueInventory();
117     } // doIt
118

119     /**
120      * Issue Receipt
121      * @return Message (clear text)
122      */

123     private String JavaDoc issueReceipt()
124     {
125         MInOut inOut = new MInOut (getCtx(), m_M_InOut_ID);
126         if (inOut.isSOTrx() || !inOut.isProcessed()
127             || !(MInOut.DOCSTATUS_Completed.equals(inOut.getDocStatus()) || MInOut.DOCSTATUS_Closed.equals(inOut.getDocStatus())))
128             throw new IllegalArgumentException JavaDoc ("Receipt not valid - " + inOut);
129         log.info("issueReceipt - " + inOut);
130         // Set Project of Receipt
131
if (inOut.getC_Project_ID() == 0)
132         {
133             inOut.setC_Project_ID(m_project.getC_Project_ID());
134             inOut.save();
135         }
136         else if (inOut.getC_Project_ID() == m_project.getC_Project_ID())
137             throw new IllegalArgumentException JavaDoc ("Receipt for other Project");
138
139         MInOutLine[] inOutLines = inOut.getMInOutLines();
140         int counter = 0;
141         for (int i = 0; i < inOutLines.length; i++)
142         {
143             // Need to have a Product
144
if (inOutLines[i].getM_Product_ID() == 0)
145                 continue;
146             // Need to have Quantity
147
if (inOutLines[i].getMovementQty() == null || inOutLines[i].getMovementQty().compareTo(Env.ZERO) == 0)
148                 continue;
149             // not issued yet
150
if (projectIssueHasReceipt(inOutLines[i].getM_InOutLine_ID()))
151                 continue;
152             // Create Issue
153
MProjectIssue pi = new MProjectIssue (m_project);
154             pi.setMandatory (inOutLines[i].getM_Locator_ID(), inOutLines[i].getM_Product_ID(), inOutLines[i].getMovementQty());
155             if (m_MovementDate != null) // default today
156
pi.setMovementDate(m_MovementDate);
157             if (m_Description != null && m_Description.length() > 0)
158                 pi.setDescription(m_Description);
159             else if (inOutLines[i].getDescription() != null)
160                 pi.setDescription(inOutLines[i].getDescription());
161             else if (inOut.getDescription() != null)
162                 pi.setDescription(inOut.getDescription());
163             pi.setM_InOutLine_ID(inOutLines[i].getM_InOutLine_ID());
164             pi.process();
165
166             // Find/Create Project Line
167
MProjectLine pl = null;
168             MProjectLine[] pls = m_project.getLines();
169             for (int ii = 0; ii < pls.length; ii++)
170             {
171                 // The Order we generated is the same as the Order of the receipt
172
if (pls[ii].getC_OrderPO_ID() == inOut.getC_Order_ID()
173                     && pls[ii].getM_Product_ID() == inOutLines[i].getM_Product_ID()
174                     && pls[ii].getC_ProjectIssue_ID() == 0) // not issued
175
{
176                     pl = pls[ii];
177                     break;
178                 }
179             }
180             if (pl == null)
181                 pl = new MProjectLine(m_project);
182             pl.setMProjectIssue(pi); // setIssue
183
pl.save();
184             addLog(pi.getLine(), pi.getMovementDate(), pi.getMovementQty(), null);
185             counter++;
186         } // all InOutLines
187

188         return "@Created@ " + counter;
189     } // issueReceipt
190

191
192     /**
193      * Issue Expense Report
194      * @return Message (clear text)
195      */

196     private String JavaDoc issueExpense()
197     {
198         // Get Expense Report
199
MTimeExpense expense = new MTimeExpense (getCtx(), m_S_TimeExpense_ID);
200         if (!expense.isProcessed())
201           throw new IllegalArgumentException JavaDoc ("Time+Expense not processed - " + expense);
202
203         // for all expense lines
204
MTimeExpenseLine[] expenseLines = expense.getLines();
205         int counter = 0;
206         for (int i = 0; i < expenseLines.length; i++)
207         {
208             // Need to have a Product
209
if (expenseLines[i].getM_Product_ID() == 0)
210                 continue;
211             // Need to have Quantity
212
if (expenseLines[i].getQty() == null || expenseLines[i].getQty().compareTo(Env.ZERO) == 0)
213                 continue;
214             // Need to the same project
215
if (expenseLines[i].getC_Project_ID() != m_project.getC_Project_ID())
216                 continue;
217             // not issued yet
218
if (projectIssueHasExpense(expenseLines[i].getS_TimeExpenseLine_ID()))
219                 continue;
220
221             // Find Location
222
int M_Locator_ID = 0;
223         // MProduct product = new MProduct (getCtx(), expenseLines[i].getM_Product_ID());
224
// if (product.getProductType().equals(MProduct.PRODUCTTYPE_Item))
225
M_Locator_ID = MStorage.getM_Location_ID(expense.getM_Warehouse_ID(), expenseLines[i].getM_Product_ID());
226             if (M_Locator_ID == 0) // Service/Expense - get default (and fallback)
227
M_Locator_ID = expense.getM_Locator_ID();
228
229             // Create Issue
230
MProjectIssue pi = new MProjectIssue (m_project);
231             pi.setMandatory (M_Locator_ID, expenseLines[i].getM_Product_ID(), expenseLines[i].getQty());
232             if (m_MovementDate != null) // default today
233
pi.setMovementDate(m_MovementDate);
234             if (m_Description != null && m_Description.length() > 0)
235                 pi.setDescription(m_Description);
236             else if (expenseLines[i].getDescription() != null)
237                 pi.setDescription(expenseLines[i].getDescription());
238             pi.setS_TimeExpenseLine_ID(expenseLines[i].getS_TimeExpenseLine_ID());
239             pi.process();
240             // Find/Create Project Line
241
MProjectLine pl = new MProjectLine(m_project);
242             pl.setMProjectIssue(pi); // setIssue
243
pl.save();
244             addLog(pi.getLine(), pi.getMovementDate(), pi.getMovementQty(), null);
245             counter++;
246         } // allExpenseLines
247

248         return "@Created@ " + counter;
249     } // issueExpense
250

251
252     /**
253      * Issue Project Line
254      * @return Message (clear text)
255      */

256     private String JavaDoc issueProjectLine()
257     {
258         MProjectLine pl = new MProjectLine(getCtx(), m_C_ProjectLine_ID);
259         if (pl.getM_Product_ID() == 0)
260             throw new IllegalArgumentException JavaDoc("Projet Line has no Product");
261         if (pl.getC_ProjectIssue_ID() != 0)
262             throw new IllegalArgumentException JavaDoc("Projet Line already been issued");
263         if (m_M_Locator_ID == 0)
264             throw new IllegalArgumentException JavaDoc("No Locator");
265         // Set to Qty 1
266
if (pl.getPlannedQty() == null || pl.getPlannedQty().compareTo(Env.ZERO) == 0)
267             pl.setPlannedQty(Env.ONE);
268         //
269
MProjectIssue pi = new MProjectIssue (m_project);
270         pi.setMandatory (m_M_Locator_ID, pl.getM_Product_ID(), pl.getPlannedQty());
271         if (m_MovementDate != null) // default today
272
pi.setMovementDate(m_MovementDate);
273         if (m_Description != null && m_Description.length() > 0)
274             pi.setDescription(m_Description);
275         else if (pl.getDescription() != null)
276             pi.setDescription(pl.getDescription());
277         pi.process();
278
279         // Update Line
280
pl.setMProjectIssue(pi);
281         pl.save();
282         addLog(pi.getLine(), pi.getMovementDate(), pi.getMovementQty(), null);
283         return "@Created@ 1";
284     } // issueProjectLine
285

286
287     /**
288      * Issue from Inventory
289      * @return Message (clear text)
290      */

291     private String JavaDoc issueInventory()
292     {
293         if (m_M_Locator_ID == 0)
294             throw new IllegalArgumentException JavaDoc("No Locator");
295         if (m_M_Product_ID == 0)
296             throw new IllegalArgumentException JavaDoc("No Product");
297         // Set to Qty 1
298
if (m_MovementQty == null || m_MovementQty.compareTo(Env.ZERO) == 0)
299             m_MovementQty = Env.ONE;
300         //
301
MProjectIssue pi = new MProjectIssue (m_project);
302         pi.setMandatory (m_M_Locator_ID, m_M_Product_ID, m_MovementQty);
303         if (m_MovementDate != null) // default today
304
pi.setMovementDate(m_MovementDate);
305         if (m_Description != null && m_Description.length() > 0)
306             pi.setDescription(m_Description);
307         pi.process();
308
309         // Create Project Line
310
MProjectLine pl = new MProjectLine(m_project);
311         pl.setMProjectIssue(pi);
312         pl.save();
313         addLog(pi.getLine(), pi.getMovementDate(), pi.getMovementQty(), null);
314         return "@Created@ 1";
315     } // issueInventory
316

317     /**
318      * Check if Project Issue already has Expense
319      * @param S_TimeExpenseLine_ID line
320      * @return true if exists
321      */

322     private boolean projectIssueHasExpense (int S_TimeExpenseLine_ID)
323     {
324         if (m_projectIssues == null)
325             m_projectIssues = m_project.getIssues();
326         for (int i = 0; i < m_projectIssues.length; i++)
327         {
328             if (m_projectIssues[i].getS_TimeExpenseLine_ID() == S_TimeExpenseLine_ID)
329                 return true;
330         }
331         return false;
332     } // projectIssueHasExpense
333

334     /**
335      * Check if Project Isssye already has Receipt
336      * @param M_InOutLine_ID line
337      * @return true if exists
338      */

339     private boolean projectIssueHasReceipt (int M_InOutLine_ID)
340     {
341         if (m_projectIssues == null)
342             m_projectIssues = m_project.getIssues();
343         for (int i = 0; i < m_projectIssues.length; i++)
344         {
345             if (m_projectIssues[i].getM_InOutLine_ID() == M_InOutLine_ID)
346                 return true;
347         }
348         return false;
349     } // projectIssueHasReceipt
350

351 } // ProjectIssue
352
Popular Tags