Esempio n. 1
0
        public ActionResult Create(ShipmentEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                foreach (var item in model.Items.ToList())
                {
                    if (item.Quantity <= 0)
                    {
                        model.Items.Remove(item);
                    }
                }

                if (model.Items.None())
                {
                    return(RedirectToAction("Create", new { orderId = model.OrderId })
                           .WithError("No products are selected".TA()));
                }

                var shipment = shipmentService.AddOrUpdate(model);

                if (model.UpdateOrderStatus)
                {
                    var order         = orderService.Find(model.OrderId);
                    var orderItemsQty = order.Items.Sum(i => i.Quantity);

                    var allShipments    = shipmentService.FindByOrder(model.OrderId).ToList();
                    var shippedItemsQty = allShipments.Sum(s => s.Items.Sum(i => i.Quantity));

                    orderService.SetStatus(model.OrderId, orderItemsQty == shippedItemsQty
                        ? OrderStatus.Shipped
                        : OrderStatus.PartiallyShipped);
                }

                return(RedirectToAction("Index")
                       .WithSuccess(string.Format("Shipment \"{0}\" has been added".TA(), shipment.Id)));
            }

            return(View(model));
        }