Example #1
0
        public ActionResult UserAppointmentCreate(string initCharge, string endCharge, string podTypeStr, int stationId)
        {
            var user = db.Users.Find(User.Identity.GetUserId());

            if (user.Appointments.Where(x => x.AppointmentStatusId == AppointmentStatusEnum.Pending ||
                                        x.AppointmentStatusId == AppointmentStatusEnum.Ongoing).Count() > 0)
            {
                TempData["msg"] = "Existe outro agendamento, se pretender, elimine-o e crie um novo.";
                return(RedirectToAction("AppointmentsRecords", "Users", null));
            }

            var      station = db.Stations.Find(stationId);
            var      pods    = station.Pods.Where(p => p.PodType.Name == podTypeStr && p.isActive);
            var      podType = podTypeStr == nameof(PodTypeEnum.Fast) ? PodTypeEnum.Fast : PodTypeEnum.Normal;
            DateTime init;
            DateTime end;

            if (DateTime.TryParseExact(initCharge, "MM/dd/yyyy HH:mm:ss", new CultureInfo("pt-PT", false), DateTimeStyles.None, out init) == false ||
                DateTime.TryParseExact(endCharge, "MM/dd/yyyy HH:mm:ss", new CultureInfo("pt-PT", false), DateTimeStyles.None, out end) == false)
            {
                return(View());
            }

            double cost = PriceGenerator.CalculatePrice(init, end, station.Prices, podType);

            if (cost > user.Wallet)
            {
                TempData["msg"] = "Dinheiro insuficiente, para efectuar agendamento tem de conter: " + cost + "€ .";
                return(RedirectToAction("AddMoney", "Users", null));
            }

            Pod pod = pods.ToList().Find(p => (p.Appointments
                                               .All(a => a.Start > end.AddMinutes(5) ||
                                                    a.End.AddMinutes(5) < init)) ||
                                         p.Appointments.Count == 0);

            return(View(new Appointment()
            {
                Start = init, End = end, Pod = pod, PodId = pod.Id, Cost = cost, Station = station, Company = station.Company, User = user
            }));
        }
Example #2
0
        public ActionResult Create([Bind(Include = "Id,CompanyId,StationId,PodId,UserId,Cost,Start,End,AppointmentStatusId")] Appointment appointment)
        {
            var employee = db.Employees.Find(User.Identity.GetUserId());
            var user     = db.Users.Find(appointment.UserId);

            if (user.Appointments.Where(x => x.AppointmentStatusId == AppointmentStatusEnum.Pending ||
                                        x.AppointmentStatusId == AppointmentStatusEnum.Ongoing).Count() > 0)
            {
                return(RedirectToAction("AppointmentRecords", "Users"));
            }

            var prices = db.Stations.Where(s => s.Id == appointment.StationId).Select(s => s.Prices).FirstOrDefault();

            if (prices.Count == 0)
            {
                return(new HttpStatusCodeResult(404, "Error:" + "The station prices are not estabilished. Appointment will not be created."));
            }

            List <Pod> pods = null;

            if (User.IsInRole(nameof(RoleEnum.Employee)))
            {
                pods = employee.Station.Pods.ToList();
            }
            else
            {
                pods = employee.Company.Stations.Where(x => x.Id == appointment.StationId)
                       .SingleOrDefault().Pods.ToList();
            }

            appointment.PodId = pods.Find(p => p.Appointments
                                          .All(a => a.Start > appointment.End.AddMinutes(5) ||
                                               a.End.AddMinutes(5) < appointment.Start) || p.Appointments.Count == 0).Id;

            appointment.AppointmentStatusId = AppointmentStatusEnum.Pending;

            appointment.CompanyId = employee.CompanyId;
            appointment.Cost      = PriceGenerator.CalculatePrice(appointment.Start, appointment.End, prices, db.Pods.Find(appointment.PodId).PodType.Id);

            if (appointment.Cost > user.Wallet)
            {
                return(new HttpStatusCodeResult(404, "Error:" + "The user don''t have enough money to make the appointment"));
            }

            if (ModelState.IsValid)
            {
                user.Wallet         -= appointment.Cost * 0.15;
                db.Entry(user).State = EntityState.Modified;

                db.Appointments.Add(appointment);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.StationId = new SelectList(db.Stations.Where(s => s.CompanyId == employee.CompanyId), "Id", "ComercialName");

            List <ApplicationUser> users = new List <ApplicationUser>();
            var db_users = db.Users.ToList();

            foreach (ApplicationUser elem in db_users)
            {
                var roleId = elem.Roles.ToList()[0].RoleId;
                var role   = db.Roles.Where(x => x.Id == roleId).FirstOrDefault();

                if (role.Name == nameof(RoleEnum.User))
                {
                    users.Add(elem);
                }
            }
            ViewBag.UserId = new SelectList(users, "Id", "Email"); //TODO: only put users of primary user role
            return(View(appointment));
        }