Exemplo n.º 1
0
 public bool AllowedToAdd(List<Rental> rentalList, Rental newRental)
 {
     bool OKToAdd = true;
     DateTime Test_period_start = newRental.CheckedOut;
     DateTime Test_period_end = newRental.DueDate;
     foreach (Rental r in rentalList)
     {
         DateTime Base_period_start = r.CheckedOut;
         DateTime Base_period_end = r.DueDate;
         if (TimePeriodOverlap(Base_period_start, Base_period_end, Test_period_start, Test_period_end))
         {
             OKToAdd = false;
         }
     }
     return OKToAdd;
 }
Exemplo n.º 2
0
        public ActionResult Details(FormCollection form)
        {
            var userId = User.Identity.GetUserId();
            DateTime checkedOut = DateTime.Parse(form["txtOut"]);
            DateTime dueDate = DateTime.Parse(form["txtReturn"]);
            ViewBag.CategoryId = form["categoryId"];
            string categoryId = form["categoryId"];

            if (checkedOut == default(DateTime))
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            if (dueDate == default(DateTime))
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            int id = Convert.ToInt16(form["id"]);

            Tool tool = db.Tools.Find(id);
            if (tool == null)
            {
                return HttpNotFound();
            }

            // Check whether checked out more than seven days.
            TimeSpan difference = dueDate - checkedOut;
            if (difference.TotalDays > 7)
            {
                TempData["isError"] = true;
                TempData["Message"] = "Date range greater than 7 days.";
                return RedirectToAction("Details", "Tools", new { id = id });
            }

            // Check to see if conflicts with another rental
            List<Rental> rentalList = (from r in db.Rentals
                                       where r.Tool.Id == id
                                       select r).ToList();
            if (rentalList.Count > 0)
            {
                // determine if already rented by user
                foreach (Rental r in rentalList)
                {
                    if (r.UserID == userId)
                    {
                        TempData["isError"] = true;
                        TempData["Message"] = "Item already checked out by user.";
                        return RedirectToAction("Details", "Tools", new { id = id, categoryId = categoryId });
                    }
                }

                Rental rental = new Rental();
                rental.CheckedOut = checkedOut;
                rental.DueDate = dueDate;
                rental.Tool = tool;
                rental.UserID = userId;
                if (AllowedToAdd(rentalList, rental))
                {
                    tool.IsCheckedOut = true;
                    db.Rentals.Add(rental);
                    db.SaveChanges();
                    db.Entry(tool).State = EntityState.Modified;
                    db.SaveChanges();
                    return View("Checkout", rental);
                }
                else
                {
                    TempData["isError"] = true;
                    TempData["Message"] = "Problem with checkout dates. Please verify no conflicts.";
                    return RedirectToAction("Details", "Tools", new { id = id, categoryId = categoryId });
                }
            }
            else
            {
                List<DateTime> ReservedDates = new List<DateTime>();
                ReservedDates = GetCheckedOutDates(tool.Id);

                foreach (DateTime d in ReservedDates)
                {
                    if (dueDate >= d && checkedOut < d) //overlapping dates
                    {
                        TempData["isError"] = true;
                        TempData["Message"] = "Problem with checkout dates. Please verify no conflicts.";
                        return RedirectToAction("Details", "Tools", new { id = id, categoryId = categoryId });
                    }
                }
                Rental rental = new Rental();
                rental.CheckedOut = checkedOut;
                rental.DueDate = dueDate;
                rental.Tool = tool;
                rental.UserID = userId;
                tool.IsCheckedOut = true;
                db.Rentals.Add(rental);
                db.SaveChanges();
                db.Entry(tool).State = EntityState.Modified;
                db.SaveChanges();
                return View("Checkout", rental);
            }

            /*List<DateTime> ReservedDates = new List<DateTime>();
            ReservedDates = GetCheckedOutDates(tool.Id);

            foreach (DateTime d in ReservedDates)
            {
                if (dueDate >= d && checkedOut < d) //overlapping dates
                {
                    DetailViewModel dd = CreateDetailviewModel(tool, d);
                    dd.ReservedDates = ReservedDates;
                    dd.TriggerOnLoad = true;
                    dd.TriggerOnLoadMessage = "Conflict with another reservation. Please choose new dates.";
                    dd.RedirectUrl = "Details";
                    return View(dd);
                }
            }

            try
            { // no overlap
                rental.CheckedOut = checkedOut;
                rental.DueDate = dueDate;
                rental.UserID = userId;
                rental.Tool = tool;
                tool.IsCheckedOut = true;
                db.Rentals.Add(rental);
                db.SaveChanges();
                db.Entry(tool).State = EntityState.Modified;
                db.SaveChanges();

                //send email
            }
            catch
            {
                DateTime due = ReservedDates.FirstOrDefault();
                DetailViewModel dd = CreateDetailviewModel(tool, due);
                dd.ReservedDates = ReservedDates;
                dd.TriggerOnLoad = true;
                dd.TriggerOnLoadMessage = "There was a problem creating this request. Please contact teh administrator.";
                dd.RedirectUrl = "Details";
                return View(dd);
            };

            //return view model
            DetailViewModel dt = CreateDetailviewModel(tool, dueDate);
            for (var newCheckedOutDates = rental.CheckedOut; newCheckedOutDates <= rental.DueDate; newCheckedOutDates = newCheckedOutDates.AddDays(1))
            {
                ReservedDates.Add(newCheckedOutDates);
            }
            ViewBag.CategoryId = ViewBag.CategoryId;
            dt.ReservedDates = ReservedDates;
            dt.TriggerOnLoad = true;
            dt.TriggerOnLoadMessage = "Item checked out successfully!";
            dt.RedirectUrl = "Index";

            return View(dt);*/
        }