| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using OzonOrdersWeb.Areas.OneCFresh.ViewModels; |
| | | 4 | | using Services.ApiServices._1CApi.OData; |
| | | 5 | | using Services.ApiServices._1CApi.OData.Models; |
| | | 6 | | |
| | | 7 | | namespace OzonOrdersWeb.Areas.OneCFresh.Controllers; |
| | | 8 | | |
| | | 9 | | [Authorize(Roles = "User,Admin")] |
| | | 10 | | [Area("OneCFresh")] |
| | | 11 | | [Route("OneCFresh/Register/[action]")] |
| | | 12 | | public class RegisterController : Controller |
| | | 13 | | { |
| | | 14 | | private readonly IOneCODataReferenceService _refService; |
| | | 15 | | private readonly IOneCODataClient _client; |
| | | 16 | | private const int StockPageSize = 500; |
| | | 17 | | |
| | 0 | 18 | | public RegisterController(IOneCODataReferenceService refService, IOneCODataClient client) |
| | | 19 | | { |
| | 0 | 20 | | _refService = refService; |
| | 0 | 21 | | _client = client; |
| | 0 | 22 | | } |
| | | 23 | | |
| | | 24 | | [HttpGet] |
| | | 25 | | public async Task<IActionResult> Stock( |
| | | 26 | | string? search = null, |
| | | 27 | | Guid? warehouseFilter = null, |
| | | 28 | | bool load = false, |
| | | 29 | | int skip = 0) |
| | | 30 | | { |
| | 0 | 31 | | ViewData["Title"] = "Запасы на складах"; |
| | | 32 | | |
| | 0 | 33 | | var warehouses = await _refService.GetWarehousesAsync(); |
| | 0 | 34 | | var whNames = warehouses.DistinctBy(w => w.RefKey).ToDictionary(w => w.RefKey, w => w.Description); |
| | | 35 | | |
| | 0 | 36 | | if (!load) |
| | | 37 | | { |
| | 0 | 38 | | return View(new StockViewModel |
| | 0 | 39 | | { |
| | 0 | 40 | | Search = search, |
| | 0 | 41 | | WarehouseFilter = warehouseFilter, |
| | 0 | 42 | | WarehouseNames = whNames, |
| | 0 | 43 | | NomenclatureNames = new Dictionary<Guid, string?>(), |
| | 0 | 44 | | NomenclatureArticles = new Dictionary<Guid, string?>(), |
| | 0 | 45 | | Items = new List<StockRow>() |
| | 0 | 46 | | }); |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | try |
| | | 50 | | { |
| | 0 | 51 | | var records = await _client.GetPageAsync<ODataStockRecord>( |
| | 0 | 52 | | "AccumulationRegister_ЗапасыНаСкладах", |
| | 0 | 53 | | top: StockPageSize, |
| | 0 | 54 | | skip: skip); |
| | | 55 | | |
| | 0 | 56 | | var balances = new Dictionary<(Guid nom, Guid wh), (decimal qty, DateTime last)>(); |
| | 0 | 57 | | foreach (var record in records) |
| | | 58 | | { |
| | 0 | 59 | | foreach (var row in record.RecordSet ?? Enumerable.Empty<ODataStockMovement>()) |
| | | 60 | | { |
| | 0 | 61 | | if (!DateTime.TryParse(row.Period, out var period)) period = DateTime.MinValue; |
| | 0 | 62 | | var key = (row.NomenclatureKey, row.WarehouseKey); |
| | 0 | 63 | | balances.TryGetValue(key, out var existing); |
| | 0 | 64 | | var delta = row.RecordType == "Receipt" ? row.Quantity : -row.Quantity; |
| | 0 | 65 | | balances[key] = (existing.qty + delta, period > existing.last ? period : existing.last); |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | var rows = balances |
| | 0 | 70 | | .Where(kv => kv.Value.qty != 0) |
| | 0 | 71 | | .Select(kv => new StockRow |
| | 0 | 72 | | { |
| | 0 | 73 | | NomenclatureKey = kv.Key.nom, |
| | 0 | 74 | | WarehouseKey = kv.Key.wh, |
| | 0 | 75 | | Quantity = kv.Value.qty, |
| | 0 | 76 | | LastMovement = kv.Value.last |
| | 0 | 77 | | }) |
| | 0 | 78 | | .Where(r => !warehouseFilter.HasValue || r.WarehouseKey == warehouseFilter.Value) |
| | 0 | 79 | | .OrderByDescending(r => r.LastMovement) |
| | 0 | 80 | | .ToList(); |
| | | 81 | | |
| | | 82 | | // Загружаем имена только для отображаемых позиций (не всех) |
| | 0 | 83 | | var nomKeys = rows.Select(r => r.NomenclatureKey).Distinct().Take(200).ToHashSet(); |
| | 0 | 84 | | var nomNames = new Dictionary<Guid, string?>(); |
| | 0 | 85 | | var nomArticles = new Dictionary<Guid, string?>(); |
| | | 86 | | |
| | 0 | 87 | | var nomPage = await _client.GetPageAsync<ODataNomenclature>( |
| | 0 | 88 | | "Catalog_Номенклатура", |
| | 0 | 89 | | top: 1000, |
| | 0 | 90 | | queryParams: new Dictionary<string, string> |
| | 0 | 91 | | { |
| | 0 | 92 | | ["$select"] = "Ref_Key,Description,Артикул", |
| | 0 | 93 | | ["$filter"] = "IsFolder eq false" |
| | 0 | 94 | | }); |
| | | 95 | | |
| | 0 | 96 | | foreach (var n in nomPage) |
| | | 97 | | { |
| | 0 | 98 | | if (!nomNames.ContainsKey(n.RefKey)) |
| | | 99 | | { |
| | 0 | 100 | | nomNames[n.RefKey] = n.Description ?? string.Empty; |
| | 0 | 101 | | nomArticles[n.RefKey] = n.Article ?? string.Empty; |
| | | 102 | | } |
| | | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | if (!string.IsNullOrWhiteSpace(search)) |
| | | 106 | | { |
| | 0 | 107 | | var s = search.Trim(); |
| | 0 | 108 | | rows = rows.Where(r => |
| | 0 | 109 | | (nomNames.TryGetValue(r.NomenclatureKey, out var name) && (name?.Contains(s, StringComparison.Ordina |
| | 0 | 110 | | (nomArticles.TryGetValue(r.NomenclatureKey, out var art) && (art?.Contains(s, StringComparison.Ordin |
| | | 111 | | } |
| | | 112 | | |
| | 0 | 113 | | ViewBag.LoadedRecorders = records.Count; |
| | 0 | 114 | | ViewBag.NextSkip = skip + StockPageSize; |
| | 0 | 115 | | ViewBag.HasMore = records.Count == StockPageSize; |
| | | 116 | | |
| | 0 | 117 | | return View(new StockViewModel |
| | 0 | 118 | | { |
| | 0 | 119 | | Items = rows, |
| | 0 | 120 | | Search = search, |
| | 0 | 121 | | WarehouseFilter = warehouseFilter, |
| | 0 | 122 | | WarehouseNames = whNames, |
| | 0 | 123 | | NomenclatureNames = nomNames, |
| | 0 | 124 | | NomenclatureArticles = nomArticles |
| | 0 | 125 | | }); |
| | | 126 | | } |
| | 0 | 127 | | catch (Exception ex) |
| | | 128 | | { |
| | 0 | 129 | | TempData["Error"] = $"Ошибка загрузки остатков: {ex.Message}"; |
| | 0 | 130 | | return View(new StockViewModel |
| | 0 | 131 | | { |
| | 0 | 132 | | Search = search, |
| | 0 | 133 | | WarehouseFilter = warehouseFilter, |
| | 0 | 134 | | WarehouseNames = whNames |
| | 0 | 135 | | }); |
| | | 136 | | } |
| | 0 | 137 | | } |
| | | 138 | | } |