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;
namespace SUNWEB
{
    public partial class InventoryView : System.Web.UI.Page
    {

        BL_SELECT ObjSel = new BL_SELECT();
        BLCompany ObjBL = new BLCompany();
        PLCOMMON ObjPL = new PLCOMMON();
        BL_USER ObjUserBL = new BL_USER(); //This Object for All User and Page Related fuctions included 
        PLCUSTOMER ObjCustPL = new PLCUSTOMER();
        Int32 K = 0;
        Int32 L = 0;
        Int32 U = 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 ();

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

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

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!string.IsNullOrEmpty(Session["Branch_HQ"] as string))
            {
            
                BranchAlias = Session["Branch_HQ"].ToString();
            }
            else
            {
                Response.Redirect("Login.aspx");
            }

            txtEffectiveDt.Text = Curr_DateOnly;
            if (!IsPostBack)
            {
                LoadInventoryview();
                LoadCategoriesDataList();        // Categoiest DataList
                LoadCategoryList();
                LoadCateCopyPrinter();
                // Load GroupValues -
                LoadGrp1();
                LoadGrp2();
                LoadGrp3();
                LoadGrp4();
                LoadGrp5();



                // DynamicDatatable for InventoryAdjust
                // CreateDynamicAdjustTable();

                btnUpdateInv.Visible = false;
                btnUpdate.Enabled = false;
                btnUpdate.CssClass = "btn btn-space btn-secondary";

            }
            else
            {
                ModalPopupExtender1.Show();      
            }


