using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using MySql.Data.MySqlClient;
using CLS_DL;
using CLS_PL;
using CLS_BL;
using SUNWEB;
namespace CLS_BL
{

    public class BL_USER
    {
        DataTable DT = new DataTable();
        MYSQLDB MysqlObj = new MYSQLDB();
        MYSQLCLOUD ObjCloud = new MYSQLCLOUD();
        BL_ErrorLog ObjLog = new BL_ErrorLog();
        string MaxId = string.Empty;
        public DataTable GetUserList1()
        {
            try
            {
                DT = MysqlObj.GetData_DT("select  * from testdata ");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error(ex.Message);
                
            }
            return DT;

        }
        // Get PageMaster Data 
        /// <summary>
        /// This Function For Selecting PageMaster Data
        /// </summary>
        /// <returns></returns>
        public DataTable GetPageMasterList()
        {
            try
            {
                DT = MysqlObj.GetData_DT("select  * from portaldb.PageMaster ");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error(ex.Message);
                
                
            }
            return DT;


        }
        // Get CompanyList with Status 'A' Which Means - Active Companies
        /// <summary>
        ///  This Function Get the Active Companies List Base Status 'A'
        /// </summary>
        /// <returns></returns>
        public DataTable GetCompanyList()
        {
            try
            {
                DT = MysqlObj.GetData_DT("SELECT distinct CM.Cm_id ,CM.cm_name FROM portaldb.companymaster as CM inner join portaldb.companydetails as CD ON CM.cm_id=CD.cm_id where CD.status='A'");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error(ex.Message);
                
            }
            return DT;

        }

        public DataTable GetBranchList(string Cm_Id)
        {
            try
            {
                DT = MysqlObj.GetData_DT("SELECT distinct CD.cd_id as cd_id, CD.cd_Branchname FROM portaldb.companymaster as CM inner join portaldb.companydetails as CD ON CM.cm_id=CD.cm_id where CD.status ='A' AND CM.cm_id=" + Cm_Id + " UNION SELECT cd_id ,cd_BranchName from portaldb.companydetails where cm_id=" + Cm_Id + ";");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error(ex.Message);
            }
            return DT;

        }

        public DataTable GetBranchInfo(string cd_Id)
        {
            try
            {
                DT = MysqlObj.GetData_DT("select  C.cm_id AS cm_id,D.cd_id AS cd_id,j.im_id,I.id_id as id_id ,C.cm_name AS cm_name,D.cd_branchname as cd_branchname,j.im_invoiceno as InvoiceNO,j.im_invoicedate as InvoiceDt ,D.status AS Status ,D.cd_regdate AS RegDt,D.cd_expdate AS ExpDt,P.Portaltype AS Portaltype,P.Portaltypeid as Portaltypeid,I.id_conyear as Yrs,I.id_conmonth as Mons,I.id_condays as Days,D.cd_hq as cd_hq,D.cd_branchAlias as cd_branchAlias, D.cd_branchid as cd_branchid     from portaldb.companymaster AS C inner  JOIN portaldb.companydetails as D ON C.cm_id=D.cm_id inner join  portaldb.invoicedetails as I  ON D.cd_id=i.cd_id inner join portaldb.invoicemaster as j on I.im_id=j.im_id    INNER JOIN portaldb.PORTALTYPEHEADER AS P ON I.portaltypeid =p.portaltypeid   where D.status='A' and D.cd_id=" + cd_Id + " ;");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error(ex.Message);
            }
            return DT;

        }
        public DataTable GetBranchInfoOnHQ(string cd_Id)
        {

            try
            {
                DT = MysqlObj.GetData_DT("select  C.cm_id AS cm_id,D.cd_id AS cd_id,C.cm_name AS cm_name,D.cd_branchname as cd_branchname,D.status AS Status ,D.cd_regdate AS RegDt,D.cd_expdate AS ExpDt,D.cd_hq as cd_hq,D.cd_branchAlias as cd_branchAlias, D.cd_branchid as cd_branchid     from portaldb.companymaster AS C inner  JOIN portaldb.companydetails as D ON C.cm_id=D.cm_id  where D.status='P' and D.cd_id=" + cd_Id + " ;");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error (ex.Message);
            }
            return DT;

        }

