| | | 1 | | using OfficeOpenXml; |
| | | 2 | | using OzonDomains.Models; |
| | | 3 | | |
| | | 4 | | namespace Services.ParserServices.FielParsers |
| | | 5 | | { |
| | | 6 | | public class ExcelExporterBuilder |
| | | 7 | | { |
| | | 8 | | private List<Order> _orders = null!; |
| | | 9 | | private List<string> _columnsToExport = null!; |
| | | 10 | | private Dictionary<string, Func<Order, object?>> _columnMappings; |
| | | 11 | | private List<string> _columnsToSum = null!; |
| | | 12 | | |
| | 0 | 13 | | public Dictionary<string, string> ColumnFormats = new Dictionary<string, string> |
| | 0 | 14 | | { |
| | 0 | 15 | | { "Номер заказа", "@" }, |
| | 0 | 16 | | { "Клиент", "@" }, |
| | 0 | 17 | | { "Принят в обработку", "dd.MM.yyyy HH:mm" }, |
| | 0 | 18 | | { "Дата отгрузки", "dd.MM.yyyy HH:mm" }, |
| | 0 | 19 | | { "Срок доставки", @"d' д. 'hh' ч.'mm' мин.'" }, |
| | 0 | 20 | | { "Статус клиента", "@" }, |
| | 0 | 21 | | { "Статус", "@" }, |
| | 0 | 22 | | { "Наименование товара", "@" }, |
| | 0 | 23 | | { "Артикул", "@" }, |
| | 0 | 24 | | { "ЧистыйАртикул", "@" }, |
| | 0 | 25 | | { "Производитель", "@" }, |
| | 0 | 26 | | { "Склад отгрузки", "@" }, |
| | 0 | 27 | | { "Поставщик", "@" }, |
| | 0 | 28 | | { "Номер заказа поставщику", "@" }, |
| | 0 | 29 | | { "Цена сайта", "#,##0.00" }, |
| | 0 | 30 | | { "Цена", "#,##0.00" }, |
| | 0 | 31 | | { "Количество", "#,##0.00" }, |
| | 0 | 32 | | { "Сумма отправления", "#,##0.00" }, |
| | 0 | 33 | | { "Категория", "@" }, |
| | 0 | 34 | | { "Объемный вес", "#,##0.00" }, |
| | 0 | 35 | | { "Цена закупки", "#,##0.00" }, |
| | 0 | 36 | | { "Себестоимость", "#,##0.00" }, |
| | 0 | 37 | | { "Комиссия ОЗОН (мин.)", "#,##0.00" }, |
| | 0 | 38 | | { "Комиссия ОЗОН (макс.)", "#,##0.00" }, |
| | 0 | 39 | | { "Прибыль (мин.)", "#,##0.00" }, |
| | 0 | 40 | | { "Прибыль (макс.)", "#,##0.00" }, |
| | 0 | 41 | | { "Наценка % (мин.)", "#,##0.00" }, |
| | 0 | 42 | | { "Наценка % (макс.)", "#,##0.00" }, |
| | 0 | 43 | | { "Город доставки", "@" }, |
| | 0 | 44 | | { "Цена закупки до перевода", "#,##0.00" }, |
| | 0 | 45 | | { "Сумма закупки (вал)", "#,##0.00" }, |
| | 0 | 46 | | { "Валюта", "@" } |
| | 0 | 47 | | }; |
| | | 48 | | |
| | | 49 | | |
| | 0 | 50 | | public ExcelExporterBuilder() |
| | | 51 | | { |
| | 0 | 52 | | _columnMappings = new Dictionary<string, Func<Order, object?>> |
| | 0 | 53 | | { |
| | 0 | 54 | | { "Номер заказа", order => order.ShipmentNumber }, |
| | 0 | 55 | | { "Клиент", order => order.OzonClient?.Name }, |
| | 0 | 56 | | { "Принят в обработку", order => order.FullFormattedProcessingDate }, |
| | 0 | 57 | | { "Дата отгрузки", order => order.FullFormattedShippingDate }, |
| | 0 | 58 | | { "Срок доставки", order => order.FormattedDeliveryPeriod }, |
| | 0 | 59 | | { "Статус клиента", order => order.Status }, |
| | 0 | 60 | | { "Статус", order => order.AppStatus?.Name }, |
| | 0 | 61 | | { "Наименование товара", order => order.ProductName }, |
| | 0 | 62 | | { "Артикул", order => order.Article }, |
| | 0 | 63 | | { "ЧистыйАртикул", order => order.CleanedArticle }, |
| | 0 | 64 | | { "Производитель", order => order.Manufacturer?.Name }, |
| | 0 | 65 | | { "Склад отгрузки", order => order.ShipmentWarehouse?.Name }, |
| | 0 | 66 | | { "Поставщик", order => order.Supplier?.Name }, |
| | 0 | 67 | | { "Номер заказа поставщику", order => order.OrderNumberToSupplier }, |
| | 0 | 68 | | { "Цена сайта", order => order.ProductInfo?.CurrentPriceWithDiscount }, |
| | 0 | 69 | | { "Цена", order => order.Price }, |
| | 0 | 70 | | { "Количество", order => order.Quantity }, |
| | 0 | 71 | | { "Сумма отправления", order => order.ShipmentAmount }, |
| | 0 | 72 | | { "Категория", order => order.ProductInfo?.CommercialCategory }, |
| | 0 | 73 | | { "Объемный вес", order => order.ProductInfo?.VolumetricWeight }, |
| | 0 | 74 | | { "Цена закупки", order => order.PurchasePrice }, |
| | 0 | 75 | | { "Себестоимость", order => order.CostPrice }, |
| | 0 | 76 | | { "Комиссия ОЗОН (мин.)", order => order.MinOzonCommission }, |
| | 0 | 77 | | { "Комиссия ОЗОН (макс.)", order => order.MaxOzonCommission }, |
| | 0 | 78 | | { "Прибыль (мин.)", order => order.MinProfit }, |
| | 0 | 79 | | { "Прибыль (макс.)", order => order.MaxProfit }, |
| | 0 | 80 | | { "Наценка % (мин.)", order => order.MinDiscount }, |
| | 0 | 81 | | { "Наценка % (макс.)", order => order.MaxDiscount }, |
| | 0 | 82 | | { "Город доставки", order => order.DeliveryCity }, |
| | 0 | 83 | | { "Цена закупки до перевода", order => order.OriginalPurchasePrice }, |
| | 0 | 84 | | { "Сумма закупки (вал)", order => (order.OriginalPurchasePrice ?? 0) * (order.Quantity ?? 0) }, |
| | 0 | 85 | | { "Валюта", order => order.Сurrency?.Name } |
| | 0 | 86 | | }; |
| | 0 | 87 | | _columnsToExport = _columnMappings.Keys.ToList(); |
| | 0 | 88 | | _columnsToSum = new List<string>(); |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | public ExcelExporterBuilder WithOrders(List<Order> orders) |
| | | 92 | | { |
| | 0 | 93 | | _orders = orders; |
| | 0 | 94 | | return this; |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | public ExcelExporterBuilder WithColumnsToExport(List<string> columns) |
| | | 98 | | { |
| | 0 | 99 | | _columnsToExport = columns; |
| | 0 | 100 | | return this; |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | public ExcelExporterBuilder WithColumnSums(List<string> columnsToSum) |
| | | 104 | | { |
| | 0 | 105 | | _columnsToSum = columnsToSum; |
| | 0 | 106 | | return this; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | public byte[] Build() |
| | | 110 | | { |
| | 0 | 111 | | using (var package = new ExcelPackage()) |
| | | 112 | | { |
| | 0 | 113 | | var worksheet = package.Workbook.Worksheets.Add("Orders"); |
| | | 114 | | |
| | 0 | 115 | | BuildTable(worksheet); |
| | 0 | 116 | | AddColumnSums(worksheet); |
| | | 117 | | |
| | 0 | 118 | | worksheet.Cells.AutoFitColumns(); |
| | | 119 | | |
| | 0 | 120 | | return package.GetAsByteArray(); |
| | | 121 | | } |
| | 0 | 122 | | } |
| | | 123 | | |
| | | 124 | | private void BuildTable(ExcelWorksheet worksheet) |
| | | 125 | | { |
| | 0 | 126 | | int colIndex = 1; |
| | | 127 | | |
| | 0 | 128 | | for (int i = 0; i < _columnsToExport.Count; i++) |
| | | 129 | | { |
| | 0 | 130 | | var columnName = _columnsToExport[i]; |
| | | 131 | | |
| | 0 | 132 | | if (columnName == "Комиссия ОЗОН" || columnName == "Прибыль" || columnName == "Наценка %") |
| | | 133 | | { |
| | 0 | 134 | | worksheet.Cells[1, colIndex].Value = columnName + " (мин.)"; |
| | 0 | 135 | | colIndex++; |
| | 0 | 136 | | worksheet.Cells[1, colIndex].Value = columnName + " (макс.)"; |
| | 0 | 137 | | colIndex++; |
| | | 138 | | } |
| | | 139 | | else |
| | | 140 | | { |
| | 0 | 141 | | worksheet.Cells[1, colIndex].Value = columnName; |
| | 0 | 142 | | colIndex++; |
| | | 143 | | } |
| | | 144 | | } |
| | | 145 | | |
| | 0 | 146 | | int row = 2; |
| | 0 | 147 | | foreach (var order in _orders) |
| | | 148 | | { |
| | 0 | 149 | | colIndex = 1; |
| | 0 | 150 | | foreach (var columnName in _columnsToExport) |
| | | 151 | | { |
| | 0 | 152 | | if (columnName == "Комиссия ОЗОН" || columnName == "Прибыль" || columnName == "Наценка %") |
| | | 153 | | { |
| | 0 | 154 | | if (_columnMappings.TryGetValue(columnName + " (мин.)", out var getMinValue) && getMinValue != n |
| | | 155 | | { |
| | 0 | 156 | | worksheet.Cells[row, colIndex].Value = getMinValue(order); |
| | 0 | 157 | | colIndex++; |
| | | 158 | | } |
| | | 159 | | |
| | 0 | 160 | | if (_columnMappings.TryGetValue(columnName + " (макс.)", out var getMaxValue) && getMaxValue != |
| | | 161 | | { |
| | 0 | 162 | | worksheet.Cells[row, colIndex].Value = getMaxValue(order); |
| | 0 | 163 | | colIndex++; |
| | | 164 | | } |
| | | 165 | | } |
| | 0 | 166 | | else if (_columnMappings.TryGetValue(columnName, out var getValue) && getValue != null) |
| | | 167 | | { |
| | 0 | 168 | | worksheet.Cells[row, colIndex].Value = getValue(order); |
| | 0 | 169 | | colIndex++; |
| | | 170 | | } |
| | | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | row++; |
| | | 174 | | } |
| | | 175 | | |
| | 0 | 176 | | ApplyColumnFormats(worksheet, row); |
| | 0 | 177 | | } |
| | | 178 | | |
| | | 179 | | private void ApplyColumnFormats(ExcelWorksheet worksheet, int totalRows) |
| | | 180 | | { |
| | 0 | 181 | | for (int colIndex = 1; colIndex <= _columnsToExport.Count; colIndex++) |
| | | 182 | | { |
| | 0 | 183 | | var columnName = _columnsToExport[colIndex - 1]; |
| | 0 | 184 | | if (ColumnFormats.TryGetValue(columnName, out var format)) |
| | | 185 | | { |
| | 0 | 186 | | worksheet.Cells[2, colIndex, totalRows, colIndex].Style.Numberformat.Format = format; |
| | | 187 | | } |
| | | 188 | | } |
| | 0 | 189 | | } |
| | | 190 | | |
| | | 191 | | private void AddColumnSums(ExcelWorksheet worksheet) |
| | | 192 | | { |
| | 0 | 193 | | int row = _orders.Count + 2; |
| | | 194 | | |
| | 0 | 195 | | foreach (var columnName in _columnsToSum) |
| | | 196 | | { |
| | 0 | 197 | | int sumColumnIndex = _columnsToExport.IndexOf(columnName) + 1; |
| | 0 | 198 | | if (sumColumnIndex > 0) |
| | | 199 | | { |
| | 0 | 200 | | worksheet.Cells[row, sumColumnIndex].Formula = |
| | 0 | 201 | | $"SUM({worksheet.Cells[2, sumColumnIndex].Address}:{worksheet.Cells[row - 1, sumColumnIndex].Add |
| | 0 | 202 | | worksheet.Cells[row, sumColumnIndex].Style.Font.Bold = true; |
| | | 203 | | } |
| | | 204 | | } |
| | 0 | 205 | | } |
| | | 206 | | } |
| | | 207 | | } |