Search in sources :

Example 21 with MAttributeSet

use of org.compiere.model.MAttributeSet 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 22 with MAttributeSet

use of org.compiere.model.MAttributeSet 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)

Aggregations

MAttributeSet (org.compiere.model.MAttributeSet)22 BigDecimal (java.math.BigDecimal)7 MAttribute (org.compiere.model.MAttribute)7 MProduct (org.compiere.model.MProduct)6 MAttributeSetInstance (org.compiere.model.MAttributeSetInstance)5 Listbox (org.adempiere.webui.component.Listbox)4 ALayoutConstraint (org.compiere.apps.ALayoutConstraint)4 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 Timestamp (java.sql.Timestamp)3 Row (org.adempiere.webui.component.Row)3 MAttributeInstance (org.compiere.model.MAttributeInstance)3 MAttributeValue (org.compiere.model.MAttributeValue)3 KeyNamePair (org.compiere.util.KeyNamePair)3 Dimension (java.awt.Dimension)2 Label (org.adempiere.webui.component.Label)2 ListItem (org.adempiere.webui.component.ListItem)2 NumberBox (org.adempiere.webui.component.NumberBox)2 VComboBox (org.compiere.grid.ed.VComboBox)2 VString (org.compiere.grid.ed.VString)2