Exemplo n.º 1
0
        public IResult <IQueryable <IInventoryTransactionReturn> > GetInventoryTransactions(string lotKey)
        {
            var lotKeyResult = KeyParserHelper.ParseResult <ILotKey>(lotKey);

            if (!lotKeyResult.Success)
            {
                return(lotKeyResult.ConvertTo <IQueryable <IInventoryTransactionReturn> >());
            }
            var parsedLotKey = lotKeyResult.ResultingObject.ToLotKey();

            var predicate = InventoryTransactionPredicates.BySourceLot(parsedLotKey);
            var select    = InventoryTransactionProjectors.Select(_inventoryUnitOfWork);

            var query = _inventoryUnitOfWork.InventoryTransactionsRepository.Filter(predicate).Select(select);

            var pickedPredicate = PickedInventoryItemPredicates.FilterByLotKey(parsedLotKey);
            var pickedSelect    = PickedInventoryItemProjectors.SelectTransaction(_inventoryUnitOfWork);
            var pickedResults   = _inventoryUnitOfWork.ProductionBatchRepository
                                  .Filter(b => new[] { b.Production.Results }.All(r => r == null))
                                  .AsExpandable()
                                  .SelectMany(b => b.Production.PickedInventory.Items
                                              .Where(i => pickedPredicate.Invoke(i))
                                              .Select(i => pickedSelect.Invoke(i, b)));

            var results = query.ToList <IInventoryTransactionReturn>().Concat(pickedResults).AsQueryable();

            return(new SuccessResult <IQueryable <IInventoryTransactionReturn> >(results));
        }
        protected IResult <ChileMaterialsReceived> Set(ChileMaterialsReceived received, ChileProduct chileProduct, ISetChileMaterialsReceivedParameters parameters, Employee employee, DateTime timeStamp)
        {
            if (received.ChileProductId != chileProduct.Id)
            {
                if (MaterialsReceivedUnitOfWork.PickedInventoryItemRepository.Filter(PickedInventoryItemPredicates.FilterByLotKey(received)).Any())
                {
                    return(new InvalidResult <ChileMaterialsReceived>(null, string.Format(UserMessages.LotHasExistingPickedInventory, received.ToLotKey())));
                }
            }

            var previousTreatment = received.ToInventoryTreatmentKey();
            var treatment         = MaterialsReceivedUnitOfWork.InventoryTreatmentRepository.FindByKey(parameters.TreatmentKey);

            if (treatment == null)
            {
                return(new InvalidResult <ChileMaterialsReceived>(null, string.Format(UserMessages.InventoryTreatmentNotFound, parameters.TreatmentKey)));
            }

            var supplier = MaterialsReceivedUnitOfWork.CompanyRepository.FindByKey(parameters.SupplierKey, c => c.CompanyTypes);

            if (supplier == null)
            {
                return(new InvalidResult <ChileMaterialsReceived>(null, string.Format(UserMessages.SupplierCompanyNotFound, parameters.SupplierKey)));
            }

            if (parameters.ChileMaterialsReceivedType == ChileMaterialsReceivedType.Dehydrated && supplier.CompanyTypes.All(c => c.CompanyTypeEnum != CompanyType.Dehydrator))
            {
                return(new InvalidResult <ChileMaterialsReceived>(null, string.Format(UserMessages.CompanyNotOfType, parameters.SupplierKey, CompanyType.Dehydrator)));
            }

            received.EmployeeId = employee.EmployeeId;
            received.TimeStamp  = timeStamp;

            received.ChileMaterialsReceivedType = parameters.ChileMaterialsReceivedType;
            received.DateReceived = parameters.DateReceived;
            received.LoadNumber   = parameters.LoadNumber;
            received.ChileLot.Lot.PurchaseOrderNumber = parameters.PurchaseOrder;
            received.ChileLot.Lot.ShipperNumber       = parameters.ShipperNumber;

            received.TreatmentId = treatment.Id;

            received.ChileProductId          = chileProduct.Id;
            received.ChileLot.ChileProductId = chileProduct.Id;

            received.Supplier   = supplier;
            received.SupplierId = supplier.Id;

            var setItemsResult = SetItems(received, parameters.Items, previousTreatment, employee, timeStamp);

            if (!setItemsResult.Success)
            {
                return(setItemsResult.ConvertTo <ChileMaterialsReceived>());
            }

            return(new SuccessResult <ChileMaterialsReceived>(received));
        }
        protected IResult <ChileLotProduction> SetMillAndWetdown <TParams>(ChileLotProduction millAndWetdown, SetMillAndWetdownParameters <TParams> parameters, DateTime timestamp, IEmployeeKey employee)
            where TParams : ISetMillAndWetdownParameters
        {
            millAndWetdown.ProductionType = ProductionType.MillAndWetdown;
            millAndWetdown.EmployeeId     = employee.EmployeeKey_Id;
            millAndWetdown.TimeStamp      = timestamp;

            millAndWetdown.Results.EmployeeId = employee.EmployeeKey_Id;
            millAndWetdown.Results.TimeStamp  = timestamp;
            millAndWetdown.Results.ProductionLineLocationId = parameters.ProductionLineKey.LocationKey_Id;
            millAndWetdown.Results.ShiftKey        = parameters.Params.ShiftKey;
            millAndWetdown.Results.ProductionBegin = parameters.Params.ProductionBegin;
            millAndWetdown.Results.ProductionEnd   = parameters.Params.ProductionEnd;

            var lotKey = new LotKey(millAndWetdown);

            if (!parameters.ChileProductKey.Equals(millAndWetdown.ResultingChileLot))
            {
                var chileProduct = ProductionUnitOfWork.ChileProductRepository.FindByKey(parameters.ChileProductKey);
                if (chileProduct == null)
                {
                    return(new InvalidResult <ChileLotProduction>(null, string.Format(UserMessages.ChileProductNotFound, parameters.ChileProductKey)));
                }

                if (chileProduct.ChileState.ToLotType() != millAndWetdown.ResultingChileLot.ChileProduct.ChileState.ToLotType())
                {
                    return(new InvalidResult <ChileLotProduction>(null, UserMessages.ChileProductDifferentLotType));
                }

                if (ProductionUnitOfWork.PickedInventoryItemRepository.Filter(PickedInventoryItemPredicates.FilterByLotKey(lotKey)).Any())
                {
                    return(new InvalidResult <ChileLotProduction>(null, string.Format(UserMessages.LotHasExistingPickedInventory, lotKey)));
                }

                millAndWetdown.ResultingChileLot.ChileProductId = parameters.ChileProductKey.ChileProductKey_ProductId;
            }

            var pickedInventoryItemModifications = PickedInventoryHelper.CreateModifyPickedInventoryItemParameters(millAndWetdown.PickedInventory, parameters.PickedItems);
            var validationResults = PickedInventoryValidator.ForMillAndWetdown.ValidateItems(ProductionUnitOfWork.InventoryRepository, pickedInventoryItemModifications.Where(i => i.DeltaQuantity > 0).Select(i => i.InventoryKey));

            if (!validationResults.Success)
            {
                return(validationResults.ConvertTo <ChileLotProduction>());
            }

            var modifyPickedResult = new ModifyPickedInventoryItemsCommand(ProductionUnitOfWork).Execute(millAndWetdown, pickedInventoryItemModifications);

            if (!modifyPickedResult.Success)
            {
                return(modifyPickedResult.ConvertTo <ChileLotProduction>());
            }

            millAndWetdown.PickedInventory.Archived = true;
            var inventoryModifications = pickedInventoryItemModifications.Select(i => i.ToModifySourceInventoryParameters()).ToList();
            var setResults             = new SetLotProductionResultItemsCommand(ProductionUnitOfWork).Execute(millAndWetdown.Results, parameters.ResultItems, ref inventoryModifications);

            if (!setResults.Success)
            {
                return(setResults.ConvertTo <ChileLotProduction>());
            }

            var transaction           = new InventoryTransactionParameters(employee, timestamp, InventoryTransactionType.CreatedMillAndWetdown, lotKey, millAndWetdown);
            var modifyInventoryResult = new ModifyInventoryCommand(ProductionUnitOfWork).Execute(inventoryModifications, transaction);

            if (!modifyInventoryResult.Success)
            {
                return(modifyInventoryResult.ConvertTo <ChileLotProduction>());
            }

            return(new SuccessResult <ChileLotProduction>(millAndWetdown));
        }
