private IResult ValidateModifyPickedInventoryItems(InventoryShipmentOrder order, IEnumerable <ModifyPickedInventoryItemParameters> items)
        {
            var validator  = PickedInventoryValidator.ForInterWarehouseOrder(order.SourceFacility);
            var orderItems = order.InventoryPickOrder.Items.ToDictionary(i => i.ToInventoryPickOrderItemKey());

            foreach (var item in items)
            {
                InventoryPickOrderItem orderItem = null;
                if (item.OrderItemKey != null && !orderItems.TryGetValue(item.OrderItemKey, out orderItem))
                {
                    return(new InvalidResult(string.Format(UserMessages.CannotPickForOrderItem_DoesNotExist, item.OrderItemKey)));
                }

                if (item.NewQuantity > item.OriginalQuantity)
                {
                    Dictionary <string, Inventory> inventory;
                    var validResult = validator.ValidateItems(_inventoryShipmentUnitOfWork.InventoryRepository, new[] { item.InventoryKey }, out inventory);

                    IChileProductKey chileProduct;
                    if (orderItem != null)
                    {
                        chileProduct = ChileProductKey.FromProductKey(orderItem.Product);
                    }
                    else
                    {
                        var lotKey = inventory.Single().Value.ToLotKey();
                        chileProduct = _inventoryShipmentUnitOfWork.ChileLotRepository.FindByKey(lotKey);
                        if (chileProduct == null)
                        {
                            return(new InvalidResult(string.Format(UserMessages.ChileLotNotFound, lotKey)));
                        }
                    }

                    IDictionary <AttributeNameKey, ChileProductAttributeRange>    productSpec;
                    IDictionary <AttributeNameKey, CustomerProductAttributeRange> customerSpec;
                    var specResult = new GetProductSpecCommand(_inventoryShipmentUnitOfWork).Execute(chileProduct, orderItem == null ? null : orderItem.Customer, out productSpec, out customerSpec);
                    if (!specResult.Success)
                    {
                        return(specResult);
                    }

                    var inventoryItem  = inventory.Values.Single();
                    var context        = new PickingValidatorContext(productSpec, customerSpec, null, null, orderItem == null ? null : orderItem.Customer);
                    var pickable       = new PickingValidator(inventoryItem.Lot);
                    var pickableResult = pickable.ValidForPicking(context);

                    if (!validResult.Success || !pickableResult.Success)
                    {
                        return(new InvalidResult(new[] { validResult, pickableResult }.CombineMessages()));
                    }
                }
            }

            return(new SuccessResult());
        }
        private IResult ValidateModifyPickedInventoryItems(SalesOrder salesOrder, IEnumerable <ModifySalesOrderPickedInventoryItemParameters> items)
        {
            var validator  = PickedInventoryValidator.ForSalesOrder(salesOrder.InventoryShipmentOrder.SourceFacility);
            var orderItems = salesOrder.SalesOrderItems.ToDictionary(i => i.ToSalesOrderItemKey());

            foreach (var item in items)
            {
                SalesOrderItem orderItem;
                if (!orderItems.TryGetValue(item.SalesOrderItemKey, out orderItem))
                {
                    return(new InvalidResult(string.Format(UserMessages.CannotPickForCustomerOrderItem_DoesNotExist, item.SalesOrderItemKey.KeyValue)));
                }

                if (item.NewQuantity > item.OriginalQuantity)
                {
                    IDictionary <AttributeNameKey, ChileProductAttributeRange>    productSpec;
                    IDictionary <AttributeNameKey, CustomerProductAttributeRange> customerSpec;
                    var specResult = new GetProductSpecCommand(_salesUnitOfWork).Execute(ChileProductKey.FromProductKey(orderItem.InventoryPickOrderItem),
                                                                                         orderItem.Order, out productSpec, out customerSpec);
                    if (!specResult.Success)
                    {
                        return(specResult);
                    }

                    Dictionary <string, Inventory> inventory;
                    var validResult    = validator.ValidateItems(_salesUnitOfWork.InventoryRepository, new[] { item.InventoryKey }, out inventory);
                    var inventoryItem  = inventory.Values.Single();
                    var context        = new PickingValidatorContext(productSpec, customerSpec, orderItem.ContractItem, orderItem, orderItem.Order);
                    var pickable       = new PickingValidator(inventoryItem.Lot);
                    var pickableResult = pickable.ValidForPicking(context);

                    if (!validResult.Success || !pickableResult.Success)
                    {
                        return(new InvalidResult(new[] { validResult, pickableResult }.CombineMessages()));
                    }
                }
            }

            return(new SuccessResult());
        }
        private void SetValidToPick(LabReportChileLotReturn chileLot)
        {
            var customerKey = chileLot.PackScheduleBaseReturn != null && chileLot.PackScheduleBaseReturn.Customer != null ? chileLot.PackScheduleBaseReturn.Customer.CustomerKeyReturn : null;
            IDictionary <AttributeNameKey, ChileProductAttributeRange>    productSpec;
            IDictionary <AttributeNameKey, CustomerProductAttributeRange> customerSpec;

            if (!new GetProductSpecCommand(_lotUnitOfWork).Execute(ChileProductKey.FromProductKey(chileLot.ChileProductKeyReturn), customerKey, out productSpec, out customerSpec).Success)
            {
                chileLot.ValidToPick = true;
            }

            var lot = _lotUnitOfWork.LotRepository.FindByKey(chileLot.LotKeyReturn.ToLotKey(),
                                                             l => l.ContractAllowances,
                                                             l => l.SalesOrderAllowances,
                                                             l => l.CustomerAllowances,
                                                             l => l.Attributes,
                                                             l => l.AttributeDefects.Select(a => a.LotDefect.Resolution));
            var context   = new PickingValidatorContext(productSpec, customerSpec, null, null, customerKey);
            var validator = new PickingValidator(lot).ValidForPicking(context);

            chileLot.ValidToPick = validator.Success;
        }