KickJava   Java API By Example, From Geeks To Geeks.

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


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-2002 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.awt.*;
17 import java.sql.*;
18 import java.util.*;
19 import java.math.*;
20
21 import org.compiere.util.*;
22
23
24 /**
25  * Unit of Measure Conversion Utilities
26  *
27  * @author Jorg Janke
28  * @version $Id: UOMConversion.java,v 1.7 2003/11/02 07:49:20 jjanke Exp $
29  */

30 public class UOMConversion
31 {
32     /**
33      * Convert qty to target UOM and round.
34      * @param ctx context
35      * @param C_UOM_ID from UOM
36      * @param C_UOM_To_ID to UOM
37      * @param qty qty
38      * @return converted qty
39      */

40     static public BigDecimal convert (Properties ctx,
41         int C_UOM_ID, int C_UOM_To_ID, BigDecimal qty)
42     {
43         if (qty == null || qty.equals(Env.ZERO) || C_UOM_ID == C_UOM_To_ID)
44             return qty;
45         BigDecimal retValue = convert (ctx, C_UOM_ID, C_UOM_To_ID);
46         if (retValue != null)
47         {
48             UOM uom = UOM.get (ctx, C_UOM_To_ID);
49             if (uom != null)
50                 return uom.round(retValue.multiply(qty));
51             return retValue.multiply(qty);
52         }
53         return null;
54     } // convert
55

56     /**
57      * Get Multiplier to target UOM
58      * @param ctx context
59      * @param C_UOM_ID from UOM
60      * @param C_UOM_To_ID to UOM
61      * @return multiplier
62      */

63     static public BigDecimal convert (Properties ctx,
64         int C_UOM_ID, int C_UOM_To_ID)
65     {
66         // nothing to do
67
if (C_UOM_ID == C_UOM_To_ID)
68             return ONE;
69         //
70
Point p = new Point(C_UOM_ID, C_UOM_To_ID);
71         // get conversion
72
BigDecimal retValue = getMultiplier(ctx, p);
73         return retValue;
74     } // convert
75

76     /**
77      * Convert qty to target UOM and round.
78      * @param ctx context
79      * @param C_UOM_ID from UOM
80      * @param qty qty
81      * @return minutes - 0 if not found
82      */

83     static public int convertToMinutes (Properties ctx,
84         int C_UOM_ID, BigDecimal qty)
85     {
86         if (qty == null)
87             return 0;
88         int C_UOM_To_ID = UOM.getMinute_UOM_ID(ctx);
89         if (C_UOM_ID == C_UOM_To_ID)
90             return qty.intValue();
91         //
92
BigDecimal result = convert (ctx, C_UOM_ID, C_UOM_To_ID, qty);
93         if (result == null)
94             return 0;
95         return result.intValue();
96     } // convert
97

98     /**
99      * Calculate End Date based on start date and qty
100      * @param ctx context
101      * @param startDate date
102      * @param C_UOM_ID UOM
103      * @param qty qty
104      * @return end date
105      */

106     static public Timestamp getEndDate (Properties ctx, Timestamp startDate, int C_UOM_ID, BigDecimal qty)
107     {
108         GregorianCalendar endDate = new GregorianCalendar();
109         endDate.setTime(startDate);
110         //
111
int minutes = UOMConversion.convertToMinutes (ctx, C_UOM_ID, qty);
112         endDate.add(Calendar.MINUTE, minutes);
113         //
114
Timestamp retValue = new Timestamp(endDate.getTimeInMillis());
115     // Log.trace(Log.l4_Data, "TimeUtil.getEndDate", "Start=" + startDate
116
// + ", Qty=" + qty + ", End=" + retValue);
117
return retValue;
118     } // startDate
119

120     /*************************************************************************/
121
122     /**
123      * Get Conversion Multiplier, try to derive it if not found directly
124      * @param ctx context
125      * @param p Point with from(x) - to(y) C_UOM_ID
126      * @return conversion multiplier or null
127      */

128     static private BigDecimal getMultiplier (Properties ctx, Point p)
129     {
130         BigDecimal retValue = null;
131         if (Ini.isClient())
132         {
133             if (s_conversions == null)
134                 createConversions(ctx);
135             retValue = (BigDecimal)s_conversions.get(p);
136         }
137         else
138             retValue = getConversion (ctx, p.x, p.y);
139         if (retValue != null)
140             return retValue;
141         // try to derive
142
return deriveConversion(ctx, p.x, p.y);
143     } // getConversion
144

145     /**
146      * Get Conversion Multiplier from Server
147      * @param ctx context
148      * @param C_UOM_ID from UOM
149      * @param C_UOM_To_ID to UOM
150      * @return conversion multiplier or null
151      */

152     public static BigDecimal getConversion (Properties ctx, int C_UOM_ID, int C_UOM_To_ID)
153     {
154         return getConvertedQty (ONE, C_UOM_ID, C_UOM_To_ID, false);
155     } // getConversion
156

157     /**
158      * Get Converted Qty
159      * @param qty The quantity to be converted
160      * @param C_UOM_From_ID The C_UOM_ID of the qty
161      * @param C_UOM_To_ID The targeted UOM
162      * @param StdPrecision if true, standard precision, if false costing precision
163      * @return amount
164      * @depreciated should not be used
165      */

166     public static BigDecimal getConvertedQty (BigDecimal qty,
167         int C_UOM_From_ID, int C_UOM_To_ID, boolean StdPrecision)
168     {
169         // Nothing to do
170
if (qty == null || qty.equals(Env.ZERO)
171                 || C_UOM_From_ID == C_UOM_To_ID)
172             return qty;
173         //
174
BigDecimal retValue = null;
175         try
176         {
177             String JavaDoc sql = "{? = call C_UOM_Convert (?,?,?,?)}";
178             CallableStatement cstmt = DB.prepareCall(sql);
179             //
180
cstmt.registerOutParameter (1, Types.NUMERIC);
181             //
182
cstmt.setBigDecimal(2, qty);
183             cstmt.setInt(3, C_UOM_From_ID);
184             cstmt.setInt(4, C_UOM_To_ID);
185             cstmt.setString (5, StdPrecision ? "Y" : "N");
186             //
187
cstmt.execute ();
188             // Result
189
retValue = cstmt.getBigDecimal (1);
190             cstmt.close();
191         }
192         catch (SQLException e)
193         {
194             Log.error("DB.getConvertedQty", e);
195             return null;
196         }
197         return retValue;
198     } // getConvertedQty
199

200
201
202     /** One */
203     public static BigDecimal ONE = new BigDecimal(1.0);
204
205     /** Conversion Map: Key=Point(from,to) Value=BigDecimal */
206     private static HashMap s_conversions = null;
207
208     /**
209      * Create Conversion Matrix (Client)
210      * @param ctx context
211      */

212     private static void createConversions(Properties ctx)
213     {
214         s_conversions = new HashMap();
215         //
216
String JavaDoc sql = MRole.getDefault(ctx, false).addAccessSQL (
217             "SELECT C_UOM_ID, C_UOM_To_ID, MultiplyRate, DivideRate "
218             + "FROM C_UOM_Conversion "
219             + "WHERE IsActive='Y'",
220             "C_UOM_Conversion", MRole.SQL_NOTQUALIFIED, MRole.SQL_RO);
221         try
222         {
223             PreparedStatement pstmt = DB.prepareStatement(sql);
224             ResultSet rs = pstmt.executeQuery();
225             while (rs.next())
226             {
227                 Point p = new Point (rs.getInt(1), rs.getInt(2));
228                 BigDecimal mr = rs.getBigDecimal(3);
229                 BigDecimal dr = rs.getBigDecimal(4);
230                 if (mr != null)
231                     s_conversions.put(p, mr);
232                 // reverse
233
if (dr == null && mr != null)
234                     dr = ONE.divide(mr, BigDecimal.ROUND_HALF_UP);
235                 if (dr != null)
236                     s_conversions.put(new Point(p.y,p.x), dr);
237             }
238             rs.close();
239             pstmt.close();
240         }
241         catch (SQLException e)
242         {
243             Log.error("UOMConversion.createConversions", e);
244         }
245     } // createConversions
246

247     /**
248      * Derive Standard Conversions
249      * @param ctx context
250      * @param C_UOM_ID from UOM
251      * @param C_UOM_To_ID to UOM
252      * @return Conversion or null
253      */

254     public static BigDecimal deriveConversion(Properties ctx,
255         int C_UOM_ID, int C_UOM_To_ID)
256     {
257         if (C_UOM_ID == C_UOM_To_ID)
258             return ONE;
259         // get Info
260
UOM from = UOM.get (ctx, C_UOM_ID);
261         UOM to = UOM.get (ctx, C_UOM_To_ID);
262         if (from == null || to == null)
263             return null;
264
265         // Time - Minute
266
if (from.isMinute())
267         {
268             if (to.isHour())
269                 return new BigDecimal(1.0/60.0);
270             if (to.isDay())
271                 return new BigDecimal(1.0/1440.0); // 24 * 60
272
if (to.isWorkDay())
273                 return new BigDecimal(1.0/480.0); // 8 * 60
274
if (to.isWeek())
275                 return new BigDecimal(1.0/10080.0); // 7 * 24 * 60
276
if (to.isMonth())
277                 return new BigDecimal(1.0/43200.0); // 30 * 24 * 60
278
if (to.isWorkMonth())
279                 return new BigDecimal(1.0/9600.0); // 4 * 5 * 8 * 60
280
if (to.isYear())
281                 return new BigDecimal(1.0/525600.0); // 365 * 24 * 60
282
}
283         // Time - Hour
284
if (from.isHour())
285         {
286             if (to.isMinute())
287                 return new BigDecimal(60.0);
288             if (to.isDay())
289                 return new BigDecimal(1.0/24.0);
290             if (to.isWorkDay())
291                 return new BigDecimal(1.0/8.0);
292             if (to.isWeek())
293                 return new BigDecimal(1.0/168.0); // 7 * 24
294
if (to.isMonth())
295                 return new BigDecimal(1.0/720.0); // 30 * 24
296
if (to.isWorkMonth())
297                 return new BigDecimal(1.0/160.0); // 4 * 5 * 8
298
if (to.isYear())
299                 return new BigDecimal(1.0/8760.0); // 365 * 24
300
}
301         // Time - Day
302
if (from.isDay())
303         {
304             if (to.isMinute())
305                 return new BigDecimal(1440.0); // 24 * 60
306
if (to.isHour())
307                 return new BigDecimal(24.0);
308             if (to.isWorkDay())
309                 return new BigDecimal(3.0); // 24 / 8
310
if (to.isWeek())
311                 return new BigDecimal(1.0/7.0); // 7
312
if (to.isMonth())
313                 return new BigDecimal(1.0/30.0); // 30
314
if (to.isWorkMonth())
315                 return new BigDecimal(1.0/20.0); // 4 * 5
316
if (to.isYear())
317                 return new BigDecimal(1.0/365.0); // 365
318
}
319         // Time - WorkDay
320
if (from.isWorkDay())
321         {
322             if (to.isMinute())
323                 return new BigDecimal(480.0); // 8 * 60
324
if (to.isHour())
325                 return new BigDecimal(8.0); // 8
326
if (to.isDay())
327                 return new BigDecimal(1.0/3.0); // 24 / 8
328
if (to.isWeek())
329                 return new BigDecimal(1.0/5); // 5
330
if (to.isMonth())
331                 return new BigDecimal(1.0/20.0); // 4 * 5
332
if (to.isWorkMonth())
333                 return new BigDecimal(1.0/20.0); // 4 * 5
334
if (to.isYear())
335                 return new BigDecimal(1.0/240.0); // 4 * 5 * 12
336
}
337         // Time - Week
338
if (from.isWeek())
339         {
340             if (to.isMinute())
341                 return new BigDecimal(10080.0); // 7 * 24 * 60
342
if (to.isHour())
343                 return new BigDecimal(168.0); // 7 * 24
344
if (to.isDay())
345                 return new BigDecimal(7.0);
346             if (to.isWorkDay())
347                 return new BigDecimal(5.0);
348             if (to.isMonth())
349                 return new BigDecimal(1.0/4.0); // 4
350
if (to.isWorkMonth())
351                 return new BigDecimal(1.0/4.0); // 4
352
if (to.isYear())
353                 return new BigDecimal(1.0/50.0); // 50
354
}
355         // Time - Month
356
if (from.isMonth())
357         {
358             if (to.isMinute())
359                 return new BigDecimal(43200.0); // 30 * 24 * 60
360
if (to.isHour())
361                 return new BigDecimal(720.0); // 30 * 24
362
if (to.isDay())
363                 return new BigDecimal(30.0); // 30
364
if (to.isWorkDay())
365                 return new BigDecimal(20.0); // 4 * 5
366
if (to.isWeek())
367                 return new BigDecimal(4.0); // 4
368
if (to.isWorkMonth())
369                 return new BigDecimal(1.5); // 30 / 20
370
if (to.isYear())
371                 return new BigDecimal(1.0/12.0); // 12
372
}
373         // Time - WorkMonth
374
if (from.isWorkMonth())
375         {
376             if (to.isMinute())
377                 return new BigDecimal(9600.0); // 4 * 5 * 8 * 60
378
if (to.isHour())
379                 return new BigDecimal(160.0); // 4 * 5 * 8
380
if (to.isDay())
381                 return new BigDecimal(20.0); // 4 * 5
382
if (to.isWorkDay())
383                 return new BigDecimal(20.0); // 4 * 5
384
if (to.isWeek())
385                 return new BigDecimal(4.0); // 4
386
if (to.isMonth())
387                 return new BigDecimal(20.0/30.0); // 20 / 30
388
if (to.isYear())
389                 return new BigDecimal(1.0/12.0); // 12
390
}
391         // Time - Year
392
if (from.isYear())
393         {
394             if (to.isMinute())
395                 return new BigDecimal(518400.0); // 12 * 30 * 24 * 60
396
if (to.isHour())
397                 return new BigDecimal(8640.0); // 12 * 30 * 24
398
if (to.isDay())
399                 return new BigDecimal(365.0); // 365
400
if (to.isWorkDay())
401                 return new BigDecimal(240.0); // 12 * 4 * 5
402
if (to.isWeek())
403                 return new BigDecimal(50.0); // 52
404
if (to.isMonth())
405                 return new BigDecimal(12.0); // 12
406
if (to.isWorkMonth())
407                 return new BigDecimal(12.0); // 12
408
}
409         //
410
return null;
411     } // deriveConversion
412

413 } // UOMConversion
414
Popular Tags