| | | 1 | | using ClosedXML.Excel; |
| | | 2 | | |
| | | 3 | | namespace Services.BitrixStockServices; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Строит Excel-файл для отчёта синхронизации остатков. |
| | | 7 | | /// Используется как из веб-контроллера, так и из фонового джоба отправки почты. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class BitrixStockSyncReportExcelBuilder |
| | | 10 | | { |
| | 1 | 11 | | private static readonly Dictionary<string, XLColor> ActionColors = |
| | 1 | 12 | | new(StringComparer.OrdinalIgnoreCase) |
| | 1 | 13 | | { |
| | 1 | 14 | | ["Push"] = XLColor.FromArgb(0xCF, 0xE2, 0xFF), |
| | 1 | 15 | | ["Decrease"] = XLColor.FromArgb(0xFF, 0xF3, 0xCD), |
| | 1 | 16 | | ["Clear"] = XLColor.FromArgb(0xF8, 0xD7, 0xDA), |
| | 1 | 17 | | ["Match"] = XLColor.FromArgb(0xD1, 0xE7, 0xDD), |
| | 1 | 18 | | ["Skip"] = XLColor.FromArgb(0xE9, 0xEC, 0xEF), |
| | 1 | 19 | | ["NotFound"] = XLColor.FromArgb(0xF3, 0xE8, 0xFF), |
| | 1 | 20 | | ["FbsNoWarehouse"] = XLColor.FromArgb(0xFF, 0xE8, 0xCC), |
| | 1 | 21 | | ["NotForSale"] = XLColor.FromArgb(0xFF, 0xCC, 0xCC), |
| | 1 | 22 | | ["CleanupExtra"] = XLColor.FromArgb(0xCF, 0xFA, 0xFE), |
| | 1 | 23 | | ["ClearDisabledLink"] = XLColor.FromArgb(0xE9, 0xEC, 0xEF), |
| | 1 | 24 | | ["BatchVariant"] = XLColor.FromArgb(0xD0, 0xE4, 0xFF), |
| | 1 | 25 | | ["BatchVariantDisabledLink"] = XLColor.FromArgb(0xE9, 0xEC, 0xEF), |
| | 1 | 26 | | ["BatchVariantZeroStock"] = XLColor.FromArgb(0xFF, 0xF3, 0xCD), |
| | 1 | 27 | | ["BatchVariantNotCreated"] = XLColor.FromArgb(0xFF, 0xD6, 0xD6), |
| | 1 | 28 | | ["BatchVariantNotInFbs"] = XLColor.FromArgb(0xFF, 0xE8, 0xCC), |
| | 1 | 29 | | }; |
| | | 30 | | |
| | 1 | 31 | | private static readonly Dictionary<string, (XLColor bg, XLColor fg, string label)> IndexStyles = |
| | 1 | 32 | | new(StringComparer.OrdinalIgnoreCase) |
| | 1 | 33 | | { |
| | 1 | 34 | | ["SUPER"] = (XLColor.FromArgb(0xED, 0xE9, 0xFE), XLColor.FromArgb(0x7C, 0x3A, 0xED), "Супервыгодный" |
| | 1 | 35 | | ["GREEN"] = (XLColor.FromArgb(0xD1, 0xE7, 0xDD), XLColor.FromArgb(0x0A, 0x3C, 0x22), "Выгодный"), |
| | 1 | 36 | | ["YELLOW"] = (XLColor.FromArgb(0xFF, 0xF3, 0xCD), XLColor.FromArgb(0x66, 0x42, 0x00), "Умеренный"), |
| | 1 | 37 | | ["RED"] = (XLColor.FromArgb(0xF8, 0xD7, 0xDA), XLColor.FromArgb(0x84, 0x1F, 0x27), "Невыгодный"), |
| | 1 | 38 | | ["WITHOUT_INDEX"] = (XLColor.FromArgb(0xE9, 0xEC, 0xEF), XLColor.Gray, "Нет индекса"), |
| | 1 | 39 | | // API-совместимость (старые имена) |
| | 1 | 40 | | ["BENEFIT"] = (XLColor.FromArgb(0xD1, 0xE7, 0xDD), XLColor.FromArgb(0x0A, 0x3C, 0x22), "Выгодный"), |
| | 1 | 41 | | ["AVERAGE"] = (XLColor.FromArgb(0xFF, 0xF3, 0xCD), XLColor.FromArgb(0x66, 0x42, 0x00), "Умеренный"), |
| | 1 | 42 | | ["OVERPRICED"] = (XLColor.FromArgb(0xF8, 0xD7, 0xDA), XLColor.FromArgb(0x84, 0x1F, 0x27), "Невыгодный"), |
| | 1 | 43 | | }; |
| | | 44 | | |
| | | 45 | | /// <summary>Загружает все строки отчёта постранично и возвращает Excel-файл в байтах.</summary> |
| | | 46 | | public static async Task<(byte[]? bytes, string filename)> BuildAsync( |
| | | 47 | | IBitrixStockSyncReportService reportService, |
| | | 48 | | string reportId, |
| | | 49 | | BitrixStockSyncReportQuery? filterQuery = null, |
| | | 50 | | Action<BitrixStockExcelBuildProgress>? progress = null, |
| | | 51 | | CancellationToken ct = default) |
| | | 52 | | { |
| | 13 | 53 | | filterQuery ??= new BitrixStockSyncReportQuery(); |
| | | 54 | | const int batchSize = 2000; |
| | 13 | 55 | | var allRows = new List<BitrixStockSyncReportWarehouseLineDto>(); |
| | 13 | 56 | | string? clientName = null; |
| | 13 | 57 | | DateTime generatedAt = default; |
| | 13 | 58 | | var isMultiClient = false; |
| | | 59 | | |
| | 13 | 60 | | progress?.Invoke(new BitrixStockExcelBuildProgress |
| | 13 | 61 | | { |
| | 13 | 62 | | Stage = BitrixStockSyncReportExportStages.Loading, |
| | 13 | 63 | | Current = 0, |
| | 13 | 64 | | Total = 100, |
| | 13 | 65 | | Percent = 5, |
| | 13 | 66 | | Message = "Загрузка страниц отчёта" |
| | 13 | 67 | | }); |
| | | 68 | | |
| | 13 | 69 | | int page = 1; |
| | 13 | 70 | | var totalPages = 1; |
| | 0 | 71 | | while (true) |
| | | 72 | | { |
| | 13 | 73 | | var q = new BitrixStockSyncReportQuery |
| | 13 | 74 | | { |
| | 13 | 75 | | Page = page, PageSize = batchSize, |
| | 13 | 76 | | Article = filterQuery.Article, OneCStores = filterQuery.OneCStores, |
| | 13 | 77 | | OzonWarehouse = filterQuery.OzonWarehouse, Action = filterQuery.Action, |
| | 13 | 78 | | OnlyRequiresAction = filterQuery.OnlyRequiresAction, |
| | 13 | 79 | | OnlyOzonAppliedSuccess = filterQuery.OnlyOzonAppliedSuccess, |
| | 13 | 80 | | // Экспорт всегда содержит все строки отчёта, включая «пустые» (OneCQty=0 И OzonQty=0), |
| | 13 | 81 | | // чтобы выгрузка совпадала с таблицей BitrixStockSyncReportLines и не было путаницы. |
| | 13 | 82 | | ShowZeroRows = true |
| | 13 | 83 | | }; |
| | 13 | 84 | | var report = await reportService.GetReportAsync(reportId, q, ct); |
| | 21 | 85 | | if (report == null) return (null, string.Empty); |
| | 5 | 86 | | totalPages = Math.Max(1, report.TotalPages); |
| | | 87 | | |
| | 5 | 88 | | clientName ??= report.OzonClientName; |
| | 10 | 89 | | if (generatedAt == default) generatedAt = report.GeneratedAtUtc; |
| | 5 | 90 | | if (report.MultiClientIds.Count > 1) |
| | | 91 | | { |
| | 0 | 92 | | isMultiClient = true; |
| | | 93 | | } |
| | | 94 | | |
| | 5 | 95 | | allRows.AddRange(report.Rows); |
| | 5 | 96 | | progress?.Invoke(new BitrixStockExcelBuildProgress |
| | 5 | 97 | | { |
| | 5 | 98 | | Stage = BitrixStockSyncReportExportStages.Loading, |
| | 5 | 99 | | Current = page, |
| | 5 | 100 | | Total = totalPages, |
| | 5 | 101 | | Percent = 5 + (int)Math.Round((Math.Min(page, totalPages) / (double)totalPages) * 35d), |
| | 5 | 102 | | Message = $"Загружена страница {page} из {totalPages}" |
| | 5 | 103 | | }); |
| | 5 | 104 | | if (page >= report.TotalPages) break; |
| | 0 | 105 | | page++; |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | // Загружаем снимки цен Ozon для этого отчёта |
| | 5 | 109 | | progress?.Invoke(new BitrixStockExcelBuildProgress |
| | 5 | 110 | | { |
| | 5 | 111 | | Stage = BitrixStockSyncReportExportStages.Loading, |
| | 5 | 112 | | Current = totalPages, |
| | 5 | 113 | | Total = totalPages, |
| | 5 | 114 | | Percent = 42, |
| | 5 | 115 | | Message = "Загрузка снимков цен Ozon" |
| | 5 | 116 | | }); |
| | 5 | 117 | | var priceInfoList = await reportService.GetOzonPriceInfoAsync(reportId, ct); |
| | 5 | 118 | | var priceMap = priceInfoList.ToDictionary( |
| | 0 | 119 | | p => (p.Article.ToUpperInvariant(), p.OzonClientId), |
| | 5 | 120 | | p => p); |
| | | 121 | | |
| | 5 | 122 | | progress?.Invoke(new BitrixStockExcelBuildProgress |
| | 5 | 123 | | { |
| | 5 | 124 | | Stage = BitrixStockSyncReportExportStages.Building, |
| | 5 | 125 | | Current = 0, |
| | 5 | 126 | | Total = Math.Max(1, allRows.Count), |
| | 5 | 127 | | Percent = 45, |
| | 5 | 128 | | Message = "Подготовка структуры Excel" |
| | 5 | 129 | | }); |
| | | 130 | | |
| | 5 | 131 | | using var wb = new XLWorkbook(); |
| | 5 | 132 | | var ws = wb.Worksheets.Add("Синхронизация остатков"); |
| | | 133 | | |
| | 5 | 134 | | var hasPrices = priceMap.Count > 0; |
| | 5 | 135 | | var hasPushes = hasPrices && priceMap.Values.Any(p => p.PricePushedNew.HasValue || p.PricePushStatus != null); |
| | 5 | 136 | | var extraCols = hasPrices ? 3 : 0; // Цена OZ | Цена продажи | Индекс |
| | 5 | 137 | | var pushCols = hasPushes ? 7 : 0; // Цена до | Цена после | Конкурент | Инд.до | Инд.после | Надб |
| | 5 | 138 | | var totalCols = (isMultiClient ? 15 : 14) + extraCols + pushCols; |
| | 5 | 139 | | ws.Cell(1, 1).Value = $"Отчёт синхронизации: {clientName}"; |
| | 5 | 140 | | ws.Cell(1, 1).Style.Font.Bold = true; |
| | 5 | 141 | | ws.Cell(1, 1).Style.Font.FontSize = 13; |
| | 5 | 142 | | ws.Range(1, 1, 1, totalCols).Merge(); |
| | | 143 | | |
| | 5 | 144 | | ws.Cell(2, 1).Value = $"Сформирован: {BitrixStockMskDateTime.Format(generatedAt)} (MSK) | Строк: {allRows.Coun |
| | 5 | 145 | | ws.Cell(2, 1).Style.Font.Italic = true; |
| | 5 | 146 | | ws.Range(2, 1, 2, totalCols).Merge(); |
| | | 147 | | |
| | 5 | 148 | | var baseHeaders = isMultiClient |
| | 5 | 149 | | ? new[] { "Артикул", "Производитель", "C0", "Δ C0", "Клиент Ozon", "Склад 1С", "Склад Ozon", "1С", "Было (Oz |
| | 5 | 150 | | : new[] { "Артикул", "Производитель", "C0", "Δ C0", "Склад 1С", "Склад Ozon", "1С", "Было (Ozon)", "Нужно", |
| | 5 | 151 | | var pushHeaders = hasPushes |
| | 5 | 152 | | ? new[] { "Цена до наката", "Цена после наката", "Конкурент", "Индекс до", "Индекс после", "Распродажа", "Да |
| | 5 | 153 | | : Array.Empty<string>(); |
| | 5 | 154 | | var headers = hasPrices |
| | 5 | 155 | | ? baseHeaders.Take(4) |
| | 5 | 156 | | .Concat(new[] { "Цена (со скидками)", "Цена продажи", "Индекс цены" }) |
| | 5 | 157 | | .Concat(pushHeaders) |
| | 5 | 158 | | .Concat(baseHeaders.Skip(4)) |
| | 5 | 159 | | .ToArray() |
| | 5 | 160 | | : baseHeaders; |
| | 160 | 161 | | for (int c = 0; c < headers.Length; c++) |
| | | 162 | | { |
| | 75 | 163 | | var cell = ws.Cell(4, c + 1); |
| | 75 | 164 | | cell.Value = headers[c]; |
| | 75 | 165 | | cell.Style.Font.Bold = true; |
| | 75 | 166 | | cell.Style.Fill.BackgroundColor = XLColor.FromArgb(0x21, 0x96, 0xF3); |
| | 75 | 167 | | cell.Style.Font.FontColor = XLColor.White; |
| | 75 | 168 | | cell.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center; |
| | | 169 | | } |
| | | 170 | | |
| | 5 | 171 | | int row = 5; |
| | 5 | 172 | | var renderedRows = 0; |
| | 18 | 173 | | foreach (var w in allRows) |
| | | 174 | | { |
| | 4 | 175 | | var col = 1; |
| | 4 | 176 | | ws.Cell(row, col++).Value = w.Article; |
| | 4 | 177 | | ws.Cell(row, col++).Value = w.ManufacturerName ?? ""; |
| | 4 | 178 | | var priceCell = ws.Cell(row, col++); |
| | 4 | 179 | | if (w.OneCPrice.HasValue) { priceCell.Value = w.OneCPrice.Value; priceCell.Style.NumberFormat.Format = "#,## |
| | 4 | 180 | | else priceCell.Value = ""; |
| | | 181 | | // Δ C0 |
| | 4 | 182 | | var deltaC0Cell = ws.Cell(row, col++); |
| | 4 | 183 | | if (w.OneCPrice.HasValue && w.PrevOneCPrice.HasValue) |
| | | 184 | | { |
| | 0 | 185 | | var diff = (long)Math.Round(w.OneCPrice.Value) - (long)Math.Round(w.PrevOneCPrice.Value); |
| | 0 | 186 | | var prevFmt = ((long)Math.Round(w.PrevOneCPrice.Value)).ToString("N0"); |
| | 0 | 187 | | if (diff == 0) |
| | | 188 | | { |
| | 0 | 189 | | deltaC0Cell.Value = "="; |
| | 0 | 190 | | deltaC0Cell.Style.Font.FontColor = XLColor.Gray; |
| | | 191 | | } |
| | 0 | 192 | | else if (diff > 0) |
| | | 193 | | { |
| | 0 | 194 | | deltaC0Cell.Value = $"↑ +{diff:N0} (было {prevFmt})"; |
| | 0 | 195 | | deltaC0Cell.Style.Font.FontColor = XLColor.FromArgb(0x19, 0x87, 0x54); |
| | | 196 | | } |
| | | 197 | | else |
| | | 198 | | { |
| | 0 | 199 | | deltaC0Cell.Value = $"↓ {diff:N0} (было {prevFmt})"; |
| | 0 | 200 | | deltaC0Cell.Style.Font.FontColor = XLColor.FromArgb(0xDC, 0x35, 0x45); |
| | | 201 | | } |
| | | 202 | | } |
| | 4 | 203 | | else deltaC0Cell.Value = ""; |
| | | 204 | | |
| | | 205 | | // Цены Ozon (если есть снимок для этого артикула + клиента) |
| | 4 | 206 | | if (hasPrices) |
| | | 207 | | { |
| | 0 | 208 | | priceMap.TryGetValue(((w.Article ?? string.Empty).ToUpperInvariant(), w.OzonClientId), out var pi); |
| | | 209 | | |
| | 0 | 210 | | var truePriceCell = ws.Cell(row, col++); |
| | 0 | 211 | | if (pi?.TruePrice is { } tp) { truePriceCell.Value = (double)Math.Round(tp); truePriceCell.Style.NumberF |
| | 0 | 212 | | else truePriceCell.Value = ""; |
| | | 213 | | |
| | 0 | 214 | | var sellingPriceCell = ws.Cell(row, col++); |
| | 0 | 215 | | if (pi?.SellingPrice is { } sp) { sellingPriceCell.Value = (double)Math.Round(sp); sellingPriceCell.Styl |
| | 0 | 216 | | else sellingPriceCell.Value = ""; |
| | | 217 | | |
| | 0 | 218 | | var indexCell = ws.Cell(row, col++); |
| | 0 | 219 | | if (!string.IsNullOrEmpty(pi?.ColorIndex) && IndexStyles.TryGetValue(pi.ColorIndex, out var style)) |
| | | 220 | | { |
| | 0 | 221 | | indexCell.Value = style.label; |
| | 0 | 222 | | indexCell.Style.Fill.BackgroundColor = style.bg; |
| | 0 | 223 | | indexCell.Style.Font.FontColor = style.fg; |
| | 0 | 224 | | indexCell.Style.Font.Bold = true; |
| | 0 | 225 | | indexCell.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center; |
| | | 226 | | } |
| | 0 | 227 | | else indexCell.Value = ""; |
| | | 228 | | |
| | 0 | 229 | | if (hasPushes) |
| | | 230 | | { |
| | 0 | 231 | | var pushedOldCell = ws.Cell(row, col++); |
| | 0 | 232 | | if (pi?.PricePushedOld is { } po) { pushedOldCell.Value = (double)Math.Round(po); pushedOldCell.Styl |
| | 0 | 233 | | else pushedOldCell.Value = ""; |
| | | 234 | | |
| | 0 | 235 | | var pushedNewCell = ws.Cell(row, col++); |
| | 0 | 236 | | var batchMult = pi != null ? BitrixStockOrphanOfferMatching.ExtractBatchMultiplier(pi.OfferId) : |
| | 0 | 237 | | var batchSuffix = batchMult > 1 ? $" ×{batchMult}" : ""; |
| | 0 | 238 | | if (pi?.PricePushedNew is { } pn) |
| | | 239 | | { |
| | 0 | 240 | | pushedNewCell.Value = (double)Math.Round(pn); |
| | 0 | 241 | | pushedNewCell.Style.NumberFormat.Format = batchMult > 1 ? $"#,##0\"{batchSuffix}\"" : "#,##0"; |
| | 0 | 242 | | pushedNewCell.Style.Font.Bold = true; |
| | 0 | 243 | | pushedNewCell.Style.Font.FontColor = XLColor.FromArgb(0x19, 0x87, 0x54); |
| | | 244 | | } |
| | 0 | 245 | | else if (pi?.PricePushStatus == "Unchanged") |
| | | 246 | | { |
| | 0 | 247 | | var curPrice = pi.SellingPrice ?? pi.TruePrice; |
| | 0 | 248 | | pushedNewCell.Value = curPrice.HasValue |
| | 0 | 249 | | ? $"без изменений · {(long)Math.Round(curPrice.Value):N0}{batchSuffix}" |
| | 0 | 250 | | : "без изменений"; |
| | 0 | 251 | | pushedNewCell.Style.Font.FontColor = XLColor.Gray; |
| | 0 | 252 | | pushedNewCell.Style.Font.Italic = true; |
| | | 253 | | } |
| | 0 | 254 | | else if (pi?.PricePushStatus == "Error") |
| | | 255 | | { |
| | 0 | 256 | | var curPrice = pi.SellingPrice ?? pi.TruePrice; |
| | 0 | 257 | | var errText = string.IsNullOrEmpty(pi.PricePushError) ? "Ошибка" : $"Ошибка: {pi.PricePushError |
| | 0 | 258 | | pushedNewCell.Value = curPrice.HasValue ? $"{errText} · {(long)Math.Round(curPrice.Value):N0}{ba |
| | 0 | 259 | | pushedNewCell.Style.Font.FontColor = XLColor.FromArgb(0xDC, 0x35, 0x45); |
| | | 260 | | } |
| | 0 | 261 | | else pushedNewCell.Value = ""; |
| | | 262 | | |
| | 0 | 263 | | var competitorCell = ws.Cell(row, col++); |
| | 0 | 264 | | if (pi?.PushedCompetitorPrice is { } cp) { competitorCell.Value = (double)Math.Round(cp); competitor |
| | 0 | 265 | | else competitorCell.Value = ""; |
| | | 266 | | |
| | 0 | 267 | | var idxBeforeCell = ws.Cell(row, col++); |
| | 0 | 268 | | if (!string.IsNullOrEmpty(pi?.PushedColorIndexBefore) && IndexStyles.TryGetValue(pi.PushedColorIndex |
| | | 269 | | { |
| | 0 | 270 | | idxBeforeCell.Value = sb.label; |
| | 0 | 271 | | idxBeforeCell.Style.Fill.BackgroundColor = sb.bg; |
| | 0 | 272 | | idxBeforeCell.Style.Font.FontColor = sb.fg; |
| | 0 | 273 | | idxBeforeCell.Style.Font.Bold = true; |
| | 0 | 274 | | idxBeforeCell.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center; |
| | | 275 | | } |
| | 0 | 276 | | else idxBeforeCell.Value = ""; |
| | | 277 | | |
| | 0 | 278 | | var idxAfterCell = ws.Cell(row, col++); |
| | 0 | 279 | | if (!string.IsNullOrEmpty(pi?.PushedColorIndexAfter) && IndexStyles.TryGetValue(pi.PushedColorIndexA |
| | | 280 | | { |
| | 0 | 281 | | idxAfterCell.Value = sa.label; |
| | 0 | 282 | | idxAfterCell.Style.Fill.BackgroundColor = sa.bg; |
| | 0 | 283 | | idxAfterCell.Style.Font.FontColor = sa.fg; |
| | 0 | 284 | | idxAfterCell.Style.Font.Bold = true; |
| | 0 | 285 | | idxAfterCell.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center; |
| | | 286 | | } |
| | 0 | 287 | | else idxAfterCell.Value = ""; |
| | | 288 | | |
| | 0 | 289 | | var markupCell = ws.Cell(row, col++); |
| | 0 | 290 | | if (pi?.PushedMarkupPct is { } mp) |
| | | 291 | | { |
| | 0 | 292 | | markupCell.Value = mp == 0 ? "—" : $"{mp:0.#}%"; |
| | 0 | 293 | | if (mp == 0) markupCell.Style.Font.FontColor = XLColor.Gray; |
| | | 294 | | } |
| | 0 | 295 | | else markupCell.Value = ""; |
| | | 296 | | |
| | 0 | 297 | | var pushedAtCell = ws.Cell(row, col++); |
| | 0 | 298 | | if (pi?.PricePushedAtUtc is { } pat) |
| | 0 | 299 | | pushedAtCell.Value = BitrixStockMskDateTime.Format(pat); |
| | 0 | 300 | | else pushedAtCell.Value = ""; |
| | | 301 | | } |
| | | 302 | | } |
| | | 303 | | |
| | 4 | 304 | | if (isMultiClient) |
| | 0 | 305 | | ws.Cell(row, col++).Value = w.OzonClientName ?? ""; |
| | 4 | 306 | | ws.Cell(row, col++).Value = w.OneCStore; |
| | 4 | 307 | | ws.Cell(row, col++).Value = w.OzonWarehouseName; |
| | 4 | 308 | | var oneCCell1 = ws.Cell(row, col++); |
| | 4 | 309 | | if (w.ReservedQty > 0) oneCCell1.Value = $"{w.OneCQty} (-{w.ReservedQty})"; |
| | 4 | 310 | | else oneCCell1.Value = w.OneCQty; |
| | 4 | 311 | | ws.Cell(row, col++).Value = w.OzonQty; // Было |
| | | 312 | | |
| | | 313 | | // Нужно — целевой остаток |
| | 4 | 314 | | var targetQty = GetTargetQty(w); |
| | 4 | 315 | | var needsCell = ws.Cell(row, col++); |
| | 8 | 316 | | if (targetQty.HasValue) needsCell.Value = targetQty.Value; |
| | 0 | 317 | | else needsCell.Value = "—"; |
| | | 318 | | |
| | | 319 | | // Стало — фактический остаток после наката |
| | 4 | 320 | | var staleCell = ws.Cell(row, col++); |
| | 4 | 321 | | if (w.OzonQtyAfter.HasValue) staleCell.Value = w.OzonQtyAfter.Value; |
| | 4 | 322 | | else staleCell.Value = "—"; |
| | | 323 | | |
| | | 324 | | // Δ итог — фактическое изменение |
| | 4 | 325 | | var deltaCell = ws.Cell(row, col++); |
| | 4 | 326 | | if (w.OzonQtyAfter.HasValue) |
| | | 327 | | { |
| | 0 | 328 | | var actualDelta = w.OzonQtyAfter.Value - w.OzonQty; |
| | 0 | 329 | | deltaCell.Value = actualDelta > 0 ? $"+{actualDelta}" : actualDelta.ToString(); |
| | 0 | 330 | | deltaCell.Style.Font.Bold = true; |
| | 0 | 331 | | deltaCell.Style.Font.FontColor = actualDelta > 0 |
| | 0 | 332 | | ? XLColor.FromArgb(0x19, 0x87, 0x54) // зелёный |
| | 0 | 333 | | : actualDelta < 0 |
| | 0 | 334 | | ? XLColor.FromArgb(0xDC, 0x35, 0x45) // красный |
| | 0 | 335 | | : XLColor.Gray; |
| | | 336 | | } |
| | | 337 | | else |
| | | 338 | | { |
| | | 339 | | // Ещё не накатили — показываем ожидаемую дельту (OneCQty - OzonQty) |
| | 4 | 340 | | deltaCell.Value = w.Delta > 0 ? $"+{w.Delta}" : w.Delta == 0 ? "0" : w.Delta.ToString(); |
| | 4 | 341 | | deltaCell.Style.Font.FontColor = XLColor.Gray; |
| | | 342 | | } |
| | | 343 | | |
| | 4 | 344 | | ws.Cell(row, col++).Value = w.ActionLabel; |
| | 4 | 345 | | ws.Cell(row, col++).Value = w.Action == BitrixStockSyncReportActions.ClearDisabledLink |
| | 4 | 346 | | ? (w.OzonApplyStatus switch |
| | 4 | 347 | | { |
| | 0 | 348 | | "Success" => $"⓪₂ Связь снята | {w.OzonWarehouseName} (Ozon ID {w.OzonWarehouseId})", |
| | 0 | 349 | | "Failed" => $"✗ {w.OzonApplyError}", |
| | 0 | 350 | | _ => "⓪₂ Связь снята" |
| | 4 | 351 | | }) |
| | 4 | 352 | | : w.OzonApplyStatus switch |
| | 4 | 353 | | { |
| | 0 | 354 | | "Success" when !string.IsNullOrEmpty(w.OzonApplyError) => GetSkipLabel(w.OzonApplyError), |
| | 0 | 355 | | "Success" => "✓ Применено", |
| | 0 | 356 | | "Failed" when (w.OzonApplyError ?? "").Contains("Не продается", StringComparison.OrdinalIgnoreCase) |
| | 0 | 357 | | => "⊘ Не продается", |
| | 0 | 358 | | "Failed" => $"✗ Ошибка: {w.OzonApplyError}", |
| | 0 | 359 | | "Pending" => "Ожидает", |
| | 0 | 360 | | "Skipped" => GetSkipLabel(w.OzonApplyError), |
| | 4 | 361 | | _ => w.Action is BitrixStockSyncReportActions.NotFound or BitrixStockSyncReportActions.FbsNoWarehouse or |
| | 4 | 362 | | or BitrixStockSyncReportActions.BatchVariant or BitrixStockSyncReportActions.BatchVariantDisable |
| | 4 | 363 | | or BitrixStockSyncReportActions.BatchVariantZeroStock or BitrixStockSyncReportActions.BatchVaria |
| | 4 | 364 | | or BitrixStockSyncReportActions.BatchVariantNotInFbs |
| | 4 | 365 | | ? "Этап 5" : "" |
| | 4 | 366 | | }; |
| | 4 | 367 | | ws.Cell(row, col++).Value = w.OzonCleanupStatus switch |
| | 4 | 368 | | { |
| | 0 | 369 | | "Success" when (w.OzonCleanupError ?? "").Contains("Сохранён", StringComparison.OrdinalIgnoreCase) => "✓ |
| | 0 | 370 | | "Success" when (w.OzonCleanupError ?? "").Contains("⓪₃", StringComparison.OrdinalIgnoreCase) => "⓪₃ Этап |
| | 0 | 371 | | "Success" when w.Action == BitrixStockSyncReportActions.CleanupExtra => "⓪₃ Этап 3: обнулён", |
| | 4 | 372 | | _ when w.Action == BitrixStockSyncReportActions.BatchVariant => |
| | 0 | 373 | | w.BatchMultiplier > 1 |
| | 0 | 374 | | ? $"×{w.BatchMultiplier} шт. | 1С: {w.OneCQty} → {w.OneCQty / w.BatchMultiplier} пак. (было {w.O |
| | 0 | 375 | | + (w.BatchPushedPrice.HasValue ? $" | Цена: {w.BatchPushedPrice.Value:F0} ₽" : "") |
| | 0 | 376 | | : "Партия", |
| | 4 | 377 | | _ when w.Action == BitrixStockSyncReportActions.BatchVariantDisabledLink => |
| | 0 | 378 | | $"⓪ Связь снята (×{w.BatchMultiplier} шт.) | {w.BatchBaseArticle}", |
| | 4 | 379 | | _ when w.Action == BitrixStockSyncReportActions.BatchVariantZeroStock => |
| | 0 | 380 | | $"⓪ Нет пак. (×{w.BatchMultiplier} шт.) | 1С: {w.OneCQty} шт. < {w.BatchMultiplier} шт./уп.", |
| | 4 | 381 | | _ when w.Action == BitrixStockSyncReportActions.BatchVariantNotCreated => |
| | 0 | 382 | | $"⚠ Не создан в Ozon FBS (×{w.BatchMultiplier} шт.) | {w.Note}", |
| | 4 | 383 | | _ when w.Action == BitrixStockSyncReportActions.BatchVariantNotInFbs => |
| | 0 | 384 | | $"⊘ Не в FBS управлении (×{w.BatchMultiplier} шт.) | 1С: {w.OneCQty} шт. | Добавить в ЛК → Управлени |
| | 0 | 385 | | "Success" when (w.OzonCleanupError ?? "").Contains("Обнулён", StringComparison.OrdinalIgnoreCase) => "⓪ |
| | 0 | 386 | | "Success" => "✓", |
| | 0 | 387 | | "Failed" => $"✗ {w.OzonCleanupError}", |
| | 0 | 388 | | "Skipped" => w.OzonCleanupError ?? "пропуск", |
| | 4 | 389 | | _ => "" |
| | 4 | 390 | | }; |
| | 4 | 391 | | ws.Cell(row, col).Value = w.Note ?? ""; |
| | | 392 | | |
| | 4 | 393 | | if (!string.IsNullOrEmpty(w.Action) && ActionColors.TryGetValue(w.Action, out var bg)) |
| | 4 | 394 | | ws.Range(row, 1, row, totalCols).Style.Fill.BackgroundColor = bg; |
| | | 395 | | |
| | 4 | 396 | | row++; |
| | 4 | 397 | | renderedRows++; |
| | 4 | 398 | | if (renderedRows % 500 == 0 || renderedRows == allRows.Count) |
| | | 399 | | { |
| | 4 | 400 | | var renderPercent = 45 + (int)Math.Round((renderedRows / (double)Math.Max(1, allRows.Count)) * 45d); |
| | 4 | 401 | | progress?.Invoke(new BitrixStockExcelBuildProgress |
| | 4 | 402 | | { |
| | 4 | 403 | | Stage = BitrixStockSyncReportExportStages.Building, |
| | 4 | 404 | | Current = renderedRows, |
| | 4 | 405 | | Total = allRows.Count, |
| | 4 | 406 | | Percent = Math.Min(90, renderPercent), |
| | 4 | 407 | | Message = $"Заполнено строк: {renderedRows:N0} / {allRows.Count:N0}" |
| | 4 | 408 | | }); |
| | | 409 | | } |
| | | 410 | | } |
| | | 411 | | |
| | 5 | 412 | | ws.Column(1).Width = 18; // Артикул |
| | 5 | 413 | | ws.Column(2).Width = 18; // Производитель |
| | 5 | 414 | | ws.Column(3).Width = 10; // C0 |
| | 5 | 415 | | ws.Column(4).Width = 22; // Δ C0 |
| | 5 | 416 | | if (hasPrices) |
| | | 417 | | { |
| | 0 | 418 | | ws.Column(5).Width = 12; // Цена OZ |
| | 0 | 419 | | ws.Column(6).Width = 14; // Цена продажи |
| | 0 | 420 | | ws.Column(7).Width = 16; // Индекс цены |
| | | 421 | | } |
| | 5 | 422 | | if (hasPushes) |
| | | 423 | | { |
| | 0 | 424 | | ws.Column(8).Width = 14; // Цена до наката |
| | 0 | 425 | | ws.Column(9).Width = 16; // Цена после наката |
| | 0 | 426 | | ws.Column(10).Width = 12; // Конкурент |
| | 0 | 427 | | ws.Column(11).Width = 14; // Индекс до |
| | 0 | 428 | | ws.Column(12).Width = 14; // Индекс после |
| | 0 | 429 | | ws.Column(13).Width = 11; // Распродажа |
| | 0 | 430 | | ws.Column(14).Width = 18; // Дата наката |
| | | 431 | | } |
| | 5 | 432 | | var shift = extraCols + pushCols; |
| | 5 | 433 | | if (isMultiClient) |
| | | 434 | | { |
| | 0 | 435 | | ws.Column(4 + shift + 1).Width = 20; // Клиент Ozon |
| | 0 | 436 | | ws.Column(4 + shift + 2).Width = 16; // Склад 1С |
| | 0 | 437 | | ws.Column(4 + shift + 3).Width = 22; // Склад Ozon |
| | 0 | 438 | | ws.Column(4 + shift + 4).Width = 8; // 1С |
| | 0 | 439 | | ws.Column(4 + shift + 5).Width = 10; // Было |
| | 0 | 440 | | ws.Column(4 + shift + 6).Width = 10; // Нужно |
| | 0 | 441 | | ws.Column(4 + shift + 7).Width = 10; // Стало |
| | 0 | 442 | | ws.Column(4 + shift + 8).Width = 16; // Δ итог |
| | 0 | 443 | | ws.Column(4 + shift + 9).Width = 26; // Действие |
| | 0 | 444 | | ws.Column(4 + shift + 10).Width = 50; // Статус Ozon |
| | 0 | 445 | | ws.Column(4 + shift + 11).Width = 30; // Этап 3 |
| | 0 | 446 | | ws.Column(4 + shift + 12).Width = 40; // Примечание |
| | | 447 | | } |
| | | 448 | | else |
| | | 449 | | { |
| | 5 | 450 | | ws.Column(4 + shift + 1).Width = 16; // Склад 1С |
| | 5 | 451 | | ws.Column(4 + shift + 2).Width = 22; // Склад Ozon |
| | 5 | 452 | | ws.Column(4 + shift + 3).Width = 8; // 1С |
| | 5 | 453 | | ws.Column(4 + shift + 4).Width = 10; // Было |
| | 5 | 454 | | ws.Column(4 + shift + 5).Width = 10; // Нужно |
| | 5 | 455 | | ws.Column(4 + shift + 6).Width = 10; // Стало |
| | 5 | 456 | | ws.Column(4 + shift + 7).Width = 16; // Δ итог |
| | 5 | 457 | | ws.Column(4 + shift + 8).Width = 26; // Действие |
| | 5 | 458 | | ws.Column(4 + shift + 9).Width = 50; // Статус Ozon |
| | 5 | 459 | | ws.Column(4 + shift + 10).Width = 30; // Этап 3 |
| | 5 | 460 | | ws.Column(4 + shift + 11).Width = 40; // Примечание |
| | | 461 | | } |
| | 5 | 462 | | ws.SheetView.FreezeRows(4); |
| | | 463 | | |
| | 0 | 464 | | static string GetSkipLabel(string? error) => BitrixStockSyncReportRowPresenter.GetSkipLabel(error); |
| | | 465 | | |
| | | 466 | | static int? GetTargetQty(BitrixStockSyncReportWarehouseLineDto w) => |
| | 4 | 467 | | BitrixStockSyncReportRowPresenter.GetTargetQty(w); |
| | | 468 | | |
| | 5 | 469 | | progress?.Invoke(new BitrixStockExcelBuildProgress |
| | 5 | 470 | | { |
| | 5 | 471 | | Stage = BitrixStockSyncReportExportStages.Saving, |
| | 5 | 472 | | Current = allRows.Count, |
| | 5 | 473 | | Total = allRows.Count, |
| | 5 | 474 | | Percent = 95, |
| | 5 | 475 | | Message = "Сохранение Excel-файла" |
| | 5 | 476 | | }); |
| | 5 | 477 | | using var ms = new MemoryStream(); |
| | 5 | 478 | | wb.SaveAs(ms); |
| | 5 | 479 | | var filename = $"sync-{reportId[..Math.Min(8, reportId.Length)]}-{DateTime.Now:yyyyMMdd}.xlsx"; |
| | 5 | 480 | | return (ms.ToArray(), filename); |
| | 13 | 481 | | } |
| | | 482 | | |
| | | 483 | | /// <summary>Excel по истории артикула во всех отчётах (структура как у отчёта синхронизации).</summary> |
| | | 484 | | public static async Task<(byte[]? bytes, string filename)> BuildArticleHistoryAsync( |
| | | 485 | | IBitrixStockSyncArticleHistoryService historyService, |
| | | 486 | | string article, |
| | | 487 | | CancellationToken ct = default) |
| | | 488 | | { |
| | 5 | 489 | | article = article.Trim(); |
| | 5 | 490 | | if (string.IsNullOrEmpty(article)) |
| | 2 | 491 | | return (null, string.Empty); |
| | | 492 | | |
| | | 493 | | // Полная история по артикулу за всё время — без ограничения на число отчётов. |
| | 3 | 494 | | var data = await historyService.GetAllAsync(article, null, ct); |
| | 3 | 495 | | if (data.Reports.Count == 0) |
| | 1 | 496 | | return (null, string.Empty); |
| | | 497 | | |
| | 2 | 498 | | var hasPrices = data.HasPrices; |
| | 2 | 499 | | var hasPushes = data.HasPushes; |
| | | 500 | | const bool isMultiClient = false; |
| | 2 | 501 | | var extraCols = hasPrices ? 3 : 0; |
| | 2 | 502 | | var pushCols = hasPushes ? 7 : 0; |
| | 2 | 503 | | var totalCols = (isMultiClient ? 15 : 14) + extraCols + pushCols; |
| | 2 | 504 | | var totalRows = data.TotalDataRowCount; |
| | | 505 | | |
| | 2 | 506 | | using var wb = new XLWorkbook(); |
| | 2 | 507 | | var ws = wb.Worksheets.Add("Синхронизация остатков"); |
| | | 508 | | |
| | 2 | 509 | | ws.Cell(1, 1).Value = $"История синхронизаций по артикулу: {article}"; |
| | 2 | 510 | | ws.Cell(1, 1).Style.Font.Bold = true; |
| | 2 | 511 | | ws.Cell(1, 1).Style.Font.FontSize = 13; |
| | 2 | 512 | | ws.Range(1, 1, 1, totalCols).Merge(); |
| | | 513 | | |
| | 2 | 514 | | var metaLine = |
| | 2 | 515 | | $"Экспорт: {BitrixStockMskDateTime.Format(DateTime.UtcNow)} (MSK) | Отчётов: {data.Reports.Count}/{data.To |
| | 2 | 516 | | if (data.ExportTruncated) |
| | 1 | 517 | | metaLine += $" | Показаны последние {data.Reports.Count} отчётов (лимит {BitrixStockArticleHistoryPageDto. |
| | 2 | 518 | | ws.Cell(2, 1).Value = metaLine; |
| | 2 | 519 | | ws.Cell(2, 1).Style.Font.Italic = true; |
| | 2 | 520 | | ws.Range(2, 1, 2, totalCols).Merge(); |
| | | 521 | | |
| | 2 | 522 | | var headers = BuildHeaders(hasPrices, hasPushes, isMultiClient); |
| | 2 | 523 | | WriteHeaderRow(ws, headers, 4); |
| | | 524 | | |
| | 2 | 525 | | var row = 5; |
| | 206 | 526 | | foreach (var report in data.Reports) |
| | | 527 | | { |
| | 101 | 528 | | var clientLabel = report.IsMultiClient |
| | 101 | 529 | | ? string.Join(", ", report.MultiClientNames.Values.DefaultIfEmpty(report.OzonClientName).Distinct()) |
| | 101 | 530 | | : report.OzonClientName; |
| | 101 | 531 | | var shortId = (report.ReportId ?? "").Length > 8 ? report.ReportId![..8] : report.ReportId ?? ""; |
| | 101 | 532 | | ws.Range(row, 1, row, totalCols).Merge(); |
| | 101 | 533 | | var sepCell = ws.Cell(row, 1); |
| | 101 | 534 | | sepCell.Value = |
| | 101 | 535 | | $"Отчёт синхронизации: {clientLabel} | Сформирован: {BitrixStockMskDateTime.Format(report.GeneratedAtU |
| | 101 | 536 | | sepCell.Style.Font.Bold = true; |
| | 101 | 537 | | sepCell.Style.Fill.BackgroundColor = XLColor.FromArgb(0xE3, 0xF2, 0xFD); |
| | 101 | 538 | | sepCell.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left; |
| | 101 | 539 | | row++; |
| | | 540 | | |
| | 404 | 541 | | foreach (var item in report.Rows) |
| | 101 | 542 | | row = WriteWarehouseRow(ws, row, item.Line, item.Price, hasPrices, hasPushes, isMultiClient, totalCols); |
| | | 543 | | } |
| | | 544 | | |
| | 2 | 545 | | ApplyColumnWidths(ws, hasPrices, hasPushes, isMultiClient, extraCols, pushCols); |
| | 2 | 546 | | ws.SheetView.FreezeRows(4); |
| | | 547 | | |
| | 2 | 548 | | using var ms = new MemoryStream(); |
| | 2 | 549 | | wb.SaveAs(ms); |
| | 16 | 550 | | var safeArticle = new string(article.Where(c => char.IsLetterOrDigit(c) || c is '-' or '_').ToArray()); |
| | 2 | 551 | | if (string.IsNullOrEmpty(safeArticle)) safeArticle = "article"; |
| | 2 | 552 | | var filename = $"sync-history-{safeArticle}-{DateTime.Now:yyyyMMdd}.xlsx"; |
| | 2 | 553 | | return (ms.ToArray(), filename); |
| | 5 | 554 | | } |
| | | 555 | | |
| | | 556 | | private static string[] BuildHeaders(bool hasPrices, bool hasPushes, bool isMultiClient) |
| | | 557 | | { |
| | 2 | 558 | | var baseHeaders = isMultiClient |
| | 2 | 559 | | ? new[] { "Артикул", "Производитель", "C0", "Δ C0", "Клиент Ozon", "Склад 1С", "Склад Ozon", "1С", "Было (Oz |
| | 2 | 560 | | : new[] { "Артикул", "Производитель", "C0", "Δ C0", "Склад 1С", "Склад Ozon", "1С", "Было (Ozon)", "Нужно", |
| | 2 | 561 | | var pushHeaders = hasPushes |
| | 2 | 562 | | ? new[] { "Цена до наката", "Цена после наката", "Конкурент", "Индекс до", "Индекс после", "Распродажа", "Да |
| | 2 | 563 | | : Array.Empty<string>(); |
| | 2 | 564 | | return hasPrices |
| | 2 | 565 | | ? baseHeaders.Take(4) |
| | 2 | 566 | | .Concat(new[] { "Цена (со скидками)", "Цена продажи", "Индекс цены" }) |
| | 2 | 567 | | .Concat(pushHeaders) |
| | 2 | 568 | | .Concat(baseHeaders.Skip(4)) |
| | 2 | 569 | | .ToArray() |
| | 2 | 570 | | : baseHeaders; |
| | | 571 | | } |
| | | 572 | | |
| | | 573 | | private static void WriteHeaderRow(IXLWorksheet ws, string[] headers, int headerRow) |
| | | 574 | | { |
| | 104 | 575 | | for (var c = 0; c < headers.Length; c++) |
| | | 576 | | { |
| | 50 | 577 | | var cell = ws.Cell(headerRow, c + 1); |
| | 50 | 578 | | cell.Value = headers[c]; |
| | 50 | 579 | | cell.Style.Font.Bold = true; |
| | 50 | 580 | | cell.Style.Fill.BackgroundColor = XLColor.FromArgb(0x21, 0x96, 0xF3); |
| | 50 | 581 | | cell.Style.Font.FontColor = XLColor.White; |
| | 50 | 582 | | cell.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center; |
| | | 583 | | } |
| | 2 | 584 | | } |
| | | 585 | | |
| | | 586 | | private static int WriteWarehouseRow( |
| | | 587 | | IXLWorksheet ws, |
| | | 588 | | int row, |
| | | 589 | | BitrixStockSyncReportWarehouseLineDto w, |
| | | 590 | | OzonPriceInfoDto? pi, |
| | | 591 | | bool hasPrices, |
| | | 592 | | bool hasPushes, |
| | | 593 | | bool isMultiClient, |
| | | 594 | | int totalCols) |
| | | 595 | | { |
| | 101 | 596 | | var col = 1; |
| | 101 | 597 | | ws.Cell(row, col++).Value = w.Article; |
| | 101 | 598 | | ws.Cell(row, col++).Value = w.ManufacturerName ?? ""; |
| | 101 | 599 | | var priceCell = ws.Cell(row, col++); |
| | 101 | 600 | | if (w.OneCPrice.HasValue) { priceCell.Value = w.OneCPrice.Value; priceCell.Style.NumberFormat.Format = "#,##0"; |
| | 101 | 601 | | else priceCell.Value = ""; |
| | | 602 | | |
| | 101 | 603 | | var deltaC0Cell = ws.Cell(row, col++); |
| | 101 | 604 | | if (w.OneCPrice.HasValue && w.PrevOneCPrice.HasValue) |
| | | 605 | | { |
| | 0 | 606 | | var diff = (long)Math.Round(w.OneCPrice.Value) - (long)Math.Round(w.PrevOneCPrice.Value); |
| | 0 | 607 | | var prevFmt = ((long)Math.Round(w.PrevOneCPrice.Value)).ToString("N0"); |
| | 0 | 608 | | if (diff == 0) |
| | | 609 | | { |
| | 0 | 610 | | deltaC0Cell.Value = "="; |
| | 0 | 611 | | deltaC0Cell.Style.Font.FontColor = XLColor.Gray; |
| | | 612 | | } |
| | 0 | 613 | | else if (diff > 0) |
| | | 614 | | { |
| | 0 | 615 | | deltaC0Cell.Value = $"↑ +{diff:N0} (было {prevFmt})"; |
| | 0 | 616 | | deltaC0Cell.Style.Font.FontColor = XLColor.FromArgb(0x19, 0x87, 0x54); |
| | | 617 | | } |
| | | 618 | | else |
| | | 619 | | { |
| | 0 | 620 | | deltaC0Cell.Value = $"↓ {diff:N0} (было {prevFmt})"; |
| | 0 | 621 | | deltaC0Cell.Style.Font.FontColor = XLColor.FromArgb(0xDC, 0x35, 0x45); |
| | | 622 | | } |
| | | 623 | | } |
| | 101 | 624 | | else deltaC0Cell.Value = ""; |
| | | 625 | | |
| | 101 | 626 | | if (hasPrices) |
| | | 627 | | { |
| | 101 | 628 | | var truePriceCell = ws.Cell(row, col++); |
| | 303 | 629 | | if (pi?.TruePrice is { } tp) { truePriceCell.Value = (double)Math.Round(tp); truePriceCell.Style.NumberForma |
| | 0 | 630 | | else truePriceCell.Value = ""; |
| | | 631 | | |
| | 101 | 632 | | var sellingPriceCell = ws.Cell(row, col++); |
| | 101 | 633 | | if (pi?.SellingPrice is { } sp) { sellingPriceCell.Value = (double)Math.Round(sp); sellingPriceCell.Style.Nu |
| | 101 | 634 | | else sellingPriceCell.Value = ""; |
| | | 635 | | |
| | 101 | 636 | | var indexCell = ws.Cell(row, col++); |
| | 101 | 637 | | if (!string.IsNullOrEmpty(pi?.ColorIndex) && IndexStyles.TryGetValue(pi.ColorIndex, out var style)) |
| | | 638 | | { |
| | 0 | 639 | | indexCell.Value = style.label; |
| | 0 | 640 | | indexCell.Style.Fill.BackgroundColor = style.bg; |
| | 0 | 641 | | indexCell.Style.Font.FontColor = style.fg; |
| | 0 | 642 | | indexCell.Style.Font.Bold = true; |
| | 0 | 643 | | indexCell.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center; |
| | | 644 | | } |
| | 101 | 645 | | else indexCell.Value = ""; |
| | | 646 | | |
| | 101 | 647 | | if (hasPushes) |
| | | 648 | | { |
| | 101 | 649 | | var pushedOldCell = ws.Cell(row, col++); |
| | 101 | 650 | | if (pi?.PricePushedOld is { } po) { pushedOldCell.Value = (double)Math.Round(po); pushedOldCell.Style.Nu |
| | 101 | 651 | | else pushedOldCell.Value = ""; |
| | | 652 | | |
| | 101 | 653 | | var pushedNewCell = ws.Cell(row, col++); |
| | 101 | 654 | | if (pi?.PricePushedNew is { } pn) |
| | | 655 | | { |
| | 101 | 656 | | pushedNewCell.Value = (double)Math.Round(pn); |
| | 101 | 657 | | pushedNewCell.Style.NumberFormat.Format = "#,##0"; |
| | 101 | 658 | | pushedNewCell.Style.Font.Bold = true; |
| | 101 | 659 | | pushedNewCell.Style.Font.FontColor = XLColor.FromArgb(0x19, 0x87, 0x54); |
| | | 660 | | } |
| | 0 | 661 | | else if (pi?.PricePushStatus == "Unchanged") |
| | | 662 | | { |
| | 0 | 663 | | var curPrice = pi.SellingPrice ?? pi.TruePrice; |
| | 0 | 664 | | pushedNewCell.Value = curPrice.HasValue |
| | 0 | 665 | | ? $"без изменений · {(long)Math.Round(curPrice.Value):N0}" |
| | 0 | 666 | | : "без изменений"; |
| | 0 | 667 | | pushedNewCell.Style.Font.FontColor = XLColor.Gray; |
| | 0 | 668 | | pushedNewCell.Style.Font.Italic = true; |
| | | 669 | | } |
| | 0 | 670 | | else if (pi?.PricePushStatus == "Error") |
| | | 671 | | { |
| | 0 | 672 | | var curPrice = pi.SellingPrice ?? pi.TruePrice; |
| | 0 | 673 | | var errText = string.IsNullOrEmpty(pi.PricePushError) ? "Ошибка" : $"Ошибка: {pi.PricePushError}"; |
| | 0 | 674 | | pushedNewCell.Value = curPrice.HasValue ? $"{errText} · {(long)Math.Round(curPrice.Value):N0}" : err |
| | 0 | 675 | | pushedNewCell.Style.Font.FontColor = XLColor.FromArgb(0xDC, 0x35, 0x45); |
| | | 676 | | } |
| | 0 | 677 | | else pushedNewCell.Value = ""; |
| | | 678 | | |
| | 101 | 679 | | var competitorCell = ws.Cell(row, col++); |
| | 101 | 680 | | if (pi?.PushedCompetitorPrice is { } cp) { competitorCell.Value = (double)Math.Round(cp); competitorCell |
| | 101 | 681 | | else competitorCell.Value = ""; |
| | | 682 | | |
| | 101 | 683 | | var idxBeforeCell = ws.Cell(row, col++); |
| | 101 | 684 | | if (!string.IsNullOrEmpty(pi?.PushedColorIndexBefore) && IndexStyles.TryGetValue(pi.PushedColorIndexBefo |
| | | 685 | | { |
| | 0 | 686 | | idxBeforeCell.Value = sb.label; |
| | 0 | 687 | | idxBeforeCell.Style.Fill.BackgroundColor = sb.bg; |
| | 0 | 688 | | idxBeforeCell.Style.Font.FontColor = sb.fg; |
| | 0 | 689 | | idxBeforeCell.Style.Font.Bold = true; |
| | 0 | 690 | | idxBeforeCell.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center; |
| | | 691 | | } |
| | 101 | 692 | | else idxBeforeCell.Value = ""; |
| | | 693 | | |
| | 101 | 694 | | var idxAfterCell = ws.Cell(row, col++); |
| | 101 | 695 | | if (!string.IsNullOrEmpty(pi?.PushedColorIndexAfter) && IndexStyles.TryGetValue(pi.PushedColorIndexAfter |
| | | 696 | | { |
| | 0 | 697 | | idxAfterCell.Value = sa.label; |
| | 0 | 698 | | idxAfterCell.Style.Fill.BackgroundColor = sa.bg; |
| | 0 | 699 | | idxAfterCell.Style.Font.FontColor = sa.fg; |
| | 0 | 700 | | idxAfterCell.Style.Font.Bold = true; |
| | 0 | 701 | | idxAfterCell.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center; |
| | | 702 | | } |
| | 101 | 703 | | else idxAfterCell.Value = ""; |
| | | 704 | | |
| | 101 | 705 | | var markupCell = ws.Cell(row, col++); |
| | 101 | 706 | | if (pi?.PushedMarkupPct is { } mp) |
| | | 707 | | { |
| | 0 | 708 | | markupCell.Value = mp == 0 ? "—" : $"{mp:0.#}%"; |
| | 0 | 709 | | if (mp == 0) markupCell.Style.Font.FontColor = XLColor.Gray; |
| | | 710 | | } |
| | 101 | 711 | | else markupCell.Value = ""; |
| | | 712 | | |
| | 101 | 713 | | var pushedAtCell = ws.Cell(row, col++); |
| | 101 | 714 | | if (pi?.PricePushedAtUtc is { } pat) |
| | 0 | 715 | | pushedAtCell.Value = BitrixStockMskDateTime.Format(pat); |
| | 101 | 716 | | else pushedAtCell.Value = ""; |
| | | 717 | | } |
| | | 718 | | } |
| | | 719 | | |
| | 101 | 720 | | if (isMultiClient) |
| | 0 | 721 | | ws.Cell(row, col++).Value = w.OzonClientName ?? ""; |
| | 101 | 722 | | ws.Cell(row, col++).Value = w.OneCStore; |
| | 101 | 723 | | ws.Cell(row, col++).Value = w.OzonWarehouseName; |
| | 101 | 724 | | var oneCCell2 = ws.Cell(row, col++); |
| | 101 | 725 | | if (w.ReservedQty > 0) oneCCell2.Value = $"{w.OneCQty} (-{w.ReservedQty})"; |
| | 101 | 726 | | else oneCCell2.Value = w.OneCQty; |
| | 101 | 727 | | ws.Cell(row, col++).Value = w.OzonQty; |
| | | 728 | | |
| | 101 | 729 | | var targetQty = BitrixStockSyncReportRowPresenter.GetTargetQty(w); |
| | 101 | 730 | | var needsCell = ws.Cell(row, col++); |
| | 202 | 731 | | if (targetQty.HasValue) needsCell.Value = targetQty.Value; |
| | 0 | 732 | | else needsCell.Value = "—"; |
| | | 733 | | |
| | 101 | 734 | | var staleCell = ws.Cell(row, col++); |
| | 101 | 735 | | if (w.OzonQtyAfter.HasValue) staleCell.Value = w.OzonQtyAfter.Value; |
| | 101 | 736 | | else staleCell.Value = "—"; |
| | | 737 | | |
| | 101 | 738 | | var deltaCell = ws.Cell(row, col++); |
| | 101 | 739 | | if (w.OzonQtyAfter.HasValue) |
| | | 740 | | { |
| | 0 | 741 | | var actualDelta = w.OzonQtyAfter.Value - w.OzonQty; |
| | 0 | 742 | | deltaCell.Value = actualDelta > 0 ? $"+{actualDelta}" : actualDelta.ToString(); |
| | 0 | 743 | | deltaCell.Style.Font.Bold = true; |
| | 0 | 744 | | deltaCell.Style.Font.FontColor = actualDelta > 0 |
| | 0 | 745 | | ? XLColor.FromArgb(0x19, 0x87, 0x54) |
| | 0 | 746 | | : actualDelta < 0 |
| | 0 | 747 | | ? XLColor.FromArgb(0xDC, 0x35, 0x45) |
| | 0 | 748 | | : XLColor.Gray; |
| | | 749 | | } |
| | | 750 | | else |
| | | 751 | | { |
| | 101 | 752 | | deltaCell.Value = w.Delta > 0 ? $"+{w.Delta}" : w.Delta == 0 ? "0" : w.Delta.ToString(); |
| | 101 | 753 | | deltaCell.Style.Font.FontColor = XLColor.Gray; |
| | | 754 | | } |
| | | 755 | | |
| | 101 | 756 | | ws.Cell(row, col++).Value = w.ActionLabel; |
| | 101 | 757 | | ws.Cell(row, col++).Value = BitrixStockSyncReportRowPresenter.FormatOzonApplyStatus(w); |
| | 101 | 758 | | ws.Cell(row, col++).Value = BitrixStockSyncReportRowPresenter.FormatOzonCleanupStatus(w); |
| | 101 | 759 | | ws.Cell(row, col).Value = w.Note ?? ""; |
| | | 760 | | |
| | 101 | 761 | | if (!string.IsNullOrEmpty(w.Action) && ActionColors.TryGetValue(w.Action, out var bg)) |
| | 101 | 762 | | ws.Range(row, 1, row, totalCols).Style.Fill.BackgroundColor = bg; |
| | | 763 | | |
| | 101 | 764 | | return row + 1; |
| | | 765 | | } |
| | | 766 | | |
| | | 767 | | private static void ApplyColumnWidths(IXLWorksheet ws, bool hasPrices, bool hasPushes, bool isMultiClient, int extra |
| | | 768 | | { |
| | 2 | 769 | | ws.Column(1).Width = 18; |
| | 2 | 770 | | ws.Column(2).Width = 18; |
| | 2 | 771 | | ws.Column(3).Width = 10; |
| | 2 | 772 | | ws.Column(4).Width = 22; |
| | 2 | 773 | | if (hasPrices) |
| | | 774 | | { |
| | 2 | 775 | | ws.Column(5).Width = 12; |
| | 2 | 776 | | ws.Column(6).Width = 14; |
| | 2 | 777 | | ws.Column(7).Width = 16; |
| | | 778 | | } |
| | 2 | 779 | | if (hasPushes) |
| | | 780 | | { |
| | 2 | 781 | | ws.Column(8).Width = 14; |
| | 2 | 782 | | ws.Column(9).Width = 16; |
| | 2 | 783 | | ws.Column(10).Width = 12; |
| | 2 | 784 | | ws.Column(11).Width = 14; |
| | 2 | 785 | | ws.Column(12).Width = 14; |
| | 2 | 786 | | ws.Column(13).Width = 11; |
| | 2 | 787 | | ws.Column(14).Width = 18; |
| | | 788 | | } |
| | 2 | 789 | | var shift = extraCols + pushCols; |
| | 2 | 790 | | if (isMultiClient) |
| | | 791 | | { |
| | 0 | 792 | | ws.Column(4 + shift + 1).Width = 20; |
| | 0 | 793 | | ws.Column(4 + shift + 2).Width = 16; |
| | 0 | 794 | | ws.Column(4 + shift + 3).Width = 22; |
| | 0 | 795 | | ws.Column(4 + shift + 4).Width = 8; |
| | 0 | 796 | | ws.Column(4 + shift + 5).Width = 10; |
| | 0 | 797 | | ws.Column(4 + shift + 6).Width = 10; |
| | 0 | 798 | | ws.Column(4 + shift + 7).Width = 10; |
| | 0 | 799 | | ws.Column(4 + shift + 8).Width = 16; |
| | 0 | 800 | | ws.Column(4 + shift + 9).Width = 26; |
| | 0 | 801 | | ws.Column(4 + shift + 10).Width = 50; |
| | 0 | 802 | | ws.Column(4 + shift + 11).Width = 30; |
| | 0 | 803 | | ws.Column(4 + shift + 12).Width = 40; |
| | | 804 | | } |
| | | 805 | | else |
| | | 806 | | { |
| | 2 | 807 | | ws.Column(4 + shift + 1).Width = 16; |
| | 2 | 808 | | ws.Column(4 + shift + 2).Width = 22; |
| | 2 | 809 | | ws.Column(4 + shift + 3).Width = 8; |
| | 2 | 810 | | ws.Column(4 + shift + 4).Width = 10; |
| | 2 | 811 | | ws.Column(4 + shift + 5).Width = 10; |
| | 2 | 812 | | ws.Column(4 + shift + 6).Width = 10; |
| | 2 | 813 | | ws.Column(4 + shift + 7).Width = 16; |
| | 2 | 814 | | ws.Column(4 + shift + 8).Width = 26; |
| | 2 | 815 | | ws.Column(4 + shift + 9).Width = 50; |
| | 2 | 816 | | ws.Column(4 + shift + 10).Width = 30; |
| | 2 | 817 | | ws.Column(4 + shift + 11).Width = 40; |
| | | 818 | | } |
| | 2 | 819 | | } |
| | | 820 | | |
| | | 821 | | public static string GetSkipLabelStatic(string? error) => |
| | 30 | 822 | | BitrixStockSyncReportRowPresenter.GetSkipLabel(error); |
| | | 823 | | } |