internal IResult Execute(LotKey lotKey)
        {
            var includePaths = DeleteLotConductor.ConstructIncludePaths <ChileLotProduction>(p => p.ResultingChileLot.Lot,
                                                                                             p => p.PickedInventory.Items.Select(i => i.CurrentLocation),
                                                                                             p => p.Results.ResultItems).ToArray();
            var lotProduction = _productionUnitOfWork.ChileLotProductionRepository.FindByKey(lotKey, includePaths);

            if (lotProduction == null)
            {
                return(new NoWorkRequiredResult());
            }

            if (lotProduction.ProductionType != ProductionType.MillAndWetdown)
            {
                return(new InvalidResult(string.Format(UserMessages.MillAndWetdownEntryNotFound, lotKey)));
            }

            var pickedInventoryItemModifications = PickedInventoryHelper.CreateModifyPickedInventoryItemParameters(lotProduction.PickedInventory, new List <PickedInventoryParameters>());
            var modifyPickedResult = new ModifyPickedInventoryItemsCommand(_productionUnitOfWork).Execute(lotProduction, pickedInventoryItemModifications);

            if (!modifyPickedResult.Success)
            {
                return(modifyPickedResult.ConvertTo <ChileLotProduction>());
            }

            var inventoryModifications = pickedInventoryItemModifications.Select(i => i.ToModifySourceInventoryParameters()).ToList();
            var setResults             = new SetLotProductionResultItemsCommand(_productionUnitOfWork).Execute(lotProduction.Results, new List <IProductionResultItemParameters>(), ref inventoryModifications);

            if (!setResults.Success)
            {
                return(setResults.ConvertTo <ChileLotProduction>());
            }

            var modifyInventoryResult = new ModifyInventoryCommand(_productionUnitOfWork).Execute(inventoryModifications, null);

            if (!modifyInventoryResult.Success)
            {
                return(modifyInventoryResult.ConvertTo <ChileLotProduction>());
            }

            _productionUnitOfWork.LotProductionResultsRepository.Remove(lotProduction.Results);
            _productionUnitOfWork.PickedInventoryRepository.Remove(lotProduction.PickedInventory);

            var deleteLotResult = new DeleteLotConductor(_productionUnitOfWork).Delete(lotProduction.ResultingChileLot.Lot);

            if (!deleteLotResult.Success)
            {
                return(deleteLotResult);
            }

            return(new SuccessResult());
        }
        protected IResult <ChileLotProduction> SetMillAndWetdown <TParams>(ChileLotProduction millAndWetdown, SetMillAndWetdownParameters <TParams> parameters, DateTime timestamp, IEmployeeKey employee)
            where TParams : ISetMillAndWetdownParameters
        {
            millAndWetdown.ProductionType = ProductionType.MillAndWetdown;
            millAndWetdown.EmployeeId     = employee.EmployeeKey_Id;
            millAndWetdown.TimeStamp      = timestamp;

            millAndWetdown.Results.EmployeeId = employee.EmployeeKey_Id;
            millAndWetdown.Results.TimeStamp  = timestamp;
            millAndWetdown.Results.ProductionLineLocationId = parameters.ProductionLineKey.LocationKey_Id;
            millAndWetdown.Results.ShiftKey        = parameters.Params.ShiftKey;
            millAndWetdown.Results.ProductionBegin = parameters.Params.ProductionBegin;
            millAndWetdown.Results.ProductionEnd   = parameters.Params.ProductionEnd;

            var lotKey = new LotKey(millAndWetdown);

            if (!parameters.ChileProductKey.Equals(millAndWetdown.ResultingChileLot))
            {
                var chileProduct = ProductionUnitOfWork.ChileProductRepository.FindByKey(parameters.ChileProductKey);
                if (chileProduct == null)
                {
                    return(new InvalidResult <ChileLotProduction>(null, string.Format(UserMessages.ChileProductNotFound, parameters.ChileProductKey)));
                }

                if (chileProduct.ChileState.ToLotType() != millAndWetdown.ResultingChileLot.ChileProduct.ChileState.ToLotType())
                {
                    return(new InvalidResult <ChileLotProduction>(null, UserMessages.ChileProductDifferentLotType));
                }

                if (ProductionUnitOfWork.PickedInventoryItemRepository.Filter(PickedInventoryItemPredicates.FilterByLotKey(lotKey)).Any())
                {
                    return(new InvalidResult <ChileLotProduction>(null, string.Format(UserMessages.LotHasExistingPickedInventory, lotKey)));
                }

                millAndWetdown.ResultingChileLot.ChileProductId = parameters.ChileProductKey.ChileProductKey_ProductId;
            }

            var pickedInventoryItemModifications = PickedInventoryHelper.CreateModifyPickedInventoryItemParameters(millAndWetdown.PickedInventory, parameters.PickedItems);
            var validationResults = PickedInventoryValidator.ForMillAndWetdown.ValidateItems(ProductionUnitOfWork.InventoryRepository, pickedInventoryItemModifications.Where(i => i.DeltaQuantity > 0).Select(i => i.InventoryKey));

            if (!validationResults.Success)
            {
                return(validationResults.ConvertTo <ChileLotProduction>());
            }

            var modifyPickedResult = new ModifyPickedInventoryItemsCommand(ProductionUnitOfWork).Execute(millAndWetdown, pickedInventoryItemModifications);

            if (!modifyPickedResult.Success)
            {
                return(modifyPickedResult.ConvertTo <ChileLotProduction>());
            }

            millAndWetdown.PickedInventory.Archived = true;
            var inventoryModifications = pickedInventoryItemModifications.Select(i => i.ToModifySourceInventoryParameters()).ToList();
            var setResults             = new SetLotProductionResultItemsCommand(ProductionUnitOfWork).Execute(millAndWetdown.Results, parameters.ResultItems, ref inventoryModifications);

            if (!setResults.Success)
            {
                return(setResults.ConvertTo <ChileLotProduction>());
            }

            var transaction           = new InventoryTransactionParameters(employee, timestamp, InventoryTransactionType.CreatedMillAndWetdown, lotKey, millAndWetdown);
            var modifyInventoryResult = new ModifyInventoryCommand(ProductionUnitOfWork).Execute(inventoryModifications, transaction);

            if (!modifyInventoryResult.Success)
            {
                return(modifyInventoryResult.ConvertTo <ChileLotProduction>());
            }

            return(new SuccessResult <ChileLotProduction>(millAndWetdown));
        }