public void testAssignmentWhenDriverIsOnHoliday() { Driver driverOne = new Driver(); DriverHoliday hols = new DriverHoliday(); hols.From = new DateTime(2012, 12, 12); hols.To = new DateTime(2012, 12, 20); driverOne.Holidays.Add(hols); Route routeOne = new Route(); routeOne.DeliverBy = new DateTime(2012, 12, 17); List <Route> routes = new List <Route> { routeOne }; LocationService locationService = new LocationService(null); DriverAssignmentService assignmentService = new DriverAssignmentService(locationService); RouteAssignment result = assignmentService.assignMultipleRoutes(routes, new List <Driver> { driverOne }); Assert.Equal(1, result.TempRouteData.Count()); Assert.Null(result.TempRouteData.ElementAt(0).Driver); }
public RouteAssignment assignMultipleRoutes(List <Route> routes, List <Driver> drivers) { var tempRouteData = new List <TempRoute>(); List <Driver> alreadyAssignedDriversForADay = new List <Driver>(); List <Driver> driversAlreadyAssignedARouteThisSession = new List <Driver>(); Route prevRoute = null; List <Route> sortedRoutes = routes.OrderByDescending(r => r.DeliverBy).ToList(); foreach (Route route in sortedRoutes) { if (prevRoute != null && !prevRoute.DeliverBy.Equals(route.DeliverBy)) { alreadyAssignedDriversForADay.Clear(); } DriverAssignmentResult assignmentResult = getBestDriverForRoute(route, drivers, alreadyAssignedDriversForADay, driversAlreadyAssignedARouteThisSession, route.DeliverBy); TempRoute parameters = new TempRoute(); parameters.DriversVehicle = assignmentResult.Vehicle; parameters.ModifiedDeliverByDate = assignmentResult.DeliverByDate; parameters.Driver = assignmentResult.Driver; parameters.RouteId = route.ID; alreadyAssignedDriversForADay.Add(assignmentResult.Driver); tempRouteData.Add(parameters); prevRoute = route; driversAlreadyAssignedARouteThisSession.Add(assignmentResult.Driver); } RouteAssignment assignemnt = new RouteAssignment(); assignemnt.Routes = routes; assignemnt.TempRouteData = tempRouteData; return(assignemnt); }
public async Task <IActionResult> AutomaticAssign() { var routesToAssign = company.Routes.Where(r => r.DriverID == null).ToList(); RouteAssignment assignemnt = DriverAssignmentService.assignMultipleRoutes(routesToAssign, company.Team.Drivers.ToList()); TempData["Assignment"] = JsonConvert.SerializeObject(assignemnt); var TempRouteData = assignemnt.TempRouteData; foreach (TempRoute tempRoute in TempRouteData) { Route route = company.Routes.Where(r => r.ID == tempRoute.RouteId).First(); if (tempRoute.Driver != null) { Driver driverEntity = _context.Drivers.Where(d => d.ID == tempRoute.Driver.ID).FirstOrDefault(); route.DriverID = driverEntity.ID; await notificationService.SendAnEmailToDriverAboutAssignedRouteAsync(route, driverEntity.User); } route.DeliveryDate = tempRoute.ModifiedDeliverByDate.AddDays(-1); if (tempRoute.DriversVehicle != null) { route.VehicleID = tempRoute.DriversVehicle.ID; } _context.Routes.Update(route); } await _context.SaveChangesAsync(); return(RedirectToAction("AssignmentResult")); }
public void testAssignMultipleRoutesAllForDifferentDaysWithOneDriver() { Driver driverOne = new Driver(); driverOne.Address = new DriverAddress(); Vehicle vehicleOne = new Vehicle(300, 100, 109, 100); driverOne.Vehicles.Add(vehicleOne); List <Driver> drivers = new List <Driver> { driverOne }; var responseMessageOne = new HttpResponseMessage(); responseMessageOne.Content = new StringContent("{\"destination_addresses\":[\"Village Way, Brighton BN1, United Kingdom\"],\"origin_addresses\":[\"Arts Rd, Falmer, Brighton BN1 9QN, United Kingdom\"],\"rows\":[{\"elements\":[{\"distance\":{\"text\":\"0.8 mi\",\"value\":2},\"duration\":{\"text\":\"4 min\",\"value\":235},\"status\":\"OK\"}]}],\"status\":\"OK\"}"); var responseMessageTwo = new HttpResponseMessage(); responseMessageTwo.Content = new StringContent("{\"destination_addresses\":[\"Village Way, Brighton BN1, United Kingdom\"],\"origin_addresses\":[\"Arts Rd, Falmer, Brighton BN1 9QN, United Kingdom\"],\"rows\":[{\"elements\":[{\"distance\":{\"text\":\"3.6 mi\",\"value\":10},\"duration\":{\"text\":\"4 min\",\"value\":235},\"status\":\"OK\"}]}],\"status\":\"OK\"}"); List <HttpResponseMessage> responses = new List <HttpResponseMessage> { responseMessageOne, responseMessageTwo }; TestGoogleMapsUtil googleMaps = new TestGoogleMapsUtil(responses); Route routeOne = new Route(); routeOne.DeliverBy = new DateTime(2015, 12, 13); routeOne.PickUpAddress = (PickUpAddress)getAddress(false); Delivery delivery = new Delivery(); delivery.ItemWeight = 20; delivery.ItemSize = ItemSize.Small; routeOne.Deliveries.Add(delivery); Route routeTwo = new Route(); routeTwo.DeliverBy = new DateTime(2015, 12, 14); routeTwo.PickUpAddress = (PickUpAddress)getAddress(false); Delivery deliveryTwo = new Delivery(); deliveryTwo.ItemWeight = 20; deliveryTwo.ItemSize = ItemSize.Small; routeTwo.Deliveries.Add(delivery); List <Route> routes = new List <Route> { routeOne, routeTwo }; LocationService locationService = new LocationService(googleMaps); DriverAssignmentService assignmentService = new DriverAssignmentService(locationService); RouteAssignment result = assignmentService.assignMultipleRoutes(routes, drivers); Assert.Equal(2, result.TempRouteData.Count()); Assert.Equal(driverOne, result.TempRouteData.ElementAt(0).Driver); Assert.Equal(driverOne, result.TempRouteData.ElementAt(1).Driver); }
public void testAssignmentWhenDriverIsOnHolidayButDeliveryIsRolledBackACoupleOfDays() { Driver driverOne = new Driver(); DriverHoliday hols = new DriverHoliday(); hols.From = new DateTime(2012, 12, 12); hols.To = new DateTime(2012, 12, 20); driverOne.Holidays.Add(hols); driverOne.Address = new DriverAddress(); Vehicle vehicleOne = new Vehicle(300, 100, 109, 100); driverOne.Vehicles.Add(vehicleOne); Route driverRoute = new Route(); driverRoute.DeliverBy = new DateTime(2015, 12, 12); driverOne.Routes.Add(driverRoute); var responseMessageOne = new HttpResponseMessage(); responseMessageOne.Content = new StringContent("{\"destination_addresses\":[\"Village Way, Brighton BN1, United Kingdom\"],\"origin_addresses\":[\"Arts Rd, Falmer, Brighton BN1 9QN, United Kingdom\"],\"rows\":[{\"elements\":[{\"distance\":{\"text\":\"0.8 mi\",\"value\":2},\"duration\":{\"text\":\"4 min\",\"value\":235},\"status\":\"OK\"}]}],\"status\":\"OK\"}"); List <HttpResponseMessage> responses = new List <HttpResponseMessage> { responseMessageOne, responseMessageOne, responseMessageOne, responseMessageOne, responseMessageOne }; TestGoogleMapsUtil googleMaps = new TestGoogleMapsUtil(responses); Route routeOne = new Route(); routeOne.PickUpAddress = (PickUpAddress)getAddress(false); routeOne.DeliverBy = new DateTime(2012, 12, 13); List <Route> routes = new List <Route> { routeOne }; LocationService locationService = new LocationService(googleMaps); DriverAssignmentService assignmentService = new DriverAssignmentService(locationService); RouteAssignment result = assignmentService.assignMultipleRoutes(routes, new List <Driver> { driverOne }); Assert.Equal(1, result.TempRouteData.Count()); Assert.Equal(driverOne, result.TempRouteData.ElementAt(0).Driver); }