        public string GetMaxCustId()
        {
            try
            {

                MaxId = MysqlObj.ExecuteScalar("SELECT MAX(UserId) FROM portaldb.usermaster;");

            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error(ex.Message);
            }
            return MaxId;
        }

        public DataTable GetPageRightsList()
        {
            try
            {
                DT = MysqlObj.GetData_DT("select * from portaldb.pagemaster ;");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error(ex.Message);
            }
            return DT;

        }
        public DataTable GetUserList()
        {
            try
            {
                DT = MysqlObj.GetData_DT("select * from portaldb.UserMaster ;");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error(ex.Message);
            }
            return DT;

        }

        public DataTable GetUserRightsDataByUserId(string UserId)
        {
            try
            {
                DT = MysqlObj.GetData_DT("select  P.Pd_id as Pd_id ,P.Pid as Pid, PM.PageName as PageName ,P.Acess_Status AS Acess_Status,P.Read_Access AS Read_Access, P.Write_Access AS Write_Access, P.IDwld_Access AS IDwld_Access  ,U.Userid AS Userid from  portaldb.pagedetails as P inner join  portaldb.usermaster AS U  on P.UserId=U.UserId inner join portaldb.pagemaster PM ON P.Pid=PM.Pid  where   U.UserId=" + UserId + ";");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error(ex.Message);
            }
            return DT;

        }

        public DataTable GetUserRightsDataByBranchID(string UserId, string BranchId)
        {
            try
            {
                DT = MysqlObj.GetData_DT("select  P.Pd_id as Pd_id ,P.Pid as Pid,P.BranchId as BranchId, PM.PageName as PageName ,P.Acess_Status AS Acess_Status,P.Read_Access AS Read_Access, P.Write_Access AS Write_Access, P.IDwld_Access AS IDwld_Access  ,U.Userid AS Userid from  portaldb.pagedetails as P inner join  portaldb.usermaster AS U  on P.UserId=U.UserId inner join portaldb.pagemaster PM ON P.Pid=PM.Pid  where   U.UserId=" + UserId + " and BranchId=" + BranchId + ";");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error (ex.Message);
            }
            return DT;

        }





        public DataTable GetSubCompDetailByUserId(string Hq_Txt, string userid)
        {
            try
            {
                DT = MysqlObj.GetData_DT("select B.BranchId  AS BranchId ,B.BranchName as Branches,S.UserId as UserId ,S.sb_Id, S.cm_id, S.cd_Branchid,S.Multi_Access from " + Hq_Txt + ".branches as B  inner join  portaldb.subcompmaster as S on B.BranchID=S.cd_Branchid where  userid=" + userid + ";");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error(ex.Message);
            }
            return DT;

        }

        public DataTable GetSubCompDetailByUserIdndBranchid(string Hq_Txt, string userid, string BranchId)
        {
            try
            {
                DT = MysqlObj.GetData_DT("select B.BranchId  AS BranchId ,B.BranchName as Branches,S.UserId as UserId ,S.sb_Id, S.cm_id, S.cd_Branchid,S.Multi_Access from " + Hq_Txt + ".branches as B  inner join  portaldb.subcompmaster as S on B.BranchID=S.cd_Branchid where  userid=" + userid + " and BranchId=" + BranchId + ";");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error(ex.Message);
            }
            return DT;

        }