            if (Session["UserName"] == null)
            {
                Response.Redirect("Login.aspx");
            }


        }

        private string SortDirection
        {
            get { return ViewState["SortDirection"] != null ? ViewState["SortDirection"].ToString() : "ASC"; }
            set { ViewState["SortDirection"] = value; }
        }


        public void LoadInventoryview(string sortExpression = null)
        {
            //string InvenQuery = "select * from " + BranchAlias + ".inventory as I join  " + BranchAlias + ".inventorycategories  as c ON I.Categoryid=c.categoryid order by inventoryid;";
            string InvenQuery = "select *,sum(pquantity-(CutQuantity+RetailQty))as OnHandICA from " + BranchAlias + ".inventory as I " +
                                " join " + BranchAlias + ".inventorycategories  as c ON I.Categoryid=c.categoryid " +
                                " join " + BranchAlias + ".inventorycostaverage ic on ic.inventoryid=i.inventoryid " +
                                " group by i.inventoryid " +
                                " order by i.inventoryid;";
            DT = ObjCloudMain.GetData_DT(InvenQuery);

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

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

        protected void grdInventoryView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdInventoryView.PageIndex = e.NewPageIndex;
            LoadInventoryview();
        }

        protected void btnInventoryCreation_Click(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", "var Mleft = (screen.width/2)-(760/2);var Mtop = (screen.height/2)-(700/2);window.open( 'Inventoycreation.aspx', null, 'height=700,width=760,status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no,top=\'+Mtop+\', left=\'+Mleft+\'' );", true);

        }

        protected void txtInventoryName_TextChanged(object sender, EventArgs e)
        {

            txtDescription.Text = txtInventoryName.Text;
        }
        
        //Update button for Category popup
        protected void btnUpdate_Click(object sender, EventArgs e)
        {

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

            if ((!string.IsNullOrEmpty(Session["CTSettings"] as string)) && (!string.IsNullOrEmpty(Session["CPSettings"] as string)))
            {
                ObjPL.Flag1 = Session["CTSettings"].ToString();
                ObjPL.Flag2 = Session["CPSettings"].ToString();

                //string UQuery = "UPDATE  " + BranchAlias + ".inventorycategoriesdl  set  CategoryName='" + ObjPL.CategoryName + "',Description='" + ObjPL.Description + "' ,DateCreated='" + ObjPL.CreatedDt + "',Flag1='" + ObjPL.Flag1 + "',Flag2='" + ObjPL.Flag2 + "'  where CategoryID=" + ObjPL.CategoryId + "   ";
                string UQuery = "UPDATE  " + BranchAlias + ".inventorycategories  set  CategoryName='" + ObjPL.CategoryName + "',Description='" + ObjPL.Description + "' ,Flag1='" + ObjPL.Flag1 + "',Flag2='" + ObjPL.Flag2 + "'  where CategoryID=" + ObjPL.CategoryId + "   ";
                U = ObjCloudMain.ExecuteNonQuery(UQuery);
                if (U > 0)
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Category Updated Successfully ') ;location.href='InventoryView.aspx'", true);

                    Session["CTSettings"]=null;
                    Session["CPSettings"] = null; 
                }
                else
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Category Not Updated Successfully ') ", true);
                }


            }
            else if ((!string.IsNullOrEmpty(Session["CTSettings"] as string)))
            {

                ObjPL.Flag1 = Session["CTSettings"].ToString();
               

                //string UQuery = "UPDATE  " + BranchAlias + ".inventorycategoriesdl SET   CategoryName='" + ObjPL.CategoryName + "',Description='" + ObjPL.Description + "' ,DateCreated='" + ObjPL.CreatedDt + "',Flag1='" + ObjPL.Flag1 + "'  where CategoryID=" + ObjPL.CategoryId + "   ";
                string UQuery = "UPDATE  " + BranchAlias + ".inventorycategories SET   CategoryName='" + ObjPL.CategoryName + "',Description='" + ObjPL.Description + "' ,Flag1='" + ObjPL.Flag1 + "'  where CategoryID=" + ObjPL.CategoryId + "   ";
                U = ObjCloudMain.ExecuteNonQuery(UQuery);
                if (U > 0)
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Category Updated Successfully ');location.href='InventoryView.aspx' ", true);
                    Session["CTSettings"] = null;
                    Session["CPSettings"] = null;
                               
                }
                else
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Category Not Updated Successfully ') ", true);
                }
            
            
            }
            else if (!string.IsNullOrEmpty(Session["CPSettings"] as string))
            {
                ObjPL.Flag2 = Session["CPSettings"].ToString();

                //string UQuery = "UPDATE  " + BranchAlias + ".inventorycategoriesdl  SET  CategoryName='" + ObjPL.CategoryName + "',Description='" + ObjPL.Description + "' ,DateCreated='" + ObjPL.CreatedDt + "',Flag2='" + ObjPL.Flag2 + "'  where CategoryID=" + ObjPL.CategoryId + "   ";
                string UQuery = "UPDATE  " + BranchAlias + ".inventorycategories  SET  CategoryName='" + ObjPL.CategoryName + "',Description='" + ObjPL.Description + "' ,Flag2='" + ObjPL.Flag2 + "'  where CategoryID=" + ObjPL.CategoryId + "   ";
                U = ObjCloudMain.ExecuteNonQuery(UQuery);
                if (U > 0)
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Category Updated Successfully ') ;location.href='InventoryView.aspx'", true);
                    Session["CTSettings"] = null;
                    Session["CPSettings"] = null; 
                }
                else
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Category Not Updated Successfully ') ", true);
                }

            }
            else
            {
                //string UQuery = "UPDATE  " + BranchAlias + ".inventorycategories  SET  CategoryName='" + ObjPL.CategoryName + "',Description='" + ObjPL.Description + "' ,DateCreated='" + ObjPL.CreatedDt + "'  where CategoryID=" + ObjPL.CategoryId + "   ";
                string UQuery = "UPDATE  " + BranchAlias + ".inventorycategories  SET  CategoryName='" + ObjPL.CategoryName + "',Description='" + ObjPL.Description + "'  where CategoryID=" + ObjPL.CategoryId + "   ";
                U = ObjCloudMain.ExecuteNonQuery(UQuery);
                if (U > 0)
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Category Updated Successfully ');location.href='InventoryView.aspx' ", true);
                    Session["CTSettings"] = null;
                    Session["CPSettings"] = null; 
                }
                else
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Category Not Updated Successfully ') ", true);
                }
            
            
            }
        }
       protected void btnSubmit_Click(object sender, EventArgs e)
        {

            if (cmbCategoryName.SelectedIndex != 0)
            {

                SaveInventoryInfo();
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Inventory Created Successfully ') ", true);
                cmbCategoryName.SelectedIndex = 0;
                txtBarcode.Text = "";
                txtInventoryName.Text = "";
                txtPrice1.Text = "";
                txtPrice2.Text = "";
                txtPrice3.Text = "";
                txtDescription.Text = "";
                txtEffectiveDt.Text = "";
                mp1.Hide();
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' You must select the CategoryName ') ", true);
            }

        }
       //nid added for update inventory information on 15-May-2019
        protected void btnUpdateInv_Click(object sender, EventArgs e)
       {
           if (txtBarcode.Text == "")
           {
               ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Please enter valid Barcode ') ", true);
               return;
           }
           if (cmbCategoryName.SelectedIndex != 0)
           {

               UpdateInventoryInfo();
               ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Inventory Updated Successfully ') ", true);
               cmbCategoryName.SelectedIndex = 0;
               txtBarcode.Text = "";
               txtInventoryName.Text = "";
               txtPrice1.Text = "";
               txtPrice2.Text = "";
               txtPrice3.Text = "";
               txtDescription.Text = "";
               txtEffectiveDt.Text = "";

               LblInvIDCaption.Visible = false;
               lblInventoryID.Visible = false;
               mp1.Hide();
           }
           else
           {
               ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' You must select the CategoryName ') ", true);
           }
       }
        protected void txtCategoryNos_TextChanged(object sender, System.EventArgs e)
        {
              DTCategorydl = ObjSel.ReadCategoryByNo(BranchAlias, txtCategoryNos.Text);
              if (DTCategorydl.Rows.Count > 0)
              {

                  btnSaveCategory.Enabled = false;
                  btnSaveCategory.CssClass = "btn btn-space btn-secondary";

                  btnUpdate.Enabled = true;
                  btnUpdate.CssClass = "btn btn-space btn-secondary";
                  DTCategorydl = ObjSel.ReadCategoryByNo(BranchAlias, txtCategoryNos.Text);
                  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();
                  ModalPopupExtender1.Show();   
                  txtCategoryName.Text = RCategoryName;
                  txtCategoryDescription.Text = RCate_Descp;

                  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")
                  {
                      imgTaxSetting.BackColor = System.Drawing.Color.Green;
                      chkT.Checked = true;
                  }
                  else
                  {
                      imgTaxSetting.BackColor = System.Drawing.Color.White;
                      chkT.Checked = false;
                  }
                  // Assgin values to Checkbox : 

                  if (c0.Equals("1"))
                  {
                      chkInvandCate.Checked = true;
                  }
                  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")
                  {

                      ImgPrintsetting.BackColor = System.Drawing.Color.Green;
                      chkP.Checked = true;

                  }
                  else
                  {
                      ImgPrintsetting.BackColor = System.Drawing.Color.White;
                      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

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

                  if (P1.Equals("1"))
                  {
                      chkRP19.Checked = true;

                  }
                  else
                  {
                      chkRP19.Checked = false;
                  }
                  if (P2.Equals("1"))
                  {
                      chkRP18.Checked = true;

                  }
                  else
                  {
                      chkRP18.Checked = false;
                  }

                  if (P3.Equals("1"))
                  {
                      chkRP17.Checked = true;

                  }
                  else
                  {
                      chkRP17.Checked = false;
                  }

                  if (P4.Equals("1"))
                  {
                      chkRP16.Checked = true;

                  }
                  else
                  {
                      chkRP16.Checked = false;
                  }

                  if (P5.Equals("1"))
                  {
                      chkRP15.Checked = true;

                  }
                  else
                  {
                      chkRP15.Checked = false;
                  }


                  if (P6.Equals("1"))
                  {
                      chkRP14.Checked = true;

                  }
                  else
                  {
                      chkRP14.Checked = false;
                  }


                  if (P7.Equals("1"))
                  {
                      chkRP13.Checked = true;

                  }
                  else
                  {
                      chkRP13.Checked = false;
                  }

                  if (P8.Equals("1"))
                  {
                      chkRP12.Checked = true;

                  }
                  else
                  {
                      chkRP12.Checked = false;
                  }



                  if (P9.Equals("1"))
                  {
                      chkRP11.Checked = true;

                  }
                  else
                  {
                      chkRP11.Checked = false;
                  }

                  if (P10.Equals("1"))
                  {
                      chkRP10.Checked = true;

                  }
                  else
                  {
                      chkRP10.Checked = false;
                  }

                  if (P11.Equals("1"))
                  {
                      chkRP9.Checked = true;

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

                  }
                  else
                  {
                      chkRP8.Checked = false;
                  }
                  if (P13.Equals("1"))
                  {
                      chkRP7.Checked = true;

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

                  }
                  else
                  {
                      chkRP6.Checked = false;
                  }

                  if (P15.Equals("1"))
                  {
                      chkRP5.Checked = true;

                  }
                  else
                  {
                      chkRP5.Checked = false;
                  }

                  if (P16.Equals("1"))
                  {
                      chkRP4.Checked = true;

                  }
                  else
                  {
                      chkRP4.Checked = false;
                  }

                  if (P17.Equals("1"))
                  {
                      chkRP3.Checked = true;

                  }
                  else
                  {
                      chkRP3.Checked = false;
                  }

                  if (P18.Equals("1"))
                  {
                      chkRP2.Checked = true;

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

                  }
                  else
                  {
                      chkRP1.Checked = false;
                  }
                  ModalPopupExtender1.Show();

              }
              else
              {
                  ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Category Name Not Exist ')", true);
                  ModalPopupExtender1.Show();
              }



        }

        protected void btnCateSearch_Click(object sender, EventArgs e)
        {
            Load_SearchData(BranchAlias, txtCateSearch.Text);
        
        }
        protected void btnCopy2Tax_Click(object sender, EventArgs e)
        {

            if (cmbCopytoallTax.SelectedItem.Text == "AllCategory")
            {
                if (!string.IsNullOrEmpty(Session["RFlag1_Tax"] as string))
                {
                    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))
                {
                    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);
                
                }
                
            }

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


            if (cmbCopytoPrinter.SelectedItem.Text == "AllCategory")
            {
                if (!string.IsNullOrEmpty(Session["RFlag2_Printer"] as string))
                {
                   
                    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))
                {
                    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 UpdateTaxValueToAll(string Flag1val)
        {
            //string qryTaxChageAll = "update  " + BranchAlias + ".inventorycategoriesdl set Flag1='" + Flag1val + "',DateCreated='"+Curr_DateOnly+"' ";
            string qryTaxChageAll = "update  " + BranchAlias + ".inventorycategories set Flag1='" + Flag1val + "',DateCreated='" + Curr_DateOnly + "' ";
            //string qryTaxChageAll = "update  " + BranchAlias + ".inventorycategoriesdl set Flag1='" + Flag1val + "',DateCreated='" + Curr_DateOnly + "' where  CategoryID=" + txtCategoryNos.Text + " ";
            K = ObjCloudMain.ExecuteNonQuery(qryTaxChageAll);

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

        public void UpdateTaxValueCustom(string Flag1val,string   CateId)
        {

            //string qryTaxChageCutom = "update  " + BranchAlias + ".inventorycategoriesdl set Flag1='" + Flag1val + "',DateCreated='" + Curr_DateOnly + "' where  CategoryID=" + CateId + " ";
            string qryTaxChageCutom = "update  " + BranchAlias + ".inventorycategories set Flag1='" + Flag1val + "',DateCreated='" + Curr_DateOnly + "' where  CategoryID=" + CateId + " ";
            K = ObjCloudMain.ExecuteNonQuery(qryTaxChageCutom);

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


        public void UpdatePrinterVal(string Flag2val )
        {


            //string qryPrinterChageAll = "update  " + BranchAlias + ".inventorycategoriesdl set Flag2='" + Flag2val + "',DateCreated='" + Curr_DateOnly + "' ";
            string qryPrinterChageAll = "update  " + BranchAlias + ".inventorycategories set Flag2='" + Flag2val + "',DateCreated='" + Curr_DateOnly + "' ";
            K = ObjCloudMain.ExecuteNonQuery(qryPrinterChageAll);

            if (K > 0)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Printer  Value Updated To All Category  ')", 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)
        {

            //string qryPrinterChageCutom = "update  " + BranchAlias + ".inventorycategoriesdl set Flag2='" + Flag2val + "',DateCreated='" + Curr_DateOnly + "' where  CategoryID=" + CateId + " ";
            string qryPrinterChageCutom = "update  " + BranchAlias + ".inventorycategories set Flag2='" + Flag2val + "',DateCreated='" + Curr_DateOnly + "' where  CategoryID=" + CateId + " ";
            K = ObjCloudMain.ExecuteNonQuery(qryPrinterChageCutom);

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


        protected void cmbCopytoallTax_SelectedIndexChanged(object sender, EventArgs e)
        {
                if (cmbCopytoallTax.SelectedItem.Text == "AllCategory")
                {
                   MPECopy2AllTax.Show();
                    
                    grdCopyTax.Visible = false;
                }
                else if (cmbCopytoallTax.SelectedItem.Text == "Custom")
                {
                    MPECopy2AllTax.Show();
                    grdCopyTax.Visible = true;
                    LoadCateCopyTax();
         
                }
                else if (cmbCopytoallTax.SelectedItem.Text == "Select")
                {
                    grdCopyTax.Visible = false;
                 
                }
                else
                {

                }
           
        }

        protected void cmbCopytoPrinter_SelectedIndexChanged(object sender, EventArgs e)
        {

            MPECopy2AllPrinter.Show();

            if (cmbCopytoPrinter.SelectedItem.Text == "AllCategory")
            {
                MPECopy2AllPrinter.Show();
                grdPrinter.Visible = false;


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

                
                MPECopy2AllPrinter.Show();
                grdPrinter.Visible = true;
                LoadCateCopyPrinter();
            }
            else if (cmbCopytoPrinter.SelectedItem.Text == "Select")
            {
                grdPrinter.Visible = false;
                MPECopy2AllPrinter.Show();
            }
            else
            {

            }
            MPECopy2AllPrinter.Show();

        }

      public void Load_SearchData(string BranchNam, string SearchTxt)
        {
            //string QuerySrc = "Select CategoryID,CategoryName  from   " + BranchNam + ".inventorycategoriesdl  where  CategoryName like '" + SearchTxt + '%' + "' ;";
            string QuerySrc = "Select CategoryID,CategoryName  from   " + BranchNam + ".inventorycategories  where  CategoryName like '" + SearchTxt + '%' + "' ;";
            DtCateoryList = ObjCloudMain.GetData_DT(QuerySrc);
            ModalPopupExtender1.Show();
            if (DtCateoryList.Rows.Count > 0)
            {
                grdCategory.DataSource = DtCateoryList;
                grdCategory.DataBind();
            }
            else
            {
                LoadCategoriesDataList();
            }
       }
         protected void btnSaveCategory_Click(object sender, EventArgs e)
        {

            MySqlConnection Cnn = new MySqlConnection(ConStr_Rds);
            Cnn.Open();
            string CateQuery = "select CategoryName from " + BranchAlias + ".inventorycategories where  CategoryName  like '" + txtCategoryName.Text + "%'";
            MySqlCommand Cmd = new MySqlCommand(CateQuery, Cnn);
            CatValue = Convert.ToString(Cmd.ExecuteScalar());
            Grp1_Maxid = ObjSel.GetMax_CatID(BranchAlias);

            if (CatValue != "")
            {

            }
            else
            {
                string Active = "-1";
                string Updated = "0";
                string BranchUpdated = "0";
                string Fireindex = "0";
                //string Ins_CatQuery = "Insert into " + BranchAlias + ".inventorycategoriesdl(CategoryID,CategoryNumber,CategoryName,Description,DateCreated,Active,Updated,BranchUpdated,Flag1,Flag2,FireIndex)values(" + Grp1_Maxid + "," + Grp1_Maxid + " ,  '" + txtCategoryName.Text + "','" + txtCategoryDescription.Text + "','" + Curr_DateOnly + "'," + Active + "," + Updated + "," + BranchUpdated + ",'" + Session["CTSettings"].ToString() + "','" + Session["CPSettings"].ToString() + "','" + Fireindex + "') ";
                string Ins_CatQuery = "Insert into " + BranchAlias + ".inventorycategories(CategoryID,CategoryNumber,CategoryName,Description,DateCreated,Active,Updated,BranchUpdated,Flag1,Flag2,FireIndex)values(" + Grp1_Maxid + "," + Grp1_Maxid + " ,  '" + txtCategoryName.Text + "','" + txtCategoryDescription.Text + "','" + Curr_DateOnly + "'," + Active + "," + Updated + "," + BranchUpdated + ",'" + Session["CTSettings"].ToString() + "','" + Session["CPSettings"].ToString() + "','" + Fireindex + "') ";
                K = ObjCloudMain.ExecuteNonQuery(Ins_CatQuery);
                LoadCategoryList();
                txtCategoryName.Text = "";
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Category Name Created ')", true);
                txtCategoryName.Text = "";
                txtCategoryDescription.Text = "";
                ModalPopupExtender1.Show();
            }

        }
        protected void btncancelnew_Click(object sender, EventArgs e)
            {
                ClearInvForm();
                mp1.Hide();
            }
        protected void ClearInvForm()
        {
            txtBarcode.Text = "";
            cmbCategoryName.SelectedIndex = 0;
            cmbClass.SelectedIndex = 0; 
            txtInventoryName.Text = "";
            txtDescription.Text = "";
            cmbUnits.SelectedIndex = 0;
            txtPrice1.Text = "";
            txtPrice2.Text = "";
            txtPrice3.Text = "";
            txtPrice4.Text = "";
            txtReorder.Text = "";
            txtSafty.Text = "";
            txtMaximum.Text = "";
            txtStockCode.Text = "";
            cmbGroup1.SelectedIndex = 0;
            cmbGroup2.SelectedIndex = 0;
            cmbGroup3.SelectedIndex = 0;
            cmbGroup4.SelectedIndex = 0;
            cmbGroup5.SelectedIndex = 0; 
            txtEffectiveDt.Text = "";

            LblInvIDCaption.Visible = false;
            lblInventoryID.Visible = false;
            btnSubmit.Visible = true;
            btnUpdateInv.Visible = false;
        }

        protected void btnSave1_Click(object sender, EventArgs e)
        {

            MySqlConnection Cnn = new MySqlConnection(ConStr_Rds);
            Cnn.Open();
            string RdQuery = "select GroupName from " + BranchAlias + ".group1 where  GroupName  like '" + txtGroupName.Text + "%'";
            MySqlCommand Cmd = new MySqlCommand(RdQuery, Cnn);
            Grp1Value = Convert.ToString(Cmd.ExecuteScalar());
            Grp1_Maxid = ObjSel.GetMax_GrpID1(BranchAlias);

            if (Grp1Value != "")
            {

            }
            else
            {
                string Active = "-1";
                string Updated = "0";
                string BranchUpdated = "0";
                //string Ins_Query = "Insert into " + BranchAlias + ".group1dl(GroupID,GroupName,Description,DateCreated,Active,Updated,BranchUpdated)values(" + Grp1_Maxid + ",'" + txtGroupName.Text + "','" + txtDescription.Text + "','" + Curr_DateOnly + "'," + Active + "," + Updated + "," + BranchUpdated + ") ";
                string Ins_Query = "Insert into " + BranchAlias + ".group1(GroupID,GroupName,Description,DateCreated,Active,Updated,BranchUpdated)values(" + Grp1_Maxid + ",'" + txtGroupName.Text + "','" + txtDescription.Text + "','" + Curr_DateOnly + "'," + Active + "," + Updated + "," + BranchUpdated + ") ";
                K = ObjCloudMain.ExecuteNonQuery(Ins_Query);
                LoadGrp1();
                txtGroupName.Text = "";
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Group Name Created ')", true);
            }

        }

        protected void btnSave2_Click(object sender, EventArgs e)
        {

            MySqlConnection Cnn = new MySqlConnection(ConStr_Rds);
            Cnn.Open();
            string RdQuery1 = "select GroupName from " + BranchAlias + ".group2 where  GroupName  like '" + txtGroupName1 + "%'";
            MySqlCommand Cmd = new MySqlCommand(RdQuery1, Cnn);
            Grp2Value = Convert.ToString(Cmd.ExecuteScalar());
            Grp2_Maxid = ObjSel.GetMax_GrpID2(BranchAlias);

            if (Grp2Value != "")
            {

            }
            else
            {
                string Active = "-1";
                string Updated = "0";
                string BranchUpdated = "0";
                //string Ins_Query1 = "Insert into " + BranchAlias + ".group2dl(GroupID,GroupName,Description,DateCreated,Active,Updated,BranchUpdated)values(" + Grp2_Maxid + ",'" + txtGroupName1.Text + "','" + txtDescription.Text + "','" + Curr_DateOnly + "'," + Active + "," + Updated + "," + BranchUpdated + ") ";
                string Ins_Query1 = "Insert into " + BranchAlias + ".group2(GroupID,GroupName,Description,DateCreated,Active,Updated,BranchUpdated)values(" + Grp2_Maxid + ",'" + txtGroupName1.Text + "','" + txtDescription.Text + "','" + Curr_DateOnly + "'," + Active + "," + Updated + "," + BranchUpdated + ") ";
                K = ObjCloudMain.ExecuteNonQuery(Ins_Query1);
                LoadGrp2();
                txtGroupName1.Text = "";
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Group Name Created ')", true);

            }
        }

        // Category Tax Settings 
        protected void btnSaveTax_Click(object sender, EventArgs e)
        {
            Flag1Value();
            ResetCheckboxCT();
        }
        // Category Printer Settings 
        protected void btnSavePrinter_Click(object sender, EventArgs e)
        {
            Flag2Value();
            ResetCheckboxCP();
        }
        // Inventory Printer Settings
        protected void btnSavePrinter2_Click(object sender, EventArgs e)
        {
             Flag2Value2();
             PSett = Session["IPrinterSetting"].ToString();

             Label1.Text = Session["IPrinterSetting"].ToString();
             ResetCheckboxIP();

        }
        // Inventory Taxi Settings 

        protected void btnSaveTax2_Click(object sender, EventArgs e)
        {
            TaxValue2();
            ResetCheckboxIT();

         }
        
       
         /// <summary>
         ///  This funcation for Inventory Tax Settings 
         /// </summary>
        public void TaxValue2()
        {
            string[] Valuu = new string[8];

            if (chk2InventoryCate.Checked == true)
            {
                Valuu[0] = "1";
            }
            else
            {
                Valuu[0] = "0";
            }
            if (chk2Always.Checked == true)
            {
                Valuu[1] = "1";
            }
            else
            {
                Valuu[1] = "0";
            }

            if (chk2TaxRate6.Checked == true)
            {
                Valuu[2] = "1";

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

            if (chk2TaxRate5.Checked == true)
            {
                Valuu[3] = "1";
            }
            else
            {
                Valuu[3] = "0";
            }
            if (chk2TaxRate4.Checked == true)
            {
                Valuu[4] = "1";
            }
            else
            {
                Valuu[4] = "0";
            }

            if (chk2TaxRate3.Checked == true)
            {
                Valuu[5] = "1";
            }
            else
            {
                Valuu[5] = "0";
            }
            if (chk2scvc.Checked == true)
            {
                Valuu[6] = "1";
            }
            else
            {
                Valuu[6] = "0";
            }
            if (chk2GST.Checked == true)
            {
                Valuu[7] = "1";

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


            StringBuilder Build = new StringBuilder();
            foreach (string valuess in Valuu)
            {
                Build.Append(valuess);
            }
            mp1.Show();
            hidenInventoryTax.Value = Convert.ToString(Build);  // Inventory TaxSetting Value

            Session["ITaxSettings"] = hidenInventoryTax.Value;
            Label1.Text = Session["ITaxSettings"].ToString();
        }

        /// <summary>
        ///  This function for Category Tax Setting
        /// </summary>
       public  void Flag1Value()
        {
            string[] Val = new string[8];

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

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

            hideCategoryTax.Value = Convert.ToString(Build);
            ModalPopupExtender1.Show();
            Session["CTSettings"] = Build.ToString();
            Label1.Text = Session["CTSettings"].ToString();
           
           
        }

        /// <summary>
        ///  This Function For Category Printer setting 
        /// </summary>
       public void Flag2Value()
       {
           string[] Printer = new string[20];

           if (chkPrinterInven.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);
           }


           ModalPopupExtender1.Show();

           Session["CPSettings"] = BuildValue.ToString();
           Label1.Text = Session["CPSettings"].ToString();
         
       }


        /// <summary>
       /// This function for Inventory Printer Settings 
        /// </summary>
        public void Flag2Value2()
       {
           string[] Printernew = new string[20];

           if (chk2Invoice2.Checked == true)
           {
               Printernew[0] = "1";

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

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

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

           if (chk2RP17.Checked == true)
           {
               Printernew[3] = "1";
           }
           else
           {
               Printernew[3] = "0";
           }
           if (chk2RP16.Checked == true)
           {
               Printernew[4] = "1";
           }
           else
           {
               Printernew[4] = "0";
           }

           if (chk2RP15.Checked == true)
           {
               Printernew[5] = "1";
           }
           else
           {
               Printernew[5] = "0";
           }
           if (chk2RP14.Checked == true)
           {
               Printernew[6] = "1";
           }
           else
           {
               Printernew[6] = "0";
           }
           if (chk2RP13.Checked == true)
           {
               Printernew[7] = "1";

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

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


           if (chk2RP11.Checked == true)
           {
               Printernew[9] = "1";

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

           if (chk2RP10.Checked == true)
           {
               Printernew[10] = "1";

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

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

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

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

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

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

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

           if (chk2RP4.Checked == true)
           {
               Printernew[16] = "1";

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


           if (chk2RP3.Checked == true)
           {
               Printernew[17] = "1";

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


           if (chk2RP2.Checked == true)
           {
               Printernew[18] = "1";

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

           if (chk2RP1.Checked == true)
           {
               Printernew[19] = "1";

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

           StringBuilder BuildVals = new StringBuilder();
           foreach (string vals in Printernew)
           {
               BuildVals.Append(vals);
           }

           HideInvenPrinter.Value = Convert.ToString(BuildVals);
           Session["IPrinterSetting"] = HideInvenPrinter.Value;

        
           Label1.Text = PSett;
           mp1.Show();
          
       }

        protected void btnSave3_Click(object sender, EventArgs e)
        {

            MySqlConnection Cnn = new MySqlConnection(ConStr_Rds);
            Cnn.Open();
            string RdQuery2 = "select GroupName from " + BranchAlias + ".group3 where  GroupName  like '" + txtGroupName3.Text + "%'";
            MySqlCommand Cmd = new MySqlCommand(RdQuery2, Cnn);
            Grp3Value = Convert.ToString(Cmd.ExecuteScalar());
            Grp3_Maxid = ObjSel.GetMax_GrpID3(BranchAlias);

            if (Grp3Value != "")
            {

            }
            else
            {

                string Active = "-1";
                string Updated = "0";
                string BranchUpdated = "0";
                //string Ins_Query2 = "Insert into " + BranchAlias + ".group3dl(GroupID,GroupName,Description,DateCreated,Active,Updated,BranchUpdated)values(" + Grp3_Maxid + ",'" + txtGroupName3.Text + "','" + txtDescription.Text + "','" + Curr_DateOnly + "'," + Active + "," + Updated + "," + BranchUpdated + ") ";
                string Ins_Query2 = "Insert into " + BranchAlias + ".group3(GroupID,GroupName,Description,DateCreated,Active,Updated,BranchUpdated)values(" + Grp3_Maxid + ",'" + txtGroupName3.Text + "','" + txtDescription.Text + "','" + Curr_DateOnly + "'," + Active + "," + Updated + "," + BranchUpdated + ") ";
                K = ObjCloudMain.ExecuteNonQuery(Ins_Query2);
                LoadGrp3();
                txtGroupName3.Text = "";
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Group Name Created ')", true);

            }
        }

        protected void btnSave4_Click(object sender, EventArgs e)
        {

            MySqlConnection Cnn = new MySqlConnection(ConStr_Rds);
            Cnn.Open();
            string RdQuery3 = "select GroupName from " + BranchAlias + ".group4 where  GroupName  like '" + txtGroupName4.Text + "%'";
            MySqlCommand Cmd = new MySqlCommand(RdQuery3, Cnn);
            Grp4Value = Convert.ToString(Cmd.ExecuteScalar());
            Grp4_Maxid = ObjSel.GetMax_GrpID4(BranchAlias);

            if (Grp4Value != "")
            {

            }
            else
            {

                string Active = "-1";
                string Updated = "0";
                string BranchUpdated = "0";
                //string Ins_Query3 = "Insert into " + BranchAlias + ".group4dl(GroupID,GroupName,Description,DateCreated,Active,Updated,BranchUpdated)values(" + Grp4_Maxid + ",'" + txtGroupName4.Text + "','" + txtDescription.Text + "','" + Curr_DateOnly + "'," + Active + "," + Updated + "," + BranchUpdated + ") ";
                string Ins_Query3 = "Insert into " + BranchAlias + ".group4(GroupID,GroupName,Description,DateCreated,Active,Updated,BranchUpdated)values(" + Grp4_Maxid + ",'" + txtGroupName4.Text + "','" + txtDescription.Text + "','" + Curr_DateOnly + "'," + Active + "," + Updated + "," + BranchUpdated + ") ";
                K = ObjCloudMain.ExecuteNonQuery(Ins_Query3);
                LoadGrp4();
                txtGroupName4.Text = "";
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Group Name Created ')", true);

            }
        }

        protected void btnSave5_Click(object sender, EventArgs e)
        {

            MySqlConnection Cnn = new MySqlConnection(ConStr_Rds);
            Cnn.Open();
            string RdQuery4 = "select GroupName from " + BranchAlias + ".group5 where  GroupName  like '" + txtGroupName5.Text + "%'";
            MySqlCommand Cmd = new MySqlCommand(RdQuery4, Cnn);
            Grp5Value = Convert.ToString(Cmd.ExecuteScalar());
            Grp5_Maxid = ObjSel.GetMax_GrpID5(BranchAlias);

            if (Grp5Value != "")
            {

            }
            else
            {

                string Active = "-1";
                string Updated = "0";
                string BranchUpdated = "0";
                //string Ins_Query4 = "Insert into " + BranchAlias + ".group5dl(GroupID,GroupName,Description,DateCreated,Active,Updated,BranchUpdated)values(" + Grp5_Maxid + ",'" + txtGroupName5.Text + "','" + txtDescription.Text + "','" + Curr_DateOnly + "'," + Active + "," + Updated + "," + BranchUpdated + ") ";
                string Ins_Query4 = "Insert into " + BranchAlias + ".group5(GroupID,GroupName,Description,DateCreated,Active,Updated,BranchUpdated)values(" + Grp5_Maxid + ",'" + txtGroupName5.Text + "','" + txtDescription.Text + "','" + Curr_DateOnly + "'," + Active + "," + Updated + "," + BranchUpdated + ") ";
                K = ObjCloudMain.ExecuteNonQuery(Ins_Query4);
                LoadGrp5();
                txtGroupName5.Text = "";
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Group Name Created ')", true);
            }
        }

        public void LoadCategoryList()
        {
            cmbCategoryName.DataSource = ObjSel.Inv_CategoryList(BranchAlias);
            cmbCategoryName.DataTextField = "CategoryName";
            cmbCategoryName.DataValueField = "CategoryID";
            cmbCategoryName.DataBind();
            cmbCategoryName.Items.Insert(0, "Select Category");

        }

        public void LoadGrp1()
        {
            cmbGroup1.DataSource = ObjSel.Inv_Grp1List(BranchAlias);
            cmbGroup1.DataValueField = "Groupid";
            cmbGroup1.DataTextField = "GroupName1";
            cmbGroup1.DataBind();
            cmbGroup1.Items.Insert(0, "");

        }

        public void LoadGrp2()
        {
            cmbGroup2.DataSource = ObjSel.Inv_Grp2List(BranchAlias);
            cmbGroup2.DataValueField = "GroupName2";
            cmbGroup2.DataTextField = "Groupid";
            cmbGroup2.DataBind();
            cmbGroup2.Items.Insert(0, "");

        }
        public void LoadGrp3()
        {
            cmbGroup3.DataSource = ObjSel.Inv_Grp3List(BranchAlias);
            cmbGroup3.DataValueField = "GroupName3";
            cmbGroup3.DataTextField = "Groupid";
            cmbGroup3.DataBind();
            cmbGroup3.Items.Insert(0, "");

        }
        public void LoadGrp4()
        {
            cmbGroup4.DataSource = ObjSel.Inv_Grp4List(BranchAlias);
            cmbGroup4.DataValueField = "GroupName4";
            cmbGroup4.DataTextField = "Groupid";
            cmbGroup4.DataBind();
            cmbGroup4.Items.Insert(0, "");
        }
        public void LoadGrp5()
        {
            cmbGroup5.DataSource = ObjSel.Inv_Grp5List(BranchAlias);
            cmbGroup5.DataValueField = "GroupName5";
            cmbGroup5.DataTextField = "Groupid";
            cmbGroup5.DataBind();
            cmbGroup5.Items.Insert(0, "");
        }

        public void SaveInventoryInfo()
        {
            MySqlConnection Cnn = new MySqlConnection(ConStr);
            Cnn.Open();
            
            DTCheckBarcode = ObjSel.CheckBarcode (BranchAlias, txtBarcode.Text);
            if (DTCheckBarcode.Rows.Count != 0)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Item already existing in the system ')", true);
                ClearInvForm();
                return;
            }


            ObjPL.InventoryId = ObjSel.GetMax_RunningNo(BranchAlias);
            ObjPL.Units = cmbUnits.SelectedItem.Text;

            if (txtPrice1.Text == "")
            {
                ObjPL.Price1 = 0;
            }
            else
            {
                ObjPL.Price1 = Convert.ToDecimal(txtPrice1.Text);
            }
            if (txtPrice2.Text == "")
            {
                ObjPL.Price2 = 0;
            }
            else
            {
                ObjPL.Price2 = Convert.ToDecimal(txtPrice2.Text);
            }

            if (txtPrice3.Text == "")
            {
                ObjPL.Price3 = 0;
            }
            else
            {
                ObjPL.Price3 = Convert.ToDecimal(txtPrice3.Text);
            }

            if (txtPrice4.Text == "")
            {
                ObjPL.Price4 = 0;
            }
            else
            {
                ObjPL.Price4 = Convert.ToDecimal(txtPrice4.Text);
            }

            if (txtReorder.Text == "")
            {
                ObjPL.ReoderLevel = 0;
            }
            else
            {
                ObjPL.ReoderLevel = Convert.ToInt64(txtReorder.Text);
            }

            if (txtSafty.Text == "")
            {
                ObjPL.SaftyLevel = 0;
            }
            else
            {
                ObjPL.SaftyLevel = Convert.ToInt64(txtSafty.Text);
            }

            if (txtMaximum.Text == "")
            {
                ObjPL.MaixmumLevel = 0;
            }
            else
            {
                ObjPL.MaixmumLevel = Convert.ToInt64(txtMaximum.Text);
            }



             string Query = "insert into " + BranchAlias + ".inventory(InventoryID,BarCode,InventoryName,Description,CategoryID,Class,Unit,Price," +
          "  Price2,Price3,Price4,DateCreated,Group1,Group2,Group3,Group4,Group5,NoPriceShift,Active,Updated," +
          "  Notes,BranchUpdated,StockCode,Flag1,Flag2,ReorderLevel,MaxiumLevel,SafetyLevel)" +
          "  values (" + ObjPL.InventoryId + "  ,'" + txtBarcode.Text + "', '" + txtInventoryName.Text + "','" + txtDescription.Text + "'," + cmbCategoryName.SelectedValue + ", "+
          "  "+cmbClass.SelectedItem.Value+" , '" + ObjPL.Units + "'," + ObjPL.Price1 + "," + ObjPL.Price2 + "," + ObjPL.Price3 + "," + ObjPL.Price4 + ",'" + Curr_DateOnly + "','" + cmbGroup1.SelectedItem.Text + "','" + cmbGroup2.SelectedItem.Text + "'," +
          "  '" + cmbGroup3.SelectedItem.Text + "','" + cmbGroup4.SelectedItem.Text + "','" + cmbGroup5.SelectedItem.Text + "',0,-1,0,' ',0, '" + txtStockCode.Text + "', '" + hidenInventoryTax.Value + "','" + HideInvenPrinter.Value + "'," + ObjPL.ReoderLevel + " ," + ObjPL.SaftyLevel + "," + ObjPL.MaixmumLevel + ");";
            K = ObjCloudMain.ExecuteNonQuery(Query);
            // Insert  CostAvg Table 
            string Query1 = "INSERT INTO " + BranchAlias + ".inventorycostaverage(`InventoryID`, `PQuantity`, `PAmount`, `CutQuantity`, `CutAmount`, `UnitCost`, `RetailQty`, `RetailAmount`)" +
           "VALUES (" + ObjPL.InventoryId + ",'0.0000','0.0000','0.0000','0.0000','0.0000','0','0.0000');";
            L = ObjCloudMain.ExecuteNonQuery(Query1);
            string QueryUPT = "update " + BranchAlias + ".runningnumber set RunningNo=" + ObjPL.InventoryId + "   where ModuleName='Inventory';";
            U = ObjCloudMain.ExecuteNonQuery(QueryUPT);
        }
        
        //for update inventory information to cloud server on 21-May-2019
        public void UpdateInventoryInfo()
        {
            MySqlConnection Cnn = new MySqlConnection(ConStr);
            Cnn.Open();

            ObjPL.InventoryId = lblInventoryID.Text;
            ObjPL.Units = cmbUnits.SelectedItem.Text;

            if (txtPrice1.Text == "")
            {
                ObjPL.Price1 = 0;
            }
            else
            {
                ObjPL.Price1 = Convert.ToDecimal(txtPrice1.Text);
            }
            if (txtPrice2.Text == "")
            {
                ObjPL.Price2 = 0;
            }
            else
            {
                ObjPL.Price2 = Convert.ToDecimal(txtPrice2.Text);
            }

            if (txtPrice3.Text == "")
            {
                ObjPL.Price3 = 0;
            }
            else
            {
                ObjPL.Price3 = Convert.ToDecimal(txtPrice3.Text);
            }

            if (txtPrice4.Text == "")
            {
                ObjPL.Price4 = 0;
            }
            else
            {
                ObjPL.Price4 = Convert.ToDecimal(txtPrice4.Text);
            }

            if (txtReorder.Text == "")
            {
                ObjPL.ReoderLevel = 0;
            }
            else
            {
                ObjPL.ReoderLevel = Convert.ToInt64(txtReorder.Text);
            }

            if (txtSafty.Text == "")
            {
                ObjPL.SaftyLevel = 0;
            }
            else
            {
                ObjPL.SaftyLevel = Convert.ToInt64(txtSafty.Text);
            }

            if (txtMaximum.Text == "")
            {
                ObjPL.MaixmumLevel = 0;
            }
            else
            {
                ObjPL.MaixmumLevel = Convert.ToInt64(txtMaximum.Text);
            }
            if (hidenInventoryTax.Value == "")
            {
                TaxValue2();
            }
            if (HideInvenPrinter.Value == "")
            {
                Flag2Value2();
            }
 
            string Query = "Update " + BranchAlias + ".Inventory Set InventoryName='" + txtInventoryName.Text + "', Description='" + txtDescription.Text + "', CategoryID=" + cmbCategoryName.SelectedValue + ", " +
                " Class=" + cmbClass.SelectedItem.Value + ",Unit='" + ObjPL.Units + "',Price=" + ObjPL.Price1 + ",Price2=" + ObjPL.Price2 + ",Price3=" + ObjPL.Price3 + ", Price4=" + ObjPL.Price4 + ", Group1='" + cmbGroup1.SelectedItem.Text + "',Group2='" + cmbGroup2.SelectedItem.Text + "', " +
                " Group3='" + cmbGroup3.SelectedItem.Text + "',Group4='" + cmbGroup4.SelectedItem.Text + "',Group5='" + cmbGroup5.SelectedItem.Text + "', " +
                " StockCode='" + txtStockCode.Text + "',Flag1='" + hidenInventoryTax.Value + "',Flag2='" + HideInvenPrinter.Value + "',ReorderLevel=" + ObjPL.ReoderLevel + ",MaxiumLevel=" + ObjPL.MaixmumLevel + ",SafetyLevel=" + ObjPL.SaftyLevel + " " +
                " Where InventoryID=" + ObjPL.InventoryId + " " ;
            K = ObjCloudMain.ExecuteNonQuery(Query);

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            //string queryserch = "select * from " + BranchAlias + ".inventory where barcode='" + txtBarcodesearch.Text + "';";
            string QueryNew = "select *,sum(pquantity-(CutQuantity+RetailQty))as OnHandICA from " + BranchAlias + ".inventory as I " +
                                 " join " + BranchAlias + ".inventorycategories  as c ON I.Categoryid=c.categoryid " +
                                 " join " + BranchAlias + ".inventorycostaverage ic on ic.inventoryid=i.inventoryid where  I.barcode='" + txtBarcodesearch.Text + "'  " +
                                 " group by i.inventoryid " +
                                 " order by i.inventoryid;";

            Dt2 = ObjCloudMain.GetData_DT(QueryNew);
            grdInventoryView.DataSource = Dt2;
            grdInventoryView.DataBind();
            
        }

        protected void txtInventoryNameSearch_TextChanged(object sender, EventArgs e)
        {

           if(txtInventoryNameSearch.Text != "")
            {
                string SearchTxt = txtInventoryNameSearch.Text + "%";
               // string QueryNew = "select * from " + BranchAlias + ".inventory as I join  " + BranchAlias + ".inventorycategories  as c ON I.Categoryid=c.categoryid  where I.InventoryName like '" + SearchTxt + "' ";

                string QueryNew = "select *,sum(pquantity-(CutQuantity+RetailQty))as OnHandICA from " + BranchAlias + ".inventory as I " +
                                 " join " + BranchAlias + ".inventorycategories  as c ON I.Categoryid=c.categoryid " +
                                 " join " + BranchAlias + ".inventorycostaverage ic on ic.inventoryid=i.inventoryid where  I.InventoryName like '" + SearchTxt + "'  " +
                                 " group by i.inventoryid " +
                                 " order by i.inventoryid;";


                Dt2 = ObjCloudMain.GetData_DT(QueryNew);
                grdInventoryView.DataSource = Dt2;
                grdInventoryView.DataBind();
           }
            else
            {
                LoadInventoryview();
               
            }
        }
        protected void txtInventoryid_TextChanged(object sender, EventArgs e)
        {

            if (txtInventoryid.Text != "")
            {

               // string QueryNew = "select * from " + BranchAlias + ".inventory as I join  " + BranchAlias + ".inventorycategories  as c ON I.Categoryid=c.categoryid  where inventoryid=" + txtInventoryid.Text + "; ";

                string QueryNew = "select *,sum(pquantity-(CutQuantity+RetailQty))as OnHandICA from " + BranchAlias + ".inventory as I " +
                                " join " + BranchAlias + ".inventorycategories  as c ON I.Categoryid=c.categoryid " +
                                " join " + BranchAlias + ".inventorycostaverage ic on ic.inventoryid=i.inventoryid where  i.inventoryid=" + txtInventoryid.Text + "  " +
                                " group by i.inventoryid " +
                                " order by i.inventoryid;";


                DT = ObjCloudMain.GetData_DT(QueryNew);
                grdInventoryView.DataSource = DT;
                grdInventoryView.DataBind();
            }
            else
            {
                 LoadInventoryview();
            }
        }

        protected void OnSorting(object sender, GridViewSortEventArgs e)
        {
            this.LoadInventoryview(e.SortExpression);
        }

        public void ResetCheckboxIP()
        {
          chk2Invoice2.Checked =false;
          chk2RP19.Checked = false;
          chkRP18.Checked = false;
          chk2RP17.Checked = false;
          chk2RP16.Checked = false;
          chk2RP15.Checked = false;

          chk2RP14.Checked = false;
          chk2RP13.Checked = false;
          chk2RP12.Checked = false;
          chk2RP11.Checked = false;
          chk2RP10.Checked = false;
          chk2RP9.Checked = false;
          chkRP8.Checked = false;
          chkRP7.Checked = false;
          chk2RP6.Checked = false;
          chk2RP5.Checked = false;
          chk2RP4.Checked = false;
          chk2RP3.Checked = false;
          chk2RP2.Checked =false;
          chk2RP1.Checked = false;


        }
        public void ResetCheckboxIT()
        {
            chk2InventoryCate.Checked =false;
            chk2Always.Checked = false;
            chk2TaxRate6.Checked = false;
            chk2TaxRate5.Checked = false;
            chk2TaxRate4.Checked = false;
            chk2TaxRate3.Checked = false;
            chk2scvc.Checked = false;
            chk2GST.Checked = false;

        }


        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;
            
        }
        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;
        }
        public void LoadCategoriesDataList()
        {
            DtCateoryList = ObjSel.GetCategoryList(BranchAlias);
            grdCategory.DataSource = DtCateoryList;
            grdCategory.DataBind();
        }

        public void LoadCateCopyTax()
        {
            DtCateoryList = ObjSel.GetCategoryList(BranchAlias);
            grdCopyTax.DataSource = DtCateoryList;
            grdCopyTax.DataBind();
        }


        public void LoadCateCopyPrinter()
        {
            DtCate_Printer = ObjSel.GetCategoryList(BranchAlias);
            grdPrinter.DataSource = DtCate_Printer;
            grdPrinter.DataBind();
        }
      

        protected void grdCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
                
                btnSaveCategory.Enabled = false;
                btnSaveCategory.CssClass = "btn btn-space btn-secondary";
                btnUpdate.Enabled = true;
                btnUpdate.CssClass = "btn btn-space btn-secondary";
                RCateYNo = (grdCategory.SelectedRow.FindControl("lblCategoryNo1") as Label).Text;
                txtCategoryNos.Text = RCateYNo;
                DTCategorydl = ObjSel.ReadCategoryByNo(BranchAlias, txtCategoryNos.Text);
                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;
                ModalPopupExtender1.Show();
                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")
                {
                    imgTaxSetting.BackColor = System.Drawing.Color.Green;
                    chkT.Checked = true;
                }
                else
                {
                    imgTaxSetting.BackColor = System.Drawing.Color.White;
                    chkT.Checked = false;
                }
                // Assgin values to Checkbox : 

                if (c0.Equals("1"))
                {
                    chkInvandCate.Checked = true;
                }
                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")
                {

                    ImgPrintsetting.BackColor = System.Drawing.Color.Green;
                    chkP.Checked = true;
                }
                else
                {
                    ImgPrintsetting.BackColor = System.Drawing.Color.White;
                    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

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

                if (P1.Equals("1"))
                {
                    chkRP19.Checked = true;

                }
                else
                {
                    chkRP19.Checked = false;
                }
                if (P2.Equals("1"))
                {
                    chkRP18.Checked = true;

                }
                else
                {
                    chkRP18.Checked = false;
                }

                if (P3.Equals("1"))
                {
                    chkRP17.Checked = true;

                }
                else
                {
                    chkRP17.Checked = false;
                }

                if (P4.Equals("1"))
                {
                    chkRP16.Checked = true;

                }
                else
                {
                    chkRP16.Checked = false;
                }

                if (P5.Equals("1"))
                {
                    chkRP15.Checked = true;

                }
                else
                {
                    chkRP15.Checked = false;
                }


                if (P6.Equals("1"))
                {
                    chkRP14.Checked = true;

                }
                else
                {
                    chkRP14.Checked = false;
                }


                if (P7.Equals("1"))
                {
                    chkRP13.Checked = true;

                }
                else
                {
                    chkRP13.Checked = false;
                }

                if (P8.Equals("1"))
                {
                    chkRP12.Checked = true;

                }
                else
                {
                    chkRP12.Checked = false;
                }



                if (P9.Equals("1"))
                {
                    chkRP11.Checked = true;

                }
                else
                {
                    chkRP11.Checked = false;
                }

                if (P10.Equals("1"))
                {
                    chkRP10.Checked = true;

                }
                else
                {
                    chkRP10.Checked = false;
                }

                if (P11.Equals("1"))
                {
                    chkRP9.Checked = true;

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

                }
                else
                {
                    chkRP8.Checked = false;
                }
                if (P13.Equals("1"))
                {
                    chkRP7.Checked = true;

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

                }
                else
                {
                    chkRP6.Checked = false;
                }

                if (P15.Equals("1"))
                {
                    chkRP5.Checked = true;

                }
                else
                {
                    chkRP5.Checked = false;
                }

                if (P16.Equals("1"))
                {
                    chkRP4.Checked = true;

                }
                else
                {
                    chkRP4.Checked = false;
                }

                if (P17.Equals("1"))
                {
                    chkRP3.Checked = true;

                }
                else
                {
                    chkRP3.Checked = false;
                }

                if (P18.Equals("1"))
                {
                    chkRP2.Checked = true;

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

                }
                else
                {
                    chkRP1.Checked = false;
                }

                ModalPopupExtender1.Show();
         

        }

        protected void btnSearchBarcode_Click(object sender, ImageClickEventArgs e)
        {
            DTInvInfo = ObjSel.GetInvInfo(BranchAlias, txtBarcode.Text);
            if (DTInvInfo.Rows.Count == 0)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Item not found, please enter valid Barcode ')", true);
                ClearInvForm();
                return;
            }
            else
            {
                ShowInventory();
                btnSubmit.Visible = false;
                btnUpdateInv.Visible = true;
            }
        }
        //protected void txtBarcode_TextChanged(object sender, EventArgs e)
        //{
        //    //ShowInventory();
        //}
        public void ShowInventory()
        {
            
            GInventoryID = DTInvInfo.Rows[0]["InventoryID"].ToString(); 
            GInventoryName = DTInvInfo.Rows[0]["InventoryName"].ToString();
            GDescription = DTInvInfo.Rows[0]["Description"].ToString();
            GCategoryID = DTInvInfo.Rows[0]["CategoryID"].ToString();
            GClass = DTInvInfo.Rows[0]["Class"].ToString();
            GUnit = DTInvInfo.Rows[0]["Unit"].ToString();
            GPrice = DTInvInfo.Rows[0]["Price"].ToString();
            GPrice2 = DTInvInfo.Rows[0]["Price2"].ToString();
            GPrice3 = DTInvInfo.Rows[0]["Price3"].ToString();
            GPrice4 = DTInvInfo.Rows[0]["Price4"].ToString();
            GGroup1 = DTInvInfo.Rows[0]["Group1"].ToString();
            GGroup2 = DTInvInfo.Rows[0]["Group2"].ToString();
            GGroup3 = DTInvInfo.Rows[0]["Group3"].ToString();
            GGroup4 = DTInvInfo.Rows[0]["Group4"].ToString();
            GGroup5 = DTInvInfo.Rows[0]["Group5"].ToString();
            GFlag1 = DTInvInfo.Rows[0]["Flag1"].ToString();
            GFlag2 = DTInvInfo.Rows[0]["Flag2"].ToString();
            GReorderLevel = DTInvInfo.Rows[0]["ReorderLevel"].ToString();
            GMaximumLevel = DTInvInfo.Rows[0]["MaxiumLevel"].ToString();
            GSafetyLevel = DTInvInfo.Rows[0]["SafetyLevel"].ToString();

            //Assigning values to form fields
            LblInvIDCaption.Visible = true;
            lblInventoryID.Visible = true;

            lblInventoryID.Text = GInventoryID;
            txtInventoryName.Text = GInventoryName;
            txtDescription.Text = GDescription;
            cmbCategoryName.SelectedIndex = cmbCategoryName.Items.IndexOf(cmbCategoryName.Items.FindByValue(GCategoryID));
            cmbClass.SelectedIndex = cmbClass.Items.IndexOf(cmbClass.Items.FindByValue(GClass));
            cmbUnits.SelectedIndex = cmbUnits.Items.IndexOf(cmbUnits.Items.FindByValue(GUnit));
            txtPrice1.Text = GPrice;
            txtPrice2.Text = GPrice2;
            txtPrice3.Text = GPrice3;
            txtPrice4.Text = GPrice4;
            cmbGroup1.SelectedIndex = cmbGroup1.Items.IndexOf(cmbGroup1.Items.FindByValue(GGroup1));
            cmbGroup2.SelectedIndex = cmbGroup2.Items.IndexOf(cmbGroup2.Items.FindByValue(GGroup2));
            cmbGroup3.SelectedIndex = cmbGroup3.Items.IndexOf(cmbGroup3.Items.FindByValue(GGroup3));
            cmbGroup4.SelectedIndex = cmbGroup4.Items.IndexOf(cmbGroup4.Items.FindByValue(GGroup4));
            cmbGroup5.SelectedIndex = cmbGroup5.Items.IndexOf(cmbGroup5.Items.FindByValue(GGroup5));

            // Get the Flag1 Values - Tax Setting 
            char[] Flag_Val = GFlag1.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 

            // Assgin values to Checkbox : 
            if (c0.Equals("1"))
            {
                chk2InventoryCate.Checked = true;
            }
            else
            {
                chk2InventoryCate.Checked = false;
            }


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

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

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

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

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

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


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

            //  Get the Flag2 Values : Printer Settings

            char[] Flag2_Val = GFlag2.ToCharArray();

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

            // Assign Values to Checkbox : 
            if (P0.Equals("1"))
            {
                chk2Invoice2.Checked = true;
            }
            else
            {
                chk2Invoice2.Checked = false;
            }

            if (P1.Equals("1"))
            {
                chk2RP19.Checked = true;
            }
            else
            {
                chk2RP19.Checked = false;
            }
            if (P2.Equals("1"))
            {
                chk2RP18.Checked = true;

            }
            else
            {
                chk2RP18.Checked = false;
            }

            if (P3.Equals("1"))
            {
                chk2RP17.Checked = true;

            }
            else
            {
                chk2RP17.Checked = false;
            }

            if (P4.Equals("1"))
            {
                chk2RP16.Checked = true;

            }
            else
            {
                chk2RP16.Checked = false;
            }

            if (P5.Equals("1"))
            {
                chk2RP15.Checked = true;

            }
            else
            {
                chk2RP15.Checked = false;
            }


            if (P6.Equals("1"))
            {
                chk2RP14.Checked = true;

            }
            else
            {
                chk2RP14.Checked = false;
            }


            if (P7.Equals("1"))
            {
                chk2RP13.Checked = true;

            }
            else
            {
                chk2RP13.Checked = false;
            }

            if (P8.Equals("1"))
            {
                chk2RP12.Checked = true;

            }
            else
            {
                chk2RP12.Checked = false;
            }



            if (P9.Equals("1"))
            {
                chk2RP11.Checked = true;

            }
            else
            {
                chk2RP11.Checked = false;
            }

            if (P10.Equals("1"))
            {
                chk2RP10.Checked = true;

            }
            else
            {
                chk2RP10.Checked = false;
            }

            if (P11.Equals("1"))
            {
                chk2RP9.Checked = true;

            }
            else
            {
                chk2RP9.Checked = false;
            }
            if (P12.Equals("1"))
            {
                chk2RP8.Checked = true;

            }
            else
            {
                chk2RP8.Checked = false;
            }
            if (P13.Equals("1"))
            {
                chk2RP7.Checked = true;
            }
            else
            {
                chk2RP7.Checked = false;
            }
            if (P14.Equals("1"))
            {
                chk2RP6.Checked = true;
            }
            else
            {
                chk2RP6.Checked = false;
            }

            if (P15.Equals("1"))
            {
                chk2RP5.Checked = true;
            }
            else
            {
                chk2RP5.Checked = false;
            }

            if (P16.Equals("1"))
            {
                chk2RP4.Checked = true;
            }
            else
            {
                chk2RP4.Checked = false;
            }

            if (P17.Equals("1"))
            {
                chk2RP3.Checked = true;
            }
            else
            {
                chk2RP3.Checked = false;
            }

            if (P18.Equals("1"))
            {
                chk2RP2.Checked = true;
            }
            else
            {
                chk2RP2.Checked = false;
            }
            if (P19.Equals("1"))
            {
                chk2RP1.Checked = true;

            }
            else
            {
                chk2RP1.Checked = false;
            }

            txtReorder.Text = GReorderLevel;
            txtMaximum.Text = GMaximumLevel;
            txtSafty.Text = GSafetyLevel;

        }
        
        //Save details to Datatable for Inventory Adjust process
        public void btnSave_Click(object sender, System.EventArgs e)
        {
            FAdjDt = (DataTable)Session["QtyTable"];        //session value assign to Final datatable for Submit
            
            if (AdjDt.Rows.Count == 0)
            {
                AdjDt.Columns.Add(new System.Data.DataColumn("SNo", typeof(String)));
                AdjDt.Columns.Add(new System.Data.DataColumn("InventoryID", typeof(String)));
                AdjDt.Columns.Add(new System.Data.DataColumn("Barcode", typeof(String)));
                AdjDt.Columns.Add(new System.Data.DataColumn("Qty", typeof(String)));
            }
            
            DataRow dr;
            if (FAdjDt != null)
            {
                if (FAdjDt.Rows.Count > 0)
                {
                    foreach (DataRow row in FAdjDt.Rows)    //load previous page session values to current datatable
                    {
                        dr = AdjDt.NewRow();
                        dr[0] = row["SNo"].ToString();
                        dr[1] = row["InventoryID"].ToString();
                        dr[2] = row["Barcode"].ToString();
                        dr[3] = row["Qty"].ToString();
                        AdjDt.Rows.Add(dr);
                        iSno = Convert.ToInt32(dr[0]); 
                    }
                }
            }
             
            foreach (GridViewRow row in grdInventoryView.Rows)    //append current page data to datatable
            {
                Label InvID = (Label)row.FindControl("lblInventoryID"); 
                Label Barcode = (Label)row.FindControl("lblBarCode");
                TextBox Qty = (TextBox)row.FindControl("txtAdjust");
                if (Qty.Text != string.Empty && Qty.Text != "0")
                {
                    iSno = iSno + 1;
                    sSno = Convert.ToString(iSno);

                    dr = AdjDt.NewRow();
                    dr[0] = sSno;
                    dr[1] = InvID.Text;
                    dr[2] = Barcode.Text;
                    dr[3] = Qty.Text;
                    AdjDt.Rows.Add(dr);
                }
            }

            Session["QtyTable"] = AdjDt;
  
            DataTable DT101 = new DataTable();
            DT101=(DataTable)Session["QtyTable"];
            GrdInvAdjView.DataSource = DT101;
            GrdInvAdjView.DataBind();
        }
       
       protected void LoadInvAdjView()
        {
            DataTable DtVal = new DataTable();
            DtVal = (DataTable)Session["QtyTable"];
            GrdInvAdjView.DataSource =DtVal;
            GrdInvAdjView.DataBind();
        }

        public void btnAdjustStock_Click(object sender, System.EventArgs e)
        {
            MPEInvAdjustView.Show();
            LoadInvAdjView();
 
        }
        
        //Inventory Adjust Code
        public void btnAdjustStockSubmit_Click(object sender, System.EventArgs e)
        {

            foreach (GridViewRow row in GrdInvAdjView.Rows)
            {
                Label SNo = (Label)row.FindControl("lblSNo");
                Label InvID = (Label)row.FindControl("lblInvID");
                Label Barcode = (Label)row.FindControl("lblBarcode");
                Label Qty = (Label)row.FindControl("lblQty"); 

                //Get Current Cost from InventoryCostAverage table
                DTQry = ObjSel.CheckUnitCost(BranchAlias, InvID.Text );
                if (DTQry.Rows.Count != 0)
                {
                    AdjInvCost = DTQry.Rows[0]["UnitCost"].ToString();
                }
                else
                {
                    AdjInvCost = "0";
                }

                //Insert Adjust details into InventoryAdjust table
                sCmd = "Insert INTO " + BranchAlias + ".inventoryadjust(InventoryID, AdjustQuantity, UnitCost, DateAdjusted, EmployeeID)values(" + InvID.Text  + "," + Qty.Text  + " ,  " + AdjInvCost + ",'" + Curr_DateOnly + "',1) ";
                K = ObjCloudMain.ExecuteNonQuery(sCmd);

                //Update InventoryCostAverage table 
                sCmd = "Update " + BranchAlias + ".inventorycostaverage Set PQuantity=PQuantity+" + Qty.Text  + " Where InventoryID=" + InvID.Text  +" ";
                K = ObjCloudMain.ExecuteNonQuery(sCmd);
            }
            MPEInvAdjustView.Hide();
            Session.Remove("QtyTable");
            AdjDt.Clear();
            FAdjDt.Clear(); 
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Inventory Stock Adjusted Successfully ');location.href='InventoryView.aspx' ", true);
            
        }
        //Delete rows from InventoryAdjustView grid
        protected void GrdInvAdjView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            MPEInvAdjustView.Show();
            DataTable dt1 = (DataTable)Session["QtyTable"];
            // DataTable dt = new DataTable();
            if (dt1.Rows.Count > 0)
            {
                dt1.Rows[e.RowIndex].Delete();
                GrdInvAdjView.DataSource = dt1;
                GrdInvAdjView.DataBind();
               
            }
            
           
         }
            
    }
}