Exemplo n.º 4
0
        private IResult <List <LotData> > GetLotData(params LotKey[] lotKeys)
        {
            var lotPredicate = lotKeys.Aggregate(PredicateBuilder.False <Lot>(), (c, n) => c.Or(n.FindByPredicate)).ExpandAll();
            var lots         = _lotUnitOfWork.LotRepository
                               .Filter(lotPredicate,
                                       l => l.ChileLot.ChileProduct.ProductAttributeRanges.Select(r => r.AttributeName),
                                       l => l.Inventory.Select(i => i.Treatment),
                                       l => l.Attributes.Select(a => a.AttributeName),
                                       l => l.LotDefects.Select(d => d.Resolution),
                                       l => l.AttributeDefects.Select(d => d.LotDefect.Resolution))
                               .ToList();

            var lotsReference = lots.ToList();
            var missingKeys   = lotKeys.Where(k => lotsReference.RemoveAll(k.Equals) < 1).ToList();

            if (missingKeys.Any())
            {
                return(new InvalidResult <List <LotData> >(null, string.Format(UserMessages.LotNotFound, missingKeys.First())));
            }

            var pickedPredicate = lotKeys
                                  .Aggregate(PredicateBuilder.False <PickedInventoryItem>(), (c, n) => c.Or(PickedInventoryItemPredicates.FilterByLotKey(n)))
                                  .And(i => !i.PickedInventory.Archived).ExpandAll();
            var pickedInventoryItems = _lotUnitOfWork.PickedInventoryItemRepository.Filter(pickedPredicate,
                                                                                           i => i.PickedInventory,
                                                                                           i => i.Treatment)
                                       .ToList()
                                       .GroupBy(i => i.ToLotKey())
                                       .ToDictionary(g => g.Key, g => g.ToList());

            var results = lots.Select(l =>
            {
                var lotKey = l.ToLotKey();
                List <PickedInventoryItem> picked;
                if (!pickedInventoryItems.TryGetValue(lotKey, out picked))
                {
                    picked = new List <PickedInventoryItem>();
                }

                return(new LotData
                {
                    Lot = l,
                    Picked = picked,
                });
            }).ToList();

            return(new SuccessResult <List <LotData> >(results));
        }