// GET: LeavesMass_Transaction

        public ActionResult Create()
        {
            try
            {
                List <SelectListItem> items = new List <SelectListItem>();
                items.Insert(0, (new SelectListItem
                {
                    Text = "All employee",
                    Value = "1",
                }));
                items.Insert(1, (new SelectListItem
                {
                    Text = "unit",
                    Value = "2",
                }));
                items.Insert(2, (new SelectListItem
                {
                    Text = "nationality",
                    Value = "3",
                }));
                items.Insert(3, (new SelectListItem
                {
                    Text = "Work location",
                    Value = "4",
                }));
                items.Insert(4, (new SelectListItem
                {
                    Text = "Cost center",
                    Value = "5",
                }));
                items.Insert(5, (new SelectListItem
                {
                    Text = "Cadre level",
                    Value = "6",
                }));
                ViewBag.Object           = new SelectList(items, "Value", "Text");
                ViewBag.Employee_Profile = dbcontext.Employee_Profile.Where(a => a.Active == true).ToList().Select(m => new { Code = m.Code + "-[" + m.Full_Name + ']', ID = m.ID });
                ViewBag.Vacations_Setup  = dbcontext.Vacations_Setup.ToList().Select(m => new { Code = "" + m.LeaveTypeCode + "-----[" + m.LeaveTypeNameEnglish + ']', ID = m.ID }).ToList();
                var Statis     = DateTime.Now;
                var new_record = new LeavesMass_Transaction {
                    TransactionDate = Statis, Created_Date = Statis
                };
                var stru   = dbcontext.StructureModels.FirstOrDefault(m => m.All_Models == ChModels.Payroll).Structure_Code;
                var model_ = dbcontext.LeavesMass_Transaction.ToList();
                if (model_.Count() == 0)
                {
                    new_record.SerialNo = stru + "1";
                }
                else
                {
                    new_record.SerialNo = stru + (model_.LastOrDefault().ID + 1).ToString();
                }

                return(View(new_record));
            }
            catch (Exception)
            {
                return(RedirectToAction("index"));
            }
        }
        public ActionResult Create(LeavesMass_Transaction model, FormCollection form, string Command)
        {
            try
            {
                List <SelectListItem> items = new List <SelectListItem>();
                items.Insert(0, (new SelectListItem
                {
                    Text = "All employee",
                    Value = "1",
                }));
                items.Insert(1, (new SelectListItem
                {
                    Text = "unit",
                    Value = "2",
                }));
                items.Insert(2, (new SelectListItem
                {
                    Text = "nationality",
                    Value = "3",
                }));
                items.Insert(3, (new SelectListItem
                {
                    Text = "Work location",
                    Value = "4",
                }));
                items.Insert(4, (new SelectListItem
                {
                    Text = "Cost center",
                    Value = "5",
                }));
                items.Insert(5, (new SelectListItem
                {
                    Text = "Cadre level",
                    Value = "6",
                }));
                ViewBag.Object           = new SelectList(items, "Value", "Text");
                ViewBag.Employee_Profile = dbcontext.Employee_Profile.Where(a => a.Active == true).ToList().Select(m => new { Code = m.Code + "-[" + m.Full_Name + ']', ID = m.ID });
                ViewBag.Vacations_Setup  = dbcontext.Vacations_Setup.ToList().Select(m => new { Code = "" + m.LeaveTypeCode + "-----[" + m.LeaveTypeNameEnglish + ']', ID = m.ID }).ToList();

                var model_          = dbcontext.LeavesMass_Transaction.ToList();
                var TransactionCode = "0";
                if (model_.Count() == 0)
                {
                    TransactionCode = "1";
                }
                else
                {
                    TransactionCode = (model_.LastOrDefault().ID + 1).ToString();
                }

                var ID_emp      = form["ID_emp"].Split(',');
                var CodeEmp     = form["CodeEmp"].Split(',');
                var NameEmp     = form["NameEmp"].Split(',');
                var CodeVac     = form["CodeVac"].Split(',');
                var NameVac     = form["NameVac"].Split(',');
                var BalDay      = form["BalDay"].Split(',');
                var TotalDay    = form["TotalDay"].Split(',');
                var Reason      = form["Reason"].Split(',');
                var FromBalance = form["FromBalance"].Split(',');

                for (var i = 0; i < CodeEmp.Length; i++)
                {
                    if (CodeEmp[i] != "")
                    {
                        LeavesMass_Transaction new_Record = new LeavesMass_Transaction();

                        var LeavesRequestMaster = dbcontext.LeavesRequestMaster.ToList();
                        var stru   = dbcontext.StructureModels.FirstOrDefault(m => m.All_Models == ChModels.Personnel).Structure_Code;
                        var model2 = dbcontext.LeavesMass_Transaction.ToList();
                        if (model2.Count() == 0)
                        {
                            new_Record.SerialNo = stru + "1";
                        }
                        else
                        {
                            new_Record.SerialNo = stru + (model2.LastOrDefault().ID + 1).ToString();
                        }
                        new_Record.TransactionCode = TransactionCode;
                        new_Record.TransactionDate = DateTime.Now;
                        new_Record.Created_By      = User.Identity.Name;
                        new_Record.Created_Date    = DateTime.Now.Date;

                        var new_details = new LeavesRequestMaster();
                        if (LeavesRequestMaster.Count() == 0)
                        {
                            new_details.SerialNo = stru + "1";
                        }
                        else
                        {
                            new_details.SerialNo = stru + (LeavesRequestMaster.LastOrDefault().ID + 1).ToString();
                        }
                        var codeva = CodeVac[i];
                        var codeem = CodeEmp[i];

                        var emp       = dbcontext.Employee_Profile.FirstOrDefault(a => a.Code == codeem);
                        var Vacations = dbcontext.Vacations_Setup.FirstOrDefault(a => a.LeaveTypeCode == codeva);

                        var      Balance  = double.Parse(BalDay[i]);
                        var      Total    = double.Parse(TotalDay[i]);
                        DateTime?year     = DateTime.Parse(FromBalance[1]);
                        int      FBalance = year.Value.Year;
                        new_details.year = FBalance;
                        DateTime?dateOrNull = model.TransactionDate;
                        if (dateOrNull != null)
                        {
                            new_details.DateFrom = dateOrNull.Value;
                        }
                        DateTime?dateOrNull2 = model.Created_Date;
                        if (dateOrNull2 != null)
                        {
                            new_details.DateTo = dateOrNull2.Value;
                        }
                        new_details.EmployeeID = emp.ID;
                        new_details.VacCode    = Vacations.ID;
                        //new_details.DateFrom = model.TransactionDate;
                        //new_details.DateTo = model.Created_Date;
                        new_details.CurrentDate  = DateTime.Now.Date;
                        new_details.ActualToDate = model.Created_Date;
                        new_details.Reason       = Reason[i];
                        new_details.Approved     = check_status.Approved.GetHashCode();
                        new_details.BalanceDays  = Balance;
                        new_details.TotalDays    = Total;
                        new_details.RemainDays   = Balance - Total;
                        new_details.Created_By   = User.Identity.Name;
                        new_details.Created_Date = DateTime.Now.Date;
                        new_details.check_status = HR.Models.Infra.check_status.Approved;
                        new_details.name_state   = nameof(check_status.Approved);
                        var username = User.Identity.GetUserName();
                        var Date     = Convert.ToDateTime("1/1/1900");
                        var s        = new status {
                            statu = HR.Models.Infra.check_status.created, created_by = username, Type = Models.Infra.Type.LeavesRequestMaster, approved_bydate = Date, cancaled_bydate = Date, created_bydate = DateTime.Now.Date, Rejected_bydate = Date, return_to_reviewdate = Date
                        };
                        var st = dbcontext.status.Add(s);
                        dbcontext.SaveChanges();
                        new_details.statID = s.ID;

                        var LeavesTransactionBalance = dbcontext.LeavesTransactionBalance.Where(a => a.VacCode == Vacations.ID && a.EmployeeID == emp.ID && a.Year == year.Value.Year && a.Check == true).ToList();
                        var LeavesTransaction        = LeavesTransactionBalance.LastOrDefault();

                        dbcontext.LeavesMass_Transaction.Add(new_Record);
                        dbcontext.SaveChanges();
                        dbcontext.LeavesRequestMaster.Add(new_details);
                        dbcontext.SaveChanges();
                        if (LeavesTransaction != null && LeavesTransaction.Check == true)
                        {
                            LeavesTransaction.Check = false;
                            dbcontext.SaveChanges();
                        }
                    }
                }

                return(RedirectToAction("index", "LeaveRequest"));
            }
            catch (Exception e)
            {
                return(View(model));
            }
        }