        public DataTable ReadUsersForIsMultiBranch(string UserName, string Password)
        {


            string command = "select Userid,UserName,Password,PortalTypeId,cd_id,Datapath,cd_BranchAlias,IsMultiBranch,IssuperAdmin from portaldb.usermaster where UserName=@UserName and Password=@Password AND IsMultiBranch=1 AND Status='A'";
            try
            {

                MySqlParameter[] parameters =
               {
                     new MySqlParameter("@UserName",UserName),
                     new MySqlParameter("@Password",Password),
                    };

                DT = ObjCloud.NewGetData_DT(command, parameters);
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error(ex.Message);
            }

            return DT;

        }

        public DataTable ReadUsersForMultiBranch(string UserName, string Password)
        {
            try
            {
                DT = MysqlObj.GetData_DT(" select Userid,UserName,Password,PortalTypeId,cd_id,Datapath,cd_BranchAlias,IsMultiBranch,IssuperAdmin from portaldb.usermaster where UserName='" + UserName + "'and Password='" + Password + "' AND IsMultiBranch=1 and Status='A' ");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error(ex.Message);
            }
            return DT;

        }

        public DataTable ReadUsers4Enduser(string UserName, string Password)
        {
            DataTable D13 = new DataTable();
            try
            {

                // Preventing SQL Injections : Parameterized Approach - 29-9-2021  


                string command = "select Userid,UserName,Password,PortalTypeId,cd_id,Datapath,cd_BranchAlias,IsMultiBranch,IssuperAdmin from portaldb.usermaster where UserName=@UserName and Password=@Password  AND IsMultiBranch=0 and IssuperAdmin=0 and Status='A'";

                MySqlParameter[] parameters =
                   {
                     new MySqlParameter("@UserName", UserName),
                     new MySqlParameter("@Password", Password),
                   };

                D13 = MysqlObj.NewGetData_DT(command, parameters);

            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error(ex.Message);
            }
            return D13;


        }
        public DataTable ReadUsersForIsSuperAdmin(string UserName, string Password)
        {



            try
            {


                // Preventing SQL Injections : Parameterized Approach 


                string command = "select Userid,UserName,Password,PortalTypeId,cd_id,Datapath,cd_BranchAlias,IsMultiBranch,IssuperAdmin from portaldb.usermaster where UserName=@UserName and Password=@Password AND IssuperAdmin=1  and Status='A'";


                MySqlParameter[] parameters =
                   {
                   new MySqlParameter("@UserName", UserName),
                   new MySqlParameter("@Password", Password),
                   };

                DT = MysqlObj.NewGetData_DT(command, parameters);


                // Existing Code 

                // DT = MysqlObj.GetData_DT(" select Userid,UserName,Password,PortalTypeId,cd_id,Datapath,cd_BranchAlias,IsMultiBranch,IssuperAdmin from portaldb.usermaster where UserName='" + UserName + "'and Password='" + Password + "' AND IssuperAdmin=1  and Status='A' ");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error(ex.Message);
            }
            return DT;





        }

        public DataTable GetBranchListForBranchUser(string UserId, string Branch_HQ)
        {
            try
            {
                DT = MysqlObj.GetData_DT("SELECT * FROM portaldb.subcompmaster AS S inner join " + Branch_HQ + ".branches as H  on S.cd_Branchid=H.BranchID   where userid=" + UserId + " AND Multi_Access='Y';");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error(ex.Message);
            }
            return DT;

        }

        public DataTable GetBranchHQval(string Branch_HQ)
        {
            try
            {
                DT = MysqlObj.GetData_DT("  select * from  " + Branch_HQ + ".branches  where Branchid=1;");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error(ex.Message);
            }
            return DT;

        }
        /// <summary>
        ///  This function for the purpose of GetBranchList 
        /// </summary>
        /// <returns></returns>
        public DataTable GetBranchUserList()
        {
            try
            {
                DT = MysqlObj.GetData_DT("select cd_branchAlias from portaldb.usermaster group by cd_branchAlias;");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error (ex.Message); 
            }
            return DT;

        }
        /// <summary>
        ///  This function uses to Get the detailed information On Users 
        /// </summary>
        /// <param name="cd_BranchAlias"></param>
        /// <returns></returns>
        public DataTable GetBranchFullDetail(string cd_BranchAlias)
        {
            try
            {
                DT = MysqlObj.GetData_DT("select  *  from portaldb.usermaster where cd_branchAlias='" + cd_BranchAlias + "' ");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error (ex.Message);
            }
            return DT;
        }
        public DataTable GetBranchFullDetailAll()
        {
            try
            {
                DT = MysqlObj.GetData_DT("select  *  from portaldb.usermaster ");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error (ex.Message);
            }
            return DT;

        }

