public async Task <ActionResult> AddAllSizesToOrderProductAddressProductionDate(int?orderId, int?productId, int?addressId, DateTime?productionDate)
        {
            if (orderId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            if (productId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            if (addressId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            if (productionDate == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            OrderProductAddressProductionDate orderProductAddressProductionDate = await context.OrderProductAddressProductionDates
                                                                                  .Include(opapd => opapd.OrderProductAddress.OrderProduct.Order)
                                                                                  .Include(opapd => opapd.OrderProductAddress.OrderProduct.Product)
                                                                                  .Include(opapd => opapd.OrderProductAddress.Address)
                                                                                  .FirstOrDefaultAsync(opapd => opapd.OrderId == orderId &&
                                                                                                       opapd.ProductId == productId &&
                                                                                                       opapd.AddressId == addressId &&
                                                                                                       opapd.ProductionDate == productionDate);

            if (orderProductAddressProductionDate == null)
            {
                return(HttpNotFound());
            }

            AddAllSizesToOrderProductAddressProductionDateViewModel model = new AddAllSizesToOrderProductAddressProductionDateViewModel();

            model.OrderProductAddressProductionDate = new OrderProductAddressProductionDateViewModel(orderProductAddressProductionDate);
            model.OrderProductAddressSizes          = GetOrderProductAddressSizeViewModels(orderProductAddressProductionDate);

            return(View(model));
        }
        public async Task <ActionResult> AddAllSizesToOrderProductAddressProductionDate(AddAllSizesToOrderProductAddressProductionDateViewModel model)
        {
            OrderProductAddressProductionDate orderProductAddressProductionDate = await context.OrderProductAddressProductionDates
                                                                                  .Include(opapd => opapd.OrderProductAddress.OrderProduct.Order)
                                                                                  .Include(opapd => opapd.OrderProductAddress.OrderProduct.Product)
                                                                                  .Include(opapd => opapd.OrderProductAddress.Address)
                                                                                  .Include(opapd => opapd.OrderProductAddressProductionDateSizes)
                                                                                  .FirstOrDefaultAsync(opapd => opapd.OrderId == model.OrderProductAddressProductionDate.OrderProductAddress.OrderProduct.Order.Id &&
                                                                                                       opapd.ProductId == model.OrderProductAddressProductionDate.OrderProductAddress.OrderProduct.Product.Id &&
                                                                                                       opapd.AddressId == model.OrderProductAddressProductionDate.OrderProductAddress.Address.Id &&
                                                                                                       opapd.ProductionDate == model.OrderProductAddressProductionDate.ProductionDate);

            if (orderProductAddressProductionDate == null)
            {
                return(HttpNotFound());
            }

            if (orderProductAddressProductionDate.OrderProductAddressProductionDateSizes.Any())
            {
                ModelState.AddModelError("", "Дата производства продукции грузополучателя заказа уже содержит размерные данные");
            }

            if (!ModelState.IsValid)
            {
                model.OrderProductAddressSizes = GetOrderProductAddressSizeViewModels(orderProductAddressProductionDate);

                return(View(model));
            }

            foreach (var orderProductAddressSizeViewModel in GetOrderProductAddressSizeViewModels(orderProductAddressProductionDate))
            {
                Size size = context.Sizes.Find(orderProductAddressSizeViewModel.Size.Id);

                if (size != null)
                {
                    context.OrderProductAddressProductionDateSizes.Add(new OrderProductAddressProductionDateSize
                    {
                        OrderProductAddressProductionDate = orderProductAddressProductionDate,
                        Size     = size,
                        Quantity = orderProductAddressSizeViewModel.Quantity
                    });
                }
            }

            try
            {
                await context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                string errorMessage = ex.Message;

                while (ex != null)
                {
                    errorMessage = ex.Message;
                    ex           = ex.InnerException;
                }

                ModelState.AddModelError("", errorMessage);

                return(View(model));
            }

            return(RedirectToAction(nameof(Details),
                                    new
            {
                orderId = orderProductAddressProductionDate.OrderId,
                productId = orderProductAddressProductionDate.ProductId,
                addressId = orderProductAddressProductionDate.AddressId,
                productionDate = orderProductAddressProductionDate.ProductionDate
            }));
        }