< Summary

Information
Class: Services.BitrixStockServices.OrderReserveAggregation
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/BitrixStockServices/OrderReserveAggregation.cs
Line coverage
100%
Covered lines: 53
Uncovered lines: 0
Coverable lines: 53
Total lines: 106
Line coverage: 100%
Branch coverage
92%
Covered branches: 26
Total branches: 28
Branch coverage: 92.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ResolveOrderNumber(...)100%44100%
ResolveReserveQty(...)100%11100%
ResolveWarehouseName(...)75%44100%
BuildLines(...)95%2020100%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/BitrixStockServices/OrderReserveAggregation.cs

#LineLine coverage
 1
 2namespace Services.BitrixStockServices;
 3
 4/// <summary>
 5/// Агрегация заказов «Не указан» / «Заказан реализатору» в карту резерва (артикул + склад).
 6/// </summary>
 7public static class OrderReserveAggregation
 8{
 9    public static string ResolveOrderNumber(string? shipmentNumber, string? key, int id)
 10    {
 711        if (!string.IsNullOrWhiteSpace(shipmentNumber))
 512            return shipmentNumber.Trim();
 213        if (!string.IsNullOrWhiteSpace(key))
 114            return key.Trim();
 115        return $"#{id}";
 16    }
 17
 18    public static int ResolveReserveQty(int? quantity, string? productKey)
 19    {
 520        var qty = quantity ?? 1;
 521        return qty * OzonDomains.Nomenclature.ProductKeyParser.BatchMultiplier(productKey);
 22    }
 23
 24    public static string? ResolveWarehouseName(string? warehouseName, string? zasylValue) =>
 625        !string.IsNullOrWhiteSpace(warehouseName) ? warehouseName.Trim()
 626        : !string.IsNullOrWhiteSpace(zasylValue) ? zasylValue.Trim()
 627        : null;
 28
 29    public static List<OrderReserveLineDto> BuildLines(
 30        IEnumerable<OrderReserveSourceRow> rows,
 31        IReadOnlySet<string>? storeFilter)
 32    {
 333        var grouped = new Dictionary<(string Article, string Store), (int Qty, Dictionary<string, OrderReserveOrderDto> 
 34
 1835        foreach (var row in rows)
 36        {
 637            if (string.IsNullOrWhiteSpace(row.Article)) continue;
 638            if (string.IsNullOrWhiteSpace(row.StatusName)) continue;
 39
 540            var warehouseName = ResolveWarehouseName(row.WarehouseName, row.ZasylValue);
 541            if (warehouseName == null) continue;
 42
 543            if (storeFilter is { Count: > 0 } && !storeFilter.Contains(warehouseName))
 44                continue;
 45
 446            var qty = ResolveReserveQty(row.Quantity, row.ProductKey);
 447            var orderNumber = ResolveOrderNumber(row.ShipmentNumber, row.Key, row.Id);
 448            var key = (row.Article.Trim(), warehouseName);
 449            var orderDto = new OrderReserveOrderDto
 450            {
 451                OrderNumber = orderNumber,
 452                ShipmentNumber = string.IsNullOrWhiteSpace(row.ShipmentNumber) ? null : row.ShipmentNumber.Trim(),
 453                ProcessingDate = row.ProcessingDate,
 454                StatusName = row.StatusName.Trim(),
 455                StatusColor = string.IsNullOrWhiteSpace(row.StatusColor) ? "#ffffff" : row.StatusColor.Trim(),
 456            };
 57
 458            if (grouped.TryGetValue(key, out var existing))
 59            {
 160                existing.Qty += qty;
 161                existing.Orders[orderNumber] = orderDto;
 162                grouped[key] = existing;
 63            }
 64            else
 65            {
 366                grouped[key] = (qty, new Dictionary<string, OrderReserveOrderDto>(StringComparer.OrdinalIgnoreCase)
 367                {
 368                    [orderNumber] = orderDto
 369                });
 70            }
 71        }
 72
 373        return grouped
 374            .Select(kvp => new OrderReserveLineDto
 375            {
 376                Article = kvp.Key.Article,
 377                Store = kvp.Key.Store,
 378                ReservedQty = kvp.Value.Qty,
 379                OrderCount = kvp.Value.Orders.Count,
 380                OrderNumbers = kvp.Value.Orders.Keys
 481                    .OrderBy(n => n, StringComparer.OrdinalIgnoreCase)
 382                    .ToList(),
 383                Orders = kvp.Value.Orders.Values
 484                    .OrderBy(o => o.OrderNumber, StringComparer.OrdinalIgnoreCase)
 385                    .ToList(),
 386            })
 387            .OrderBy(l => l.Article, StringComparer.OrdinalIgnoreCase)
 388            .ThenBy(l => l.Store, StringComparer.OrdinalIgnoreCase)
 389            .ToList();
 90    }
 91}
 92
 93public sealed class OrderReserveSourceRow
 94{
 95    public int Id { get; init; }
 96    public string? Article { get; init; }
 97    public string? ProductKey { get; init; }
 98    public int? Quantity { get; init; }
 99    public string? WarehouseName { get; init; }
 100    public string? ZasylValue { get; init; }
 101    public string? ShipmentNumber { get; init; }
 102    public string? Key { get; init; }
 103    public DateTime? ProcessingDate { get; init; }
 104    public string? StatusName { get; init; }
 105    public string? StatusColor { get; init; }
 106}