internal IResult <Dictionary <string, double> > Execute(Dictionary <IInventoryKey, int> inventoryKeysAndQuantities)
        {
            if (inventoryKeysAndQuantities == null)
            {
                throw new ArgumentNullException("inventoryKeysAndQuantities");
            }
            if (inventoryKeysAndQuantities.Count <= 0)
            {
                return(new InvalidResult <Dictionary <string, double> >(null, UserMessages.EmptySet));
            }

            var keysAndQuantities = inventoryKeysAndQuantities.Select(i => new LotPackagingKeyValueQuantity(i.Key, i.Key, i.Value)).ToList();

            var lotPredicate = PredicateHelper.OrPredicates(keysAndQuantities.DistinctBySelect(k => k.LotKeyValue, k => k.LotPredicateExpression));
            var lots         = _lotUnifOfWork.LotRepository.Filter(lotPredicate, l => l.Attributes.Select(a => a.AttributeName)).ToList();

            var packagingPredicate = PredicateHelper.OrPredicates(keysAndQuantities.DistinctBySelect(k => k.PackagingKeyValue, k => k.PackagingPredicateExpression));
            var packagingProducts  = _lotUnifOfWork.PackagingProductRepository.Filter(packagingPredicate).ToList();

            List <IWeightedLotAttributes> attributesAndWeights;

            try
            {
                attributesAndWeights = keysAndQuantities.Select(k => (IWeightedLotAttributes) new LotAttributesAndWeight(k, lots, packagingProducts)).ToList();
            }
            catch (Exception exception)
            {
                if (exception is ObjectNotFoundException)
                {
                    return(new FailureResult <Dictionary <string, double> >(null, exception.Message));
                }
                throw;
            }

            var weightedAveragesResult = CalculateWeightedAverages(attributesAndWeights);

            if (!weightedAveragesResult.Success)
            {
                return(weightedAveragesResult.ConvertTo <Dictionary <string, double> >());
            }

            return(new SuccessResult <Dictionary <string, double> >(weightedAveragesResult.ResultingObject.Where(a => a.Key.Attribute.NameActive)
                                                                    .ToDictionary(a => a.Key.Attribute.Name, a => a.Value)));
        }
Exemple #2
0
        internal IResult Execute(List <LocationKey> keys, bool @lock)
        {
            if (keys == null)
            {
                throw new ArgumentNullException("keys");
            }

            var predicate  = PredicateHelper.OrPredicates(keys.Select(k => k.FindByPredicate));
            var locations  = _facilityUnitOfWork.LocationRepository.Filter(predicate).ToList();
            var missingKey = keys.FirstOrDefault(k => !locations.Any(k.FindByPredicate.Compile()));

            if (missingKey != null)
            {
                return(new InvalidResult(string.Format(UserMessages.LocationNotFound, missingKey)));
            }

            locations.ForEach(l => l.Locked = @lock);

            return(new SuccessResult());
        }