public void Creates_new_tblLot_record_and_KillSwitch_will_not_have_been_engaged_if_service_method_and_synchronization_were_successful()
            {
                //Arrange
                var productionDate = new DateTime(2020, 3, 29);
                int?newSequence    = null;

                while (newSequence == null)
                {
                    var existingLots = RVCUnitOfWork.ChileLotRepository.Filter(l => l.LotDateCreated == productionDate && l.LotTypeId == (int)LotTypeEnum.WIP);
                    var sequence     = existingLots.Any() ? existingLots.Max(l => l.LotDateSequence) : 0;
                    if (sequence < 99)
                    {
                        newSequence = sequence + 1;
                    }
                    else
                    {
                        productionDate = productionDate.AddDays(1);
                    }
                }

                const int pickedQuantity    = 23;
                var       chileProduct      = RVCUnitOfWork.ChileProductRepository.Filter(c => c.ChileState == ChileStateEnum.WIP).First();
                var       productionLine    = RVCUnitOfWork.LocationRepository.Filter(l => l.LocationType == LocationType.ProductionLine).First();
                var       pickedInventory   = RVCUnitOfWork.InventoryRepository.Filter(i => i.Lot.Hold == null && i.Lot.QualityStatus == LotQualityStatus.Released && i.Quantity > pickedQuantity && i.Location.Facility.Name.Contains("rincon")).First();
                var       warehouseLocation = RVCUnitOfWork.LocationRepository.Filter(l => l.LocID != null).First();
                var       packagingProduct  = RVCUnitOfWork.PackScheduleRepository.All().First();

                var parameters = new CreateMillAndWetdownParameters
                {
                    UserToken = TestUser.UserName,

                    ProductionDate  = productionDate,
                    LotSequence     = newSequence.Value,
                    ChileProductKey = chileProduct.ToChileProductKey(),

                    ShiftKey          = "SHIFTY",
                    ProductionLineKey = productionLine.ToLocationKey(),
                    ProductionBegin   = productionDate.AddDays(-1),
                    ProductionEnd     = productionDate.AddHours(12),
                    PickedItems       = new[]
                    {
                        new MillAndWetdownPickedItemParameters
                        {
                            InventoryKey = pickedInventory.ToInventoryKey(),
                            Quantity     = pickedQuantity
                        }
                    },
                    ResultItems = new[]
                    {
                        new MillAndWetdownResultItemParameters
                        {
                            PackagingProductKey = packagingProduct.ToPackagingProductKey(),
                            LocationKey         = warehouseLocation.ToLocationKey(),
                            Quantity            = 10
                        }
                    }
                };

                TestHelper.ResetContext();

                //Act
                var result = Service.CreateMillAndWetdown(parameters);

                result.AssertSuccess();
                var lotString = GetKeyFromConsoleString(ConsoleOutput.AddedLot);

                //Assert
                MockKillSwitch.Verify(k => k.Engage(), Times.Never());

                var newLot = int.Parse(lotString);

                Assert.IsNotNull(new RioAccessSQLEntities().tblLots.FirstOrDefault(t => t.Lot == newLot));
            }
        internal IResult <ChileLotProduction> Execute(DateTime timeStamp, CreateMillAndWetdownParameters parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            var employeeResult = new GetEmployeeCommand(ProductionUnitOfWork).GetEmployee(parameters.Params);

            if (!employeeResult.Success)
            {
                return(employeeResult.ConvertTo <ChileLotProduction>());
            }
            var employee = employeeResult.ResultingObject;

            var chileLotResult = new CreateNewChileLotCommand(ProductionUnitOfWork).Execute(new CreateNewChileLotCommandParameters
            {
                EmployeeKey            = employeeResult.ResultingObject,
                TimeStamp              = timeStamp,
                LotDate                = parameters.Params.ProductionDate,
                LotSequence            = parameters.Params.LotSequence,
                LotType                = LotTypeEnum.WIP,
                ChileProductKey        = parameters.ChileProductKey,
                SetLotProductionStatus = LotProductionStatus.Produced,
                SetLotQualityStatus    = LotQualityStatus.Pending
            });

            if (!chileLotResult.Success)
            {
                return(chileLotResult.ConvertTo <ChileLotProduction>());
            }
            var chileLot = chileLotResult.ResultingObject;

            var pickedInventoryResult = new CreatePickedInventoryCommand(ProductionUnitOfWork).Execute(new CreatePickedInventoryCommandParameters
            {
                PickedReason = PickedReason.Production,
                EmployeeKey  = employee,
                TimeStamp    = timeStamp
            });

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

            var millAndWetdown = ProductionUnitOfWork.ChileLotProductionRepository.Add(new ChileLotProduction
            {
                LotDateCreated    = chileLot.LotDateCreated,
                LotDateSequence   = chileLot.LotDateSequence,
                LotTypeId         = chileLot.LotTypeId,
                ResultingChileLot = chileLot,

                PickedInventoryDateCreated = pickedInventoryResult.ResultingObject.DateCreated,
                PickedInventorySequence    = pickedInventoryResult.ResultingObject.Sequence,
                PickedInventory            = pickedInventoryResult.ResultingObject,

                Results = new LotProductionResults
                {
                    LotDateCreated  = chileLot.LotDateCreated,
                    LotDateSequence = chileLot.LotDateSequence,
                    LotTypeId       = chileLot.LotTypeId,

                    DateTimeEntered = timeStamp,
                    ResultItems     = new List <LotProductionResultItem>()
                }
            });

            return(SetMillAndWetdown(millAndWetdown, parameters, timeStamp, employee));
        }