use of org.compiere.model.MProduct in project adempiere by adempiere.
the class WAttributeGrid method getGridElement.
// createGrid
/**
* Get Grid Element
* @param xValue X value
* @param yValue Y value
* @return Panel with Info
*/
private Panel getGridElement(MAttributeValue xValue, MAttributeValue yValue) {
Panel element = new Panel();
element.setStyle("border-width: thin; border-color: black;");
String sql = "SELECT * FROM M_Product WHERE IsActive='Y'";
// Product Attributes
if (xValue != null)
sql += " AND M_AttributeSetInstance_ID IN " + "(SELECT M_AttributeSetInstance_ID " + "FROM M_AttributeInstance " + "WHERE M_Attribute_ID=" + xValue.getM_Attribute_ID() + " AND M_AttributeValue_ID=" + xValue.getM_AttributeValue_ID() + ")";
if (yValue != null)
sql += " AND M_AttributeSetInstance_ID IN " + "(SELECT M_AttributeSetInstance_ID " + "FROM M_AttributeInstance " + "WHERE M_Attribute_ID=" + yValue.getM_Attribute_ID() + " AND M_AttributeValue_ID=" + yValue.getM_AttributeValue_ID() + ")";
sql = MRole.getDefault().addAccessSQL(sql, "M_Product", MRole.SQL_NOTQUALIFIED, MRole.SQL_RO);
PreparedStatement pstmt = null;
int noProducts = 0;
try {
pstmt = DB.prepareStatement(sql, null);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
MProduct product = new MProduct(Env.getCtx(), rs, null);
addProduct(element, product);
noProducts++;
}
rs.close();
pstmt.close();
pstmt = null;
} catch (Exception e) {
log.log(Level.SEVERE, sql, e);
}
try {
if (pstmt != null)
pstmt.close();
pstmt = null;
} catch (Exception e) {
pstmt = null;
}
int mode = modeCombo.getSelectedIndex();
// No Products
if (noProducts == 0 && mode == MODE_VIEW) {
// CButton button = ConfirmPanel.createNewButton(true);
// button.addActionListener(this);
// element.add(button);
} else // Additional Elements
{
if (mode == MODE_PRICE) {
// Price Field
} else if (mode == MODE_PO) {
// Qty Field
}
}
return element;
}
use of org.compiere.model.MProduct in project adempiere by adempiere.
the class WBOMDrop method addBOMLine.
// addBOMLines
/**
* Add BOM Line to this.
* Calls addBOMLines if added product is a BOM
* @param line BOM Line
* @param qty quantity
*/
private void addBOMLine(MProductBOM line, BigDecimal qty) {
log.fine(line.toString());
String bomType = line.getBOMType();
if (bomType == null)
bomType = MProductBOM.BOMTYPE_StandardPart;
//
BigDecimal lineQty = line.getBOMQty().multiply(qty);
MProduct product = line.getProduct();
if (product == null)
return;
if (product.isBOM() && product.isVerified())
// recursive
addBOMLines(product, lineQty);
else
addDisplay(line.getM_Product_ID(), product.getM_Product_ID(), bomType, product.getName(), lineQty);
}
use of org.compiere.model.MProduct in project adempiere by adempiere.
the class SB_InOutGenerateFromOrderLine method generate.
// doIt
/**
* Generate Shipments
* @param pstmt Order Query
* @return info
*/
private String generate(MOrder order) {
try {
if (!p_ConsolidateDocument || (m_shipment != null && (m_shipment.getC_BPartner_Location_ID() != order.getC_BPartner_Location_ID() || m_shipment.getM_Shipper_ID() != order.getM_Shipper_ID())))
completeShipment();
log.fine("check: " + order + " - DeliveryRule=" + order.getDeliveryRule());
//
Timestamp minGuaranteeDate = m_movementDate;
boolean completeOrder = MOrder.DELIVERYRULE_CompleteOrder.equals(order.getDeliveryRule());
// OrderLine WHERE
String where = "";
// Exclude Auto Delivery if not Force
if (!MOrder.DELIVERYRULE_Force.equals(order.getDeliveryRule()))
where += " AND (C_OrderLine.M_Product_ID IS NULL" + " OR EXISTS (SELECT * FROM M_Product p " + "WHERE C_OrderLine.M_Product_ID=p.M_Product_ID" + " AND IsExcludeAutoDelivery='N'))";
// Deadlock Prevention - Order by M_Product_ID
MOrderLine[] lines = order.getLines(where, "C_BPartner_Location_ID, M_Product_ID");
for (MOrderLine oLine : lines) {
if (!getSelectionKeys().contains(oLine.getC_OrderLine_ID()))
continue;
log.fine("check: " + oLine);
BigDecimal onHand = Env.ZERO;
BigDecimal toDeliver = getQtyToDeliver(oLine);
if (toDeliver.compareTo(Env.ZERO) == 0)
continue;
MProduct product = oLine.getProduct();
// Nothing to Deliver
if (product != null && toDeliver.signum() == 0)
continue;
// or it's a charge - Bug#: 1603966
if (oLine.getC_Charge_ID() != 0 && toDeliver.signum() == 0)
continue;
// Check / adjust for confirmations
BigDecimal unconfirmedShippedQty = Env.ZERO;
// Comments & lines w/o product & services
if ((product == null || !product.isStocked()) && (// comments
oLine.getQtyOrdered().signum() == 0 || // lines w/o product
toDeliver.signum() != 0)) {
if (// printed later
!MOrder.DELIVERYRULE_CompleteOrder.equals(order.getDeliveryRule()))
createLine(order, oLine, toDeliver, null, false);
continue;
}
// Stored Product
String MMPolicy = product.getMMPolicy();
MStorage[] storages = getStorages(oLine.getM_Warehouse_ID(), oLine.getM_Product_ID(), oLine.getM_AttributeSetInstance_ID(), minGuaranteeDate, MClient.MMPOLICY_FiFo.equals(MMPolicy));
for (int j = 0; j < storages.length; j++) {
MStorage storage = storages[j];
onHand = onHand.add(storage.getQtyOnHand());
}
boolean fullLine = onHand.compareTo(toDeliver) >= 0 || toDeliver.signum() < 0;
// Complete Order
if (completeOrder && !fullLine) {
log.fine("Failed CompleteOrder - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + "), ToDeliver=" + toDeliver + " - " + oLine);
completeOrder = false;
break;
} else // Complete Line
if (fullLine && MOrder.DELIVERYRULE_CompleteLine.equals(order.getDeliveryRule())) {
log.fine("CompleteLine - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + ", ToDeliver=" + toDeliver + " - " + oLine);
//
createLine(order, oLine, toDeliver, storages, false);
} else // Availability
if (MOrder.DELIVERYRULE_Availability.equals(order.getDeliveryRule()) && (onHand.signum() > 0 || toDeliver.signum() < 0)) {
BigDecimal deliver = toDeliver;
if (deliver.compareTo(onHand) > 0)
deliver = onHand;
log.fine("Available - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + "), ToDeliver=" + toDeliver + ", Delivering=" + deliver + " - " + oLine);
//
createLine(order, oLine, deliver, storages, false);
} else // Force
if (MOrder.DELIVERYRULE_Force.equals(order.getDeliveryRule())) {
BigDecimal deliver = toDeliver;
log.fine("Force - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + "), ToDeliver=" + toDeliver + ", Delivering=" + deliver + " - " + oLine);
//
createLine(order, oLine, deliver, storages, true);
} else // Manual
if (MOrder.DELIVERYRULE_Manual.equals(order.getDeliveryRule()))
log.fine("Manual - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + ") - " + oLine);
else
log.fine("Failed: " + order.getDeliveryRule() + " - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + "), ToDeliver=" + toDeliver + " - " + oLine);
}
// Complete Order successful
if (completeOrder && MOrder.DELIVERYRULE_CompleteOrder.equals(order.getDeliveryRule())) {
for (MOrderLine oLine : lines) {
MProduct product = oLine.getProduct();
BigDecimal toDeliver = oLine.getQtyOrdered().subtract(oLine.getQtyDelivered());
//
MStorage[] storages = null;
if (product != null && product.isStocked()) {
String MMPolicy = product.getMMPolicy();
storages = getStorages(oLine.getM_Warehouse_ID(), oLine.getM_Product_ID(), oLine.getM_AttributeSetInstance_ID(), minGuaranteeDate, MClient.MMPOLICY_FiFo.equals(MMPolicy));
}
//
createLine(order, oLine, toDeliver, storages, false);
}
}
m_line += 1000;
} catch (Exception e) {
log.log(Level.SEVERE, m_sql, e);
}
completeShipment();
return "@Created@ = " + m_created;
}
use of org.compiere.model.MProduct in project adempiere by adempiere.
the class ExpenseTypesFromAccounts method doIt.
@Override
protected String doIt() throws Exception {
// Fetch price list
MPriceList priceList = new MPriceList(getCtx(), m_priceListId, get_TrxName());
// Get current client id from price list since I for some reason can't read it from
// context.
m_clientId = priceList.getAD_Client_ID();
// Get active price list version
MPriceListVersion pv = priceList.getPriceListVersion(null);
if (pv == null)
throw new Exception("Pricelist " + priceList.getName() + " has no default version.");
MProduct product;
// Read all existing applicable products into memory for quick comparison.
List<MProduct> products = new Query(getCtx(), I_M_Product.Table_Name, "ProductType=?", get_TrxName()).setParameters(MProduct.PRODUCTTYPE_ExpenseType).list();
Map<String, MProduct> productMap = new TreeMap<String, MProduct>();
for (Iterator<MProduct> it = products.iterator(); it.hasNext(); ) {
product = it.next();
productMap.put(product.getValue(), product);
}
// Read all existing valid combinations comparison
MAccount validComb;
List<MAccount> validCombs = new Query(getCtx(), I_C_ValidCombination.Table_Name, "C_AcctSchema_ID=? and AD_Client_ID=? and AD_Org_ID=0", get_TrxName()).setParameters(m_acctSchemaId, m_clientId).list();
Map<Integer, MAccount> validCombMap = new TreeMap<Integer, MAccount>();
for (Iterator<MAccount> it = validCombs.iterator(); it.hasNext(); ) {
validComb = it.next();
validCombMap.put(validComb.getAccount_ID(), validComb);
}
// Read all accounttypes that fit the given criteria.
List<MElementValue> result = new Query(getCtx(), I_C_ElementValue.Table_Name, "AccountType=? and isSummary='N' and Value>=? and Value<=? and AD_Client_ID=?", get_TrxName()).setParameters(MElementValue.ACCOUNTTYPE_Expense, m_startElement, m_endElement, m_clientId).list();
MElementValue elem;
MProductPrice priceRec;
X_M_Product_Acct productAcct;
String expenseItemValue;
BigDecimal zero = Env.ZERO;
int addCount = 0;
int skipCount = 0;
for (Iterator<MElementValue> it = result.iterator(); it.hasNext(); ) {
elem = it.next();
expenseItemValue = m_productValuePrefix + elem.getValue() + m_productValueSuffix;
// See if a product with this key already exists
product = productMap.get(expenseItemValue);
if (product == null) {
// Create a new product from the account element
product = new MProduct(getCtx(), 0, get_TrxName());
product.set_ValueOfColumn("AD_Client_ID", Integer.valueOf(m_clientId));
product.setValue(expenseItemValue);
product.setName(elem.getName());
product.setDescription(elem.getDescription());
product.setIsActive(true);
product.setProductType(MProduct.PRODUCTTYPE_ExpenseType);
product.setM_Product_Category_ID(m_productCategoryId);
product.setC_UOM_ID(m_uomId);
product.setC_TaxCategory_ID(m_taxCategoryId);
product.setIsStocked(false);
product.setIsPurchased(true);
product.setIsSold(false);
// Save the product
product.saveEx(get_TrxName());
// Add a zero product price to the price list so it shows up in the price list
priceRec = new MProductPrice(getCtx(), pv.get_ID(), product.get_ID(), get_TrxName());
priceRec.set_ValueOfColumn("AD_Client_ID", Integer.valueOf(m_clientId));
priceRec.setPrices(zero, zero, zero);
priceRec.saveEx(get_TrxName());
// Set the revenue and expense accounting of the product to the given account element
// Get the valid combination
validComb = validCombMap.get(elem.getC_ElementValue_ID());
if (validComb == null) {
// Create new valid combination
validComb = new MAccount(getCtx(), 0, get_TrxName());
validComb.set_ValueOfColumn("AD_Client_ID", Integer.valueOf(m_clientId));
validComb.setAD_Org_ID(0);
validComb.setAlias(elem.getValue());
validComb.setAccount_ID(elem.get_ID());
validComb.setC_AcctSchema_ID(m_acctSchemaId);
validComb.saveEx(get_TrxName());
}
// TODO: It might be needed to make the accounting more specific, but the purpose
// of the process now is to create general accounts so this is intentional.
productAcct = new Query(getCtx(), I_M_Product_Acct.Table_Name, "M_Product_ID=? and C_AcctSchema_ID=?", get_TrxName()).setParameters(product.get_ID(), m_acctSchemaId).first();
productAcct.setP_Expense_Acct(validComb.get_ID());
productAcct.setP_Revenue_Acct(validComb.get_ID());
productAcct.saveEx(get_TrxName());
addCount++;
} else {
skipCount++;
}
}
String returnStr = addCount + " products added.";
if (skipCount > 0)
returnStr += " " + skipCount + " products skipped.";
return (returnStr);
}
use of org.compiere.model.MProduct in project adempiere by adempiere.
the class CostUpdate method getCosts.
// update
/**
* Get Costs
* @param cost cost
* @param to where to get costs from
* @return costs (could be 0) or null if not found
* @throws Exception
*/
private BigDecimal getCosts(MCost cost, String to) throws Exception {
BigDecimal retValue = null;
MCostElement costElement = MCostElement.getByMaterialCostElementType(cost);
if (costElement == null)
throw new AdempiereSystemError("@M_CostElement_ID@ @NotFound@: ");
// Average Invoice
if (to.equals(TO_AverageInvoice)) {
if (costElement == null)
throw new AdempiereSystemError("CostElement not found: " + TO_AverageInvoice);
MCost costDimension = getDimension(cost, costElement.getM_CostElement_ID());
if (costDimension != null)
retValue = costDimension.getCurrentCostPrice();
} else // Average Invoice History
if (to.equals(TO_AverageInvoiceHistory)) {
if (costElement == null)
throw new AdempiereSystemError("CostElement not found: " + TO_AverageInvoice);
MCost costDimension = getDimension(cost, costElement.getM_CostElement_ID());
if (costDimension != null)
retValue = costDimension.getHistoryAverage();
} else // Average PO
if (to.equals(TO_AveragePO)) {
if (costElement == null)
throw new AdempiereSystemError("CostElement not found: " + TO_AveragePO);
MCost costDimension = getDimension(cost, costElement.getM_CostElement_ID());
if (costDimension != null)
retValue = costDimension.getCurrentCostPrice();
} else // Average PO History
if (to.equals(TO_AveragePOHistory)) {
if (costElement == null)
throw new AdempiereSystemError("CostElement not found: " + TO_AveragePO);
MCost costDimension = getDimension(cost, costElement.getM_CostElement_ID());
if (costDimension != null)
retValue = costDimension.getHistoryAverage();
} else // FiFo
if (to.equals(TO_FiFo)) {
if (costElement == null)
throw new AdempiereSystemError("CostElement not found: " + TO_FiFo);
MCost costDimension = getDimension(cost, costElement.getM_CostElement_ID());
if (costDimension != null)
retValue = costDimension.getCurrentCostPrice();
} else // Future Std Costs
if (to.equals(TO_FutureStandardCost))
retValue = cost.getFutureCostPrice();
else // Last Inv Price
if (to.equals(TO_LastInvoicePrice)) {
if (costElement != null) {
MCost costDimension = getDimension(cost, costElement.getM_CostElement_ID());
if (costDimension != null)
retValue = costDimension.getCurrentCostPrice();
}
if (retValue == null) {
MProduct product = MProduct.get(getCtx(), cost.getM_Product_ID());
MAcctSchema as = MAcctSchema.get(getCtx(), cost.getC_AcctSchema_ID());
retValue = MCost.getLastInvoicePrice(product, cost.getM_AttributeSetInstance_ID(), cost.getAD_Org_ID(), as.getC_Currency_ID());
}
} else // Last PO Price
if (to.equals(TO_LastPOPrice)) {
if (costElement != null) {
MCost costDimension = getDimension(cost, costElement.getM_CostElement_ID());
if (costDimension != null)
retValue = costDimension.getCurrentCostPrice();
}
if (retValue == null) {
MProduct product = MProduct.get(getCtx(), cost.getM_Product_ID());
MAcctSchema as = MAcctSchema.get(getCtx(), cost.getC_AcctSchema_ID());
retValue = MCost.getLastPOPrice(product, cost.getM_AttributeSetInstance_ID(), cost.getAD_Org_ID(), as.getC_Currency_ID());
}
} else // FiFo
if (to.equals(TO_LiFo)) {
if (costElement == null)
throw new AdempiereSystemError("CostElement not found: " + TO_LiFo);
MCost costDimension = getDimension(cost, costElement.getM_CostElement_ID());
if (costDimension != null)
retValue = costDimension.getCurrentCostPrice();
} else // Old Std Costs
if (to.equals(TO_OldStandardCost))
retValue = getOldCurrentCostPrice(cost);
else // Price List
if (to.equals(TO_PriceListLimit))
retValue = getPrice(cost);
else // Standard Costs
if (to.equals(TO_StandardCost))
retValue = cost.getCurrentCostPrice();
return retValue;
}
Aggregations