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 System.Data.OleDb;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing.Printing;
using System.Drawing;
using System.Drawing.Text;
using System.Net;

namespace SUNWEB
{
    public partial class Iorderlasetting : System.Web.UI.Page
    {
        MYSQLCLOUD ObjCloudMain = new MYSQLCLOUD();
        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;
        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 Dt2 = new DataTable();
        DataTable DtLicense = new DataTable();
        DataTable DtSuperAdmin = new DataTable();
        DataTable DtMultiBranch = new DataTable();
        DataTable DtNormalUser = new DataTable();
        DataTable DTProductSale = new DataTable();
        DataTable DtRead = new DataTable();
        DataTable DtTimeRead = new DataTable();
        DataTable DtInventoryList = new DataTable();
        DataTable DtinventoryCnt = new DataTable();

        MySqlCommand Cmdv = new MySqlCommand();
        // User Variables 
        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;
        // License Variables ..
        string PID = string.Empty;
        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_IorderlaSetting = string.Empty;
        string Branchidval = string.Empty;
        string BranchFullName = string.Empty;
        string Curr_BranchFullName = string.Empty;
        string Cmb_BranchName = string.Empty;

        string HQ = string.Empty;
        string Cm_id = string.Empty;
        string ISHQ = string.Empty;
        DataSet DS = new DataSet();

        string Vw_Chk = string.Empty;

        string RBranchid = string.Empty;
        string Branchid = string.Empty;
        string BranchAlias = string.Empty;
        string sActive = string.Empty;
        string ReadHQ = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {

            PID = "38";      // This is for Iorderla Setting 


            if (!IsPostBack)
            {
                //txtStartDt.Text = Curr_DateOnly;
                //txtEndDt.Text = Curr_DateOnly;

                lblSInvetoryName.Visible = false;
                lblSInvetoryDesc.Visible = false;
                // For CommonUpdate
                chkCommonUpdate.Visible = false;
                lblCommonUpateText.Visible = false;
                // For NotAvailable 
                chkNotAvilable.Visible = false;
                lblNotAvilable.Visible = false;

                chkOutofStock.Visible = false;
                lblOutofStock.Visible = false;





            }
            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)
                {
                    LoadBranchesOnID();


                    string QueryCal = "select ActiveValue from portaldb.usermaster  where Userid=" + Session["UserId"].ToString() + " ";
                    ActiveCallvalue = ObjMain.ExecuteScalar(QueryCal);
                    if (ActiveCallvalue != " " || ActiveCallvalue != "Select Branch")
                    {
                        ActiveCall();
                    }
                }
            }
            else if (((Convert.ToBoolean(Session["IsSuperAdmin"])) == true) && (Session["PortalTypeId"].ToString() == ""))
            {
                if (!IsPostBack)
                {
                    LoadBranchesOnID();
                }
                cd_BranchAlias = cmbBranches.SelectedItem.Text;
                //LoadInventoryListGrid(cd_BranchAlias, txtStartDt.Text, txtEndDt.Text);
            }
            else if (((Convert.ToBoolean(Session["IsMultiBranch"])) == true) && (Session["PortalTypeId"].ToString() == ""))
            {
                if (!IsPostBack)
                {
                    LoadBranchesOnID();
                    //  LoadInventoryName();
                    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 = "Branch Name" + "&nbsp;" + "-" + "&nbsp;" + Curr_BranchFullName + "&nbsp; ";

                cd_BranchAlias = Session["Branch_HQ"].ToString();
                Datapath = Session["DataPath"].ToString();
                PortalTypeId = Session["PortalTypeId"].ToString();

                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_IorderlaSetting"].ToString() == "N") || (Session["Page_Acess"].ToString() == "N"))
                {
                    Response.Redirect("DeniedPage.aspx");
                }
                else
                {
                    // lblMsgTotalSelection.Text = "Item Sale  Between" + "&nbsp;&nbsp;&nbsp;&nbsp;" + txtStartDt.Text + "&nbsp;&nbsp;&nbsp;  and  &nbsp;&nbsp;&nbsp;" + txtEndDt.Text;
                    if (!IsPostBack)
                    {
                        if (Session["Isdowld_Acess"].ToString() == "N")
                        {
                            // btnExcel.Enabled = false;
                            Response.Write(@"<script language='javascript'>alert('No Access To download.')</script>");
                        }
                        else
                        {
                            //btnGetReports.Visible = true;

                            if (cmbBranches.Visible == false)
                            {

                                //  LoadBaseTiming();
                            }

                        }


                    }

                }

            }
            else
            {

            }


        }


        public void ActiveCall()
        {
            string QueryCal = "select ActiveValue from portaldb.usermaster  where Userid={0} ; ";
            string command = string.Format(QueryCal, Session["UserId"].ToString());
            ActiveCallvalue = ObjMain.ExecuteScalar(command);
            if (ActiveCallvalue == "")
            {



                if (ActiveCallvalue == "")
                {
                    #region  Default Branch Validation:Either HQ or First Branch Based On IsHQ Ini in PMS 
                    string _hq_query = "select IsHQ from  portaldb.companymaster where cm_id=" + Session["CM_ID"] + " ;";
                    string _Is_HQ_Enabled = Convert.ToString(ObjMain.ExecuteScalar(_hq_query));
                    if (_Is_HQ_Enabled == "1")
                    {
                        ActiveCallvalue = ObjSel.GetHQ(Session["Branch_HQ"].ToString());

                    }
                    else if (_Is_HQ_Enabled == "0")
                    {
                        ActiveCallvalue = ObjSel.GETBrachAlias8(Session["CM_ID"].ToString(), Session["Branch_HQ"].ToString(), Session["UserId"].ToString());
                    }

                    #endregion




                }

            }

            #region  HQ Validation in ActiveCall Function : Starts 
            cd_BranchAlias = ActiveCallvalue;
            // HQ Validation For Loading Branch Name : Dropdown  Added Karthik  On 02-12-21 

            ReadHQ = ObjUserBL.GetBranchFullNameByBranchName(Session["Branch_HQ"].ToString(), cd_BranchAlias);

            if (ReadHQ == "HQ")
            {
                cmbBranches.Items.FindByText(ReadHQ).Selected = true;
            }
            else
            {
                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);

            }

