// GET: Deliveries/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Delivery delivery = db.Deliveries.Find(id); if (delivery == null) { return(HttpNotFound()); } var viewModel = new DeliveryViewModel() { Id = delivery.Id, ClientId = delivery.ClientId, DeliveryDate = delivery.DeliveryDate, ODNotes = delivery.ODNotes, DriverNotes = delivery.DriverNotes, DateDelivered = delivery.DateDelivered, FamilyMembers = AppRoutines.GetFamilyMembers(delivery.ClientId), FamilySelectList = AppRoutines.GetFamilySelectList(delivery.ClientId) }; foreach (var mbr in viewModel.FamilyMembers) { mbr.Age = AppRoutines.GetAge(mbr.DateOfBirth, DateTime.Today); if (mbr.Age < 18) { viewModel.KidsCount += 1; } if (mbr.Age >= 18 && mbr.Age < 60) { viewModel.AdultsCount += 1; } if (mbr.Age >= 60) { viewModel.SeniorsCount += 1; } } var client = db.Clients.Find(delivery.ClientId); if (client != null) { viewModel.Client = client; viewModel.ClientNameAddress = client.LastName + ", " + client.FirstName + " " + client.StreetNumber + " " + client.StreetName + " " + client.Zip; viewModel.Notes = client.Notes; } if (delivery.FullBags != null) { viewModel.FullBags = (int)delivery.FullBags; } if (delivery.HalfBags != null) { viewModel.HalfBags = (int)delivery.HalfBags; } if (delivery.KidSnacks != null) { viewModel.KidSnacks = (int)delivery.KidSnacks; } if (delivery.GiftCardsEligible != null) { viewModel.GiftCardsEligible = (int)delivery.GiftCardsEligible; } return(View(viewModel)); }
// GET: Deliveries public ActionResult Index() { var listDeliveries = new List <Delivery>(db.Deliveries).Where(d => d.DateDelivered == null).OrderBy(d => d.DeliveryDate).ToList(); // to add order .ThenBy(Zip) means storing the Zip in Client.cs AND Delivery/cs var listDeliveryViewModels = new List <DeliveryViewModel>(); foreach (var delivery in listDeliveries) { var client = db.Clients.Find(delivery.ClientId); if (client != null) { var deliveryView = new DeliveryViewModel { Id = delivery.Id, ClientId = client.Id, Client = client, DeliveryDate = Convert.ToDateTime(Session["CallLogDate"]), FamilyMembers = AppRoutines.GetFamilyMembers(client.Id), FamilySelectList = AppRoutines.GetFamilySelectList(client.Id), Kids = new List <FamilyMember>(), Adults = new List <FamilyMember>(), Seniors = new List <FamilyMember>() }; foreach (var mbr in deliveryView.FamilyMembers) { mbr.Age = AppRoutines.GetAge(mbr.DateOfBirth, DateTime.Today); if (mbr.Age < 18) { deliveryView.Kids.Add(mbr); deliveryView.KidsCount += 1; } if (mbr.Age >= 18 && mbr.Age < 60) { deliveryView.Adults.Add(mbr); deliveryView.AdultsCount += 1; } if (mbr.Age >= 60) { deliveryView.Seniors.Add(mbr); deliveryView.SeniorsCount += 1; } } deliveryView.KidsCount = deliveryView.Kids.Count(); deliveryView.AdultsCount = deliveryView.Adults.Count(); deliveryView.SeniorsCount = deliveryView.Seniors.Count(); deliveryView.FullBags = 0; deliveryView.HalfBags = 0; deliveryView.KidSnacks = 0; deliveryView.GiftCards = 0; deliveryView.GiftCardsEligible = 0; // !!! calculate this value deliveryView.DateLastDelivery = DateTime.Today.AddDays(-7); // !!! calculate this value deliveryView.DateLastGiftCard = DateTime.Today.AddDays(-7); // !!! calculate this value if (delivery.DriverId != null) { var driver = db.Users.Find(delivery.DriverId); if (driver != null) { deliveryView.DriverName = driver.FullName; } } else { deliveryView.DriverName = "(nobody yet)"; } var userIid = System.Web.HttpContext.Current.User.Identity.GetUserId(); if (userIid != null) { var user = db.Users.Find(userIid); deliveryView.User = user; } ; deliveryView.FirstName = client.FirstName; deliveryView.LastName = client.LastName; deliveryView.StreetNumber = client.StreetNumber; deliveryView.StreetName = client.StreetName; // (full length on mouseover) \u00a0 is the Unicode character for NO-BREAK-SPACE. deliveryView.StreetToolTip = client.StreetName.Replace(" ", "\u00a0"); deliveryView.City = client.City; deliveryView.CityToolTip = client.City.Replace(" ", "\u00a0"); deliveryView.Zip = client.Zip; deliveryView.Phone = client.Phone; deliveryView.PhoneToolTip = client.Phone.Replace(" ", "\u00a0"); string s; if (client.Notes != null) { deliveryView.Notes = client.Notes; deliveryView.NotesToolTip = client.Notes.Replace(" ", "\u00a0"); s = deliveryView.Notes; s = s.Length <= 12 ? s : s.Substring(0, 12) + "..."; deliveryView.Notes = s; } if (delivery.ODNotes != null) { deliveryView.ODNotes = delivery.ODNotes; deliveryView.ODNotesToolTip = delivery.ODNotes.Replace(" ", "\u00a0"); s = deliveryView.ODNotes; s = s.Length <= 12 ? s : s.Substring(0, 12) + "..."; deliveryView.ODNotes = s; } if (delivery.DriverNotes != null) { deliveryView.DriverNotes = delivery.DriverNotes; deliveryView.DriverNotesToolTip = deliveryView.DriverNotes.Replace(" ", "\u00a0"); s = deliveryView.DriverNotes; // For display, abbreviate to 12 characters: s = s.Length <= 12 ? s : s.Substring(0, 12) + "..."; deliveryView.DriverNotes = s; } s = deliveryView.StreetName; s = s.Length <= 9 ? s : s.Substring(0, 9) + "..."; deliveryView.StreetName = s; s = deliveryView.City; // For display, abbreviate to 11 characters: s = s.Length <= 10 ? s : s.Substring(0, 10) + "..."; deliveryView.City = s; s = deliveryView.Phone; // For display, abbreviate to 12 characters: s = s.Length <= 12 ? s : s.Substring(0, 12) + "..."; deliveryView.Phone = s; listDeliveryViewModels.Add(deliveryView); } } return(View(listDeliveryViewModels)); }