public ActionResult Deliver(long?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ClientSubscription clientSubscription = db.ClientSubscriptions.Find(id); if (clientSubscription == null) { return(HttpNotFound()); } ClientSubscriptionDeliverViewModel model = new ClientSubscriptionDeliverViewModel() { ClientAccountId = clientSubscription.ClientAccountId, ClientSubscriptionId = clientSubscription.ClientSubscriptionId, ClientAccountName = clientSubscription.Client.ClientAccountName, VesselName = clientSubscription.VesselName, StationAccountName = clientSubscription.Station.StationName, Quantity = clientSubscription.CurrentQuantity }; return(View(model)); }
public ActionResult DeliverConfirmed([Bind(Include = "ClientSubscriptionId, Quantity, Notes")] ClientSubscriptionDeliverViewModel model) { if (ModelState.IsValid) { DateTime time = DateTime.Now; ClientSubscription subscription = db.ClientSubscriptions.Find(model.ClientSubscriptionId); long clientAccountId = subscription.ClientAccountId; float leftQuantity = subscription.CurrentQuantity - model.Quantity; if (leftQuantity == 0) { subscription.State = ClientSubscriptionState.Delivered; } else { subscription.State = ClientSubscriptionState.PartialDelivered; } subscription.CurrentQuantity = leftQuantity; subscription.Timestamp = time; subscription.Notes = model.Notes; db.Entry(subscription).State = EntityState.Modified; var clientSubscriptionHistory = new ClientSubscriptionHistory() { UnitPrice = subscription.UnitPrice, State = subscription.State, Quantity = model.Quantity, Notes = model.Notes, VesselName = subscription.VesselName, ClientSubscriptionId = subscription.ClientSubscriptionId, Timestamp = time }; db.ClientSubscriptionHistories.Add(clientSubscriptionHistory); db.SaveChanges(); return(RedirectToAction("Index", new { accountId = clientAccountId })); } return(View("Invalid state.")); }