internal IResult Execute(IEnumerable <ModifyInventoryParameters> inventoryModifications, InventoryTransactionParameters transactionParameters)
        {
            if (inventoryModifications == null)
            {
                throw new ArgumentNullException("inventoryModifications");
            }

            var transactionCommand = new CreateInventoryTransactionCommand(_inventoryUnitOfWork);

            var groupedModifications = inventoryModifications.GroupBy(a => a.InventoryKey);
            var finalModifications   = groupedModifications.Select(g => new ModifyInventoryParameters(g.Key, g.Sum(a => a.ModifyQuantity)));

            var resultingInventory = new List <Data.Models.Inventory>();

            foreach (var modification in finalModifications.Where(a => a.ModifyQuantity != 0))
            {
                var modifyResult = ModifyInventory(modification);
                if (!modifyResult.Success)
                {
                    return(modifyResult);
                }

                resultingInventory.Add(modifyResult.ResultingObject);

                if (transactionParameters != null)
                {
                    var transactionResult = transactionCommand.Create(transactionParameters, modification.InventoryKey, modification.ModifyQuantity);
                    if (!transactionResult.Success)
                    {
                        return(transactionResult);
                    }
                }
            }

            var negativeLots = resultingInventory
                               .Where(i => i != null && i.Quantity < 0)
                               .Select(i => new LotKey(i).KeyValue)
                               .Distinct().ToList();

            if (negativeLots.Any())
            {
                return(new InvalidResult(string.Format(UserMessages.NegativeInventoryLots, negativeLots.Aggregate((string)null, (c, n) => string.Format("{0}{1}", c == null ? "" : ", ", n)))));
            }

            return(new SuccessResult());
        }
        internal IResult <IntraWarehouseOrder> Create(DateTime timestamp, ICreateIntraWarehouseOrderParameters parameters)
        {
            var orderResult = GetUpdatedOrder(timestamp, null, parameters);

            if (!orderResult.Success)
            {
                return(orderResult);
            }
            var order = orderResult.ResultingObject;

            order.TrackingSheetNumber = parameters.TrackingSheetNumber;

            if (parameters.PickedItems == null || !parameters.PickedItems.Any())
            {
                return(new InvalidResult <IntraWarehouseOrder>(null, UserMessages.PickedItemsManifestRequired));
            }

            var items = parameters.PickedItems.ToParsedParameters();

            if (!items.Success)
            {
                return(items.ConvertTo <IntraWarehouseOrder>());
            }

            var locationKey = items.ResultingObject.Select(i => new LocationKey(i.InventoryKey)).First();
            var location    = UnitOfWork.LocationRepository.FindByKey(locationKey, l => l.Facility.Locations);

            if (location == null)
            {
                return(new InvalidResult <IntraWarehouseOrder>(null, string.Format(UserMessages.LocationNotFound, locationKey)));
            }

            var locationKeys = location.Facility.Locations.Select(l => new LocationKey(l)).ToList();

            if (items.ResultingObject.Any(i => locationKeys.All(k => !k.Equals(i.InventoryKey)) || locationKeys.All(k => !k.Equals(i.CurrentLocationKey))))
            {
                return(new InvalidResult <IntraWarehouseOrder>(null, UserMessages.IntraWarehouseOrderDifferentWarehouses));
            }

            var pickedResult = UpdatePickedInventory(null, order.Employee, order.TimeStamp, order.PickedInventory, items.ResultingObject, true);

            if (!pickedResult.Success)
            {
                return(pickedResult.ConvertTo <IntraWarehouseOrder>());
            }

            var transactionParameters    = new InventoryTransactionParameters(order, order.TimeStamp, InventoryTransactionType.InternalMovement, order.TrackingSheetNumber.ToString());
            var createTransactionCommand = new CreateInventoryTransactionCommand(UnitOfWork);

            foreach (var item in items.ResultingObject)
            {
                var result = createTransactionCommand.Create(transactionParameters, item.InventoryKey, -item.Quantity);
                if (!result.Success)
                {
                    return(result.ConvertTo <IntraWarehouseOrder>());
                }

                result = createTransactionCommand.Create(transactionParameters, new InventoryKey(item.InventoryKey, item.InventoryKey, item.CurrentLocationKey, item.InventoryKey, item.InventoryKey.InventoryKey_ToteKey), item.Quantity);
                if (!result.Success)
                {
                    return(result.ConvertTo <IntraWarehouseOrder>());
                }
            }

            return(new SuccessResult <IntraWarehouseOrder>(order));
        }
Exemplo n.º 3
0
        private IResult <LotProductionResults> SetProductionResults(ProductionBatch productionBatch, DateTime timestamp, ProductionResultParameters parameters, bool logOriginalPicked)
        {
            if (productionBatch == null)
            {
                throw new ArgumentNullException("productionBatch");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            var employee = new GetEmployeeCommand(_productionUnitOfWork).GetEmployee(parameters.Parameters);

            if (!employee.Success)
            {
                return(employee.ConvertTo <LotProductionResults>());
            }

            var productionLine = _productionUnitOfWork.LocationRepository.FindByKey(parameters.ProductionLineLocationKey);

            if (productionLine == null)
            {
                return(new InvalidResult <LotProductionResults>(null, string.Format(UserMessages.ProductionLocationNotFound, parameters.ProductionLineLocationKey)));
            }

            productionBatch.ProductionHasBeenCompleted          = true;
            productionBatch.Production.PickedInventory.Archived = true;
            productionBatch.Production.ResultingChileLot.Lot.ProductionStatus = LotProductionStatus.Produced;

            var productionResults = productionBatch.Production.Results;

            productionResults.EmployeeId               = employee.ResultingObject.EmployeeId;
            productionResults.TimeStamp                = timestamp;
            productionResults.ProductionLineLocation   = productionLine;
            productionResults.ProductionLineLocationId = productionLine.Id;
            productionResults.ProductionBegin          = parameters.Parameters.ProductionStartTimestamp;
            productionResults.ProductionEnd            = parameters.Parameters.ProductionEndTimestamp;
            productionResults.ShiftKey = parameters.Parameters.ProductionShiftKey;

            List <ModifyInventoryParameters> inventoryModifications = null;
            var updateResultItems = new SetLotProductionResultItemsCommand(_productionUnitOfWork).Execute(productionResults, parameters.InventoryItems, ref inventoryModifications);

            if (!updateResultItems.Success)
            {
                return(updateResultItems.ConvertTo <LotProductionResults>());
            }

            var transactionParameters = new InventoryTransactionParameters(employee.ResultingObject, timestamp, InventoryTransactionType.ProductionResults, parameters.TransactionSourceReference, productionResults);

            if (logOriginalPicked)
            {
                var logPicked = new CreateInventoryTransactionCommand(_productionUnitOfWork).LogPickedInventory(transactionParameters, productionResults.Production.PickedInventory.Items);
                if (!logPicked.Success)
                {
                    return(logPicked.ConvertTo <LotProductionResults>());
                }
            }

            var updatePickedItems = UpdatePickedItems(employee.ResultingObject, timestamp, productionBatch, parameters.PickedInventoryItemChanges, ref inventoryModifications);

            if (!updatePickedItems.Success)
            {
                return(updatePickedItems.ConvertTo <LotProductionResults>());
            }

            var modifyInventory = new ModifyInventoryCommand(_productionUnitOfWork).Execute(inventoryModifications, transactionParameters);

            if (!modifyInventory.Success)
            {
                return(modifyInventory.ConvertTo <LotProductionResults>());
            }

            return(new SuccessResult <LotProductionResults>(productionResults));
        }