| | | 1 | | |
| | | 2 | | namespace Services.BitrixStockServices; |
| | | 3 | | |
| | | 4 | | /// <summary> |
| | | 5 | | /// Агрегация заказов «Не указан» / «Заказан реализатору» в карту резерва (артикул + склад). |
| | | 6 | | /// </summary> |
| | | 7 | | public static class OrderReserveAggregation |
| | | 8 | | { |
| | | 9 | | public static string ResolveOrderNumber(string? shipmentNumber, string? key, int id) |
| | | 10 | | { |
| | | 11 | | if (!string.IsNullOrWhiteSpace(shipmentNumber)) |
| | | 12 | | return shipmentNumber.Trim(); |
| | | 13 | | if (!string.IsNullOrWhiteSpace(key)) |
| | | 14 | | return key.Trim(); |
| | | 15 | | return $"#{id}"; |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | public static int ResolveReserveQty(int? quantity, string? productKey) |
| | | 19 | | { |
| | | 20 | | var qty = quantity ?? 1; |
| | | 21 | | return qty * OzonDomains.Nomenclature.ProductKeyParser.BatchMultiplier(productKey); |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | public static string? ResolveWarehouseName(string? warehouseName, string? zasylValue) => |
| | | 25 | | !string.IsNullOrWhiteSpace(warehouseName) ? warehouseName.Trim() |
| | | 26 | | : !string.IsNullOrWhiteSpace(zasylValue) ? zasylValue.Trim() |
| | | 27 | | : null; |
| | | 28 | | |
| | | 29 | | public static List<OrderReserveLineDto> BuildLines( |
| | | 30 | | IEnumerable<OrderReserveSourceRow> rows, |
| | | 31 | | IReadOnlySet<string>? storeFilter) |
| | | 32 | | { |
| | | 33 | | var grouped = new Dictionary<(string Article, string Store), (int Qty, Dictionary<string, OrderReserveOrderDto> |
| | | 34 | | |
| | | 35 | | foreach (var row in rows) |
| | | 36 | | { |
| | | 37 | | if (string.IsNullOrWhiteSpace(row.Article)) continue; |
| | | 38 | | if (string.IsNullOrWhiteSpace(row.StatusName)) continue; |
| | | 39 | | |
| | | 40 | | var warehouseName = ResolveWarehouseName(row.WarehouseName, row.ZasylValue); |
| | | 41 | | if (warehouseName == null) continue; |
| | | 42 | | |
| | | 43 | | if (storeFilter is { Count: > 0 } && !storeFilter.Contains(warehouseName)) |
| | | 44 | | continue; |
| | | 45 | | |
| | | 46 | | var qty = ResolveReserveQty(row.Quantity, row.ProductKey); |
| | | 47 | | var orderNumber = ResolveOrderNumber(row.ShipmentNumber, row.Key, row.Id); |
| | | 48 | | var key = (row.Article.Trim(), warehouseName); |
| | | 49 | | var orderDto = new OrderReserveOrderDto |
| | | 50 | | { |
| | | 51 | | OrderNumber = orderNumber, |
| | | 52 | | ShipmentNumber = string.IsNullOrWhiteSpace(row.ShipmentNumber) ? null : row.ShipmentNumber.Trim(), |
| | | 53 | | ProcessingDate = row.ProcessingDate, |
| | | 54 | | StatusName = row.StatusName.Trim(), |
| | | 55 | | StatusColor = string.IsNullOrWhiteSpace(row.StatusColor) ? "#ffffff" : row.StatusColor.Trim(), |
| | | 56 | | }; |
| | | 57 | | |
| | | 58 | | if (grouped.TryGetValue(key, out var existing)) |
| | | 59 | | { |
| | | 60 | | existing.Qty += qty; |
| | | 61 | | existing.Orders[orderNumber] = orderDto; |
| | | 62 | | grouped[key] = existing; |
| | | 63 | | } |
| | | 64 | | else |
| | | 65 | | { |
| | | 66 | | grouped[key] = (qty, new Dictionary<string, OrderReserveOrderDto>(StringComparer.OrdinalIgnoreCase) |
| | | 67 | | { |
| | | 68 | | [orderNumber] = orderDto |
| | | 69 | | }); |
| | | 70 | | } |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | return grouped |
| | | 74 | | .Select(kvp => new OrderReserveLineDto |
| | | 75 | | { |
| | | 76 | | Article = kvp.Key.Article, |
| | | 77 | | Store = kvp.Key.Store, |
| | | 78 | | ReservedQty = kvp.Value.Qty, |
| | | 79 | | OrderCount = kvp.Value.Orders.Count, |
| | | 80 | | OrderNumbers = kvp.Value.Orders.Keys |
| | | 81 | | .OrderBy(n => n, StringComparer.OrdinalIgnoreCase) |
| | | 82 | | .ToList(), |
| | | 83 | | Orders = kvp.Value.Orders.Values |
| | | 84 | | .OrderBy(o => o.OrderNumber, StringComparer.OrdinalIgnoreCase) |
| | | 85 | | .ToList(), |
| | | 86 | | }) |
| | | 87 | | .OrderBy(l => l.Article, StringComparer.OrdinalIgnoreCase) |
| | | 88 | | .ThenBy(l => l.Store, StringComparer.OrdinalIgnoreCase) |
| | | 89 | | .ToList(); |
| | | 90 | | } |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | public sealed class OrderReserveSourceRow |
| | | 94 | | { |
| | 10 | 95 | | public int Id { get; init; } |
| | 16 | 96 | | public string? Article { get; init; } |
| | 4 | 97 | | public string? ProductKey { get; init; } |
| | 10 | 98 | | public int? Quantity { get; init; } |
| | 11 | 99 | | public string? WarehouseName { get; init; } |
| | 5 | 100 | | public string? ZasylValue { get; init; } |
| | 18 | 101 | | public string? ShipmentNumber { get; init; } |
| | 4 | 102 | | public string? Key { get; init; } |
| | 4 | 103 | | public DateTime? ProcessingDate { get; init; } |
| | 16 | 104 | | public string? StatusName { get; init; } |
| | 8 | 105 | | public string? StatusColor { get; init; } |
| | | 106 | | } |