private async Task CreateShipments(ImportShipmentsFromExcelJobArgs args, List <ImportShipmentDto> shipmentDtos) { var invalidShipments = new List <ImportShipmentDto>(); var logistics = await _logisticsManager.GetTenantLogisticsByIdAsync(args.TenantLogisticsId); foreach (var shipmentDto in shipmentDtos) { if (shipmentDto.CanBeImported()) { var order = await _orderManager.FindByOrderNumberAsync(shipmentDto.OrderNumber); if (order == null) { shipmentDto.Exception = "找不到订单"; continue; } try { var shipment = await _orderImportor.AddShipment(new OrderImport() { OrderStatus = OrderStatus.Processing, TrackingNumber = shipmentDto.LogisticsNumber, LogisticsId = logistics.LogisticsId, LogisticsName = logistics.Name, CreatedOnUtc = DateTime.UtcNow, }, order); await _orderProcessingManager.ShipAsync(shipment, false); } catch (UserFriendlyException exception) { shipmentDto.Exception = exception.Message; invalidShipments.Add(shipmentDto); } catch (Exception exception) { shipmentDto.Exception = exception.ToString(); invalidShipments.Add(shipmentDto); } } else { invalidShipments.Add(shipmentDto); } } AsyncHelper.RunSync(() => ProcessImportShipmentsResultAsync(args, invalidShipments)); }
public async Task QuickDelivery(QuickDeliveryInput input) { var order = await _orderManager.GetByIdAsync(input.OrderId); if (order == null) { throw new UserFriendlyException("无效的订单"); } await _orderManager.OrderRepository.EnsureCollectionLoadedAsync(order, o => o.Items); await _orderManager.OrderRepository.EnsureCollectionLoadedAsync(order, o => o.Shipments); Shipment shipment = null; decimal totalWeight = 0; decimal totalVolume = 0; foreach (var orderItem in order.Items.ToList()) { //是否还有订单项需要发货 var maxQtyToAdd = orderItem.GetTotalNumberOfItemsCanBeAddedToShipment(_shipmentManager); if (maxQtyToAdd <= 0) { continue; } int qtyToAdd = orderItem.Quantity; //默认 //validate quantity if (qtyToAdd <= 0) { continue; } if (qtyToAdd > maxQtyToAdd) { qtyToAdd = maxQtyToAdd; } var orderItemTotalWeight = orderItem.Weight > 0 ? orderItem.Weight * qtyToAdd : 0; totalWeight += orderItemTotalWeight; var orderItemTotalVolume = orderItem.Volume > 0 ? orderItem.Volume * qtyToAdd : 0; totalVolume += orderItemTotalVolume; if (shipment == null) { var logistics = await _logisticsManager.FindTenantLogisticsByIdAsync(input.LogisticsId); shipment = new Shipment() { OrderId = order.Id, OrderNumber = order.OrderNumber, LogisticsName = logistics.Name, LogisticsId = logistics.LogisticsId, LogisticsNumber = input.LogisticsNumber, TotalWeight = orderItemTotalWeight, TotalVolume = orderItemTotalVolume, Status = ShippingStatus.NoTrace, AdminComment = input.AdminComment, Items = new Collection <ShipmentItem>() }; } //create a shipment item var shipmentItem = new ShipmentItem() { OrderItemId = orderItem.Id, Quantity = qtyToAdd, }; shipment.Items.Add(shipmentItem); } //if we have at least one item in the shipment, then save it if (shipment != null && shipment.Items.Count > 0) { shipment.TotalWeight = totalWeight; shipment.TotalVolume = totalVolume; if (shipment.Id == 0) { await _shipmentManager.CreateAsync(shipment); } else { await _shipmentManager.UpdateAsync(shipment); } //修改状态为已发货 await _orderProcessingManager.ShipAsync(shipment, true); } }