private DateTime? CalculateVisitTime(ServiceTicket serviceTicket)
        {
            var time = this.scheduleItemRepository.All.SingleOrDefault(e => e.ServiceTicketID == serviceTicket.ID);

            if (time == null) return null;

            var number = new Random((int)DateTime.Now.Ticks).Next(0, 30);
            return time.Start.AddMinutes(number % 2 == 0 ? number : -1 * number);
        }
 public void InsertOrUpdate(ServiceTicket serviceticket)
 {
     if (serviceticket.ID == default(int))
     {
         this.context.ServiceTickets.Add(serviceticket);
     }
     else
     {
         this.context.Entry(serviceticket).State = EntityState.Modified;
     }
 }
        public void ScheduleActionCorrectlyUpdatesRepositories()
        {
            // Arrange
            var scheduleItems = new List<ScheduleItem>();
            scheduleItems.Add(new ScheduleItem { ServiceTicketID = 1 });
            mockScheduleItemRepo.All.Returns(scheduleItems.AsQueryable<ScheduleItem>());
         
            ServiceTicket ticket = new ServiceTicket { ID = 0 };
            mockServiceTicketRepo.Find(1).Returns(ticket);

            // Act
            controller.AssignSchedule(1, 101, 0);

            // Assert
            Assert.AreEqual(101, ticket.AssignedToID);
            mockScheduleItemRepo.ReceivedCalls();
            mockServiceTicketRepo.ReceivedCalls();
        }
        public void ScheduleActionCorrectlyUpdatesRepositories()
        {
            this.SetupController();

            // Arrange
            var scheduleItems = new List<ScheduleItem>();
            scheduleItems.Add(new ScheduleItem { ServiceTicketID = 1 });
            //mockScheduleItemRepo.SetReturnValue("get_All", scheduleItems.AsQueryable<ScheduleItem>());
            mockScheduleItemRepo.SetReturnValue("InsertOrUpdate", null);
            mockScheduleItemRepo.SetReturnValue("Delete", null);
            mockScheduleItemRepo.ExpectAndReturn("Save", null);

            ServiceTicket ticket = new ServiceTicket { ID = 0 };
            mockServiceTicketRepo.SetReturnValue("Find", ticket);
            mockServiceTicketRepo.ExpectAndReturn("Save", null);

            // Act
            controller.AssignSchedule(1, 101, 0);

            // Assert
            Assert.AreEqual(101, ticket.AssignedToID);
            mockScheduleItemRepo.Verify();
            mockServiceTicketRepo.Verify();
        }
        public ActionResult Create(ServiceTicket serviceticket)
        {
            if (ModelState.IsValid)
            {
                serviceticket.Opened = DateTime.Now;

                var createdBy = this.employeeRepository.All.Where(e => e.Identity == "NORTHAMERICA\\drobbins").FirstOrDefault();

                if (createdBy != null)
                    serviceticket.CreatedByID = createdBy.ID;

                var serviceLogEntry = new ServiceLogEntry
                {
                    ServiceTicket = serviceticket,
                    CreatedAt = DateTime.Now,
                    CreatedBy = serviceticket.CreatedBy,
                    CreatedByID = serviceticket.CreatedByID,
                    Description = "Created",
                };

                this.serviceLogEntryRepository.InsertOrUpdate(serviceLogEntry);
                this.serviceLogEntryRepository.Save();

                return RedirectToAction("Details", new { serviceticket.ID });
            }
            else
            {
                ViewBag.PossibleCustomers = this.customerRepository.All;
                ViewBag.PossibleCreatedBies = this.employeeRepository.All;
                ViewBag.PossibleAssignedToes = this.employeeRepository.All;
                ViewBag.PossibleEscalationLevels = new Dictionary<string, string> { { "1", "Level 1" }, { "2", "Level 2" }, { "3", "Level 3" } };

                var newTicket = new ServiceTicket
                {
                    CreatedBy = this.employeeRepository.All.Where(e => e.Identity == "NORTHAMERICA\\drobbins").FirstOrDefault(),
                };

                return View(newTicket);
            }
        }
        public ActionResult Create()
        {
            ViewBag.PossibleCustomers = this.customerRepository.All;
            ViewBag.PossibleCreatedBies = this.employeeRepository.All;
            ViewBag.PossibleAssignedToes = this.employeeRepository.All;
            ViewBag.PossibleEscalationLevels = new Dictionary<string, string> { { "1", "Level 1" }, { "2", "Level 2" }, { "3", "Level 3" } };

            var newTicket = new ServiceTicket
            {
                CreatedBy = this.employeeRepository.All.Where(e => e.Identity == "NORTHAMERICA\\drobbins").FirstOrDefault(),
            };

            return View(newTicket);
        }
        public ActionResult Edit(ServiceTicket serviceticket)
        {
            if (ModelState.IsValid)
            {
                this.serviceTicketRepository.InsertOrUpdate(serviceticket);
                this.serviceTicketRepository.Save();
                return RedirectToAction("Details", new { id = serviceticket.ID });
            }

            this.ViewBag.PossibleCustomers = this.customerRepository.All;
            this.ViewBag.PossibleCreatedBies = this.employeeRepository.All;
            this.ViewBag.PossibleAssignedToes = this.employeeRepository.All;
            this.ViewBag.PossibleEscalationLevels = new Dictionary<string, string> { { "1", "Level 1" }, { "2", "Level 2" }, { "3", "Level 3" } };

            return this.View();
        }