KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > model > MProductPriceVO


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.model;
15
16 import java.sql.*;
17 import java.math.*;
18
19 import org.compiere.util.*;
20
21 /**
22  * Product Price VO
23  *
24  * @author Jorg Janke
25  * @version $Id: MProductPriceVO.java,v 1.2 2003/11/06 07:08:06 jjanke Exp $
26  */

27 public class MProductPriceVO
28 {
29     /**
30      * Constructor
31      * @param m_product_ID product
32      * @param m_PriceList_Version_ID price list version
33      * @param priceList list price
34      * @param priceStd standard price
35      * @param priceLimit limit price
36      */

37     public MProductPriceVO (int m_product_ID, int m_PriceList_Version_ID,
38         BigDecimal priceList, BigDecimal priceStd, BigDecimal priceLimit)
39     {
40         M_Product_ID = m_product_ID;
41         M_PriceList_Version_ID = m_PriceList_Version_ID;
42         PriceList = priceList;
43         if (PriceList == null)
44             PriceList = Env.ZERO;
45         PriceStd = priceStd;
46         if (PriceStd == null)
47             PriceStd = Env.ZERO;
48         PriceLimit = priceLimit;
49         if (PriceLimit == null)
50             PriceLimit = Env.ZERO;
51     } // MProductPriceVO
52

53     /** Product */
54     public int M_Product_ID = 0;
55     /** Price List Version */
56     public int M_PriceList_Version_ID = 0;
57     
58     /** List Price */
59     public BigDecimal PriceList;
60     /** Standard Price */
61     public BigDecimal PriceStd;
62     /** Limit Price */
63     public BigDecimal PriceLimit;
64
65     /*************************************************************************/
66     
67     /** Valid From (PLV) */
68     private Timestamp m_ValidFrom = null;
69     
70     /** Currency (PL) */
71     private int m_C_Currency_ID = 0;
72     /** PriceLimit (PL) */
73     private Boolean JavaDoc m_enforcePriceLimit = null;
74     /** Price List */
75     private int m_M_PriceList_ID = 0;
76     
77     /** Log */
78     private static Logger s_log = Logger.getCLogger(MProductPriceVO.class);
79     
80     
81     /**
82      * Get Price List Version - Valid From
83      * @return Valid From
84      */

85     public Timestamp getValidFrom()
86     {
87         if (m_ValidFrom == null)
88             loadPLV();
89         return m_ValidFrom;
90     } // getValidFrom
91

92     /**
93      * Load Price List Version Info
94      */

95     private void loadPLV()
96     {
97         X_M_PriceList_Version plv = new X_M_PriceList_Version (Env.getCtx(), M_PriceList_Version_ID);
98         m_ValidFrom = plv.getValidFrom();
99         m_M_PriceList_ID = plv.getM_PriceList_ID();
100     } // loadPLV
101

102     /**
103      * Get Price List Currency
104      * @return currency
105      */

106     public int getC_Currency_ID()
107     {
108         if (m_C_Currency_ID == 0)
109             loadPL();
110         return m_C_Currency_ID;
111     } // getC_Currency_ID
112

113     /**
114      * Is Price List enforded
115      * @return enforce limit
116      */

117     public boolean isEnforcePriceLimit()
118     {
119         if (m_enforcePriceLimit == null)
120             loadPL();
121         return m_enforcePriceLimit.booleanValue();
122     } // isEnforcePriceLimit
123

124     /**
125      * Load Price List Info
126      */

127     private void loadPL()
128     {
129         if (m_M_PriceList_ID == 0)
130             loadPLV();
131         X_M_PriceList pl = new X_M_PriceList (Env.getCtx(), m_M_PriceList_ID);
132         m_C_Currency_ID = pl.getC_Currency_ID();
133         m_enforcePriceLimit = new Boolean JavaDoc(pl.isEnforcePriceLimit());
134     } // loadPL
135

136     /*************************************************************************/
137     
138     /**
139      * Get Prices for product from default price list
140      * @param M_Product_ID product
141      * @param SOPriceList Sales Order PL
142      * @param docDate document date
143      * @return prices
144      */

145     public static MProductPriceVO getBOMPrices (int M_Product_ID, boolean SOPriceList, Timestamp docDate)
146     {
147         if (docDate == null)
148             docDate = new Timestamp (System.currentTimeMillis());
149         MProductPriceVO retValue = null;
150         String JavaDoc sql = "SELECT pp.M_Product_ID,plv.M_PriceList_Version_ID, " // 1..2
151
+ "BOM_PriceList(pp.M_Product_ID,plv.M_PriceList_Version_ID) AS PriceList,"
152             + "BOM_PriceStd(pp.M_Product_ID,plv.M_PriceList_Version_ID) AS PriceStd,"
153             + "BOM_PriceLimit(pp.M_Product_ID,plv.M_PriceList_Version_ID) AS PriceLimit,"
154             + "plv.ValidFrom, pl.C_Currency_ID,pl.EnforcePriceLimit " // 6..8
155
+ "FROM M_ProductPrice pp"
156             + " INNER JOIN M_PriceList_Version plv ON (pp.M_PriceList_Version_ID=plv.M_PriceList_Version_ID)"
157             + " INNER JOIN M_Pricelist pl ON (plv.M_PriceList_ID=pl.M_PriceList_ID) "
158             + "WHERE pp.M_Product_ID=?"
159             + " AND pl.IsDefault='Y' AND pl.IsSOPriceList=" + (SOPriceList ? "'Y'" : "'N'")
160             + " AND plv.IsActive='Y' AND pl.IsActive='Y' "
161             + "ORDER BY pl.M_PriceList_ID, plv.ValidFrom DESC";
162         try
163         {
164             PreparedStatement pstmt = DB.prepareStatement(sql);
165             pstmt.setInt(1, M_Product_ID);
166             ResultSet rs = pstmt.executeQuery();
167             boolean noPrice = true;
168             while (noPrice && rs.next())
169             {
170                 Timestamp plvDate = rs.getTimestamp(6);
171                 // we have the price list
172
// if order date is after or equal PriceList validFrom
173
if (plvDate == null || !docDate.before(plvDate))
174                 {
175                     noPrice = false;
176                     retValue = new MProductPriceVO (rs.getInt(1), rs.getInt(2),
177                         rs.getBigDecimal(3), rs.getBigDecimal(4), rs.getBigDecimal(5));
178                     retValue.m_ValidFrom = plvDate;
179                     retValue.m_C_Currency_ID = rs.getInt(7);
180                     retValue.m_enforcePriceLimit = new Boolean JavaDoc("Y".equals(rs.getString(8)));
181                 }
182             }
183             rs.close();
184             pstmt.close();
185         }
186         catch (Exception JavaDoc e)
187         {
188             s_log.error("getBOMPricesPL\n" + sql, e);
189         }
190         return retValue;
191     } // getBOMPrices
192

193     /**
194      * Get Prices for product and PL
195      * @param M_Product_ID product
196      * @param M_PriceList_ID PL
197      * @param docDate document date
198      * @return prices
199      */

200     public static MProductPriceVO getBOMPricesPL (int M_Product_ID, int M_PriceList_ID, Timestamp docDate)
201     {
202         if (docDate == null)
203             docDate = new Timestamp (System.currentTimeMillis());
204         MProductPriceVO retValue = null;
205         String JavaDoc sql = "SELECT pp.M_Product_ID,plv.M_PriceList_Version_ID, " // 1..2
206
+ "BOM_PriceList(pp.M_Product_ID,plv.M_PriceList_Version_ID) AS PriceList,"
207             + "BOM_PriceStd(pp.M_Product_ID,plv.M_PriceList_Version_ID) AS PriceStd,"
208             + "BOM_PriceLimit(pp.M_Product_ID,plv.M_PriceList_Version_ID) AS PriceLimit,"
209             + "plv.ValidFrom, pl.C_Currency_ID,pl.EnforcePriceLimit " // 6..8
210
+ "FROM M_ProductPrice pp"
211             + " INNER JOIN M_PriceList_Version plv ON (pp.M_PriceList_Version_ID=plv.M_PriceList_Version_ID)"
212             + " INNER JOIN M_Pricelist pl ON (plv.M_PriceList_ID=pl.M_PriceList_ID) "
213             + "WHERE pp.M_Product_ID=?"
214             + " AND pl.M_PriceList_ID=?"
215             + " AND plv.IsActive='Y' AND pl.IsActive='Y' "
216             + "ORDER BY plv.ValidFrom DESC";
217         try
218         {
219             PreparedStatement pstmt = DB.prepareStatement(sql);
220             pstmt.setInt(1, M_Product_ID);
221             pstmt.setInt(2, M_PriceList_ID);
222             ResultSet rs = pstmt.executeQuery();
223             boolean noPrice = true;
224             while (noPrice && rs.next())
225             {
226                 Timestamp plvDate = rs.getTimestamp(6);
227                 // we have the price list
228
// if order date is after or equal PriceList validFrom
229
if (plvDate == null || !docDate.before(plvDate))
230                 {
231                     noPrice = false;
232                     retValue = new MProductPriceVO (rs.getInt(1), rs.getInt(2),
233                         rs.getBigDecimal(3), rs.getBigDecimal(4), rs.getBigDecimal(5));
234                     retValue.m_ValidFrom = plvDate;
235                     retValue.m_C_Currency_ID = rs.getInt(7);
236                     retValue.m_enforcePriceLimit = new Boolean JavaDoc("Y".equals(rs.getString(8)));
237                 }
238             }
239             rs.close();
240             pstmt.close();
241         }
242         catch (Exception JavaDoc e)
243         {
244             s_log.error("getBOMPricesPL", e);
245         }
246         return retValue;
247     } // getBOMPricesPL
248

249     /**
250      * Get Prices for product and PLV
251      * @param M_Product_ID product
252      * @param M_PriceList_Version_ID PLV
253      * @return prices
254      */

255     public static MProductPriceVO getBOMPricesPLV (int M_Product_ID, int M_PriceList_Version_ID)
256     {
257         MProductPriceVO retValue = null;
258         String JavaDoc sql = "SELECT pp.M_Product_ID,plv.M_PriceList_Version_ID, " // 1..2
259
+ "BOM_PriceList(pp.M_Product_ID,plv.M_PriceList_Version_ID) AS PriceList,"
260             + "BOM_PriceStd(pp.M_Product_ID,plv.M_PriceList_Version_ID) AS PriceStd,"
261             + "BOM_PriceLimit(pp.M_Product_ID,plv.M_PriceList_Version_ID) AS PriceLimit,"
262             + "plv.ValidFrom, pl.C_Currency_ID,pl.EnforcePriceLimit " // 6..8
263
+ "FROM M_ProductPrice pp"
264             + " INNER JOIN M_PriceList_Version plv ON (pp.M_PriceList_Version_ID=plv.M_PriceList_Version_ID)"
265             + " INNER JOIN M_PriceList pl ON (plv.M_PriceList_ID=pl.M_PriceList_ID) "
266             + "WHERE pp.M_Product_ID=?"
267             + " AND plv.M_PriceList_Version_ID=?"
268             + " AND plv.IsActive='Y' AND pl.IsActive='Y'";
269         try
270         {
271             PreparedStatement pstmt = DB.prepareStatement(sql);
272             pstmt.setInt(1, M_Product_ID);
273             pstmt.setInt(2, M_PriceList_Version_ID);
274             ResultSet rs = pstmt.executeQuery();
275             if (rs.next())
276             {
277                 retValue = new MProductPriceVO (rs.getInt(1), rs.getInt(2),
278                     rs.getBigDecimal(3), rs.getBigDecimal(4), rs.getBigDecimal(5));
279                 retValue.m_ValidFrom = rs.getTimestamp(6);
280                 retValue.m_C_Currency_ID = rs.getInt(7);
281                 retValue.m_enforcePriceLimit = new Boolean JavaDoc("Y".equals(rs.getString(8)));
282             }
283             rs.close();
284             pstmt.close();
285         }
286         catch (Exception JavaDoc e)
287         {
288             s_log.error("getBOMPricesPLV", e);
289         }
290         return retValue;
291     } // getBOMPricesPLV
292

293 } // MProductPriceVO
294
Popular Tags