use of org.adempiere.exceptions.DBException in project adempiere by adempiere.
the class StatementProxy method init.
/**
* Initialise the statement wrapper object
*/
protected void init() {
try {
Connection conn = null;
Trx trx = p_vo.getTrxName() == null ? null : Trx.get(p_vo.getTrxName(), false);
if (trx != null) {
conn = trx.getConnection();
} else {
if (p_vo.getResultSetConcurrency() == ResultSet.CONCUR_UPDATABLE)
m_conn = DB.getConnectionRW();
else
m_conn = DB.getConnectionRO();
conn = m_conn;
}
if (conn == null)
throw new DBException("No Connection");
p_stmt = conn.createStatement(p_vo.getResultSetType(), p_vo.getResultSetConcurrency());
} catch (SQLException e) {
log.log(Level.SEVERE, "CStatement", e);
throw new DBException(e);
}
}
use of org.adempiere.exceptions.DBException in project adempiere by adempiere.
the class CallableStatementProxy method init.
/**
* Initialise the prepared statement wrapper object
*/
protected void init() {
try {
Connection conn = null;
Trx trx = p_vo.getTrxName() == null ? null : Trx.get(p_vo.getTrxName(), true);
if (trx != null) {
conn = trx.getConnection();
} else {
if (p_vo.getResultSetConcurrency() == ResultSet.CONCUR_UPDATABLE)
m_conn = DB.getConnectionRW();
else
m_conn = DB.getConnectionRO();
conn = m_conn;
}
if (conn == null)
throw new DBException("No Connection");
p_stmt = conn.prepareCall(p_vo.getSql(), p_vo.getResultSetType(), p_vo.getResultSetConcurrency());
return;
} catch (Exception e) {
log.log(Level.SEVERE, p_vo.getSql(), e);
throw new DBException(e);
}
}
use of org.adempiere.exceptions.DBException in project adempiere by adempiere.
the class MCost method calculateFiFo.
// calculateAveragePO
/**
* Calculate FiFo Cost
* @param product product
* @param M_AttributeSetInstance_ID asi
* @param as acct schema
* @param AD_Org_ID org
* @return costs or null
*/
public static BigDecimal calculateFiFo(MProduct product, int M_AttributeSetInstance_ID, MAcctSchema as, int AD_Org_ID) {
String sql = "SELECT t.MovementQty, mi.Qty, il.QtyInvoiced, il.PriceActual," + " i.C_Currency_ID, i.DateAcct, i.C_ConversionType_ID, i.AD_Client_ID, i.AD_Org_ID, t.M_Transaction_ID " + "FROM M_Transaction t" + " INNER JOIN M_MatchInv mi ON (t.M_InOutLine_ID=mi.M_InOutLine_ID)" + " INNER JOIN C_InvoiceLine il ON (mi.C_InvoiceLine_ID=il.C_InvoiceLine_ID)" + " INNER JOIN C_Invoice i ON (il.C_Invoice_ID=i.C_Invoice_ID) " + "WHERE t.M_Product_ID=?";
if (AD_Org_ID != 0)
sql += " AND t.AD_Org_ID=?";
else if (M_AttributeSetInstance_ID != 0)
sql += " AND t.M_AttributeSetInstance_ID=?";
sql += " ORDER BY t.M_Transaction_ID";
PreparedStatement pstmt = null;
ResultSet rs = null;
//
int oldTransaction_ID = 0;
ArrayList<QtyCost> fifo = new ArrayList<QtyCost>();
try {
pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, product.getM_Product_ID());
if (AD_Org_ID != 0)
pstmt.setInt(2, AD_Org_ID);
else if (M_AttributeSetInstance_ID != 0)
pstmt.setInt(2, M_AttributeSetInstance_ID);
rs = pstmt.executeQuery();
while (rs.next()) {
BigDecimal movementQty = rs.getBigDecimal(1);
int M_Transaction_ID = rs.getInt(10);
if (M_Transaction_ID == oldTransaction_ID)
// assuming same price for receipt
continue;
M_Transaction_ID = oldTransaction_ID;
//
BigDecimal matchQty = rs.getBigDecimal(2);
if (// out (negative)
matchQty == null) {
if (fifo.size() > 0) {
QtyCost pp = (QtyCost) fifo.get(0);
pp.Qty = pp.Qty.add(movementQty);
BigDecimal remainder = pp.Qty;
if (remainder.signum() == 0)
fifo.remove(0);
else {
while (remainder.signum() != 0) {
if (// Last
fifo.size() == 1) {
pp.Cost = Env.ZERO;
remainder = Env.ZERO;
} else {
fifo.remove(0);
pp = (QtyCost) fifo.get(0);
pp.Qty = pp.Qty.add(movementQty);
remainder = pp.Qty;
}
}
}
} else {
QtyCost pp = new QtyCost(movementQty, Env.ZERO);
fifo.add(pp);
}
s_log.finer("Movement=" + movementQty + ", Size=" + fifo.size());
continue;
}
// Assumption: everything is matched
BigDecimal price = rs.getBigDecimal(4);
int C_Currency_ID = rs.getInt(5);
Timestamp DateAcct = rs.getTimestamp(6);
int C_ConversionType_ID = rs.getInt(7);
int Client_ID = rs.getInt(8);
int Org_ID = rs.getInt(9);
BigDecimal cost = MConversionRate.convert(product.getCtx(), price, C_Currency_ID, as.getC_Currency_ID(), DateAcct, C_ConversionType_ID, Client_ID, Org_ID);
// Add Stock
boolean used = false;
if (fifo.size() == 1) {
QtyCost pp = (QtyCost) fifo.get(0);
if (pp.Qty.signum() < 0) {
pp.Qty = pp.Qty.add(movementQty);
if (pp.Qty.signum() == 0)
fifo.remove(0);
else
pp.Cost = cost;
used = true;
}
}
if (!used) {
QtyCost pp = new QtyCost(movementQty, cost);
fifo.add(pp);
}
s_log.finer("Movement=" + movementQty + ", Size=" + fifo.size());
}
} catch (SQLException e) {
throw new DBException(e, sql);
} finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
if (fifo.size() == 0) {
return null;
}
QtyCost pp = (QtyCost) fifo.get(0);
s_log.finer(product.getName() + " = " + pp.Cost);
return pp.Cost;
}
use of org.adempiere.exceptions.DBException in project adempiere by adempiere.
the class MCost method calculateLiFo.
// calculateFiFo
/**
* Calculate LiFo costs
* @param product product
* @param M_AttributeSetInstance_ID asi
* @param as acct schema
* @param AD_Org_ID org
* @return costs or null
*/
public static BigDecimal calculateLiFo(MProduct product, int M_AttributeSetInstance_ID, MAcctSchema as, int AD_Org_ID) {
String sql = "SELECT t.MovementQty, mi.Qty, il.QtyInvoiced, il.PriceActual," + " i.C_Currency_ID, i.DateAcct, i.C_ConversionType_ID, i.AD_Client_ID, i.AD_Org_ID, t.M_Transaction_ID " + "FROM M_Transaction t" + " INNER JOIN M_MatchInv mi ON (t.M_InOutLine_ID=mi.M_InOutLine_ID)" + " INNER JOIN C_InvoiceLine il ON (mi.C_InvoiceLine_ID=il.C_InvoiceLine_ID)" + " INNER JOIN C_Invoice i ON (il.C_Invoice_ID=i.C_Invoice_ID) " + "WHERE t.M_Product_ID=?";
if (AD_Org_ID != 0)
sql += " AND t.AD_Org_ID=?";
else if (M_AttributeSetInstance_ID != 0)
sql += " AND t.M_AttributeSetInstance_ID=?";
// Starting point?
sql += " ORDER BY t.M_Transaction_ID DESC";
PreparedStatement pstmt = null;
ResultSet rs = null;
//
int oldTransaction_ID = 0;
ArrayList<QtyCost> lifo = new ArrayList<QtyCost>();
try {
pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, product.getM_Product_ID());
if (AD_Org_ID != 0)
pstmt.setInt(2, AD_Org_ID);
else if (M_AttributeSetInstance_ID != 0)
pstmt.setInt(2, M_AttributeSetInstance_ID);
rs = pstmt.executeQuery();
while (rs.next()) {
BigDecimal movementQty = rs.getBigDecimal(1);
int M_Transaction_ID = rs.getInt(10);
if (M_Transaction_ID == oldTransaction_ID)
// assuming same price for receipt
continue;
M_Transaction_ID = oldTransaction_ID;
//
BigDecimal matchQty = rs.getBigDecimal(2);
if (// out (negative)
matchQty == null) {
if (lifo.size() > 0) {
QtyCost pp = (QtyCost) lifo.get(lifo.size() - 1);
pp.Qty = pp.Qty.add(movementQty);
BigDecimal remainder = pp.Qty;
if (remainder.signum() == 0)
lifo.remove(lifo.size() - 1);
else {
while (remainder.signum() != 0) {
if (// Last
lifo.size() == 1) {
pp.Cost = Env.ZERO;
remainder = Env.ZERO;
} else {
lifo.remove(lifo.size() - 1);
pp = (QtyCost) lifo.get(lifo.size() - 1);
pp.Qty = pp.Qty.add(movementQty);
remainder = pp.Qty;
}
}
}
} else {
QtyCost pp = new QtyCost(movementQty, Env.ZERO);
lifo.add(pp);
}
s_log.finer("Movement=" + movementQty + ", Size=" + lifo.size());
continue;
}
// Assumption: everything is matched
BigDecimal price = rs.getBigDecimal(4);
int C_Currency_ID = rs.getInt(5);
Timestamp DateAcct = rs.getTimestamp(6);
int C_ConversionType_ID = rs.getInt(7);
int Client_ID = rs.getInt(8);
int Org_ID = rs.getInt(9);
BigDecimal cost = MConversionRate.convert(product.getCtx(), price, C_Currency_ID, as.getC_Currency_ID(), DateAcct, C_ConversionType_ID, Client_ID, Org_ID);
//
QtyCost pp = new QtyCost(movementQty, cost);
lifo.add(pp);
s_log.finer("Movement=" + movementQty + ", Size=" + lifo.size());
}
} catch (SQLException e) {
throw new DBException(e, sql);
} finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
if (lifo.size() == 0) {
return null;
}
QtyCost pp = (QtyCost) lifo.get(lifo.size() - 1);
s_log.finer(product.getName() + " = " + pp.Cost);
return pp.Cost;
}
use of org.adempiere.exceptions.DBException in project adempiere by adempiere.
the class MCost method getLastPOPrice.
// getLastInvoicePrice
/**
* Get Last PO Price in currency
* @param product product
* @param M_ASI_ID attribute set instance
* @param AD_Org_ID org
* @param C_Currency_ID accounting currency
* @return last PO price in currency or null
*/
public static BigDecimal getLastPOPrice(MProduct product, int M_ASI_ID, int AD_Org_ID, int C_Currency_ID) {
BigDecimal retValue = null;
String sql = "SELECT currencyConvert(ol.PriceCost, o.C_Currency_ID, ?, o.DateAcct, o.C_ConversionType_ID, ol.AD_Client_ID, ol.AD_Org_ID)," + " currencyConvert(ol.PriceActual, o.C_Currency_ID, ?, o.DateAcct, o.C_ConversionType_ID, ol.AD_Client_ID, ol.AD_Org_ID) " + // ,ol.PriceCost,ol.PriceActual, ol.QtyOrdered, o.DateOrdered, ol.Line
"FROM C_OrderLine ol" + " INNER JOIN C_Order o ON (ol.C_Order_ID=o.C_Order_ID) " + "WHERE ol.M_Product_ID=?" + " AND o.IsSOTrx='N'";
if (AD_Org_ID != 0)
sql += " AND ol.AD_Org_ID=?";
else if (M_ASI_ID != 0)
sql += " AND ol.M_AttributeSetInstance_ID=?";
sql += " ORDER BY o.DateOrdered DESC, ol.Line DESC";
//
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sql, product.get_TrxName());
pstmt.setInt(1, C_Currency_ID);
pstmt.setInt(2, C_Currency_ID);
pstmt.setInt(3, product.getM_Product_ID());
if (AD_Org_ID != 0)
pstmt.setInt(4, AD_Org_ID);
else if (M_ASI_ID != 0)
pstmt.setInt(4, M_ASI_ID);
rs = pstmt.executeQuery();
if (rs.next()) {
retValue = rs.getBigDecimal(1);
if (retValue == null || retValue.signum() == 0)
retValue = rs.getBigDecimal(2);
}
} catch (SQLException e) {
throw new DBException(e, sql);
} finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
if (retValue != null) {
s_log.finer(product.getName() + " = " + retValue);
return retValue;
}
return null;
}
Aggregations