//=============================================================================================================

        /// <summary>
        /// Add Attribute Set Value
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        public string AddAttributeSet(VA005_AddAttributeSet values)
        {
            MAttributeSet ctrl = new MAttributeSet(_ctx, values.ID, null);

            ctrl.SetName(values.name);
            ctrl.SetDescription(values.description);
            ctrl.SetMandatoryType(values.mandatorytype);
            ctrl.SetIsGuaranteeDate(values.IsGuaranteeDate);
            ctrl.SetIsGuaranteeDateMandatory(values.IsGuaranteeDateMandatory);
            ctrl.SetIsLot(values.islotcheck);
            ctrl.SetIsSerNo(values.isserialcheck);
            if (!String.IsNullOrEmpty(values.lotvalue))
            {
                ctrl.SetM_LotCtl_ID(Convert.ToInt32(values.lotvalue));
            }
            else
            {
                ctrl.SetM_LotCtl_ID(0);
            }

            if (!String.IsNullOrEmpty(values.serialvalue))
            {
                ctrl.SetM_SerNoCtl_ID(Convert.ToInt32(values.serialvalue));
            }
            else
            {
                ctrl.SetM_SerNoCtl_ID(0);
            }

            if (ctrl.Save())
            {
                return(ctrl.GetM_AttributeSet_ID().ToString());
            }
            return(Msg.GetMsg(_ctx, "VA005_UnableToAddAttributeSet"));
        }
        public string DeleteAttributeSetValue(VA005_DeleteAttributeSetValue value)
        {
            //MAttributeValue obj = new MAttributeValue(_ctx, value.attributesetdelID, null);
            MAttributeSet obj      = new MAttributeSet(_ctx, value.attributesetdelID, null);
            int           attvalid = obj.GetM_AttributeSet_ID();
            string        _result  = "";

            if (!obj.Delete(true))
            {
                ValueNamePair pp = VLogger.RetrieveError();
                _result = pp.ToString();
            }

            return(_result);
        }
        public AttributeInstance SaveAttribute(int windowNoParent, string strLotStringC, string strSerNoC, string dtGuaranteeDateC, string strAttrCodeC,
                                               bool productWindow, int mAttributeSetInstanceId, int mProductId, int windowNo, List <KeyNamePair> values, Ctx ctx)
        {
            var editors = values;
            AttributeInstance obj = new AttributeInstance();
            String            strLotString = "", strSerNo = "", strAttrCode = "";
            int           attributeID = 0, prdAttributes = 0, pAttribute_ID = 0, product_id = 0;
            StringBuilder sql             = new StringBuilder();
            string        qry             = "";
            StringBuilder qryAttr         = null;
            DataSet       ds              = null;
            DateTime?     dtGuaranteeDate = null;
            bool          _changed        = false;

            if (!productWindow && strLotStringC != null)
            {
                strLotString = strLotStringC;
            }   //	L

            if (!productWindow && strSerNoC != null)
            {
                log.Fine("SerNo=" + strSerNoC);
                strSerNo = strSerNoC;
            }

            if (!productWindow && dtGuaranteeDateC != null)
            {
                dtGuaranteeDate = Convert.ToDateTime(dtGuaranteeDateC);
            }   //	Gua

            if (!productWindow && strAttrCodeC != null)
            {
                strAttrCode = strAttrCodeC;
            }
            if (String.IsNullOrEmpty(strAttrCode))
            {
                ctx.SetContext(windowNoParent, "AttrCode", "");
            }
            else
            {
                ctx.SetContext(windowNoParent, "AttrCode", strAttrCode);
            }

            MAttributeSet aset = null;

            MAttribute[] attributes = null;
            String       mandatory  = "";
            var          _masi      = MAttributeSetInstance.Get(ctx, 0, mProductId);

            aset = _masi.GetMAttributeSet();
            if (aset == null)
            {
                return(null);
            }

            if (!productWindow && strAttrCode != "")
            {
                qryAttr = new StringBuilder();
                qryAttr.Append(@"SELECT count(*) FROM M_Product prd LEFT JOIN M_ProductAttributes patr on (prd.M_Product_ID=patr.M_Product_ID) " +
                               " LEFT JOIN M_Manufacturer muf on (prd.M_Product_ID=muf.M_Product_ID) WHERE (patr.UPC = '" + strAttrCode + "' OR prd.UPC = '" + strAttrCode + "' OR muf.UPC = '" + strAttrCode + "')");
                //"AND (patr.M_Product_ID = " + _M_Product_ID + " OR prd.M_Product_ID = " + _M_Product_ID + " OR muf.M_Product_ID = " + _M_Product_ID + ")";
                prdAttributes = Util.GetValueOfInt(DB.ExecuteScalar(qryAttr.ToString()));
                if (prdAttributes != 0)
                {
                    //qryAttr.Clear();
                    //qryAttr.Append("SELECT M_AttributeSetInstance_ID FROM M_ProductAttributes WHERE UPC = '" + strAttrCode + "' AND M_Product_ID = " + _M_Product_ID);
                    //attributeID = Util.GetValueOfInt(DB.ExecuteScalar(qryAttr.ToString()));
                    //if (attributeID == 0)
                    //{
                    qryAttr.Clear();
                    qryAttr.Append("SELECT M_ProductAttributes_ID FROM M_ProductAttributes WHERE UPC = '" + strAttrCode + "'");
                    pAttribute_ID = Util.GetValueOfInt(DB.ExecuteScalar(qryAttr.ToString()));
                    if (pAttribute_ID != 0)
                    {
                        MProductAttributes patr = new MProductAttributes(ctx, pAttribute_ID, null);
                        attributeID = patr.GetM_AttributeSetInstance_ID();
                        product_id  = patr.GetM_Product_ID();
                    }
                    //}
                }
                _changed = true;
            }   //	Attribute Code

            if (!productWindow && aset.IsLot())
            {
                log.Fine("Lot=" + strLotString);
                String text = strLotString;
                _masi.SetLot(text);
                sql.Append("UPPER(ats.Lot) = '" + text.ToUpper() + "'");
                if (aset.IsLotMandatory() && (text == null || text.Length == 0))
                {
                    mandatory += " - " + Msg.Translate(ctx, "Lot");
                }
                _changed = true;
            }   //	Lot
            if (!productWindow && aset.IsSerNo())
            {
                log.Fine("SerNo=" + strSerNo);
                String text = strSerNo;
                _masi.SetSerNo(text);
                _masi.SetSerNo(text);
                if (sql.Length > 0)
                {
                    sql.Append(" and UPPER(ats.SerNo) = '" + text.ToUpper() + "'");
                }
                else
                {
                    sql.Append(" UPPER(ats.SerNo) = '" + text.ToUpper() + "'");
                }
                if (aset.IsSerNoMandatory() && (text == null || text.Length == 0))
                {
                    mandatory += " - " + Msg.Translate(ctx, "SerNo");
                }
                _changed = true;
            }
            if (!productWindow && aset.IsGuaranteeDate())
            {
                log.Fine("GuaranteeDate=" + dtGuaranteeDate);
                DateTime?ts = dtGuaranteeDate;
                _masi.SetGuaranteeDate(ts);
                if (sql.Length > 0)
                {
                    sql.Append(" AND ats.GuaranteeDate = " + GlobalVariable.TO_DATE(dtGuaranteeDate, true));
                }
                else
                {
                    sql.Append(" ats.GuaranteeDate = " + GlobalVariable.TO_DATE(dtGuaranteeDate, true));
                }
                if (aset.IsGuaranteeDateMandatory() && ts == null)
                {
                    mandatory += " - " + Msg.Translate(ctx, "GuaranteeDate");
                }
                _changed = true;
            }   //	GuaranteeDate

            if (sql.Length > 0)
            {
                sql.Insert(0, " where ");
            }
            sql.Append(" order by ats.m_attributesetinstance_id");

            //	***	Save Attributes ***
            //	New Instance
            if (_changed || _masi.GetM_AttributeSetInstance_ID() == 0)
            {
                //_masi.Save();
                //obj.M_AttributeSetInstance_ID = _masi.GetM_AttributeSetInstance_ID();
                //mAttributeSetInstanceId = _masi.GetM_AttributeSetInstance_ID();
                //obj.M_AttributeSetInstanceName = _masi.GetDescription();
            }
            //	Save Instance Attributes
            attributes = aset.GetMAttributes(!productWindow);

            if (attributes.Length > 0)
            {
                qry = @"SELECT ats.M_AttributeSetInstance_ID, av.M_AttributeValue_ID,ats.M_AttributeSet_ID,au.Value,att.AttributeValueType FROM M_AttributeSetInstance ats 
                        INNER JOIN M_AttributeInstance au ON ats.M_AttributeSetInstance_ID=au.M_AttributeSetInstance_ID LEFT JOIN M_Attribute att 
                        ON au.M_Attribute_ID=att.M_Attribute_ID LEFT JOIN M_AttributeValue av ON au.M_AttributeValue_ID=av.M_AttributeValue_ID";
            }
            else
            {
                qry = @"SELECT ats.M_AttributeSetInstance_ID FROM M_AttributeSetInstance ats ";
            }

            if (sql.Length > 0)
            {
                qry += sql;
            }
            if (attributes.Length > 0)
            {
                qry += ",au.M_Attribute_ID";
            }

            ds = DB.ExecuteDataset(qry, null, null);
            Dictionary <MAttribute, object> lst = new Dictionary <MAttribute, object>();

            for (int i = 0; i < attributes.Length; i++)
            {
                if (MAttribute.ATTRIBUTEVALUETYPE_List.Equals(attributes[i].GetAttributeValueType()))
                {
                    object          editor = editors[i];
                    MAttributeValue value  = null;
                    if (Convert.ToInt32(editors[i].Key) > 0)
                    {
                        value = new MAttributeValue(ctx, Convert.ToInt32(editors[i].Key), null);
                        value.SetName(editors[i].Name);
                    }
                    log.Fine(attributes[i].GetName() + "=" + value);
                    if (attributes[i].IsMandatory() && value == null)
                    {
                        mandatory += " - " + attributes[i].GetName();
                    }
                    lst[attributes[i]] = value;
                    //attributes[i].SetMAttributeInstance(mAttributeSetInstanceId, value);
                }
                else if (MAttribute.ATTRIBUTEVALUETYPE_Number.Equals(attributes[i].GetAttributeValueType()))
                {
                    object  editor = editors[i].Name;
                    decimal value  = Convert.ToDecimal(editor);
                    log.Fine(attributes[i].GetName() + "=" + value);
                    if (attributes[i].IsMandatory())
                    {
                        mandatory += " - " + attributes[i].GetName();
                    }
                    lst[attributes[i]] = value;
                    //attributes[i].SetMAttributeInstance(mAttributeSetInstanceId, value);
                }
                else
                {
                    object editor = editors[i].Name;
                    String value  = Convert.ToString(editor);
                    log.Fine(attributes[i].GetName() + "=" + value);
                    if (attributes[i].IsMandatory() && (value == null || value.Length == 0))
                    {
                        mandatory += " - " + attributes[i].GetName();
                    }
                    lst[attributes[i]] = value;
                    //attributes[i].SetMAttributeInstance(mAttributeSetInstanceId, value);
                }
                _changed = true;
            }

            if (_changed)
            {
                if (mandatory.Length > 0)
                {
                    obj.Error = Msg.GetMsg(ctx, "FillMandatory") + mandatory;
                    return(obj);
                }
                if (attributes.Length > 0)
                {
                    if (ds != null)
                    {
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            int attCount = 0;
                            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                            {
                                int    attSetID  = Util.GetValueOfInt(ds.Tables[0].Rows[i]["M_AttributeSet_ID"]);
                                string valueType = Util.GetValueOfString(ds.Tables[0].Rows[i]["AttributeValueType"]);
                                int    attributesetinstance_iD = Util.GetValueOfInt(ds.Tables[0].Rows[i]["M_AttributeSetInstance_ID"]);
                                if (i > 0 && attributesetinstance_iD != Util.GetValueOfInt(ds.Tables[0].Rows[i - 1]["M_AttributeSetInstance_ID"]))
                                {
                                    attCount = 0;
                                }
                                for (int j = 0; j < attributes.Length; j++)
                                {
                                    if (MAttribute.ATTRIBUTEVALUETYPE_List.Equals(attributes[j].GetAttributeValueType()) && MAttribute.ATTRIBUTEVALUETYPE_List.Equals(valueType))
                                    {
                                        int             attID = Util.GetValueOfInt(ds.Tables[0].Rows[i]["M_AttributeValue_ID"]);
                                        MAttributeValue atr   = new MAttributeValue(ctx, attID, null);

                                        if (Util.GetValueOfString(atr.GetName()) == Util.GetValueOfString(lst[attributes[j]]) && attSetID == aset.GetM_AttributeSet_ID())
                                        {
                                            attCount += 1;
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                    }
                                    else if (MAttribute.ATTRIBUTEVALUETYPE_Number.Equals(attributes[j].GetAttributeValueType()) && MAttribute.ATTRIBUTEVALUETYPE_Number.Equals(valueType))
                                    {
                                        decimal?attVal = Util.GetValueOfDecimal(ds.Tables[0].Rows[i]["Value"]);
                                        if (attVal == Util.GetValueOfDecimal(lst[attributes[j]]) && attSetID == aset.GetM_AttributeSet_ID())
                                        {
                                            attCount += 1;
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                    }
                                    else if (MAttribute.ATTRIBUTEVALUETYPE_StringMax40.Equals(attributes[j].GetAttributeValueType()) && MAttribute.ATTRIBUTEVALUETYPE_StringMax40.Equals(valueType))
                                    {
                                        string attVal = Util.GetValueOfString(ds.Tables[0].Rows[i]["Value"]);
                                        if (attVal == Util.GetValueOfString(lst[attributes[j]]) && attSetID == aset.GetM_AttributeSet_ID())
                                        {
                                            attCount += 1;
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                    }
                                }

                                if (attCount == attributes.Length)
                                {
                                    mAttributeSetInstanceId = Util.GetValueOfInt(ds.Tables[0].Rows[i]["M_AttributeSetInstance_ID"]);
                                    break;
                                }
                            }
                            if (attCount != attributes.Length)
                            {
                                mAttributeSetInstanceId = 0;
                            }
                        }
                        else
                        {
                            mAttributeSetInstanceId = 0;
                        }
                    }
                    else
                    {
                        mAttributeSetInstanceId = 0;
                    }
                    ds.Dispose();
                }
                else
                {
                    if (ds != null)
                    {
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                            {
                                mAttributeSetInstanceId = Util.GetValueOfInt(ds.Tables[0].Rows[i]["M_AttributeSetInstance_ID"]);
                                break;
                            }
                        }
                        else
                        {
                            mAttributeSetInstanceId = 0;
                        }
                    }
                    else
                    {
                        mAttributeSetInstanceId = 0;
                    }
                    ds.Dispose();
                }

                if (mAttributeSetInstanceId == 0)
                {
                    _masi.Save();

                    mAttributeSetInstanceId        = _masi.GetM_AttributeSetInstance_ID();
                    obj.M_AttributeSetInstance_ID  = _masi.GetM_AttributeSetInstance_ID();
                    obj.M_AttributeSetInstanceName = _masi.GetDescription();
                }

                else
                {
                    _masi = new MAttributeSetInstance(ctx, mAttributeSetInstanceId, null);
                }

                for (int i = 0; i < attributes.Length; i++)
                {
                    if (MAttribute.ATTRIBUTEVALUETYPE_List.Equals(attributes[i].GetAttributeValueType()))
                    {
                        MAttributeValue value = lst[attributes[i]] != null ? lst[attributes[i]] as MAttributeValue : null;
                        attributes[i].SetMAttributeInstance(mAttributeSetInstanceId, value);
                    }
                    else if (MAttribute.ATTRIBUTEVALUETYPE_Number.Equals(attributes[i].GetAttributeValueType()))
                    {
                        attributes[i].SetMAttributeInstance(mAttributeSetInstanceId, (decimal?)lst[attributes[i]]);
                    }
                    else
                    {
                        attributes[i].SetMAttributeInstance(mAttributeSetInstanceId, (String)lst[attributes[i]]);
                    }
                }

                if (attributeID == 0 && strAttrCode != "")
                {
                    MProductAttributes pAttr = new MProductAttributes(ctx, 0, null);
                    pAttr.SetUPC(strAttrCode);
                    pAttr.SetM_Product_ID(mProductId);
                    pAttr.SetM_AttributeSetInstance_ID(mAttributeSetInstanceId);
                    pAttr.Save();
                }
                _masi.SetDescription();
                _masi.Save();

                mAttributeSetInstanceId        = _masi.GetM_AttributeSetInstance_ID();
                obj.M_AttributeSetInstance_ID  = _masi.GetM_AttributeSetInstance_ID();
                obj.M_AttributeSetInstanceName = _masi.GetDescription();
                //
                if (attributeID != 0 && (attributeID != mAttributeSetInstanceId || product_id != mProductId))
                {
                    obj.Error = Msg.GetMsg(ctx, "AttributeCodeExists");
                }
            }
            else
            {
                obj.M_AttributeSetInstance_ID  = _masi.GetM_AttributeSetInstance_ID();
                obj.M_AttributeSetInstanceName = _masi.GetDescription();
            }
            return(obj);
        }
Esempio n. 4
0
        }       //	prepare

        /// <summary>
        /// Perrform Process.
        /// </summary>
        /// <returns>Info</returns>
        protected override String DoIt()
        {
            log.Info("M_Locator_ID=" + _M_Locator_ID + ",MovementDate=" + _MovementDate);
            //
            StringBuilder sql         = null;
            int           no          = 0;
            String        clientCheck = " AND AD_Client_ID=" + _AD_Client_ID;

            //	****	Prepare	****

            //	Delete Old Imported
            if (_DeleteOldImported)
            {
                sql = new StringBuilder("DELETE FROM I_Inventory "
                                        + "WHERE I_IsImported='Y'").Append(clientCheck);
                no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
                log.Fine("Delete Old Impored =" + no);
            }

            //	Set Client, Org, Location, IsActive, Created/Updated
            sql = new StringBuilder("UPDATE I_Inventory "
                                    + "SET AD_Client_ID = COALESCE (AD_Client_ID,").Append(_AD_Client_ID).Append("),"
                                                                                                                 + " AD_Org_ID = COALESCE (AD_Org_ID,").Append(_AD_Org_ID).Append("),");
            if (_MovementDate != null)
            {
                sql.Append(" MovementDate = COALESCE (MovementDate,").Append(DataBase.DB.TO_DATE(_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 = NULL,"
                       + " M_Warehouse_ID = NULL," //	reset
                       + " I_IsImported = 'N' "
                       + "WHERE I_IsImported<>'Y' OR I_IsImported IS NULL");
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Info("Reset=" + no);

            String ts = DataBase.DB.IsPostgreSQL() ? "COALESCE(I_ErrorMsg,'')" : "I_ErrorMsg";  //java bug, it could not be used directly

            sql = new StringBuilder("UPDATE I_Inventory o "
                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||'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 = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            if (no != 0)
            {
                log.Warning("Invalid Org=" + no);
            }

            // gwu: bug 1703137
            // if Warehouse key provided, get Warehouse ID
            sql = new StringBuilder("UPDATE I_Inventory i "
                                    + "SET M_Warehouse_ID=(SELECT MAX(M_Warehouse_ID) FROM M_Warehouse w"
                                    + " WHERE i.WarehouseValue=w.Value AND i.AD_Client_ID=w.AD_Client_ID) "
                                    + "WHERE M_Warehouse_ID IS NULL AND WarehouseValue IS NOT NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set Warehouse from Value =" + no);

            //	Location
            sql = new StringBuilder("UPDATE I_Inventory i "
                                    + "SET M_Locator_ID=(SELECT MAX(M_Locator_ID) FROM M_Locator l"
                                    + " WHERE i.LocatorValue=l.Value AND COALESCE (i.M_Warehouse_ID, l.M_Warehouse_ID)=l.M_Warehouse_ID 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 = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set Locator from Value =" + no);
            sql = new StringBuilder("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 COALESCE (i.M_Warehouse_ID, l.M_Warehouse_ID)=l.M_Warehouse_ID 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 = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set Locator from X,Y,Z =" + no);
            if (_M_Locator_ID != 0)
            {
                sql = new StringBuilder("UPDATE I_Inventory "
                                        + "SET M_Locator_ID = ").Append(_M_Locator_ID).Append(
                    " WHERE M_Locator_ID IS NULL"
                    + " AND I_IsImported<>'Y'").Append(clientCheck);
                no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
                log.Fine("Set Locator from Parameter=" + no);
            }
            sql = new StringBuilder("UPDATE I_Inventory "
                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||'ERR=No Location, ' "
                                    + "WHERE M_Locator_ID IS NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            if (no != 0)
            {
                log.Warning("No Location=" + no);
            }


            //	Set M_Warehouse_ID
            sql = new StringBuilder("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 = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set Warehouse from Locator =" + no);
            sql = new StringBuilder("UPDATE I_Inventory "
                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||'ERR=No Warehouse, ' "
                                    + "WHERE M_Warehouse_ID IS NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            if (no != 0)
            {
                log.Warning("No Warehouse=" + no);
            }


            //	Product
            sql = new StringBuilder("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 = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set Product from Value=" + no);
            sql = new StringBuilder("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 = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set Product from UPC=" + no);
            sql = new StringBuilder("UPDATE I_Inventory "
                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||'ERR=No Product, ' "
                                    + "WHERE M_Product_ID IS NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            if (no != 0)
            {
                log.Warning("No Product=" + no);
            }

            //	No QtyCount
            sql = new StringBuilder("UPDATE I_Inventory "
                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||'ERR=No Qty Count, ' "
                                    + "WHERE QtyCount IS NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            if (no != 0)
            {
                log.Warning("No QtyCount=" + no);
            }

            Commit();

            /*********************************************************************/

            MInventory inventory = null;

            int noInsert     = 0;
            int noInsertLine = 0;

            //	Go through Inventory Records
            sql = new StringBuilder("SELECT * FROM I_Inventory "
                                    + "WHERE I_IsImported='N'").Append(clientCheck)
                  .Append(" ORDER BY M_Warehouse_ID, TRUNC(MovementDate,'DD'), I_Inventory_ID");
            IDataReader idr = null;

            try
            {
                //PreparedStatement pstmt = DataBase.prepareStatement (sql.ToString (), Get_TrxName());
                //ResultSet rs = pstmt.executeQuery ();
                idr = DataBase.DB.ExecuteReader(sql.ToString(), null, Get_TrxName());
                //
                int      x_M_Warehouse_ID = -1;
                DateTime?x_MovementDate   = null;
                while (idr.Read())
                {
                    X_I_Inventory imp          = new X_I_Inventory(GetCtx(), idr, Get_TrxName());
                    DateTime?     MovementDate = TimeUtil.GetDay(imp.GetMovementDate());

                    if (inventory == null ||
                        imp.GetM_Warehouse_ID() != x_M_Warehouse_ID ||
                        !MovementDate.Equals(x_MovementDate))
                    {
                        inventory = new MInventory(GetCtx(), 0, Get_TrxName());
                        inventory.SetClientOrg(imp.GetAD_Client_ID(), imp.GetAD_Org_ID());
                        inventory.SetDescription("I " + imp.GetM_Warehouse_ID() + " " + MovementDate);
                        inventory.SetM_Warehouse_ID(imp.GetM_Warehouse_ID());
                        inventory.SetMovementDate(MovementDate);
                        //
                        if (!inventory.Save())
                        {
                            log.Log(Level.SEVERE, "Inventory not saved");
                            break;
                        }
                        x_M_Warehouse_ID = imp.GetM_Warehouse_ID();
                        x_MovementDate   = MovementDate;
                        noInsert++;
                    }

                    //	Line
                    int M_AttributeSetInstance_ID = 0;
                    if (imp.GetLot() != null || imp.GetSerNo() != null)
                    {
                        MProduct product = MProduct.Get(GetCtx(), imp.GetM_Product_ID());
                        if (product.IsInstanceAttribute())
                        {
                            MAttributeSet         mas  = product.GetAttributeSet();
                            MAttributeSetInstance masi = new MAttributeSetInstance(GetCtx(), 0, mas.GetM_AttributeSet_ID(), Get_TrxName());
                            if (mas.IsLot() && imp.GetLot() != null)
                            {
                                masi.SetLot(imp.GetLot(), imp.GetM_Product_ID());
                            }
                            if (mas.IsSerNo() && imp.GetSerNo() != null)
                            {
                                masi.SetSerNo(imp.GetSerNo());
                            }
                            masi.SetDescription();
                            masi.Save();
                            M_AttributeSetInstance_ID = masi.GetM_AttributeSetInstance_ID();
                        }
                    }
                    MInventoryLine line = new MInventoryLine(inventory,
                                                             imp.GetM_Locator_ID(), imp.GetM_Product_ID(), M_AttributeSetInstance_ID,
                                                             imp.GetQtyBook(), imp.GetQtyCount());
                    if (line.Save())
                    {
                        imp.SetI_IsImported(X_I_Inventory.I_ISIMPORTED_Yes);
                        imp.SetM_Inventory_ID(line.GetM_Inventory_ID());
                        imp.SetM_InventoryLine_ID(line.GetM_InventoryLine_ID());
                        imp.SetProcessed(true);
                        if (imp.Save())
                        {
                            noInsertLine++;
                        }
                    }
                }
                idr.Close();
            }
            catch (Exception e)
            {
                if (idr != null)
                {
                    idr.Close();
                }
                log.Log(Level.SEVERE, sql.ToString(), e);
            }

            //	Set Error to indicator to not imported
            sql = new StringBuilder("UPDATE I_Inventory "
                                    + "SET I_IsImported='N', Updated=SysDate "
                                    + "WHERE I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            AddLog(0, null, Utility.Util.GetValueOfDecimal(no), "@Errors@");
            //
            AddLog(0, null, Utility.Util.GetValueOfDecimal(noInsert), "@M_Inventory_ID@: @Inserted@");
            AddLog(0, null, Utility.Util.GetValueOfDecimal(noInsertLine), "@M_InventoryLine_ID@: @Inserted@");
            return("");
        }       //	doIt