public async Task <ActionResult> Index(Guid id, bool?backToOverview = null)
        {
            var packagingTypes = CheckBoxCollectionViewModel.CreateFromEnum <PackagingType>();

            packagingTypes.ShowEnumValue = true;

            //We need to exclude 'other' as this will be handled separately
            packagingTypes.PossibleValues =
                packagingTypes.PossibleValues.Where(p => (PackagingType)Convert.ToInt32(p.Value) != PackagingType.Other)
                .ToList();

            var model = new PackagingTypesViewModel
            {
                PackagingTypes = packagingTypes,
                NotificationId = id
            };

            var packagingData =
                await mediator.SendAsync(new GetPackagingInfoForNotification(id));

            if (packagingData != null)
            {
                model.PackagingTypes.SetSelectedValues(packagingData.PackagingTypes);
                if (!string.IsNullOrWhiteSpace(packagingData.OtherDescription))
                {
                    model.OtherSelected    = true;
                    model.OtherDescription = packagingData.OtherDescription;
                }
            }

            return(View(model));
        }
        public async Task<ActionResult> Index(Guid id, bool? backToOverview = null)
        {
            var packagingTypes = CheckBoxCollectionViewModel.CreateFromEnum<PackagingType>();
            packagingTypes.ShowEnumValue = true;

            //We need to exclude 'other' as this will be handled separately
            packagingTypes.PossibleValues =
                packagingTypes.PossibleValues.Where(p => (PackagingType)Convert.ToInt32(p.Value) != PackagingType.Other)
                    .ToList();

            var model = new PackagingTypesViewModel
            {
                PackagingTypes = packagingTypes,
                NotificationId = id
            };

            var packagingData =
                await mediator.SendAsync(new GetPackagingInfoForNotification(id));

            if (packagingData != null)
            {
                model.PackagingTypes.SetSelectedValues(packagingData.PackagingTypes);
                if (!string.IsNullOrWhiteSpace(packagingData.OtherDescription))
                {
                    model.OtherSelected = true;
                    model.OtherDescription = packagingData.OtherDescription;
                }
            }

            return View(model);
        }
        public async Task <ActionResult> Index(PackagingTypesViewModel model, bool?backToOverview = null)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            try
            {
                var selectedPackagingTypes =
                    model.PackagingTypes.PossibleValues.Where(p => p.Selected)
                    .Select(p => (PackagingType)(Convert.ToInt32(p.Value)))
                    .ToList();

                if (model.OtherSelected)
                {
                    selectedPackagingTypes.Add(PackagingType.Other);
                }

                if (!selectedPackagingTypes.Any())
                {
                    ModelState.AddModelError(string.Empty, PackagingTypesResources.ChoosePackagingType);
                    return(View(model));
                }

                var existingPackagingData = await mediator.SendAsync(new GetPackagingInfoForNotification(model.NotificationId));

                await mediator.SendAsync(new SetPackagingInfoForNotification(selectedPackagingTypes, model.NotificationId,
                                                                             model.OtherDescription));

                await this.auditService.AddAuditEntry(this.mediator,
                                                      model.NotificationId,
                                                      User.GetUserId(),
                                                      existingPackagingData.PackagingTypes.Count == 0?NotificationAuditType.Added : NotificationAuditType.Updated,
                                                      NotificationAuditScreenType.PackagingTypes);

                if (backToOverview.GetValueOrDefault())
                {
                    return(RedirectToAction("Index", "Home", new { id = model.NotificationId }));
                }
                else
                {
                    return(RedirectToAction("Index", "SpecialHandling", new { id = model.NotificationId }));
                }
            }
            catch (ApiBadRequestException ex)
            {
                this.HandleBadRequest(ex);

                if (ModelState.IsValid)
                {
                    throw;
                }
            }
            return(View(model));
        }
        public async Task<ActionResult> Index(PackagingTypesViewModel model, bool? backToOverview = null)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            try
            {
                var selectedPackagingTypes =
                    model.PackagingTypes.PossibleValues.Where(p => p.Selected)
                        .Select(p => (PackagingType)(Convert.ToInt32(p.Value)))
                        .ToList();

                if (model.OtherSelected)
                {
                    selectedPackagingTypes.Add(PackagingType.Other);
                }

                if (!selectedPackagingTypes.Any())
                {
                    ModelState.AddModelError(string.Empty, PackagingTypesResources.ChoosePackagingType);
                    return View(model);
                }

                await mediator.SendAsync(new SetPackagingInfoForNotification(selectedPackagingTypes, model.NotificationId,
                        model.OtherDescription));

                if (backToOverview.GetValueOrDefault())
                {
                    return RedirectToAction("Index", "Home", new { id = model.NotificationId });
                }
                else
                {
                    return RedirectToAction("Index", "SpecialHandling", new { id = model.NotificationId });
                }
            }
            catch (ApiBadRequestException ex)
            {
                this.HandleBadRequest(ex);

                if (ModelState.IsValid)
                {
                    throw;
                }
            }
            return View(model);
        }
        public async Task<ActionResult> PackagingTypes(Guid notificationId, PackagingTypesViewModel model)
        {
            TempData[MovementNumberKey] = model.MovementNumber;
            TempData[PackagingTypesKey] = model.SelectedValues;

            if (!ModelState.IsValid)
            {
                ViewBag.MovementNumber = model.MovementNumber;
                return View(model);
            }

            object shipmentDateResult;
            object quantityResult;
            object unitResult;
            object packagingTypesResult;

            var tempDataExists = TempData.TryGetValue(ShipmentDateKey, out shipmentDateResult);
            tempDataExists &= TempData.TryGetValue(QuantityKey, out quantityResult);
            tempDataExists &= TempData.TryGetValue(UnitKey, out unitResult);
            tempDataExists &= TempData.TryGetValue(PackagingTypesKey, out packagingTypesResult);

            if (tempDataExists)
            {
                var shipmentDate = (DateTime)shipmentDateResult;
                var quantity = Convert.ToDecimal(quantityResult);
                var unit = (ShipmentQuantityUnits)unitResult;
                var packagingTypes = (IList<PackagingType>)packagingTypesResult;

                var newMovementDetails = new NewMovementDetails
                {
                    Quantity = quantity,
                    Units = unit,
                    PackagingTypes = packagingTypes
                };

                var newMovementId = await mediator.SendAsync(new CreateMovementAndDetails(notificationId, shipmentDate, newMovementDetails));

                return RedirectToAction("Download", "Create", new { id = newMovementId });
            }

            return RedirectToAction("ShipmentDate", "Create");
        }
        public async Task<ActionResult> PackagingTypes(Guid notificationId)
        {
            object result;
            if (TempData.TryGetValue(MovementNumberKey, out result))
            {
                var movementNumber = (int)result;
                var availablePackagingTypes = await mediator.SendAsync(new GetPackagingTypes(notificationId));

                ViewBag.MovementNumber = movementNumber;
                var model = new PackagingTypesViewModel(availablePackagingTypes, movementNumber);

                return View(model);
            }

            return RedirectToAction("ShipmentDate", "Create");
        }