        /// <summary>
        /// This datatable which returns data based on MultiBranch Access Value in subcompmaster 
        /// </summary>
        /// <param name="BranchHQ"></param>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public DataTable GetBranchOnLoginValues(string BranchHQ, string UserId)
        {
            try
            {
                //DT = MysqlObj.GetData_DT("SELECT  Branchid ,BranchName,b.datapath as Datapath FROM " + BranchHQ + ".branches  as b inner join portaldb.subcompmaster as S  on b.Branchid=S.cd_Branchid  where S.userid=" + UserId + " and Multi_Access<>'N'");
                DT = MysqlObj.GetData_DT("SELECT  Branchid ,branchfullname,BranchName,b.datapath as Datapath FROM " + BranchHQ + ".branches  as b inner join portaldb.subcompmaster as S  on b.Branchid=S.cd_Branchid  where S.userid=" + UserId + " and Multi_Access<>'N' order by b.Branchid  ");

            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error (ex.Message);
            }
            return DT;

        }

        /// <summary>
        ///  This datatable which returns data based on MultiBranch Access Value in subcompmaster : Testing Traial 
        /// </summary>
        /// <param name="BranchHQ"></param>
        /// <param name="UserId"></param>
        /// <param name="Cm_id"></param>
        /// <returns></returns>
        public DataTable GetBranchOnLoginValues8(string BranchHQ, string UserId, string Cm_id)
        {
            try
            {
                //DT = MysqlObj.GetData_DT("SELECT  Branchid ,BranchName,b.datapath as Datapath FROM " + BranchHQ + ".branches  as b inner join portaldb.subcompmaster as S  on b.Branchid=S.cd_Branchid  where S.userid=" + UserId + " and Multi_Access<>'N'");
                DT = MysqlObj.GetData_DT("SELECT  Branchid ,branchfullname,BranchName,b.datapath as Datapath FROM " + BranchHQ + ".branches     as b inner join  portaldb.Companydetails as C on C.cd_branchid=b.Branchid   inner join portaldb.subcompmaster as S  on b.Branchid=S.cd_Branchid  where S.userid=" + UserId + " and Multi_Access<>'N' and c.cm_id=" + Cm_id + " AND C.Status='A' order by b.Branchid ; ");

            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error (ex.Message);
            }
            return DT;

        }



        public DataTable GetUserListViaCm_id(string Cm_id)
        {
            try
            {
                DT = MysqlObj.GetData_DT("select * from portaldb.UserMaster where cm_id=" + Cm_id + "  ;");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
                Log.Error (ex.Message);
            }
            return DT;

        }














        /// <summary>
        /// This Data table which returns data based on If Portalid have value in UserMaster
        /// </summary>
        /// <param name="BranchName"></param>
        /// <returns></returns>
        public DataTable GetBranchOnLoginPortalid(string BranchName)
        {
            try
            {
                DT = MysqlObj.GetData_DT("SELECT * FROM " + BranchName + ".branches  where branchname='" + BranchName + "'");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
            }
            return DT;

        }
        /// <summary>
        /// This Datatable Retruns data based on Issuperadmin Value is True
        /// </summary>
        /// <param name="BranchHQ"></param>
        /// <param name="UserId"></param>
        /// <returns></returns> 
        public DataTable GetBranchOnSuperAdmin(string BranchHQ, string UserId)
        {
            try
            {
                DT = MysqlObj.GetData_DT("SELECT b. Branchid ,b.BranchName,b.branchfullname,b.datapath FROM " + BranchHQ + ".branches  as b inner join portaldb.subcompmaster as S  on b.Branchid=S.cd_Branchid     where S.userid=" + UserId + " and Multi_Access<>'N' ");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
            }
            return DT;
        }

