public void OnUpdated(Shipment entity) { var orderItems = entity.ShipmentItems.Select(x => x.OrderItem).ToList(); foreach (var orderItem in orderItems) { _purchaseAccountant.EvaluateOrderStatus(orderItem.Order); } //if the shipment status is InTransit/Returned, that means we should update the inventory, as the item has been shipped/returned if (entity.ShipmentStatus == ShipmentStatus.InTransit || entity.ShipmentStatus == ShipmentStatus.Returned) { var warehouseId = entity.WarehouseId; var shippedProductIds = orderItems.Select(x => x.ProductId).ToList(); var inventories = _warehouseInventoryService.GetByProducts(shippedProductIds, warehouseId).ToList(); foreach (var shippedItem in entity.ShipmentItems) { var quantityShipped = shippedItem.Quantity; var shippedProductId = shippedItem.OrderItem.ProductId; var variantId = shippedItem.OrderItem.ProductVariantId; var inventory = shippedItem.OrderItem.ProductVariantId > 0 ? inventories.FirstOrDefault(x => x.ProductVariantId == variantId) : inventories.FirstOrDefault(x => x.ProductId == shippedProductId); if (inventory == null) { continue; //weird it's not there. but we can't do anything for this. something is wrong. this shouldn't have hit } if (entity.ShipmentStatus == ShipmentStatus.InTransit) { inventory.ReservedQuantity -= quantityShipped; inventory.TotalQuantity -= quantityShipped; } else { inventory.TotalQuantity += quantityShipped; } _warehouseInventoryService.Update(inventory); } } //update shipment history //get the history var shipmentHistoryItems = _shipmentStatusHistoryService.Get(x => x.ShipmentId == entity.Id).OrderByDescending(x => x.Id).ToList(); //if the current status is already there as the latest one, no need to do anything, if (shipmentHistoryItems.Any() && shipmentHistoryItems.First().ShipmentStatus == entity.ShipmentStatus) { return; } //we'll add this to the history now var shipmentHistory = new ShipmentHistory() { CreatedOn = DateTime.UtcNow, ShipmentStatus = entity.ShipmentStatus, ShipmentId = entity.Id }; _shipmentStatusHistoryService.Insert(shipmentHistory); }
public IList <OrderFulfillment> SaveAutOrderFulfillments(Order order) { var fulfillments = GetAutoOrderFulfillments(order); if (fulfillments != null) { Transaction.Initiate(transaction => { foreach (var fulfillment in fulfillments) { fulfillment.WarehouseInventory.ReservedQuantity += fulfillment.Quantity; //update inventory _warehouseInventoryService.Update(fulfillment.WarehouseInventory, transaction); _orderFulfillmentService.Insert(fulfillment, transaction); } }); } return(fulfillments); }