internal IResult Execute(LotProductionResults productionResults, IEnumerable <IProductionResultItemParameters> resultItems, ref List <ModifyInventoryParameters> inventoryModifications)
        {
            var nextSequence  = productionResults.ResultItems.Select(i => i.ResultItemSequence).DefaultIfEmpty(0).Max() + 1;
            var existingItems = productionResults.ResultItems.ToDictionary(i => new InventoryKey(i));

            inventoryModifications = inventoryModifications ?? new List <ModifyInventoryParameters>();

            foreach (var item in resultItems)
            {
                if (item.Quantity <= 0)
                {
                    return(new InvalidResult(UserMessages.QuantityNotGreaterThanZero));
                }

                var inventoryKey = new InventoryKey(productionResults, item.PackagingProductKey, item.LocationKey, item.InventoryTreatmentKey, "");
                if (inventoryModifications.Any(m => m.InventoryKey.Equals(inventoryKey) && m.ModifyQuantity > 0))
                {
                    return(new InvalidResult(string.Format(UserMessages.ProductionResultAlreadyPendingAddition, inventoryKey)));
                }

                LotProductionResultItem resultItem;
                if (existingItems.TryGetValue(inventoryKey, out resultItem))
                {
                    existingItems.Remove(inventoryKey);
                }
                else
                {
                    resultItem = _productionUnitOfWork.LotProductionResultItemsRepository.Add(new LotProductionResultItem
                    {
                        LotDateCreated     = inventoryKey.LotKey_DateCreated,
                        LotDateSequence    = inventoryKey.LotKey_DateSequence,
                        LotTypeId          = inventoryKey.LotKey_LotTypeId,
                        ResultItemSequence = nextSequence++,
                        Quantity           = 0
                    });
                }

                var deltaQuantity = item.Quantity - resultItem.Quantity;
                if (deltaQuantity != 0)
                {
                    inventoryModifications.Add(new ModifyInventoryParameters(inventoryKey, deltaQuantity));
                }

                resultItem.PackagingProductId = inventoryKey.PackagingProductKey_ProductId;
                resultItem.LocationId         = inventoryKey.LocationKey_Id;
                resultItem.TreatmentId        = inventoryKey.InventoryTreatmentKey_Id;
                resultItem.Quantity           = item.Quantity;
            }

            foreach (var item in existingItems.Values)
            {
                _productionUnitOfWork.LotProductionResultItemsRepository.Remove(item);
                inventoryModifications.Add(new ModifyInventoryParameters(item, -item.Quantity));
            }

            return(new SuccessResult());
        }
        public CreateNewProductionResultResult CreateNewProductionResult(IPackScheduleKey packScheduleKey, ILotKey lotKey, ProductionBatchEntityObjectMother.tblLotResultDTO outputLot, out LotProductionResults productionResult)
        {
            productionResult = null;

            if (outputLot == null)
            {
                return(CreateNewProductionResultResult.OutputLotIsNull);
            }

            if (outputLot.BatchStatID == null)
            {
                return(CreateNewProductionResultResult.OutputLotBatchStatIDIsNull);
            }

            if (BatchStatIDHelper.GetBatchStatID(outputLot.BatchStatID.Value) != BatchStatID.Produced)
            {
                return(CreateNewProductionResultResult.OutputLotBatchStatNotProduced);
            }

            if (outputLot.EntryDate == null)
            {
                return(CreateNewProductionResultResult.OutputLotEntryDateIsNull);
            }
            var timeStamp = outputLot.EntryDate.Value.ConvertLocalToUTC();

            var beginTime      = ((outputLot.BatchBegTime ?? outputLot.ProductionDate).Value).ConvertLocalToUTC();
            var endTime        = ((outputLot.BatchEndTime ?? outputLot.ProductionDate).Value).ConvertLocalToUTC();
            var productionLine = outputLot.ProductionLine ?? 1;

            var lineLocation = _newContextHelper.GetProductionLine(productionLine);

            if (lineLocation == null)
            {
                return(CreateNewProductionResultResult.OutputLotProductionLineLocationNotFound);
            }

            var deserializedResults = _serializedData.GetDeserialized <SerializableEmployeeIdentifiable>(SerializableType.LotProductionResults, outputLot.Lot.ToString());

            productionResult = new LotProductionResults
            {
                EmployeeId = deserializedResults == null ? outputLot.EmployeeID.Value : deserializedResults.EmployeeKey.EmployeeKeyId,
                TimeStamp  = deserializedResults == null ? timeStamp : deserializedResults.TimeStamp,

                LotDateCreated  = lotKey.LotKey_DateCreated,
                LotDateSequence = lotKey.LotKey_DateSequence,
                LotTypeId       = lotKey.LotKey_LotTypeId,

                ProductionLineLocationId = lineLocation.Id,

                ShiftKey        = outputLot.Shift,
                DateTimeEntered = timeStamp,
                ProductionBegin = beginTime,
                ProductionEnd   = endTime
            };

            return(CreateNewProductionResultResult.Success);
        }
        internal static LotProductionResults EmptyItems(this LotProductionResults productionResult)
        {
            if (productionResult == null)
            {
                throw new ArgumentNullException("productionResult");
            }

            productionResult.ResultItems = null;

            return(productionResult);
        }