use of org.compiere.model.MProduct in project adempiere by adempiere.
the class StorageEngine method checkMaterialPolicy.
private static void checkMaterialPolicy(IDocumentLine line, String MovementType, Timestamp MovementDate, int M_Warehouse_ID) {
deleteMA(line);
// Incoming Trx
// V+ Vendor Receipt
boolean incomingTrx = MovementType.charAt(1) == '+';
MProduct product = MProduct.get(line.getCtx(), line.getM_Product_ID());
// Need to have Location
if (line.getM_Locator_ID() == 0) {
//MWarehouse w = MWarehouse.get(getCtx(), getM_Warehouse_ID());
//line.setM_Warehouse_ID(M_Warehouse_ID);
//line.setM_Locator_ID(getM_Locator_ID(line.getCtx(),line.getM_Warehouse_ID(), line.getM_Product_ID(),line.getM_AttributeSetInstance_ID(), incomingTrx ? Env.ZERO : line.getMovementQty(), line.get_TrxName()));
}
// Create an Attribute Set Instance to any receipt FIFO/LIFO
if (line.getM_AttributeSetInstance_ID() == 0) {
//Validate Transaction
if (incomingTrx) {
MAttributeSetInstance asi = null;
//auto balance negative on hand
MStorage[] storages = MStorage.getWarehouse(line.getCtx(), M_Warehouse_ID, line.getM_Product_ID(), 0, null, MClient.MMPOLICY_FiFo.equals(product.getMMPolicy()), false, line.getM_Locator_ID(), line.get_TrxName());
for (MStorage storage : storages) {
if (storage.getQtyOnHand().signum() < 0) {
asi = new MAttributeSetInstance(line.getCtx(), storage.getM_AttributeSetInstance_ID(), line.get_TrxName());
break;
}
}
//always create asi so fifo/lifo work.
if (asi == null) {
asi = MAttributeSetInstance.create(line.getCtx(), product, line.get_TrxName());
}
line.setM_AttributeSetInstance_ID(asi.getM_AttributeSetInstance_ID());
log.config("New ASI=" + line);
createMA(line, line.getM_AttributeSetInstance_ID(), line.getMovementQty());
} else // Create consume the Attribute Set Instance using policy FIFO/LIFO
{
String MMPolicy = product.getMMPolicy();
Timestamp minGuaranteeDate = MovementDate;
MStorage[] storages = MStorage.getWarehouse(line.getCtx(), M_Warehouse_ID, line.getM_Product_ID(), line.getM_AttributeSetInstance_ID(), minGuaranteeDate, MClient.MMPOLICY_FiFo.equals(MMPolicy), true, line.getM_Locator_ID(), line.get_TrxName());
BigDecimal qtyToDeliver = line.getMovementQty();
for (MStorage storage : storages) {
if (storage.getQtyOnHand().compareTo(qtyToDeliver) >= 0) {
createMA(line, storage.getM_AttributeSetInstance_ID(), qtyToDeliver);
qtyToDeliver = Env.ZERO;
} else {
createMA(line, storage.getM_AttributeSetInstance_ID(), storage.getQtyOnHand());
qtyToDeliver = qtyToDeliver.subtract(storage.getQtyOnHand());
log.fine("QtyToDeliver=" + qtyToDeliver);
}
if (qtyToDeliver.signum() == 0)
break;
}
if (qtyToDeliver.signum() != 0) {
//deliver using new asi
MAttributeSetInstance asi = MAttributeSetInstance.create(line.getCtx(), product, line.get_TrxName());
createMA(line, asi.getM_AttributeSetInstance_ID(), qtyToDeliver);
}
}
// outgoing Trx
} else {
if (incomingTrx) {
;
} else {
createMA(line, line.getM_AttributeSetInstance_ID(), line.getMovementQty());
}
}
save(line);
}
use of org.compiere.model.MProduct in project adempiere by adempiere.
the class FifoLifoCostingMethod method calculate.
public void calculate() {
ProductCost pc = new ProductCost(model.getCtx(), model.getM_Product_ID(), model.getM_AttributeSetInstance_ID(), model.get_TrxName());
pc.setQty(transaction.getMovementQty());
//
List<CostComponent> ccs = pc.getProductCostsLayers(dimension, 0, false);
if (ccs == null || ccs.size() == 0) {
MProduct product = MProduct.get(Env.getCtx(), model.getM_Product_ID());
throw new AdempiereException("No Costs for " + product.getName());
}
m_calculatedCosts = ccs;
}
use of org.compiere.model.MProduct in project adempiere by adempiere.
the class SB_InOutGenerateFromOrderLine method createLine.
// generate
/**************************************************************************
* Create Line
* @param order order
* @param orderLine line
* @param qty qty
* @param storages storage info
* @param force force delivery
*/
private void createLine(MOrder order, MOrderLine orderLine, BigDecimal qty, MStorage[] storages, boolean force) {
// Complete last Shipment - can have multiple shipments
if (m_lastC_BPartner_Location_ID != orderLine.getC_BPartner_Location_ID())
completeShipment();
m_lastC_BPartner_Location_ID = orderLine.getC_BPartner_Location_ID();
// Create New Shipment
if (m_shipment == null) {
m_shipment = new MInOut(order, 0, m_movementDate);
if (p_C_DocType_ID != 0)
m_shipment.setC_DocType_ID(p_C_DocType_ID);
// sets Org too
m_shipment.setM_Warehouse_ID(orderLine.getM_Warehouse_ID());
if (order.getC_BPartner_ID() != orderLine.getC_BPartner_ID())
m_shipment.setC_BPartner_ID(orderLine.getC_BPartner_ID());
if (order.getC_BPartner_Location_ID() != orderLine.getC_BPartner_Location_ID())
m_shipment.setC_BPartner_Location_ID(orderLine.getC_BPartner_Location_ID());
if (!m_shipment.save())
throw new IllegalStateException("Could not create Shipment");
}
// Non Inventory Lines
if (storages == null) {
MInOutLine line = new MInOutLine(m_shipment);
line.setOrderLine(orderLine, 0, Env.ZERO);
// Correct UOM
line.setQty(qty);
if (orderLine.getQtyEntered().compareTo(orderLine.getQtyOrdered()) != 0)
line.setQtyEntered(qty.multiply(orderLine.getQtyEntered()).divide(orderLine.getQtyOrdered(), 12, BigDecimal.ROUND_HALF_UP));
line.setLine(m_line + orderLine.getLine());
if (!line.save())
throw new IllegalStateException("Could not create Shipment Line");
log.fine(line.toString());
return;
}
// Inventory Lines
ArrayList<MInOutLine> list = new ArrayList<MInOutLine>();
MProduct product = (MProduct) orderLine.getM_Product();
BigDecimal toDeliver = qty;
for (int i = 0; i < storages.length; i++) {
MStorage storage = storages[i];
BigDecimal deliver = toDeliver;
//skip negative storage record
if (storage.getQtyOnHand().signum() < 0)
continue;
// Not enough On Hand
if (deliver.compareTo(storage.getQtyOnHand()) > 0 && // positive storage
storage.getQtyOnHand().signum() >= 0) {
if (// Adjust to OnHand Qty
!force || // if force not on last location
(force && i + 1 != storages.length))
deliver = storage.getQtyOnHand();
}
if (// zero deliver
deliver.signum() == 0)
continue;
int M_Locator_ID = storage.getM_Locator_ID();
//
MInOutLine line = null;
if (// find line with Locator
orderLine.getM_AttributeSetInstance_ID() == 0) {
for (int ll = 0; ll < list.size(); ll++) {
MInOutLine test = (MInOutLine) list.get(ll);
if (test.getM_Locator_ID() == M_Locator_ID && test.getM_AttributeSetInstance_ID() == 0) {
line = test;
break;
}
}
}
if (// new line
line == null) {
line = new MInOutLine(m_shipment);
line.setOrderLine(orderLine, M_Locator_ID, order.isSOTrx() ? deliver : Env.ZERO);
line.setQty(deliver);
if (product != null && product.isASIMandatory(order.isSOTrx(), line.getAD_Org_ID()))
line.setM_AttributeSetInstance_ID(storage.getM_AttributeSetInstance_ID());
list.add(line);
} else
// existing line
line.setQty(line.getMovementQty().add(deliver));
if (orderLine.getQtyEntered().compareTo(orderLine.getQtyOrdered()) != 0)
line.setQtyEntered(line.getMovementQty().multiply(orderLine.getQtyEntered()).divide(orderLine.getQtyOrdered(), 12, BigDecimal.ROUND_HALF_UP));
line.setLine(m_line + orderLine.getLine());
if (!line.save())
throw new IllegalStateException("Could not create Shipment Line");
log.fine("ToDeliver=" + qty + "/" + deliver + " - " + line);
toDeliver = toDeliver.subtract(deliver);
// Temp adjustment, actual update happen in MInOut.completeIt
storage.setQtyOnHand(storage.getQtyOnHand().subtract(deliver));
//
if (toDeliver.signum() == 0)
break;
}
if (toDeliver.signum() != 0) {
if (!force) {
throw new IllegalStateException("Not All Delivered - Remainder=" + toDeliver);
} else {
MInOutLine line = new MInOutLine(m_shipment);
line.setOrderLine(orderLine, 0, order.isSOTrx() ? toDeliver : Env.ZERO);
line.setQty(toDeliver);
if (!line.save())
throw new IllegalStateException("Could not create Shipment Line");
}
}
}
use of org.compiere.model.MProduct in project adempiere by adempiere.
the class Doc_ProjectIssue method createFacts.
// getBalance
/**
* Create Facts (the accounting logic) for
* PJI
* <pre>
* Issue
* ProjectWIP DR
* Inventory CR
* </pre>
* Project Account is either Asset or WIP depending on Project Type
* @param as accounting schema
* @return Fact
*/
public ArrayList<Fact> createFacts(MAcctSchema as) {
// create Fact Header
Fact fact = new Fact(this, as, Fact.POST_Actual);
setC_Currency_ID(as.getC_Currency_ID());
MProject project = new MProject(getCtx(), m_issue.getC_Project_ID(), getTrxName());
String ProjectCategory = project.getProjectCategory();
MProduct product = MProduct.get(getCtx(), m_issue.getM_Product_ID());
// Line pointers
FactLine dr = null;
FactLine cr = null;
// Issue Cost
BigDecimal costs = null;
BigDecimal total = Env.ZERO;
if (m_issue.getM_InOutLine_ID() != 0)
costs = getPOCost(as);
else if (m_issue.getS_TimeExpenseLine_ID() != 0)
costs = getLaborCost(as);
if (// standard Product Costs
costs == null) {
for (MCostDetail cost : m_line.getCostDetail(as, false)) {
if (!MCostDetail.existsCost(cost))
continue;
costs = MCostDetail.getTotalCost(cost, as);
total = total.add(costs);
}
}
if (total == null || total.signum() == 0) {
p_Error = "Resubmit - No Costs for " + product.getName();
log.log(Level.WARNING, p_Error);
return null;
}
// Project DR
int acctType = ACCTTYPE_ProjectWIP;
if (MProject.PROJECTCATEGORY_AssetProject.equals(ProjectCategory))
acctType = ACCTTYPE_ProjectAsset;
dr = fact.createLine(m_line, getAccount(acctType, as), as.getC_Currency_ID(), costs, null);
dr.setQty(m_line.getQty().negate());
// Inventory CR
acctType = ProductCost.ACCTTYPE_P_Asset;
if (product.isService())
acctType = ProductCost.ACCTTYPE_P_Expense;
cr = fact.createLine(m_line, m_line.getAccount(acctType, as), as.getC_Currency_ID(), null, costs);
cr.setM_Locator_ID(m_line.getM_Locator_ID());
// from Loc
cr.setLocationFromLocator(m_line.getM_Locator_ID(), true);
//
ArrayList<Fact> facts = new ArrayList<Fact>();
facts.add(fact);
return facts;
}
use of org.compiere.model.MProduct in project adempiere by adempiere.
the class ImportInventory method doIt.
// prepare
/**
* Perform process.
* @return Message
* @throws Exception
*/
protected String doIt() throws java.lang.Exception {
log.info("M_Locator_ID=" + p_M_Locator_ID + ",MovementDate=" + p_MovementDate);
if (p_UpdateCosting) {
if (p_C_AcctSchema_ID <= 0) {
throw new IllegalArgumentException("Accounting Schema required!");
}
if (p_M_CostType_ID <= 0) {
throw new IllegalArgumentException("Cost Type required!");
}
if (p_M_CostElement_ID <= 0) {
throw new IllegalArgumentException("Cost Element required!");
}
if (p_AD_OrgTrx_ID < 0) {
throw new IllegalArgumentException("AD_OrgTrx required!");
}
acctSchema = MAcctSchema.get(getCtx(), p_C_AcctSchema_ID, get_TrxName());
}
StringBuffer sql = null;
int no = 0;
String clientCheck = " AND AD_Client_ID=" + p_AD_Client_ID;
// Delete Old Imported
if (p_DeleteOldImported) {
sql = new StringBuffer("DELETE I_Inventory " + "WHERE I_IsImported='Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.fine("Delete Old Imported=" + no);
}
// Set Client, Org, Location, IsActive, Created/Updated
sql = new StringBuffer("UPDATE I_Inventory " + "SET AD_Client_ID = COALESCE (AD_Client_ID,").append(p_AD_Client_ID).append(")," + " AD_Org_ID = COALESCE (AD_Org_ID,").append(p_AD_Org_ID).append("),");
if (p_MovementDate != null)
sql.append(" MovementDate = COALESCE (MovementDate,").append(DB.TO_DATE(p_MovementDate)).append("),");
sql.append(" IsActive = COALESCE (IsActive, 'Y')," + " Created = COALESCE (Created, SysDate)," + " CreatedBy = COALESCE (CreatedBy, 0)," + " Updated = COALESCE (Updated, SysDate)," + " UpdatedBy = COALESCE (UpdatedBy, 0)," + " I_ErrorMsg = ' '," + // reset
" M_Warehouse_ID = NULL," + " I_IsImported = 'N' " + "WHERE I_IsImported<>'Y' OR I_IsImported IS NULL");
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.info("Reset=" + no);
sql = new StringBuffer("UPDATE I_Inventory o " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid Org, '" + "WHERE (AD_Org_ID IS NULL OR AD_Org_ID=0" + " OR EXISTS (SELECT * FROM AD_Org oo WHERE o.AD_Org_ID=oo.AD_Org_ID AND (oo.IsSummary='Y' OR oo.IsActive='N')))" + " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
if (no != 0)
log.warning("Invalid Org=" + no);
// Location
sql = new StringBuffer("UPDATE I_Inventory i " + "SET M_Locator_ID=(SELECT MAX(M_Locator_ID) FROM M_Locator l" + " WHERE i.LocatorValue=l.Value AND i.AD_Client_ID=l.AD_Client_ID) " + "WHERE M_Locator_ID IS NULL AND LocatorValue IS NOT NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.fine("Set Locator from Value =" + no);
sql = new StringBuffer("UPDATE I_Inventory i " + "SET M_Locator_ID=(SELECT MAX(M_Locator_ID) FROM M_Locator l" + " WHERE i.X=l.X AND i.Y=l.Y AND i.Z=l.Z AND i.AD_Client_ID=l.AD_Client_ID) " + "WHERE M_Locator_ID IS NULL AND X IS NOT NULL AND Y IS NOT NULL AND Z IS NOT NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.fine("Set Locator from X,Y,Z =" + no);
if (p_M_Locator_ID != 0) {
sql = new StringBuffer("UPDATE I_Inventory " + "SET M_Locator_ID = ").append(p_M_Locator_ID).append(" WHERE M_Locator_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.fine("Set Locator from Parameter=" + no);
}
sql = new StringBuffer("UPDATE I_Inventory " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=No Location, ' " + "WHERE M_Locator_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
if (no != 0)
log.warning("No Location=" + no);
// Set M_Warehouse_ID
sql = new StringBuffer("UPDATE I_Inventory i " + "SET M_Warehouse_ID=(SELECT M_Warehouse_ID FROM M_Locator l WHERE i.M_Locator_ID=l.M_Locator_ID) " + "WHERE M_Locator_ID IS NOT NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.fine("Set Warehouse from Locator =" + no);
sql = new StringBuffer("UPDATE I_Inventory " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=No Warehouse, ' " + "WHERE M_Warehouse_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
if (no != 0)
log.warning("No Warehouse=" + no);
// Product
sql = new StringBuffer("UPDATE I_Inventory i " + "SET M_Product_ID=(SELECT MAX(M_Product_ID) FROM M_Product p" + " WHERE i.Value=p.Value AND i.AD_Client_ID=p.AD_Client_ID) " + "WHERE M_Product_ID IS NULL AND Value IS NOT NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.fine("Set Product from Value=" + no);
sql = new StringBuffer("UPDATE I_Inventory i " + "SET M_Product_ID=(SELECT MAX(M_Product_ID) FROM M_Product p" + " WHERE i.UPC=p.UPC AND i.AD_Client_ID=p.AD_Client_ID) " + "WHERE M_Product_ID IS NULL AND UPC IS NOT NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.fine("Set Product from UPC=" + no);
sql = new StringBuffer("UPDATE I_Inventory " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=No Product, ' " + "WHERE M_Product_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
if (no != 0)
log.warning("No Product=" + no);
// No QtyCount
sql = new StringBuffer("UPDATE I_Inventory " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=No Qty Count, ' " + "WHERE QtyCount IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
if (no != 0)
log.warning("No QtyCount=" + no);
commitEx();
/*********************************************************************/
MInventory inventory = null;
int noInsert = 0;
int noInsertLine = 0;
// Go through Inventory Records
sql = new StringBuffer("SELECT * FROM I_Inventory " + "WHERE I_IsImported='N'").append(clientCheck).append(" ORDER BY M_Warehouse_ID, TRUNC(MovementDate), I_Inventory_ID");
try {
PreparedStatement preparedStatement = DB.prepareStatement(sql.toString(), get_TrxName());
ResultSet resultSet = preparedStatement.executeQuery();
//
int warehouseId = -1;
Timestamp lastMovementDate = null;
while (resultSet.next()) {
X_I_Inventory importInventory = new X_I_Inventory(getCtx(), resultSet, get_TrxName());
Timestamp movementDate = TimeUtil.getDay(importInventory.getMovementDate());
if (inventory == null || importInventory.getM_Warehouse_ID() != warehouseId || !movementDate.equals(lastMovementDate)) {
inventory = new MInventory(getCtx(), 0, get_TrxName());
inventory.setClientOrg(importInventory.getAD_Client_ID(), importInventory.getAD_Org_ID());
inventory.setDescription("I " + importInventory.getM_Warehouse_ID() + " " + movementDate);
inventory.setM_Warehouse_ID(importInventory.getM_Warehouse_ID());
inventory.setMovementDate(movementDate);
//
if (!inventory.save()) {
log.log(Level.SEVERE, "Inventory not saved");
break;
}
warehouseId = importInventory.getM_Warehouse_ID();
lastMovementDate = movementDate;
noInsert++;
}
// Line
int attributeSetInstanceId = 0;
if (importInventory.getLot() != null || importInventory.getSerNo() != null) {
MProduct product = MProduct.get(getCtx(), importInventory.getM_Product_ID());
if (product.isInstanceAttribute()) {
MAttributeSet attributeSet = product.getAttributeSet();
MAttributeSetInstance attributeSetInstance = new MAttributeSetInstance(getCtx(), 0, attributeSet.getM_AttributeSet_ID(), get_TrxName());
if (attributeSet.isLot() && importInventory.getLot() != null)
attributeSetInstance.setLot(importInventory.getLot(), importInventory.getM_Product_ID());
if (attributeSet.isSerNo() && importInventory.getSerNo() != null)
attributeSetInstance.setSerNo(importInventory.getSerNo());
attributeSetInstance.setDescription();
attributeSetInstance.saveEx();
attributeSetInstanceId = attributeSetInstance.getM_AttributeSetInstance_ID();
}
}
MInventoryLine inventoryLine = new MInventoryLine(inventory, importInventory.getM_Locator_ID(), importInventory.getM_Product_ID(), attributeSetInstanceId, importInventory.getQtyBook(), importInventory.getQtyCount());
inventoryLine.saveEx();
importInventory.setI_IsImported(true);
importInventory.setM_Inventory_ID(inventoryLine.getM_Inventory_ID());
importInventory.setM_InventoryLine_ID(inventoryLine.getM_InventoryLine_ID());
importInventory.setProcessed(true);
importInventory.saveEx();
noInsertLine++;
//@Trifon update Product cost record if Update costing is enabled
if (p_UpdateCosting) {
inventoryLine.setCurrentCostPrice(importInventory.getCurrentCostPrice());
inventoryLine.setCurrentCostPriceLL(importInventory.getCurrentCostPriceLL());
inventoryLine.saveEx();
}
}
resultSet.close();
preparedStatement.close();
} catch (Exception e) {
log.log(Level.SEVERE, sql.toString(), e);
}
// Set Error to indicator to not imported
sql = new StringBuffer("UPDATE I_Inventory " + "SET I_IsImported='N', Updated=SysDate " + "WHERE I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
addLog(0, null, new BigDecimal(no), "@Errors@");
//
addLog(0, null, new BigDecimal(noInsert), "@M_Inventory_ID@: @Inserted@");
addLog(0, null, new BigDecimal(noInsertLine), "@M_InventoryLine_ID@: @Inserted@");
return "";
}
Aggregations