        public DataTable GetBranchOnSuperAdmin8(string BranchHQ, string UserId, string cm_Id)
        {
            try
            {
                DT = MysqlObj.GetData_DT("SELECT b. Branchid ,b.BranchName,b.branchfullname,b.datapath FROM " + BranchHQ + ".branches    as b    inner join  portaldb.Companydetails as C on C.cd_branchid=b.Branchid    inner join portaldb.subcompmaster as S  on b.Branchid=S.cd_Branchid     where S.userid=" + UserId + " and c.Cm_id=" + cm_Id + " and Multi_Access<>'N' and c.status='A' ;");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
            }
            return DT;
        }


        public DataTable GetBranchOnSuperAdmin8_Light(string BranchHQ, string UserId, string cm_Id)
        {
            try
            {
                DT = MysqlObj.GetData_DT("SELECT b. Branchid ,b.BranchName,b.branchfullname,b.datapath FROM dahq.branches    as b    " 
                    + " inner join  portaldb.Companydetails as C on C.cd_branchid = b.Branchid "
                    + " inner join portaldb.subcompmaster as S  on b.Branchid = S.cd_Branchid "
                    + " where S.userid ="+ UserId +" and c.Cm_id ="+cm_Id+" and S.Multi_Access<>'N' and c.status = 'A'  "
                    + " AND C.IsLightReport = 'Y' order by b.branchid  ; ");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
            }
            return DT;
        }





        public DataTable GetBranchIDList(string BranchHQ, string UserId,string cm_Id)
        {
            try
            {
                //DT = MysqlObj.GetData_DT(" SELECT b.Branchid  FROM " + BranchHQ + ".branches  as b inner join portaldb.subcompmaster as S  on b.Branchid=S.cd_Branchid  inner join companydetails as c on b.branchid=c.cd_Branchid     where S.userid=" + UserId + "  AND c.Status'A' and Multi_Access<>'N'  ");

                DT = MysqlObj.GetData_DT(" SELECT distinct b.Branchid  FROM " + BranchHQ + ".branches  as b inner join portaldb.subcompmaster as S  on b.Branchid=S.cd_Branchid inner join portaldb.companydetails as c on b.branchid=c.cd_Branchid    where S.userid=" + UserId + " and c.cm_id="+cm_Id+" and Multi_Access<>'N' and c.Status='A' order by b.Branchid; ");

            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
            }
            return DT;
        }








        public DataTable GetBranchOnSuperAdminWithHQ(string BranchHQ, string UserID) //john added on 10-July-2019
        {
            try
            {
                DT = MysqlObj.GetData_DT("SELECT  Branchid ,BranchName,branchfullname,datapath FROM " + BranchHQ + ".branches ");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
            }
            return DT;

        }


        public DataTable GetBranchOnSuperAdminWithHQ8(string BranchHQ, string cm_id) //Karthik  added on 12-09-2020
        {
            try
            {
                DT = MysqlObj.GetData_DT("SELECT   Branchid ,BranchName,branchfullname,datapath FROM " + BranchHQ + ".branches as b  inner join  portaldb.Companydetails as C on C.cd_branchid=b.Branchid  where c.cm_id=" + cm_id + " and c.status in ('P','A') ;");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
            }
            return DT;

        }