            #endregion



            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=" + 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_IorderlaSetting = DtLicense.Rows[0]["IORDERLAPRODUCTTIMING"].ToString();
                Session["L_IorderlaSetting"] = L_IorderlaSetting;
            }
            if ((Session["L_IorderlaSetting"] == null)|| Session["L_IorderlaSetting"].ToString() == "N" || Session["Page_Acess"].ToString() == "N")
            {

                if (cmbBranches.SelectedItem.Text == "HQ")
                {

                    #region  Validation Between HQ Branch and Normal Branch 
                    BranchFullName = cmbBranches.SelectedItem.Text;      // This will Return Branch FullName 
                    if (BranchFullName == "HQ")
                    {
                        BranchAlias = Session["Branch_HQ"].ToString();
                    }
                    else
                    {
                        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;

                    }
                    #endregion

                    LoadBaseTiming(BranchAlias);
                    LoadInventoryview();
                    INS_ScheduleFlag(BranchAlias);
                    // Call Empty Gridview
                    grdTimingbyid.DataSource = null;
                    grdTimingbyid.DataBind();
                    grdTimingbyid.EmptyDataText = "Select the InventoryName To Fetch Data !";



                }
                else
                {
                    Response.Redirect("DeniedPage.aspx");

                }



            }
            else
            {
                cd_BranchAlias = Microsoft.Security.Application.Encoder.HtmlEncode(cmbBranches.SelectedItem.Text);
                //LoadInventoryName(Cmb_BranchName);
                LoadBaseTiming(Cmb_BranchName);
                LoadInventoryview();
                INS_ScheduleFlag(Cmb_BranchName);
                // Call Empty Gridview
                grdTimingbyid.DataSource = null;
                grdTimingbyid.DataBind();
                grdTimingbyid.EmptyDataText = "Select the InventoryName To Fetch Data !";

            }

        }
        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, "Select Branch");
            }
        }
        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, "Select Branch");
            }
        }
        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");

            }

        }

        public void LoadBranchesOnID()
        {
            DT = ObjUserBL.GetBranchIDList(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), Session["CM_ID"].ToString());
            StringBuilder StrBObj = new StringBuilder();

            for (int i = 0; i < DT.Rows.Count; i++)
            {
                DataRow dr = DT.Rows[i];
                RBranchid = dr["Branchid"].ToString();
                if (StrBObj.Length != 0)
                {

                    RBranchid = "," + RBranchid;
                    StrBObj.Append("  " + RBranchid + "    ");
                }
                else
                {
                    Branchid = RBranchid;
                    StrBObj.Append("SELECT  Branchid ,BranchName,branchfullname,datapath FROM " + Session["Branch_HQ"].ToString() + ".branches where branchid in (1 , " + Branchid + " ");

                }


            }
            StrBObj.Append(")");
            Dt2 = ObjCloudMain.GetData_DT(StrBObj.ToString());

            if (Dt2.Rows.Count > 0)
            {
                cmbBranches.DataSource = Dt2;
                cmbBranches.DataTextField = "branchfullname";
                cmbBranches.DataValueField = "Branchid";
                cmbBranches.DataBind();
                cmbBranches.Items.Insert(0, "Select Branch");
            }

        }


        //protected void btnGetReports_Click(object sender, EventArgs e)
        //{

        //}

        protected void cmbBranches_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbBranches.SelectedIndex == 0)
            {
                Response.Write("Iorderlasetting.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.GetFirstBranch7(Session["Branch_HQ"].ToString(), Session["UserId"].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=" + Microsoft.Security.Application.Encoder.HtmlEncode(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);
                }



                if (DtLicense.Rows.Count > 0)
                {
                    L_MaxUserCnt = DtLicense.Rows[0]["UserCnt"].ToString();
                    L_IorderlaSetting = DtLicense.Rows[0]["IORDERLAPRODUCTTIMING"].ToString();
                    Session["L_IorderlaSetting"] = L_IorderlaSetting;


                }

                if (Session["L_IorderlaSetting"].ToString() == "N" || Session["Page_Acess"].ToString() == "N")
                {
                    if (cmbBranches.SelectedItem.Text == "HQ")
                    {

                        #region  Validation Between HQ Branch and Normal Branch 
                        BranchFullName = cmbBranches.SelectedItem.Text;      // This will Return Branch FullName 
                        if (BranchFullName == "HQ")
                        {
                            BranchAlias = Session["Branch_HQ"].ToString();
                        }
                        else
                        {
                            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;

                        }
                        #endregion
                    }
                    else
                    {
                        Response.Redirect("DeniedPage.aspx");
                    }



                }
                else if (Session["Write_Acess"].ToString() == "N")
                {
                    btnGeneratebyinventoryid.Enabled = false;
                    btnGenerateIorderlaTiming.Enabled = false;
                    //btnUpdate.Enabled = false;
                }

                else
                {
                    // btnExcel.Enabled = true;
                    INS_ScheduleFlag(Cmb_BranchName);
                    LoadInventoryview();

                    btnGeneratebyinventoryid.Enabled = true;
                    btnGenerateIorderlaTiming.Enabled = true;
                    //btnUpdate.Enabled = true;
                    //LoadInventoryName(Cmb_BranchName);
                    LoadBaseTiming(Cmb_BranchName);


                }
            }
            UPDATEBRANCH(Cmb_BranchName);
        }

        //protected void cmbInventoryName_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    if (cmbBranches.Visible == false)
        //    {

        //        BranchAlias = Session["Branch_HQ"].ToString();

        //    }
        //    else
        //    {
        //        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;
        //    }

        //    string ReadQry = "select inventoryid from " + BranchAlias + ".inventory where inventoryid='" + cmbInventoryName.SelectedValue + "';";
        //    string RInvenid=ObjCloudMain.ExecuteScalar(ReadQry);
        //    txtInvenId.Text = RInvenid;

        //    LoadTimingByInventoryId(BranchAlias, RInvenid);




        //}
        public void LoadBaseTiming(string Branchname)
        {
            string query = "select * from  {0}.Baseschedule;";
            string command = string.Format(query, Branchname);
            DT = ObjCloudMain.GetData_DT(command);
            grdTiming.DataSource = DT;
            grdTiming.DataBind();
        }


        public void LoadTimingByInventoryId(string Branchname, string inventoryid)
        {

            #region   Load the IsCommonUpdate Part
            chkCommonUpdate.Visible = true;
            lblCommonUpateText.Visible = true;




            string query1 = "select IsCommonUpdate from  {0}.timescheduleFlag where inventoryid=@inventoryid;";
            string command1 = string.Format(query1, Branchname);
            MySqlParameter[] parameters1 =
                  {
                     new MySqlParameter("@inventoryid",inventoryid),

                  };
            string ChkBoxVal = ObjMain.NewExecuteScalar(command1, parameters1);


            if (ChkBoxVal.Equals("0"))
            {
                chkCommonUpdate.Checked = false;
            }
            else
            {
                chkCommonUpdate.Checked = true;
            }
            #endregion


            #region Load NotAvailable Part
            chkNotAvilable.Visible = true;
            lblNotAvilable.Visible = true;

         

            string query2 = "select IsNotAvailable from  {0}.timescheduleFlag where inventoryid=@inventoryid;";
            string command2 = string.Format(query2, Branchname);
            MySqlParameter[] parameters2 =
                  {
                     new MySqlParameter("@inventoryid",inventoryid),

                  };
            string ChkBoxVal2 = ObjMain.NewExecuteScalar(command2, parameters2);

            if (ChkBoxVal2.Equals("0"))
            {
                chkNotAvilable.Checked = false;
            }
            else
            {
                chkNotAvilable.Checked = true;
            }
            #endregion

            string query = "select * from  {0}.timeschedule where inventoryid=@inventoryid;";
            string command = string.Format(query, Branchname);

            MySqlParameter[] parameters =
                   {
                     new MySqlParameter("@inventoryid",inventoryid),

                    };

            DT = ObjMain.NewGetData_DT(command, parameters);
            grdTimingbyid.DataSource = DT;
            grdTimingbyid.DataBind();


            LoadPOSKIOSKNotAvailable(Branchname, inventoryid); //john added on 13-March-2024
        }

        public void LoadPOSKIOSKNotAvailable(string Branchname, string inventoryid)
        {
            chkOutofStock.Visible = true;
            lblOutofStock.Visible = true;

            string query = "select outofstock from {0}.inventory where inventoryid = @inventoryid";
            string scmd = string.Format(query, Branchname);
            MySqlParameter[] mySqlParameter =
            {
                new MySqlParameter("@inventoryid",inventoryid),
            };

            string chkValue = ObjMain.NewExecuteScalar(scmd, mySqlParameter);
            if (chkValue.Equals("0"))
            {
                chkOutofStock.Checked = false;
            }
            else
            {
                chkOutofStock.Checked = true;
            }
                
        }

        //public void LoadInventoryName(string Branchname)
        //{
        //    DtInventoryList = ObjCloudMain.GetData_DT("select inventoryid, inventoryname from " + Branchname + ".inventory;");
        //    cmbInventoryName.DataTextField = "inventoryname";
        //    cmbInventoryName.DataValueField = "inventoryid";
        //    cmbInventoryName.DataSource = DtInventoryList;
        //    cmbInventoryName.DataBind();
        //    cmbInventoryName.Items.Insert(0, "Select InventoryName");

        //}

        //protected void btnUpdate_Click(object sender, EventArgs e)
        //{
        //    if (cmbBranches.Visible == false)
        //    {

        //        BranchAlias = Session["Branch_HQ"].ToString();

        //    }
        //    else
        //    {
        //        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;
        //    }






        //    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert('Timing Updated  Successfully ');location.href='Iorderlasetting.aspx'", true);

        //}

        protected void btnGenerateIorderlaTiming_Click(object sender, EventArgs e)
        {
            if (cmbBranches.Visible == false)
            {

                BranchAlias = Session["Branch_HQ"].ToString();

            }
            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 )
                BranchAlias = Cmb_BranchName;
            }



            foreach (GridViewRow gvrow in grdTiming.Rows)
            {
                var checkbox = gvrow.FindControl("chkSelect") as CheckBox;
                if (checkbox.Checked)
                {
                    Label lblid = gvrow.FindControl("lblid") as Label;
                    Label lblDays_Value = gvrow.FindControl("lblDays") as Label;

                    TextBox txtRT1_StTm = (TextBox)gvrow.FindControl("txtT1_StTm");
                    TextBox txtRT1_EndTm = (TextBox)gvrow.FindControl("txtT1_EndTm");
                    TextBox txtRT2_EndTm = (TextBox)gvrow.FindControl("txtT2_EndTm");

                    string Added = txtRT1_StTm.Text;
                    string NewStartDt = string.Concat(Curr_DateOnly, " ", Added);
                    string Added1 = txtRT1_EndTm.Text;
                    string NewEndtDt = string.Concat(Curr_DateOnly, " ", Added1);
                    string Added2 = txtRT2_EndTm.Text;
                    string NewSplEndtDt = string.Concat(Curr_DateOnly, " ", Added2);




                    string Query = "Update  {0}.Baseschedule set T1_StTm=@T1_StTm, T1_EndTm=@T1_EndTm ,T2_EndTm=@T2_EndTm where id=@id AND Days=@Days; ";
                    string command = string.Format(Query, BranchAlias);

                    MySqlParameter[] parameters =
                   {
                     new MySqlParameter("@T1_StTm",NewStartDt),
                     new MySqlParameter("@T1_EndTm",NewEndtDt),
                     new MySqlParameter("@T2_EndTm",NewSplEndtDt),
                     new MySqlParameter("@id",lblid.Text),
                     new MySqlParameter("@Days",lblDays_Value.Text),
                   };

                    K = ObjMain.ExecuteQuery(command, parameters);

                }

            }


            // Step : 1
            // Get the list of Inventoryid's 
            string Inventoryid = string.Empty;
            string Barcode = string.Empty;

            string ReadInventoryids = "select Inventoryid,Barcode from {0}.inventory order by Inventoryid; ";
            string commandReads = string.Format(ReadInventoryids, BranchAlias);
            DtRead = ObjCloudMain.GetData_DT(commandReads);

            for (int i = 0; i < DtRead.Rows.Count; i++)
            {
                DataRow dr = DtRead.Rows[i];

                Inventoryid = dr["Inventoryid"].ToString();
                Barcode = dr["BarCode"].ToString();


                // Step : 2
                // Read the Timing Baseschedule - Day by day 

                string ReadBaseTiming = "select * from  {0}.Baseschedule;";
                string CommandBT = string.Format(ReadBaseTiming, BranchAlias);
                DtTimeRead = ObjCloudMain.GetData_DT(CommandBT);

                for (int k = 0; k < DtTimeRead.Rows.Count; k++)
                {
                    DataRow drr = DtTimeRead.Rows[k];
                    string DayName = drr["Days"].ToString();
                    string T1_StTm = drr["T1_StTm"].ToString();
                    string T1_EndTm = drr["T1_EndTm"].ToString();
                    string T2_StTm = drr["T2_StTm"].ToString();
                    string T2_EndTm = drr["T2_EndTm"].ToString();
                    string T3_StTm = drr["T3_StTm"].ToString();
                    string T3_EndTm = drr["T3_EndTm"].ToString();


                    // Check Wether the Record Exist Or Not ( Key - Day & Inventoryid )

                    string ChecktimescheduleQuery = "select Barcode from   {0}.timeschedule  where Days=@Days and Inventoryid=@Inventoryid ; ";
                    string command = string.Format(ChecktimescheduleQuery, BranchAlias);

                    MySqlParameter[] parameters =
                    {
                     new MySqlParameter("@Days",DayName),
                     new MySqlParameter("@Inventoryid",Inventoryid),
                   };

                    string Checktimeschedule = ObjMain.NewExecuteScalar(command, parameters);
                    if (Checktimeschedule != "")
                    {


                        string ReadIscommonValue = "select IsCommonUpdate from {0}.timescheduleFlag where Inventoryid={1} ; ";
                        string chk_command = string.Format(ReadIscommonValue, BranchAlias, Inventoryid);
                        string IsCommonUpdate = string.Empty;

                        IsCommonUpdate = ObjMain.ExecuteScalar(chk_command);

                        if (IsCommonUpdate.Equals("0"))
                        {
                            // Call Update : If Exist
                            string UPT_Timing = "Update {0}.timeschedule  set  T1_StTm=@T1_StTm,T1_EndTm=@T1_EndTm,T2_EndTm=@T2_EndTm  where Days=@Days and Inventoryid=@Inventoryid; ";
                            string commandA = string.Format(UPT_Timing, BranchAlias);
                            MySqlParameter[] parametersA =
                              {
                             new MySqlParameter("@T1_StTm",T1_StTm),
                             new MySqlParameter("@T1_EndTm",T1_EndTm),
                             new MySqlParameter("@T2_EndTm",T2_EndTm),
                             new MySqlParameter("@Days",DayName),
                             new MySqlParameter("@Inventoryid",Inventoryid),

                          };


                            K = ObjMain.ExecuteQuery(commandA, parametersA);
                        }
                        else
                        {
                            // IF IsCommonUpdate  Value is '1'  it won't Update From Base schedule 
                        }


                    }
                    else
                    {
                        // Call Insert : If not Exist
                        string Ins_Timing = "insert into {0}.timeschedule (Days,Days_Visibility,T1_StTm,T1_EndTm,T2_StTm,T2_EndTm,T3_StTm,T3_EndTm,Inventoryid,Barcode) "
                        + " values(@Days,Days_Visibility,@T1_StTm,@T1_EndTm,@T2_StTm,@T2_EndTm,@T3_StTm,@T3_EndTm,@Inventoryid,@Barcode); ";
                        string commandB = string.Format(Ins_Timing, BranchAlias);

                        MySqlParameter[] parametersB =
                         {
                            new MySqlParameter("@Days",DayName),
                            new MySqlParameter("@Days_Visibility","1"),
                            new MySqlParameter("@T1_StTm",T1_StTm),
                            new MySqlParameter("@T1_EndTm",T1_EndTm),
                            new MySqlParameter("@T2_StTm",T2_StTm),
                            new MySqlParameter("@T2_EndTm",T2_EndTm),
                            new MySqlParameter("@T3_StTm",T3_StTm),
                            new MySqlParameter("@T3_EndTm",T3_EndTm),
                            new MySqlParameter("@Inventoryid",Inventoryid),
                            new MySqlParameter("@Barcode",Barcode),

                          };
                        K = ObjMain.ExecuteQuery(commandB, parametersB);
                    }

                }


            }



            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert('Timing Generated  Successfully ');location.href='Iorderlasetting.aspx'", true);
        }

        protected void btnGeneratebyinventoryid_Click(object sender, EventArgs e)
        {
            LoadTiming_Byid();

        }

        public void LoadTiming_Byid()
        {
            if (cmbBranches.Visible == false)
            {

                BranchAlias = Session["Branch_HQ"].ToString();

            }
            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 )
                BranchAlias = Cmb_BranchName;
            }


            // Step : 1
            // Get the list of Inventoryid's 
            string Inventoryid = string.Empty;
            string Barcode = string.Empty;

            string ReadInventoryids = "select Inventoryid,Barcode from {0}.inventory where Inventoryid={1} ; ";
            string command = string.Format(ReadInventoryids, BranchAlias, hidvalue.Value);
            DtRead = ObjCloudMain.GetData_DT(command);

            for (int i = 0; i < DtRead.Rows.Count; i++)
            {
                DataRow dr = DtRead.Rows[i];

                Inventoryid = dr["Inventoryid"].ToString();
                Barcode = dr["BarCode"].ToString();


                // Step : 2
                // Read the Timing Baseschedule - Day by day 



                // Existing Approach 

                /*
                string ReadBaseTiming = "select * from  " + BranchAlias + ".Baseschedule;";
                DtTimeRead = ObjCloudMain.GetData_DT(ReadBaseTiming);

                for (int k = 0; k < DtTimeRead.Rows.Count; k++)
                {
                    DataRow drr = DtTimeRead.Rows[k];
                    string DayName = drr["Days"].ToString();
                    string T1_StTm = drr["T1_StTm"].ToString();
                    string T1_EndTm = drr["T1_EndTm"].ToString();
                    string T2_StTm = drr["T2_StTm"].ToString();
                    string T2_EndTm = drr["T2_EndTm"].ToString();
                    string T3_StTm = drr["T3_StTm"].ToString();
                    string T3_EndTm = drr["T3_EndTm"].ToString();


                    // Check Wether the Record Exist Or Not ( Key - Day & Inventoryid )

                    string ChecktimescheduleQuery = "select Barcode from   " + BranchAlias + ".timeschedule  where Days='" + DayName + "' and Inventoryid='" + cmbInventoryName.SelectedValue + "'; ";
                    string Checktimeschedule = ObjCloudMain.ExecuteScalar(ChecktimescheduleQuery);
                    if (Checktimeschedule != "")
                    {
                        // Call Update : If Exist
                        string UPT_Timing = "Update " + BranchAlias + ".timeschedule  set  T1_StTm='" + T1_StTm + "',T1_EndTm='" + T1_EndTm + "'  where Days='" + DayName + "' and Inventoryid='" + cmbInventoryName.SelectedValue + "'; ";
                        K = ObjMain.ExecuteNonQuery(UPT_Timing);
                    }
                    else
                    {
                        // Call Insert : If not Exist
                        string Ins_Timing = "insert into " + BranchAlias + ".timeschedule (Days,Days_Visibility,T1_StTm,T1_EndTm,T2_StTm,T2_EndTm,T3_StTm,T3_EndTm,Inventoryid,Barcode) "
                        + " values('" + DayName + "','1','" + T1_StTm + "','" + T1_EndTm + "','" + T2_StTm + "','" + T2_EndTm + "','" + T3_StTm + "','" + T3_EndTm + "','" + Inventoryid + "','" + Barcode + "'); ";
                        K = ObjCloudMain.ExecuteNonQuery(Ins_Timing);
                    }
                 */

                // Read the IsCommon Value For Update 






                foreach (GridViewRow gvrows in grdTimingbyid.Rows)
                {
                    var checkbox = gvrows.FindControl("chkSelect") as CheckBox;
                    if (checkbox.Checked)
                    {
                        Label lblid = gvrows.FindControl("lblid") as Label;
                        Label lblDays_Value = gvrows.FindControl("lblDays") as Label;

                        TextBox txtRT1_StTm = (TextBox)gvrows.FindControl("txtT1_StTm");
                        TextBox txtRT1_EndTm = (TextBox)gvrows.FindControl("txtT1_EndTm");
                        TextBox txtRT2_EndTm = (TextBox)gvrows.FindControl("txtT2_EndTm");
                        Label lblTInventoryid = gvrows.FindControl("lblInventoryid") as Label;

                        string Added = txtRT1_StTm.Text;
                        string NewStartDt = string.Concat(Curr_DateOnly, " ", Added);
                        string Added1 = txtRT1_EndTm.Text;
                        string NewEndtDt = string.Concat(Curr_DateOnly, " ", Added1);

                        string Added2 = txtRT2_EndTm.Text;
                        string NewsplEndtDt = string.Concat(Curr_DateOnly, " ", Added2);




                        string Query = "Update  {0}.timeschedule set T1_StTm=@T1_StTm, T1_EndTm=@T1_EndTm,T2_EndTm=@T2_EndTm  where id=@id AND Days=@Days and  inventoryid=@inventoryid ;";
                        string commandA = string.Format(Query, BranchAlias);
                        MySqlParameter[] parameters =
                        {
                             new MySqlParameter("@T1_StTm",NewStartDt),
                             new MySqlParameter("@T1_EndTm",NewEndtDt),
                             new MySqlParameter("@T2_EndTm",NewsplEndtDt),
                             new MySqlParameter("@id",lblid.Text),
                             new MySqlParameter("@Days",lblDays_Value.Text),
                             new MySqlParameter("@inventoryid",lblTInventoryid.Text),

                            };

                        K = ObjMain.ExecuteQuery(commandA, parameters);

                    }


                }

                LoadTimingByInventoryId(BranchAlias, hidvalue.Value);
                
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert('Timing Generated  Successfully ');", true);









            }



        }
        protected void cmbSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "ShowModalInventoryList();", true);

        }
        protected void grdInventoryView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                //Determine the RowIndex of the Row whose Button was clicked.
                int rowIndex = Convert.ToInt32(e.CommandArgument);
                //Reference the GridView Row.
                GridViewRow row = grdInventoryView.Rows[rowIndex];
                //Fetch value of Name.
                string Barcode = (row.FindControl("lblBarCode") as Label).Text.ToString().Trim();
                //Page.ClientScript.RegisterStartupScript(this.GetType(), "hash", "location.hash = '#Inventoryx';", true);

            }
        }


        protected void OnSelectedIndexChanged(object sender, EventArgs e)
        {

            if (cmbBranches.Visible == false)
            {
                BranchAlias = Session["Branch_HQ"].ToString();
            }
            else
            {
                if (cmbBranches.SelectedItem.Text == "HQ")
                {
                    BranchFullName = Microsoft.Security.Application.Encoder.HtmlEncode(cmbBranches.SelectedItem.Text);    // This will Return Branch FullName 
                    Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);
                    // UPDATEBRANCH(Cmb_BranchName);
                    BranchAlias = Cmb_BranchName;
                }
                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 )
                    BranchAlias = Cmb_BranchName;
                }
            }



            string Barcode = (grdInventoryView.SelectedRow.FindControl("lblBarCode") as Label).Text;
            // Get the Inventoryid 

            string Query1 = " select inventoryid from {0}.inventory where barcode='{1}' ";
            string command = string.Format(Query1, BranchAlias, Barcode.ToString().Trim());
            string Inventoryid = ObjCloudMain.ExecuteScalar(command);
            hidvalue.Value = Inventoryid;
            LoadTimingByInventoryId(BranchAlias, Inventoryid);

            string Query2 = " select inventoryName from {0}.inventory where inventoryid={1} ";
            string command1 = string.Format(Query2, BranchAlias, hidvalue.Value);
            string sInventoryName = ObjCloudMain.ExecuteScalar(command1);

            string Query3 = " select Description from {0}.inventory where inventoryid={1} ";
            string command2 = string.Format(Query3, BranchAlias, hidvalue.Value);
            string sInventoryDescription = ObjCloudMain.ExecuteScalar(command2);

            lblRInventoryName.Text = sInventoryName;
            lblRInventoryDescription.Text = sInventoryDescription;
            lblSInvetoryName.Visible = true;
            lblSInvetoryDesc.Visible = true;


        }
        protected void txtSearchVal_TextChanged(object sender, EventArgs e)
        {

        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {

            string sActive = string.Empty;

            txtSearchVal.CssClass = "form-control";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "hash", "location.hash = '#Inventoryx';", true);

            //Page.ClientScript.RegisterStartupScript(this.GetType(), "hash", "location.hash = '#GridContend';", true);
            //  ModalPopupExtender7.Show();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "ShowModalInventoryList();", true);

            if (cmbBranches.Visible == true)
            {
                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 )
                BranchAlias = Cmb_BranchName;
            }
            else
            {
                BranchAlias = Session["Branch_HQ"].ToString();
            }



            if (cmbSearch.SelectedIndex == 0)
            {
                //John added This All Condition
                if (chkActiveonly.Checked == true)// John added 02-Nov-2020 Active/InActive Include inside Query
                {
                    sActive = " where I.active <> 0";
                }
                else
                {
                    sActive = " where I.active = 0";
                }
                string Inv_Qry1 = "select * from {0}.inventory as I join  {0}.inventorycategories  as c ON I.Categoryid=c.categoryid " + sActive;
                string command = string.Format(Inv_Qry1, BranchAlias);
                DT = ObjMain.GetData_DT(command);
                grdInventoryView.DataSource = DT;
                grdInventoryView.DataBind();
            }
            else if (cmbSearch.SelectedIndex == 1)
            {
                if (chkActiveonly.Checked == true) // John added 02-Nov-2020 Active/InActive Include inside Query
                {
                    sActive = " and I.active <> 0";
                }
                else
                {
                    sActive = " and I.active = 0";
                }

                string Inv_Qry1 = "select * from {0}.inventory as I join  {0}.inventorycategories  as c ON I.Categoryid=c.categoryid where I.Inventoryid=@SearchText " + sActive;
                string command = string.Format(Inv_Qry1, BranchAlias);

                MySqlParameter[] parameters =
                   {
                     new MySqlParameter("@SearchText",txtSearchVal.Text.ToString().Trim()),
                   };
                DT = ObjMain.NewGetData_DT(command, parameters);
                grdInventoryView.DataSource = DT;
                grdInventoryView.DataBind();

            }
            else if (cmbSearch.SelectedIndex == 2)
            {
                if (chkActiveonly.Checked == true)// John added 02-Nov-2020 Active/InActive Include inside Query
                {
                    sActive = " and I.active <> 0";
                }
                else
                {
                    sActive = " and I.active = 0";
                }


                string Inv_Qry2 = "select * from {0}.inventory as I join  {0}.inventorycategories  as c ON I.Categoryid=c.categoryid where  I.barcode like CONCAT('%',@SearchText,'%')  " + sActive;
                string command = string.Format(Inv_Qry2, BranchAlias);
                MySqlParameter[] parameters =
                   {
                     new MySqlParameter("@SearchText",txtSearchVal.Text.ToString().Trim()),
                   };

                DT = ObjMain.NewGetData_DT(command, parameters);
                grdInventoryView.DataSource = DT;
                grdInventoryView.DataBind();

            }

            else if (cmbSearch.SelectedIndex == 3)
            {
                if (chkActiveonly.Checked == true) // John added 02-Nov-2020 Active/InActive Include inside Query
                {
                    sActive = " and I.active <> 0";
                }
                else
                {
                    sActive = " and I.active = 0";
                }


                string Inv_Qry3 = "select * from {0}.inventory as I join {0}.inventorycategories  as c ON I.Categoryid=c.categoryid where  I.InventoryName like CONCAT('%',@SearchText,'%') " + sActive;
                string command = string.Format(Inv_Qry3, BranchAlias);
                MySqlParameter[] parameters =
                   {
                     new MySqlParameter("@SearchText",txtSearchVal.Text.ToString().Trim()),
                   };

                DT = ObjMain.NewGetData_DT(command, parameters);
                grdInventoryView.DataSource = DT;
                grdInventoryView.DataBind();


            }
            else if (cmbSearch.SelectedIndex == 4)
            {
                if (chkActiveonly.Checked == true) // John added 02-Nov-2020 Active/InActive Include inside Query
                {
                    sActive = " and I.active <> 0";
                }
                else
                {
                    sActive = " and I.active = 0";
                }
                string Inv_Qry4 = "select * from {0}.inventory as I join  {0}.inventorycategories  as c ON I.Categoryid=c.categoryid where  I.Description like CONCAT('%',@SearchText,'%') " + sActive;
                string command = string.Format(Inv_Qry4, BranchAlias);
                MySqlParameter[] parameters =
                  {
                     new MySqlParameter("@SearchText",txtSearchVal.Text.ToString().Trim()),
                   };
                DT = ObjMain.NewGetData_DT(command, parameters);
                grdInventoryView.DataSource = DT;
                grdInventoryView.DataBind();

            }

            else if (cmbSearch.SelectedIndex == 5)
            {
                if (chkActiveonly.Checked == true) // John added 02-Nov-2020 Active/InActive Include inside Query
                {
                    sActive = " and I.active <> 0";
                }
                else
                {
                    sActive = " and I.active = 0";
                }

                string Inv_Qry4 = "select * from {0}.inventory as I join  {0}.inventorycategories  as c ON I.Categoryid=c.categoryid where Price =@SearchText  OR price2 =@SearchText  OR price3 =@SearchText OR  price3 =@SearchText " + sActive;
                string command = string.Format(Inv_Qry4, BranchAlias);

                MySqlParameter[] parameters =
                 {
                     new MySqlParameter("@SearchText",txtSearchVal.Text.ToString().Trim()),
                   };

                DT = ObjMain.NewGetData_DT(command, parameters);
                grdInventoryView.DataSource = DT;
                grdInventoryView.DataBind();


            }


            else if (cmbSearch.SelectedIndex == 6)
            {
                string sActivex = string.Empty;

                if (chkActiveonly.Checked == true) // John added 02-Nov-2020 Active/InActive Include inside Query
                {
                    sActivex = " and I.active <> 0";
                }
                else
                {
                    sActivex = " and I.active = 0";
                }


                //string Inv_Qry5 = "select * from " + BranchAlias + ".inventory as I join  " + BranchAlias + ".inventorycategories  as c ON I.Categoryid=c.categoryid ORDER BY I.inventoryID asc LIMIT " + txtTop.Text.ToString().Trim() + "";
                string Inv_Qry5 = "Select *,sum(pquantity-(CutQuantity+RetailQty))as OnHandICA from {0}.inventory as I " +
                                " join {0}.inventorycategories  as c ON I.Categoryid=c.categoryid " +
                                " join {0}.inventorycostaverage ic on ic.inventoryid=i.inventoryid where c.categoryname like CONCAT('%',@SearchText,'%')   " + sActivex + "    group by I.inventoryid";

                string command = string.Format(Inv_Qry5, BranchAlias);
                MySqlParameter[] parameters =
                 {
                     new MySqlParameter("@SearchText",txtSearchVal.Text.ToString().Trim()),
                 };

                DT = ObjMain.NewGetData_DT(command, parameters);
                grdInventoryView.DataSource = DT;
                grdInventoryView.DataBind();
                //txtSearchVal.Text = "";

            }
            /*
           else if (cmbSearch.SelectedIndex == 7)
           {
               if (chkActiveonly.Checked == true) // John added 02-Nov-2020 Active/InActive Include inside Query
               {
                   sActive = " and I.active <> 0";
               }
               else
               {
                   sActive = " and I.active = 0";
               }

               string sCmdMBarcode = "Select * from " + BranchAlias + ".inventory as A join " + BranchAlias + ".inventorycategories as B on A.CategoryID = B.CategoryID Where Class = '10' and BarCode like '%" + txtSearchVal.Text.ToString().Trim() + "%' " + sActive;
               DT = ObjMain.GetData_DT(sCmdMBarcode);
               grdInventoryView.DataSource = DT;
               grdInventoryView.DataBind();

           }
           */


            // Groups Condition
            if (cmbSearch.SelectedIndex == 8)
            {
                string group1Value = "group1";
                LoadGroupDetail(group1Value, Microsoft.Security.Application.Encoder.HtmlEncode(txtSearchVal.Text.ToString().Trim()));

            }
            else if (cmbSearch.SelectedIndex == 9)
            {
                string group2Value = "group2";
                LoadGroupDetail(group2Value, Microsoft.Security.Application.Encoder.HtmlEncode(txtSearchVal.Text.ToString().Trim()));

            }
            else if (cmbSearch.SelectedIndex == 10)
            {
                string group3Value = "group3";
                LoadGroupDetail(group3Value, Microsoft.Security.Application.Encoder.HtmlEncode(txtSearchVal.Text.ToString().Trim()));

            }

            else if (cmbSearch.SelectedIndex == 11)
            {
                string group4Value = "group4";
                LoadGroupDetail(group4Value, Microsoft.Security.Application.Encoder.HtmlEncode(txtSearchVal.Text.ToString().Trim()));

            }
            else if (cmbSearch.SelectedIndex == 12)
            {
                string group5Value = "group5";
                LoadGroupDetail(group5Value, Microsoft.Security.Application.Encoder.HtmlEncode(txtSearchVal.Text.ToString().Trim()));

            }








        }

        public void LoadGroupDetail(string groupname, string SearchText)
        {



            if (cmbBranches.Visible == true)
            {
                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 )
                BranchAlias = Cmb_BranchName;
            }
            else
            {
                BranchAlias = Session["Branch_HQ"].ToString();
            }




            if (chkActiveonly.Checked == true)
            {
                sActive = " and I.active <> 0";
            }
            else
            {
                sActive = " and I.active = 0";
            }


            string Inv_Qry5 = "Select *,sum(pquantity-(CutQuantity+RetailQty))as OnHandICA from {0}.inventory as I " +
                       " join {0}.inventorycategories  as c ON I.Categoryid=c.categoryid " +
                       " join {0}.inventorycostaverage ic on ic.inventoryid=i.inventoryid where   I.@Groupname like CONCAT('%',@SearchText,'%')   " + sActive + "    group by I.inventoryid";
            string command = string.Format(Inv_Qry5, BranchAlias);

            MySqlParameter[] parameters =
                   {
                     new MySqlParameter("@Groupname",groupname),
                      new MySqlParameter("@SearchText",txtSearchVal.Text.ToString().Trim()),
                    };
            DT = ObjMain.NewGetData_DT(command, parameters);
            grdInventoryView.DataSource = DT;
            grdInventoryView.DataBind();
            //txtSearchVal.Text = "";
        }


        public void LoadInventoryview(string sortExpression = null)
        {

            if (cmbBranches.Visible == true)
            {
                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 )
                BranchAlias = Cmb_BranchName;
            }
            else
            {
                BranchAlias = Session["Branch_HQ"].ToString();
            }



            //string InvenQuery = "select * from " + BranchAlias + ".inventory as I join  " + BranchAlias + ".inventorycategories  as c ON I.Categoryid=c.categoryid order by inventoryid;";
            //string InvenQuery = "select *,sum(pquantity-(CutQuantity+RetailQty))as OnHandICA from " + BranchAlias + ".inventory as I " +
            //                    " join " + BranchAlias + ".inventorycategories  as c ON I.Categoryid=c.categoryid " +
            //                    " join " + BranchAlias + ".inventorycostaverage ic on ic.inventoryid=i.inventoryid " +
            //                    " where i.active  in (1) " +
            //                    " group by i.inventoryid " +
            //                    " ORDER BY I.inventoryID asc LIMIT 50000;";

            string InvenQuery = "select *,sum(pquantity-(CutQuantity+RetailQty))as OnHandICA from {0}.inventory as I  " +
                                " join {0}.inventorycategories  as c ON I.Categoryid=c.categoryid  " +
                                " join {0}.inventorycostaverage ic on ic.inventoryid=i.inventoryid  " +
                                " where i.active  <> 0  group by i.inventoryid  ORDER BY I.inventoryID asc LIMIT 50000;";

            string command = string.Format(InvenQuery, BranchAlias);
            DT = ObjCloudMain.GetData_DT(command);

            if (sortExpression != null)
            {
                DataView dv = DT.AsDataView();
                //  this.SortDirection = this.SortDirection == "ASC" ? "DESC" : "ASC";

                //dv.Sort = sortExpression + " " + this.SortDirection;
                grdInventoryView.DataSource = dv;
            }
            else
            {
                grdInventoryView.DataSource = DT;
            }
            grdInventoryView.DataBind();


        }

        protected void btnNotAvailable_Click(object sender, EventArgs e)
        {


            if (grdTimingbyid.Rows.Count > 0)
            {

                /* Timing Swap Concept To make the Products 'Not Avaialable' */
                if (cmbBranches.Visible == false)
                {

                    BranchAlias = Session["Branch_HQ"].ToString();

                }
                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 )
                    BranchAlias = Cmb_BranchName;
                }


                // Step : 1
                // Get the list of Inventoryid's 
                string Inventoryid = string.Empty;
                string Barcode = string.Empty;

                string ReadInventoryids = "select Inventoryid,Barcode from {0}.inventory where Inventoryid={1} ; ";
                string command = string.Format(ReadInventoryids, BranchAlias, hidvalue.Value);
                DtRead = ObjCloudMain.GetData_DT(command);

                for (int i = 0; i < DtRead.Rows.Count; i++)
                {
                    DataRow dr = DtRead.Rows[i];

                    Inventoryid = dr["Inventoryid"].ToString();
                    Barcode = dr["BarCode"].ToString();




                    foreach (GridViewRow gvrows in grdTimingbyid.Rows)
                    {
                        var checkbox = gvrows.FindControl("chkSelect") as CheckBox;
                        if (checkbox.Checked)
                        {
                            Label lblid = gvrows.FindControl("lblid") as Label;
                            Label lblDays_Value = gvrows.FindControl("lblDays") as Label;

                            TextBox txtRT1_StTm = (TextBox)gvrows.FindControl("txtT1_StTm");
                            TextBox txtRT1_EndTm = (TextBox)gvrows.FindControl("txtT1_EndTm");
                            Label lblTInventoryid = gvrows.FindControl("lblInventoryid") as Label;

                            string Added = "23:59:59";
                            string NewStartDt = string.Concat(Curr_DateOnly, " ", Added);
                            string Added1 = "00:00:01";
                            string NewEndtDt = string.Concat(Curr_DateOnly, " ", Added1);


                            string Query = "Update  {0}.timeschedule set T1_StTm=@T1_StTm, T1_EndTm=@T1_EndTm where id=@id AND Days=@Days and  inventoryid=@inventoryid ;";
                            string commandA = string.Format(Query, BranchAlias);
                            MySqlParameter[] parameters =
                            {
                             new MySqlParameter("@T1_StTm",NewStartDt),
                             new MySqlParameter("@T1_EndTm",NewEndtDt),
                             new MySqlParameter("@id",lblid.Text),
                             new MySqlParameter("@Days",lblDays_Value.Text),
                             new MySqlParameter("@inventoryid",lblTInventoryid.Text),

                            };

                            K = ObjMain.ExecuteQuery(commandA, parameters);

                        }


                    }


                    LoadTimingByInventoryId(BranchAlias, hidvalue.Value);
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' This Item Changed to Not Available '); ", true);
                    //location.href = 'Iorderlasetting.aspx'


                }

            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Select Any Inventory Item ! '); ", true);

            }


        }

        protected void btnAvailable_Click(object sender, EventArgs e)
        {


            if (grdTimingbyid.Rows.Count > 0)
            {

                /* Timing Swap Concept To make the Products 'Not Avaialable' */
                if (cmbBranches.Visible == false)
                {

                    BranchAlias = Session["Branch_HQ"].ToString();

                }
                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 )
                    BranchAlias = Cmb_BranchName;
                }


                // Step : 1
                // Get the list of Inventoryid's 
                string Inventoryid = string.Empty;
                string Barcode = string.Empty;

                string ReadInventoryids = "select Inventoryid,Barcode from {0}.inventory where Inventoryid={1} ; ";
                string command = string.Format(ReadInventoryids, BranchAlias, hidvalue.Value);
                DtRead = ObjCloudMain.GetData_DT(command);

                for (int i = 0; i < DtRead.Rows.Count; i++)
                {
                    DataRow dr = DtRead.Rows[i];

                    Inventoryid = dr["Inventoryid"].ToString();
                    Barcode = dr["BarCode"].ToString();

                    foreach (GridViewRow gvrows in grdTimingbyid.Rows)
                    {
                        var checkbox = gvrows.FindControl("chkSelect") as CheckBox;
                        if (checkbox.Checked)
                        {
                            Label lblid = gvrows.FindControl("lblid") as Label;
                            Label lblDays_Value = gvrows.FindControl("lblDays") as Label;

                            TextBox txtRT1_StTm = (TextBox)gvrows.FindControl("txtT1_StTm");
                            TextBox txtRT1_EndTm = (TextBox)gvrows.FindControl("txtT1_EndTm");
                            Label lblTInventoryid = gvrows.FindControl("lblInventoryid") as Label;

                            string Added = "00:00:01";
                            string NewStartDt = string.Concat(Curr_DateOnly, " ", Added);
                            string Added1 = "23:59:59";
                            string NewEndtDt = string.Concat(Curr_DateOnly, " ", Added1);


                            string Query = "Update  {0}.timeschedule set T1_StTm=@T1_StTm, T1_EndTm=@T1_EndTm where id=@id AND Days=@Days and  inventoryid=@inventoryid ;";
                            string commandA = string.Format(Query, BranchAlias);
                            MySqlParameter[] parameters =
                            {
                             new MySqlParameter("@T1_StTm",NewStartDt),
                             new MySqlParameter("@T1_EndTm",NewEndtDt),
                             new MySqlParameter("@id",lblid.Text),
                             new MySqlParameter("@Days",lblDays_Value.Text),
                             new MySqlParameter("@inventoryid",lblTInventoryid.Text),

                            };

                            K = ObjMain.ExecuteQuery(commandA, parameters);

                        }


                    }



                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' This Item Changed to Available '); ", true);
                    LoadTimingByInventoryId(BranchAlias, hidvalue.Value);
                    //location.href = 'Iorderlasetting.aspx'


                }

            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Select Any Inventory Item ! '); ", true);

            }






        }

        protected void chkCommonUpdate_CheckedChanged(object sender, EventArgs e)
        {
            if (cmbBranches.Visible == false)
            {

                BranchAlias = Session["Branch_HQ"].ToString();

            }
            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 )
                BranchAlias = Cmb_BranchName;
            }
            string Chkboxvalue = string.Empty;

            if (chkCommonUpdate.Checked == true)
            {
                Chkboxvalue = "1";
            }
            else
            {
                Chkboxvalue = "0";
            }






            string Query = "Update  {0}.timescheduleFlag set IsCommonUpdate=@IsCommonUpdate where  inventoryid=@inventoryid ;";
            string commandA = string.Format(Query, BranchAlias);
            MySqlParameter[] parameters =
            {
                             new MySqlParameter("@IsCommonUpdate",Chkboxvalue),
                             new MySqlParameter("@inventoryid",hidvalue.Value),

                            };

            K = ObjMain.ExecuteQuery(commandA, parameters);


        }

        protected void chkNotAvilable_CheckedChanged(object sender, EventArgs e)
        {
            #region  Get Current Database
            if (cmbBranches.Visible == false)
            {

                BranchAlias = Session["Branch_HQ"].ToString();

            }
            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 )
                BranchAlias = Cmb_BranchName;
            }
            #endregion


            string Chkbox5 = string.Empty;

            if (chkNotAvilable.Checked == true)
            {
                Chkbox5 = "1";
            }
            else
            {
                Chkbox5 = "0";
            }




            string Query5 = "Update  {0}.timescheduleFlag set IsNotAvailable=@IsNotAvailable where  inventoryid=@inventoryid ;";
            string command5 = string.Format(Query5, BranchAlias);
            MySqlParameter[] parameters5 =
            {
                             new MySqlParameter("@IsNotAvailable",Chkbox5),
                             new MySqlParameter("@inventoryid",hidvalue.Value),

            };

            L = ObjMain.ExecuteQuery(command5, parameters5);



            // Include the Logic 

            if (chkNotAvilable.Checked == true)
            {


                if (grdTimingbyid.Rows.Count > 0)
                {

                    /* Timing Swap Concept To make the Products 'Not Avaialable' */
                    if (cmbBranches.Visible == false)
                    {

                        BranchAlias = Session["Branch_HQ"].ToString();

                    }
                    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 )
                        BranchAlias = Cmb_BranchName;
                    }


                    // Step : 1
                    // Get the list of Inventoryid's 
                    string Inventoryid = string.Empty;
                    string Barcode = string.Empty;

                    string ReadInventoryids = "select Inventoryid,Barcode from {0}.inventory where Inventoryid={1} ; ";
                    string command = string.Format(ReadInventoryids, BranchAlias, hidvalue.Value);
                    DtRead = ObjCloudMain.GetData_DT(command);

                    for (int i = 0; i < DtRead.Rows.Count; i++)
                    {
                        DataRow dr = DtRead.Rows[i];

                        Inventoryid = dr["Inventoryid"].ToString();
                        Barcode = dr["BarCode"].ToString();




                        foreach (GridViewRow gvrows in grdTimingbyid.Rows)
                        {
                            var checkbox = gvrows.FindControl("chkSelect") as CheckBox;
                            if (checkbox.Checked)
                            {
                                Label lblid = gvrows.FindControl("lblid") as Label;
                                Label lblDays_Value = gvrows.FindControl("lblDays") as Label;

                                TextBox txtRT1_StTm = (TextBox)gvrows.FindControl("txtT1_StTm");
                                TextBox txtRT1_EndTm = (TextBox)gvrows.FindControl("txtT1_EndTm");
                                Label lblTInventoryid = gvrows.FindControl("lblInventoryid") as Label;

                                string Added = "23:59:59";
                                string NewStartDt = string.Concat(Curr_DateOnly, " ", Added);
                                string Added1 = "00:00:01";
                                string NewEndtDt = string.Concat(Curr_DateOnly, " ", Added1);


                                string Query = "Update  {0}.timeschedule set T1_StTm=@T1_StTm, T1_EndTm=@T1_EndTm where id=@id AND Days=@Days and  inventoryid=@inventoryid ;";
                                string commandA = string.Format(Query, BranchAlias);
                                MySqlParameter[] parameters =
                                {
                             new MySqlParameter("@T1_StTm",NewStartDt),
                             new MySqlParameter("@T1_EndTm",NewEndtDt),
                             new MySqlParameter("@id",lblid.Text),
                             new MySqlParameter("@Days",lblDays_Value.Text),
                             new MySqlParameter("@inventoryid",lblTInventoryid.Text),

                            };

                                K = ObjMain.ExecuteQuery(commandA, parameters);

                            }


                        }


                        LoadTimingByInventoryId(BranchAlias, hidvalue.Value);
                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' This Item Changed to Not Available '); ", true);
                        //location.href = 'Iorderlasetting.aspx'


                    }

                }
                else
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Select Any Inventory Item ! '); ", true);

                }







            }
            else if (chkNotAvilable.Checked == false)
            {


                if (grdTimingbyid.Rows.Count > 0)
                {





                    /* Timing Swap Concept To make the Products 'Not Avaialable' */
                    if (cmbBranches.Visible == false)
                    {

                        BranchAlias = Session["Branch_HQ"].ToString();

                    }
                    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 )
                        BranchAlias = Cmb_BranchName;
                    }


                    // Step : 1
                    // Get the list of Inventoryid's 
                    string Inventoryid = string.Empty;
                    string Barcode = string.Empty;

                    string ReadInventoryids = "select Inventoryid,Barcode from {0}.inventory where Inventoryid={1} ; ";
                    string command = string.Format(ReadInventoryids, BranchAlias, hidvalue.Value);
                    DtRead = ObjCloudMain.GetData_DT(command);

                    for (int i = 0; i < DtRead.Rows.Count; i++)
                    {
                        DataRow dr = DtRead.Rows[i];

                        Inventoryid = dr["Inventoryid"].ToString();
                        Barcode = dr["BarCode"].ToString();

                        foreach (GridViewRow gvrows in grdTimingbyid.Rows)
                        {
                            var checkbox = gvrows.FindControl("chkSelect") as CheckBox;
                            if (checkbox.Checked)
                            {
                                Label lblid = gvrows.FindControl("lblid") as Label;
                                Label lblDays_Value = gvrows.FindControl("lblDays") as Label;

                                TextBox txtRT1_StTm = (TextBox)gvrows.FindControl("txtT1_StTm");
                                TextBox txtRT1_EndTm = (TextBox)gvrows.FindControl("txtT1_EndTm");
                                Label lblTInventoryid = gvrows.FindControl("lblInventoryid") as Label;

                                string Added = "00:00:01";
                                string NewStartDt = string.Concat(Curr_DateOnly, " ", Added);
                                string Added1 = "23:59:59";
                                string NewEndtDt = string.Concat(Curr_DateOnly, " ", Added1);


                                string Query = "Update  {0}.timeschedule set T1_StTm=@T1_StTm, T1_EndTm=@T1_EndTm where id=@id AND Days=@Days and  inventoryid=@inventoryid ;";
                                string commandA = string.Format(Query, BranchAlias);
                                MySqlParameter[] parameters =
                                {
                             new MySqlParameter("@T1_StTm",NewStartDt),
                             new MySqlParameter("@T1_EndTm",NewEndtDt),
                             new MySqlParameter("@id",lblid.Text),
                             new MySqlParameter("@Days",lblDays_Value.Text),
                             new MySqlParameter("@inventoryid",lblTInventoryid.Text),

                            };

                                K = ObjMain.ExecuteQuery(commandA, parameters);

                            }


                        }



                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' This Item Changed to Available '); ", true);
                        LoadTimingByInventoryId(BranchAlias, hidvalue.Value);
                        //location.href = 'Iorderlasetting.aspx'


                    }

                }
                else
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Select Any Inventory Item ! '); ", true);

                }





            }









        }

        protected void chkOutofStock_CheckedChanged(object sender, EventArgs e)
        {
            int i;
            int chkValue;
            #region  Get Current Database
            if (cmbBranches.Visible == false)
            {
                BranchAlias = Session["Branch_HQ"].ToString();
            }
            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 )
                BranchAlias = Cmb_BranchName;
            }
            #endregion

            if (chkOutofStock.Checked == false)
            {
                chkValue = 0;
            }
            else
            {
                chkValue = 1;
            }


            string query = "update {0}.inventory set outofstock = @outofstock where inventoryid = @inventoryid";
            string cmd = string.Format(query, BranchAlias);
            MySqlParameter[] parameters =
            {
                new MySqlParameter("@outofstock",chkValue),
                new MySqlParameter("@inventoryid", hidvalue.Value),
            };

            ObjMain.ExecuteQuery(cmd, parameters);
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' This Item Changed to Not Available(POS/KIOSK)'); ", true);
        }


        public void INS_ScheduleFlag(string sdb)
        {

            // Local Variable 

            string Inventoryid = string.Empty;
            string Barcode = string.Empty;
            DataTable DtScheduleFlag = new DataTable();

            //#1 Check the PriceFlage table Record  count 

            string TimescheduleFlagQuery = "select * from {0}.timescheduleFlag;";
            string schedule_command = string.Format(TimescheduleFlagQuery, sdb);
            DtScheduleFlag = ObjMain.GetData_DT(schedule_command);

            //#2 Get Inventory Table Record Count 

            string InventoryRowCountQuery = "select * from {0}.Inventory; ";
            string Inventory_command = string.Format(InventoryRowCountQuery, sdb);
            DtinventoryCnt = ObjMain.GetData_DT(Inventory_command);

            //#3. If PriceFlag  is NULL 
            if (DtScheduleFlag.Rows.Count == 0)
            {
                // Call Insert Function

                string ReadInventoryids = "select Inventoryid,Barcode from {0}.inventory order by Inventoryid; ";
                string commandReads = string.Format(ReadInventoryids, sdb);
                DtRead = ObjMain.GetData_DT(commandReads);

                for (int i = 0; i < DtRead.Rows.Count; i++)
                {
                    DataRow dr = DtRead.Rows[i];

                    Inventoryid = dr["Inventoryid"].ToString();
                    Barcode = dr["BarCode"].ToString();

                    // #1.2 IF Not Exist   
                    // Call Insert  Function : PriceFlag
                    #region   Insert Portion
                    string Ins_ScheduleFlag = " insert into {0}.timescheduleFlag (Inventoryid,Barcode,IsCommonUpdate)" +
                                              " values " +
                                              " (@Inventoryid,@Barcode,@IsCommonUpdate);";
                    string Ins_command = string.Format(Ins_ScheduleFlag, sdb);
                    MySqlParameter[] parameters =
                        {
                             new MySqlParameter("@Inventoryid",Inventoryid),
                             new MySqlParameter("@Barcode",Barcode),
                             new MySqlParameter("@IsCommonUpdate","0"),
                        };
                    Q = ObjMain.ExecuteQuery(Ins_command, parameters);




                    #endregion
                }


            }
            //#4. If ScheduleFlag  < Inventory 
            else if (DtScheduleFlag.Rows.Count < DtinventoryCnt.Rows.Count)
            {

                string CheckA = string.Empty;

                //#1.1 IF Inventoryid is exist  in  PriceFlag Table Ignore to Insert 

                string ReadInventoryids = "select Inventoryid,Barcode from {0}.inventory order by Inventoryid; ";
                string commandReads = string.Format(ReadInventoryids, sdb);
                DtRead = ObjMain.GetData_DT(commandReads);

                for (int i = 0; i < DtRead.Rows.Count; i++)
                {
                    DataRow dr = DtRead.Rows[i];

                    Inventoryid = dr["Inventoryid"].ToString();
                    Barcode = dr["BarCode"].ToString();

                    // #1.2 IF Inventoryid is  Exist  in PriceFlag  Ignore to Insert  
                    // Check the Inventoryid in 'PriceFlag'

                    string ReadInventoryidOnScheduleFlag = "select Inventoryid from {0}.timescheduleFlag where  Inventoryid=@Inventoryid ; ";
                    string command_ReadScheduleflag = string.Format(ReadInventoryidOnScheduleFlag, sdb);
                    MySqlParameter[] parameters =
                    {
                     new MySqlParameter("@Inventoryid",Inventoryid),
                    };
                    CheckA = ObjMain.NewExecuteScalar(command_ReadScheduleflag, parameters);

                    if (CheckA != "")
                    {
                        // Record Already Exist In ScheduleFlag : Ignore for insert 
                    }
                    else
                    {
                        // Call Insert  Function : ScheduleFlag  
                        #region   Insert Portion
                        string Ins_ScheduleFlag = " insert into {0}.timescheduleFlag (Inventoryid,Barcode,IsCommonUpdate)" +
                                               " values " +
                                               " (@Inventoryid,@Barcode,@IsCommonUpdate);";
                        string Ins_command = string.Format(Ins_ScheduleFlag, sdb);
                        MySqlParameter[] parametersAA =
                            {
                             new MySqlParameter("@Inventoryid",Inventoryid),
                             new MySqlParameter("@Barcode",Barcode),
                             new MySqlParameter("@IsCommonUpdate","0"),
                        };
                        Q = ObjMain.ExecuteQuery(Ins_command, parametersAA);




                        #endregion

                    }


                }



            }
            else
            {
                // Nothing To :  Process
            }




        }




    }

}