public async Task GetRoute_WithValidRouteId_ReturnsRoute()
        {
            var svc    = new RouteService();
            var routes = await svc.SearchAsync(new SearchRoute { CenterNumber = 22, FilterStartDate = System.DateTime.Today.AddDays(-6), FilterEndDate = System.DateTime.Today.AddDays(-3) });

            var result = await svc.GetByRouteIdAsync(routes.First().RouteId);

            Assert.AreEqual(routes.First().RouteId, result.RouteId);
        }
        public async Task <ActionResult> Stops(int routeId)
        {
            var routeWithStops = await RouteService.GetByRouteIdAsync(routeId);

            var model = new RouteStopViewModel(routeWithStops)
            {
                StopColumnOption = new ColumnOptionViewModel(await LookUpService.GetColumnOptionsForUserAndPage(UserName, "ROUTETRACKERSTOPS"), ColumnTypes.Stop)
            };

            return(PartialView("_stopList", model));
        }
        public async Task <ActionResult> MovedStopPopup(int routePlanId, int modifiedType)
        {
            //call service layer to get collection of stops with the route plan ID
            var matchingStopCollection = new List <Stop>(await RouteService.GetStopsByRoutePlanIdAsync(routePlanId));

            //stop collection should only ever have 2 entries
            if (matchingStopCollection.Count != 2)
            {
                throw new UnexpectedStopException("An unexpected number of Stops was encountered");
            }
            //decide which stop is destination based on direction you are coming from
            int desiredRouteId = matchingStopCollection.First(s => s.RoutePlanModificationTypeId != modifiedType).RouteId;

            //get route info from routeID
            var model = new MovedStopPopupRouteStopViewModel(await RouteService.GetByRouteIdAsync(desiredRouteId))
            {
                //need both route and stop column options
                StopColumnOption  = new ColumnOptionViewModel(await LookUpService.GetColumnOptionsForUserAndPage(UserName, "ROUTETRACKERSTOPS"), ColumnTypes.Stop),
                RouteColumnOption = new ColumnOptionViewModel(await LookUpService.GetColumnOptionsForUserAndPage(UserName, "ROUTETRACKERROUTES"), ColumnTypes.Route)
            };

            //send model of route info to popup
            return(PartialView("_movedStopPopup", model));
        }
        public async Task <ActionResult> ChangeStopTime(UpdateRouteWithStopsViewModel route)
        {
            var updatedRouteStopList = new List <atm.services.models.UpdateStop>();
            var stops = route.Stops;

            //Get the current version of the route from the API
            var existingRoute = await RouteService.GetByRouteIdAsync(route.RouteId);

            var existingRouteStops = existingRoute.RouteStops;

            for (int x = 0; x < stops.Count(); x++)
            {
                var existingStop = existingRouteStops.Single(s => s.RoutePlanId == stops[x].RoutePlanId);
                var updatedStop  = new UpdateStop()
                {
                    RoutePlanId = stops[x].RoutePlanId,
                    RouteId     = stops[x].RouteId,
                    BillTo      = stops[x].BillTo,
                    ShipTo      = stops[x].ShipTo,
                    AdjustedDeliveryDateTime  = stops[x].AdjustedDeliveryDateTime,
                    ScheduledDeliveryDateTime = stops[x].ScheduledDeliveryDateTime,
                    Comment   = stops[x].LastCustomerCommunicationComment,
                    EarlyLate = stops[x].EarlyLate,
                    RoutePlanModificationTypeId = stops[x].RoutePlanModificationTypeId,
                    RouteNumber         = stops[x].RouteNumber,
                    AdjustedRouteNumber = stops[x].AdjustedRouteNumber,
                    StopNumber          = stops[x].AdjustedStopNumber,
                    AdjustedStopNumber  = stops[x].AdjustedStopNumber,
                };
                //Set the comment to empty string if value is null otherwise leave it alone
                existingStop.Comment = existingStop.Comment ?? "";
                updatedStop.Comment  = updatedStop.Comment ?? "";

                //if User has updated the Comment and/or ScheduledDeliveryDateTime
                if (!System.DateTime.Equals(updatedStop.AdjustedDeliveryDateTime, existingStop.AdjustedDeliveryDateTime) || !System.DateTime.Equals(updatedStop.ScheduledDeliveryDateTime, existingStop.ScheduledDeliveryDateTime) || updatedStop.Comment != existingStop.LastCustomerCommunicationComment)
                {
                    //If stop currently has RoutePlanModificationTypeId of NONE or stop was added and time modified.
                    if (updatedStop.RoutePlanModificationTypeId == 0 || updatedStop.RoutePlanModificationTypeId == 2)
                    {
                        //Set status to Time Changed.  If the original modification type was a 2, the API will reset it back to 2.
                        updatedStop.RoutePlanModificationTypeId = 3;
                    }

                    if (updatedStop.StopNumber != existingStop.StopNumber)
                    {
                        updatedStop.IncludesStopChanges = true;
                    }
                    if (!System.DateTime.Equals(updatedStop.ScheduledDeliveryDateTime, existingStop.ScheduledDeliveryDateTime))
                    {
                        updatedStop.IncludesStopScheduledDateTimeChanges = true;
                        if (!System.DateTime.Equals(updatedStop.AdjustedDeliveryDateTime, updatedStop.ScheduledDeliveryDateTime))
                        {
                            updatedStop.IncludesStopAdjustedDateTimeChanges = true;
                        }
                    }
                    else
                    {
                        if (!System.DateTime.Equals(updatedStop.AdjustedDeliveryDateTime, existingStop.AdjustedDeliveryDateTime))
                        {
                            updatedStop.IncludesStopAdjustedDateTimeChanges = true;
                        }
                    }


                    //Save the stop
                    updatedRouteStopList.Add(updatedStop);
                }
            }

            var updateRouteWithStops = new UpdateRouteWithStops()
            {
                SygmaCenterNo = route.SygmaCenterNo,
                RouteNumber   = route.RouteNumber,
                RouteId       = route.RouteId,
                RouteName     = route.RouteName,
                RouteStops    = updatedRouteStopList
            };

            await RouteService.UpdateRouteAsync(route.RouteNumber, route.SygmaCenterNo, updateRouteWithStops);

            foreach (var stop in updateRouteWithStops.RouteStops)
            {
                if (!string.IsNullOrEmpty(stop.Comment.Trim()))
                {
                    var existingStop = existingRouteStops.Single(s => s.RoutePlanId == stop.RoutePlanId);
                    if (existingStop.LastCustomerCommunicationComment != stop.Comment)
                    {
                        await CommentService.AddCommentAsync(new services.models.AddComment
                        {
                            CenterNumber      = updateRouteWithStops.SygmaCenterNo,
                            CreatedBy         = LastFirstName.ToUpperInvariant(),
                            Status            = 3, // non-internal
                            PrimaryRecordId   = $"{((int)stop.BillTo).ToString()}-{((int)stop.ShipTo).ToString()}",
                            SecondaryRecordId = stop.RoutePlanId.ToString(),
                            Screen            = "RT",
                            ShortComment      = "Customer Communication",
                            LongComment       = stop.Comment
                        });
                    }
                }

                if (stop.IncludesStopChanges)
                {
                    var existingStop = existingRouteStops.Single(s => s.RoutePlanId == stop.RoutePlanId);
                    await CommentService.AddCommentAsync(new services.models.AddComment
                    {
                        CenterNumber      = updateRouteWithStops.SygmaCenterNo,
                        CreatedBy         = LastFirstName.ToUpperInvariant(),
                        Status            = 2, // internal
                        PrimaryRecordId   = $"{((int)stop.BillTo).ToString()}-{((int)stop.ShipTo).ToString()}",
                        SecondaryRecordId = stop.RoutePlanId.ToString(),
                        Screen            = "RT",
                        ShortComment      = "Stop Adjustment",
                        LongComment       = $"Stop moved from Stop: {stop.StopNumber} at {existingStop.AdjustedDeliveryDateTime} to Stop: {stop.AdjustedStopNumber} at {stop.AdjustedDeliveryDateTime}"
                    });
                }

                if (stop.IncludesStopAdjustedDateTimeChanges)
                {
                    var existingStop = existingRouteStops.Single(s => s.RoutePlanId == stop.RoutePlanId);
                    await CommentService.AddCommentAsync(new services.models.AddComment
                    {
                        CenterNumber      = updateRouteWithStops.SygmaCenterNo,
                        CreatedBy         = LastFirstName.ToUpperInvariant(),
                        Status            = 2, // internal
                        PrimaryRecordId   = $"{((int)stop.BillTo).ToString()}-{((int)stop.ShipTo).ToString()}",
                        SecondaryRecordId = stop.RoutePlanId.ToString(),
                        Screen            = "RT",
                        ShortComment      = "Time Adjustment",
                        LongComment       = $"Stop {stop.StopNumber} adjusted delivery was changed from {existingStop.AdjustedDeliveryDateTime} to {stop.AdjustedDeliveryDateTime}"
                    });
                }
                if (stop.IncludesStopScheduledDateTimeChanges)
                {
                    var existingStop = existingRouteStops.Single(s => s.RoutePlanId == stop.RoutePlanId);
                    await CommentService.AddCommentAsync(new services.models.AddComment
                    {
                        CenterNumber      = updateRouteWithStops.SygmaCenterNo,
                        CreatedBy         = LastFirstName.ToUpperInvariant(),
                        Status            = 2, // internal
                        PrimaryRecordId   = $"{((int)stop.BillTo).ToString()}-{((int)stop.ShipTo).ToString()}",
                        SecondaryRecordId = stop.RoutePlanId.ToString(),
                        Screen            = "RT",
                        ShortComment      = "Time Adjustment",
                        LongComment       = $"Stop {stop.StopNumber} scheduled delivery was changed from {existingStop.ScheduledDeliveryDateTime} to {stop.ScheduledDeliveryDateTime}"
                    });
                }
            }

            return(new HttpStatusCodeResult(System.Net.HttpStatusCode.Created));
        }
 public async Task GetRoute_WithInvalidRouteId_Returns404()
 {
     var svc    = new RouteService();
     var result = await svc.GetByRouteIdAsync(999999);
 }