use of org.compiere.model.MInventoryLine in project adempiere by adempiere.
the class CostResult method createPhisicalInventory.
public MInventory createPhisicalInventory(Timestamp documentDate, BigDecimal qty, BigDecimal qtyBook) {
int M_Locator_ID = 101;
MInventory inventory = new MInventory(getCtx(), 0, trxName);
inventory.setAD_Org_ID(Env.getAD_Org_ID(getCtx()));
inventory.setM_Warehouse_ID(w.getM_Warehouse_ID());
inventory.setC_DocType_ID((MDocType.getDocType(MDocType.DOCBASETYPE_MaterialPhysicalInventory)));
inventory.setMovementDate(documentDate);
inventory.saveEx();
MInventoryLine inventoryLine = new MInventoryLine(getCtx(), 0, trxName);
inventoryLine.setAD_Org_ID(Env.getAD_Org_ID(getCtx()));
inventoryLine.setM_Inventory_ID(inventory.getM_Inventory_ID());
inventoryLine.setM_Product_ID(product.getM_Product_ID());
inventoryLine.setQtyBook(qtyBook);
inventoryLine.setInventoryType(MInventoryLine.INVENTORYTYPE_InventoryDifference);
inventoryLine.setQtyCount(qty);
// Default HQ Locator
inventoryLine.setM_Locator_ID(M_Locator_ID);
inventoryLine.saveEx();
inventory.processIt(DocAction.ACTION_Complete);
inventory.saveEx();
return inventory;
}
use of org.compiere.model.MInventoryLine in project adempiere by adempiere.
the class InventoryUtil method createInventory.
public static MInventory createInventory(MMDocument doc, String trxName) {
Properties ctx = Env.getCtx();
int AD_Org_ID = getFirst_Org_ID();
MProduct product = getCreateProduct(doc.ProductValue, null);
MLocator locator = getCreateLocator(AD_Org_ID, doc.LocatorValue, doc.LocatorValue);
//
MInventory inv = new MInventory(ctx, 0, trxName);
inv.setAD_Org_ID(AD_Org_ID);
//inv.setIsInternalUseInventory(true);
inv.setMovementDate(doc.Date);
inv.setM_Warehouse_ID(locator.getM_Warehouse_ID());
setGeneratedTag(inv);
inv.saveEx();
//
MInventoryLine line = new MInventoryLine(inv, locator.get_ID(), product.get_ID(), 0, null, null);
line.setQtyInternalUse(doc.Qty);
line.setC_Charge_ID(getCreateCharge("junit-charge").get_ID());
line.saveEx();
//
doc.document = inv;
processDocument(doc, MInventory.DOCACTION_Complete, MInventory.DOCSTATUS_Completed);
if (!Util.isEmpty(doc.ASI)) {
line.load(trxName);
doc.scenario.registerASICode(doc.ASI, line.getM_AttributeSetInstance_ID(), line.getQtyInternalUse().signum() <= 0);
}
return inv;
}
use of org.compiere.model.MInventoryLine in project adempiere by adempiere.
the class InventoryCountCreate method createInventoryLine.
// doIt
/**
* Create/Add to Inventory Line
* @param M_Product_ID product
* @param M_Locator_ID locator
* @param M_AttributeSetInstance_ID asi
* @param QtyOnHand qty
* @param M_AttributeSet_ID as
* @return lines added
*/
private int createInventoryLine(int M_Locator_ID, int M_Product_ID, int M_AttributeSetInstance_ID, BigDecimal QtyOnHand, int M_AttributeSet_ID) {
boolean oneLinePerASI = false;
if (M_AttributeSet_ID != 0) {
MAttributeSet mas = MAttributeSet.get(getCtx(), M_AttributeSet_ID);
oneLinePerASI = mas.isInstanceAttribute();
}
if (oneLinePerASI) {
MInventoryLine line = new MInventoryLine(m_inventory, M_Locator_ID, M_Product_ID, M_AttributeSetInstance_ID, QtyOnHand, // book/count
QtyOnHand);
if (line.save())
return 1;
return 0;
}
if (QtyOnHand.signum() == 0)
M_AttributeSetInstance_ID = 0;
if (m_line != null && m_line.getM_Locator_ID() == M_Locator_ID && m_line.getM_Product_ID() == M_Product_ID) {
if (QtyOnHand.signum() == 0)
return 0;
// Same ASI (usually 0)
if (m_line.getM_AttributeSetInstance_ID() == M_AttributeSetInstance_ID) {
m_line.setQtyBook(m_line.getQtyBook().add(QtyOnHand));
m_line.setQtyCount(m_line.getQtyCount().add(QtyOnHand));
m_line.saveEx();
return 0;
} else // Save Old Line info
if (m_line.getM_AttributeSetInstance_ID() != 0) {
MInventoryLineMA ma = new MInventoryLineMA(m_line, m_line.getM_AttributeSetInstance_ID(), m_line.getQtyBook());
if (!ma.save())
;
}
m_line.setM_AttributeSetInstance_ID(0);
m_line.setQtyBook(m_line.getQtyBook().add(QtyOnHand));
m_line.setQtyCount(m_line.getQtyCount().add(QtyOnHand));
m_line.saveEx();
//
MInventoryLineMA ma = new MInventoryLineMA(m_line, M_AttributeSetInstance_ID, QtyOnHand);
if (!ma.save())
;
return 0;
}
// new line
m_line = new MInventoryLine(m_inventory, M_Locator_ID, M_Product_ID, M_AttributeSetInstance_ID, QtyOnHand, // book/count
QtyOnHand);
if (m_line.save())
return 1;
return 0;
}
use of org.compiere.model.MInventoryLine in project adempiere by adempiere.
the class InventoryCountUpdate method updateWithMA.
// doIt
/**
* Update Inventory Lines With Material Allocation
* @return no updated
*/
private int updateWithMA() {
int no = 0;
//
String sql = "SELECT * FROM M_InventoryLine WHERE M_Inventory_ID=? AND M_AttributeSetInstance_ID=0";
PreparedStatement preparedStatement = null;
try {
preparedStatement = DB.prepareStatement(sql, get_TrxName());
preparedStatement.setInt(1, getRecord_ID());
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
MInventoryLine inventoryLine = new MInventoryLine(getCtx(), resultSet, get_TrxName());
BigDecimal onHand = Env.ZERO;
MStorage[] storages = MStorage.getAll(getCtx(), inventoryLine.getM_Product_ID(), inventoryLine.getM_Locator_ID(), get_TrxName());
MInventoryLineMA inventoryLineMA = null;
for (int i = 0; i < storages.length; i++) {
MStorage storage = storages[i];
if (storage.getQtyOnHand().signum() == 0)
continue;
onHand = onHand.add(storage.getQtyOnHand());
// No ASI
if (storage.getM_AttributeSetInstance_ID() == 0 && storages.length == 1)
continue;
// Save ASI
inventoryLineMA = new MInventoryLineMA(inventoryLine, storage.getM_AttributeSetInstance_ID(), storage.getQtyOnHand());
if (!inventoryLineMA.save())
;
}
inventoryLine.setQtyBook(onHand);
if (isUpdateCountQty())
inventoryLine.setQtyCount(onHand);
if (inventoryLine.save())
no++;
}
resultSet.close();
preparedStatement.close();
preparedStatement = null;
} catch (Exception e) {
log.log(Level.SEVERE, sql, e);
}
try {
if (preparedStatement != null)
preparedStatement.close();
preparedStatement = null;
} catch (Exception e) {
preparedStatement = null;
}
//
log.info("#" + no);
return no;
}
use of org.compiere.model.MInventoryLine 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