public BillController()
 {
     sessid = Convert.ToString(System.Web.HttpContext.Current.Session["usercode"]); // "a";  //Convert.ToString(Session["UserId"]);
     if (sessid == "")
     {
         return;
     }
     branchcode = Convert.ToString(System.Web.HttpContext.Current.Session["branchcode"]);
     partnername = Convert.ToString(System.Web.HttpContext.Current.Session["partnername"]);
     usertype = Convert.ToString(System.Web.HttpContext.Current.Session["usertype"]);
     username = Convert.ToString(System.Web.HttpContext.Current.Session["username"]);
     useEmail = Convert.ToString(System.Web.HttpContext.Current.Session["useremailid"]);
     cld = new BILLDAL();
 }
        public void datawithminats()
        {
            TextboxExample.Text = "";
            DropDownExTextboxExample.Items.Clear();
            DropDownExTextboxExample1.Items.Clear();
            BILLDAL bal = new BILLDAL();
            DataTable dt = bal.tariff_dataFill(ddltariff.SelectedItem.ToString());
            DataRow[] dtt_rif = dt.Select("CHARGESTYPE ='DATA PAY PER USE' ");
            foreach (var item in dtt_rif)
            {
                DropDownExTextboxExample1.Items.Add(item["A"].ToString());
                DropDownExTextboxExample.Items.Add(item["VALUE"].ToString());
                TextboxExample.Text = item["VALUE"].ToString();
            }

            DataRow[] dr_l1 = dt.Select("CHARGESTYPE = 'RENTAL'", "");
            if (dr_l1.Length > 0)
            {
                System.Web.HttpContext.Current.Session["rntl"] = dr_l1[0]["VALUE"].ToString();
            }
            else
                System.Web.HttpContext.Current.Session["rntl"] = 0;

            DataRow[] dr_lfree = dt.Select("CHARGESTYPE = 'FREE VALUE'", "");
            if (dr_lfree.Length > 0)
            {
                System.Web.HttpContext.Current.Session["Free_Value"] = dr_lfree[0]["VALUE"].ToString();
            }
            else
                System.Web.HttpContext.Current.Session["Free_Value"] = 0;

            //==================================================================== Bolton====================
            DataRow[] dr_l18 = dt.Select("CHARGESTYPE = 'BOLTONS'", "");
            if (dr_l1.Length > 0)
            {
                System.Web.HttpContext.Current.Session["rntl8"] = dr_l18[0]["VALUE"].ToString();
            }
            else
                System.Web.HttpContext.Current.Session["rntl8"] = 0;

            //=============================================================================End
        }
        protected void fillallvalue()
        {
            datawithminats();

            int indiaunits = 0;
            int othersunits = 0;
            BILLDAL bal = new BILLDAL();
            DataTable dtrowdata = bal.country_name();
             dt = bal.tarifupload(ddltariff.SelectedItem.ToString());

            DataRow[] dr_lunits = dt.Select("CHARGESTYPE = 'FREE VALUE'", "");
            decimal freeval = 0;
            string strmin = "";
            string cnty = "";
            if (dr_lunits.Length > 0)
            {
                foreach (var item in dr_lunits)
                {
                    freeval = Convert.ToDecimal(item["VALUE"]);
                    strmin = item["A"].ToString();
                    cnty = item["COUNTRY_TO"].ToString().ToUpper();
                }
            }
            string col = "";
            for (int i = 0; i < (GridView1.Rows.Count); i++)
            {
                TextBox txt_dr = (TextBox)GridView1.Rows[i].FindControl("txtrate");
                Label txtlblunits = (Label)GridView1.Rows[i].FindControl("lblunits");
                Label txtllbldcost = (Label)GridView1.Rows[i].FindControl("lbldcost");

                Label txtnum = (Label)GridView1.Rows[i].FindControl("lblnum");

                if (Information.IsNumeric(txt_dr.Text) == true)
                {
                    string str_rate = "";
                    string strnumv = txtnum.Text.Substring(0, 2);
                    DataRow[] dr = dtrowdata.Select("isdcode = '" + strnumv + "'", "");

                    if (dr.Length == 0)
                    {
                        //check for size=28 in DataRow[]

                        col += strnumv+ ";";
                      //  col += strnumv + ";";
                      // col[0] = strnumv;

                        Label2.Visible = true;
                        Label2.Text = "Country codes not available in Database ";
                        txtnum.ForeColor = System.Drawing.Color.Red;

                    }
                    string strcut = "";
                    foreach (var item in dr)
                    {
                        strcut = item["country"].ToString().Replace('+', ' ').TrimStart().Trim();
                        break;
                    }
                    if (strcut == ddlcountry.SelectedItem.ToString())
                    {
                        DataRow[] dr_l = dt.Select("CHARGESTYPE = 'LOCAL OUTGOING CALL'", "");
                        if (dr_l.Length > 0)
                        {
                            if (strmin.ToUpper() == "MINUTES")
                            {
                                if (cnty == "OTHER")
                                {
                                    othersunits = Convert.ToInt32(txtlblunits.Text) + othersunits;
                                    if (othersunits > freeval)
                                    {
                                        str_rate = dr_l[0]["VALUE"].ToString();
                                    }
                                    else
                                    {
                                        str_rate = "0";
                                    }
                                }
                                else
                                {
                                    foreach (var item in dr_l)
                                    {
                                        str_rate = dr_l[0]["VALUE"].ToString();
                                        break;
                                    }
                                }

                            }
                            else
                            {
                                str_rate = dr_l[0]["VALUE"].ToString();
                            }
                        }
                    }
                    else if (strcut.ToUpper() == "INDIA")
                    {

                        DataRow[] dr_l = dt.Select("CHARGESTYPE = 'INDIA OUTGOING CALL'", "");
                        if (dr_l.Length > 0)
                        {
                            if (strmin.ToUpper() == "MINUTES")
                            {
                                if (cnty == "OTHER")
                                {
                                    othersunits = Convert.ToInt32(txtlblunits.Text) + othersunits;
                                    if (othersunits > freeval)
                                    {
                                        str_rate = dr_l[0]["VALUE"].ToString();
                                    }
                                }
                                else
                                {
                                    indiaunits = Convert.ToInt32(txtlblunits.Text) + indiaunits;
                                    if (indiaunits > freeval)
                                    {
                                        str_rate = dr_l[0]["VALUE"].ToString();
                                    }
                                    else
                                    {
                                        str_rate = "0";
                                    }
                                }
                            }
                            else
                            {
                                str_rate = dr_l[0]["VALUE"].ToString();
                            }
                        }

                        else
                        {
                            DataRow[] dr_l41 = dt.Select("CHARGESTYPE = 'OTHER COUNTRIES OUTGOING CALL' and country_to = '" + strcut + "' ", "");
                            if (dr_l.Length > 0)
                            {
                                if (strmin.ToUpper() == "MINUTES")
                                {
                                    if (cnty == "OTHER")
                                    {
                                        othersunits = Convert.ToInt32(txtlblunits.Text) + othersunits;
                                        if (othersunits > freeval)
                                        {
                                            str_rate = dr_l41[0]["VALUE"].ToString();
                                        }
                                        else
                                        {
                                            str_rate = "0";
                                        }
                                    }
                                    else
                                    {
                                        foreach (var item in dr_l)
                                        {
                                            str_rate = dr_l41[0]["VALUE"].ToString();
                                            break;
                                        }
                                    }

                                }
                                else
                                {
                                    str_rate = dr_l41[0]["VALUE"].ToString();
                                }
                            }
                            else
                            {
                                DataRow[] dr_oth = dt.Select("CHARGESTYPE = 'OTHER COUNTRIES OUTGOING CALL' and country_to = 'Other' ", "");
                                if (dr_oth.Length > 0)
                                {
                                    if (strmin.ToUpper() == "MINUTES")
                                    {
                                        if (cnty == "OTHER")
                                        {
                                            othersunits = Convert.ToInt32(txtlblunits.Text) + othersunits;
                                            if (othersunits > freeval)
                                            {
                                                str_rate = dr_oth[0]["VALUE"].ToString();
                                            }
                                            else
                                            {
                                                str_rate = "0";
                                            }
                                        }
                                        else
                                        {
                                            foreach (var item in dr_l)
                                            {
                                                str_rate = dr_oth[0]["VALUE"].ToString();
                                                break;
                                            }
                                        }

                                    }
                                    else
                                    {
                                        str_rate = dr_oth[0]["VALUE"].ToString();
                                    }
                                }
                            }

                        }
                    }
                    else
                    {
                        DataRow[] dr_l = dt.Select("CHARGESTYPE = 'OTHER COUNTRIES OUTGOING CALL' and country_to = '" + strcut + "' ", "");
                        if (dr_l.Length > 0)
                        {
                            if (strmin.ToUpper() == "MINUTES")
                            {
                                othersunits = Convert.ToInt32(txtlblunits.Text) + othersunits;
                                if (othersunits > freeval)
                                {
                                    foreach (var item in dr_l)
                                    {
                                        str_rate = item["VALUE"].ToString();
                                        break;
                                    }
                                }
                                else
                                {
                                    foreach (var item in dr_l)
                                    {
                                        str_rate = "0";  //item["VALUE"].ToString();
                                        break;
                                    }
                                }

                            }
                            else
                            {
                                str_rate = dr_l[0]["VALUE"].ToString();
                            }
                        }
                        else
                        {
                            DataRow[] dr_oth = dt.Select("CHARGESTYPE = 'OTHER COUNTRIES OUTGOING CALL' and country_to = 'Other' ", "");
                            if (dr_oth.Length > 0)
                            {
                                if (strmin.ToUpper() == "MINUTES")
                                {
                                    if (cnty == "OTHER")
                                    {
                                        othersunits = Convert.ToInt32(txtlblunits.Text) + othersunits;
                                        if (othersunits > freeval)
                                        {
                                            str_rate = dr_oth[0]["VALUE"].ToString();
                                        }
                                        else
                                        {
                                            str_rate = "0";
                                        }
                                    }
                                    else
                                    {
                                        str_rate = dr_oth[0]["VALUE"].ToString();
                                    }

                                }
                                else
                                {
                                    str_rate = dr_oth[0]["VALUE"].ToString();
                                }
                            }
                        }
                    }

                    //   DataRow[] drtp = TARIFDATA.Select("CHARGESTYPE = 'OTHER COUNTRIES OUTGOING CALL'");

                    txt_dr.Text = str_rate == "" ? "0" : str_rate; // dt.Rows[0]["LOCALOUTGOINGCALL"].ToString();

                    decimal dval = Convert.ToDecimal(txtlblunits.Text) * Convert.ToDecimal(str_rate == "" ? "0" : str_rate);
                    txtllbldcost.Text = dval.ToString();
                }
                Label txt_dv = (Label)GridView1.Rows[i].FindControl("lbldtime");

                Label txt_dtime = (Label)GridView1.Rows[i].FindControl("LabelDATE");

                Label txt_orgin = (Label)GridView1.Rows[i].FindControl("LabelORIGIN");

                Label txt_daytime = (Label)GridView1.Rows[i].FindControl("LabelDAYDATE");

                if (txt_dv.Text == "SMS" && txt_dv != null || txt_dtime.Text == "SMS" && txt_dtime != null || txt_orgin.Text == "SMS" && txt_orgin != null || txt_daytime.Text == "SMS" && txt_daytime != null)
                {
                    for (int ct = i; ct < (GridView1.Rows.Count); ct++)
                    {
                        i++;
                        TextBox txtr = (TextBox)GridView1.Rows[ct].FindControl("txtrate");
                        if (Information.IsNumeric(txtr.Text) == true)
                        {

                            //==============================================================================
                            Label txt_num = (Label)GridView1.Rows[ct].FindControl("lblnum");
                            string str_te = "";
                            string strnumv = txt_num.Text.Substring(0, 2);

                            string str_rate = "";

                            DataRow[] dr = dtrowdata.Select("isdcode = '" + strnumv + "'", "");
                            string strcut = "";
                            foreach (var item in dr)
                            {
                                strcut = item["country"].ToString();
                                break;
                            }
                            if (strcut == ddlcountry.SelectedItem.ToString())
                            {
                                DataRow[] dr_l = dt.Select("CHARGESTYPE = 'LOCAL SMS'", "");
                                foreach (var item in dr_l)
                                {
                                    str_rate = item["VALUE"].ToString();
                                    break;
                                }
                            }
                            else
                            {
                                DataRow[] dr_l = dt.Select("CHARGESTYPE = 'INTL SMS'", "");
                                foreach (var item in dr_l)
                                {
                                    str_rate = item["VALUE"].ToString();
                                    break;
                                }

                            }
                            //==============================================================================
                            Label txtlbl_units = (Label)GridView1.Rows[ct].FindControl("lblunits");
                            Label txtllbl_dcost = (Label)GridView1.Rows[ct].FindControl("lbldcost");
                            txtr.Text = str_rate == "" ? "0" : str_rate; // dt.Rows[0]["LOCALSMS"].ToString();
                            decimal dval = Convert.ToDecimal(txtlbl_units.Text) * Convert.ToDecimal(str_rate == "" ? "0" : str_rate);
                            txtllbl_dcost.Text = dval.ToString();
                        }
                    }

                }

                if (txt_dv.Text == "DATA" && txt_dv != null || txt_dtime.Text == "DATA PAY PER USE" && txt_dtime != null || txt_orgin.Text == "DATA" && txt_orgin != null || txt_daytime.Text == "SMS" && txt_daytime != null)
                {
                    for (int ct = i; ct < (GridView1.Rows.Count); ct++)
                    {
                        i++;
                        TextBox txtr = (TextBox)GridView1.Rows[ct].FindControl("txtrate");
                        if (Information.IsNumeric(txtr.Text) == true)
                        {
                            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                            //==============================================================================
                            //Label txt_num = (Label)GridView1.Rows[ct].FindControl("lblnum");
                            //string str_te = "";
                            //string strnumv = txt_num.Text.Substring(0, 2);

                            string str_rate = "";

                            //DataRow[] dr = dtrowdata.Select("isdcode = '" + strnumv + "'", "");
                            string strcut = "";
                            //foreach (var item in dr)
                            //{
                            //    strcut = item["country"].ToString();
                            //    break;
                            //}

                            string rateval = "";
                            if (strcut != ddlcountry.SelectedItem.ToString())
                            {
                                Label units = (Label)GridView1.Rows[ct].FindControl("lblunits");
                                string[] stmbcharges = DropDownListBoltion.SelectedItem.ToString().Split(' ');

                                string mbdata = "";
                                if (stmbcharges[1] == "GB")
                                {
                                    mbdata = (Convert.ToDecimal(stmbcharges[0]) * 1024).ToString();
                                }
                                else
                                    mbdata= stmbcharges[0].ToString();

                                if (Convert.ToDecimal(units.Text) > Convert.ToDecimal(mbdata))
                                {
                                    decimal _ddata = 0;
                                    decimal _dvalue = 0;
                                    DataRow[] dr_l = dt.Select("CHARGESTYPE = 'DATA PAY PER USE'", "");
                                    if (dr_l.Length > 0)
                                    {
                                        _dvalue = Convert.ToDecimal( dr_l[0]["VALUE"]);

                                    }
                                    _ddata = Convert.ToDecimal(units.Text) - Convert.ToDecimal(stmbcharges[0]);
                                    rateval = _dvalue.ToString();
                                    str_rate = (_dvalue * _ddata).ToString();

                                }
                                else
                                {

                                    rateval = "0";
                                    str_rate = "0";
                                }
                            }

                            //==============================================================================
                            Label txtlbl_units = (Label)GridView1.Rows[ct].FindControl("lblunits");
                            Label txtllbl_dcost = (Label)GridView1.Rows[ct].FindControl("lbldcost");
                            txtr.Text = rateval == "" ?  "0" : rateval; // dt.Rows[0]["LOCALSMS"].ToString();
                         //  decimal dval = Convert.ToDecimal(str_rate) * Convert.ToDecimal(str_rate == "" ? "0" : str_rate);
                            txtllbl_dcost.Text = str_rate;

                        }
                    }

                }

            }
        }
        protected void ddlcountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            BILLDAL bl = new BILLDAL();
            ddltariff.DataSource = bl.Tariffcodefill_withcountry(ddlcountry.SelectedItem.ToString(), Convert.ToString(System.Web.HttpContext.Current.Session["usertype"]),
            Convert.ToString(System.Web.HttpContext.Current.Session["usercode"]), Convert.ToString(System.Web.HttpContext.Current.Session["branchcode"]));

            ddltariff.DataValueField = "tariffcode";
            ddltariff.DataTextField = "tariffcode";
            ddltariff.DataBind();
            ddltariff.Items.Insert(0, new ListItem("Select", "0"));
        }
        public void TARIFFPRINT(string tariffcode)
        {
            Add_Tariffcls ads = new Add_Tariffcls();
            BILLDAL bdl = new BILLDAL();
            System.Data.DataTable dt = bdl.tariff_dataFill(tariffcode);
            ads.TARIFFCODE = dt.Rows[0]["TARIFF_CODE"].ToString();
            ads.COUNTRY = dt.Rows[0]["country"].ToString();

            DataRow[] drowrental = dt.Select("chargestype = 'RENTAL'", "");
            foreach (var item in drowrental)
            {
                ads.RENTAL = (item["CURRENCY"].ToString() == "?" ? "INR" : item["CURRENCY"].ToString()) + " " + item["value"].ToString();
            }

            DataRow[] drowvalidity = dt.Select("chargestype = 'VALIDITY'", "");
            foreach (var item in drowvalidity)
            {
                ads.VALIDITY = item["value"] + " " + "DAYS";
            }

            DataRow[] drowfreeval = dt.Select("chargestype = 'FREE VALUE'", "");
            foreach (var item in drowfreeval)
            {
                ads.FREEVALUE = (item["CURRENCY"].ToString() == "?" ? "INR" : item["CURRENCY"].ToString()) + " " + item["value"].ToString();
            }

            DataRow[] drowincoming = dt.Select("chargestype = 'INCOMING CALL'", "");
            foreach (var item in drowincoming)
            {
                ads.INCOMING = (item["CURRENCY"].ToString() == "?" ? "INR" : item["CURRENCY"].ToString()) + " " + item["value"].ToString();
            }

            DataRow[] drowlocaloutgoing = dt.Select("chargestype = 'LOCAL OUTGOING CALL'", "");
            foreach (var item in drowlocaloutgoing)
            {
                ads.LOCALOUTGOING = (item["CURRENCY"].ToString() == "?" ? "INR" : item["CURRENCY"].ToString()) + " " + item["value"].ToString();
                ads.CURRENCY = item["CURRENCY"].ToString() == "?" ? "INR" : item["CURRENCY"].ToString();
            }

            DataRow[] drowintcallindia = dt.Select("chargestype = 'INDIA OUTGOING CALL'", "");
            foreach (var item in drowintcallindia)
            {
                ads.INTCALL = (item["CURRENCY"].ToString() == "?" ? "INR" : item["CURRENCY"].ToString()) + " " + item["value"].ToString();
            }
            DataRow[] drowlocalsms = dt.Select("chargestype = 'LOCAL SMS'", "");
            foreach (var item in drowlocalsms)
            {
                ads.LOCALSMS = (item["CURRENCY"].ToString() == "?" ? "INR" : item["CURRENCY"].ToString()) + " " + item["value"].ToString();
            }
            DataRow[] drowintsms = dt.Select("chargestype = 'INTL SMS'", "");
            foreach (var item in drowintsms)
            {
               // ads.INTSMS = item["value"].ToString();
                ads.INTSMS = (item["CURRENCY"].ToString() == "?" ? "INR" : item["CURRENCY"].ToString()) + " " + item["value"].ToString();
            }

            DataRow[] drowcug = dt.Select("chargestype = 'CUG CALLING'", "");
            foreach (var item in drowcug)
            {
               // ads.CUG = item["value"].ToString();
                ads.CUG = (item["CURRENCY"].ToString() == "?" ? "INR" : item["CURRENCY"].ToString()) + " " + item["value"].ToString();
            }

            DataRow[] drowdatause = dt.Select("chargestype = 'DATA PAY PER USE'", "");
            foreach (var item in drowdatause)
            {
               // ads.DATAPER = item["A"].ToString();
                ads.DATAPER = (item["CURRENCY"].ToString() == "?" ? "INR" : item["CURRENCY"].ToString()) + " " + item["value"].ToString();
            }
            DataRow[] drowboltans = dt.Select("chargestype = 'BOLTONS'", "");
            foreach (var item in drowboltans)
            {
                ads.Add(new ADD_TARIFFCLS1
                {
                    DATANAME = item["A"].ToString(),
                    DATAVALIDITY = item["Validity_Days"].ToString(),
                    DATAUSE = (item["CURRENCY"].ToString() == "?" ? "INR" : item["CURRENCY"].ToString()) + " " + item["value"].ToString()
                });

            }

            System.Web.HttpContext.Current.Session["dexcel"] = ads;
        }