internal IResult <InventoryTransaction> Create(InventoryTransactionParameters parameters, IInventoryKey inventoryKey, int quantity) { var date = parameters.TimeStamp.Date; var sequence = new EFUnitOfWorkHelper(_coreUnitOfWork).GetNextSequence <InventoryTransaction>(t => t.DateCreated == date, t => t.Sequence); var setDestinationLot = quantity < 0 && parameters.DestinationLotKey != null; var transaction = _coreUnitOfWork.InventoryTransactionsRepository.Add(new InventoryTransaction { DateCreated = date, Sequence = sequence, EmployeeId = parameters.EmployeeKey.EmployeeKey_Id, TimeStamp = parameters.TimeStamp, TransactionType = parameters.TransactionType, Description = parameters.Description, SourceReference = parameters.SourceReference, SourceLotDateCreated = inventoryKey.LotKey_DateCreated, SourceLotDateSequence = inventoryKey.LotKey_DateSequence, SourceLotTypeId = inventoryKey.LotKey_LotTypeId, PackagingProductId = inventoryKey.PackagingProductKey_ProductId, LocationId = inventoryKey.LocationKey_Id, TreatmentId = inventoryKey.InventoryTreatmentKey_Id, ToteKey = inventoryKey.InventoryKey_ToteKey, DestinationLotDateCreated = setDestinationLot ? parameters.DestinationLotKey.LotKey_DateCreated : (DateTime?)null, DestinationLotDateSequence = setDestinationLot ? parameters.DestinationLotKey.LotKey_DateSequence : (int?)null, DestinationLotTypeId = setDestinationLot ? parameters.DestinationLotKey.LotKey_LotTypeId : (int?)null, Quantity = quantity }); return(new SuccessResult <InventoryTransaction>(transaction)); }
internal IResult LogPickedInventory(InventoryTransactionParameters parameters, IEnumerable <PickedInventoryItem> items) { foreach (var item in items) { var createResult = Create(parameters, new InventoryKey(item, item, new LocationKey(item.FromLocationId), item, item.ToteKey), -item.Quantity); if (!createResult.Success) { return(createResult); } } 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)); }
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)); }
internal IResult Post(Parameters parameters, DateTime timestamp) { if (parameters == null) { throw new ArgumentNullException("parameters"); } var employeeResult = new GetEmployeeCommand(_inventoryShipmentOrderUnitOfWork).GetEmployee(parameters.Params); if (!employeeResult.Success) { return(employeeResult); } var order = _inventoryShipmentOrderUnitOfWork.InventoryShipmentOrderRepository.FindByKey(parameters.InventoryShipmentOrderKey, o => o.ShipmentInformation, o => o.PickedInventory.Items, o => o.DestinationFacility.Locations); if (order == null) { return(new InvalidResult(string.Format(UserMessages.InventoryShipmentOrderNotFound, parameters.InventoryShipmentOrderKey))); } var moveResult = PostOrder(parameters, order, employeeResult.ResultingObject); if (!moveResult.Success) { return(moveResult); } var sourceReference = parameters.InventoryShipmentOrderKey.KeyValue; InventoryTransactionType transactionType; switch (order.OrderType) { case InventoryShipmentOrderTypeEnum.InterWarehouseOrder: order.OrderStatus = OrderStatus.Fulfilled; order.PickedInventory.Archived = true; transactionType = InventoryTransactionType.PostedInterWarehouseOrder; break; case InventoryShipmentOrderTypeEnum.TreatmentOrder: transactionType = InventoryTransactionType.PostedTreatmentOrder; break; case InventoryShipmentOrderTypeEnum.SalesOrder: order.OrderStatus = OrderStatus.Fulfilled; order.PickedInventory.Archived = true; transactionType = InventoryTransactionType.PostedCustomerOrder; if (order.MoveNum != null) { sourceReference = order.MoveNum.ToString(); } break; case InventoryShipmentOrderTypeEnum.MiscellaneousOrder: order.OrderStatus = OrderStatus.Fulfilled; order.PickedInventory.Archived = true; transactionType = InventoryTransactionType.PostedMiscellaneousOrder; if (order.MoveNum != null) { sourceReference = order.MoveNum.ToString(); } break; default: throw new ArgumentOutOfRangeException("order.OrderType"); } var transactionParameters = new InventoryTransactionParameters(employeeResult.ResultingObject, timestamp, transactionType, sourceReference); var modifyResult = new ModifyInventoryCommand(_inventoryShipmentOrderUnitOfWork).Execute(moveResult.ResultingObject, transactionParameters); if (!modifyResult.Success) { return(modifyResult); } return(new CreateInventoryTransactionCommand(_inventoryShipmentOrderUnitOfWork).LogPickedInventory(transactionParameters, order.PickedInventory.Items)); }
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()); }