using System.Collections.Generic; using System.Linq; using DeclarationAutomatization.Models; namespace DeclarationAutomatization.Services; public class TransformService { // Группирует строки СПРАВКИ по ТН ВЭД → одна позиция Листа2. // Количество, сумма, вес — суммируются. // Рег. номер / дата — берётся первый непустой в группе. // п/п — нумерация по порядку с 1. public List BuildDeclarationItems(IEnumerable allItems) { int sequentialNumber = 1; return allItems .GroupBy(s => s.TnVed) .Select(g => { var first = g.First(); var regEntry = g.FirstOrDefault(s => !string.IsNullOrWhiteSpace(s.RegNumber)); return new DeclarationItem { SequentialNumber = sequentialNumber++, Description = first.Description, TnVed = first.TnVed, CountryId = first.CountryId, Quantity = g.Sum(s => s.Quantity), AmountWithVat = g.Sum(s => s.AmountWithVat), GrossWeight = g.Sum(s => s.GrossWeight), NetWeight = g.Sum(s => s.NetWeight), RegNumber = regEntry?.RegNumber ?? "", RegDate = regEntry?.RegDate ?? "", }; }) .ToList(); } }