public void SetResults(ProductionBatch productionBatch, tblLot lot)
        {
            var productionStart = productionBatch.Production.Results.ProductionBegin.ConvertUTCToLocal().RoundMillisecondsForSQL();

            lot.Shift          = productionBatch.Production.Results.ShiftKey;
            lot.Produced       = productionStart.Date; // this is how they are expecting to find lots for entry of production results. -VK 2/19/14
            lot.ProductionDate = productionBatch.LotDateCreated.Date;
            lot.BatchBegTime   = productionStart;
            lot.BatchEndTime   = productionBatch.Production.Results.ProductionEnd.ConvertUTCToLocal().RoundMillisecondsForSQL();
            lot.ProductionLine = ProductionLineParser.GetProductionLineNumber(productionBatch.Production.Results.ProductionLineLocation);
            lot.BatchStatID    = (int?)BatchStatID.Produced;

            SetIncomingItems(productionBatch, lot);
            SetOutgoingItems(productionBatch, lot);
        }
            public void Updates_tblLot_production_result_data_and_KillSwitch_will_not_have_been_engaged_on_success()
            {
                //Arrange
                var productionResult = RVCUnitOfWork.LotProductionResultsRepository.Filter(r => r.Production.ProductionType == ProductionType.ProductionBatch &&
                                                                                           r.Production.ResultingChileLot.Lot.Inventory.Any(i => i.Quantity > 0), r => r.ResultItems).First();
                var productionLine = RVCUnitOfWork.LocationRepository.Filter(l => l.LocationType == LocationType.ProductionLine && l != productionResult.ProductionLineLocation).First();

                var startTimestamp = new DateTime(2014, 1, 1, 2, 3, 45, 670);
                var parameters     = new UpdateProductionResultsParameters
                {
                    UserToken                = TestUser.UserName,
                    ProductionResultKey      = new LotKey(productionResult),
                    ProductionShiftKey       = "TestShiftKey",
                    ProductionLine           = productionLine,
                    ProductionStartTimestamp = startTimestamp,
                    ProductionEndTimestamp   = startTimestamp.AddDays(1),
                    InventoryItems           = productionResult.ResultItems.Select(i => new BatchResultItemParameters
                    {
                        PackagingKey          = new PackagingProductKey(i),
                        LocationKey           = new LocationKey(i),
                        InventoryTreatmentKey = new InventoryTreatmentKey(i),
                        Quantity = i.Quantity + 1
                    }).ToList()
                };

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

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

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

                var newLot = int.Parse(lotString);

                using (var context = new RioAccessSQLEntities())
                {
                    var tblLot = context.tblLots.First(l => l.Lot == newLot);
                    Assert.AreEqual(parameters.ProductionShiftKey, tblLot.Shift);
                    Assert.AreEqual(ProductionLineParser.GetProductionLineNumber(parameters.ProductionLine), tblLot.ProductionLine);
                    parameters.ProductionStartTimestamp.AssertUTCSameAsMST(tblLot.BatchBegTime.Value);
                    parameters.ProductionEndTimestamp.AssertUTCSameAsMST(tblLot.BatchEndTime.Value);
                }
            }
            public void Updates_tblBatchItems_and_KillSwitch_will_not_have_been_engaged_on_success()
            {
                //Arrange
                var productionBatch = RVCUnitOfWork.ProductionBatchRepository
                                      .Filter(b =>
                                              b.Production.ResultingChileLot.Lot.Inventory.Any(i => i.Quantity > 0) && b.Production.PickedInventory.Items.Any() &&
                                              b.Production.PickedInventory.Items.All(i => i.Quantity > 2) &&
                                              b.Production.PickedInventory.Items.Distinct().Count() == b.Production.PickedInventory.Items.Count(),
                                              b => b.Production.PickedInventory.Items,
                                              b => b.Production.Results.ResultItems)
                                      .FirstOrDefault();

                if (productionBatch == null)
                {
                    Assert.Inconclusive("Could not find suitable ProductionBatch for testing.");
                }

                var pickedModifications = productionBatch.Production.PickedInventory.Items.Select(i => new
                {
                    ExpectedQuantity = i.Quantity - 1,
                    Parameter        = new SetPickedInventoryItemParameters
                    {
                        InventoryKey = new InventoryKey(i),
                        Quantity     = -1
                    }
                }).ToList();
                var productionLine = RVCUnitOfWork.LocationRepository
                                     .Filter(l => l.LocationType == LocationType.ProductionLine && l != productionBatch.Production.Results.ProductionLineLocation).First();

                var startTimestamp = new DateTime(2014, 1, 1, 2, 3, 45, 670);
                var parameters     = new UpdateProductionResultsParameters
                {
                    UserToken                = TestUser.UserName,
                    ProductionResultKey      = new LotKey(productionBatch),
                    ProductionShiftKey       = "TestShiftKey",
                    ProductionLine           = productionLine,
                    ProductionStartTimestamp = startTimestamp,
                    ProductionEndTimestamp   = startTimestamp.AddDays(1),
                    InventoryItems           = productionBatch.Production.Results.ResultItems.Select(i => new BatchResultItemParameters
                    {
                        PackagingKey          = new PackagingProductKey(i),
                        LocationKey           = new LocationKey(i),
                        InventoryTreatmentKey = new InventoryTreatmentKey(i),
                        Quantity = i.Quantity + 1
                    }).ToList(),
                    PickedInventoryItemChanges = pickedModifications.Select(m => m.Parameter).ToList()
                };

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

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

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

                var newLot = int.Parse(lotString);

                using (var context = new RioAccessSQLEntities())
                {
                    var lotSelect = context.tblLots.Where(l => l.Lot == newLot)
                                    .Select(l => new
                    {
                        l,
                        l.inputBatchItems
                    }).First();
                    var tblLot = lotSelect.l;

                    Assert.AreEqual(parameters.ProductionShiftKey, tblLot.Shift);
                    Assert.AreEqual(ProductionLineParser.GetProductionLineNumber(parameters.ProductionLine), tblLot.ProductionLine);
                    parameters.ProductionStartTimestamp.AssertUTCSameAsMST(tblLot.BatchBegTime.Value);
                    parameters.ProductionEndTimestamp.AssertUTCSameAsMST(tblLot.BatchEndTime.Value);

                    var batchItems = tblLot.inputBatchItems.ToList();
                    Assert.AreEqual(pickedModifications.Count, batchItems.Count);
                    foreach (var item in pickedModifications)
                    {
                        var batchItem = batchItems.Single(i => i.Lot == LotNumberBuilder.BuildLotNumber(new InventoryKey().Parse(item.Parameter.InventoryKey)));
                        Assert.AreEqual(item.ExpectedQuantity, batchItem.Quantity);
                    }
                }
            }