public async Task <IActionResult> Search(OwnerDriversViewModel driversViewModel)
        {
            string userId = HttpContext.Session.GetString("CurrentUserId");

            if (string.IsNullOrEmpty(userId))
            {
                return(RedirectToAction("Login", "Users"));
            }



            var keyword = driversViewModel.searchModel?.keyword;

            var drivers = GetAllDrivers();

            if (!string.IsNullOrEmpty(keyword))
            {
                var ogdrivers = _context.Driver.Where(x => (x.FirstName + " " + x.LastName).Contains(keyword)).ToList();
                drivers = MapDriverToViewModels(ogdrivers);
            }

            foreach (var d in drivers)
            {
                d.IsActive = drivers.FirstOrDefault(x => d.DriverId == x.DriverId)?.IsActive ?? false;
            }

            driversViewModel.AllDrivers = drivers;

            await _context.SaveChangesAsync();

            return(View("Index", driversViewModel));
        }
        private OwnerDriversViewModel Load(string userId)
        {
            Owner owner = _context.Owner.FirstOrDefault(x => x.UserId == int.Parse(userId));

            IEnumerable <DriverViewModel> allDrivers = GetAllDrivers();
            IEnumerable <DriverViewModel> drivers    = GetDrivers(owner.OwnerId);

            OwnerDriversViewModel model = new OwnerDriversViewModel();

            model.AllDrivers     = allDrivers;
            model.CurrentDrivers = drivers;
            model.EditDriver     = new DriverViewModel();
            model.searchModel    = new SearchViewModel();
            return(model);
        }
