Ejemplo n.º 1
0
        /// <summary>
        /// save location
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="pref"></param>
        /// <returns></returns>
        public MLocation LocationSave(Ctx ctx, Dictionary <string, object> pref)
        {
            var       c_Location_Id = Convert.ToInt32(pref["clocationId"]);
            MLocation _location     = new MLocation(ctx, c_Location_Id, null);

            _location.SetAddress1(Convert.ToString(pref["addvalue1"]));
            _location.SetAddress2(Convert.ToString(pref["addvalue2"]));
            _location.SetAddress3(Convert.ToString(pref["addvalue3"]));
            _location.SetAddress4(Convert.ToString(pref["addvalue4"]));
            _location.SetCity(Convert.ToString(pref["cityValue"]));
            _location.SetPostal(Convert.ToString(pref["zipValue"]));
            //  Country/Region
            _location.SetC_Country_ID(Convert.ToInt32(pref["countryId"]));

            if (_location.GetCountry().IsHasRegion())
            {
                _location.SetC_Region_ID(Convert.ToInt32(pref["stateId"]));
            }
            else
            {
                _location.SetC_Region_ID(0);
            }
            _location.SetRegionName(Convert.ToString(pref["stateValue"]));
            _location.SetC_City_ID(Convert.ToInt32(pref["cityId"]));

            _location.Save();
            return(_location);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// save location
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="pref"></param>
        /// <returns></returns>
        public MLocation LocationSave(Ctx ctx, Dictionary <string, object> pref)
        {
            MLocation _location = null;

            var c_Location_Id = Convert.ToInt32(pref["clocationId"]);

            string sql   = "SELECT COUNT(*) FROM C_Location WHERE C_Location_ID=" + c_Location_Id;
            object count = DB.ExecuteScalar(sql);

            if (count == null || count == DBNull.Value || Util.GetValueOfInt(count) == 0)
            {
                c_Location_Id = 0;
            }

            _location = new MLocation(ctx, c_Location_Id, null);
            _location.SetAddress1(Convert.ToString(pref["addvalue1"]));
            _location.SetAddress2(Convert.ToString(pref["addvalue2"]));
            _location.SetAddress3(Convert.ToString(pref["addvalue3"]));
            _location.SetAddress4(Convert.ToString(pref["addvalue4"]));
            _location.SetCity(Convert.ToString(pref["cityValue"]));
            _location.SetPostal(Convert.ToString(pref["zipValue"]));
            //  Country/Region
            _location.SetC_Country_ID(Convert.ToInt32(pref["countryId"]));

            if (_location.GetCountry().IsHasRegion())
            {
                _location.SetC_Region_ID(Convert.ToInt32(pref["stateId"]));
            }
            else
            {
                _location.SetC_Region_ID(0);
            }
            _location.SetRegionName(Convert.ToString(pref["stateValue"]));
            _location.SetC_City_ID(Convert.ToInt32(pref["cityId"]));

            _location.Save();
            return(_location);
        }
Ejemplo n.º 3
0
        }                                  //	prepare

        /// <summary>
        /// Perrform Process.
        /// </summary>
        /// <returns>clear message</returns>
        protected override String DoIt()
        {
            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_Invoice "
                                        + "WHERE I_IsImported='Y'").Append(clientCheck);
                no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
                log.Fine("Delete Old Impored =" + no);
            }

            //	Set Client, Org, IsActive, Created/Updated
            sql = new StringBuilder("UPDATE I_Invoice "
                                    + "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("),"
                                                                                                                                                                                  + " IsActive = COALESCE (IsActive, 'Y'),"
                                                                                                                                                                                  + " Created = COALESCE (Created, SysDate),"
                                                                                                                                                                                  + " CreatedBy = COALESCE (CreatedBy, 0),"
                                                                                                                                                                                  + " Updated = COALESCE (Updated, SysDate),"
                                                                                                                                                                                  + " UpdatedBy = COALESCE (UpdatedBy, 0),"
                                                                                                                                                                                  + " I_ErrorMsg = NULL,"
                                                                                                                                                                                  + " 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_Invoice 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);
            }

            //	Document Type - PO - SO
            sql = new StringBuilder("UPDATE I_Invoice o "
                                    + "SET C_DocType_ID=(SELECT C_DocType_ID FROM C_DocType d WHERE d.Name=o.DocTypeName"
                                    + " AND d.DocBaseType IN ('API','APC') AND o.AD_Client_ID=d.AD_Client_ID) "
                                    + "WHERE C_DocType_ID IS NULL AND IsSOTrx='N' AND DocTypeName IS NOT NULL AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            if (no != 0)
            {
                log.Fine("Set PO DocType=" + no);
            }
            sql = new StringBuilder("UPDATE I_Invoice o "
                                    + "SET C_DocType_ID=(SELECT C_DocType_ID FROM C_DocType d WHERE d.Name=o.DocTypeName"
                                    + " AND d.DocBaseType IN ('ARI','ARC') AND o.AD_Client_ID=d.AD_Client_ID) "
                                    + "WHERE C_DocType_ID IS NULL AND IsSOTrx='Y' AND DocTypeName IS NOT NULL AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            if (no != 0)
            {
                log.Fine("Set SO DocType=" + no);
            }
            sql = new StringBuilder("UPDATE I_Invoice o "
                                    + "SET C_DocType_ID=(SELECT C_DocType_ID FROM C_DocType d WHERE d.Name=o.DocTypeName"
                                    + " AND d.DocBaseType IN ('API','ARI','APC','ARC') AND o.AD_Client_ID=d.AD_Client_ID) "
                                    //+ "WHERE C_DocType_ID IS NULL AND IsSOTrx IS NULL AND DocTypeName IS NOT NULL AND I_IsImported<>'Y'").Append (clientCheck);
                                    + "WHERE C_DocType_ID IS NULL AND DocTypeName IS NOT NULL AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            if (no != 0)
            {
                log.Fine("Set DocType=" + no);
            }
            sql = new StringBuilder("UPDATE I_Invoice "
                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||'ERR=Invalid DocTypeName, ' "
                                    + "WHERE C_DocType_ID IS NULL AND DocTypeName IS NOT NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            if (no != 0)
            {
                log.Warning("Invalid DocTypeName=" + no);
            }
            //	DocType Default
            sql = new StringBuilder("UPDATE I_Invoice o "
                                    + "SET C_DocType_ID=(SELECT MAX(C_DocType_ID) FROM C_DocType d WHERE d.IsDefault='Y'"
                                    + " AND d.DocBaseType='API' AND o.AD_Client_ID=d.AD_Client_ID) "
                                    + "WHERE C_DocType_ID IS NULL AND IsSOTrx='N' AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            if (no != 0)
            {
                log.Fine("Set PO Default DocType=" + no);
            }
            sql = new StringBuilder("UPDATE I_Invoice o "
                                    + "SET C_DocType_ID=(SELECT MAX(C_DocType_ID) FROM C_DocType d WHERE d.IsDefault='Y'"
                                    + " AND d.DocBaseType='ARI' AND o.AD_Client_ID=d.AD_Client_ID) "
                                    + "WHERE C_DocType_ID IS NULL AND IsSOTrx='Y' AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            if (no != 0)
            {
                log.Fine("Set SO Default DocType=" + no);
            }
            sql = new StringBuilder("UPDATE I_Invoice o "
                                    + "SET C_DocType_ID=(SELECT MAX(C_DocType_ID) FROM C_DocType d WHERE d.IsDefault='Y'"
                                    + " AND d.DocBaseType IN('ARI','API') AND o.AD_Client_ID=d.AD_Client_ID) "
                                    + "WHERE C_DocType_ID IS NULL AND IsSOTrx IS NULL AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            if (no != 0)
            {
                log.Fine("Set Default DocType=" + no);
            }
            sql = new StringBuilder("UPDATE I_Invoice "
                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||'ERR=No DocType, ' "
                                    + "WHERE C_DocType_ID IS NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            if (no != 0)
            {
                log.Warning("No DocType=" + no);
            }

            //	Set IsSOTrx
            sql = new StringBuilder("UPDATE I_Invoice o SET IsSOTrx='Y' "
                                    + "WHERE EXISTS (SELECT * FROM C_DocType d WHERE o.C_DocType_ID=d.C_DocType_ID AND d.DocBaseType='ARI' AND o.AD_Client_ID=d.AD_Client_ID)"
                                    + " AND C_DocType_ID IS NOT NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set IsSOTrx=Y=" + no);
            sql = new StringBuilder("UPDATE I_Invoice o SET IsSOTrx='N' "
                                    + "WHERE EXISTS (SELECT * FROM C_DocType d WHERE o.C_DocType_ID=d.C_DocType_ID AND d.DocBaseType='API' AND o.AD_Client_ID=d.AD_Client_ID)"
                                    + " AND C_DocType_ID IS NOT NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set IsSOTrx=N=" + no);

            //	Price List
            sql = new StringBuilder("UPDATE I_Invoice o "
                                    + "SET M_PriceList_ID=(SELECT MAX(M_PriceList_ID) FROM M_PriceList p WHERE p.IsDefault='Y'"
                                    + " AND p.C_Currency_ID=o.C_Currency_ID AND p.IsSOPriceList=o.IsSOTrx AND o.AD_Client_ID=p.AD_Client_ID) "
                                    + "WHERE M_PriceList_ID IS NULL AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set Default Currency PriceList=" + no);
            sql = new StringBuilder("UPDATE I_Invoice o "
                                    + "SET M_PriceList_ID=(SELECT MAX(M_PriceList_ID) FROM M_PriceList p WHERE p.IsDefault='Y'"
                                    + " AND p.IsSOPriceList=o.IsSOTrx AND o.AD_Client_ID=p.AD_Client_ID) "
                                    + "WHERE M_PriceList_ID IS NULL AND C_Currency_ID IS NULL AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set Default PriceList=" + no);
            sql = new StringBuilder("UPDATE I_Invoice o "
                                    + "SET M_PriceList_ID=(SELECT MAX(M_PriceList_ID) FROM M_PriceList p "
                                    + " WHERE p.C_Currency_ID=o.C_Currency_ID AND p.IsSOPriceList=o.IsSOTrx AND o.AD_Client_ID=p.AD_Client_ID) "
                                    + "WHERE M_PriceList_ID IS NULL AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set Currency PriceList=" + no);
            sql = new StringBuilder("UPDATE I_Invoice o "
                                    + "SET M_PriceList_ID=(SELECT MAX(M_PriceList_ID) FROM M_PriceList p "
                                    + " WHERE p.IsSOPriceList=o.IsSOTrx AND o.AD_Client_ID=p.AD_Client_ID) "
                                    + "WHERE M_PriceList_ID IS NULL AND C_Currency_ID IS NULL AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set PriceList=" + no);
            //
            sql = new StringBuilder("UPDATE I_Invoice "
                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||'ERR=No PriceList, ' "
                                    + "WHERE M_PriceList_ID IS NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            if (no != 0)
            {
                log.Warning("No PriceList=" + no);
            }

            //	Payment Rule
            //  We support Payment Rule being input in the login language
            VAdvantage.Login.Language language = VAdvantage.Login.Language.GetLoginLanguage(GetCtx());          //	Base Language
            String AD_Language = language.GetAD_Language();

            sql = new StringBuilder("UPDATE I_Invoice O " +
                                    "SET PaymentRule= " +
                                    "(SELECT R.Value " +
                                    "  FROM AD_Ref_List R " +
                                    "  left outer join AD_Ref_List_Trl RT " +
                                    "  on RT.AD_Ref_List_ID = R.AD_Ref_List_ID and RT.AD_Language = @param " +
                                    "  WHERE R.AD_Reference_ID = 195 and coalesce( RT.Name, R.Name ) = O.PaymentRuleName ) " +
                                    "WHERE PaymentRule is null AND PaymentRuleName IS NOT NULL AND I_IsImported<>'Y'").Append(clientCheck);
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@param", AD_Language);
            no       = DataBase.DB.ExecuteQuery(sql.ToString(), param, Get_TrxName());
            log.Fine("Set PaymentRule=" + no);
            // do not set a default; if null, the import logic will derive from the business partner
            // do not error in absence of a default

            //	Payment Term
            sql = new StringBuilder("UPDATE I_Invoice o "
                                    + "SET C_PaymentTerm_ID=(SELECT C_PaymentTerm_ID FROM C_PaymentTerm p"
                                    + " WHERE o.PaymentTermValue=p.Value AND o.AD_Client_ID=p.AD_Client_ID) "
                                    + "WHERE C_PaymentTerm_ID IS NULL AND PaymentTermValue IS NOT NULL AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set PaymentTerm=" + no);
            sql = new StringBuilder("UPDATE I_Invoice o "
                                    + "SET C_PaymentTerm_ID=(SELECT MAX(C_PaymentTerm_ID) FROM C_PaymentTerm p"
                                    + " WHERE p.IsDefault='Y' AND o.AD_Client_ID=p.AD_Client_ID) "
                                    + "WHERE C_PaymentTerm_ID IS NULL AND o.PaymentTermValue IS NULL AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set Default PaymentTerm=" + no);
            //
            sql = new StringBuilder("UPDATE I_Invoice "
                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||'ERR=No PaymentTerm, ' "
                                    + "WHERE C_PaymentTerm_ID IS NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            if (no != 0)
            {
                log.Warning("No PaymentTerm=" + no);
            }

            //	BP from EMail
            sql = new StringBuilder("UPDATE I_Invoice o "
                                    + "SET (C_BPartner_ID,AD_User_ID)=(SELECT C_BPartner_ID,AD_User_ID FROM AD_User u"
                                    + " WHERE o.EMail=u.EMail AND o.AD_Client_ID=u.AD_Client_ID AND u.C_BPartner_ID IS NOT NULL) "
                                    + "WHERE C_BPartner_ID IS NULL AND EMail IS NOT NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set BP from EMail=" + no);
            //	BP from ContactName
            sql = new StringBuilder("UPDATE I_Invoice o "
                                    + "SET (C_BPartner_ID,AD_User_ID)=(SELECT C_BPartner_ID,AD_User_ID FROM AD_User u"
                                    + " WHERE o.ContactName=u.Name AND o.AD_Client_ID=u.AD_Client_ID AND u.C_BPartner_ID IS NOT NULL) "
                                    + "WHERE C_BPartner_ID IS NULL AND ContactName IS NOT NULL"
                                    + " AND EXISTS (SELECT Name FROM AD_User u WHERE o.ContactName=u.Name AND o.AD_Client_ID=u.AD_Client_ID AND u.C_BPartner_ID IS NOT NULL GROUP BY Name HAVING COUNT(*)=1)"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set BP from ContactName=" + no);
            //	BP from Value
            sql = new StringBuilder("UPDATE I_Invoice o "
                                    + "SET C_BPartner_ID=(SELECT MAX(C_BPartner_ID) FROM C_BPartner bp"
                                    + " WHERE o.BPartnerValue=bp.Value AND o.AD_Client_ID=bp.AD_Client_ID) "
                                    + "WHERE C_BPartner_ID IS NULL AND BPartnerValue IS NOT NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set BP from Value=" + no);
            //	Default BP
            sql = new StringBuilder("UPDATE I_Invoice o "
                                    + "SET C_BPartner_ID=(SELECT C_BPartnerCashTrx_ID FROM AD_ClientInfo c"
                                    + " WHERE o.AD_Client_ID=c.AD_Client_ID) "
                                    + "WHERE C_BPartner_ID IS NULL AND BPartnerValue IS NULL AND Name IS NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set Default BP=" + no);

            //	Existing Location ? Exact Match
            sql = new StringBuilder("UPDATE I_Invoice o "
                                    + "SET C_BPartner_Location_ID=(SELECT C_BPartner_Location_ID"
                                    + " FROM C_BPartner_Location bpl INNER JOIN C_Location l ON (bpl.C_Location_ID=l.C_Location_ID)"
                                    + " WHERE o.C_BPartner_ID=bpl.C_BPartner_ID AND bpl.AD_Client_ID=o.AD_Client_ID"
                                    + " AND DUMP(o.Address1)=DUMP(l.Address1) AND DUMP(o.Address2)=DUMP(l.Address2)"
                                    + " AND DUMP(o.City)=DUMP(l.City) AND DUMP(o.Postal)=DUMP(l.Postal)"
                                    + " AND DUMP(o.C_Region_ID)=DUMP(l.C_Region_ID) AND DUMP(o.C_Country_ID)=DUMP(l.C_Country_ID)) "
                                    + "WHERE C_BPartner_ID IS NOT NULL AND C_BPartner_Location_ID IS NULL"
                                    + " AND I_IsImported='N'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Found Location=" + no);
            //	Set Location from BPartner
            sql = new StringBuilder("UPDATE I_Invoice o "
                                    + "SET C_BPartner_Location_ID=(SELECT MAX(C_BPartner_Location_ID) FROM C_BPartner_Location l"
                                    + " WHERE l.C_BPartner_ID=o.C_BPartner_ID AND o.AD_Client_ID=l.AD_Client_ID"
                                    + " AND ((l.IsBillTo='Y' AND o.IsSOTrx='Y') OR o.IsSOTrx='N')"
                                    + ") "
                                    + "WHERE C_BPartner_ID IS NOT NULL AND C_BPartner_Location_ID IS NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set BP Location from BP=" + no);
            //
            sql = new StringBuilder("UPDATE I_Invoice "
                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||'ERR=No BP Location, ' "
                                    + "WHERE C_BPartner_ID IS NOT NULL AND C_BPartner_Location_ID IS NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            if (no != 0)
            {
                log.Warning("No BP Location=" + no);
            }

            //	Set Country

            /**
             * sql = new StringBuilder ("UPDATE I_Invoice o "
             + "SET CountryCode=(SELECT CountryCode FROM C_Country c WHERE c.IsDefault='Y'"
             + " AND c.AD_Client_ID IN (0, o.AD_Client_ID) AND ROWNUM=1) "
             + "WHERE C_BPartner_ID IS NULL AND CountryCode IS NULL AND C_Country_ID IS NULL"
             + " AND I_IsImported<>'Y'").Append (clientCheck);
             + no = DataBase.DB.ExecuteQuery(sql.ToString(),null, Get_TrxName());
             + log.Fine("Set Country Default=" + no);
             **/
            sql = new StringBuilder("UPDATE I_Invoice o "
                                    + "SET C_Country_ID=(SELECT C_Country_ID FROM C_Country c"
                                    + " WHERE o.CountryCode=c.CountryCode AND c.IsSummary='N' AND c.AD_Client_ID IN (0, o.AD_Client_ID)) "
                                    + "WHERE C_BPartner_ID IS NULL AND C_Country_ID IS NULL AND CountryCode IS NOT NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set Country=" + no);
            //
            sql = new StringBuilder("UPDATE I_Invoice "
                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||'ERR=Invalid Country, ' "
                                    + "WHERE C_BPartner_ID IS NULL AND C_Country_ID IS NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            if (no != 0)
            {
                log.Warning("Invalid Country=" + no);
            }

            //	Set Region
            sql = new StringBuilder("UPDATE I_Invoice o "
                                    + "Set RegionName=(SELECT MAX(Name) FROM C_Region r"
                                    + " WHERE r.IsDefault='Y' AND r.C_Country_ID=o.C_Country_ID"
                                    + " AND r.AD_Client_ID IN (0, o.AD_Client_ID)) "
                                    + "WHERE C_BPartner_ID IS NULL AND C_Region_ID IS NULL AND RegionName IS NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set Region Default=" + no);
            //
            sql = new StringBuilder("UPDATE I_Invoice o "
                                    + "Set C_Region_ID=(SELECT C_Region_ID FROM C_Region r"
                                    + " WHERE r.Name=o.RegionName AND r.C_Country_ID=o.C_Country_ID"
                                    + " AND r.AD_Client_ID IN (0, o.AD_Client_ID)) "
                                    + "WHERE C_BPartner_ID IS NULL AND C_Region_ID IS NULL AND RegionName IS NOT NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set Region=" + no);
            //
            sql = new StringBuilder("UPDATE I_Invoice o "
                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||'ERR=Invalid Region, ' "
                                    + "WHERE C_BPartner_ID IS NULL AND C_Region_ID IS NULL "
                                    + " AND EXISTS (SELECT * FROM C_Country c"
                                    + " WHERE c.C_Country_ID=o.C_Country_ID AND c.HasRegion='Y')"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            if (no != 0)
            {
                log.Warning("Invalid Region=" + no);
            }

            //	Product
            sql = new StringBuilder("UPDATE I_Invoice o "
                                    + "SET M_Product_ID=(SELECT MAX(M_Product_ID) FROM M_Product p"
                                    + " WHERE o.ProductValue=p.Value AND o.AD_Client_ID=p.AD_Client_ID) "
                                    + "WHERE M_Product_ID IS NULL AND ProductValue 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_Invoice o "
                                    + "SET M_Product_ID=(SELECT MAX(M_Product_ID) FROM M_Product p"
                                    + " WHERE o.UPC=p.UPC AND o.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_Invoice o "
                                    + "SET M_Product_ID=(SELECT MAX(M_Product_ID) FROM M_Product p"
                                    + " WHERE o.SKU=p.SKU AND o.AD_Client_ID=p.AD_Client_ID) "
                                    + "WHERE M_Product_ID IS NULL AND SKU IS NOT NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set Product fom SKU=" + no);
            sql = new StringBuilder("UPDATE I_Invoice "
                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||'ERR=Invalid Product, ' "
                                    + "WHERE M_Product_ID IS NULL AND (ProductValue IS NOT NULL OR UPC IS NOT NULL OR SKU IS NOT NULL)"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            if (no != 0)
            {
                log.Warning("Invalid Product=" + no);
            }

            //	Tax
            sql = new StringBuilder("UPDATE I_Invoice o "
                                    + "SET C_Tax_ID=(SELECT MAX(C_Tax_ID) FROM C_Tax t"
                                    + " WHERE o.TaxIndicator=t.TaxIndicator AND o.AD_Client_ID=t.AD_Client_ID) "
                                    + "WHERE C_Tax_ID IS NULL AND TaxIndicator IS NOT NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set Tax=" + no);
            sql = new StringBuilder("UPDATE I_Invoice "
                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||'ERR=Invalid Tax, ' "
                                    + "WHERE C_Tax_ID IS NULL AND TaxIndicator IS NOT NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            if (no != 0)
            {
                log.Warning("Invalid Tax=" + no);
            }

            Commit();

            //	-- New BPartner ---------------------------------------------------

            //	Go through Invoice Records w/o C_BPartner_ID
            sql = new StringBuilder("SELECT * FROM I_Invoice "
                                    + "WHERE I_IsImported='N' AND C_BPartner_ID IS NULL").Append(clientCheck);
            IDataReader idr = null;

            try
            {
                //PreparedStatement pstmt = DataBase.prepareStatement (sql.ToString(), Get_TrxName());
                idr = DataBase.DB.ExecuteReader(sql.ToString(), null, Get_TrxName());
                while (idr.Read())
                {
                    X_I_Invoice imp = new X_I_Invoice(GetCtx(), idr, Get_TrxName());
                    if (imp.GetBPartnerValue() == null)
                    {
                        if (imp.GetEMail() != null)
                        {
                            imp.SetBPartnerValue(imp.GetEMail());
                        }
                        else if (imp.GetName() != null)
                        {
                            imp.SetBPartnerValue(imp.GetName());
                        }
                        else
                        {
                            continue;
                        }
                    }
                    if (imp.GetName() == null)
                    {
                        if (imp.GetContactName() != null)
                        {
                            imp.SetName(imp.GetContactName());
                        }
                        else
                        {
                            imp.SetName(imp.GetBPartnerValue());
                        }
                    }
                    //	BPartner
                    MBPartner bp = MBPartner.Get(GetCtx(), imp.GetBPartnerValue());
                    if (bp == null)
                    {
                        bp = new MBPartner(GetCtx(), -1, Get_TrxName());
                        bp.SetClientOrg(imp.GetAD_Client_ID(), imp.GetAD_Org_ID());
                        bp.SetValue(imp.GetBPartnerValue());
                        bp.SetName(imp.GetName());
                        if (!bp.Save())
                        {
                            continue;
                        }
                    }
                    imp.SetC_BPartner_ID(bp.GetC_BPartner_ID());

                    //	BP Location
                    MBPartnerLocation   bpl  = null;
                    MBPartnerLocation[] bpls = bp.GetLocations(true);
                    for (int i = 0; bpl == null && i < bpls.Length; i++)
                    {
                        if (imp.GetC_BPartner_Location_ID() == bpls[i].GetC_BPartner_Location_ID())
                        {
                            bpl = bpls[i];
                        }
                        //	Same Location ID
                        else if (imp.GetC_Location_ID() == bpls[i].GetC_Location_ID())
                        {
                            bpl = bpls[i];
                        }
                        //	Same Location Info
                        else if (imp.GetC_Location_ID() == 0)
                        {
                            MLocation loc = bpl.GetLocation(false);
                            if (loc.Equals(imp.GetC_Country_ID(), imp.GetC_Region_ID(),
                                           imp.GetPostal(), "", imp.GetCity(),
                                           imp.GetAddress1(), imp.GetAddress2()))
                            {
                                bpl = bpls[i];
                            }
                        }
                    }
                    if (bpl == null)
                    {
                        //	New Location
                        MLocation loc = new MLocation(GetCtx(), 0, Get_TrxName());
                        loc.SetAddress1(imp.GetAddress1());
                        loc.SetAddress2(imp.GetAddress2());
                        loc.SetCity(imp.GetCity());
                        loc.SetPostal(imp.GetPostal());
                        if (imp.GetC_Region_ID() != 0)
                        {
                            loc.SetC_Region_ID(imp.GetC_Region_ID());
                        }
                        loc.SetC_Country_ID(imp.GetC_Country_ID());
                        if (!loc.Save())
                        {
                            continue;
                        }
                        //
                        bpl = new MBPartnerLocation(bp);
                        bpl.SetC_Location_ID(imp.GetC_Location_ID());
                        if (!bpl.Save())
                        {
                            continue;
                        }
                    }
                    imp.SetC_Location_ID(bpl.GetC_Location_ID());
                    imp.SetC_BPartner_Location_ID(bpl.GetC_BPartner_Location_ID());

                    //	User/Contact
                    if (imp.GetContactName() != null ||
                        imp.GetEMail() != null ||
                        imp.GetPhone() != null)
                    {
                        MUser[] users = bp.GetContacts(true);
                        MUser   user  = null;
                        for (int i = 0; user == null && i < users.Length; i++)
                        {
                            String name = users[i].GetName();
                            if (name.Equals(imp.GetContactName()) ||
                                name.Equals(imp.GetName()))
                            {
                                user = users[i];
                                imp.SetAD_User_ID(user.GetAD_User_ID());
                            }
                        }
                        if (user == null)
                        {
                            user = new MUser(bp);
                            if (imp.GetContactName() == null)
                            {
                                user.SetName(imp.GetName());
                            }
                            else
                            {
                                user.SetName(imp.GetContactName());
                            }
                            user.SetEMail(imp.GetEMail());
                            user.SetPhone(imp.GetPhone());
                            if (user.Save())
                            {
                                imp.SetAD_User_ID(user.GetAD_User_ID());
                            }
                        }
                    }
                    imp.Save();
                }       //	for all new BPartners
                idr.Close();

                //
            }
            catch (Exception e)
            {
                if (idr != null)
                {
                    idr.Close();
                }
                log.Log(Level.SEVERE, "CreateBP", e);
            }
            sql = new StringBuilder("UPDATE I_Invoice "
                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||'ERR=No BPartner, ' "
                                    + "WHERE C_BPartner_ID IS NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            if (no != 0)
            {
                log.Warning("No BPartner=" + no);
            }

            Commit();

            //	-- New Invoices -----------------------------------------------------

            int noInsert     = 0;
            int noInsertLine = 0;

            //	Go through Invoice Records w/o
            sql = new StringBuilder("SELECT * FROM I_Invoice "
                                    + "WHERE I_IsImported='N'").Append(clientCheck)
                  .Append(" ORDER BY C_BPartner_ID, C_BPartner_Location_ID, I_Invoice_ID");
            try
            {
                //PreparedStatement pstmt = DataBase.prepareStatement (sql.ToString(), Get_TrxName());
                idr = DataBase.DB.ExecuteReader(sql.ToString(), null, Get_TrxName());
                //	Group Change
                int    oldC_BPartner_ID          = 0;
                int    oldC_BPartner_Location_ID = 0;
                String oldDocumentNo             = "";
                //
                MInvoice invoice = null;
                int      lineNo  = 0;
                while (idr.Read())
                {
                    X_I_Invoice imp           = new X_I_Invoice(GetCtx(), idr, null);
                    String      cmpDocumentNo = imp.GetDocumentNo();
                    if (cmpDocumentNo == null)
                    {
                        cmpDocumentNo = "";
                    }
                    //	New Invoice
                    if (oldC_BPartner_ID != imp.GetC_BPartner_ID() ||
                        oldC_BPartner_Location_ID != imp.GetC_BPartner_Location_ID() ||
                        !oldDocumentNo.Equals(cmpDocumentNo))
                    {
                        if (invoice != null)
                        {
                            invoice.ProcessIt(_docAction);
                            invoice.Save();
                        }
                        //	Group Change
                        oldC_BPartner_ID          = imp.GetC_BPartner_ID();
                        oldC_BPartner_Location_ID = imp.GetC_BPartner_Location_ID();
                        oldDocumentNo             = imp.GetDocumentNo();
                        if (oldDocumentNo == null)
                        {
                            oldDocumentNo = "";
                        }
                        //
                        invoice = new MInvoice(GetCtx(), 0, null);
                        invoice.SetClientOrg(imp.GetAD_Client_ID(), imp.GetAD_Org_ID());
                        invoice.SetC_DocTypeTarget_ID(imp.GetC_DocType_ID(), true);
                        if (imp.GetDocumentNo() != null)
                        {
                            invoice.SetDocumentNo(imp.GetDocumentNo());
                        }
                        //
                        invoice.SetC_BPartner_ID(imp.GetC_BPartner_ID());
                        invoice.SetC_BPartner_Location_ID(imp.GetC_BPartner_Location_ID());
                        if (imp.GetAD_User_ID() != 0)
                        {
                            invoice.SetAD_User_ID(imp.GetAD_User_ID());
                        }
                        //
                        if (imp.GetDescription() != null)
                        {
                            invoice.SetDescription(imp.GetDescription());
                        }
                        if (imp.GetPaymentRule() != null)
                        {
                            invoice.SetPaymentRule(imp.GetPaymentRule());
                        }
                        invoice.SetC_PaymentTerm_ID(imp.GetC_PaymentTerm_ID());
                        invoice.SetM_PriceList_ID(imp.GetM_PriceList_ID());

                        MPriceList pl = MPriceList.Get(GetCtx(), imp.GetM_PriceList_ID(), Get_TrxName());
                        invoice.SetIsTaxIncluded(pl.IsTaxIncluded());

                        //	SalesRep from Import or the person running the import
                        if (imp.GetSalesRep_ID() != 0)
                        {
                            invoice.SetSalesRep_ID(imp.GetSalesRep_ID());
                        }
                        if (invoice.GetSalesRep_ID() == 0)
                        {
                            invoice.SetSalesRep_ID(GetAD_User_ID());
                        }
                        //
                        if (imp.GetAD_OrgTrx_ID() != 0)
                        {
                            invoice.SetAD_OrgTrx_ID(imp.GetAD_OrgTrx_ID());
                        }
                        if (imp.GetC_Activity_ID() != 0)
                        {
                            invoice.SetC_Activity_ID(imp.GetC_Activity_ID());
                        }
                        if (imp.GetC_Campaign_ID() != 0)
                        {
                            invoice.SetC_Campaign_ID(imp.GetC_Campaign_ID());
                        }
                        if (imp.GetC_Project_ID() != 0)
                        {
                            invoice.SetC_Project_ID(imp.GetC_Project_ID());
                        }
                        //
                        if (imp.GetDateInvoiced() != null)
                        {
                            invoice.SetDateInvoiced(imp.GetDateInvoiced());
                        }
                        if (imp.GetDateAcct() != null)
                        {
                            invoice.SetDateAcct(imp.GetDateAcct());
                        }
                        //
                        invoice.Save();
                        noInsert++;
                        lineNo = 10;
                    }
                    imp.SetC_Invoice_ID(invoice.GetC_Invoice_ID());
                    //	New InvoiceLine
                    MInvoiceLine line = new MInvoiceLine(invoice);
                    if (imp.GetLineDescription() != null)
                    {
                        line.SetDescription(imp.GetLineDescription());
                    }
                    line.SetLine(lineNo);
                    lineNo += 10;
                    if (imp.GetM_Product_ID() != 0)
                    {
                        line.SetM_Product_ID(imp.GetM_Product_ID(), true);
                    }
                    line.SetQty(imp.GetQtyOrdered());
                    line.SetPrice();
                    Decimal?price = (Decimal?)imp.GetPriceActual();
                    if (price != null && Env.ZERO.CompareTo(price) != 0)
                    {
                        line.SetPrice(price.Value);
                    }
                    if (imp.GetC_Tax_ID() != 0)
                    {
                        line.SetC_Tax_ID(imp.GetC_Tax_ID());
                    }
                    else
                    {
                        line.SetTax();
                        imp.SetC_Tax_ID(line.GetC_Tax_ID());
                    }
                    Decimal?taxAmt = (Decimal?)imp.GetTaxAmt();
                    if (taxAmt != null && Env.ZERO.CompareTo(taxAmt) != 0)
                    {
                        line.SetTaxAmt(taxAmt);
                    }
                    line.Save();
                    //
                    imp.SetC_InvoiceLine_ID(line.GetC_InvoiceLine_ID());
                    imp.SetI_IsImported(X_I_Invoice.I_ISIMPORTED_Yes);
                    imp.SetProcessed(true);
                    //
                    if (imp.Save())
                    {
                        noInsertLine++;
                    }
                }
                if (invoice != null)
                {
                    invoice.ProcessIt(_docAction);
                    invoice.Save();
                }
                idr.Close();
            }
            catch (Exception e)
            {
                if (idr != null)
                {
                    idr.Close();
                }
                log.Log(Level.SEVERE, "CreateInvoice", e);
            }

            //	Set Error to indicator to not imported
            sql = new StringBuilder("UPDATE I_Invoice "
                                    + "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), "@C_Invoice_ID@: @Inserted@");
            AddLog(0, null, Utility.Util.GetValueOfDecimal(noInsertLine), "@C_InvoiceLine_ID@: @Inserted@");
            return("");
        }       //	doIt
Ejemplo n.º 4
0
        }                                  //	prepare

        /// <summary>
        /// Perrform Process.
        /// </summary>
        /// <returns>message</returns>
        protected override String DoIt()
        {
            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_BPartner "
                                        + "WHERE I_IsImported='Y'").Append(clientCheck);
                no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
                log.Fine("Delete Old Impored =" + no);
            }

            //	Set Client, Org, IsActive, Created/Updated
            sql = new StringBuilder("UPDATE I_BPartner "
                                    + "SET AD_Client_ID = COALESCE (AD_Client_ID, ").Append(_AD_Client_ID).Append("),"
                                                                                                                  + " AD_Org_ID = COALESCE (AD_Org_ID, 0),"
                                                                                                                  + " IsActive = COALESCE (IsActive, 'Y'),"
                                                                                                                  + " Created = COALESCE (Created, SysDate),"
                                                                                                                  + " CreatedBy = COALESCE (CreatedBy, 0),"
                                                                                                                  + " Updated = COALESCE (Updated, SysDate),"
                                                                                                                  + " UpdatedBy = COALESCE (UpdatedBy, 0),"
                                                                                                                  + " I_ErrorMsg = NULL,"
                                                                                                                  + " I_IsImported = 'N' "
                                                                                                                  + "WHERE I_IsImported<>'Y' OR I_IsImported IS NULL");
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Reset=" + no);

            //	Set BP_Group
            sql = new StringBuilder("UPDATE I_BPartner i "
                                    + "SET GroupValue=(SELECT MAX(Value) FROM C_BP_Group g WHERE g.IsDefault='Y'"
                                    + " AND g.AD_Client_ID=i.AD_Client_ID) ");
            sql.Append("WHERE GroupValue IS NULL AND C_BP_Group_ID IS NULL"
                       + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set Group Default=" + no);
            //
            sql = new StringBuilder("UPDATE I_BPartner i "
                                    + "SET C_BP_Group_ID=(SELECT C_BP_Group_ID FROM C_BP_Group g"
                                    + " WHERE i.GroupValue=g.Value AND g.AD_Client_ID=i.AD_Client_ID) "
                                    + "WHERE C_BP_Group_ID IS NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set Group=" + no);
            //
            String ts = DataBase.DB.IsPostgreSQL() ? "COALESCE(I_ErrorMsg,'')" : "I_ErrorMsg";  //java bug, it could not be used directly

            sql = new StringBuilder("UPDATE I_BPartner "
                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||'ERR=Invalid Group, ' "
                                    + "WHERE C_BP_Group_ID IS NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Config("Invalid Group=" + no);

            //	Set Country

            /**
             * sql = new StringBuilder ("UPDATE I_BPartner i "
             + "SET CountryCode=(SELECT CountryCode FROM C_Country c WHERE c.IsDefault='Y'"
             + " AND c.AD_Client_ID IN (0, i.AD_Client_ID) AND ROWNUM=1) "
             + "WHERE CountryCode IS NULL AND C_Country_ID IS NULL"
             + " AND I_IsImported<>'Y'").Append(clientCheck);
             + no = DataBase.DB.ExecuteQuery(sql.ToString(),null, Get_TrxName());
             + log.Fine("Set Country Default=" + no);
             **/
            //
            sql = new StringBuilder("UPDATE I_BPartner i "
                                    + "SET C_Country_ID=(SELECT C_Country_ID FROM C_Country c"
                                    + " WHERE i.CountryCode=c.CountryCode AND c.IsSummary='N' AND c.AD_Client_ID IN (0, i.AD_Client_ID)) "
                                    + "WHERE C_Country_ID IS NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set Country=" + no);
            //
            sql = new StringBuilder("UPDATE I_BPartner "
                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||'ERR=Invalid Country, ' "
                                    + "WHERE C_Country_ID IS NULL AND (City IS NOT NULL OR Address1 IS NOT NULL)"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Config("Invalid Country=" + no);

            //	Set Region
            sql = new StringBuilder("UPDATE I_BPartner i "
                                    + "Set RegionName=(SELECT Name FROM C_Region r"
                                    + " WHERE r.IsDefault='Y' AND r.C_Country_ID=i.C_Country_ID"
                                    + " AND r.AD_Client_ID IN (0, i.AD_Client_ID)) ");

            /*
             * if (DataBase.isOracle()) //jz
             * {
             *  sql.Append(" AND ROWNUM=1) ");
             * }
             * else
             *  sql.Append(" AND r.UPDATED IN (SELECT MAX(UPDATED) FROM C_Region r1"
             + " WHERE r1.IsDefault='Y' AND r1.C_Country_ID=i.C_Country_ID"
             + " AND r1.AD_Client_ID IN (0, i.AD_Client_ID) ");
             */
            sql.Append("WHERE RegionName IS NULL AND C_Region_ID IS NULL"
                       + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set Region Default=" + no);
            //
            sql = new StringBuilder("UPDATE I_BPartner i "
                                    + "Set C_Region_ID=(SELECT C_Region_ID FROM C_Region r"
                                    + " WHERE r.Name=i.RegionName AND r.C_Country_ID=i.C_Country_ID"
                                    + " AND r.AD_Client_ID IN (0, i.AD_Client_ID)) "
                                    + "WHERE C_Region_ID IS NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set Region=" + no);
            //
            sql = new StringBuilder("UPDATE I_BPartner i "
                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||'ERR=Invalid Region, ' "
                                    + "WHERE C_Region_ID IS NULL "
                                    + " AND EXISTS (SELECT * FROM C_Country c"
                                    + " WHERE c.C_Country_ID=i.C_Country_ID AND c.HasRegion='Y')"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Config("Invalid Region=" + no);

            //	Set Greeting
            sql = new StringBuilder("UPDATE I_BPartner i "
                                    + "SET C_Greeting_ID=(SELECT C_Greeting_ID FROM C_Greeting g"
                                    + " WHERE i.BPContactGreeting=g.Name AND g.AD_Client_ID IN (0, i.AD_Client_ID)) "
                                    + "WHERE C_Greeting_ID IS NULL AND BPContactGreeting IS NOT NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set Greeting=" + no);
            //
            sql = new StringBuilder("UPDATE I_BPartner i "
                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||'ERR=Invalid Greeting, ' "
                                    + "WHERE C_Greeting_ID IS NULL AND BPContactGreeting IS NOT NULL"
                                    + " AND I_IsImported<>'Y'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Config("Invalid Greeting=" + no);

            //	Existing User ?
            sql = new StringBuilder("UPDATE I_BPartner i "
                                    + "SET (C_BPartner_ID,AD_User_ID)="
                                    + "(SELECT C_BPartner_ID,AD_User_ID FROM AD_User u "
                                    + "WHERE i.EMail=u.EMail AND u.AD_Client_ID=i.AD_Client_ID) "
                                    + "WHERE i.EMail IS NOT NULL AND I_IsImported='N'").Append(clientCheck);

            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Found EMail User="******"UPDATE I_BPartner i "
                                    + "SET C_BPartner_ID=(SELECT C_BPartner_ID FROM C_BPartner p"
                                    + " WHERE i.Value=p.Value AND p.AD_Client_ID=i.AD_Client_ID) "
                                    + "WHERE C_BPartner_ID IS NULL AND Value IS NOT NULL"
                                    + " AND I_IsImported='N'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Found BPartner=" + no);

            //	Existing Contact ? Match Name
            sql = new StringBuilder("UPDATE I_BPartner i "
                                    + "SET AD_User_ID=(SELECT AD_User_ID FROM AD_User c"
                                    + " WHERE i.ContactName=c.Name AND i.C_BPartner_ID=c.C_BPartner_ID AND c.AD_Client_ID=i.AD_Client_ID) "
                                    + "WHERE C_BPartner_ID IS NOT NULL AND AD_User_ID IS NULL AND ContactName IS NOT NULL"
                                    + " AND I_IsImported='N'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Found Contact=" + no);

            //	Existing Location ? Exact Match
            sql = new StringBuilder("UPDATE I_BPartner i "
                                    + "SET C_BPartner_Location_ID=(SELECT C_BPartner_Location_ID"
                                    + " FROM C_BPartner_Location bpl INNER JOIN C_Location l ON (bpl.C_Location_ID=l.C_Location_ID)"
                                    + " WHERE i.C_BPartner_ID=bpl.C_BPartner_ID AND bpl.AD_Client_ID=i.AD_Client_ID"
                                    + " AND DUMP(i.Address1)=DUMP(l.Address1) AND DUMP(i.Address2)=DUMP(l.Address2)"
                                    + " AND DUMP(i.City)=DUMP(l.City) AND DUMP(i.Postal)=DUMP(l.Postal) AND DUMP(i.Postal_Add)=DUMP(l.Postal_Add)"
                                    + " AND DUMP(i.C_Region_ID)=DUMP(l.C_Region_ID) AND DUMP(i.C_Country_ID)=DUMP(l.C_Country_ID)) "
                                    + "WHERE C_BPartner_ID IS NOT NULL AND C_BPartner_Location_ID IS NULL"
                                    + " AND I_IsImported='N'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Found Location=" + no);

            //	Interest Area
            sql = new StringBuilder("UPDATE I_BPartner i "
                                    + "SET R_InterestArea_ID=(SELECT R_InterestArea_ID FROM R_InterestArea ia "
                                    + "WHERE i.InterestAreaName=ia.Name AND ia.AD_Client_ID=i.AD_Client_ID) "
                                    + "WHERE R_InterestArea_ID IS NULL AND InterestAreaName IS NOT NULL"
                                    + " AND I_IsImported='N'").Append(clientCheck);
            no = DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
            log.Fine("Set Interest Area=" + no);


            Commit();
            //	-------------------------------------------------------------------
            int         noInsert = 0;
            int         noUpdate = 0;
            IDataReader idr      = null;

            //	Go through Records
            sql = new StringBuilder("SELECT * FROM I_BPartner "
                                    + "WHERE I_IsImported='N'").Append(clientCheck);
            try
            {
                //PreparedStatement pstmt = DataBase.prepareStatement(sql.ToString(), Get_TrxName());
                //ResultSet rs = pstmt.executeQuery();
                idr = DataBase.DB.ExecuteReader(sql.ToString(), null, Get_TrxName());
                while (idr.Read())
                {
                    X_I_BPartner impBP = new X_I_BPartner(GetCtx(), idr, Get_TrxName());
                    log.Fine("I_BPartner_ID=" + impBP.GetI_BPartner_ID()
                             + ", C_BPartner_ID=" + impBP.GetC_BPartner_ID()
                             + ", C_BPartner_Location_ID=" + impBP.GetC_BPartner_Location_ID()
                             + ", AD_User_ID=" + impBP.GetAD_User_ID());


                    //	****	Create/Update BPartner	****
                    MBPartner bp = null;
                    if (impBP.GetC_BPartner_ID() == 0)  //	Insert new BPartner
                    {
                        bp = new MBPartner(impBP);
                        if (bp.Save())
                        {
                            impBP.SetC_BPartner_ID(bp.GetC_BPartner_ID());
                            log.Finest("Insert BPartner - " + bp.GetC_BPartner_ID());
                            noInsert++;
                        }
                        else
                        {
                            sql = new StringBuilder("UPDATE I_BPartner i "
                                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||")
                                  .Append("Cannot Insert BPartner")
                                  .Append("WHERE I_BPartner_ID=").Append(impBP.GetI_BPartner_ID());
                            DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
                            continue;
                        }
                    }
                    else                                //	Update existing BPartner
                    {
                        bp = new MBPartner(GetCtx(), impBP.GetC_BPartner_ID(), Get_TrxName());
                        //	if (impBP.getValue() != null)			//	not to overwite
                        //		bp.setValue(impBP.getValue());
                        if (impBP.GetName() != null)
                        {
                            bp.SetName(impBP.GetName());
                            bp.SetName2(impBP.GetName2());
                        }
                        if (impBP.GetDUNS() != null)
                        {
                            bp.SetDUNS(impBP.GetDUNS());
                        }
                        if (impBP.GetTaxID() != null)
                        {
                            bp.SetTaxID(impBP.GetTaxID());
                        }
                        if (impBP.GetNAICS() != null)
                        {
                            bp.SetNAICS(impBP.GetNAICS());
                        }
                        if (impBP.GetC_BP_Group_ID() != 0)
                        {
                            bp.SetC_BP_Group_ID(impBP.GetC_BP_Group_ID());
                        }
                        if (impBP.GetDescription() != null)
                        {
                            bp.SetDescription(impBP.GetDescription());
                        }
                        //
                        if (bp.Save())
                        {
                            log.Finest("Update BPartner - " + bp.GetC_BPartner_ID());
                            noUpdate++;
                        }
                        else
                        {
                            sql = new StringBuilder("UPDATE I_BPartner i "
                                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||")
                                  .Append("' Cannot Update BPartner' ") //jz
                                  .Append("WHERE I_BPartner_ID=").Append(impBP.GetI_BPartner_ID());
                            DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
                            continue;
                        }
                    }

                    //	****	Create/Update BPartner Location	****
                    MBPartnerLocation bpl = null;
                    if (impBP.GetC_BPartner_Location_ID() != 0)         //	Update Location
                    {
                        bpl = new MBPartnerLocation(GetCtx(), impBP.GetC_BPartner_Location_ID(), Get_TrxName());
                        MLocation location = new MLocation(GetCtx(), bpl.GetC_Location_ID(), Get_TrxName());
                        location.SetC_Country_ID(impBP.GetC_Country_ID());
                        location.SetC_Region_ID(impBP.GetC_Region_ID());
                        location.SetCity(impBP.GetCity());
                        location.SetAddress1(impBP.GetAddress1());
                        location.SetAddress2(impBP.GetAddress2());
                        location.SetPostal(impBP.GetPostal());
                        location.SetPostal_Add(impBP.GetPostal_Add());
                        location.SetRegionName(impBP.GetRegionName());
                        if (!location.Save())
                        {
                            log.Warning("Location not updated");
                        }
                        else
                        {
                            bpl.SetC_Location_ID(location.GetC_Location_ID());
                        }
                        if (impBP.GetPhone() != null)
                        {
                            bpl.SetPhone(impBP.GetPhone());
                        }
                        if (impBP.GetPhone2() != null)
                        {
                            bpl.SetPhone2(impBP.GetPhone2());
                        }
                        if (impBP.GetFax() != null)
                        {
                            bpl.SetFax(impBP.GetFax());
                        }
                        bpl.Save();
                    }
                    else        //	New Location
                    if (impBP.GetC_Country_ID() != 0 &&
                        impBP.GetAddress1() != null &&
                        impBP.GetCity() != null)
                    {
                        MLocation location = new MLocation(GetCtx(), impBP.GetC_Country_ID(),
                                                           impBP.GetC_Region_ID(), impBP.GetCity(), Get_TrxName());
                        location.SetAddress1(impBP.GetAddress1());
                        location.SetAddress2(impBP.GetAddress2());
                        location.SetPostal(impBP.GetPostal());
                        location.SetPostal_Add(impBP.GetPostal_Add());
                        location.SetRegionName(impBP.GetRegionName());
                        if (location.Save())
                        {
                            log.Finest("Insert Location - " + location.GetC_Location_ID());
                        }
                        else
                        {
                            Rollback();
                            noInsert--;
                            sql = new StringBuilder("UPDATE I_BPartner i "
                                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||")
                                  .Append("Cannot Insert Location")
                                  .Append("WHERE I_BPartner_ID=").Append(impBP.GetI_BPartner_ID());
                            DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
                            continue;
                        }
                        //
                        bpl = new MBPartnerLocation(bp);
                        bpl.SetC_Location_ID(location.GetC_Location_ID());
                        bpl.SetPhone(impBP.GetPhone());
                        bpl.SetPhone2(impBP.GetPhone2());
                        bpl.SetFax(impBP.GetFax());
                        if (bpl.Save())
                        {
                            log.Finest("Insert BP Location - " + bpl.GetC_BPartner_Location_ID());
                            impBP.SetC_BPartner_Location_ID(bpl.GetC_BPartner_Location_ID());
                        }
                        else
                        {
                            Rollback();
                            noInsert--;
                            sql = new StringBuilder("UPDATE I_BPartner i "
                                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||")
                                  .Append("Cannot Insert BPLocation")
                                  .Append("WHERE I_BPartner_ID=").Append(impBP.GetI_BPartner_ID());
                            DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
                            continue;
                        }
                    }

                    //	****	Create/Update Contact	****
                    MUser user = null;
                    if (impBP.GetAD_User_ID() != 0)
                    {
                        user = new MUser(GetCtx(), impBP.GetAD_User_ID(), Get_TrxName());
                        if (user.GetC_BPartner_ID() == 0)
                        {
                            user.SetC_BPartner_ID(bp.GetC_BPartner_ID());
                        }
                        else if (user.GetC_BPartner_ID() != bp.GetC_BPartner_ID())
                        {
                            Rollback();
                            noInsert--;
                            sql = new StringBuilder("UPDATE I_BPartner i "
                                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||")
                                  .Append("BP of User <> BP")
                                  .Append("WHERE I_BPartner_ID=").Append(impBP.GetI_BPartner_ID());
                            DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
                            continue;
                        }
                        if (impBP.GetC_Greeting_ID() != 0)
                        {
                            user.SetC_Greeting_ID(impBP.GetC_Greeting_ID());
                        }
                        String name = impBP.GetContactName();
                        if (name == null || name.Length == 0)
                        {
                            name = impBP.GetEMail();
                        }
                        user.SetName(name);
                        if (impBP.GetTitle() != null)
                        {
                            user.SetTitle(impBP.GetTitle());
                        }
                        if (impBP.GetContactDescription() != null)
                        {
                            user.SetDescription(impBP.GetContactDescription());
                        }
                        if (impBP.GetComments() != null)
                        {
                            user.SetComments(impBP.GetComments());
                        }
                        if (impBP.GetPhone() != null)
                        {
                            user.SetPhone(impBP.GetPhone());
                        }
                        if (impBP.GetPhone2() != null)
                        {
                            user.SetPhone2(impBP.GetPhone2());
                        }
                        if (impBP.GetFax() != null)
                        {
                            user.SetFax(impBP.GetFax());
                        }
                        if (impBP.GetEMail() != null)
                        {
                            user.SetEMail(impBP.GetEMail());
                        }
                        if (impBP.GetBirthday() != null)
                        {
                            user.SetBirthday(impBP.GetBirthday());
                        }
                        if (bpl != null)
                        {
                            user.SetC_BPartner_Location_ID(bpl.GetC_BPartner_Location_ID());
                        }
                        if (user.Save())
                        {
                            log.Finest("Update BP Contact - " + user.GetAD_User_ID());
                        }
                        else
                        {
                            Rollback();
                            noInsert--;
                            sql = new StringBuilder("UPDATE I_BPartner i "
                                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||")
                                  .Append("Cannot Update BP Contact")
                                  .Append("WHERE I_BPartner_ID=").Append(impBP.GetI_BPartner_ID());
                            DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
                            continue;
                        }
                    }
                    else        //	New Contact
                    if (impBP.GetContactName() != null || impBP.GetEMail() != null)
                    {
                        user = new MUser(bp);
                        if (impBP.GetC_Greeting_ID() != 0)
                        {
                            user.SetC_Greeting_ID(impBP.GetC_Greeting_ID());
                        }
                        String name = impBP.GetContactName();
                        if (name == null || name.Length == 0)
                        {
                            name = impBP.GetEMail();
                        }
                        user.SetName(name);
                        user.SetTitle(impBP.GetTitle());
                        user.SetDescription(impBP.GetContactDescription());
                        user.SetComments(impBP.GetComments());
                        user.SetPhone(impBP.GetPhone());
                        user.SetPhone2(impBP.GetPhone2());
                        user.SetFax(impBP.GetFax());
                        user.SetEMail(impBP.GetEMail());
                        user.SetBirthday(impBP.GetBirthday());
                        if (bpl != null)
                        {
                            user.SetC_BPartner_Location_ID(bpl.GetC_BPartner_Location_ID());
                        }
                        if (user.Save())
                        {
                            log.Finest("Insert BP Contact - " + user.GetAD_User_ID());
                            impBP.SetAD_User_ID(user.GetAD_User_ID());
                        }
                        else
                        {
                            Rollback();
                            noInsert--;
                            sql = new StringBuilder("UPDATE I_BPartner i "
                                                    + "SET I_IsImported='E', I_ErrorMsg=" + ts + "||")
                                  .Append("Cannot Insert BPContact")
                                  .Append("WHERE I_BPartner_ID=").Append(impBP.GetI_BPartner_ID());
                            DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName());
                            continue;
                        }
                    }

                    //	Interest Area
                    if (impBP.GetR_InterestArea_ID() != 0 && user != null)
                    {
                        MContactInterest ci = MContactInterest.Get(GetCtx(),
                                                                   impBP.GetR_InterestArea_ID(), user.GetAD_User_ID(),
                                                                   true, Get_TrxName());
                        ci.Save();              //	don't subscribe or re-activate
                    }
                    //
                    impBP.SetI_IsImported(X_I_BPartner.I_ISIMPORTED_Yes);
                    impBP.SetProcessed(true);
                    impBP.SetProcessing(false);
                    impBP.Save();
                    Commit();
                }       //	for all I_Product
                idr.Close();
            }
            catch (Exception e)
            {
                if (idr != null)
                {
                    idr.Close();
                }
                log.Log(Level.SEVERE, "", e);
                Rollback();
            }

            //	Set Error to indicator to not imported
            sql = new StringBuilder("UPDATE I_BPartner "
                                    + "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), "@C_BPartner_ID@: @Inserted@");
            AddLog(0, null, Utility.Util.GetValueOfDecimal(noUpdate), "@C_BPartner_ID@: @Updated@");
            return("");
        }       //	doIt