// GET: Bnds/Details/5
        public ActionResult Details(int?id)
        {
            if (Session["ID"] == null)
            {
                return(RedirectToAction("Index", "Home"));
            }
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Bnd bnd = db.Bnds.Find(id);

            if (bnd == null)
            {
                return(HttpNotFound());
            }
            var typeName = (string)Session["Type"];
            var type     = db.EmployeeTypes.Where(x => x.Type == typeName).FirstOrDefault();

            if (type.SeeAll == true)
            {
                ViewBag.TitleSideBar = "Bnds";
                return(View(bnd));
            }
            return(RedirectToAction("Default", "Home"));
        }
        public ActionResult Edit(Bnd bnd)
        {
            if (Session["ID"] == null)
            {
                return(RedirectToAction("Index", "Home"));
            }
            var typeName = (string)Session["Type"];
            var type     = db.EmployeeTypes.Where(x => x.Type == typeName).FirstOrDefault();

            if (type.Finance == true)
            {
                if (bnd.Month13Share + bnd.Month10Share + bnd.Month11Share + bnd.Month12Share + bnd.Month1Share + bnd.Month2Share + bnd.Month3Share + bnd.Month4Share + bnd.Month5Share + bnd.Month6Share + bnd.Month7Share + bnd.Month8Share + bnd.Month9Share != bnd.TotalNum)
                {
                    ViewBag.error        = "الرجاء التأكد من تساوي مجموع ميزانيات الاشهر مع الميزانية العامة وأن جميع الاشهر قد تم تعبأتها بقيمة من الصفر او اعلى";
                    ViewBag.TitleSideBar = "Bnds";

                    return(View(bnd));
                }
                if (ModelState.IsValid)
                {
                    db.Entry(bnd).State = EntityState.Modified;
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                ViewBag.TitleSideBar = "Bnds";
                return(View(bnd));
            }
            return(RedirectToAction("Default", "Home"));
        }
        public ActionResult DeleteConfirmed(int id)
        {
            if (Session["ID"] == null)
            {
                return(RedirectToAction("Index", "Home"));
            }
            var typeName = (string)Session["Type"];
            var type     = db.EmployeeTypes.Where(x => x.Type == typeName).FirstOrDefault();

            if (type.Finance == true)
            {
                Bnd bnd = db.Bnds.Find(id);
                db.Bnds.Remove(bnd);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            return(RedirectToAction("Default", "Home"));
        }
        public ActionResult Create(/*[Bind(Include = "id,Name,TotalNum,Month1Share,Month2Share,Month3Share,Month4Share,Month5Share,Month6Share,Month7Share,Month8Share,Month9Share,Month10Share,Month11Share,Month12Share")]*/ Bnd bnd)
        {
            if (Session["ID"] == null)
            {
                return(RedirectToAction("Index", "Home"));
            }
            var typeName = (string)Session["Type"];
            var type     = db.EmployeeTypes.Where(x => x.Type == typeName).FirstOrDefault();

            if (type.Finance == true)
            {
                bnd.TotalNum          = bnd.NumberOfUnits * bnd.PerUnitCost * bnd.PeriodOnMonth;
                bnd.AfterReductionNum = bnd.TotalNum;
                double MonthsNum = (double)(bnd.Month10Share + bnd.Month11Share + bnd.Month12Share + bnd.Month13Share + bnd.Month1Share + bnd.Month2Share + bnd.Month3Share + bnd.Month4Share + bnd.Month5Share + bnd.Month6Share + bnd.Month7Share + bnd.Month8Share + bnd.Month9Share);
                if (MonthsNum != bnd.TotalNum)
                {
                    ViewBag.error        = "الرجاء التأكد من تساوي مجموع ميزانيات الاشهر مع الميزانية العامة";
                    ViewBag.TitleSideBar = "Bnds";
                    return(View(bnd));
                }

                try
                {
                    bnd.id = db.Bnds.OrderByDescending(x => x.id).FirstOrDefault().id + 1;
                }
                catch (Exception)
                {
                    bnd.id = 1;
                }

                if (ModelState.IsValid)
                {
                    db.Bnds.Add(bnd);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                ViewBag.TitleSideBar = "Bnds";

                return(View(bnd));
            }
            return(RedirectToAction("Default", "Home"));
        }
        public ActionResult ImportBnds(HttpPostedFileBase file)
        {
            if (Session["ID"] == null)
            {
                return(RedirectToAction("Index", "Home"));
            }
            DataSet ds = new DataSet();

            if (Request.Files["file"].ContentLength > 0)
            {
                string fileExtension = System.IO.Path.GetExtension(Request.Files["file"].FileName);
                if (fileExtension == ".xls" || fileExtension == ".xlsx")
                {
                    string fileLocation = Server.MapPath("~/App_Data/Test/" + Request.Files["file"].FileName);
                    if (System.IO.File.Exists(fileLocation))
                    {
                        System.IO.File.Delete(fileLocation);
                    }
                    Request.Files["file"].SaveAs(fileLocation);
                    string excelConnectionString = string.Empty;
                    excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
                    //connection String for xls file format.
                    if (fileExtension == ".xls")
                    {
                        excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
                    }
                    //connection String for xlsx file format.
                    else if (fileExtension == ".xlsx")
                    {
                        excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
                    }
                    //Create Connection to Excel work book and add oledb namespace
                    OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
                    excelConnection.Open();
                    DataTable dt = new DataTable();

                    dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    if (dt == null)
                    {
                        return(null);
                    }
                    String[] excelSheets = new String[dt.Rows.Count];
                    int      t           = 0;
                    //excel data saves in temp file here.
                    foreach (DataRow row in dt.Rows)
                    {
                        excelSheets[t] = row["TABLE_NAME"].ToString();
                        t++;
                    }
                    OleDbConnection excelConnection1 = new OleDbConnection(excelConnectionString);
                    string          query            = string.Format("Select * from [{0}]", excelSheets[0]);
                    using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1))
                    {
                        dataAdapter.Fill(ds);
                    }

                    excelConnection.Close();
                    excelConnection1.Close();
                }
                if (fileExtension.ToString().ToLower().Equals(".xml"))
                {
                    string fileLocation = Server.MapPath("~/Test/") + Request.Files["FileUpload"].FileName;
                    if (System.IO.File.Exists(fileLocation))
                    {
                        System.IO.File.Delete(fileLocation);
                    }

                    Request.Files["FileUpload"].SaveAs(fileLocation);
                    XmlTextReader xmlreader = new XmlTextReader(fileLocation);
                    // DataSet ds = new DataSet();
                    ds.ReadXml(xmlreader);
                    xmlreader.Close();
                }

                int counter;
                try
                {
                    counter = db.Bnds.OrderByDescending(x => x.id).FirstOrDefault().id + 1;
                }
                catch (Exception)
                {
                    counter = 1;
                }

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    Bnd bnd = new Bnd();
                    bnd.id = counter;
                    counter++;
                    bnd.Simbol         = ds.Tables[0].Rows[i][0].ToString();
                    bnd.BudgetLineItem = ds.Tables[0].Rows[i][1].ToString();
                    bnd.Name           = ds.Tables[0].Rows[i][2].ToString();
                    bnd.Unit           = ds.Tables[0].Rows[i][3].ToString();

                    try
                    {
                        if (ds.Tables[0].Rows[i][22].ToString() == "Indirect")
                        {
                            double duble = Convert.ToDouble(ds.Tables[0].Rows[i][4].ToString()) * 100;
                            bnd.NumberOfUnits = (int)duble;
                        }
                        else
                        {
                            double duble = Convert.ToDouble(ds.Tables[0].Rows[i][4].ToString()) * 100;
                            bnd.NumberOfUnits = (int)duble;
                        }
                    }
                    catch
                    {
                        bnd.NumberOfUnits = null;
                    }
                    try
                    {
                        bnd.PerUnitCost = Convert.ToDouble(ds.Tables[0].Rows[i][5].ToString());
                    }
                    catch
                    {
                        bnd.PerUnitCost = null;
                    }
                    try
                    {
                        bnd.PeriodOnMonth = Convert.ToDouble(ds.Tables[0].Rows[i][6].ToString());
                    }
                    catch
                    {
                        bnd.PeriodOnMonth = null;
                    }



                    try
                    {
                        bnd.PercentOfCostChargedtoProject = Convert.ToDouble(ds.Tables[0].Rows[i][7].ToString()) * 100;
                    }
                    catch
                    {
                        bnd.PercentOfCostChargedtoProject = null;
                    }
                    try
                    {
                        bnd.TotalNum = Convert.ToDouble(ds.Tables[0].Rows[i][8].ToString());
                    }
                    catch
                    {
                        bnd.TotalNum = null;
                    }
                    try
                    {
                        bnd.AfterReductionNum = Convert.ToDouble(ds.Tables[0].Rows[i][8].ToString());
                    }
                    catch
                    {
                        bnd.AfterReductionNum = null;
                    }

                    try
                    {
                        bnd.Month13Share = Convert.ToDouble(ds.Tables[0].Rows[i][9].ToString());
                    }
                    catch
                    {
                        bnd.Month13Share = null;
                    }
                    try
                    {
                        bnd.Month10Share = Convert.ToDouble(ds.Tables[0].Rows[i][10].ToString());
                    }
                    catch
                    {
                        bnd.Month10Share = null;
                    }
                    try
                    {
                        bnd.Month11Share = Convert.ToDouble(ds.Tables[0].Rows[i][11].ToString());
                    }
                    catch
                    {
                        bnd.Month11Share = null;
                    }
                    try
                    {
                        bnd.Month12Share = Convert.ToDouble(ds.Tables[0].Rows[i][12].ToString());
                    }
                    catch
                    {
                        bnd.Month12Share = null;
                    }
                    try
                    {
                        bnd.Month1Share = Convert.ToDouble(ds.Tables[0].Rows[i][13].ToString());
                    }
                    catch
                    {
                        bnd.Month1Share = null;
                    }
                    try
                    {
                        bnd.Month2Share = Convert.ToDouble(ds.Tables[0].Rows[i][14].ToString());
                    }
                    catch
                    {
                        bnd.Month2Share = null;
                    }
                    try
                    {
                        bnd.Month3Share = Convert.ToDouble(ds.Tables[0].Rows[i][15].ToString());
                    }
                    catch
                    {
                        bnd.Month3Share = null;
                    }
                    try
                    {
                        bnd.Month4Share = Convert.ToDouble(ds.Tables[0].Rows[i][16].ToString());
                    }
                    catch
                    {
                        bnd.Month4Share = null;
                    }
                    try
                    {
                        bnd.Month5Share = Convert.ToDouble(ds.Tables[0].Rows[i][17].ToString());
                    }
                    catch
                    {
                        bnd.Month5Share = null;
                    }
                    try
                    {
                        bnd.Month6Share = Convert.ToDouble(ds.Tables[0].Rows[i][18].ToString());
                    }
                    catch
                    {
                        bnd.Month6Share = null;
                    }
                    try
                    {
                        bnd.Month7Share = Convert.ToDouble(ds.Tables[0].Rows[i][19].ToString());
                    }
                    catch
                    {
                        bnd.Month7Share = null;
                    }
                    try
                    {
                        bnd.Month8Share = Convert.ToDouble(ds.Tables[0].Rows[i][20].ToString());
                    }
                    catch
                    {
                        bnd.Month8Share = null;
                    }

                    try
                    {
                        bnd.Month9Share = Convert.ToDouble(ds.Tables[0].Rows[i][21].ToString());
                    }
                    catch
                    {
                        bnd.Month9Share = null;
                    }
                    bnd.WhichTable = ds.Tables[0].Rows[i][22].ToString();
                    db.Bnds.Add(bnd);
                    //string conn = ConfigurationManager.ConnectionStrings["Data Source=DESKTOP-N6HJU8V;Initial Catalog=TaalimCopyOnline;Integrated Security=True"].ConnectionString;
                    //SqlConnection con = new SqlConnection(conn);
                    //string query = "Insert into Employees(id,name,surname,BDate,Certificate,CType,SDate,Job,Sex,FathersName,Centerid,Periodid,CityID) Values('" + ds.Tables[0].Rows[i][0].ToString() + "','" + ds.Tables[0].Rows[i][1].ToString() + "','" + ds.Tables[0].Rows[i][2].ToString() + "')";
                    //con.Open();
                    //SqlCommand cmd = new SqlCommand(query, con);
                    //cmd.ExecuteNonQuery();
                    //con.Close();
                }
                try
                {
                    db.Configuration.ValidateOnSaveEnabled = false;
                    db.SaveChanges();
                }
                catch
                {
                    ViewBag.Importerror = "حصل خطأ اثناء الحفظ";
                }
            }
            return(RedirectToAction("Index", "Students"));
        }