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 System.Web.UI.DataVisualization.Charting;
using SunWeb.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using DocumentFormat.OpenXml.Bibliography;
using static SunWeb.Model.CNavBrand;
using static SunWeb.Model.CNAVItem;
using DocumentFormat.OpenXml.Drawing;

namespace SUNWEB
{
    public partial class CategoryMaster : System.Web.UI.Page
    {

        BL_API objAPI = new BL_API();
        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 U = 0;
        Int32 S = 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");

        DataSet ds = new DataSet();
        MYSQLDB ObjMain = new MYSQLDB();
        MYSQLCLOUD ObjCloudMain = new MYSQLCLOUD();
        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 DTCategorydl = new DataTable();
        DataTable DtCateoryList = new DataTable();
        DataTable DtCate_Printer = new DataTable();
        DataTable DTInvInfo = new DataTable();
        DataTable DTCheckBarcode = new DataTable();
        DataTable DTQry = new DataTable();
        DataTable DTPrinterList = new DataTable();
        DataTable DTCatExist = new DataTable();
        // string BranchAlias = "saffron1";
        string BranchAlias = string.Empty;
        string PSett;
        string CatValue;
        string Grp1Value;
        string Grp2Value;
        string Grp3Value;
        string Grp4Value;
        string Grp5Value;
        string Grp1_Maxid = string.Empty;
        string Grp2_Maxid = string.Empty;
        string Grp3_Maxid = string.Empty;
        string Grp4_Maxid = string.Empty;
        string Grp5_Maxid = string.Empty;
        string Ins_CatQuery = string.Empty;

        string Ins_Menu = string.Empty;
        private string SearchTxt_G1;
        private string SearchTxt_G2;
        private string SearchTxt_G3;
        private string SearchTxt_G4;
        private string SearchTxt_G5;
        // Category Variable :  

        string RCategoryNo = string.Empty;
        string RCategoryName = string.Empty;
        string RCate_Descp = string.Empty;
        string RActive_Stauts = string.Empty;
        string RUpdate_Status = string.Empty;
        string RBranch_Update = string.Empty;
        string RFlag1_Tax = string.Empty;
        string RFlag2_Printer = string.Empty;
        string RFireIndex = string.Empty;
        string RCateYNo = string.Empty;
        string CateVl;
        string CateValue;

        //Inventory variables for editing purpose
        string GInventoryID = string.Empty;
        string GBarcode = string.Empty;
        string GInventoryName = string.Empty;
        string GDescription = string.Empty;
        string GCategoryID = string.Empty;
        string GClass = string.Empty;
        string GUnit = string.Empty;
        string GPrice = string.Empty;
        string GPrice2 = string.Empty;
        string GPrice3 = string.Empty;
        string GPrice4 = string.Empty;
        string GDateCreated = string.Empty;
        string GGroup1 = string.Empty;
        string GGroup2 = string.Empty;
        string GGroup3 = string.Empty;
        string GGroup4 = string.Empty;
        string GGroup5 = string.Empty;
        string GFlag1 = string.Empty;
        string GFlag2 = string.Empty;
        string GReorderLevel = string.Empty;
        string GMaximumLevel = string.Empty;
        string GSafetyLevel = string.Empty;

        string PortalTypeId = string.Empty;
        string ActiveCallvalue = string.Empty;
        string Datapath = string.Empty;
        string cd_BranchAlias = string.Empty;
        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_Inventory = 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;

        //for InventoryAdjust Process
        string AdjInvCost = string.Empty;

        //for check barcode function
        string sChkBarcode = string.Empty;

        DataTable AdjDt = new DataTable();
        DataTable FAdjDt = new DataTable();
        string sSno = string.Empty;
        int iSno = 0;
        string sCmd = string.Empty;
        string ISHQ = string.Empty;

        string RBranchid = string.Empty;
        string Branchid = string.Empty;
        string ChkTableExist = string.Empty;
        string URL = string.Empty;
        string MaxSeqNo = string.Empty;
        string PrinterName1, PrinterName2, PrinterName3, PrinterName4, PrinterName5, PrinterName6, PrinterName7,
               PrinterName8, PrinterName9, PrinterName10, PrinterName11, PrinterName12, PrinterName13, PrinterName14, PrinterName15, PrinterName16, PrinterName17, PrinterName18, PrinterName19;
        string RPrintno1, RPrintno2, RPrintno3, RPrintno4, RPrintno5, RPrintno6, RPrintno7, RPrintno8, RPrintno9, RPrintno10, RPrintno11, RPrintno12, RPrintno13, RPrintno14, RPrintno15, RPrintno16, RPrintno17, RPrintno18, RPrintno19;

