public void AssignOrderItemToBatch(int batchId, IPurchaseOrder order, long orderItemId, decimal assignmentQuantity) { var orderItem = GetAllOrderItems(order).FirstOrDefault(i => i.Id == orderItemId); if (orderItem == null) { throw new InvalidOperationException("Invalid OrderItemId"); } var material = m_virtualProductFacade.GetOrderItemMaterialForSingleUnit(order, orderItem); var batch = m_batchRepository.GetBatchById(batchId); if (batch == null) { throw new InvalidOperationException("Invalid batch reference"); } if (material.MaterialId != batch.Batch.MaterialId) { throw new InvalidOperationException("Batch material mismatch"); } if (batch.IsLocked) { throw new InvalidOperationException($"Šarže '{batch.Batch?.BatchNumber}' je zamčená"); } if (batch.IsClosed) { throw new InvalidOperationException($"Šarže '{batch.Batch?.BatchNumber}' je uzavřená"); } ReleaseBatchAmountCache(batchId); using (var tx = m_database.OpenTransaction()) { var available = GetAvailableAmount(batchId); var subtracted = m_amountProcessor.Subtract( available, new Amount(assignmentQuantity, material.Amount.Unit)); if (subtracted.Value < 0m) { throw new InvalidOperationException($"Požadované množství {new Amount(assignmentQuantity, material.Amount.Unit)} již není k dispozici v šarži {batch.Batch.BatchNumber}"); } m_orderRepository.UpdateOrderItemBatch(orderItem, batchId, assignmentQuantity); tx.Commit(); ReleaseBatchAmountCache(batchId); } }
public PackingOrderModel SetItemBatchAllocation(BatchAllocationChangeRequest request) { WebSession.EnsureUserRight(OrdersPackingUserRights.OrderBatchAssignment); PackingOrderModel result; using (var tx = m_database.OpenTransaction()) { var order = m_orderRepository.GetOrder(request.OrderId); if (order == null) { throw new InvalidOperationException("Objednávka nebyla v systému nalezena"); } if (!string.IsNullOrWhiteSpace(request.OriginalBatchNumber)) { m_batchFacade.ChangeOrderItemBatchAssignment( order, request.OrderItemId, request.OriginalBatchNumber, request.NewAmount); order = m_orderRepository.GetOrder(order.Id); if (request.NewAmount > 0m) { request.NewBatchSearchQuery = request.NewBatchSearchQuery ?? request.OriginalBatchNumber; } } var item = m_ordersFacade.GetAllConcreteOrderItems(order).FirstOrDefault(i => i.Id == request.OrderItemId).Ensure(); if (!string.IsNullOrWhiteSpace(request.NewBatchSearchQuery) && ((request.NewAmount ?? item.Quantity) > 0m)) { var material = m_virtualProductFacade.GetOrderItemMaterialForSingleUnit(order, item); var batch = m_batchFacade.FindBatchBySearchQuery(material.MaterialId, request.NewBatchSearchQuery); result = MapOrder( order, new Tuple <long, BatchKey, decimal>(item.Id, batch, request.NewAmount ?? item.Quantity)); } else { result = MapOrder(order); } tx.Commit(); return(result); } }