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 TaxMaster : 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;
        Int32 S = 0;
        string ConStr = ConfigurationManager.ConnectionStrings["ConMysql"].ConnectionString;
        string ConStr_Rds = ConfigurationManager.ConnectionStrings["ConRDS"].ConnectionString;
        string Curr_Date = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        string Curr_DateOnly = System.DateTime.Now.ToString("yyyy-MM-dd");

        DataSet ds = new DataSet();
        MYSQLDB ObjMain = new MYSQLDB();
        MYSQLCLOUD ObjCloudMain = new MYSQLCLOUD();
        DataTable DT = new DataTable();
        DataTable Dt2 = new DataTable();
        DataTable DtLicense = new DataTable();
        DataTable DtSuperAdmin = new DataTable();
        DataTable DtMultiBranch = new DataTable();
        DataTable DtNormalUser = new DataTable();
        DataTable DTProductSale = new DataTable();
        DataTable DTCategorydl = new DataTable();
        DataTable DtCateoryList = new DataTable();
        DataTable DtCate_Printer = new DataTable();
        DataTable DTInvInfo = new DataTable();
        DataTable DTCheckBarcode = new DataTable();
        DataTable DTQry = new DataTable();
        // Tax Variables 
        // Service 
        string ServiceC_TaxName;
        decimal Service_Taxval;
        string Service_AddOn;
        string Service_TaxOn;
        // Gst 
        string Gst_TaxName;
        decimal Gst_TaxVal;
        string Gst_AddOn;
        string Gst_TaxOn;
        // Tax3
        string Tax3_TaxName;
        decimal Tax3_TaxVal;
        string Tax3_AddOn;
        string Tax3_TaxOn;
        // Tax4 
        string Tax4_TaxName;
        decimal Tax4_TaxVal;
        string Tax4_AddOn;
        string Tax4_TaxOn;
        // Tax5
        string Tax5_TaxName;
        decimal Tax5_TaxVal;
        string Tax5_AddOn;
        string Tax5_TaxOn;
        // Tax6
        string Tax6_TaxName;
        decimal Tax6_TaxVal;
        string Tax6_AddOn;
        string Tax6_TaxOn;
        string TaxId = string.Empty;

        string PortalTypeId = string.Empty;
        string ActiveCallvalue = string.Empty;
        string Datapath = string.Empty;
        string cd_BranchAlias = string.Empty;
        string PID = string.Empty;
        string Page_Acess = string.Empty;
        string Read_Acess = string.Empty;
        string Write_Acess = string.Empty;
        string Isdowld_Acess = string.Empty;
        string Rcd_id = string.Empty;
        string RPortalid = string.Empty;
        string Useridvalue = string.Empty;
        string QueryCurr_Select = string.Empty;
        string L_MaxUserCnt = string.Empty;

        string L_Inventory = string.Empty;
        string Branchidval = string.Empty;
        string BranchFullName = string.Empty;
        string Curr_BranchFullName = string.Empty;
        string Cmb_BranchName = string.Empty;
        string HQ = string.Empty;
        string Cm_id = string.Empty;
        string ISHQ = string.Empty;
        string BranchAlias = string.Empty;
        string RBranchid = string.Empty;
        string Branchid = string.Empty;

        // Added On 02-12-2021
        string ReadHQ = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {


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


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

            if (((Convert.ToBoolean(Session["IsSuperAdmin"])) == true) && (Session["PortalTypeId"].ToString() == "") && ((Convert.ToBoolean(Session["IsMultiBranch"])) == true))
            {
                if (!IsPostBack)
                {

                    LoadBranchesOnID();
                    string QueryCal = "select ActiveValue from portaldb.usermaster  where Userid=" + Session["UserId"].ToString() + " ";
                    ActiveCallvalue = ObjMain.ExecuteScalar(QueryCal);
                    if (ActiveCallvalue != " " || ActiveCallvalue != "Select Branch")
                    {
                        ActiveCall();
                    }
                }
            }
            else if (((Convert.ToBoolean(Session["IsSuperAdmin"])) == true) && (Session["PortalTypeId"].ToString() == ""))
            {
                if (!IsPostBack)
                {
                    LoadBranchName4SuperAdmin();
                }
                BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
                Cmb_BranchName = ObjUserBL.GetBranchRealValue(Session["Branch_HQ"].ToString(), Session["UserId"].ToString(), BranchFullName);  // This will Return BranchName( Actual Database Name )
                BranchAlias = Cmb_BranchName;

                if (!IsPostBack)
                {

                    GetTaxDetails();

                }


            }
            else if (((Convert.ToBoolean(Session["IsMultiBranch"])) == true) && (Session["PortalTypeId"].ToString() == ""))
            {
                if (!IsPostBack)
                {
                    LoadBranchesOnID();
                    ActiveCall();
                }
            }
            else if (((Session["PortalTypeId"].ToString() != "") || (Convert.ToBoolean(Session["IsSuperAdmin"])) == false) && ((Convert.ToBoolean(Session["IsMultiBranch"])) == false))
            {
                cmbBranches.Enabled = false;
                cmbBranches.Visible = false;

                Cm_id = ObjSel.GETcmid(Session["Cd_Id"].ToString());  // Get cm_id
                HQ = ObjSel.Get_HQ(Cm_id);     // Get the HQ Value
                Curr_BranchFullName = ObjSel.GetBranchFullName(HQ, Session["Branch_HQ"].ToString());


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

                DT = ObjUserBL.GetPageRightsDetailsPid(Session["UserId"].ToString(), PID);
                if (DT.Rows.Count > 0)
                {
                    Page_Acess = DT.Rows[0]["Acess_status"].ToString();
                    Read_Acess = DT.Rows[0]["Read_Access"].ToString();
                    Write_Acess = DT.Rows[0]["Write_Access"].ToString();
                    Isdowld_Acess = DT.Rows[0]["IDwld_Access"].ToString();

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

                else
                {

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

                            if (cmbBranches.Visible == false)
                            {

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

                                if (!IsPostBack)
                                {
                                    GetTaxDetails();  // Loading Part 

                                }


                            }

                        }


                    }

                }

            }
            else
            {

            }


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


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

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

                #endregion


            }




            // HQ Validation For Loading Branch Name : Dropdown  Added Karthik  On 02-12-21 
            #region  HQ Validation Starts Here ...  
            cd_BranchAlias = ActiveCallvalue;
            ReadHQ = ObjUserBL.GetBranchFullNameByBranchName(Session["Branch_HQ"].ToString(), cd_BranchAlias);

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

            }
            #endregion  


            DT = ObjUserBL.GetPageRightsDetailsPIDNew(Session["UserId"].ToString(), PID, Branchidval);
            if (DT.Rows.Count > 0)
            {
                Page_Acess = DT.Rows[0]["Acess_status"].ToString();
                Read_Acess = DT.Rows[0]["Read_Access"].ToString();
                Write_Acess = DT.Rows[0]["Write_Access"].ToString();
                Isdowld_Acess = DT.Rows[0]["IDwld_Access"].ToString();

                Session["Page_Acess"] = Page_Acess;
                Session["Read_Acess"] = Read_Acess;
                Session["Write_Acess"] = Write_Acess;
                Session["Isdowld_Acess"] = Isdowld_Acess;
            }
            Rcd_id = ObjMain.ExecuteScalar("select cd_id from portaldb.companydetails where cd_branchid=" + cmbBranches.SelectedItem.Value + " and cd_branchalias='" + ActiveCallvalue + "';");
            RPortalid = ObjMain.ExecuteScalar("select portaltypeid  from portaldb.invoicedetails where cd_id=" + Rcd_id + ";");
            DtLicense = ObjUserBL.GetLicenseDetails(RPortalid);
            if (DtLicense.Rows.Count > 0)
            {
                L_Inventory = DtLicense.Rows[0]["INVENTORY"].ToString();
                Session["L_Inventory"] = L_Inventory;
            }
            if ((Session["L_Inventory"] == null) || Session["L_Inventory"].ToString() == "N" || Session["Page_Acess"].ToString() == "N")
            {
                if (cmbBranches.SelectedItem.Text == "HQ")
                {
                    BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
                    Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);
                    UPDATEBRANCH(Cmb_BranchName);
                    BranchAlias = Microsoft.Security.Application.Encoder.HtmlEncode(Cmb_BranchName);
                    GetTaxDetails();
                }
                else
                {
                    Response.Redirect("DeniedPage.aspx");
                }
                
            }
            else if (Session["Write_Acess"].ToString() == "N")
            {
                Response.Redirect("DeniedPage.aspx");
            }

            else
            {
                // btnExcel.Enabled = true;
                BranchAlias = Microsoft.Security.Application.Encoder.HtmlEncode(Cmb_BranchName);
                GetTaxDetails();

            }

        }

        public void GetTaxDetails()
        {
            //XSS Attack Prevention 

            DT = ObjSel.ReadTaxMasterValue(BranchAlias);
            if (DT.Rows.Count > 0)
            {
                ServiceC_TaxName = DT.Rows[0]["TaxName"].ToString();
                Service_Taxval = Convert.ToDecimal(DT.Rows[0]["Tax"].ToString());
                Service_AddOn = DT.Rows[0]["AddOn"].ToString();
                Service_TaxOn = DT.Rows[0]["TaxOnTax"].ToString();

                Gst_TaxName = DT.Rows[1]["TaxName"].ToString();
                Gst_TaxVal = Convert.ToDecimal(DT.Rows[1]["Tax"]);
                Gst_AddOn = DT.Rows[1]["AddOn"].ToString();
                Gst_TaxOn = DT.Rows[1]["TaxOnTax"].ToString();

                Tax3_TaxName = DT.Rows[2]["TaxName"].ToString();
                Tax3_TaxVal = Convert.ToDecimal(DT.Rows[2]["Tax"].ToString());
                Tax3_AddOn = DT.Rows[2]["AddOn"].ToString();
                Tax3_TaxOn = DT.Rows[2]["TaxOnTax"].ToString();

                Tax4_TaxName = DT.Rows[3]["TaxName"].ToString();
                Tax4_TaxVal = Convert.ToDecimal(DT.Rows[3]["Tax"].ToString());
                Tax4_AddOn = DT.Rows[3]["AddOn"].ToString();
                Tax4_TaxOn = DT.Rows[3]["TaxOnTax"].ToString();

                Tax5_TaxName = DT.Rows[4]["TaxName"].ToString();
                Tax5_TaxVal = Convert.ToDecimal(DT.Rows[4]["Tax"].ToString());
                Tax5_AddOn = DT.Rows[4]["AddOn"].ToString();
                Tax5_TaxOn = DT.Rows[4]["TaxOnTax"].ToString();

                Tax6_TaxName = DT.Rows[5]["TaxName"].ToString();
                Tax6_TaxVal = Convert.ToDecimal(DT.Rows[5]["Tax"].ToString());
                Tax6_AddOn = DT.Rows[5]["AddOn"].ToString();
                Tax6_TaxOn = DT.Rows[5]["TaxOnTax"].ToString();

                txtServiceTax.Text = ServiceC_TaxName;
                decimal S_Tax = 0;
                S_Tax = Service_Taxval * 100;
                txtServiceTaxValue.Text = Convert.ToInt16((S_Tax)).ToString();

                if (Service_AddOn.Equals("0"))
                {
                    chkSERVICECAddon.Checked = false;
                }
                else
                {
                    chkSERVICECAddon.Checked = true;
                }

                //if (Service_TaxOn.Equals("0"))      // Commmanded on 18-6-21
                //{
                //    chkSERVICETaxon.Checked = false;
                //}
                //else
                //{
                //    chkSERVICETaxon.Checked = true;
                //}

                txtGST.Text = Gst_TaxName;
                decimal G_Tax = 0;
                G_Tax = Gst_TaxVal * 100;
                txtGSTTaxValue.Text = Microsoft.Security.Application.Encoder.HtmlEncode(Convert.ToInt16(G_Tax).ToString());

                if (Gst_AddOn.Equals("0"))
                {
                    chkGSTAddon.Checked = false;
                }
                else
                {
                    chkGSTAddon.Checked = true;
                }
                //if (Gst_TaxOn.Equals("0"))      // Commmanded on 18-6-21
                //{ 
                //    chkGSTTaxon.Checked = false;
                //}
                //else
                //{
                //    chkGSTTaxon.Checked = true;
                //}



                txtTax3.Text = Tax3_TaxName;
                decimal T3_Tax = 0;
                T3_Tax = Tax3_TaxVal * 100;
                txtTax3TaxValue.Text = Microsoft.Security.Application.Encoder.HtmlEncode(Convert.ToInt16(T3_Tax).ToString());

                if (Tax3_AddOn.Equals("0"))
                {
                    chkTax3AddOn.Checked = false;
                }
                else
                {
                    chkTax3AddOn.Checked = true;
                }

                //if (Tax3_TaxOn.Equals("0"))     // Commmanded on 18-6-21
                //{
                //    chkTax3TaxOn.Checked = false;
                //}
                //else
                //{
                //    chkTax3TaxOn.Checked = true;
                //}


                txtTax4.Text = Tax4_TaxName;
                decimal T4_Tax = 0;
                T4_Tax = Tax4_TaxVal * 100;
                txtTax4Taxvalue.Text = Microsoft.Security.Application.Encoder.HtmlEncode(Convert.ToInt16(T4_Tax).ToString());

                if (Tax4_AddOn.Equals("0"))
                {
                    chkTax4Addpn.Checked = false;
                }
                else
                {
                    chkTax4Addpn.Checked = true;
                }

                //if (Tax4_TaxOn.Equals("0"))     // Commmanded on 18-6-21
                //{
                //    chkTax4Taxon.Checked = false;
                //}
                //else
                //{
                //    chkTax4Taxon.Checked =true;
                //}

                txtTax5.Text = Tax5_TaxName;
                decimal T5_Tax = 0;
                T5_Tax = Tax5_TaxVal * 100;
                txtTax5TaxValue.Text = Microsoft.Security.Application.Encoder.HtmlEncode(Convert.ToInt16(T5_Tax).ToString());
                if (Tax5_AddOn.Equals("0"))
                {
                    chkTax5Addon.Checked = false;
                }
                else
                {
                    chkTax5Addon.Checked = true;
                }
                //if (Tax5_TaxOn.Equals("0"))       // Commmanded on 18-6-21
                //{
                //    chk5TaxOn.Checked = false;
                //}
                //else
                //{
                //    chk5TaxOn.Checked =true;
                //}


                txtTax6.Text = Tax6_TaxName;
                decimal T6_Tax = 0;
                T6_Tax = Tax6_TaxVal * 100;
                txtTax6TaxValue.Text = Microsoft.Security.Application.Encoder.HtmlEncode(Convert.ToInt16(T6_Tax).ToString());
                if (Tax6_AddOn.Equals("0"))
                {
                    chkTax6Addon.Checked = false;
                }
                else
                {
                    chkTax6Addon.Checked = true;
                }

                //if (Tax6_TaxOn.Equals("0"))    // Commmanded on 18-6-21
                //{
                //    chkTax6TaxOn.Checked = false;
                //}
                //else
                //{
                //    chkTax6TaxOn.Checked = true;
                //}

            }

        }

        protected void cmbBranches_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbBranches.SelectedIndex == 0)
            {
                Response.Write("Inventory.aspx");
            }
            else
            {
                if (Branchidval != "")
                {
                    DT = ObjUserBL.GetPageRightsDetailsPIDNew(Session["UserId"].ToString(), PID, Branchidval);
                }
                else
                {
                    DT = ObjUserBL.GetPageRightsDetailsPIDNew(Session["UserId"].ToString(), PID, Session["Fistbranch"].ToString());

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

                if (cmbBranches.SelectedIndex == 1)
                {

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

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

                if (DtLicense.Rows.Count > 0)
                {

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

                if ((Session["L_Inventory"] == null) || Session["L_Inventory"].ToString() == "N" || Session["Page_Acess"].ToString() == "N")
                {
                    if (cmbBranches.SelectedItem.Text == "HQ")
                    {
                        BranchFullName = cmbBranches.SelectedItem.Text;    // This will Return Branch FullName 
                        Cmb_BranchName = ObjSel.GetBranchDBNamebyFullName(Session["Branch_HQ"].ToString(), BranchFullName);
                        UPDATEBRANCH(Cmb_BranchName);


                        // btnExcel.Enabled = true;
                        BranchAlias = Cmb_BranchName;
                        GetTaxDetails();

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




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

                else
                {
                    // btnExcel.Enabled = true;
                    BranchAlias = Microsoft.Security.Application.Encoder.HtmlEncode(Cmb_BranchName);
                    GetTaxDetails();

                }
            }
            UPDATEBRANCH(BranchAlias);
        }


        public void UPDATEBRANCH(string ActiveCallvalue)
        {
            if (!string.IsNullOrEmpty(Session["UserId"] as string))
            {
                Useridvalue = Session["UserId"].ToString();
                QueryCurr_Select = "Update portaldb.usermaster set ActiveValue='" + ActiveCallvalue + "' where Userid=" + Useridvalue + "";
                S = ObjMain.ExecuteNonQuery(QueryCurr_Select);
            }
            else
            {
                Response.Redirect("Login.aspx");

            }

        }


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

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

                    RBranchid = "," + RBranchid;
                    StrBObj.Append("  " + RBranchid + "    ");
                }
                else
                {
                    string _get_HQ = ObjSel.GetHQ_Status(Session["CM_ID"].ToString());
                    if (_get_HQ == "0")
                    {

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


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


                    }


                }


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

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

        }

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

        }


        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            if (cmbBranches.Visible == false)
            {

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

            }
            else
            {
                BranchFullName = cmbBranches.SelectedItem.Text;      // This will Return Branch FullName 
                if (BranchFullName == "HQ")
                {
                    BranchAlias = Session["Branch_HQ"].ToString();
                }
                else
                {

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



               
            }


            // Servicec
            if (chkSERVICECAddon.Checked == false)
            {
                chkSERVICECAddon.Text = "0";
            }
            else
            {
                chkSERVICECAddon.Text = "1";
            }

            if (txtServiceTaxValue.Text != "0")
            {
                if (chkSERVICETaxon.Checked == false)
                {
                    chkSERVICETaxon.Text = "0";
                }
                else
                {
                    chkSERVICETaxon.Text = "1";
                }


            }
            else
            {
                chkSERVICETaxon.Text = "0";
            }





            decimal CurrTax_Service = Convert.ToDecimal(txtServiceTaxValue.Text) / 100;
            UPTdateTax(BranchAlias, "1", Microsoft.Security.Application.Encoder.HtmlEncode(txtServiceTax.Text), CurrTax_Service, chkSERVICECAddon.Text, chkSERVICETaxon.Text);
            // Gst 


            if (chkGSTAddon.Checked == false)
            {
                chkGSTAddon.Text = "0";
            }
            else
            {
                chkGSTAddon.Text = "1";
            }


            if (txtGSTTaxValue.Text != "0")
            {

                if (chkGSTTaxon.Checked == false)
                {
                    chkGSTTaxon.Text = "0";
                }
                else
                {
                    chkGSTTaxon.Text = "1";
                }


            }
            else
            {
                chkGSTTaxon.Text = "0";

            }





            decimal CurrTax_Gst = Convert.ToDecimal(Microsoft.Security.Application.Encoder.HtmlEncode(txtGSTTaxValue.Text)) / 100;
            UPTdateTax(BranchAlias, "2", Microsoft.Security.Application.Encoder.HtmlEncode(txtGST.Text), CurrTax_Gst, Microsoft.Security.Application.Encoder.HtmlEncode(chkGSTAddon.Text), Microsoft.Security.Application.Encoder.HtmlEncode(chkGSTTaxon.Text));
            // Tax 3 
            if (chkTax3AddOn.Checked == false)
            {
                chkTax3AddOn.Text = "0";
            }
            else
            {
                chkTax3AddOn.Text = "1";
            }

            string TaxonTax3 = string.Empty;
            if (txtTax3TaxValue.Text != "0")
            {
                if (chkTax3TaxOn.Checked == false)
                {
                    chkTax3TaxOn.Text = "0";
                    TaxonTax3 = "0";
                }
                else
                {
                    chkTax3TaxOn.Text = "1";
                    TaxonTax3 = "1";
                }

            }
            else
            {
                TaxonTax3 = "0";
            }


            decimal CurrTax_Tax3 = Convert.ToDecimal(Microsoft.Security.Application.Encoder.HtmlEncode(txtTax3TaxValue.Text)) / 100;
            UPTdateTax(BranchAlias, "3", Microsoft.Security.Application.Encoder.HtmlEncode(txtTax3.Text), CurrTax_Tax3, chkTax3AddOn.Text, TaxonTax3);

            // Tax 4 


            if (chkTax4Addpn.Checked == false)
            {
                chkTax4Addpn.Text = "0";
            }
            else
            {
                chkTax4Addpn.Text = "1";
            }


            if (txtTax4Taxvalue.Text != "0")
            {

                if (chkTax4Taxon.Checked == false)
                {
                    chkTax4Taxon.Text = "0";
                }
                else
                {
                    chkTax4Taxon.Text = "1";
                }
            }
            else
            {
                chkTax4Taxon.Text = "0";
            }

            decimal CurrTax_Tax4 = Convert.ToDecimal(Microsoft.Security.Application.Encoder.HtmlEncode(txtTax4Taxvalue.Text)) / 100;
            UPTdateTax(BranchAlias, "4", Microsoft.Security.Application.Encoder.HtmlEncode(txtTax4.Text), CurrTax_Tax4, chkTax4Addpn.Text, chkTax4Taxon.Text);
            // Tax 5 
            if (chkTax5Addon.Checked == false)
            {
                chkTax5Addon.Text = "0";
            }

            else
            {
                chkTax5Addon.Text = "1";
            }


            if (txtTax5TaxValue.Text != "0")
            {
                if (chk5TaxOn.Checked == false)
                {
                    chk5TaxOn.Text = "0";
                }
                else
                {
                    chk5TaxOn.Text = "1";
                }

            }
            else
            {
                chk5TaxOn.Text = "0";
            }



            decimal CurrTax_Tax5 = Convert.ToDecimal(Microsoft.Security.Application.Encoder.HtmlEncode(txtTax5TaxValue.Text)) / 100;
            UPTdateTax(BranchAlias, "5", Microsoft.Security.Application.Encoder.HtmlEncode(txtTax5.Text), CurrTax_Tax5, chkTax5Addon.Text, chk5TaxOn.Text);

            // Tax 6

            if (chkTax6Addon.Checked == false)
            {
                chkTax6Addon.Text = "0";
            }

            else
            {
                chkTax6Addon.Text = "1";
            }

            if (txtTax6TaxValue.Text != "0")
            {
                if (chkTax6TaxOn.Checked == false)
                {
                    chkTax6TaxOn.Text = "0";
                }
                else
                {
                    chkTax6TaxOn.Text = "1";
                }
            }
            else
            {
                chkTax6TaxOn.Text = "0";
            }
            decimal CurrTax_Tax6 = Convert.ToDecimal(Microsoft.Security.Application.Encoder.HtmlEncode(txtTax6TaxValue.Text)) / 100;
            UPTdateTax(BranchAlias, "6", Microsoft.Security.Application.Encoder.HtmlEncode(txtTax6.Text), CurrTax_Tax6, Microsoft.Security.Application.Encoder.HtmlEncode(chkTax6Addon.Text), Microsoft.Security.Application.Encoder.HtmlEncode(chkTax6TaxOn.Text));
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert('Tax Setting Updated Successfully');location.href='TaxMaster.aspx' ", true);
        }
        public void UPTdateTax(string BranchAlias, string Taxid, string TaxName, decimal TaxValue, string AddOn, string TaxOn)
        {
            string Query = "UPDATE   {0}.Tax  SET TaxName=@TaxName ,Tax= @Tax ,AddOn=@AddOn,TaxOnTax=@TaxOnTax WHERE TaxID=@TaxID  ";

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

            MySqlParameter[] parameters =
                {
                     new MySqlParameter("@TaxName",TaxName),
                     new MySqlParameter("@Tax",TaxValue),
                     new MySqlParameter("@AddOn",AddOn),
                     new MySqlParameter("@TaxOnTax",TaxOn),
                     new MySqlParameter("@TaxID",Taxid),
               };

            K = ObjMain.ExecuteQuery(command, parameters);

        }
    }
}