internal IResult Execute(LotKey lotKey) { var includePaths = DeleteLotConductor.ConstructIncludePaths <ChileLotProduction>(p => p.ResultingChileLot.Lot, p => p.PickedInventory.Items.Select(i => i.CurrentLocation), p => p.Results.ResultItems).ToArray(); var lotProduction = _productionUnitOfWork.ChileLotProductionRepository.FindByKey(lotKey, includePaths); if (lotProduction == null) { return(new NoWorkRequiredResult()); } if (lotProduction.ProductionType != ProductionType.MillAndWetdown) { return(new InvalidResult(string.Format(UserMessages.MillAndWetdownEntryNotFound, lotKey))); } var pickedInventoryItemModifications = PickedInventoryHelper.CreateModifyPickedInventoryItemParameters(lotProduction.PickedInventory, new List <PickedInventoryParameters>()); var modifyPickedResult = new ModifyPickedInventoryItemsCommand(_productionUnitOfWork).Execute(lotProduction, pickedInventoryItemModifications); if (!modifyPickedResult.Success) { return(modifyPickedResult.ConvertTo <ChileLotProduction>()); } var inventoryModifications = pickedInventoryItemModifications.Select(i => i.ToModifySourceInventoryParameters()).ToList(); var setResults = new SetLotProductionResultItemsCommand(_productionUnitOfWork).Execute(lotProduction.Results, new List <IProductionResultItemParameters>(), ref inventoryModifications); if (!setResults.Success) { return(setResults.ConvertTo <ChileLotProduction>()); } var modifyInventoryResult = new ModifyInventoryCommand(_productionUnitOfWork).Execute(inventoryModifications, null); if (!modifyInventoryResult.Success) { return(modifyInventoryResult.ConvertTo <ChileLotProduction>()); } _productionUnitOfWork.LotProductionResultsRepository.Remove(lotProduction.Results); _productionUnitOfWork.PickedInventoryRepository.Remove(lotProduction.PickedInventory); var deleteLotResult = new DeleteLotConductor(_productionUnitOfWork).Delete(lotProduction.ResultingChileLot.Lot); if (!deleteLotResult.Success) { return(deleteLotResult); } return(new SuccessResult()); }
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)); }
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)); }