public void ShouldReturnErrorIfDelVmIsWrong()
        {
            //var routes = new List<NewRouteVm>() { new Route { Id = 1, CreatedDateTime = DateTime.Now, DelegationId = 1, RouteTypeId = 1, TypeOfTransportId = 1,
            //    RouteDetail = new RouteDetail() { Id = 1, RouteRef = 1, VehicleId = 2, StartDate = DateTime.Now, EndDate = DateTime.Now,
            //        StartPoint = "start", EndPoint = "end", Kilometers = 35 } } };
            //var costs = new List<NewCostVm>() { new Cost() { Id = 1, Amount = 300, CostTypeId = 1, DelegationId = 1 }, new Cost() { Id = 2, Amount = 100, DelegationId = 1, CostTypeId = 2 } };
            var delegation = new NewDelegationVm()
            {
                DelegationStatusId = 1,
                Purpose            = "xxx",
                EmployeeId         = 1,
                DestinationId      = 2

                                     //CreatedDateTime = DateTime.Now,
                                     //DelegationStatusId = 1,
                                     //DelegationStatus = new DelegationStatus() { Id = 1, Name = "open" },
                                     //DestinationId = 1,
                                     //Destination = new Destination() { Id = 1, Name = "testDestination" },
                                     //EmployeeId = 1,
                                     //Employee = new Employee() { Id = 1, FirstName = "testName", LastName = "testSurname" },
                                     //Routes = routes,
                                     //Costs = costs
            };

            var config = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MappingProfile());
            });
            var mapper  = config.CreateMapper();
            var vehRepo = new Mock <IVehicleRepository>();
            var empRepo = new Mock <IEmployeeRepository>();
            var delRepo = new Mock <IDelegationRepository>();
            //delRepo.Setup(d => d.AddDelegation(delegation)).Returns(0);
            var delServ = new DelegationService(delRepo.Object, empRepo.Object, vehRepo.Object, mapper);

            var delResult = delServ.AddDelegation(delegation);
        }
        public ActionResult AppointDelegation([Bind(Include = "DelegationId,EmployeeId,StartDate,EndDate")] Delegation delegation, string sessionId)
        {
            ViewData["sessionId"] = sessionId;
            int empId = (int)Session["empId"];

            ViewData["Role"] = db.Employees.Where(r => r.EmployeeId == empId).Select(r => r.Role).SingleOrDefault();
            DelegationService ds      = new DelegationService();
            List <string>     msglist = new List <string>();

            if (ModelState.IsValid)
            {
                var deptId = db.Employees.Where(r => r.EmployeeId == empId).Select(r => r.Department.DeptId).SingleOrDefault();
                ViewBag.EmployeeId = new SelectList(db.Employees.Where(d => d.Role == "DEP_STAFF" && d.DeptId == deptId), "EmployeeId", "EmployeeName");
                int value = DateTime.Compare(delegation.StartDate, delegation.EndDate);
                if (value > 0 || delegation.StartDate < DateTime.Today)
                {
                    if (value > 0)
                    {
                        msglist.Add("Enddate must follow Startdate");
                    }
                    else if (delegation.StartDate < DateTime.Today)
                    {
                        msglist.Add("Select date preceeds today's date");
                    }
                    ViewData["list"] = msglist;
                    return(View("AppointDelegation"));
                }
                else
                {
                    var case1 = db.Delegations.Include(d => d.Employee).Where(d => d.StartDate >= delegation.StartDate && d.EndDate >= delegation.EndDate && d.Employee.DeptId == deptId).ToList();
                    var case2 = db.Delegations.Include(d => d.Employee).Where(d => d.StartDate <= delegation.StartDate && d.EndDate <= delegation.EndDate && d.Employee.DeptId == deptId).ToList();
                    var case3 = db.Delegations.Include(d => d.Employee).Where(d => d.StartDate >= delegation.StartDate && d.EndDate <= delegation.EndDate && d.Employee.DeptId == deptId).ToList();
                    var case4 = db.Delegations.Include(d => d.Employee).Where(d => d.StartDate <delegation.StartDate && d.EndDate> delegation.EndDate && d.Employee.DeptId == deptId).ToList();

                    if (case1.Count == 0 && case2.Count == 0 && case3.Count == 0 && case4.Count == 0)
                    {
                        ds.AddDelegation(delegation, empId);
                        TempData["toastmessage"] = "Delegation Appointment Successful";
                        return(RedirectToAction("ViewDelegation", new { sessionId = sessionId }));
                    }
                    else
                    {
                        if (case3.Count != 0)
                        {
                            foreach (var item in case3)
                            {
                                List <string> msg = ds.CaptureErrorMsg(item);
                                msglist.Add(msg[0] + " is deleagated from " + msg[1] + " to" + msg[2]);
                            }
                            ViewData["list"] = msglist;
                            return(View("AppointDelegation"));
                        }

                        else if (case4.Count != 0)
                        {
                            foreach (var item in case4)
                            {
                                List <string> msg = ds.CaptureErrorMsg(item);
                                msglist.Add(msg[0] + " is deleagated from " + msg[1] + " to" + msg[2]);
                            }
                            ViewData["list"] = msglist;
                            return(View("AppointDelegation"));
                        }

                        else if (case1.Count != 0)
                        {
                            int flag = 0;
                            foreach (var item in case1)
                            {
                                if (item.StartDate > delegation.EndDate)
                                {
                                    flag++;
                                }
                                else
                                {
                                    List <string> msg = ds.CaptureErrorMsg(item);
                                    msglist.Add(msg[0] + " is deleagated from " + msg[1] + " to" + msg[2]);
                                }
                            }
                            if (flag == case1.Count && case2.Count == 0)
                            {
                                ds.AddDelegation(delegation, empId);
                                TempData["toastmessage"] = "Delegation Appointment Successful";
                                return(RedirectToAction("ViewDelegation", new { sessionId = sessionId }));
                            }
                            else if (flag == case1.Count && case2.Count != 0)
                            {
                                int flag1 = 0;
                                foreach (var item in case2)
                                {
                                    if (item.EndDate < delegation.StartDate)
                                    {
                                        flag1++;
                                    }
                                    else
                                    {
                                        List <string> msg = ds.CaptureErrorMsg(item);
                                        msglist.Add(msg[0] + " is  deleagated from " + msg[1] + " to" + msg[2]);
                                    }
                                }
                                if (flag1 == case2.Count)
                                {
                                    ds.AddDelegation(delegation, empId);
                                    TempData["toastmessage"] = "Delegation Appointment Successful";
                                    return(RedirectToAction("ViewDelegation", new { sessionId = sessionId }));
                                }
                                else
                                {
                                    ViewData["list"] = msglist;
                                    return(View("AppointDelegation"));
                                }
                            }
                            else
                            {
                                ViewData["list"] = msglist;
                                return(View("AppointDelegation"));
                            }
                        }

                        else if (case2.Count != 0)
                        {
                            int flag = 0;
                            foreach (var item in case2)
                            {
                                if (item.EndDate < delegation.StartDate)
                                {
                                    flag++;
                                }
                                else
                                {
                                    List <string> msg = ds.CaptureErrorMsg(item);
                                    msglist.Add(msg[0] + " is  deleagated from " + msg[1] + " to" + msg[2]);
                                }
                            }
                            if (flag == case2.Count)
                            {
                                ds.AddDelegation(delegation, empId);
                                TempData["toastmessage"] = "Delegation Appointment Successful";
                                return(RedirectToAction("ViewDelegation", new { sessionId = sessionId }));
                            }
                            else
                            {
                                ViewData["list"] = msglist;
                                return(View("AppointDelegation"));
                            }
                        }
                    }
                }
            }
            ViewBag.EmployeeId = new SelectList(db.Employees, "EmployeeId", "EmployeeName", delegation.EmployeeId);
            return(View(delegation));
        }