📄 mproductpricing.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 Smart Business Solution. The Initial
* Developer of the Original Code is Jorg Janke. Portions created by Jorg Janke
* are Copyright (C) 1999-2005 Jorg Janke.
* All parts are Copyright (C) 1999-2005 ComPiere, Inc. All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.model;
import java.math.*;
import java.sql.*;
import java.util.logging.*;
import org.compiere.util.*;
/**
* Product Price Calculations
*
* @author Jorg Janke
* @version $Id: MProductPricing.java,v 1.16 2005/11/25 21:57:24 jjanke Exp $
*/
public class MProductPricing
{
/**
* Constructor
* @param M_Product_ID product
* @param C_BPartner_ID partner
* @param Qty quantity
* @param isSOTrx SO or PO
*/
public MProductPricing (int M_Product_ID, int C_BPartner_ID,
BigDecimal Qty, boolean isSOTrx)
{
m_M_Product_ID = M_Product_ID;
m_C_BPartner_ID = C_BPartner_ID;
if (Qty != null && Env.ZERO.compareTo(Qty) != 0)
m_Qty = Qty;
m_isSOTrx = isSOTrx;
} // MProductPricing
private int m_M_Product_ID;
private int m_C_BPartner_ID;
private BigDecimal m_Qty = Env.ONE;
private boolean m_isSOTrx = true;
//
private int m_M_PriceList_ID = 0;
private int m_M_PriceList_Version_ID = 0;
private Timestamp m_PriceDate;
/** Precision -1 = no rounding */
private int m_precision = -1;
private boolean m_calculated = false;
private Boolean m_found = null;
private BigDecimal m_PriceList = Env.ZERO;
private BigDecimal m_PriceStd = Env.ZERO;
private BigDecimal m_PriceLimit = Env.ZERO;
private int m_C_Currency_ID = 0;
private boolean m_enforcePriceLimit = false;
private int m_C_UOM_ID = 0;
private int m_M_Product_Category_ID;
private boolean m_discountSchema = false;
private boolean m_isTaxIncluded = false;
/** Logger */
protected CLogger log = CLogger.getCLogger(getClass());
/**
* Calculate Price
* @return true if calculated
*/
public boolean calculatePrice ()
{
if (m_M_Product_ID == 0
|| (m_found != null && !m_found.booleanValue())) // previously not found
return false;
// Price List Version known
if (!m_calculated)
m_calculated = calculatePLV ();
// Price List known
if (!m_calculated)
m_calculated = calculatePL();
// Base Price List used
if (!m_calculated)
m_calculated = calculateBPL();
// Set UOM, Prod.Category
if (!m_calculated)
setBaseInfo();
// User based Discount
if (m_calculated)
calculateDiscount();
setPrecision(); // from Price List
//
m_found = new Boolean (m_calculated);
return m_calculated;
} // calculatePrice
/**
* Calculate Price based on Price List Version
* @return true if calculated
*/
private boolean calculatePLV()
{
if (m_M_Product_ID == 0 || m_M_PriceList_Version_ID == 0)
return false;
//
String sql = "SELECT bomPriceStd(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceStd," // 1
+ " bomPriceList(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceList," // 2
+ " bomPriceLimit(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceLimit," // 3
+ " p.C_UOM_ID,pv.ValidFrom,pl.C_Currency_ID,p.M_Product_Category_ID," // 4..7
+ " pl.EnforcePriceLimit, pl.IsTaxIncluded " // 8..9
+ "FROM M_Product p"
+ " INNER JOIN M_ProductPrice pp ON (p.M_Product_ID=pp.M_Product_ID)"
+ " INNER JOIN M_PriceList_Version pv ON (pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID)"
+ " INNER JOIN M_Pricelist pl ON (pv.M_PriceList_ID=pl.M_PriceList_ID) "
+ "WHERE pv.IsActive='Y'"
+ " AND p.M_Product_ID=?" // #1
+ " AND pv.M_PriceList_Version_ID=?"; // #2
m_calculated = false;
try
{
PreparedStatement pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, m_M_Product_ID);
pstmt.setInt(2, m_M_PriceList_Version_ID);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
// Prices
m_PriceStd = rs.getBigDecimal(1);
if (rs.wasNull())
m_PriceStd = Env.ZERO;
m_PriceList = rs.getBigDecimal(2);
if (rs.wasNull())
m_PriceList = Env.ZERO;
m_PriceLimit = rs.getBigDecimal(3);
if (rs.wasNull())
m_PriceLimit = Env.ZERO;
//
m_C_UOM_ID = rs.getInt(4);
m_C_Currency_ID = rs.getInt(6);
m_M_Product_Category_ID = rs.getInt(7);
m_enforcePriceLimit = "Y".equals(rs.getString(8));
m_isTaxIncluded = "Y".equals(rs.getString(9));
//
log.fine("M_PriceList_Version_ID=" + m_M_PriceList_Version_ID + " - " + m_PriceStd);
m_calculated = true;
}
rs.close();
pstmt.close();
}
catch (Exception e)
{
log.log(Level.SEVERE, sql, e);
m_calculated = false;
}
return m_calculated;
} // calculatePLV
/**
* Calculate Price based on Price List
* @return true if calculated
*/
private boolean calculatePL()
{
if (m_M_Product_ID == 0)
return false;
// Get Price List
/**
if (m_M_PriceList_ID == 0)
{
String sql = "SELECT M_PriceList_ID, IsTaxIncluded "
+ "FROM M_PriceList pl"
+ " INNER JOIN M_Product p ON (pl.AD_Client_ID=p.AD_Client_ID) "
+ "WHERE M_Product_ID=? "
+ "ORDER BY IsDefault DESC";
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement(sql);
pstmt.setInt(1, m_M_Product_ID);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
m_M_PriceList_ID = rs.getInt(1);
m_isTaxIncluded = "Y".equals(rs.getString(2));
}
rs.close();
pstmt.close();
pstmt = null;
}
catch (Exception e)
{
log.log(Level.SEVERE, "calculatePL (PL)", e);
}
finally
{
try
{
if (pstmt != null)
pstmt.close ();
}
catch (Exception e)
{}
pstmt = null;
}
}
/** **/
if (m_M_PriceList_ID == 0)
{
log.log(Level.SEVERE, "No PriceList");
Trace.printStack();
return false;
}
// Get Prices for Price List
String sql = "SELECT bomPriceStd(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceStd," // 1
+ " bomPriceList(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceList," // 2
+ " bomPriceLimit(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceLimit," // 3
+ " p.C_UOM_ID,pv.ValidFrom,pl.C_Currency_ID,p.M_Product_Category_ID,pl.EnforcePriceLimit " // 4..8
+ "FROM M_Product p"
+ " INNER JOIN M_ProductPrice pp ON (p.M_Product_ID=pp.M_Product_ID)"
+ " INNER JOIN M_PriceList_Version pv ON (pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID)"
+ " INNER JOIN M_Pricelist pl ON (pv.M_PriceList_ID=pl.M_PriceList_ID) "
+ "WHERE pv.IsActive='Y'"
+ " AND p.M_Product_ID=?" // #1
+ " AND pv.M_PriceList_ID=?" // #2
+ "ORDER BY pv.ValidFrom DESC";
m_calculated = false;
if (m_PriceDate == null)
m_PriceDate = new Timestamp (System.currentTimeMillis());
try
{
PreparedStatement pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, m_M_Product_ID);
pstmt.setInt(2, m_M_PriceList_ID);
ResultSet rs = pstmt.executeQuery();
while (!m_calculated && rs.next())
{
Timestamp plDate = rs.getTimestamp(5);
// we have the price list
// if order date is after or equal PriceList validFrom
if (plDate == null || !m_PriceDate.before(plDate))
{
// Prices
m_PriceStd = rs.getBigDecimal (1);
if (rs.wasNull ())
m_PriceStd = Env.ZERO;
m_PriceList = rs.getBigDecimal (2);
if (rs.wasNull ())
m_PriceList = Env.ZERO;
m_PriceLimit = rs.getBigDecimal (3);
if (rs.wasNull ())
m_PriceLimit = Env.ZERO;
//
m_C_UOM_ID = rs.getInt (4);
m_C_Currency_ID = rs.getInt (6);
m_M_Product_Category_ID = rs.getInt(7);
m_enforcePriceLimit = "Y".equals(rs.getString(8));
//
log.fine("M_PriceList_ID=" + m_M_PriceList_ID
+ "(" + plDate + ")" + " - " + m_PriceStd);
m_calculated = true;
break;
}
}
rs.close();
pstmt.close();
}
catch (Exception e)
{
log.log(Level.SEVERE, sql, e);
m_calculated = false;
}
if (!m_calculated)
log.finer("Not found (PL)");
return m_calculated;
} // calculatePL
/**
* Calculate Price based on Base Price List
* @return true if calculated
*/
private boolean calculateBPL()
{
if (m_M_Product_ID == 0 || m_M_PriceList_ID == 0)
return false;
//
String sql = "SELECT bomPriceStd(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceStd," // 1
+ " bomPriceList(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceList," // 2
+ " bomPriceLimit(p.M_Product_ID,pv.M_PriceList_Version_ID) AS PriceLimit," // 3
+ " p.C_UOM_ID,pv.ValidFrom,pl.C_Currency_ID,p.M_Product_Category_ID," // 4..7
+ " pl.EnforcePriceLimit, pl.IsTaxIncluded " // 8..9
+ "FROM M_Product p"
+ " INNER JOIN M_ProductPrice pp ON (p.M_Product_ID=pp.M_Product_ID)"
+ " INNER JOIN M_PriceList_Version pv ON (pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID)"
+ " INNER JOIN M_Pricelist bpl ON (pv.M_PriceList_ID=bpl.M_PriceList_ID)"
+ " INNER JOIN M_Pricelist pl ON (bpl.M_PriceList_ID=pl.BasePriceList_ID) "
+ "WHERE pv.IsActive='Y'"
+ " AND p.M_Product_ID=?" // #1
+ " AND pl.M_PriceList_ID=?" // #2
+ "ORDER BY pv.ValidFrom DESC";
m_calculated = false;
if (m_PriceDate == null)
m_PriceDate = new Timestamp (System.currentTimeMillis());
try
{
PreparedStatement pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, m_M_Product_ID);
pstmt.setInt(2, m_M_PriceList_ID);
ResultSet rs = pstmt.executeQuery();
while (!m_calculated && rs.next())
{
Timestamp plDate = rs.getTimestamp(5);
// we have the price list
// if order date is after or equal PriceList validFrom
if (plDate == null || !m_PriceDate.before(plDate))
{
// Prices
m_PriceStd = rs.getBigDecimal (1);
if (rs.wasNull ())
m_PriceStd = Env.ZERO;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -