📄 uomconversion.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Business Solution
* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
* Portions created by Jorg Janke are Copyright (C) 1999-2002 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.model;
import java.awt.*;
import java.sql.*;
import java.util.*;
import java.math.*;
import org.compiere.util.*;
/**
* Unit of Measure Conversion Utilities
*
* @author Jorg Janke
* @version $Id: UOMConversion.java,v 1.3 2002/08/12 01:55:12 danb Exp $
*/
public class UOMConversion
{
/**
* Convert qty to target UOM and round.
* @param ctx context
* @param C_UOM_ID from UOM
* @param C_UOM_To_ID to UOM
* @param qty qty
* @return converted qty
*/
static public BigDecimal convert (Properties ctx,
int C_UOM_ID, int C_UOM_To_ID, BigDecimal qty)
{
if (qty == null || qty.equals(Env.ZERO) || C_UOM_ID == C_UOM_To_ID)
return qty;
BigDecimal retValue = convert (ctx, C_UOM_ID, C_UOM_To_ID);
if (retValue != null)
{
UOM uom = UOM.getUOM (ctx, C_UOM_To_ID);
if (uom != null)
return uom.round(retValue.multiply(qty));
return retValue.multiply(qty);
}
return null;
} // convert
/**
* Get Multiplier to target UOM
* @param ctx context
* @param C_UOM_ID from UOM
* @param C_UOM_To_ID to UOM
* @return multiplier
*/
static public BigDecimal convert (Properties ctx,
int C_UOM_ID, int C_UOM_To_ID)
{
// nothing to do
if (C_UOM_ID == C_UOM_To_ID)
return ONE;
//
Point p = new Point(C_UOM_ID, C_UOM_To_ID);
// get conversion
BigDecimal retValue = getMultiplier(ctx, p);
return retValue;
} // convert
/**
* Convert qty to target UOM and round.
* @param ctx context
* @param C_UOM_ID from UOM
* @param qty qty
* @return minutes - 0 if not found
*/
static public int convertToMinutes (Properties ctx,
int C_UOM_ID, BigDecimal qty)
{
if (qty == null)
return 0;
int C_UOM_To_ID = UOM.getMinute_UOM_ID(ctx);
if (C_UOM_ID == C_UOM_To_ID)
return qty.intValue();
//
BigDecimal result = convert (ctx, C_UOM_ID, C_UOM_To_ID, qty);
if (result == null)
return 0;
return result.intValue();
} // convert
/*************************************************************************/
/**
* Get Conversion Multiplier, try to derive it if not found directly
* @param ctx context
* @param p Point with from(x) - to(y) C_UOM_ID
* @return conversion multiplier or null
*/
static private BigDecimal getMultiplier (Properties ctx, Point p)
{
BigDecimal retValue = null;
if (Ini.isClient())
{
if (s_conversions == null)
createConversions(ctx);
retValue = (BigDecimal)s_conversions.get(p);
}
else
retValue = getConversion (ctx, p.x, p.y);
if (retValue != null)
return retValue;
// try to derive
return deriveConversion(ctx, p.x, p.y);
} // getConversion
/**
* Get Conversion Multiplier from Server
* @param ctx context
* @param C_UOM_ID from UOM
* @param C_UOM_To_ID to UOM
* @return conversion multiplier or null
*/
public static BigDecimal getConversion (Properties ctx, int C_UOM_ID, int C_UOM_To_ID)
{
return getConvertedQty (ONE, C_UOM_ID, C_UOM_To_ID, false);
} // getConversion
/**
* Get Converted Qty
* @param qty The quantity to be converted
* @param C_UOM_From_ID The C_UOM_ID of the qty
* @param C_UOM_To_ID The targeted UOM
* @param StdPrecision if true, standard precision, if false costing precision
* @return amount
* @depreciated should not be used
*/
public static BigDecimal getConvertedQty (BigDecimal qty,
int C_UOM_From_ID, int C_UOM_To_ID, boolean StdPrecision)
{
// Nothing to do
if (qty == null || qty.equals(Env.ZERO)
|| C_UOM_From_ID == C_UOM_To_ID)
return qty;
//
BigDecimal retValue = null;
try
{
String sql = "{? = call C_UOM_Convert (?,?,?,?)}";
CallableStatement cstmt = DB.prepareCall(sql);
//
cstmt.registerOutParameter (1, Types.NUMERIC);
//
cstmt.setBigDecimal(2, qty);
cstmt.setInt(3, C_UOM_From_ID);
cstmt.setInt(4, C_UOM_To_ID);
cstmt.setString (5, StdPrecision ? "Y" : "N");
//
cstmt.execute ();
// Result
retValue = cstmt.getBigDecimal (1);
cstmt.close();
}
catch (SQLException e)
{
Log.error("DB.getConvertedQty", e);
return null;
}
return retValue;
} // getConvertedQty
/** One */
public static BigDecimal ONE = new BigDecimal(1.0);
/** Conversion Map: Key=Point(from,to) Value=BigDecimal */
private static HashMap s_conversions = null;
/**
* Create Conversion Matrix (Client)
* @param ctx context
*/
private static void createConversions(Properties ctx)
{
s_conversions = new HashMap();
//
String sql = Access.addROAccessSQL (ctx,
"SELECT C_UOM_ID, C_UOM_To_ID, MultiplyRate, DivideRate "
+ "FROM C_UOM_Conversion "
+ "WHERE IsActive='Y'",
"C_UOM_Conversion", false);
try
{
PreparedStatement pstmt = DB.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
Point p = new Point (rs.getInt(1), rs.getInt(2));
BigDecimal mr = rs.getBigDecimal(3);
BigDecimal dr = rs.getBigDecimal(4);
if (mr != null)
s_conversions.put(p, mr);
// reverse
if (dr == null && mr != null)
dr = ONE.divide(mr, BigDecimal.ROUND_HALF_UP);
if (dr != null)
s_conversions.put(new Point(p.y,p.x), dr);
}
rs.close();
pstmt.close();
}
catch (SQLException e)
{
Log.error("UOMConversion.createConversions", e);
}
} // createConversions
/**
* Derive Standard Conversions
* @param ctx context
* @param C_UOM_ID from UOM
* @param C_UOM_To_ID to UOM
* @return Conversion or null
*/
public static BigDecimal deriveConversion(Properties ctx,
int C_UOM_ID, int C_UOM_To_ID)
{
if (C_UOM_ID == C_UOM_To_ID)
return ONE;
// get Info
UOM from = UOM.getUOM (ctx, C_UOM_ID);
UOM to = UOM.getUOM (ctx, C_UOM_To_ID);
if (from == null || to == null)
return null;
// Time - Minute
if (from.isMinute())
{
if (to.isHour())
return new BigDecimal(1.0/60.0);
if (to.isDay())
return new BigDecimal(1.0/1440.0); // 24 * 60
if (to.isWorkDay())
return new BigDecimal(1.0/480.0); // 8 * 60
if (to.isWeek())
return new BigDecimal(1.0/10080.0); // 7 * 24 * 60
if (to.isMonth())
return new BigDecimal(1.0/43200.0); // 30 * 24 * 60
if (to.isWorkMonth())
return new BigDecimal(1.0/9600.0); // 4 * 5 * 8 * 60
if (to.isYear())
return new BigDecimal(1.0/525600.0); // 365 * 24 * 60
}
// Time - Hour
if (from.isHour())
{
if (to.isMinute())
return new BigDecimal(60.0);
if (to.isDay())
return new BigDecimal(1.0/24.0);
if (to.isWorkDay())
return new BigDecimal(1.0/8.0);
if (to.isWeek())
return new BigDecimal(1.0/168.0); // 7 * 24
if (to.isMonth())
return new BigDecimal(1.0/720.0); // 30 * 24
if (to.isWorkMonth())
return new BigDecimal(1.0/160.0); // 4 * 5 * 8
if (to.isYear())
return new BigDecimal(1.0/8760.0); // 365 * 24
}
// Time - Day
if (from.isDay())
{
if (to.isMinute())
return new BigDecimal(1440.0); // 24 * 60
if (to.isHour())
return new BigDecimal(24.0);
if (to.isWorkDay())
return new BigDecimal(3.0); // 24 / 8
if (to.isWeek())
return new BigDecimal(1.0/7.0); // 7
if (to.isMonth())
return new BigDecimal(1.0/30.0); // 30
if (to.isWorkMonth())
return new BigDecimal(1.0/20.0); // 4 * 5
if (to.isYear())
return new BigDecimal(1.0/365.0); // 365
}
// Time - WorkDay
if (from.isWorkDay())
{
if (to.isMinute())
return new BigDecimal(480.0); // 8 * 60
if (to.isHour())
return new BigDecimal(8.0); // 8
if (to.isDay())
return new BigDecimal(1.0/3.0); // 24 / 8
if (to.isWeek())
return new BigDecimal(1.0/5); // 5
if (to.isMonth())
return new BigDecimal(1.0/20.0); // 4 * 5
if (to.isWorkMonth())
return new BigDecimal(1.0/20.0); // 4 * 5
if (to.isYear())
return new BigDecimal(1.0/240.0); // 4 * 5 * 12
}
// Time - Week
if (from.isWeek())
{
if (to.isMinute())
return new BigDecimal(10080.0); // 7 * 24 * 60
if (to.isHour())
return new BigDecimal(168.0); // 7 * 24
if (to.isDay())
return new BigDecimal(7.0);
if (to.isWorkDay())
return new BigDecimal(5.0);
if (to.isMonth())
return new BigDecimal(1.0/4.0); // 4
if (to.isWorkMonth())
return new BigDecimal(1.0/4.0); // 4
if (to.isYear())
return new BigDecimal(1.0/50.0); // 50
}
// Time - Month
if (from.isMonth())
{
if (to.isMinute())
return new BigDecimal(43200.0); // 30 * 24 * 60
if (to.isHour())
return new BigDecimal(720.0); // 30 * 24
if (to.isDay())
return new BigDecimal(30.0); // 30
if (to.isWorkDay())
return new BigDecimal(20.0); // 4 * 5
if (to.isWeek())
return new BigDecimal(4.0); // 4
if (to.isWorkMonth())
return new BigDecimal(1.5); // 30 / 20
if (to.isYear())
return new BigDecimal(1.0/12.0); // 12
}
// Time - WorkMonth
if (from.isWorkMonth())
{
if (to.isMinute())
return new BigDecimal(9600.0); // 4 * 5 * 8 * 60
if (to.isHour())
return new BigDecimal(160.0); // 4 * 5 * 8
if (to.isDay())
return new BigDecimal(20.0); // 4 * 5
if (to.isWorkDay())
return new BigDecimal(20.0); // 4 * 5
if (to.isWeek())
return new BigDecimal(4.0); // 4
if (to.isMonth())
return new BigDecimal(20.0/30.0); // 20 / 30
if (to.isYear())
return new BigDecimal(1.0/12.0); // 12
}
// Time - Year
if (from.isYear())
{
if (to.isMinute())
return new BigDecimal(518400.0); // 12 * 30 * 24 * 60
if (to.isHour())
return new BigDecimal(8640.0); // 12 * 30 * 24
if (to.isDay())
return new BigDecimal(365.0); // 365
if (to.isWorkDay())
return new BigDecimal(240.0); // 12 * 4 * 5
if (to.isWeek())
return new BigDecimal(50.0); // 52
if (to.isMonth())
return new BigDecimal(12.0); // 12
if (to.isWorkMonth())
return new BigDecimal(12.0); // 12
}
//
return null;
} // deriveConversion
} // UOMConversion
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -