

using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Configuration;
using MySql.Data.MySqlClient;
using MySql.Data;
using CLS_BL;
using CLS_DL;
using CLS_PL;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using DocumentFormat.OpenXml.Packaging;
namespace SUNWEB
{
    public partial class UpdateInventory : System.Web.UI.Page
    {
        BL_SELECT ObjSel = new BL_SELECT();
        BLCompany ObjBL = new BLCompany();
        PLCOMMON ObjPL = new PLCOMMON();
        BL_USER ObjUserBL = new BL_USER(); //This Object for All User and Page Related fuctions included 
        PLCUSTOMER ObjCustPL = new PLCUSTOMER();
        Int32 K = 0;
        Int32 L = 0;
        Int32 S = 0;
        Int32 Q = 0;
        Int32 D = 0;
        string ConStr = ConfigurationManager.ConnectionStrings["ConMysql"].ConnectionString;
        string ConStr_Rds = ConfigurationManager.ConnectionStrings["ConRDS"].ConnectionString;
        string Curr_Date = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        string Curr_DateOnly = System.DateTime.Now.ToString("yyyy-MM-dd");
        DateTime MonthstartDt;
        DataSet ds = new DataSet();
        MYSQLDB ObjMain = new MYSQLDB();
        DataTable DT = new DataTable();
        DataTable Dt1 = new DataTable();
        DataTable Dt2 = new DataTable();
        DataTable DtLicense = new DataTable();
        DataTable DtSuperAdmin = new DataTable();
        DataTable DtMultiBranch = new DataTable();
        DataTable DtNormalUser = new DataTable();
        DataTable DTProductSale = new DataTable();
        MySqlCommand Cmdv = new MySqlCommand();
        string UserId = string.Empty;
        string UserName = string.Empty;
        string PortalTypeId = string.Empty;
        string cd_Id = string.Empty;
        string Datapath = string.Empty;
        string cd_BranchAlias = string.Empty;
        string IsMultiBranch = string.Empty;
        string IsSuperAdmin = string.Empty;
        string ActiveCallvalue = string.Empty;
        string PID = string.Empty;   // License Variables ..
        string Page_Acess = string.Empty;
        string Read_Acess = string.Empty;
        string Write_Acess = string.Empty;
        string Isdowld_Acess = string.Empty;
        string Rcd_id = string.Empty;
        string RPortalid = string.Empty;
        string Useridvalue = string.Empty;
        string QueryCurr_Select = string.Empty;
        string L_MaxUserCnt = string.Empty;
      
        string L_UpdateBranch = string.Empty;
        string BranchFullName = string.Empty;
        string Curr_BranchFullName = string.Empty;
        string Cmb_BranchName = string.Empty;
        string Branchidval = string.Empty;
        string BraNam = "";
        //string FDate = "";
        //string TDate = "";
        string HQ = string.Empty;
        string Cm_id = string.Empty;
        string HQAlias = string.Empty;
        string BRAlias = string.Empty;
        string ISHQ = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {           
            PID = "15";                      // This is For UpdateBranch
            MySqlConnection Cn = new MySqlConnection(ConStr_Rds);
            Cn.Open();

            //for HQ db name
            HQAlias = Convert.ToString(Session["Branch_HQ"]);

            if (Session["UserName"] == null)
            {
                Response.Redirect("Login.aspx");
            }
            MonthstartDt = new DateTime(System.DateTime.Now.Year, System.DateTime.Now.Month, 1);

            if (Session["UserName"] == null)
            {
                Response.Redirect("Login.aspx");
            }
 
            if (((Convert.ToBoolean(Session["IsSuperAdmin"])) == true) && (Session["PortalTypeId"].ToString() == "") && ((Convert.ToBoolean(Session["IsMultiBranch"])) == true))
            {
                if (!IsPostBack)
                {
                    LoadBranchName4SuperAdmin();

                  
                    string QueryCal = "select ActiveValue from portaldb.usermaster  where Userid=" + Session["UserId"].ToString() + " ";
                    ActiveCallvalue = ObjMain.ExecuteScalar(QueryCal);
                    if (ActiveCallvalue != " " || ActiveCallvalue != "ALL")
                    {
                        ActiveCall();
                    }
                  
                }
            }
            else if (((Convert.ToBoolean(Session["IsSuperAdmin"])) == true) && (Session["PortalTypeId"].ToString() == ""))
            {
                if (!IsPostBack)
                {
                    LoadBranchName4SuperAdmin();
                }
                cd_BranchAlias = cmbBranches.SelectedItem.Text;
                BraNam = cd_BranchAlias;
                //FDate = txtFromDt.Text;
                //TDate = txtToDt.Text;
                //LoadGroupSalesMainQuery(cd_BranchAlias, txtFromDt.Text, txtToDt.Text);
            }
            else if (((Convert.ToBoolean(Session["IsMultiBranch"])) == true) && (Session["PortalTypeId"].ToString() == ""))
            {
                if (!IsPostBack)
                {
                    LoadBranchName();
                    ActiveCall();
                }
            }
            else if (((Session["PortalTypeId"].ToString() != "") || (Convert.ToBoolean(Session["IsSuperAdmin"])) == false) && ((Convert.ToBoolean(Session["IsMultiBranch"])) == false))
            {
                cmbBranches.Enabled = false;
                cmbBranches.Visible = false;

                Cm_id = ObjSel.GETcmid(Session["Cd_Id"].ToString());  // Get cm_id
                HQ = ObjSel.Get_HQ(Cm_id);     // Get the HQ Value
                Curr_BranchFullName = ObjSel.GetBranchFullName(HQ, Session["Branch_HQ"].ToString());
                lblBranchAliasName.ForeColor = System.Drawing.Color.DodgerBlue;
                lblBranchAliasName.Text = "&nbsp;" + "-" + "&nbsp;" + Curr_BranchFullName + "&nbsp; ";
                cd_BranchAlias = Session["Branch_HQ"].ToString();
                Datapath = Session["DataPath"].ToString();
                PortalTypeId = Session["PortalTypeId"].ToString();
                PID = "15"; //  This is for ItemSale 
                DT = ObjUserBL.GetPageRightsDetailsPid(Session["UserId"].ToString(), PID);
                if (DT.Rows.Count > 0)
                {
                    Page_Acess = DT.Rows[0]["Acess_status"].ToString();
                    Read_Acess = DT.Rows[0]["Read_Access"].ToString();
                    Write_Acess = DT.Rows[0]["Write_Access"].ToString();
                    Isdowld_Acess = DT.Rows[0]["IDwld_Access"].ToString();
                    Session["Page_Acess"] = Page_Acess;
                    Session["Read_Acess"] = Read_Acess;
                    Session["Write_Acess"] = Write_Acess;
                    Session["Isdowld_Acess"] = Isdowld_Acess;
                }
                if ((Session["L_UpdateBranch"].ToString() == "N") || (Session["Page_Acess"].ToString() == "N"))
                {
                    Response.Redirect("DeniedPage.aspx", false);
                }
                else
                {
                    //lblMsgTotalSelection.Text = "Group Sales Report  Between" + "&nbsp;&nbsp;&nbsp;&nbsp;" + txtFromDt.Text + "&nbsp;&nbsp;&nbsp;  And &nbsp;&nbsp;&nbsp;" + txtToDt.Text;
                    if (!IsPostBack)
                    {
                        if (Session["Isdowld_Acess"].ToString() == "N")
                        {
                            // btnExcel.Enabled = false;
                            Response.Write(@"<script language='javascript'>alert('No Access To download.')</script>");
                        }
                        else
                        {
                            //btnGenerate.Visible = true;
                            //btnExcel.Enabled = true;
                            if (cmbBranches.Visible == false)
                            {

                                //LoadGroupSalesMainQuery(Session["Branch_HQ"].ToString(), txtFromDt.Text, txtToDt.Text);
                            }

                        }
                    }
                }
            }
            else
            {

            }            
            
        }
        public void LoadBranchName4SuperAdmin()
        {
            DtSuperAdmin = ObjUserBL.GetBranchOnSuperAdmin8(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), Session["CM_ID"].ToString());
            if (DtSuperAdmin.Rows.Count > 0)
            {
                cmbBranches.DataSource = DtSuperAdmin;
                cmbBranches.DataTextField = "branchfullname";
                cmbBranches.DataValueField = "Branchid";
                cmbBranches.DataBind();
                cmbBranches.Items.Insert(0, "All");
            }
        }
        public void LoadBranchName()
        {
            DtNormalUser = ObjUserBL.GetBranchOnLoginValues8(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), Session["CM_ID"].ToString());
            if (DtNormalUser.Rows.Count > 0)
            {
                cmbBranches.DataSource = DtNormalUser;
                cmbBranches.DataTextField = "branchfullname";
                cmbBranches.DataValueField = "Branchid";
                cmbBranches.DataBind();
                cmbBranches.Items.Insert(0, "All");

            }
        }

        public void UPDATEBRANCH(string ActiveCallvalue)
        {
            if (!string.IsNullOrEmpty(Session["UserId"] as string))
            {
                Useridvalue = Session["UserId"].ToString();
                QueryCurr_Select = "Update portaldb.usermaster set ActiveValue='" + ActiveCallvalue + "' where Userid=" + Useridvalue + "";
                S = ObjMain.ExecuteNonQuery(QueryCurr_Select);
            }
            else
            {
                Response.Redirect("Login.aspx", false);
            }
        }
        public void ActiveCall()
        {
            string QueryCal = "select ActiveValue from portaldb.usermaster  where Userid=" + Session["UserId"].ToString() + " ";
            ActiveCallvalue = ObjMain.ExecuteScalar(QueryCal);
            if (ActiveCallvalue == "")
            {
                ActiveCallvalue = ObjSel.GETBrachAlias8(Session["CM_ID"].ToString(), Session["Branch_HQ"].ToString(), Session["UserId"].ToString());
            }

            ISHQ = ObjMain.ExecuteScalar("select BranchName from " + ActiveCallvalue + ".branches where Branchname='" + ActiveCallvalue + "' and branchID=1;");  //now 

            if (ISHQ != "")
            {
                ActiveCallvalue = ObjSel.GetFirstBranch7(Session["Branch_HQ"].ToString(), Session["UserId"].ToString());
                UPDATEBRANCH(ActiveCallvalue);
            }

            cd_BranchAlias = ActiveCallvalue;
            Curr_BranchFullName = ObjUserBL.GetBranchCurr_BranchVal(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), cd_BranchAlias);
            Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), Curr_BranchFullName);  // This will Return BranchName( Actual Database Name )
            cmbBranches.Items.FindByText(Curr_BranchFullName).Selected = true;
            Branchidval = ObjUserBL.GetBranchIDbyName(ActiveCallvalue, ActiveCallvalue);
            if (Branchidval != "")
            {
                DT = ObjUserBL.GetPageRightsDetailsPIDNew(Session["UserId"].ToString(), PID, Branchidval);
            }
            else
            {
                DT = ObjUserBL.GetPageRightsDetailsPIDNew(Session["UserId"].ToString(), PID, Session["Fistbranch"].ToString());

            }
            if (DT.Rows.Count > 0)
            {
                Page_Acess = DT.Rows[0]["Acess_status"].ToString();
                Read_Acess = DT.Rows[0]["Read_Access"].ToString();
                Write_Acess = DT.Rows[0]["Write_Access"].ToString();
                Isdowld_Acess = DT.Rows[0]["IDwld_Access"].ToString();

                Session["Page_Acess"] = Page_Acess;
                Session["Read_Acess"] = Read_Acess;
                Session["Write_Acess"] = Write_Acess;
                Session["Isdowld_Acess"] = Isdowld_Acess;
            }

            Rcd_id = ObjMain.ExecuteScalar("select cd_id from portaldb.companydetails where cd_branchid=" + Microsoft.Security.Application.Encoder.HtmlEncode(cmbBranches.SelectedItem.Value) + " and cd_branchalias='" + ActiveCallvalue + "';");
            RPortalid = ObjMain.ExecuteScalar("select portaltypeid  from portaldb.invoicedetails where cd_id=" + Rcd_id + ";");
            DtLicense = ObjUserBL.GetLicenseDetails(RPortalid);

            if (DtLicense.Rows.Count > 0)
            {
               
                L_UpdateBranch = DtLicense.Rows[0]["UPDATEBRANCH"].ToString();
                Session["L_UpdateBranch"] = L_UpdateBranch;
            }
            if (Session["L_UpdateBranch"].ToString() == "N" || Session["Page_Acess"].ToString() == "N")
            {
                Response.Redirect("DeniedPage.aspx", false);
            }
            else
            {
                //btnExcel.Enabled = true;
                //lblMsgTotalSelection.Text = "Group Sales Report Between" + "&nbsp;&nbsp;&nbsp;" + txtFromDt.Text + "&nbsp;&nbsp;&nbsp;  And &nbsp;&nbsp;&nbsp;" + txtToDt.Text;
                BraNam = cd_BranchAlias;
                //FDate = txtFromDt.Text;
                //TDate = txtToDt.Text;
                //LoadGroupSalesMainQuery(cd_BranchAlias, txtFromDt.Text, txtToDt.Text);
                //BindGridview(ActiveCallvalue, txtStartDt.Text, txtEndDt.Text);
                System.Threading.Thread.Sleep(2000);

               string IsPostFunction= IsUnderPostFuction(BraNam);
                if (IsPostFunction == "1")
                {
                    lblmsg.Text = "&#x2705;" + " " + "Post Function";
                }
                else if(IsPostFunction == "0") 
                {
                    lblmsg.Text = "&#10060" + " " + "Post Function";
                }
                
               
            }
        }



        //protected void RadioButton_CheckedChanged(object sender, System.EventArgs e)
        //{
        //    if (OptAll.Checked == true)
        //    {
        //        OptSelectAll(); 
        //    }
        //    else
        //    {
        //        OptClearSelections();
        //    }
        //}
        protected void chkSelectAll_CheckedChanged(object sender, System.EventArgs e)
        {
            if (chkSelectALL.Checked  == true)
            {
                OptSelectAll();
            }
            else
            {
                OptClearSelections();
            }
        }
        public void OptSelectAll()
        {
            chkUnits.Checked = true;
            chkCategories.Checked = true;
            chkGroupInformations.Checked = true;
            chkGroup1.Checked = true;
            chkGroup2.Checked = true;
            chkGroup3.Checked = true;
            chkGroup4.Checked = true;
            chkGroup5.Checked = true;
            chkInventoryItems.Checked = true;
            chkInventoryPackage.Checked = true;
            //chkPriceShift.Checked = true;
            chkPriceShiftNew.Checked = true;
            chkInventoryAssembly.Checked = true;
            chkCondiment.Checked = true;
            chkCondimentGroup.Checked = true;
            chkCondimentGroupDetails.Checked = true;
            chkModifierControl.Checked = true;
            chkReasons.Checked = true;
            //chkInventoryCostAverage.Checked = true;
            //chkInventoryCostAverageCostOnly.Checked = true;
            //chkUpdatePriceShiftID.Checked = true;
        }

        public void OptClearSelections()
        {
            chkUnits.Checked = false;
            chkCategories.Checked = false;
            chkGroupInformations.Checked = false;
            chkGroup1.Checked = false;
            chkGroup2.Checked = false;
            chkGroup3.Checked = false;
            chkGroup4.Checked = false;
            chkGroup5.Checked = false;
            chkInventoryItems.Checked = false;
            chkInventoryPackage.Checked = false;
            //chkPriceShift.Checked = false;
            chkPriceShiftNew.Checked = false;
            chkInventoryAssembly.Checked = false;
            chkCondiment.Checked = false;
            chkCondimentGroup.Checked = false;
            chkCondimentGroupDetails.Checked = false;
            chkModifierControl.Checked = false;
            chkReasons.Checked = false;
            chkInventoryCostAverage.Checked = false;
            chkInventoryCostAverageCostOnly.Checked = false;
            chkUpdatePriceShiftID.Checked = false;
            chkUpdateWebOrderPromo.Checked = false;
        }

        //protected void RadioButton_CheckedChangedP(object sender, System.EventArgs e)
        //{
        //    if (OptAllP.Checked == true)
        //    {
        //        OptSelectAllP();
        //    }
        //    else
        //    {
        //        OptClearSelectionsP();
        //    }
        //}
        protected void chkSelectAllP_CheckedChanged(object sender, System.EventArgs e)
        {
            if (chkSelectAllP.Checked == true)
            {
                OptSelectAllP();
            }
            else
            {
                OptClearSelectionsP();
            }
           
        }
        public void OptSelectAllP()
        {
            chkEmployees.Checked = true;
            chkOperators.Checked = true;
            chkCustomers.Checked = true;
            chkMemberMaster.Checked = true;
            chkRoles.Checked = true;
            chkPermissionSet.Checked = true;
            chkRolePermission.Checked = true; 
        }
        public void OptClearSelectionsP()
        {
            chkEmployees.Checked = false;
            chkOperators.Checked = false;
            chkCustomers.Checked = false;
            chkMemberMaster.Checked = false;
            chkRoles.Checked = false;
            chkPermissionSet.Checked = false;
            chkRolePermission.Checked = false; 
        }

        protected void cmbBranches_SelectedIndexChanged(object sender, EventArgs e)
        {
            System.Threading.Thread.Sleep(1000);
            if (cmbBranches.SelectedIndex == 0)
            {
               // Response.Redirect("UpdateInventory.aspx");
            }
            else
            {
                DT = ObjUserBL.GetPageRightsDetailsPIDNew(Session["UserId"].ToString(), PID, Microsoft.Security.Application.Encoder.HtmlEncode(cmbBranches.SelectedItem.Value));
                if (DT.Rows.Count > 0)
                {
                    Page_Acess = DT.Rows[0]["Acess_status"].ToString();
                    Read_Acess = DT.Rows[0]["Read_Access"].ToString();
                    Write_Acess = DT.Rows[0]["Write_Access"].ToString();
                    Isdowld_Acess = DT.Rows[0]["IDwld_Access"].ToString();

                    Session["Page_Acess"] = Page_Acess;
                    Session["Read_Acess"] = Read_Acess;
                    Session["Write_Acess"] = Write_Acess;
                    Session["Isdowld_Acess"] = Isdowld_Acess;
                }

                ISHQ = ObjMain.ExecuteScalar("select BranchName from branches where Branchname='" + ActiveCallvalue + "' and branchID=1;");  //now 

                if (ISHQ != "")
                {
                    ActiveCallvalue = ObjSel.GetFirstBranch(Session["Branch_HQ"].ToString());
                    UPDATEBRANCH(ActiveCallvalue);
                }
                else
                {
                    BranchFullName = Microsoft.Security.Application.Encoder.HtmlEncode(cmbBranches.SelectedItem.Text);    // This will Return Branch FullName 
                    Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                    Rcd_id = ObjMain.ExecuteScalar("select cd_id from portaldb.companydetails where cd_branchid=" + cmbBranches.SelectedItem.Value + " and cd_branchalias='" + Cmb_BranchName + "';");
                    RPortalid = ObjMain.ExecuteScalar("select portaltypeid  from portaldb.invoicedetails where cd_id=" + Rcd_id + ";");
                    DtLicense = ObjUserBL.GetLicenseDetails(RPortalid);

                
                }

                UPDATEBRANCH(Cmb_BranchName);
                if (DtLicense.Rows.Count > 0)
                {
                    L_UpdateBranch = DtLicense.Rows[0]["UPDATEBRANCH"].ToString();
                    Session["L_UpdateBranch"] = L_UpdateBranch;
                }

                if (Session["L_UpdateBranch"].ToString() == "N" || Session["Page_Acess"].ToString() == "N")
                {
                    Response.Redirect("DeniedPage.aspx");
                }
                else
                {

                    string IsPostFunction = IsUnderPostFuction(Cmb_BranchName);
                    if (IsPostFunction == "1")
                    {
                        lblmsg.Text ="&#x2705;"+" " + "Post Function";
                    }
                    else if (IsPostFunction == "0")
                    {
                        lblmsg.Text = "&#10060"+ " "+ "Post Function";
                    }



                }

            }




        }
        //Invnetoryupdate Starts here*******************************************************************************************************
        protected void btnUpdateInventory_Click (object sender, EventArgs e)
        {
            InventoryDataDistributionProc();
        }
        
        //for data distribution to selected branches
        public void InventoryDataDistributionProc()
        { 
            string sCmd =string.Empty ;
            
            string sBranchPath = string.Empty;
            int lBranchID = 0;
            int i = 0;

            //for dummy db connections
            if (cmbBranches.SelectedItem.Text == "All")
            {
                sCmd = "Select * From " + HQAlias + ".Branches Where Active=1 And UpdateBranch=1 And BranchID<>1";
                            
            }
            else
            {
                sCmd = "Select * From " + HQAlias + ".Branches Where BranchID=" + Microsoft.Security.Application.Encoder.HtmlEncode(cmbBranches.SelectedItem.Value);
            }
            DT  = ObjMain.GetData_DT(sCmd);
            foreach (DataRow dr in DT.Rows)
            {
                sBranchPath =Convert.ToString(dr["Datapath"]);
                lBranchID = Convert.ToInt16(dr["BranchID"]);
                BRAlias = Convert.ToString(dr["BranchName"]);

                //BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
                //Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                //BranchAlias = Cmb_BranchName;

                //for checking selected checkbox values for updating


                #region  Post:Branch Ini Validation 

                string Chk_Post = IsUnderPostFuction(BRAlias);
                if (Chk_Post == "1")
                {

                    if (chkUnits.Checked == true)
                    {
                        SaveModule(1, sBranchPath, lBranchID);
                    }
                    if (chkCategories.Checked == true)
                    {
                        SaveModule(2, sBranchPath, lBranchID);
                    }
                    if (chkGroupInformations.Checked == true)
                    {
                        SaveModule(3, sBranchPath, lBranchID);
                    }
                    if (chkGroup1.Checked == true)
                    {
                        SaveModule(4, sBranchPath, lBranchID);
                    }
                    if (chkGroup2.Checked == true)
                    {
                        SaveModule(5, sBranchPath, lBranchID);
                    }
                    if (chkGroup3.Checked == true)
                    {
                        SaveModule(6, sBranchPath, lBranchID);
                    }
                    if (chkGroup4.Checked == true)
                    {
                        SaveModule(7, sBranchPath, lBranchID);
                    }
                    if (chkGroup5.Checked == true)
                    {
                        SaveModule(8, sBranchPath, lBranchID);
                    }
                    if (chkInventoryItems.Checked == true)
                    {
                        SaveModule(9, sBranchPath, lBranchID);
                    }
                    //if (chkPriceShift.Checked == true)                //nid hide as per Benny's request on 09-May-2022
                    //{
                    //    SaveModule(10, sBranchPath, lBranchID);
                    //}
                    if (chkInventoryAssembly.Checked == true)
                    {
                        SaveModule(11, sBranchPath, lBranchID);
                    }
                    if (chkCondiment.Checked == true)
                    {
                        SaveModule(12, sBranchPath, lBranchID);
                    }
                    if (chkCondimentGroup.Checked == true)
                    {
                        SaveModule(13, sBranchPath, lBranchID);
                    }
                    if (chkModifierControl.Checked == true)
                    {
                        SaveModule(14, sBranchPath, lBranchID);
                    }
                    if (chkReasons.Checked == true)
                    {
                        SaveModule(15, sBranchPath, lBranchID);
                    }
                    if (chkInventoryCostAverage.Checked == true)
                    {
                        SaveModule(16, sBranchPath, lBranchID);
                    }
                    if (chkInventoryCostAverageCostOnly.Checked == true)
                    {
                        SaveModule(17, sBranchPath, lBranchID);
                    }
                    if (chkUpdatePriceShiftID.Checked == true)
                    {
                        SaveModule(18, sBranchPath, lBranchID);
                    }
                    if (chkInventoryPackage.Checked == true)
                    {
                        SaveModule(19, sBranchPath, lBranchID);
                    }
                    if (chkPriceShiftNew.Checked == true)
                    {
                        SaveModule(20, sBranchPath, lBranchID);
                    }
                    if (chkCondimentGroupDetails.Checked == true)
                    {
                        SaveModule(21, sBranchPath, lBranchID);
                    }
                    if(chkUpdateWebOrderPromo.Checked == true)
                    {
                        SaveModule(22, sBranchPath, lBranchID);
                    }


                }
                else
                {
                    
                }

                #endregion
               
            }
            //for update RunningNumebrs
            UpdateRunningNo();

            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Update Branch (Inventory Information) completed Successfully ');location.href='UpdateInventory.aspx' ", true);

        }
        //for function selection for updates
        public void SaveModule(int pIndex, string pBranchPath,  int pBranchID)
        {
            switch (pIndex)
            {
                case 1:
                    {
                        SaveI00 (pBranchPath);
                        break;
                    }
                case 2:
                    {
                        SaveI01(pBranchPath);
                        break;
                    }
                case 3:
                    {
                        SaveI02(pBranchPath);
                        break;
                    }
                case 4:
                    {
                        SaveI03(pBranchPath);
                        break;
                    }
                case 5:
                    {
                        SaveI04(pBranchPath);
                        break;
                    }
                case 6:
                    {
                        SaveI05(pBranchPath);
                        break;
                    }
                case 7:
                    {
                        SaveI06(pBranchPath);
                        break;
                    }
                case 8:
                    {
                        SaveI07(pBranchPath);
                        break;
                    }
                case 9:
                    {
                        SaveI08(pBranchPath);
                        break;
                    }
                case 10:
                    {
                        //SaveI09(pBranchPath);
                        break;
                    }
                case 11:
                    {
                        SaveI10(pBranchPath);
                        break;
                    }
                case 12:
                    {
                        SaveI11(pBranchPath);
                        break;
                    }
                case 13:
                    {
                        SaveI12(pBranchPath);
                        break;
                    }
                case 14:
                    {
                        SaveI13(pBranchPath);
                        break;
                    }
                case 15:
                    {
                        SaveI14(pBranchPath);
                        break;
                    }
                case 16:
                    {
                        SaveI15(pBranchPath);
                        break;
                    }
                case 17:
                    {
                        SaveI16(pBranchPath);
                        break;
                    }
                case 18:
                    {
                        SaveI17(pBranchPath);
                        break;
                    }
                case 19:
                    {
                        SaveI18(pBranchPath);
                        break;
                    }
                case 20:
                    {
                        SaveI19(pBranchPath);
                        break;
                    }
                case 21:
                    {
                        SaveI20(pBranchPath);
                        break;
                    }
                case 22:
                    {
                        SaveI21(pBranchPath);
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
        }
        //for InventoryUnits
        public void SaveI00(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;

            sCmd = "Replace INTO {0}.InventoryUnits(UnitID, Fixed, Unit, Active, Updated, Notes, BranchUpdated, UnitValue)" +
                " Select UnitID, Fixed, Unit, Active, Updated, Notes, BranchUpdated, UnitValue From {1}.InventoryUnits ";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
        }
        //for InventoryCategories
        public void SaveI01(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;

            sCmd = "Replace INTO {0}.InventoryCategories(CategoryID, CategoryNumber, CategoryName, Description, DateCreated, Active, Updated, BranchUpdated, Flag1, Flag2, FireIndex, Breakline)" +
                " Select CategoryID, CategoryNumber, CategoryName, Description, DateCreated, Active, Updated, BranchUpdated, Flag1, Flag2, FireIndex, Breakline From {1}.InventoryCategories";
            string command = string.Format(sCmd, BRAlias, HQAlias);

            Q = ObjMain.ExecuteNonQuery(command);
        }
        //for GroupsInformation
        public void SaveI02(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;

            sCmd = "Replace INTO {0}.GroupsInformation(GID, Fixed, GroupAlias, GroupDescription, ImportTable, NameField, DescriptionField, Updated, BranchUpdated)" +
                " Select GID, Fixed, GroupAlias, GroupDescription, ImportTable, NameField, DescriptionField, Updated, BranchUpdated From {1}.GroupsInformation";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
        }
        //for Group1
        public void SaveI03(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;

            sCmd = "Replace INTO {0}.Group1(GroupID, GroupName, Description, DateCreated, Active, Updated, BranchUpdated, breakline)" +
                " Select GroupID, GroupName, Description, DateCreated, Active, Updated, BranchUpdated, breakline From {1}.Group1";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
        }
        //for Group2
        public void SaveI04(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;

            sCmd = "Replace INTO {0}.Group2(GroupID, GroupName, Description, DateCreated, Active, Updated, BranchUpdated, breakline)" +
                " Select GroupID, GroupName, Description, DateCreated, Active, Updated, BranchUpdated, breakline From {1}.Group2";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
        }
        //for Group3
        public void SaveI05(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;

            sCmd = "Replace INTO {0}.Group3(GroupID, GroupName, Description, DateCreated, Active, Updated, BranchUpdated, breakline)" +
                " Select GroupID, GroupName, Description, DateCreated, Active, Updated, BranchUpdated, breakline From {1}.Group3";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
        }
        //for Group4
        public void SaveI06(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;

            sCmd = "Replace INTO {0}.Group4(GroupID, GroupName, Description, DateCreated, Active, Updated, BranchUpdated, breakline)" +
                " Select GroupID, GroupName, Description, DateCreated, Active, Updated, BranchUpdated, breakline From {1}.Group4";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
        }
        //for Group5
        public void SaveI07(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;

            sCmd = "Replace INTO {0}.Group5(GroupID, GroupName, Description, DateCreated, Active, Updated, BranchUpdated, breakline)" +
                " Select GroupID, GroupName, Description, DateCreated, Active, Updated, BranchUpdated, breakline From {1}.Group5";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
        }
        //for Inventory
        public void SaveI08(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;

            //promo column added on 12-Nov-2025
            sCmd = "Replace INTO {0}.Inventory(InventoryID, BarCode, InventoryName, Description, CategoryID, Class, Unit, Price, Price2, Price3, Price4, Onhand, ReorderLevel, MaxiumLevel, SafetyLevel, DateCreated, Group1, Group2, Group3, Group4, Group5, NoPriceShift, Active, Updated, Photograph, Notes, BranchUpdated, StockCode, Flag1, Flag2, PriceShiftId, AllowDecimalValue, PackageType, PackageValue, PackageDiscount, Serialized,Imagepath,promo)" +
                " Select InventoryID, BarCode, InventoryName, Description, CategoryID, Class, Unit, Price, Price2, Price3, Price4, Onhand, ReorderLevel, MaxiumLevel, SafetyLevel, DateCreated, Group1, Group2, Group3, Group4, Group5, NoPriceShift, Active, Updated, Photograph, Notes, BranchUpdated, StockCode, Flag1, Flag2, PriceShiftId, AllowDecimalValue, PackageType, PackageValue, PackageDiscount, Serialized,Imagepath,promo From {1}.Inventory";

            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);

            sCmd = "Insert INTO {0}.InventoryCostAverage(InventoryID) " +
                " Select A.InventoryID From {0}.Inventory A Left Join {0}.InventoryCostAverage B On A.InventoryID=B.InventoryID  " +
                " Where IsNull(B.InventoryID)";
            string command1 = string.Format(sCmd, BRAlias);
            Q = ObjMain.ExecuteNonQuery(command1);


            #region  Delete duplicate ids In Both Inventory and Inventorycostaverge 
            // Karthi Added on 11-09-25

            // Inventory duplicate delete 
            string Inventory_Del_Query = "DELETE Child "
                   + " FROM {0}.inventory AS Child "
                   + " LEFT JOIN {1}.Inventory AS HQ "
                   + " ON Child.Inventoryid = HQ.Inventoryid "
                   + " WHERE HQ.Inventoryid IS NULL; ";

            string  cmd_Inventory_Del= string.Format(Inventory_Del_Query, BRAlias, HQAlias);
            D = ObjMain.ExecuteNonQuery(cmd_Inventory_Del);

            // Inventorycostaverage duplicate delete
            string Inventorycostaverage_Del_Query = "DELETE Child "
                  + " FROM {0}.Inventorycostaverage AS Child "
                  + " LEFT JOIN {1}.Inventorycostaverage AS HQ "
                  + " ON Child.Inventoryid = HQ.Inventoryid "
                  + " WHERE HQ.Inventoryid IS NULL; ";

            string cmd_Inventorycostaverage_Del = string.Format(Inventorycostaverage_Del_Query, BRAlias, HQAlias);
            D = ObjMain.ExecuteNonQuery(cmd_Inventorycostaverage_Del);
            
            #endregion








        }
        //for InventoryPriceShift
        public void SaveI09(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;

            sCmd = "Replace INTO {0}.InventoryPriceShift(PriceShiftID, PriceShiftName, Forever, ShiftType, EffectiveFrom, EffectiveTo, QuantityShiftType, QuantityFrom, Mixed, ShiftValue, QuantityFrom2, ShiftValue2, QuantityFrom3, ShiftValue3, QuantityFrom4, ShiftValue4, QuantityFrom5, ShiftValue5, OddDisc, SellingPriceDisc, AmountFrom1, AmountShift1, AmountFrom2, AmountShift2, AmountFrom3, AmountShift3, AmountFrom4, AmountShift4, AmountFrom5, AmountShift5, Description, Active, Updated, BranchUpdated)" +
                " Select PriceShiftID, PriceShiftName, Forever, ShiftType, EffectiveFrom, EffectiveTo, QuantityShiftType, QuantityFrom, Mixed, ShiftValue, QuantityFrom2, ShiftValue2, QuantityFrom3, ShiftValue3, QuantityFrom4, ShiftValue4, QuantityFrom5, ShiftValue5, OddDisc, SellingPriceDisc, AmountFrom1, AmountShift1, AmountFrom2, AmountShift2, AmountFrom3, AmountShift3, AmountFrom4, AmountShift4, AmountFrom5, AmountShift5, Description, Active, Updated, BranchUpdated From {1}.InventoryPriceShift";

            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
        }
        //for InventoryAssembly
        public void SaveI10(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;

            sCmd = "Replace INTO {0}.InventoryAssembly(AssemblyID, InventoryID, AssemblyInventoryID, Quantity, Notes, Updated)" +
                " Select AssemblyID, InventoryID, AssemblyInventoryID, Quantity, Notes, Updated From {1}.InventoryAssembly";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
        }
        //for Condiment
        public void SaveI11(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;

            sCmd ="Replace INTO {0}.Condiment(CondimentID, CondimentName, CondimentDesc, ImagePath, " 
               + " Available, SingleSelection,Price1,Price2,Price3,Price4,Discount,kitchenflag,SpecialCondiment,nosubcondiment,condimentitem,CondimentDefaultQTY) "
               + " Select CondimentID, CondimentName, CondimentDesc, ImagePath, Available, SingleSelection," 
               + " Price1,Price2,Price3,Price4,Discount,kitchenflag,SpecialCondiment,nosubcondiment,condimentitem,CondimentDefaultQTY From {1}.Condiment";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);

            #region  Delete duplicate  CondimentID in condiment
            // Karthi Added on 11-09-25

            // condiment duplicate delete 
            string Condiment_Del_Query = " DELETE Child "
                        + " FROM {0}.condiment AS Child "
                        + " LEFT JOIN {1}.condiment AS HQ "
                        + " ON Child.CondimentID = HQ.CondimentID "
                        + " WHERE HQ.CondimentID IS NULL; "; 

            string cmd_Inventory_Del = string.Format(Condiment_Del_Query, BRAlias, HQAlias);
            D = ObjMain.ExecuteNonQuery(cmd_Inventory_Del);

            #endregion

        }
        //for CondimentGroup
        public void SaveI12(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;
            //CondimentGroupDefaultQTY, condimentgroupoptional these 2 fields added into POST on 12-Nov-2025
            sCmd = "Replace INTO {0}.CondimentGroup(CondimentGroupID, CondimentGroupName,CondimentGroupDefaultQTY, condimentgroupoptional)" +
                " Select CondimentGroupID, CondimentGroupName,CondimentGroupDefaultQTY, condimentgroupoptional From {1}.CondimentGroup";
            string command = string.Format(sCmd, BRAlias, HQAlias);

            Q = ObjMain.ExecuteNonQuery(command);



            #region  Delete duplicate  CondimentGroup
            // Karthi Added on 12-09-25

            // CondimentGroup duplicate delete 
            string CondimentGroup_Del_Query = " DELETE Child "
                        + " FROM {0}.CondimentGroup AS Child "
                        + " LEFT JOIN {1}.CondimentGroup AS HQ "
                        + " ON Child.CondimentGroupID = HQ.CondimentGroupID "
                        + " WHERE HQ.CondimentGroupID IS NULL; ";

            string cmd_CondimentGroup_Del = string.Format(CondimentGroup_Del_Query, BRAlias, HQAlias);
            D = ObjMain.ExecuteNonQuery(cmd_CondimentGroup_Del);

            #endregion




        }
        //for ModifierControl
        public void SaveI13(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;

            sCmd = "Replace INTO {0}.ModifierControl(ControlID, ControlType, ControlCode, ControlName, ProcessType1, ProcessCode1, ProcessName1, ProcessMin1, ProcessMax1, ProcessOption1, ProcessUpgrade1, ProcessType2, ProcessCode2, ProcessName2, ProcessMin2, ProcessMax2, ProcessOption2, ProcessUpgrade2, ProcessType3, ProcessCode3, ProcessName3, ProcessMin3, ProcessMax3, ProcessOption3, ProcessUpgrade3, ProcessType4, ProcessCode4, ProcessName4, ProcessMin4, ProcessMax4, ProcessOption4, ProcessUpgrade4, ProcessType5, ProcessCode5, ProcessName5, ProcessMin5, ProcessMax5, ProcessOption5, ProcessUpgrade5, LinkID, ProcessType6, ProcessCode6, ProcessName6, ProcessMin6, ProcessMax6, ProcessOption6, ProcessUpgrade6, ProcessType7, ProcessCode7, ProcessName7, ProcessMin7, ProcessMax7, ProcessOption7, ProcessUpgrade7, ProcessType8, ProcessCode8, ProcessName8, ProcessMin8, ProcessMax8, ProcessOption8, ProcessUpgrade8, ProcessType9, ProcessCode9, ProcessName9, ProcessMin9, ProcessMax9, ProcessOption9, ProcessUpgrade9, ProcessType10, ProcessCode10, ProcessName10, ProcessMin10, ProcessMax10, ProcessOption10, ProcessUpgrade10)" +
                " Select ControlID, ControlType, ControlCode, ControlName, ProcessType1, ProcessCode1, ProcessName1, ProcessMin1, ProcessMax1, ProcessOption1, ProcessUpgrade1, ProcessType2, ProcessCode2, ProcessName2, ProcessMin2, ProcessMax2, ProcessOption2, ProcessUpgrade2, ProcessType3, ProcessCode3, ProcessName3, ProcessMin3, ProcessMax3, ProcessOption3, ProcessUpgrade3, ProcessType4, ProcessCode4, ProcessName4, ProcessMin4, ProcessMax4, ProcessOption4, ProcessUpgrade4, ProcessType5, ProcessCode5, ProcessName5, ProcessMin5, ProcessMax5, ProcessOption5, ProcessUpgrade5, LinkID, ProcessType6, ProcessCode6, ProcessName6, ProcessMin6, ProcessMax6, ProcessOption6, ProcessUpgrade6, ProcessType7, ProcessCode7, ProcessName7, ProcessMin7, ProcessMax7, ProcessOption7, ProcessUpgrade7, ProcessType8, ProcessCode8, ProcessName8, ProcessMin8, ProcessMax8, ProcessOption8, ProcessUpgrade8, ProcessType9, ProcessCode9, ProcessName9, ProcessMin9, ProcessMax9, ProcessOption9, ProcessUpgrade9, ProcessType10, ProcessCode10, ProcessName10, ProcessMin10, ProcessMax10, ProcessOption10, ProcessUpgrade10 From {1}.ModifierControl";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
            
            // Karthi Added on 12-09-25
            // ModifierControl duplicate delete 

            string ModifierControl_Del_Query = " DELETE Child "
                        + " FROM {0}.ModifierControl AS Child "
                        + " LEFT JOIN {1}.ModifierControl AS HQ "
                        + " ON Child.ControlID = HQ.ControlID "
                        + " WHERE HQ.ControlID IS NULL; ";

            string cmd_ModifierControl_Del = string.Format(ModifierControl_Del_Query, BRAlias, HQAlias);
            D = ObjMain.ExecuteNonQuery(cmd_ModifierControl_Del);

        }
        //for Reasons
        public void SaveI14(string BranchPath)
        {
            string sCmd = string.Empty;     
            string sCr = string.Empty;

            sCmd = "Replace INTO {0}.Reasons(ID, Reason, Type)" +
                " Select ID, Reason, Type From {1}.Reasons";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
        }
        //for InventoryCostAverage
        public void SaveI15(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;

            sCmd = "Replace INTO {0}.InventoryCostAverage(InventoryID, PQuantity, PAmount, CutQuantity, CutAmount, UnitCost, RetailQty, RetailAmount)" +
                " Select InventoryID, PQuantity, PAmount, CutQuantity, CutAmount, UnitCost, RetailQty, RetailAmount From {1}.InventoryCostAverage";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
        }
        //for InventoryCostAverage Cost Only
        public void SaveI16(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;

            sCmd = " Update {0}.InventoryCostAverage IC Left Outer Join {1}.InventoryCostAverage IC1 On IC.InventoryID=IC1.InventoryID" +
                   " Set IC.UnitCost=IC1.UnitCost";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
        }
        //for Update PriceshiftID Only
        public void SaveI17(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;

            sCmd = " Update {0}.Inventory I Left Outer Join {1}.Inventory I1 On I.InventoryID=I1.InventoryID " +
                   " Set I.PriceshiftID=I1.PriceshiftID Where I.InventoryID Is Not Null ";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
        }
        //for InventoryPackage
        public void SaveI18(string BranchPath)
        {
            DeletePackageIDS();
            string sCmd = string.Empty;
            string sCr = string.Empty;

            sCmd = "Replace INTO {0}.InventoryPackage(PackageID, InventoryID, PackageInventoryID, Quantity, Price, Updated)" +
                " Select PackageID, InventoryID, PackageInventoryID, Quantity, Price, Updated From {1}.InventoryPackage";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
        }
        /// <summary>
        /// This DeletePackage ids not there in branchHq those are deleted from  Branch  inorder to maintain data integrity 
        /// </summary>
        public void DeletePackageIDS()
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;
            try
            {
                sCmd = " DELETE D FROM  {0}.InventoryPackage AS D LEFT JOIN  {1}.InventoryPackage AS S  On  D.Packageid=S.Packageid WHERE S.Packageid IS NULL;";
                string command = string.Format(sCmd, BRAlias, HQAlias);
                Q = ObjMain.ExecuteNonQuery(command);

            }
            catch (Exception ex) 
            {
                throw ex;            
            }
            
        }


        //for Priceshift New
        public void SaveI19(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;
            //InventoryPromix
            sCmd = "Replace INTO {0}.InventoryPromix(MixMatchID, Name, TimeType, FromTime, ToTime, PriceType, Mixed, OddDisc, QuantityShiftType, Quantity1, DiscValue1, Quantity2, DiscValue2, Quantity3, DiscValue3, Quantity4, DiscValue4, Quantity5, DiscValue5, Description, Active, NoDiscQty)" +
                " Select MixMatchID, Name, TimeType, FromTime, ToTime, PriceType, Mixed, OddDisc, QuantityShiftType, Quantity1, DiscValue1, Quantity2, DiscValue2, Quantity3, DiscValue3, Quantity4, DiscValue4, Quantity5, DiscValue5, Description, Active, NoDiscQty From {1}.InventoryPromix";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
            //nid added for removing deleted records from branch table if records deleted from hq table on 16-Nov-2023
            sCmd = "DELETE {0}.Inventorypromix FROM {0}.Inventorypromix LEFT JOIN {1}.Inventorypromix " +
                 " ON {0}.Inventorypromix.MixMatchID = {1}.Inventorypromix.MixMatchID " +
                 " WHERE {1}.Inventorypromix.MixMatchID is NULL; ";
            string command2 = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command2);


            //InventoryPromotionDetail
            sCmd = "Replace INTO {0}.InventoryPromotionDetail(MixMatchID, BarCode, QTY, IPDID)" +
                " Select MixMatchID, BarCode, QTY, IPDID From {1}.InventoryPromotionDetail";
            string command1 = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command1);
            //nid added for removing deleted records from branch table if records deleted from hq table on 16-Nov-2023
            sCmd = "DELETE {0}.Inventorypromotiondetail FROM {0}.Inventorypromotiondetail LEFT JOIN {1}.Inventorypromotiondetail " +
                " ON {0}.Inventorypromotiondetail.IPDID = {1}.Inventorypromotiondetail.IPDID " +
                " WHERE {1}.Inventorypromotiondetail.IPDID is NULL; ";
            string command3 = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command3);
        }
        //for CondimentGroupDetails
        public void SaveI20(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;

            sCmd = " Replace INTO {0}.CondimentGroupDetails(CondimentGroupID, CondimentID,CGID)" +
                  "  Select CondimentGroupID, CondimentID,CGID  From {1}.CondimentGroupDetails";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
            // Karthi Added on 11-09-25
            // CondimentGroup duplicate delete 
            string CondimentGroupDetails_Del_Query = " DELETE Child "
                        + " FROM {0}.CondimentGroupDetails AS Child "
                        + " LEFT JOIN {1}.CondimentGroupDetails AS HQ "
                        + " ON Child.CGID = HQ.CGID "
                        + " WHERE HQ.CGID IS NULL; ";

            string cmd_CondimentGroupdetail_Del = string.Format(CondimentGroupDetails_Del_Query, BRAlias, HQAlias);
            D = ObjMain.ExecuteNonQuery(cmd_CondimentGroupdetail_Del);

        }

        ///from WebOrdePromo
        public void SaveI21(string BranchPath)
        {

            // WebOrdePromo duplicate delete 
            string webpromo_scmd = " DELETE Child "
                        + " FROM {0}.WebOrderPromo AS Child "
                        + " LEFT JOIN {1}.WebOrderPromo AS HQ "
                        + " ON Child.id = HQ.id "
                        + " WHERE HQ.id IS NULL; ";

            string scmdDel = string.Format(webpromo_scmd, BRAlias, HQAlias);
            D = ObjMain.ExecuteNonQuery(scmdDel);

            string sCmd = string.Empty;
            string sCr = string.Empty;
            sCmd = "Replace INTO {0}.WebOrderPromo(InventoryId,Barcode,MainPosition,SubPosition) " +
                  " Select InventoryId,Barcode,MainPosition,SubPosition from {1}.WebOrderPromo;";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);

        }


        //for RunningNumbers
        public void UpdateRunningNo()
        { 
            string sCmd = string.Empty;
            sCmd = "Update  {0}.RunningNumber As A Left Outer Join {1}.RunningNumber AS B On A.ModuleName=B.ModuleName SET A.RunningNo=B.RunningNo" +
                " Where A.ModuleName in ('InventoryUnit','InventoryCategory','Group1','Group2','Group3','Group4','Group5','Inventory','InventoryPriceShift','Condiment','CondimentGroup','ModifierControl','Reasons')";

            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
        }
        //InvnetoryUpdate Ends here**********************************************************************************************************

        //PeopleUpdate Starts here***********************************************************************************************************
        protected void btnUpdatePeople_Click(object sender, EventArgs e)
        {
            PeopleDataDistributionProc();
        }
        //for data distribution to selected branches
        public void PeopleDataDistributionProc()
        {
            string sCmd = string.Empty;
            string sBranchPath = string.Empty;
            int lBranchID = 0;
            int i = 0;

            //for dummy db connections
            if (cmbBranches.SelectedItem.Text == "All")
            {
                sCmd = "Select * From " + HQAlias + ".Branches Where Active=1 And UpdateBranch=1 And BranchID<>1";
            }
            else
            {
                sCmd = "Select * From " + HQAlias + ".Branches Where BranchID=" + Microsoft.Security.Application.Encoder.HtmlEncode(cmbBranches.SelectedItem.Value);
            }
            DT = ObjMain.GetData_DT(sCmd);
            foreach (DataRow dr in DT.Rows)
            {
                sBranchPath = Convert.ToString(dr["Datapath"]);
                lBranchID = Convert.ToInt16(dr["BranchID"]);
                BRAlias = Convert.ToString(dr["BranchName"]);

                //BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
                //Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                //BranchAlias = Cmb_BranchName;

                //for checking selected checkbox values for updating

                #region  Post:Branch Ini Validation 

                string validation_Command = "select inivalue from {0}.weborderinginisetting where id = 48;";
                string _commmand = string.Format(validation_Command, BRAlias);
                string Chk_Post = ObjMain.ExecuteScalar(_commmand);
                if (Chk_Post == "1")
                {


                    if (chkEmployees.Checked == true)
                    {
                        SaveModuleP(1, sBranchPath, lBranchID);
                    }
                    if (chkOperators.Checked == true)
                    {
                        SaveModuleP(2, sBranchPath, lBranchID);
                    }
                    if (chkCustomers.Checked == true)
                    {
                        SaveModuleP(3, sBranchPath, lBranchID);
                    }
                    if (chkMemberMaster.Checked == true)
                    {
                        SaveModuleP(4, sBranchPath, lBranchID);
                    }
                    if (chkRoles.Checked == true)
                    {
                        SaveModuleP(5, sBranchPath, lBranchID);
                    }
                    if (chkPermissionSet.Checked == true)
                    {
                        SaveModuleP(6, sBranchPath, lBranchID);
                    }                   
                }
                else
                { 
                
                }

                #endregion


            }
            //for update RunningNumebrs
            UpdateRunningNoP();

            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Update Branch (People Information) completed Successfully ');location.href='UpdateInventory.aspx' ", true);
        }

        //for function selection for updates
        public void SaveModuleP(int pIndex, string pBranchPath, int pBranchID)
        {
            switch (pIndex)
            {
                case 1:
                    {
                        SaveP00(pBranchPath);
                        break;
                    }
                case 2:
                    {
                        SaveP01(pBranchPath);
                        break;
                    }
                case 3:
                    {
                        SaveP02(pBranchPath);
                        break;
                    }
                case 4:
                    {
                        SaveP03(pBranchPath);
                        break;
                    }
                case 5:
                    {
                        SaveP04(pBranchPath);
                        break;
                    }
                case 6:
                    {
                        SaveP05(pBranchPath);
                        break;
                    }
                case 7:
                    {
                        SaveP06(pBranchPath);
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
        }
        //for Employees
        public void SaveP00(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;

            sCmd = "Replace INTO {0}.Employees(EmployeeID, Fixed, EmployeeNumber, EmployeeName, Sex, Birthdate, DateHired, Title, Address, City, StateOrProvince, Country, PostalCode, EmailAddress, WorkPhone, HomePhone, Mobile, Pager, Photograph, Active, Updated, Notes, BranchUpdated, FingurePath, VoucherAmt, Commission)" +
                " Select EmployeeID, Fixed, EmployeeNumber, EmployeeName, Sex, Birthdate, DateHired, Title, Address, City, StateOrProvince, Country, PostalCode, EmailAddress, WorkPhone, HomePhone, Mobile, Pager, Photograph, Active, Updated, Notes, BranchUpdated, FingurePath, VoucherAmt, Commission From {1}.Employees";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
        }
        //for Operators
        public void SaveP01(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;

            sCmd = "Replace INTO {0}.Operators(OperatorID, Fixed, DateCreated, OperatorName, OperationPassword, EmployeeID, SysAdm, Active, Notes, Updated, BranchUpdated, RoleId, CheckIn)" +
                " Select OperatorID, Fixed, DateCreated, OperatorName, OperationPassword, EmployeeID, SysAdm, Active, Notes, Updated, BranchUpdated, RoleId, CheckIn From {1}.Operators";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
        }
        //for Customers
        public void SaveP02(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;

            sCmd = "Replace INTO {0}.Customers(CustomerID, CustomerNumber, ID, CustomerName, CompanyName, OpenDate, LastDate, Birthday, ExpiryDate, Address, PostalCode, City, StateOrProvince, Country, Tel1, Tel2, Fax, Mobile, Pager, EmailAddress, Contact, Active, Updated, Notes, CardType, BranchUpdated, EmployeeNumber, MotherNric, MotherName, MotherMobile, MotherEmail, FatherNric, FatherName, FatherMobile, FatherEmail, Age, Relation1, Relation2)" +
                " Select CustomerID, CustomerNumber, ID, CustomerName, CompanyName, OpenDate, LastDate, Birthday, ExpiryDate, Address, PostalCode, City, StateOrProvince, Country, Tel1, Tel2, Fax, Mobile, Pager, EmailAddress, Contact, Active, Updated, Notes, CardType, BranchUpdated, EmployeeNumber, MotherNric, MotherName, MotherMobile, MotherEmail, FatherNric, FatherName, FatherMobile, FatherEmail, Age, Relation1, Relation2 From {1}.Customers";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
        }
        //for MemberMaster
        public void SaveP03(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;

            //Membercard
            sCmd = "Replace INTO {0}.Membercard(MemberCardId, CardName, NFDForDiscoutedItem, NFDAfterTotalDisc, DiscAmount1, DiscValue1, DiscAmount2, DiscValue2, DiscAmount3, DiscValue3, DiscAmount4, DiscValue4, DiscAmount5, DiscValue5, PointAmount1, PointValue1, PointAmount2, PointValue2, PointAmount3, PointValue3, PointAmount4, PointValue4, PointAmount5, PointValue5, Updated, BranchUpdated, Active)" +
                " Select MemberCardId, CardName, NFDForDiscoutedItem, NFDAfterTotalDisc, DiscAmount1, DiscValue1, DiscAmount2, DiscValue2, DiscAmount3, DiscValue3, DiscAmount4, DiscValue4, DiscAmount5, DiscValue5, PointAmount1, PointValue1, PointAmount2, PointValue2, PointAmount3, PointValue3, PointAmount4, PointValue4, PointAmount5, PointValue5, Updated, BranchUpdated, Active From {1}.Membercard";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
            //MemberCardItemDisc
            sCmd = "Replace INTO {0}.MemberCardItemDisc(MemberCardId, InventoryID, DiscValue, DiscAmt) " +
                " Select MemberCardId, InventoryID, DiscValue, DiscAmt From {1}.MemberCardItemDisc ";
            string command1 = string.Format(sCmd, BRAlias, HQAlias);

            Q = ObjMain.ExecuteNonQuery(command1);
            //MemberCardDeptDisc
            sCmd = "Replace INTO {0}.MemberCardDeptDisc(MemberCardID, MemberDeptNumber, MemberDeptID, DiscValue, DiscAmt)" +
                " Select MemberCardID, MemberDeptNumber, MemberDeptID, DiscValue, DiscAmt From {1}.MemberCardDeptDisc";
            string command2 = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command2);
            //MemberBirthDisc
            sCmd = "Replace INTO {0}.MemberBirthDisc(MemberCardID, BirthDay, BirthDayDisc, BirthDayValue, Repeated, HistoryAmount, AmountDisc, AmountValue, Amount, DiscAfterAmount, DiscbyMonth, FromDate, ToDate)" +
                " Select MemberCardID, BirthDay, BirthDayDisc, BirthDayValue, Repeated, HistoryAmount, AmountDisc, AmountValue, Amount, DiscAfterAmount, DiscbyMonth, FromDate, ToDate From {1}.MemberBirthDisc";
            string command3 = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command3);
        }
        //for Roles
        public void SaveP04(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;

            sCmd = " Replace INTO {0}.RoleList(RoleId, RoleName)" +
                   " Select RoleId, RoleName From {1}.RoleList" ;
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
        }
        //for PermissionSet
        public void SaveP05(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;

            sCmd = "Replace INTO {0}.PermissionSet(PermissionID, ParentId, Name)" +
                " Select PermissionID, ParentId, Name From {1}.PermissionSet";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
        }
        //for RolePermission
        public void SaveP06(string BranchPath)
        {
            string sCmd = string.Empty;
            string sCr = string.Empty;

            sCmd = "Replace INTO {0}.RolePermission(DetailId, RoleId, PermissionId)" +
                " Select DetailId, RoleId, PermissionId From {1}.RolePermission";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
        }

        //for RunningNumbers
        public void UpdateRunningNoP()
        {
            string sCmd = string.Empty;
            sCmd = "Update {0}.RunningNumber As A Left Outer Join {1}.RunningNumber AS B On A.ModuleName=B.ModuleName SET A.RunningNo=B.RunningNo" +
                " Where A.ModuleName in ('Employees','Operator','RoleList','Customer')";
            string command = string.Format(sCmd, BRAlias, HQAlias);
            Q = ObjMain.ExecuteNonQuery(command);
        }

        //PeopleUpdate Ends here***********************************************************************************************************


        public string IsUnderPostFuction(string BRAlias)
        {
            string validation_Command = "select inivalue from {0}.weborderinginisetting where id = 48;";
            string _commmand = string.Format(validation_Command, BRAlias);
            string Chk_Post = ObjMain.ExecuteScalar(_commmand);
            return Chk_Post;
        }



    }
}