Search in sources :

Example 11 with MAttributeSetInstance

use of org.compiere.model.MAttributeSetInstance 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);
}
Also used : MProduct(org.compiere.model.MProduct) MAttributeSetInstance(org.compiere.model.MAttributeSetInstance) MStorage(org.compiere.model.MStorage) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal)

Example 12 with MAttributeSetInstance

use of org.compiere.model.MAttributeSetInstance 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 "";
}
Also used : MProduct(org.compiere.model.MProduct) MInventoryLine(org.compiere.model.MInventoryLine) PreparedStatement(java.sql.PreparedStatement) MAttributeSetInstance(org.compiere.model.MAttributeSetInstance) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) X_I_Inventory(org.compiere.model.X_I_Inventory) MAttributeSet(org.compiere.model.MAttributeSet) MInventory(org.compiere.model.MInventory) ResultSet(java.sql.ResultSet)

Example 13 with MAttributeSetInstance

use of org.compiere.model.MAttributeSetInstance in project adempiere by adempiere.

the class ImportProductASI method importProductASI.

/**
	 * Import Product ASI using X_I_Product_ASI table
	 * 
	 * @param X_I_Product_ASI
	 *            Product ASI
	 * @return Attribute Instance
	 * @throws SQLException
	 */
private MAttributeInstance importProductASI(X_I_Product_ASI ip_asi) throws SQLException {
    MProduct product = (MProduct) ip_asi.getM_Product();
    MAttributeSet attributeSet = null;
    if (ip_asi.getM_AttributeSet_ID() <= 0) {
        attributeSet = getAttributeSet(ip_asi);
        product.setM_AttributeSet_ID(attributeSet.get_ID());
        product.saveEx();
        ip_asi.setM_AttributeSet_ID(attributeSet.getM_AttributeSet_ID());
        ip_asi.saveEx();
    } else {
        attributeSet = (MAttributeSet) product.getM_AttributeSet();
    }
    // Attribute
    MAttribute attribute = null;
    if (ip_asi.getM_Attribute_ID() <= 0) {
        attribute = getAttribute(ip_asi);
        ip_asi.setM_Attribute_ID(attribute.getM_Attribute_ID());
        ip_asi.saveEx();
    } else
        attribute = (MAttribute) ip_asi.getM_Attribute();
    // Check Attribute in Attribute Set
    checkAttributeUse(attributeSet, attribute);
    MAttributeSetInstance asi = (MAttributeSetInstance) product.getM_AttributeSetInstance();
    if (asi == null || asi.get_ID() <= 0) {
        asi = getAttributeSetInstance(product, attributeSet);
        product.setM_AttributeSetInstance_ID(asi.get_ID());
        product.saveEx();
        ip_asi.setM_AttributeSetInstance_ID(asi.getM_AttributeSetInstance_ID());
        ip_asi.saveEx();
    }
    MAttributeInstance ai = addAttributeLine(asi, attribute, ip_asi);
    ip_asi.saveEx();
    commitEx();
    return ai;
}
Also used : MProduct(org.compiere.model.MProduct) MAttributeInstance(org.compiere.model.MAttributeInstance) MAttributeSetInstance(org.compiere.model.MAttributeSetInstance) MAttributeSet(org.compiere.model.MAttributeSet) MAttribute(org.compiere.model.MAttribute)

Example 14 with MAttributeSetInstance

use of org.compiere.model.MAttributeSetInstance in project adempiere by adempiere.

the class ImportProductASI method getAttributeSetInstance.

private MAttributeSetInstance getAttributeSetInstance(MProduct product, MAttributeSet attributeSet) {
    MAttributeSetInstance asi = new MAttributeSetInstance(getCtx(), product.getM_AttributeSetInstance_ID(), product.get_TrxName());
    if (asi == null || asi.get_ID() <= 0) {
        asi = new MAttributeSetInstance(product.getCtx(), 0, product.get_TrxName());
        asi.setM_AttributeSet_ID(product.getM_AttributeSet_ID());
        asi.saveEx();
    }
    return asi;
}
Also used : MAttributeSetInstance(org.compiere.model.MAttributeSetInstance)

Example 15 with MAttributeSetInstance

use of org.compiere.model.MAttributeSetInstance in project adempiere by adempiere.

the class HTMLMessenger method getBOMInfo.

public String getBOMInfo(BOMWrapper pb) {
    SimpleDateFormat df = Env.getLanguage(Env.getCtx()).getDateFormat();
    MProduct p = new MProduct(Env.getCtx(), pb.getM_Product_ID(), MProduct.Table_Name);
    MAttributeSetInstance asi = new MAttributeSetInstance(Env.getCtx(), pb.getM_AttributeSetInstance_ID(), "M_AttributeSetInstance");
    Object[] obj = new Object[] { pb.getDocumentNo(), pb.getName(), pb.getValidFrom() == null ? "" : df.format(pb.getValidFrom()), pb.getValidTo() == null ? "" : df.format(pb.getValidTo()), pb.getValue(), p.getName(), getAttributeSetInstanceInfo(asi, false), getBOMLinesInfo(pb.getLines()) };
    return MessageFormat.format(BOM_INFO_PATTERN, obj);
}
Also used : MProduct(org.compiere.model.MProduct) MAttributeSetInstance(org.compiere.model.MAttributeSetInstance) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

MAttributeSetInstance (org.compiere.model.MAttributeSetInstance)19 MProduct (org.compiere.model.MProduct)13 BigDecimal (java.math.BigDecimal)8 MAttributeSet (org.compiere.model.MAttributeSet)5 MAttribute (org.compiere.model.MAttribute)4 Timestamp (java.sql.Timestamp)3 MInventoryLine (org.compiere.model.MInventoryLine)3 MStorage (org.compiere.model.MStorage)3 KeyNamePair (org.compiere.util.KeyNamePair)3 SimpleDateFormat (java.text.SimpleDateFormat)2 Vector (java.util.Vector)2 AdempiereException (org.adempiere.exceptions.AdempiereException)2 MAttributeInstance (org.compiere.model.MAttributeInstance)2 MInOutLine (org.compiere.model.MInOutLine)2 PO (org.compiere.model.PO)2 Query (org.compiere.model.Query)2 MWMInOutBoundLine (org.eevolution.model.MWMInOutBoundLine)2 MWMInOutBoundLineMA (org.eevolution.model.MWMInOutBoundLineMA)2 Dimension (java.awt.Dimension)1 PreparedStatement (java.sql.PreparedStatement)1