        protected void btnDelete_Click(object sender, EventArgs e)
        {

            #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

            #region Delete inventory  Table    
            string Del_Inventorycategories = " Delete from {0}.Inventorycategories where Categoryid=@Categoryid;";
            string Del_command = string.Format(Del_Inventorycategories, BranchAlias);
            MySqlParameter[] Del_Inventorycategories_parameters =
                       {
                             new MySqlParameter("@Categoryid",Convert.ToString(hidCategoryNo.Value)),

                        };
            L = ObjMain.ExecuteQuery(Del_command, Del_Inventorycategories_parameters);
            // Delete Menu table using  Categoryid 
            string Del_Menu = "Delete from {0}.Menu  where  Categoryid=@Categoryid;";
            string Del_Menu_Command = string.Format(Del_Menu, BranchAlias);
            MySqlParameter[] Del_Menu_parameters =
                       {
                             new MySqlParameter("@Categoryid",Convert.ToString(hidCategoryNo.Value)),

                        };
            S = ObjMain.ExecuteQuery(Del_Menu_Command, Del_Menu_parameters);
            if (L > 0 && S > 0)
            {
                CreateLog_Inventorycategories(hidCategoryNo.Value);

                #region After Successfull Delete category changed the Categoryid's associated with Inventory to Default CategoryID -> 0 
                // #1. Check NO CATEGORY With 0 Categoryid is exist or Not..

                string Chk_NO_CATEGORY = ObjMain.ExecuteScalar("select CategoryName from  " + BranchAlias + ".InventoryCategories where categoryid=0; ");

                if (Chk_NO_CATEGORY != "")
                {
                    // If there is  'NO CATEGORY' then Insert it.
                }
                else
                {
                    // if there is  No 'NO CATEGORY' then Insert it . 
                    string _category = "0";
                    string _categoryname = "NO CATEGORY";
                    string _active = "-1";
                    string Insert_Inventorycategories_1 = @" Insert into  " + BranchAlias + ".InventoryCategories "
                        + " (CategoryID,CategoryNumber,CategoryName,Description,DateCreated,Active) "
                        + "  Values "
                        + " (@CategoryID,@CategoryNumber,@CategoryName,@Description,@DateCreated,@Active); ";
                    string Ins_command = string.Format(Insert_Inventorycategories_1, BranchAlias);
                    MySqlParameter[] Ins_Inventorycategories_parameters =
                               {
                             new MySqlParameter("@Categoryid",Int32.Parse(_category)),
                             new MySqlParameter("@CategoryNumber",Int32.Parse(_category)),
                             new MySqlParameter("@CategoryName",_categoryname),
                             new MySqlParameter("@Description",_categoryname),
                             new MySqlParameter("@DateCreated",Curr_DateOnly),
                             new MySqlParameter("@Active",_active),
                           };
                    L = ObjMain.ExecuteQuery(Ins_command, Ins_Inventorycategories_parameters);
                }
                // #2. Get the InventoryList By Deletable Categoryid

                DataTable DTList = new DataTable();
                DTList = ObjMain.GetData_DT("select Inventoryid,barcode from  " + BranchAlias + ".Inventory where categoryid=" + hidCategoryNo.Value + ";");
                if (DTList.Rows.Count > 0)
                {
                    string _category = "0";
                    for (int i = 0; i < DTList.Rows.Count; i++)
                    {
                        string RInventoryid = DTList.Rows[i]["Inventoryid"].ToString();
                        string RBarcode = DTList.Rows[i]["barcode"].ToString();
                        // #3. Update All Category with NO CATEGORY 
                        string Update_Inventory_By_Inventoryid = " Update {0}.Inventory  set CategoryID=@Categoryid  where InventoryId=@InventoryId;";
                        string Update_Inventory_command = string.Format(Update_Inventory_By_Inventoryid, BranchAlias);
                        MySqlParameter[] Update_Inventory_parameters =
                                   {
                                    new MySqlParameter("@Categoryid",Int32.Parse(_category)),
                                    new MySqlParameter("@InventoryId",Int32.Parse(RInventoryid)),
                                   };
                        K = ObjMain.ExecuteQuery(Update_Inventory_command, Update_Inventory_parameters);

                    }

                }

                #endregion

            }

            LoadCategoriesDataList(BranchAlias);
            if (L > 0)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert('Category Deleted Successfully');location.href='CategoryMaster.aspx'", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert('Category Not Deleted');", true);
            }
            #endregion
        }

        /// <summary>
        ///  Function for to create Log Entry for  InventoryCategoryDelete
        /// </summary>
        /// <param name="categoryid"></param>
        public void CreateLog_Inventorycategories(string categoryid)
        {
            string Category_Insert = "Insert into {0}.InventoryCategoryDeleteLog(Categoryid,Deleteby,Deleteddate,userid)values(@Categoryid,@Deleteby,@Deleteddate,@userid); ";
            string Ins_CategoryLog_Command = string.Format(Category_Insert, BranchAlias);
            MySqlParameter[] Ins_Category_parameters =
                      {
                        new MySqlParameter("@Categoryid",Convert.ToInt32(categoryid)),
                        new MySqlParameter("@Deleteby",Session["UserName"].ToString()),
                        new MySqlParameter("@Deleteddate",Curr_Date),
                        new MySqlParameter("@userid", Convert.ToInt32(Session["UserId"].ToString())),

                      };
            K = ObjMain.ExecuteQuery(Ins_CategoryLog_Command, Ins_Category_parameters);
        }

        // Added On 02-12-2021
        string ReadHQ = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null)
            {
                Response.Redirect("Login.aspx");
            }

            PID = "13";    // This is for Inventory  PageAccess

            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();

                }
                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;

                if (!IsPostBack)
                {
                    LoadCategoriesDataList(BranchAlias);
                    chkActiveStatus.Enabled = true;
                    chkT.Enabled = false;
                    chkP.Enabled = true;
                    btnUpdate.Visible = false;
                    btnUpdate.CssClass = "btn btn-primary";

                }


            }
            else if (((Convert.ToBoolean(Session["IsMultiBranch"])) == true) && (Session["PortalTypeId"].ToString() == ""))
            {
                if (!IsPostBack)
                {
                    LoadBranchName4SuperAdmin();
                    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();

                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_ItemSales"].ToString() == "N") || (Session["Page_Acess"].ToString() == "N"))
                {
                    Response.Redirect("DeniedPage.aspx");
                }
                else if (Session["Write_Acess"].ToString() == "N")
                {
                    Response.Redirect("DeniedPage.aspx");
                }

                else
                {

                    if (!IsPostBack)
                    {
                        if (Session["Isdowld_Acess"].ToString() == "N")
                        {
                            // btnExcel.Enabled = false;
                            Response.Write(@"<script language='javascript'>alert('No Access To download.')</script>");
                        }
                        else
                        {

                            if (cmbBranches.Visible == false)
                            {

                                BranchAlias = Session["Branch_HQ"].ToString();
                                // Loading Parts : 
                                if (!IsPostBack)
                                {
                                    LoadCategoriesDataList(BranchAlias);
                                }
                                chkActiveStatus.Enabled = false;
                                chkT.Enabled = false;
                                chkP.Enabled = true;
                                btnUpdate.Visible = false;
                                btnUpdate.CssClass = "btn btn-primary";

                            }
                        }
                    }
                }
            }

            if (!IsPostBack)
            {
                BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
                Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);

                lblclientname.Text = Session["CM_ID"].ToString();
                lblstorename.Text = Cmb_BranchName;
            }

        }

        public void ActiveCall()
        {
            string QueryCal = "select ActiveValue from portaldb.usermaster  where Userid=" + Session["UserId"].ToString() + " ";
            ActiveCallvalue = ObjMain.ExecuteScalar(QueryCal);
            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


            }


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

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

            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);

            }

            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_Inventory = DtLicense.Rows[0]["INVENTORY"].ToString();
                Session["L_Inventory"] = L_Inventory;


            }
            if ((Session["L_Inventory"] == null) || Session["L_Inventory"].ToString() == "N" || Session["Page_Acess"].ToString() == "N")
            {

                if (cmbBranches.SelectedItem.Text == "HQ")
                {
                    // btnExcel.Enabled = true;
                    // HQ Validation    02-12-21

                    #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


                    LoadCategoriesDataList(BranchAlias);
                    LoadPrinterValue();
                    LoadTaxLables();
                    chkActiveStatus.Enabled = false;
                    chkT.Enabled = false;
                    chkP.Enabled = true;
                    btnUpdate.Visible = false;
                    btnUpdate.CssClass = "btn btn-primary";
                    btnDelete.Visible = false;
                    UPDATEBRANCH(BranchAlias);

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



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

            else
            {


                #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

                // btnExcel.Enabled = true;
                // HQ Validation    02-12-21

                //nid added for AUDHQ checking for showing Navision Integration import button on 16-Oct-2023
                if (Session["Branch_HQ"].ToString() == "audhq")
                {
                    btnNavImport.Visible = true;
                }
                else
                {
                    btnNavImport.Visible = false;
                }

                LoadCategoriesDataList(BranchAlias);
                LoadPrinterValue();
                LoadTaxLables();
                chkActiveStatus.Enabled = false;
                chkT.Enabled = false;
                chkP.Enabled = true;
                btnUpdate.Visible = false;
                btnUpdate.CssClass = "btn btn-primary";
                btnDelete.Visible = false;
                UPDATEBRANCH(BranchAlias);
            }

        }


        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
                {
                    string _get_HQ = ObjSel.GetHQ_Status(Session["CM_ID"].ToString());
                    if (_get_HQ == "0")
                    {

                        //UPDATEBRANCH(Cmb_BranchName);
                        //Response.Redirect(Request.RawUrl);
                        Branchid = RBranchid;
                        StrBObj.Append("SELECT  Branchid ,BranchName,branchfullname,datapath FROM " + Session["Branch_HQ"].ToString() + ".branches where branchid in ( " + Branchid + " ");


                    }
                    else if (_get_HQ == "1")
                    {
                        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");
            }

        }

        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()
        {

            //DtNormalUser = ObjUserBL.GetBranchOnSuperAdminWithHQ(Session["Branch_HQ"].ToString(), Session["UserId"].ToString());  Live 
            DtNormalUser = ObjUserBL.GetBranchOnSuperAdminWithHQ8(Session["Branch_HQ"].ToString(), Session["CM_ID"].ToString());    // Testing : 12-09-20
            if (DtNormalUser.Rows.Count > 0)
            {
                cmbBranches.DataSource = DtNormalUser;
                cmbBranches.DataTextField = "branchfullname";
                cmbBranches.DataValueField = "Branchid";
                cmbBranches.DataBind();
                cmbBranches.Items.Insert(0, "Select Branch");
            }

        }

        protected void btnSaveTax_Click(object sender, EventArgs e)
        {
            Flag1Value();
            ResetCheckboxCT();
        }

        protected void cmbBranches_SelectedIndexChanged(object sender, EventArgs e)
        {


            if (cmbBranches.SelectedIndex == 0)
            {
                Response.Write("Inventory.aspx");
            }
            else
            {
                DT = ObjUserBL.GetPageRightsDetailsPIDNew(Session["UserId"].ToString(), PID, 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;
                }

                if (cmbBranches.SelectedIndex == 1)
                {

                    BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
                    Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);
                    UPDATEBRANCH(Cmb_BranchName);

                }
                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 )
                    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);

                }
                if (DtLicense.Rows.Count > 0)
                {

                    L_Inventory = DtLicense.Rows[0]["INVENTORY"].ToString();
                    Session["L_Inventory"] = L_Inventory;
                }

                if ((Session["L_Inventory"] == null) || Session["L_Inventory"].ToString() == "N" || Session["Page_Acess"].ToString() == "N")
                {
                    if (cmbBranches.SelectedIndex == 1)
                    {
                        // btnExcel.Enabled = true;

                        #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
                        LoadCategoriesDataList(BranchAlias);
                        UPDATEBRANCH(BranchAlias);
                        chkActiveStatus.Enabled = false;
                        chkT.Enabled = false;
                        chkP.Enabled = true;
                        btnUpdate.Visible = false;
                        btnUpdate.CssClass = "btn btn-primary";
                        btnDelete.Visible = false;
                        txtCategoryName.Text = "";
                        lblCategoryName.Text = "";
                        txtCategoryDescription.Text = "";

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



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

                    #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


                    LoadCategoriesDataList(BranchAlias);
                    LoadTaxLables();
                    UPDATEBRANCH(BranchAlias);
                    chkActiveStatus.Enabled = false;
                    chkT.Enabled = false;
                    chkP.Enabled = true;
                    btnUpdate.Visible = false;
                    btnUpdate.CssClass = "btn btn-primary";
                    btnDelete.Visible = false;
                    txtCategoryName.Text = "";
                    txtCategoryDescription.Text = "";


                }
            }

        }

        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");

            }

        }

        protected void btnSaveCategory_Click(object sender, EventArgs e)
        {
            // XSS Attack Prevention
            if (cmbBranches.Visible == false)
            {

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

            }
            else
            {

                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;
                }


            }

            // Prevents SQL Injections 
            // Parameterized Approach

            string CateQuery = "select CategoryName from {0}.inventorycategories where  CategoryName = @CategoryName;";
            string command = string.Format(CateQuery, BranchAlias);
            MySqlParameter[] parameters =
            {
               new MySqlParameter("@CategoryName",txtCategoryName.Text),
               };

            CatValue = Convert.ToString(ObjMain.NewExecuteScalar(command, parameters));
            //CatValue = Convert.ToString(Cmd.ExecuteScalar());     Existing Code 

            Grp1_Maxid = ObjSel.GetMax_CatID(BranchAlias);

            if (CatValue != "")
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Category Name Already Exist ');location.href='CategoryMaster.aspx'", true);
            }
            else
            {
                string Active = "-1";
                string Updated = "0";
                string BranchUpdated = "0";
                string Fireindex = "0";

                string ReadTax = "00000000";
                string ReadPrinter = "00000000000000000000";

                Flag2Value();        // Printer Values 
                ResetCheckboxCP();

                Flag1Value();        // Tax Values 
                ResetCheckboxCT();



                if ((hidenCategoryTax.Value != "") && (HideCatPrinter.Value != ""))
                {
                    // Insert : Weborder Menu 

                    Ins_CatQuery = " Insert into {0}.inventorycategories(CategoryID,CategoryNumber,CategoryName,Description,DateCreated,Active,Updated,BranchUpdated,Flag1,Flag2,FireIndex)" +
                                   " values" +
                                   " (@CategoryID,@CategoryNumber ,@CategoryName,@Description,@DateCreated,@Active,@Updated,@BranchUpdated,@Flag1,@Flag2,@FireIndex) ";
                    string commandA = string.Format(Ins_CatQuery, BranchAlias);
                    MySqlParameter[] paramss =
                     {
                     new MySqlParameter("@CategoryID",Grp1_Maxid),
                     new MySqlParameter("@CategoryNumber",Grp1_Maxid),
                     new MySqlParameter("@CategoryName", txtCategoryName.Text),
                     new MySqlParameter("@Description", txtCategoryDescription.Text),
                     new MySqlParameter("@DateCreated", Curr_DateOnly),
                     new MySqlParameter("@Active", Active),
                     new MySqlParameter("@Updated", Updated),
                     new MySqlParameter("@BranchUpdated", BranchUpdated),
                     new MySqlParameter("@Flag1", hidenCategoryTax.Value),
                     new MySqlParameter("@Flag2", HideCatPrinter.Value),
                     new MySqlParameter("@FireIndex", Fireindex),

                    };
                    Session["MaxID"] = Grp1_Maxid;

                    K = ObjMain.ExecuteQuery(commandA, paramss);
                }

                else
                {
                    Ins_CatQuery = " Insert into {0}.inventorycategories(CategoryID,CategoryNumber,CategoryName,Description,DateCreated,Active,Updated,BranchUpdated,Flag1,Flag2,FireIndex)" +
                                  " values" +
                                  " (@CategoryID,@CategoryNumber ,@CategoryName,@Description,@DateCreated,@Active,@Updated,@BranchUpdated,@Flag1,@Flag2,@FireIndex) ";


                    string commandA = string.Format(Ins_CatQuery, BranchAlias);
                    MySqlParameter[] paramss =
                     {
                     new MySqlParameter("@CategoryID",Grp1_Maxid),
                     new MySqlParameter("@CategoryNumber",Grp1_Maxid),
                     new MySqlParameter("@CategoryName", txtCategoryName.Text),
                     new MySqlParameter("@Description", txtCategoryDescription.Text),
                     new MySqlParameter("@DateCreated", Curr_DateOnly),
                     new MySqlParameter("@Active", Active),
                     new MySqlParameter("@Updated", Updated),
                     new MySqlParameter("@BranchUpdated", BranchUpdated),
                     new MySqlParameter("@Flag1", ReadTax),
                     new MySqlParameter("@Flag2", ReadPrinter),
                     new MySqlParameter("@FireIndex", Fireindex),

                    };
                    Session["MaxID"] = Grp1_Maxid;
                    K = ObjMain.ExecuteQuery(commandA, paramss);

                }
                // Insert : Weborder Menu Sync
                // Check Wether Menu Table Exist Or Not 
                string Qr1 = "SELECT table_name FROM information_schema.tables WHERE table_schema = '{0}' AND table_name = 'Menu';";
                string commandx = string.Format(Qr1, BranchAlias);
                ChkTableExist = ObjMain.ExecuteScalar(commandx);


                if (ChkTableExist != "")
                {
                    string EmptyDate = "1001-10-10 00:00:00";
                    URL = "~/ProductDetails.aspx?Categoryid=" + Session["MaxID"].ToString() + "";

                    string Qr2 = "SELECT max(seqno)+1 from {0}.Menu;";
                    string commandY = string.Format(Qr2, BranchAlias);
                    MaxSeqNo = ObjMain.ExecuteScalar(commandY);

                    Ins_Menu = "Insert into {0}.Menu (ParentMenuId,Title,Description,Url,CategoryId,SeqNo,Visibility,FromTime,ToTime)" +
                                "values" +
                                "(@ParentMenuId,@Title,@Description,@Url,@CategoryId,@SeqNo,@Visibility,@FromTime,@ToTime )";

                    string commandB = string.Format(Ins_Menu, BranchAlias);

                    MySqlParameter[] paramss =
                    {
                     new MySqlParameter("@ParentMenuId", int.Parse("0")),
                     new MySqlParameter("@Title",txtCategoryName.Text),
                     new MySqlParameter("@Description",  txtCategoryDescription.Text),
                     new MySqlParameter("@Url", URL),
                     new MySqlParameter("@CategoryId", Session["MaxID"].ToString() ),
                     new MySqlParameter("@SeqNo", MaxSeqNo),
                     new MySqlParameter("@Visibility", "Enable"),
                     new MySqlParameter("@FromTime", EmptyDate),
                     new MySqlParameter("@ToTime", EmptyDate),
                    };

                    U = ObjMain.ExecuteQuery(commandB, paramss);
                }
                else
                {

                }

                // Insert : End 


                txtCategoryName.Text = "";
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Category Name Created ');location.href='CategoryMaster.aspx'", true);
                txtCategoryName.Text = "";
                txtCategoryDescription.Text = "";

            }

        }

        public void ClearForm()
        {

            txtCategoryName.Text = "";

            txtCategoryName.Text = "";
            txtCategoryDescription.Text = "";

        }



        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            // XSS Attack Prevention

            if (cmbBranches.Visible == false)
            {

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

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

            Flag2Value();
            Flag1Value();


            string ReadTax = "00000000";
            string ReadPrinter = "00000000000000000000";

            ObjPL.CategoryId = hidCategoryNo.Value;
            ObjPL.CategoryName = txtCategoryName.Text;
            ObjPL.Description = txtCategoryDescription.Text;
            ObjPL.CreatedDt = Convert.ToDateTime(Curr_DateOnly);
            // Get Flag 1 And Flag 2 Value :  

            if ((hidenCategoryTax.Value != "") && (HideCatPrinter.Value != ""))
            {
                ObjPL.Flag1 = hidenCategoryTax.Value;
                ObjPL.Flag2 = HideCatPrinter.Value;


                string UQuery = "UPDATE  {0}.inventorycategories  " +
                    "set  " +
                    "CategoryName=@CategoryName,Description=@Description ,Flag1=@Flag1," +
                    "Flag2=@Flag2  where CategoryID=@CategoryID   ";
                string command = string.Format(UQuery, BranchAlias);

                MySqlParameter[] paramss =
                  {
                     new MySqlParameter("@CategoryName",ObjPL.CategoryName),
                     new MySqlParameter("@Description", ObjPL.Description),
                     new MySqlParameter("@Flag1", ObjPL.Flag1 ),
                     new MySqlParameter("@Flag2",ObjPL.Flag2),
                     new MySqlParameter("@CategoryID",ObjPL.CategoryId),
                  };


                U = ObjMain.ExecuteQuery(command, paramss);



                #region  Update Weborder : Menu Table 

                // Prevents SQL Injections 
                // Parameterized Approch 


                string UPTWeb_Category = " Update {0}.Menu " +
                                         " set Title=@Title , Description=@Description where  CategoryID=@CategoryID;";
                string commandC = string.Format(UPTWeb_Category, BranchAlias);
                MySqlParameter[] paramss1 =
                 {
                     new MySqlParameter("@Title",ObjPL.CategoryName),
                     new MySqlParameter("@Description",ObjPL.Description),
                     new MySqlParameter("@CategoryID",ObjPL.CategoryId),
                 };
                S = ObjMain.ExecuteQuery(commandC, paramss1);
                #endregion

                if (U > 0)
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Category Updated Successfully ') ;location.href='CategoryMaster.aspx'", true);
                }

                else
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Category Not Updated Successfully ') ", true);
                }


            }

            else
            {

                string UQuery = " UPDATE  {0}.inventorycategories  set " +
                                " CategoryName=@CategoryName,Description=@Description ,Flag1=@Flag1," +
                                " Flag2=@Flag2  where CategoryID=@CategoryID   ";

                string cmmandD = string.Format(UQuery, BranchAlias);


                MySqlParameter[] paramss1 =
                {
                     new MySqlParameter("@CategoryName",ObjPL.CategoryName),
                     new MySqlParameter("@Description",ObjPL.Description ),
                     new MySqlParameter("@Flag1",ReadTax ),
                     new MySqlParameter("@Flag2",ReadPrinter ),
                     new MySqlParameter("@CategoryID",ObjPL.CategoryId ),
                 };


                U = ObjCloudMain.ExecuteQuery(cmmandD, paramss1);

                //U = ObjCloudMain.ExecuteNonQuery(UQuery);  Existing Code 
                if (U > 0)
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Category Updated Successfully ');location.href='CategoryMaster.aspx' ", true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Category Not Updated Successfully ') ", true);
                }


            }
        }

        public void Flag1Value()
        {
            string[] Val = new string[8];

            //if (chkInvandCate.Checked == true)
            //{
            //    Val[0] = "1";

            //}
            //else
            //{
            //    Val[0] = "0";
            //}

            // Check 1 Val of Tax Identifier. 
            // If Anyone of the Tax  is enabled ,it became '0' 
            // If All the checkbox is unchecked , then it became to '1'

            if (chkTaxRate6.Checked == true || chkTaxRate5.Checked == true || chkTax4.Checked == true || chkTax3.Checked == true || chkScvc.Checked == true || chkGstt.Checked == true)
            {
                Val[0] = "0";

            }
            else
            {
                Val[0] = "0";

            }

            if (chkAlways.Checked == true)
            {
                Val[1] = "1";
            }
            else
            {
                Val[1] = "0";
            }

            if (chkTaxRate6.Checked == true)
            {
                Val[2] = "1";

            }
            else
            {
                Val[2] = "0";
            }

            if (chkTaxRate5.Checked == true)
            {
                Val[3] = "1";
            }
            else
            {
                Val[3] = "0";
            }
            if (chkTax4.Checked == true)
            {
                Val[4] = "1";
            }
            else
            {
                Val[4] = "0";
            }

            if (chkTax3.Checked == true)
            {
                Val[5] = "1";
            }
            else
            {
                Val[5] = "0";
            }
            if (chkScvc.Checked == true)
            {
                Val[6] = "1";
            }
            else
            {
                Val[6] = "0";
            }
            if (chkGstt.Checked == true)
            {
                Val[7] = "1";

            }
            else
            {
                Val[7] = "0";
            }


            StringBuilder Build = new StringBuilder();
            foreach (string value in Val)
            {
                Build.Append(value);
            }

            hidenCategoryTax.Value = Convert.ToString(Build);

            if (txtCategoryName.Text == "")
            {
                btnSaveCategory.Visible = true;
                btnSaveCategory.CssClass = "btn btn-primary";

                btnUpdate.Visible = false;
                btnUpdate.CssClass = "btn btn-primary";

            }
            else
            {
                btnUpdate.Visible = true;
                btnUpdate.CssClass = "btn btn-primary";

            }




            Session["CTSettings"] = Build.ToString();
            lblmsgCat_Tax.Text = Session["CTSettings"].ToString();


        }

        public void ResetCheckboxCT()
        {
            chkInvandCate.Checked = false;
            chkAlways.Checked = false;
            chkTaxRate6.Checked = false;
            chkTaxRate5.Checked = false;
            chkTax4.Checked = false;
            chkTax3.Checked = false;
            chkScvc.Checked = false;
            chkGstt.Checked = false;
        }

        protected void btnSavePrinter_Click(object sender, EventArgs e)
        {
            Flag2Value();
            ResetCheckboxCP();
        }

        public void Flag2Value()
        {
            string[] Printer = new string[20];

            //if (chkPrinterInven.Checked == true)
            //{
            //    Printer[0] = "1";

            //}
            //else
            //{
            //    Printer[0] = "0";
            //}


            if (chkRP19.Checked == true || chkRP18.Checked == true || chkRP17.Checked == true || chkRP16.Checked == true || chkRP15.Checked == true || chkRP14.Checked == true || chkRP13.Checked == true || chkRP12.Checked == true || chkRP11.Checked == true || chkRP10.Checked == true || chkRP9.Checked == true || chkRP8.Checked == true || chkRP7.Checked == true || chkRP6.Checked == true || chkRP5.Checked == true || chkRP4.Checked == true || chkRP3.Checked == true || chkRP2.Checked == true || chkRP1.Checked == true)
            {
                Printer[0] = "1";

            }
            else
            {
                Printer[0] = "0";
            }







            if (chkRP19.Checked == true)
            {
                Printer[1] = "1";
            }
            else
            {
                Printer[1] = "0";
            }

            if (chkRP18.Checked == true)
            {
                Printer[2] = "1";

            }
            else
            {
                Printer[2] = "0";
            }

            if (chkRP17.Checked == true)
            {
                Printer[3] = "1";
            }
            else
            {
                Printer[3] = "0";
            }
            if (chkRP16.Checked == true)
            {
                Printer[4] = "1";
            }
            else
            {
                Printer[4] = "0";
            }

            if (chkRP15.Checked == true)
            {
                Printer[5] = "1";
            }
            else
            {
                Printer[5] = "0";
            }
            if (chkRP14.Checked == true)
            {
                Printer[6] = "1";
            }
            else
            {
                Printer[6] = "0";
            }
            if (chkRP13.Checked == true)
            {
                Printer[7] = "1";

            }
            else
            {
                Printer[7] = "0";
            }
            if (chkRP12.Checked == true)
            {
                Printer[8] = "1";

            }
            else
            {
                Printer[8] = "0";
            }


            if (chkRP11.Checked == true)
            {
                Printer[9] = "1";

            }
            else
            {
                Printer[9] = "0";
            }

            if (chkRP10.Checked == true)
            {
                Printer[10] = "1";

            }
            else
            {
                Printer[10] = "0";
            }
            if (chkRP9.Checked == true)
            {
                Printer[11] = "1";

            }
            else
            {
                Printer[11] = "0";
            }
            if (chkRP8.Checked == true)
            {
                Printer[12] = "1";

            }
            else
            {
                Printer[12] = "0";
            }

            if (chkRP7.Checked == true)
            {
                Printer[13] = "1";

            }
            else
            {
                Printer[13] = "0";
            }
            if (chkRP6.Checked == true)
            {
                Printer[14] = "1";

            }
            else
            {
                Printer[14] = "0";
            }
            if (chkRP5.Checked == true)
            {
                Printer[15] = "1";

            }
            else
            {
                Printer[15] = "0";
            }

            if (chkRP4.Checked == true)
            {
                Printer[16] = "1";

            }
            else
            {
                Printer[16] = "0";
            }


            if (chkRP3.Checked == true)
            {
                Printer[17] = "1";

            }
            else
            {
                Printer[17] = "0";
            }


            if (chkRP2.Checked == true)
            {
                Printer[18] = "1";

            }
            else
            {
                Printer[18] = "0";
            }

            if (chkRP1.Checked == true)
            {
                Printer[19] = "1";

            }
            else
            {
                Printer[19] = "0";
            }

            StringBuilder BuildValue = new StringBuilder();
            foreach (string value in Printer)
            {
                BuildValue.Append(value);
            }

            if (txtCategoryName.Text == "")
            {
                btnSaveCategory.Visible = true;
                btnSaveCategory.CssClass = "btn btn-primary";

                btnUpdate.Visible = false;
                btnUpdate.CssClass = "btn btn-primary";

            }
            else
            {
                btnUpdate.Visible = true;
                btnUpdate.CssClass = "btn btn-primary";

            }

            HideCatPrinter.Value = BuildValue.ToString();
            lblmsgCate_Printer.Text = BuildValue.ToString();

        }

        public void ResetCheckboxCP()
        {
            chkPrinterInven.Checked = false;
            chkRP19.Checked = false;
            chkRP18.Checked = false;
            chkRP17.Checked = false;
            chkRP16.Checked = false;
            chkRP15.Checked = false;
            chkRP14.Checked = false;
            chkRP13.Checked = false;
            chkRP12.Checked = false;
            chkRP11.Checked = false;
            chkRP10.Checked = false;
            chkRP9.Checked = false;
            chkRP8.Checked = false;
            chkRP7.Checked = false;
            chkRP6.Checked = false;
            chkRP5.Checked = false;
            chkRP4.Checked = false;
            chkRP3.Checked = false;
            chkRP2.Checked = false;
            chkRP1.Checked = false;
        }

        protected void btnCateSearch_Click(object sender, EventArgs e)
        {

            Load_SearchData(BranchAlias, txtCateSearch.Text);
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "ShowModalCategoryItems();", true);
        }

        protected void grdCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            // XSS Attack Prevention 
            if (cmbBranches.Visible == false)
            {

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

            }
            else
            {
                if (cmbBranches.SelectedItem.Text == "HQ")
                {
                    // btnExcel.Enabled = true;
                    // HQ Validation    02-12-21
                    BranchFullName = 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 = 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;
                }
            }

            btnSaveCategory.Visible = false;
            btnSaveCategory.Enabled = false;
            btnSaveCategory.CssClass = "btn btn-primary";
            btnUpdate.Visible = true;
            btnUpdate.Enabled = true;
            btnDelete.Visible = true;
            btnUpdate.CssClass = "btn btn-primary";
            RCateYNo = (grdCategory.SelectedRow.FindControl("lblCategoryNo1") as Label).Text;
            hidCategoryNo.Value = RCateYNo;
            DTCategorydl = ObjSel.ReadCategoryByNo(BranchAlias, RCateYNo);
            RCategoryNo = DTCategorydl.Rows[0]["CategoryNumber"].ToString();
            RCategoryName = DTCategorydl.Rows[0]["CategoryName"].ToString();
            RCate_Descp = DTCategorydl.Rows[0]["Description"].ToString();
            RActive_Stauts = DTCategorydl.Rows[0]["Active"].ToString();
            RUpdate_Status = DTCategorydl.Rows[0]["Updated"].ToString();
            RBranch_Update = DTCategorydl.Rows[0]["BranchUpdated"].ToString();
            RFlag1_Tax = DTCategorydl.Rows[0]["Flag1"].ToString();
            RFlag2_Printer = DTCategorydl.Rows[0]["Flag2"].ToString();
            RFireIndex = DTCategorydl.Rows[0]["FireIndex"].ToString();
            //txtCategoryNos.Text = RCateYNo;
            txtCategoryName.Text = RCategoryName;
            txtCategoryDescription.Text = RCate_Descp;
            lblCategoryName.Text = RCategoryName;
            hdnurl.Value = "https://suntoyo-aws-s3.s3.ap-southeast-1.amazonaws.com/309/outlet1/category/jpg/classicdelight.jpg";

            Session["RFlag1_Tax"] = RFlag1_Tax;
            Session["RFlag2_Printer"] = RFlag2_Printer;

            // Get the Flag1 Values - Tax Setting 
            char[] Flag_Val = RFlag1_Tax.ToCharArray();
            string c0 = Flag_Val[0].ToString();  //  1: Inventory Flag ; 0:Category Flag
            string c1 = Flag_Val[1].ToString();  //  Always : 0
            string c2 = Flag_Val[2].ToString();  //  Tax Rate 6
            string c3 = Flag_Val[3].ToString();  //  Tax Rate 5
            string c4 = Flag_Val[4].ToString();  //  Tax Rate 4
            string c5 = Flag_Val[5].ToString();  //  Tax Rate 3 
            string c6 = Flag_Val[6].ToString();  //  Scv.C
            string c7 = Flag_Val[7].ToString();  //  GST 

            if (RFlag1_Tax != "00000000")
            {

                chkT.Checked = true;
            }
            else
            {

                chkT.Checked = false;
            }
            // Assgin values to Checkbox : 

            if (c0.Equals("1"))
            {
                chkInvandCate.Checked = false;
            }
            else
            {
                chkInvandCate.Checked = false;
            }


            if (c1.Equals("1"))
            {
                chkAlways.Checked = true;
            }
            else
            {
                chkAlways.Checked = false;
            }

            if (c2.Equals("1"))
            {
                chkTaxRate6.Checked = true;
            }
            else
            {
                chkTaxRate6.Checked = false;
            }

            if (c3.Equals("1"))
            {
                chkTaxRate5.Checked = true;
            }
            else
            {
                chkTaxRate5.Checked = false;
            }

            if (c4.Equals("1"))
            {
                chkTax4.Checked = true;
            }
            else
            {
                chkTax4.Checked = false;
            }

            if (c5.Equals("1"))
            {
                chkTax3.Checked = true;
            }
            else
            {
                chkTax3.Checked = false;
            }

            if (c6.Equals("1"))
            {
                chkScvc.Checked = true;
            }
            else
            {
                chkScvc.Checked = false;
            }


            if (c7.Equals("1"))
            {
                chkGstt.Checked = true;
            }
            else
            {
                chkGstt.Checked = false;
            }

            if (RFlag2_Printer != "00000000000000000000")
            {


                chkP.Checked = true;
            }
            else
            {

                chkP.Checked = false;
            }

            //  Get the Flag2 Values : Printer Settings

            char[] Flag2_Val = RFlag2_Printer.ToCharArray();

            string P0 = RFlag2_Printer[0].ToString();           //1: Inventory Flag ; 0:Category Flag  
            string P1 = RFlag2_Printer[1].ToString();           // Print on Remote Printer 19                               
            string P2 = RFlag2_Printer[2].ToString();           // Print on Remote Printer 18  
            string P3 = RFlag2_Printer[3].ToString();           //  Print on Remote Printer 17  
            string P4 = RFlag2_Printer[4].ToString();           //  Print on Remote Printer 16  
            string P5 = RFlag2_Printer[5].ToString();          //   Print on Remote Printer 15  
            string P6 = RFlag2_Printer[6].ToString();          //   Print on Remote Printer 14   
            string P7 = RFlag2_Printer[7].ToString();          //  Print on Remote Printer 13  
            string P8 = RFlag2_Printer[8].ToString();          //  Print on Remote Printer 12 
            string P9 = RFlag2_Printer[9].ToString();          //  Print on Remote Printer 11 
            string P10 = RFlag2_Printer[10].ToString();       //   Print on Remote Printer 10
            string P11 = RFlag2_Printer[11].ToString();       //   Print on Remote Printer 9
            string P12 = RFlag2_Printer[12].ToString();       //   Print on Remote Printer 8
            string P13 = RFlag2_Printer[13].ToString();       //   Print on Remote Printer 7
            string P14 = RFlag2_Printer[14].ToString();       //   Print on Remote Printer 6
            string P15 = RFlag2_Printer[15].ToString();       //   Print on Remote Printer 5
            string P16 = RFlag2_Printer[16].ToString();       //   Print on Remote Printer 4
            string P17 = RFlag2_Printer[17].ToString();       //   Print on Remote Printer 3
            string P18 = RFlag2_Printer[18].ToString();       //   Print on Remote Printer 2 
            string P19 = RFlag2_Printer[19].ToString();       //   Print on Remote Printer 1

            // Get the Printer Value and Its Lable 


            string Printer_Query = "select id,PrinterNo,PrinterName,Visible  from  " + BranchAlias + ".PrinterLable;";
            DTPrinterList = ObjCloudMain.GetData_DT(Printer_Query);

            if (DTPrinterList.Rows.Count > 0)
            {
                // Load 19th 

                RPrintno19 = DTPrinterList.Rows[0]["Visible"].ToString();
                PrinterName19 = DTPrinterList.Rows[0]["PrinterName"].ToString();
                lblLPrinter19.Text = PrinterName19;  // Assign to  Lable

                if (RPrintno19.Equals("0"))
                {
                    chkRP19.Visible = false;
                    lblLPrinter19.Visible = false;
                }
                else
                {
                    chkRP19.Visible = true;
                    lblLPrinter19.Visible = true;
                }


                // Load 18th
                RPrintno18 = DTPrinterList.Rows[1]["Visible"].ToString();
                PrinterName18 = DTPrinterList.Rows[1]["PrinterName"].ToString();
                lblLPrinter18.Text = PrinterName18;  // Assign to  Lable

                if (RPrintno18.Equals("0"))
                {
                    chkRP18.Visible = false;
                    lblLPrinter18.Visible = false;
                }
                else
                {
                    chkRP18.Visible = true;
                    lblLPrinter18.Visible = true;

                }


                // Load 17th 
                RPrintno17 = DTPrinterList.Rows[2]["Visible"].ToString();
                PrinterName17 = DTPrinterList.Rows[2]["PrinterName"].ToString();
                lblLPrinter17.Text = PrinterName17;  // Assign to  Lable

                if (RPrintno17.Equals("0"))
                {
                    chkRP17.Visible = false;
                    lblLPrinter17.Visible = false;
                }
                else
                {
                    chkRP17.Visible = true;
                    lblLPrinter17.Visible = true;
                }



                // Load 16th 
                RPrintno16 = DTPrinterList.Rows[3]["Visible"].ToString();
                PrinterName16 = DTPrinterList.Rows[3]["PrinterName"].ToString();
                lblLPrinter16.Text = PrinterName16;  // Assign to  Lable
                if (RPrintno16.Equals("0"))
                {
                    chkRP16.Visible = false;
                    lblLPrinter16.Visible = false;

                }
                else
                {
                    chkRP16.Visible = true;
                    lblLPrinter16.Visible = true;

                }
                // Load 15th 
                RPrintno15 = DTPrinterList.Rows[4]["Visible"].ToString();
                PrinterName15 = DTPrinterList.Rows[4]["PrinterName"].ToString();
                lblLPrinter15.Text = PrinterName15;  // Assign to  Lable
                if (RPrintno15.Equals("0"))
                {
                    chkRP15.Visible = false;
                    lblLPrinter15.Visible = false;

                }
                else
                {
                    chkRP15.Visible = true;
                    lblLPrinter15.Visible = true;
                }
                // Load 14th
                RPrintno14 = DTPrinterList.Rows[5]["Visible"].ToString();
                PrinterName14 = DTPrinterList.Rows[5]["PrinterName"].ToString();
                lblLPrinter14.Text = PrinterName14;  // Assign to  Lable
                if (RPrintno14.Equals("0"))
                {
                    chkRP14.Visible = false;
                    lblLPrinter14.Visible = false;
                }
                else
                {
                    chkRP14.Visible = true;
                    lblLPrinter14.Visible = true;
                }
                // Load 13th
                RPrintno13 = DTPrinterList.Rows[6]["Visible"].ToString();
                PrinterName13 = DTPrinterList.Rows[6]["PrinterName"].ToString();
                lblLPrinter13.Text = PrinterName13;  // Assign to  Lable
                if (RPrintno13.Equals("0"))
                {
                    chkRP13.Visible = false;
                    lblLPrinter13.Visible = false;
                }
                else
                {
                    chkRP13.Visible = false;
                    lblLPrinter13.Visible = false;
                }

                // Load 12th
                RPrintno12 = DTPrinterList.Rows[7]["Visible"].ToString();
                PrinterName12 = DTPrinterList.Rows[7]["PrinterName"].ToString();
                lblLPrinter12.Text = PrinterName12;  // Assign to  Lable
                if (RPrintno12.Equals("0"))
                {
                    chkRP12.Visible = false;
                    lblLPrinter12.Visible = false;
                }
                else
                {
                    chkRP12.Visible = true;
                    lblLPrinter12.Visible = true;
                }



                // Load 11th
                RPrintno11 = DTPrinterList.Rows[8]["Visible"].ToString();
                PrinterName11 = DTPrinterList.Rows[8]["PrinterName"].ToString();
                lblLPrinter11.Text = PrinterName11;  // Assign to  Lable
                if (RPrintno11.Equals("0"))
                {
                    chkRP11.Visible = false;
                    lblLPrinter11.Visible = false;
                }
                else
                {
                    chkRP11.Visible = true;
                    lblLPrinter11.Visible = true;
                }


                // Load 10th 
                RPrintno10 = DTPrinterList.Rows[9]["Visible"].ToString();
                PrinterName10 = DTPrinterList.Rows[9]["PrinterName"].ToString();
                lblLPrinter10.Text = PrinterName10;  // Assign to  Lable
                if (RPrintno10.Equals("0"))
                {
                    chkRP10.Visible = false;
                    lblLPrinter10.Visible = false;
                }
                else
                {
                    chkRP10.Visible = true;
                    lblLPrinter10.Visible = true;
                }

                // Load 9th 
                RPrintno9 = DTPrinterList.Rows[10]["Visible"].ToString();
                PrinterName9 = DTPrinterList.Rows[10]["PrinterName"].ToString();
                lblLPrinter9.Text = PrinterName9;  // Assign to  Lable
                if (RPrintno9.Equals("0"))
                {
                    chkRP9.Visible = false;
                    lblLPrinter9.Visible = false;
                }
                else
                {
                    chkRP9.Visible = true;
                    lblLPrinter9.Visible = true;
                }

                // Load 8th 
                RPrintno8 = DTPrinterList.Rows[11]["Visible"].ToString();
                PrinterName8 = DTPrinterList.Rows[11]["PrinterName"].ToString();
                lblLPrinter8.Text = PrinterName8;  // Assign to  Lable
                if (RPrintno8.Equals("0"))
                {
                    chkRP8.Visible = false;
                    lblLPrinter8.Visible = false;
                }
                else
                {
                    chkRP8.Visible = false;
                    lblLPrinter8.Visible = false;
                }

                // Load 7th 
                RPrintno7 = DTPrinterList.Rows[12]["Visible"].ToString();
                PrinterName7 = DTPrinterList.Rows[12]["PrinterName"].ToString();
                lblLPrinter7.Text = PrinterName7;  // Assign to  Lable
                if (RPrintno7.Equals("0"))
                {
                    chkRP7.Visible = false;
                    lblLPrinter7.Visible = false;
                }
                else
                {
                    chkRP7.Visible = true;
                    lblLPrinter7.Visible = true;

                }


                // Load 6th 
                RPrintno6 = DTPrinterList.Rows[13]["Visible"].ToString();
                PrinterName6 = DTPrinterList.Rows[13]["PrinterName"].ToString();
                lblLPrinter6.Text = PrinterName6;  // Assign to  Lable
                if (RPrintno6.Equals("0"))
                {
                    chkRP6.Visible = false;
                    lblLPrinter6.Visible = false;
                }
                else
                {
                    chkRP6.Visible = true;
                    lblLPrinter6.Visible = true;

                }

                // Load 5th 
                RPrintno5 = DTPrinterList.Rows[14]["Visible"].ToString();
                PrinterName5 = DTPrinterList.Rows[14]["PrinterName"].ToString();
                lblLPrinter5.Text = PrinterName5;  // Assign to  Lable
                if (RPrintno5.Equals("0"))
                {
                    chkRP5.Visible = false;
                    lblLPrinter5.Visible = false;
                }
                else
                {

                    chkRP5.Visible = true;
                    lblLPrinter5.Visible = true;
                }
                // Load 4th 
                RPrintno4 = DTPrinterList.Rows[15]["Visible"].ToString();
                PrinterName4 = DTPrinterList.Rows[15]["PrinterName"].ToString();
                lblLPrinter4.Text = PrinterName4;  // Assign to  Lable
                if (RPrintno4.Equals("0"))
                {
                    chkRP4.Visible = false;
                    lblLPrinter4.Visible = false;

                }
                else
                {
                    chkRP4.Visible = true;
                    lblLPrinter4.Visible = true;

                }

                // Load 3rd 
                RPrintno3 = DTPrinterList.Rows[16]["Visible"].ToString();
                PrinterName3 = DTPrinterList.Rows[16]["PrinterName"].ToString();
                lblLPrinter3.Text = PrinterName3;  // Assign to  Lable
                if (RPrintno3.Equals("0"))
                {
                    chkRP3.Visible = false;
                    lblLPrinter3.Visible = false;

                }
                else
                {
                    chkRP3.Visible = true;
                    lblLPrinter3.Visible = true;
                }

                // Load 2nd 
                RPrintno2 = DTPrinterList.Rows[17]["Visible"].ToString();
                PrinterName2 = DTPrinterList.Rows[17]["PrinterName"].ToString();
                lblLPrinter2.Text = PrinterName2;  // Assign to  Lable

                if (RPrintno2.Equals("0"))
                {
                    chkRP2.Visible = false;
                    lblLPrinter2.Visible = false;
                }
                else
                {
                    chkRP2.Visible = true;
                    lblLPrinter2.Visible = true;

                }

                // Load 1st
                RPrintno1 = DTPrinterList.Rows[18]["Visible"].ToString();
                PrinterName1 = DTPrinterList.Rows[18]["PrinterName"].ToString();
                lblLPrinter1.Text = PrinterName1;  // Assign to  Lable

                if (RPrintno1.Equals("0"))
                {
                    chkRP1.Visible = false;
                    lblLPrinter1.Visible = false;

                }
                else
                {
                    chkRP1.Visible = true;
                    lblLPrinter1.Visible = true;

                }


            }

            if (RActive_Stauts == "-1")
            {
                chkActiveStatus.Checked = true;
            }
            else
            {
                chkActiveStatus.Checked = false;
            }

            // Assign Values to Checkbox : 

            if (P0.Equals("1"))
            {
                chkPrinterInven.Checked = true;

            }
            else
            {
                chkPrinterInven.Checked = false;
            }


            // Check the Visible value For Printer 19
            if (RPrintno19.Equals("1"))
            {
                if (P1.Equals("1"))
                {

                    chkRP19.Checked = true;
                    chkRP19.Visible = true;
                    lblLPrinter19.Visible = true;

                }
                else
                {


                    chkRP19.Checked = false;
                    chkRP19.Visible = true;
                    lblLPrinter19.Visible = true;
                }



            }
            else
            {
                chkRP19.Visible = false;
                lblLPrinter19.Visible = false;


            }
            // Check the Visible value For Printer 18

            if (RPrintno18.Equals("1"))
            {
                if (P2.Equals("1"))
                {
                    chkRP18.Checked = true;

                    chkRP18.Visible = true;
                    lblLPrinter18.Visible = true;
                }
                else
                {
                    chkRP18.Checked = false;

                    chkRP18.Visible = true;
                    lblLPrinter18.Visible = true;
                }



            }
            else
            {
                chkRP18.Visible = false;
                lblLPrinter18.Visible = false;

            }

            // Check the Visible value For Printer 17
            if (RPrintno17.Equals("1"))
            {
                if (P3.Equals("1"))
                {
                    chkRP17.Checked = true;
                    chkRP17.Visible = true;
                    lblLPrinter18.Visible = true;

                }
                else
                {
                    chkRP17.Checked = false;
                    chkRP17.Visible = true;
                    lblLPrinter17.Visible = true;

                }

            }
            else
            {
                chkRP17.Visible = false;
                lblLPrinter17.Visible = false;

            }



            // Check the Visible value For Printer 16
            if (RPrintno16.Equals("1"))
            {
                if (P4.Equals("1"))
                {
                    chkRP16.Checked = true;
                    chkRP16.Visible = true;
                    lblLPrinter16.Visible = true;
                }
                else
                {
                    chkRP16.Checked = false;
                    chkRP16.Visible = true;
                    lblLPrinter16.Visible = true;
                }

            }
            else
            {
                chkRP16.Visible = false;
                lblLPrinter16.Visible = false;

            }


            // Check the Visible value For Printer 15
            if (RPrintno15.Equals("1"))
            {
                if (P5.Equals("1"))
                {
                    chkRP15.Checked = true;

                }
                else
                {
                    chkRP15.Checked = false;
                    chkRP15.Visible = true;
                    lblLPrinter15.Visible = true;
                }

            }
            else
            {
                chkRP15.Visible = false;
                lblLPrinter15.Visible = false;

            }


            // Check the Visible value For Printer 14 : 

            if (RPrintno14.Equals("1"))
            {
                if (P6.Equals("1"))
                {
                    chkRP14.Checked = true;
                    chkRP14.Visible = true;
                    lblLPrinter14.Visible = true;
                }
                else
                {
                    chkRP14.Checked = false;
                    chkRP14.Visible = true;
                    lblLPrinter14.Visible = true;
                }

            }
            else
            {
                chkRP14.Visible = false;
                lblLPrinter14.Visible = false;
            }

            // Check the Visible value For Printer 13
            if (RPrintno13.Equals("1"))
            {
                if (P7.Equals("1"))
                {
                    chkRP13.Checked = true;
                    chkRP13.Visible = true;
                    lblLPrinter13.Visible = true;
                }
                else
                {
                    chkRP13.Checked = false;
                    chkRP13.Visible = true;
                    lblLPrinter13.Visible = true;


                }

            }
            else
            {
                chkRP13.Visible = false;
                lblLPrinter13.Visible = false;

            }

            // Check the Visible value For Printer 12
            if (RPrintno12.Equals("1"))
            {
                if (P8.Equals("1"))
                {
                    chkRP12.Checked = true;
                    chkRP12.Visible = true;
                    lblLPrinter12.Visible = true;
                }
                else
                {
                    chkRP12.Checked = false;
                    chkRP12.Visible = true;
                    lblLPrinter12.Visible = true;
                }


            }
            else
            {
                chkRP12.Visible = false;
                lblLPrinter12.Visible = false;
            }

            // Check the Visible value For Printer 11
            if (RPrintno11.Equals("1"))
            {
                if (P9.Equals("1"))
                {
                    chkRP11.Checked = true;
                    chkRP11.Visible = true;
                    lblLPrinter11.Visible = true;
                }
                else
                {
                    chkRP11.Checked = false;
                    chkRP11.Visible = true;
                    lblLPrinter11.Visible = true;
                }

            }
            else
            {
                chkRP11.Visible = false;
                lblLPrinter11.Visible = false;
            }

            // Check the Visible value For Printer 10
            if (RPrintno10.Equals("1"))
            {
                if (P10.Equals("1"))
                {
                    chkRP10.Checked = true;
                    chkRP10.Visible = true;
                    lblLPrinter10.Visible = true;
                }
                else
                {
                    chkRP10.Checked = false;
                    chkRP10.Visible = true;
                    lblLPrinter10.Visible = true;
                }


            }
            else
            {
                chkRP10.Visible = false;
                lblLPrinter10.Visible = false;
            }
            // Check the Visible value For Printer 9

            if (RPrintno9.Equals("1"))
            {
                if (P11.Equals("1"))
                {
                    chkRP9.Checked = true;
                    chkRP9.Visible = true;
                    lblLPrinter9.Visible = true;
                }
                else
                {
                    chkRP9.Checked = false;
                    chkRP9.Visible = true;
                    lblLPrinter9.Visible = true;
                }
            }
            else
            {
                chkRP9.Visible = false;
                lblLPrinter9.Visible = false;
            }
            // Check the Visible value For Printer 8

            if (RPrintno8.Equals("1"))
            {
                if (P12.Equals("1"))
                {
                    chkRP8.Checked = true;
                    chkRP8.Visible = true;
                    lblLPrinter8.Visible = true;
                }
                else
                {
                    chkRP8.Checked = false;
                    chkRP8.Visible = true;
                    lblLPrinter8.Visible = true;
                }

            }
            else
            {
                chkRP8.Visible = false;
                lblLPrinter8.Visible = false;
            }

            // Check the Visible value For Printer 7
            if (RPrintno7.Equals("1"))
            {
                if (P13.Equals("1"))
                {
                    chkRP7.Checked = true;
                    chkRP7.Visible = true;
                    lblLPrinter7.Visible = true;
                }
                else
                {
                    chkRP7.Checked = false;
                    chkRP7.Visible = true;
                    lblLPrinter7.Visible = true;

                }

            }
            else
            {
                chkRP7.Visible = false;
                lblLPrinter7.Visible = false;

            }
            // Check the Visible value For Printer 6

            if (RPrintno6.Equals("1"))
            {
                if (P14.Equals("1"))
                {
                    chkRP6.Checked = true;
                    chkRP6.Visible = true;
                    lblLPrinter6.Visible = true;
                }
                else
                {
                    chkRP6.Checked = false;
                    chkRP6.Visible = true;
                    lblLPrinter6.Visible = true;
                }

            }
            else
            {
                chkRP6.Visible = false;
                lblLPrinter6.Visible = false;
            }

            // Check the Visible value For Printer 5

            if (RPrintno5.Equals("1"))
            {
                if (P15.Equals("1"))
                {
                    chkRP5.Checked = true;
                    chkRP5.Visible = true;
                    lblLPrinter5.Visible = true;
                }
                else
                {
                    chkRP5.Checked = false;
                    chkRP5.Visible = true;
                    lblLPrinter5.Visible = true;
                }
            }
            else
            {
                chkRP5.Visible = false;
                lblLPrinter5.Visible = false;
            }

            // Check the Visible value For Printer 4
            if (RPrintno4.Equals("1"))
            {
                if (P16.Equals("1"))
                {
                    chkRP4.Checked = true;
                    chkRP4.Visible = true;
                    lblLPrinter4.Visible = true;
                }
                else
                {
                    chkRP4.Checked = false;
                    chkRP4.Visible = true;
                    lblLPrinter4.Visible = true;
                }

            }
            else
            {
                chkRP4.Visible = false;
                lblLPrinter4.Visible = false;

            }


            // Check the Visible value For Printer 3
            if (RPrintno3.Equals("1"))
            {
                if (P17.Equals("1"))
                {
                    chkRP3.Checked = true;
                    chkRP3.Visible = true;
                    lblLPrinter3.Visible = true;
                }
                else
                {
                    chkRP3.Checked = false;
                    chkRP3.Visible = true;
                    lblLPrinter3.Visible = true;
                }
            }
            else
            {
                chkRP3.Visible = false;
                lblLPrinter3.Visible = false;
            }
            // Check the Visible value For Printer 2

            if (RPrintno2.Equals("1"))
            {
                if (P18.Equals("1"))
                {
                    chkRP2.Checked = true;
                    chkRP2.Visible = true;
                    lblLPrinter2.Visible = true;
                }
                else
                {
                    chkRP2.Checked = false;
                    chkRP2.Visible = true;
                    lblLPrinter2.Visible = true;
                }


            }
            else
            {
                chkRP2.Visible = false;
                lblLPrinter2.Visible = false;
            }

            // Check the Visible value For Printer 1

            if (RPrintno1.Equals("1"))
            {
                if (P19.Equals("1"))
                {
                    chkRP1.Checked = true;
                    chkRP1.Visible = true;
                    lblLPrinter1.Visible = true;
                }
                else
                {
                    chkRP1.Checked = false;
                    chkRP1.Visible = true;
                    lblLPrinter1.Visible = true;
                }

            }
            else
            {
                chkRP1.Visible = false;
                lblLPrinter1.Visible = false;
            }



            System.Web.UI.ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "loadImage", "loadImage()", true);


        }


        public void LoadCategoriesDataList(string sDb)
        {

            DtCateoryList = ObjSel.GetCategoryList(sDb);
            grdCategory.DataSource = DtCateoryList;
            grdCategory.DataBind();
        }

        public void LoadCateCopyTax()
        {
            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;
            }

            DtCateoryList = ObjSel.GetCategoryList(BranchAlias);
            grdCopyTax.DataSource = DtCateoryList;
            grdCopyTax.DataBind();
        }


        public void Load_SearchData(string BranchNam, string SearchTxt)
        {
            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 QuerySrc = "Select CategoryID,CategoryName  from   {0}.inventorycategories  where  CategoryName like CONCAT('%', @CategoryName, '%')  ;";
            string command = string.Format(QuerySrc, BranchAlias);

            MySqlParameter[] parameters =
                   {
                     new MySqlParameter("@CategoryName",SearchTxt),
                   };

            DtCateoryList = ObjMain.NewGetData_DT(command, parameters);

            //N   DtCateoryList = ObjCloudMain.GetData_DT(QuerySrc);

            if (DtCateoryList.Rows.Count > 0)
            {
                grdCategory.DataSource = DtCateoryList;
                grdCategory.DataBind();
            }
            else
            {
                LoadCategoriesDataList(BranchAlias);
            }
        }


        protected void cmbCopytoallTax_SelectedIndexChanged(object sender, EventArgs e)
        {


            if (cmbCopytoallTax.SelectedItem.Text == "AllCategory")
            {
                grdCopyTax.Visible = false;
                lblTaxMessage.Text = " above Tax setting to be Applied All Categories.";

            }
            else if (cmbCopytoallTax.SelectedItem.Text == "Custom")
            {

                grdCopyTax.Visible = true;
                LoadCateCopyTax();
                lblTaxMessage.Text = " above Tax setting to be Applied Selectex Category.";
            }
            else if (cmbCopytoallTax.SelectedItem.Text == "Select")
            {
                grdCopyTax.Visible = false;
                lblTaxMessage.Text = string.Empty;
            }
            else
            {

            }


        }

        protected void btnCopy2Tax_Click(object sender, EventArgs e)
        {

            if (cmbCopytoallTax.SelectedItem.Text == "AllCategory")
            {
                if (!string.IsNullOrEmpty(Session["RFlag1_Tax"] as string) && txtCategoryName.Text != "" && txtCategoryDescription.Text != "")
                {
                    ObjPL.Flag1 = Session["RFlag1_Tax"].ToString();
                    //ObjPL.Flag2 = Session["RFlag2_Printer"].ToString();
                    UpdateTaxValueToAll(ObjPL.Flag1);                              // Update All Category 

                }
                else
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' You Must Select Category For Tax Setting')", true);
                }


            }
            else if (cmbCopytoallTax.SelectedItem.Text == "Custom")
            {

                if (!string.IsNullOrEmpty(Session["RFlag1_Tax"] as string) && txtCategoryName.Text != "" && txtCategoryDescription.Text != "")
                {
                    grdCopyTax.Visible = true;

                    ObjPL.Flag1 = Session["RFlag1_Tax"].ToString();
                    string str = string.Empty;
                    string strname = string.Empty;
                    foreach (GridViewRow gvrow in grdCopyTax.Rows)
                    {
                        var checkbox = gvrow.FindControl("chkSelect") as CheckBox;
                        if (checkbox.Checked)
                        {
                            Label lblcd_id_Values = gvrow.FindControl("lblCategoryNo1") as Label;
                            CateVl = lblcd_id_Values.Text;
                            UpdateTaxValueCustom(ObjPL.Flag1, CateVl);                 //  Update Tax Value : Custom
                        }

                    }


                }
                else
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' You Must Select Category For Tax Setting')", true);

                }

            }

        }

        public void UpdateTaxValueToAll(string Flag1val)
        {
            // XSS Attack Prevention 
            // Prevents SQL Injections
            // Parameterized Approach  


            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 qryTaxChageAll = "update  {0}.inventorycategories set " +
                " Flag1=@Flag1,DateCreated=@DateCreated ";

            string command = string.Format(qryTaxChageAll, BranchAlias);


            MySqlParameter[] parameters =
                    {
                     new MySqlParameter("@Flag1",Flag1val),
                     new MySqlParameter("@DateCreated",Curr_DateOnly),
                    };

            K = ObjCloudMain.ExecuteQuery(command, parameters);

            // N  K = ObjCloudMain.ExecuteNonQuery(qryTaxChageAll);  Existing code 

            if (K > 0)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Tax Value Updated To All Category');location.href='CategoryMaster.aspx '", true);
            }
        }

        public void UpdateTaxValueCustom(string Flag1val, string CateId)
        {

            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;
            }
            // Prevents SQL Injection 
            // Parameterized Approach 

            string qryTaxChageCutom = "update  {0}.inventorycategories set " +
                " Flag1=@Flag1,DateCreated=@DateCreated where  CategoryID=@CategoryID ";
            string command = string.Format(qryTaxChageCutom, BranchAlias);

            MySqlParameter[] parameters =
                   {
                     new MySqlParameter("@Flag1",Flag1val),
                     new MySqlParameter("@DateCreated",Curr_DateOnly),
                     new MySqlParameter("@CategoryID",CateId),
                   };

            K = ObjMain.ExecuteQuery(command, parameters);

            // N   K = ObjCloudMain.ExecuteNonQuery(qryTaxChageCutom);

            if (K > 0)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Tax Value Updated To Selected Category  ');location.href='CategoryMaster.aspx '", true);
            }
        }

        protected void cmbCopytoPrinter_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbCopytoPrinter.SelectedItem.Text == "AllCategory")
            {
                grdPrinter.Visible = false;
                lblMessage.Text = " The above Printer setting to be Applied All Categories.";
            }
            else if (cmbCopytoPrinter.SelectedItem.Text == "Custom")
            {
                grdPrinter.Visible = true;
                LoadCateCopyPrinter();
                lblMessage.Text = " The above Printer setting to be Applied to the  Selected Categories";
                LoadCateCopyPrinter();
            }
            else if (cmbCopytoPrinter.SelectedItem.Text == "Select")
            {
                grdPrinter.Visible = false;
                lblMessage.Text = string.Empty;
            }
            else
            {

            }

        }
        public void LoadCateCopyPrinter()
        {
            // XSS Attack Prevention 

            if (cmbBranches.Visible == false)
            {

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

            }
            else
                grdPrinter.DataBind();
            {
                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;
            }


            DtCate_Printer = ObjSel.GetCategoryList(BranchAlias);
            grdPrinter.DataSource = DtCate_Printer;
        }

        protected void btnCopy2Printer_Click(object sender, EventArgs e)
        {


            if (cmbCopytoPrinter.SelectedItem.Text == "AllCategory")
            {
                if (!string.IsNullOrEmpty(Session["RFlag2_Printer"] as string) && txtCategoryName.Text != "" && txtCategoryDescription.Text != "")
                {

                    ObjPL.Flag2 = Session["RFlag2_Printer"].ToString();
                    UpdatePrinterVal(ObjPL.Flag2);                                          // Update All Category 

                }
                else
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' You Must Select Category For Printer Setting')", true);
                }


            }
            else if (cmbCopytoPrinter.SelectedItem.Text == "Custom")
            {

                if (!string.IsNullOrEmpty(Session["RFlag2_Printer"] as string) && txtCategoryName.Text != "" && txtCategoryDescription.Text != "")
                {
                    grdCopyTax.Visible = true;

                    ObjPL.Flag2 = Session["RFlag2_Printer"].ToString();
                    string str = string.Empty;
                    string strname = string.Empty;
                    foreach (GridViewRow gvrow in grdPrinter.Rows)
                    {
                        var checkbox = gvrow.FindControl("chkSelect") as CheckBox;
                        if (checkbox.Checked)
                        {
                            Label lblCategoryIdd = gvrow.FindControl("lblCategoryNo1") as Label;
                            CateValue = lblCategoryIdd.Text;
                            UpdatePrinterValCustom(ObjPL.Flag2, CateValue);     //  Update Tax Value : Custom
                        }


                    }


                }
                else
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' You Must Select Category For Tax Setting')", true);

                }

            }



        }


        public void UpdatePrinterVal(string Flag2val)
        {

            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 qryPrinterChageAll = "update  {0}.inventorycategories set Flag2=@Flag2,DateCreated=@DateCreated ";

            string command = string.Format(qryPrinterChageAll, BranchAlias);

            MySqlParameter[] parameters =
                   {
                     new MySqlParameter("@Flag2",Flag2val),
                     new MySqlParameter("@DateCreated",Curr_DateOnly),
                  // new MySqlParameter("@CategoryID",CateId),
                   };

            K = ObjMain.ExecuteQuery(command, parameters);
            // K = ObjCloudMain.ExecuteNonQuery(qryPrinterChageAll);  Exiting Code 

            if (K > 0)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Printer  Value Updated To All Category  ');location.href='CategoryMaster.aspx '", true);
            }



        }
        /// <summary>
        ///  This Function For Update Printer Settign Value: Cutom Selection 
        /// </summary>
        /// <param name="Flag2val"></param>
        /// <param name="CateId"></param>
        public void UpdatePrinterValCustom(string Flag2val, string CateId)
        {

            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;
            }


            // Prevents SQL Injection 
            // Parameterized Approach 

            string qryPrinterChageCutom = "update  {0}.inventorycategories set " +
                                          "Flag2=@Flag2,DateCreated=@DateCreated where  CategoryID=@CategoryID ";

            string command = string.Format(qryPrinterChageCutom, BranchAlias);

            MySqlParameter[] parameters =
                   {
                     new MySqlParameter("@Flag2",Flag2val),
                     new MySqlParameter("@DateCreated",Curr_DateOnly),
                     new MySqlParameter("@CategoryID",CateId),
                   };

            K = ObjMain.ExecuteQuery(command, parameters);



            // N   K = ObjCloudMain.ExecuteNonQuery(qryPrinterChageCutom);   Exiting Code 

            if (K > 0)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Printer Value Updated To Selected Category  ');location.href='CategoryMaster.aspx '", true);
            }
        }

        protected void btnlnkPDF_Click(object sender, EventArgs e)
        {
            ReadPdfFile();
        }
        private void ReadPdfFile()
        {
            //string path = @"C:\Swift3D.pdf";
            //WebClient client = new WebClient();
            //Byte[] buffer = client.DownloadData(path);

            //if (buffer != null)
            //{
            //    Response.ContentType = "application/pdf";
            //    Response.AddHeader("content-length", buffer.Length.ToString());
            //    Response.BinaryWrite(buffer);
            //}

        }


        public void LoadPrinterValue()
        {
            string Printer_Query = "select id,PrinterNo,PrinterName,Visible  from  {0}.PrinterLable;";

            string command = string.Format(Printer_Query, BranchAlias);
            DTPrinterList = ObjCloudMain.GetData_DT(command);

            if (DTPrinterList.Rows.Count > 0)
            {
                // Load 19th 

                RPrintno19 = DTPrinterList.Rows[0]["Visible"].ToString();
                PrinterName19 = DTPrinterList.Rows[0]["PrinterName"].ToString();
                lblLPrinter19.Text = PrinterName19;  // Assign to  Lable

                if (RPrintno19.Equals("0"))
                {
                    chkRP19.Visible = false;
                    lblLPrinter19.Visible = false;
                }
                else
                {
                    chkRP19.Visible = true;
                    lblLPrinter19.Visible = true;
                }

                // Load 18th
                RPrintno18 = DTPrinterList.Rows[1]["Visible"].ToString();
                PrinterName18 = DTPrinterList.Rows[1]["PrinterName"].ToString();
                lblLPrinter18.Text = PrinterName18;  // Assign to  Lable

                if (RPrintno18.Equals("0"))
                {
                    chkRP18.Visible = false;
                    lblLPrinter18.Visible = false;
                }
                else
                {
                    chkRP18.Visible = true;
                    lblLPrinter18.Visible = true;

                }


                // Load 17th 
                RPrintno17 = DTPrinterList.Rows[2]["Visible"].ToString();
                PrinterName17 = DTPrinterList.Rows[2]["PrinterName"].ToString();
                lblLPrinter17.Text = PrinterName17;  // Assign to  Lable

                if (RPrintno17.Equals("0"))
                {
                    chkRP17.Visible = false;
                    lblLPrinter17.Visible = false;
                }
                else
                {
                    chkRP17.Visible = true;
                    lblLPrinter17.Visible = true;
                }



                // Load 16th 
                RPrintno16 = DTPrinterList.Rows[3]["Visible"].ToString();
                PrinterName16 = DTPrinterList.Rows[3]["PrinterName"].ToString();
                lblLPrinter16.Text = PrinterName16;  // Assign to  Lable
                if (RPrintno16.Equals("0"))
                {
                    chkRP16.Visible = false;
                    lblLPrinter16.Visible = false;

                }
                else
                {
                    chkRP16.Visible = true;
                    lblLPrinter16.Visible = true;

                }
                // Load 15th 
                RPrintno15 = DTPrinterList.Rows[4]["Visible"].ToString();
                PrinterName15 = DTPrinterList.Rows[4]["PrinterName"].ToString();
                lblLPrinter15.Text = PrinterName15;  // Assign to  Lable
                if (RPrintno15.Equals("0"))
                {
                    chkRP15.Visible = false;
                    lblLPrinter15.Visible = false;

                }
                else
                {
                    chkRP15.Visible = true;
                    lblLPrinter15.Visible = true;
                }
                // Load 14th
                RPrintno14 = DTPrinterList.Rows[5]["Visible"].ToString();
                PrinterName14 = DTPrinterList.Rows[5]["PrinterName"].ToString();
                lblLPrinter14.Text = PrinterName14;  // Assign to  Lable
                if (RPrintno14.Equals("0"))
                {
                    chkRP14.Visible = false;
                    lblLPrinter14.Visible = false;
                }
                else
                {
                    chkRP14.Visible = true;
                    lblLPrinter14.Visible = true;
                }
                // Load 13th
                RPrintno13 = DTPrinterList.Rows[6]["Visible"].ToString();
                PrinterName13 = DTPrinterList.Rows[6]["PrinterName"].ToString();
                lblLPrinter13.Text = PrinterName13;  // Assign to  Lable
                if (RPrintno13.Equals("0"))
                {
                    chkRP13.Visible = false;
                    lblLPrinter13.Visible = false;
                }
                else
                {
                    chkRP13.Visible = true;
                    lblLPrinter13.Visible = true;
                }

                // Load 12th
                RPrintno12 = DTPrinterList.Rows[7]["Visible"].ToString();
                PrinterName12 = DTPrinterList.Rows[7]["PrinterName"].ToString();
                lblLPrinter12.Text = PrinterName12;  // Assign to  Lable
                if (RPrintno12.Equals("0"))
                {
                    chkRP12.Visible = false;
                    lblLPrinter12.Visible = false;
                }
                else
                {
                    chkRP12.Visible = true;
                    lblLPrinter12.Visible = true;
                }



                // Load 11th
                RPrintno11 = DTPrinterList.Rows[8]["Visible"].ToString();
                PrinterName11 = DTPrinterList.Rows[8]["PrinterName"].ToString();
                lblLPrinter11.Text = PrinterName11;  // Assign to  Lable
                if (RPrintno11.Equals("0"))
                {
                    chkRP11.Visible = false;
                    lblLPrinter11.Visible = false;
                }
                else
                {
                    chkRP11.Visible = true;
                    lblLPrinter11.Visible = true;
                }


                // Load 10th 
                RPrintno10 = DTPrinterList.Rows[9]["Visible"].ToString();
                PrinterName10 = DTPrinterList.Rows[9]["PrinterName"].ToString();
                lblLPrinter10.Text = PrinterName10;  // Assign to  Lable
                if (RPrintno10.Equals("0"))
                {
                    chkRP10.Visible = false;
                    lblLPrinter10.Visible = false;
                }
                else
                {
                    chkRP10.Visible = true;
                    lblLPrinter10.Visible = true;
                }

                // Load 9th 
                RPrintno9 = DTPrinterList.Rows[10]["Visible"].ToString();
                PrinterName9 = DTPrinterList.Rows[10]["PrinterName"].ToString();
                lblLPrinter9.Text = PrinterName9;  // Assign to  Lable
                if (RPrintno9.Equals("0"))
                {
                    chkRP9.Visible = false;
                    lblLPrinter9.Visible = false;
                }
                else
                {
                    chkRP9.Visible = true;
                    lblLPrinter9.Visible = true;
                }

                // Load 8th 
                RPrintno8 = DTPrinterList.Rows[11]["Visible"].ToString();
                PrinterName8 = DTPrinterList.Rows[11]["PrinterName"].ToString();
                lblLPrinter8.Text = PrinterName8;  // Assign to  Lable
                if (RPrintno8.Equals("0"))
                {
                    chkRP8.Visible = false;
                    lblLPrinter8.Visible = false;
                }
                else
                {
                    chkRP8.Visible = true;
                    lblLPrinter8.Visible = true;
                }

                // Load 7th 
                RPrintno7 = DTPrinterList.Rows[12]["Visible"].ToString();
                PrinterName7 = DTPrinterList.Rows[12]["PrinterName"].ToString();
                lblLPrinter7.Text = PrinterName7;  // Assign to  Lable
                if (RPrintno7.Equals("0"))
                {
                    chkRP7.Visible = false;
                    lblLPrinter7.Visible = false;
                }
                else
                {
                    chkRP7.Visible = true;
                    lblLPrinter7.Visible = true;

                }


                // Load 6th 
                RPrintno6 = DTPrinterList.Rows[13]["Visible"].ToString();
                PrinterName6 = DTPrinterList.Rows[13]["PrinterName"].ToString();
                lblLPrinter6.Text = PrinterName6;  // Assign to  Lable
                if (RPrintno6.Equals("0"))
                {
                    chkRP6.Visible = false;
                    lblLPrinter6.Visible = false;
                }
                else
                {
                    chkRP6.Visible = true;
                    lblLPrinter6.Visible = true;

                }

                // Load 5th 
                RPrintno5 = DTPrinterList.Rows[14]["Visible"].ToString();
                PrinterName5 = DTPrinterList.Rows[14]["PrinterName"].ToString();
                lblLPrinter5.Text = PrinterName5;  // Assign to  Lable
                if (RPrintno5.Equals("0"))
                {
                    chkRP5.Visible = false;
                    lblLPrinter5.Visible = false;
                }
                else
                {

                    chkRP5.Visible = true;
                    lblLPrinter5.Visible = true;
                }
                // Load 4th 
                RPrintno4 = DTPrinterList.Rows[15]["Visible"].ToString();
                PrinterName4 = DTPrinterList.Rows[15]["PrinterName"].ToString();
                lblLPrinter4.Text = PrinterName4;  // Assign to  Lable
                if (RPrintno4.Equals("0"))
                {
                    chkRP4.Visible = false;
                    lblLPrinter4.Visible = false;

                }
                else
                {
                    chkRP4.Visible = true;
                    lblLPrinter4.Visible = true;

                }

                // Load 3rd 
                RPrintno3 = DTPrinterList.Rows[16]["Visible"].ToString();
                PrinterName3 = DTPrinterList.Rows[16]["PrinterName"].ToString();
                lblLPrinter3.Text = PrinterName3;  // Assign to  Lable
                if (RPrintno3.Equals("0"))
                {
                    chkRP3.Visible = false;
                    lblLPrinter3.Visible = false;

                }
                else
                {
                    chkRP3.Visible = true;
                    lblLPrinter3.Visible = true;
                }

                // Load 2nd 
                RPrintno2 = DTPrinterList.Rows[17]["Visible"].ToString();
                PrinterName2 = DTPrinterList.Rows[17]["PrinterName"].ToString();
                lblLPrinter2.Text = PrinterName2;  // Assign to  Lable

                if (RPrintno2.Equals("0"))
                {
                    chkRP2.Visible = false;
                    lblLPrinter2.Visible = false;
                }
                else
                {
                    chkRP2.Visible = true;
                    lblLPrinter2.Visible = true;

                }

                // Load 1st
                RPrintno1 = DTPrinterList.Rows[18]["Visible"].ToString();
                PrinterName1 = DTPrinterList.Rows[18]["PrinterName"].ToString();
                lblLPrinter1.Text = PrinterName1;  // Assign to  Lable

                if (RPrintno1.Equals("0"))
                {
                    chkRP1.Visible = false;
                    lblLPrinter1.Visible = false;

                }
                else
                {
                    chkRP1.Visible = true;
                    lblLPrinter1.Visible = true;

                }




            }

        }


        public string GenericTax(int taxid)
        {
            string TaxName = string.Empty;

            #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

            string Query_Tax = "Select TaxName  from   {0}.tax  where Taxid=@Taxid ;";
            string command = string.Format(Query_Tax, BranchAlias);

            MySqlParameter[] parameters =
                   {
                     new MySqlParameter("@Taxid",taxid),
                   };

            TaxName = ObjMain.NewExecuteScalar(command, parameters);

            return TaxName;
        }



        /// <summary>
        ///  This Funcation to load the Lable from the TaxMaster 
        /// </summary>
        public void LoadTaxLables()
        {
            lblATax6.Text = GenericTax(6);
            lblATax5.Text = GenericTax(5);
            lblATax4.Text = GenericTax(4);
            lblATax3.Text = GenericTax(3);
            lblATax2.Text = GenericTax(2);
            lblATax1.Text = GenericTax(1);
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            ClearForm();
            Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
        }



        #region "Navision Import Inventory Information"
        protected void btnNavImport_Click(object sender, EventArgs e)
        {
            try
            {

                //for getting AccessToken
                string nAccessToken = objAPI.Nav_GetBarerTokenAsync().ToString();
                Console.WriteLine(nAccessToken);

                //for Getting Brand Information
                var sBrandData = objAPI.Nav_GET_Brand(nAccessToken);
                Console.WriteLine(sBrandData);
                //var cNavBrand  =  JsonConvert.DeserializeObject<Brand>((string)sBrandData);
                Brand valueslist = Newtonsoft.Json.JsonConvert.DeserializeObject<Brand>(sBrandData);
                foreach (var Valuesitems in valueslist.Value)
                {

                    string V0 = Valuesitems.odataetag.ToString();
                    //string V1 = Valuesitems.dimensionCode.ToString();         //Nav removed this field because field not available in live credentials on 16-Nov-2023
                    string V2 = Valuesitems.dimValue_Code.ToString();
                    string V3 = Valuesitems.dimValue_Name.ToString();
                    BrandInsertUpdate(V2, V3);
                }

                //for Getting Inventory Items Information
                var sItemData = objAPI.Nav_GET_Item(nAccessToken);
                Console.WriteLine(sItemData);
                Item valuesItemlist = Newtonsoft.Json.JsonConvert.DeserializeObject<Item>(sItemData);
                foreach (var Valuesitems in valuesItemlist.value)
                {

                    string I0 = Valuesitems.odataetag.ToString();
                    string I1 = Valuesitems.item_CrossReferenceNo.ToString();               //Barcode
                    string I2 = Valuesitems.item_SuntoyoInventoryName.ToString();           //Inventory Name
                    string I3 = Valuesitems.item_Description.ToString();                    //InvDescription
                    string I4 = Valuesitems.item_GlobalDim2Code.ToString();                 //CategoryID
                    string I5 = Valuesitems.item_SalesPriceAmount.ToString();               //Price
                    string I6 = Valuesitems.item_BaseUnitOfMeasure.ToString();              //UOM
                    string I7 = Valuesitems.item_No.ToString();                             //StockCode
                    ItemInsertUpdate(I1, I2, I3, I4, I5, I6, I7);
                }


                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert(' Data Import completed Successfully!  ');", true);
                //Response.Write("Done");
            }
            catch (Exception)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Data Importing Failed!  ');location.href='CategoryMaster.aspx '", true);
            }
        }

        #region Navision Brand Insert/Update
        //nid added for Insert/Update Categories table for AUD-Navision Integration on 12-Oct-2023
        public void BrandInsertUpdate(string sCatName, string sCatDescription)
        {
            try
            {
                int nCategoryID = 0;
                DataTable DTRunningNo = new DataTable();
                BranchAlias = Session["Branch_HQ"].ToString();

                // Prevents SQL Injection 
                // Parameterized Approach 

                //For Checking Category exist or not
                string sCheckCat = "Select CategoryName From  {0}.inventorycategories Where CategoryName=@CategoryName ";
                string command = string.Format(sCheckCat, BranchAlias);
                MySqlParameter[] parameters =
                    {
                    new MySqlParameter("@CategoryName",sCatName),
                };
                DTCatExist = ObjMain.NewGetData_DT(command, parameters);
                if (DTCatExist.Rows.Count == 0)
                {
                    //for Insert New Records
                    //for getting Running Number
                    string sGetRunningNo = "Select RunningNo From {0}.RunningNumber Where ModuleName=@ModuleName ";
                    command = string.Format(sGetRunningNo, BranchAlias);
                    MySqlParameter[] parameters1 =
                        {
                        new MySqlParameter("@ModuleName","InventoryCategory"),
                    };
                    DTRunningNo = ObjMain.NewGetData_DT(command, parameters1);
                    if (DTRunningNo.Rows.Count > 0)
                    {
                        nCategoryID = (int)DTRunningNo.Rows[0]["RunningNo"];
                        nCategoryID = nCategoryID + 1;
                    }
                    else
                    {
                        nCategoryID = 1;
                    }

                    //for insert into InventoryCategories table
                    string sInsertCategory = "Insert INTO {0}.InventoryCategories(CategoryID, CategoryNumber, CategoryName, Description,DateCreated, Active,Flag1) Values( " +
                         " @CategoryID,@CategoryNumber,@CategoryName,@Description,@DateCreated,@Active,@Flag1 )";
                    command = string.Format(sInsertCategory, BranchAlias);
                    MySqlParameter[] parameters2 =
                     {
                         new MySqlParameter("@CategoryID",nCategoryID),
                         new MySqlParameter("@CategoryNumber",nCategoryID),
                         new MySqlParameter("@CategoryName",sCatName),
                         new MySqlParameter("@Description",sCatDescription),
                         new MySqlParameter("@DateCreated",Curr_DateOnly),
                         new MySqlParameter("@Active",-1),
                         new MySqlParameter("@Flag1","00000010"),
                     };
                    K = ObjMain.ExecuteQuery(command, parameters2);

                    //for updating RunningNo 
                    string sUpdateRunningNo = "Update {0}.RunningNumber SET RunningNo=@RunningNo Where ModuleName=@ModuleName ";
                    command = string.Format(sUpdateRunningNo, BranchAlias);
                    MySqlParameter[] parameters3 =
                     {
                         new MySqlParameter("@RunningNo",nCategoryID),
                         new MySqlParameter("@ModuleName","InventoryCategory"),
                     };
                    K = ObjMain.ExecuteQuery(command, parameters3);

                }
                else
                {
                    //for Update Existing Records
                    string sUpdateCategory = "Update {0}.InventoryCategories Set Description=@Description Where CategoryName=@CategoryName ";
                    command = string.Format(sUpdateCategory, BranchAlias);
                    MySqlParameter[] parameters4 =
                    {
                        new MySqlParameter("@CategoryName",sCatName),
                        new MySqlParameter("@Description",sCatDescription),
                    };
                    K = ObjMain.ExecuteQuery(command, parameters4);

                }
            }
            catch (Exception)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Data Importing (Category) process Failed!  ');location.href='CategoryMaster.aspx '", true);
            }
        }
        #endregion

        #region Navision Inventory Item Insert/Update
        //nid added for Insert/Update Inventory Items table for AUD-Navision Integration on 13-Oct-2023
        public void ItemInsertUpdate(string sBarcode, string sInventoryName, string sDescription, string sCategoryID, string sPrice, string sUnit, string sStockCode)
        {
            try
            {
                int nInventoryID = 0;
                int nCategoryID = 0;
                DataTable DTInvExist = new DataTable();
                DataTable DTRunningNo = new DataTable();
                DataTable DTCatID = new DataTable();
                BranchAlias = Session["Branch_HQ"].ToString();

                //For Checking Inventory existance 
                string sCheckInv = "Select Barcode From  {0}.inventory Where Barcode=@Barcode ";
                string command = string.Format(sCheckInv, BranchAlias);
                MySqlParameter[] parameters =
                {
                    new MySqlParameter("@Barcode",sBarcode ),
                };
                DTInvExist = ObjMain.NewGetData_DT(command, parameters);
                if (DTInvExist.Rows.Count == 0)
                {
                    //for Insert New Records
                    //for getting Running Number
                    string sGetRunningNo = "Select RunningNo From {0}.RunningNumber Where ModuleName=@ModuleName ";
                    command = string.Format(sGetRunningNo, BranchAlias);
                    MySqlParameter[] parameters1 =
                    {
                        new MySqlParameter("@ModuleName","Inventory"),
                    };
                    DTRunningNo = ObjMain.NewGetData_DT(command, parameters1);
                    if (DTRunningNo.Rows.Count > 0)
                    {
                        nInventoryID = (int)DTRunningNo.Rows[0]["RunningNo"];
                        nInventoryID = nInventoryID + 1;
                    }
                    else
                    {
                        nInventoryID = 1;
                    }

                    //for getting CategoryID
                    string sGetCatID = "Select CategoryID From {0}.InventoryCategories Where CategoryName=@CategoryName ";
                    command = string.Format(sGetCatID, BranchAlias);
                    MySqlParameter[] parameters2 =
                    {
                        new MySqlParameter("@CategoryName",sCategoryID ),
                    };
                    DTCatID = ObjMain.NewGetData_DT(command, parameters2);
                    if (DTCatID.Rows.Count > 0)
                    {
                        nCategoryID = (int)DTCatID.Rows[0]["CategoryID"];
                    }
                    else
                    {
                        nCategoryID = 1;
                    }

                    //for insert into Inventory table
                    string sInsertInventory = "Insert INTO {0}.Inventory(InventoryID, BarCode, InventoryName, Description, StockCode, CategoryID, Unit, Price,Price2, Price3,Price4,DateCreated, Group1, Group2, Group3, Group4, Group5, NoPriceShift, Active, Notes ) Values( " +
                         " @InventoryID, @BarCode, @InventoryName, @Description, @StockCode, @CategoryID, @Unit, @Price,@Price2, @Price3,@Price4,@DateCreated, @Group1, @Group2, @Group3, @Group4, @Group5,@NoPriceShift,@Active, @Notes )";
                    command = string.Format(sInsertInventory, BranchAlias);
                    MySqlParameter[] parameters3 =
                     {
                         new MySqlParameter("@InventoryID",nInventoryID),
                         new MySqlParameter("@BarCode",sBarcode ),
                         new MySqlParameter("@InventoryName",sInventoryName),
                         new MySqlParameter("@Description",sDescription),
                         new MySqlParameter("@StockCode",sStockCode),
                         new MySqlParameter("@CategoryID",nCategoryID),
                         new MySqlParameter("@Unit",sUnit),
                         new MySqlParameter("@Price",sPrice),
                         new MySqlParameter("@Price2","0.00"),
                         new MySqlParameter("@Price3","0.00"),
                         new MySqlParameter("@Price4","0.00"),
                         new MySqlParameter("@DateCreated",Curr_DateOnly),
                         new MySqlParameter("@Group1",""),
                         new MySqlParameter("@Group2",""),
                         new MySqlParameter("@Group3",""),
                         new MySqlParameter("@Group4",""),
                         new MySqlParameter("@Group5",""),
                         new MySqlParameter("@NoPriceShift","0"),
                         new MySqlParameter("@Active",-1),
                         new MySqlParameter("@Notes",""),
                     };
                    K = ObjMain.ExecuteQuery(command, parameters3);

                    //for Inserting record into InventoryCostAverage
                    string sInsertICA = "Insert INTO {0}.InventoryCostAverage (InventoryID) VALUES (@InventoryID)";
                    command = string.Format(sInsertICA, BranchAlias);
                    MySqlParameter[] parameters4 =
                    {
                         new MySqlParameter("@InventoryID",nInventoryID ),
                    };
                    K = ObjMain.ExecuteQuery(command, parameters4);

                    //for updating RunningNo 
                    string sUpdateRunningNo = "Update {0}.RunningNumber SET RunningNo=@RunningNo Where ModuleName=@ModuleName ";
                    command = string.Format(sUpdateRunningNo, BranchAlias);
                    MySqlParameter[] parameters5 =
                     {
                         new MySqlParameter("@RunningNo",nInventoryID ),
                         new MySqlParameter("@ModuleName","Inventory"),
                     };
                    K = ObjMain.ExecuteQuery(command, parameters5);
                }
                else
                {
                    //for getting CategoryID
                    string sGetCatID = "Select CategoryID From {0}.InventoryCategories Where CategoryName=@CategoryName ";
                    command = string.Format(sGetCatID, BranchAlias);
                    MySqlParameter[] parameters6 =
                    {
                        new MySqlParameter("@CategoryName",sCategoryID ),
                    };
                    DTCatID = ObjMain.NewGetData_DT(command, parameters6);
                    if (DTCatID.Rows.Count > 0)
                    {
                        nCategoryID = (int)DTCatID.Rows[0]["CategoryID"];
                    }
                    else
                    {
                        nCategoryID = 1;
                    }

                    //for Update Existing Records
                    string sUpdateInventory = "Update {0}.Inventory Set  InventoryName=@InventoryName, Description=@Description, StockCode=@StockCode, CategoryID=@CategoryID, Unit=@Unit, Price=@Price Where Barcode=@Barcode ";
                    command = string.Format(sUpdateInventory, BranchAlias);
                    MySqlParameter[] parameters7 =
                    {
                         new MySqlParameter("@BarCode",sBarcode ),
                         new MySqlParameter("@InventoryName",sInventoryName),
                         new MySqlParameter("@Description",sDescription),
                         new MySqlParameter("@StockCode",sStockCode),
                         new MySqlParameter("@CategoryID",nCategoryID),
                         new MySqlParameter("@Unit",sUnit),
                         new MySqlParameter("@Price",sPrice),
                    };
                    K = ObjMain.ExecuteQuery(command, parameters7);

                }

            }
            catch (Exception)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Data Importing (Inventory) process Failed!  ');location.href='CategoryMaster.aspx '", true);
            }
        }
        #endregion 


        #endregion



    }


}