KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Generate Purchase Order from Project.
25  *
26  * @author Jorg Janke
27  * @version $Id: ProjectGenPO.java,v 1.2 2003/10/11 05:20:32 jjanke Exp $
28  */

29 public class ProjectGenPO extends SvrProcess
30 {
31     /** Project Parameter */
32     private int m_C_Project_ID = 0;
33     /** Opt Project Line Parameter */
34     private int m_C_ProjectLine_ID = 0;
35     /** Consolidate Document */
36     private boolean m_ConsolidateDocument = true;
37     /** List of POs for Consolidation */
38     private ArrayList m_pos = new ArrayList();
39
40     /**
41      * Prepare - e.g., get Parameters.
42      */

43     protected void prepare()
44     {
45         ProcessInfoParameter[] para = getParameter();
46         for (int i = 0; i < para.length; i++)
47         {
48             String JavaDoc name = para[i].getParameterName();
49             if (para[i].getParameter() == null)
50                 ;
51             else if (name.equals("C_Project_ID"))
52                 m_C_Project_ID = ((BigDecimal)para[i].getParameter()).intValue();
53             else if (name.equals("C_ProjectLine_ID"))
54                 m_C_ProjectLine_ID = ((BigDecimal)para[i].getParameter()).intValue();
55             else if (name.equals("ConsolidateDocument"))
56                 m_ConsolidateDocument = "Y".equals(para[i].getParameter());
57             else
58                 log.error("prepare - Unknown Parameter: " + name);
59         }
60     } // prepare
61

62     /**
63      * Perrform process.
64      * @return Message (clear text)
65      * @throws Exception if not successful
66      */

67     protected String JavaDoc doIt() throws Exception JavaDoc
68     {
69         log.info("doIt - C_Project_ID=" + m_C_Project_ID + " - C_ProjectLine_ID=" + m_C_ProjectLine_ID + " - Consolidate=" + m_ConsolidateDocument);
70         if (m_C_ProjectLine_ID != 0)
71             createPO (new MProjectLine(getCtx(), m_C_ProjectLine_ID));
72         else
73         {
74             MProject project = new MProject (getCtx(), m_C_Project_ID);
75             MProjectLine[] lines = project.getLines();
76             for (int i = 0; i < lines.length; i++)
77                 createPO (lines[i]);
78         }
79         return "";
80     } // doIt
81

82     /**
83      * Create PO from Planned Amt/Qty
84      * @param projectLine project line
85      */

86     private void createPO (MProjectLine projectLine)
87     {
88         if (projectLine.getM_Product_ID() == 0)
89         {
90             addLog (projectLine.getLine() ,null,null, "Line has no Product");
91             return;
92         }
93         if (projectLine.getC_OrderPO_ID() != 0)
94         {
95             addLog (projectLine.getLine() ,null,null, "Line was ordered previously");
96             return;
97         }
98
99         // PO Record
100
X_M_Product_PO po = getMProduct_PO(projectLine.getM_Product_ID());
101         if (po == null)
102         {
103             addLog (projectLine.getLine() ,null,null, "Product has no PO record");
104             return;
105         }
106
107         // Try to find price
108
BigDecimal poPrice = projectLine.getPlannedPrice();
109         if (poPrice == null || poPrice.compareTo(Env.ZERO) == 0)
110             poPrice = po.getPricePO();
111         if (poPrice == null || poPrice.compareTo(Env.ZERO) == 0)
112             poPrice = po.getPriceLastPO();
113         if (poPrice == null || poPrice.compareTo(Env.ZERO) == 0)
114             poPrice = po.getPriceList();
115
116         // Create to Order
117
MOrder order = null;
118         // try to find PO to C_BPartner
119
for (int i = 0; i < m_pos.size(); i++)
120         {
121             MOrder test = (MOrder)m_pos.get(i);
122             if (test.getC_BPartner_ID() == po.getC_BPartner_ID())
123             {
124                 order = test;
125                 break;
126             }
127         }
128         if (order == null) // create new Order
129
{
130             // Vendor
131
MBPartner bp = new MBPartner (getCtx(), po.getC_BPartner_ID());
132             // New Order
133
order = new MOrder (getCtx (), 0);
134             order.setClientOrg (projectLine.getAD_Client_ID (), projectLine.getAD_Org_ID ());
135             order.setIsSOTrx (false);
136             order.setBPartner (bp);
137             order.setC_DocTypeTarget_ID ();
138             order.save ();
139             // optionally save for consolidation
140
if (m_ConsolidateDocument)
141                 m_pos.add(order);
142         }
143
144         // Create Line
145
MOrderLine orderLine = new MOrderLine (order);
146         orderLine.setM_Product_ID(projectLine.getM_Product_ID());
147         orderLine.setQtyOrdered(projectLine.getPlannedQty());
148         orderLine.setPrice();
149         if (poPrice != null && poPrice.compareTo(Env.ZERO) != 0)
150             orderLine.setPriceActual(poPrice);
151         orderLine.setDescription(projectLine.getDescription());
152         orderLine.setTax();
153         orderLine.save();
154
155         // update ProjectLine
156
projectLine.setC_OrderPO_ID(order.getC_Order_ID());
157         projectLine.save();
158         addLog (projectLine.getLine(), null, projectLine.getPlannedQty(), order.getDocumentNo());
159     } // createPOfromProjectLine
160

161
162     /**
163      * Get current PO of Product
164      * @param M_Product_ID product
165      * @return PO
166      */

167     private X_M_Product_PO getMProduct_PO (int M_Product_ID)
168     {
169         X_M_Product_PO retValue = null;
170         String JavaDoc sql = "SELECT * FROM M_Product_PO WHERE M_Product_ID=? ORDER BY IsCurrentVendor DESC";
171         PreparedStatement pstmt = null;
172         try
173         {
174             pstmt = DB.prepareStatement (sql);
175             pstmt.setInt (1, M_Product_ID);
176             ResultSet rs = pstmt.executeQuery ();
177             if (rs.next ())
178                 retValue = new X_M_Product_PO (getCtx(), rs);
179             rs.close ();
180             pstmt.close ();
181             pstmt = null;
182         }
183         catch (SQLException ex)
184         {
185             log.error ("getMProduct_PO", ex);
186         }
187         try
188         {
189             if (pstmt != null)
190                 pstmt.close ();
191         }
192         catch (SQLException ex1)
193         {
194         }
195         pstmt = null;
196         return retValue;
197     } // getMProduct_PO
198

199 } // ProjectGenPO
200
Popular Tags