        /// <summary>
        ///  This Function To Get the License Details of Particular OutLet 
        ///  Modified this function   on 06-11-2024  By Karthik
        /// </summary>
        /// <param name="PortalTypeId"></param>
        /// <returns></returns>
        public DataTable GetLicenseDetails(string PortalTypeId)
        {
            try
            {
                DT = MysqlObj.GetData_DT("SELECT PT.PORTALTYPE,PT.MAX_UserCnt AS UserCnt,PC.RETAILDASHBOARD,PC.SUMMARYSALES,PC.ITEMSALE,PC.TAXANALYSIS,PC.TOPSALE,"
                + " PC.MONTHLYSALE,PC.HOURLYSALE,PC.GROUPSALE ,PC.USERCREATION,PC.USERRIGHTS,PC.STOCK,PC.INVENTORY, PC.CONDIMENTREPORT,PC.UPDATEBRANCH,PC.STOCKMOVEMENT,"
                + " PC.JOURNALREPORT,PC.RAPOREPORT,PC.RETAILPAYMENT,PC.DETAILSTOCKREPORT,PC.STOCKCOLLECTOR,PC.CUSTOMERMASTER ,PC.MEMBERHISTORY, PC.POINTSHISTORY ,PC.INVENTORYPROMOTIONS , "
                + " PC.ONLINEKDS,PC.CONDIMENT,PC.CONDIMENTGROUP,PC.MODIFIER, PC.CATEGORYCUSTOMIZE,PC.LANDINGCUSTOMIZE,PC.OUTLETCLOSURE, PC.TAKEAWAYSETTING, PC.SPECIALDISCOUNT,   "
                + " PC.PRICECAPTIONSETTING,PC.IORDERLAPRODUCTTIMING,PC.LAYOUTSETTING,PC.UPSELLINGSETTING, PC.CONSUMERBEHAVIOURANALYSIS,PC.REORDERREPORT,PC.TIMEINOUTREPORT,PC.PROMOCODE, "
                + " PC.CRMCUSTOMERMASTER,PC.CRMMEMBERSHIPCAMPAIGN, PC.OPERATINGHOURS, PC.TIMEDURATIONREPORT,PC.INVENTORYPACKAGE,PC.CAMPAIGNADJUSTMENT,PC.CAMPAIGNREPORT,  "
                + " PC.MENUGROUPCREATION,PC.ZEROINTEGRATION,PC.SUPPLIERWISECOSTANDCOLLECTIONREPORT,PC.INTERACTIVEDASHBOARD, PC.ZOHO,PC.ONETIMEPROMOCODE,PC.INVENTORYBATCHADJUST, " 
                + " PC.PDPACONTEND, PC.BANNERTIMESCHEDULE, PC.MOBILEINVENTORYBATCHADJUST, PC.PRICESWITCH,PC.SALES, PC.STKMOVEMENT , PC.SALESINVOICE, PC.SALESINVOICELIST,PC.KIOSKLANDINGPAGE, " 
                + " PC.KIOSKBANNER,PC.WEBSTOCKPRESET,PC.QUICKBOOK ,PC.SUMMARYREPORT_LIGHT,PC.TAXANALYSIS_LIGHT,PC.PRODUCTSALE_LIGHT "
                + " FROM portaldb.PORTALTYPEHEADER AS PT INNER JOIN portaldb.PORTALTYPECHILD AS PC ON PT.PORTALTYPEID=PC.PORTALTYPEID "
                + " where PC.PORTALTYPEID=" + PortalTypeId + ";");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
            }
            return DT;
        }

        public DataTable GetPageRightsDetailsPid(string UserID, string Pid)
        {
            try
            {
                DT = MysqlObj.GetData_DT("select Pm.pid , Pm.PageName,PD.Branchid ,Acess_status,PD.Read_Access,PD.Write_Access,PD.IDwld_Access  from portaldb.pagemaster as PM inner join portaldb.pagedetails as PD ON PM.Pid =PD.Pid where  PD.Userid=" + UserID + " and pd.Pid=" + Pid + ";");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
            }
            return DT;
        }


        public DataTable GetPageRightsDetailsPIDNew(string UserID, string Pid, string Branchid)
        {
            try
            {
                DT = MysqlObj.GetData_DT("select Pm.pid , Pm.PageName,PD.Branchid ,Acess_status,PD.Read_Access,PD.Write_Access,PD.IDwld_Access  from portaldb.pagemaster as PM inner join portaldb.pagedetails as PD ON PM.Pid =PD.Pid where  PD.Userid=" + UserID + " and pd.Pid=" + Pid + " and Branchid=" + Branchid + "");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
            }

            return DT;
        }
        // Get the branchid :through  Cd_branchAlias
        public string GetBranchIDbyName(string BranchAlias, string BranchName)
        {
            string BranchID = string.Empty;
            try
            {
                BranchID = MysqlObj.ExecuteScalar("Select branchid from " + BranchAlias + ".Branches where Branchname = '" + BranchName + "'");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
            }
            return BranchID;
        }


        public string GetBranchRealValue(string BranchHQ, string Userid, string BranchFullNameName)
        {
            string BranchName_Real = string.Empty;
            try
            {
                BranchName_Real = MysqlObj.ExecuteScalar("SELECT  BranchName FROM " + BranchHQ + ".branches  as b inner join portaldb.subcompmaster as S  on b.Branchid=S.cd_Branchid   where S.userid='" + Userid + "' and Branchfullname='" + BranchFullNameName + "';");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
            }
            return BranchName_Real;
        }


        /// <summary>
        ///  This One For  HQ Validation  Added On 02-12-21
        /// </summary>
        /// <param name="BranchHQ"></param>
        /// <param name="BranchName"></param>
        /// <returns></returns>
        public string GetBranchFullNameByBranchName(string BranchHQ, string BranchName)
        {
            string BranchName_Real = string.Empty;
            try
            {
                BranchName_Real = MysqlObj.ExecuteScalar("SELECT BranchFullName FROM " + BranchHQ + ".branches where BranchName ='" + BranchName + "' ;");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
            }
            return BranchName_Real;
        }









        public string GetHQBranch(string BranchHQ, string Userid, string BranchFullNameName)
        {
            string BranchName_Real = string.Empty;
            try
            {
                BranchName_Real = MysqlObj.ExecuteScalar("SELECT  BranchName FROM " + BranchHQ + ".branches  as b inner join portaldb.subcompmaster as S  on b.Branchid=S.cd_Branchid   where S.userid='" + Userid + "' and Branchfullname='" + BranchFullNameName + "';");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
            }
            return BranchName_Real;
        }



        public string GetBranchCurr_BranchVal(string BanchHQ, string Userid, string BranchName)
        {
            string BranchName_Real = string.Empty;
            try
            {
                BranchName_Real = MysqlObj.ExecuteScalar("SELECT  BranchFullName FROM " + BanchHQ + ".branches  as b inner join portaldb.subcompmaster as S  on b.Branchid=S.cd_Branchid   where S.userid='" + Userid + "' and Branchname='" + BranchName + "';");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
            }
            return BranchName_Real;
        }

        // Added by Manju for Inventory Data
        internal DataTable GetAllInventoryList(string BranchHQ)
        {
            try
            {
                DT = MysqlObj.GetData_DT("SELECT  InventoryName ,InventoryId FROM " + BranchHQ + ".inventory;");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
            }
            return DT;
        }

        internal DataTable GetCategoryList(string BranchName)
        {
            try
            {
                DT = MysqlObj.GetData_DT("SELECT  CategoryName ,CategoryId FROM " + BranchName + ".inventorycategories;");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
            }
            return DT;
        }

        public int GetCDR_Period(string BranchName) //john added on 09-July-2024
        {
            string period = string.Empty ;
            try
            {
                period = MysqlObj.ExecuteScalar("SELECT customer_data_retention_period from " + BranchName + ".icrmsetting");
            }
            catch(Exception ex)
            {
                ObjLog.Write(ex);
            }
            return int.Parse(period);
        }               
    }

}