Ejemplo n.º 3
0
        // GET: Owners
        public async Task <IActionResult> Index()
        {
            OwnerDriversViewModel view = new OwnerDriversViewModel();
            var schedules = _context.Schedule.Include(s => s.Driver).Where(x => ((x.ShiftTime.Date >= DateTime.Now) && x.ShiftTime.Date <= DateTime.Now.AddDays(1)) || (x.ShiftTimeEnd >= DateTime.Now && x.ShiftTimeEnd <= DateTime.Now.AddDays(1)));

            //var drivers = _context.Driver.ToList();
            view.TodaysSchedule = new List <ScheduleViewModel>();
            var listofschedules = new List <ScheduleViewModel>();

            if (schedules != null && schedules.Any())
            {
                foreach (var sched in schedules)
                {
                    //  sched.Driver = drivers.FirstOrDefault(x=>x.DriverId == sched.DriverId);
                    var newSchedule = convertToScheduleViewModel(sched);
                    listofschedules.Add(newSchedule);
                }

                view.TodaysSchedule = listofschedules;
            }

            //graph data
            var earnings = _context.Earnings.ToList();

            var a = earnings?.Where(x => x.ShiftDate.Month == 1).Sum(x => x.IncomeEarned) ?? 0;

            List <DataPoint> dataPoints1 = new List <DataPoint>();

            dataPoints1.Add(new DataPoint("Jan", earnings?.Where(x => x.ShiftDate.Month == 1 && x.ShiftDate.Year == DateTime.Now.Year).Sum(x => x.IncomeEarned) ?? 0));
            dataPoints1.Add(new DataPoint("Feb", earnings?.Where(x => x.ShiftDate.Month == 2 && x.ShiftDate.Year == DateTime.Now.Year).Sum(x => x.IncomeEarned) ?? 0));
            dataPoints1.Add(new DataPoint("Mar", earnings?.Where(x => x.ShiftDate.Month == 3 && x.ShiftDate.Year == DateTime.Now.Year).Sum(x => x.IncomeEarned) ?? 0));
            dataPoints1.Add(new DataPoint("Apr", earnings?.Where(x => x.ShiftDate.Month == 4 && x.ShiftDate.Year == DateTime.Now.Year).Sum(x => x.IncomeEarned) ?? 0));
            dataPoints1.Add(new DataPoint("May", earnings?.Where(x => x.ShiftDate.Month == 5 && x.ShiftDate.Year == DateTime.Now.Year).Sum(x => x.IncomeEarned) ?? 0));
            dataPoints1.Add(new DataPoint("Jun", earnings?.Where(x => x.ShiftDate.Month == 6 && x.ShiftDate.Year == DateTime.Now.Year).Sum(x => x.IncomeEarned) ?? 0));
            dataPoints1.Add(new DataPoint("Jul", earnings?.Where(x => x.ShiftDate.Month == 7 && x.ShiftDate.Year == DateTime.Now.Year).Sum(x => x.IncomeEarned) ?? 0));
            dataPoints1.Add(new DataPoint("Aug", earnings?.Where(x => x.ShiftDate.Month == 8 && x.ShiftDate.Year == DateTime.Now.Year).Sum(x => x.IncomeEarned) ?? 0));
            dataPoints1.Add(new DataPoint("Sep", earnings?.Where(x => x.ShiftDate.Month == 9 && x.ShiftDate.Year == DateTime.Now.Year).Sum(x => x.IncomeEarned) ?? 0));
            dataPoints1.Add(new DataPoint("Oct", earnings?.Where(x => x.ShiftDate.Month == 10 && x.ShiftDate.Year == DateTime.Now.Year).Sum(x => x.IncomeEarned) ?? 0));
            dataPoints1.Add(new DataPoint("Nov", earnings?.Where(x => x.ShiftDate.Month == 11 && x.ShiftDate.Year == DateTime.Now.Year).Sum(x => x.IncomeEarned) ?? 0));
            dataPoints1.Add(new DataPoint("Dec", earnings?.Where(x => x.ShiftDate.Month == 12 && x.ShiftDate.Year == DateTime.Now.Year).Sum(x => x.IncomeEarned) ?? 0));
            ViewBag.DataPoints1 = JsonConvert.SerializeObject(dataPoints1);

            return(View(view));
        }
        public async Task <IActionResult> SaveDriver(OwnerDriversViewModel owner)
        {
            if (owner?.EditDriver?.DriverId == 0)
            {
                return(RedirectToAction("Index", "Drivers"));
            }

            Driver currentDriver = _context.Driver.Include(x => x.User).FirstOrDefault(x => x.DriverId == owner.EditDriver.DriverId);

            string userId = HttpContext.Session.GetString("CurrentUserId");

            if (string.IsNullOrEmpty(userId))
            {
                return(RedirectToAction("Login", "Users"));
            }


            if (owner.EditDriver.Email != currentDriver.Email)
            {
                var emailExists = _context.Users.Any(x => x.Email == owner.EditDriver.Email);
                if (emailExists)
                {
                    owner.Error = "Driver's email cannot be updated";
                    return(View("Index", owner));
                }
            }

            var currentOwner = _context.Owner.FirstOrDefault(m => m.UserId == int.Parse(userId));
            var existsInOD   = _context.OwnerDriver.Any(x => x.DriverId == owner.EditDriver.DriverId && x.OwnerId == currentOwner.OwnerId);

            Driver upadteDriver = MapViewModelToDriver(owner.EditDriver, currentDriver);

            upadteDriver.User.Email = currentDriver.Email;

            _context.Update(upadteDriver);
            _context.SaveChanges();

            return(RedirectToAction("Index", "Drivers"));
        }
        public async Task <IActionResult> AddDriver(OwnerDriversViewModel owner)
        {
            string userId = HttpContext.Session.GetString("CurrentUserId");

            if (string.IsNullOrEmpty(userId))
            {
                return(RedirectToAction("Login", "Users"));
            }
            IEnumerable <DriverViewModel> allDrivers = GetAllDrivers();
            IEnumerable <DriverViewModel> drivers    = GetAllDrivers();

            foreach (var d in allDrivers)
            {
                d.IsActive = drivers.FirstOrDefault(x => d.DriverId == x.DriverId)?.IsActive ?? false;
            }

            owner.CurrentDrivers = drivers;
            owner.searchModel    = new SearchViewModel();


            var emailExists    = _context.Driver.Any(x => x.Email == owner.AddDriver.Email);
            var usernameExists = _context.Driver.Include(x => x.User).Any(x => x.User.UserName == owner.AddDriver.UserName);

            if (emailExists)
            {
                owner.Error = "Email already exists. Driver's email cannot be created";
                return(View("Index", owner));
            }

            if (usernameExists)
            {
                owner.Error = "Username already exisits. Driver's email cannot be created";
                return(View("Index", owner));
            }

            Users user = new Users();

            user.UserName  = owner.AddDriver.UserName;
            user.FirstName = owner.AddDriver.FirstName;
            user.LastName  = owner.AddDriver.LastName;
            user.Email     = owner.AddDriver.Email;
            user.Age       = owner.AddDriver.Age;
            var tempPassword = GeneratePwd();

            user.Password = Encryption.Encrypt(tempPassword);
            _context.Users.Add(user);
            _context.SaveChanges();

            Driver driver = new Driver();

            driver.FirstName      = owner.AddDriver.FirstName;
            driver.LastName       = owner.AddDriver.LastName;
            driver.Email          = owner.AddDriver.Email;
            driver.Age            = owner.AddDriver.Age;
            driver.DriversLicense = owner.AddDriver.DriversLicense;
            driver.IsActive       = true;
            driver.UserId         = user.UserId;
            _context.Driver.Add(driver);
            _context.SaveChanges();

            using (MailMessage mail = new MailMessage())
            {
                mail.From = new MailAddress("*****@*****.**");
                mail.To.Add(driver.User.Email);
                mail.Subject = "New driver - Fateh Taxi Management System";
                mail.Body    = "<h1>Fateh Management System - New driver</h1><p>Hi " + driver.FirstName + ",</p><p> Welcome to Fateh Taxi Management System. You account has been created here are your user details.<br>" +
                               "Username:<b>" + user.UserName + "<b> <br> Password: <b>" + tempPassword + "</b></p><p>Please login using this password and remember to reset your password once you login.</p>";
                mail.IsBodyHtml = true;

                using (SmtpClient smtp = new SmtpClient(_config.GetValue <string>("Email:Smtp:Host"), Convert.ToInt32(_config.GetValue <string>("Email:Smtp:Port"))))
                {
                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials           = new NetworkCredential(_config.GetValue <string>("Email:Smtp:Username"), _config.GetValue <string>("Email:Smtp:Password"));
                    smtp.EnableSsl             = true;
                    smtp.Send(mail);
                }
            }

            owner.Status = "An email with a new password has been sent to " + driver.FirstName + " " + driver.LastName + ".";


            return(RedirectToAction("Index", "Drivers"));
        }