| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonRepositories.Context; |
| | | 3 | | |
| | | 4 | | namespace OzonOrdersWeb.WebServices.ArmtekEtp; |
| | | 5 | | |
| | | 6 | | /// <summary>Сводка взаиморасчётов одного поставщика: сальдо по последнему документу, объёмы и время обновления.</summar |
| | | 7 | | public sealed record SettlementSupplierSummary( |
| | | 8 | | int SettlementsCount, |
| | | 9 | | int InvoicesCount, |
| | | 10 | | int ItemsCount, |
| | | 11 | | DateTime? LastUpdatedAtUtc, |
| | | 12 | | string? LastError, |
| | | 13 | | DateTime? LastErrorAtUtc, |
| | | 14 | | decimal? Balance, |
| | | 15 | | string? BalanceCurrency, |
| | | 16 | | DateTime? BalanceDate); |
| | | 17 | | |
| | | 18 | | /// <summary>Плашка поставщика на странице /Settlements.</summary> |
| | | 19 | | public sealed record SettlementSupplierTile( |
| | | 20 | | string Key, |
| | | 21 | | string Name, |
| | | 22 | | string LogoUrl, |
| | | 23 | | string? LogoMark, |
| | | 24 | | string Description, |
| | | 25 | | string Url, |
| | | 26 | | SettlementSupplierSummary Summary, |
| | | 27 | | IReadOnlyList<(string Title, string Icon, string Url)> Links); |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Поставщики раздела «Взаиморасчёты». Сейчас только Armtek (кабинет etp.armtek.ru); следующий поставщик добавляется |
| | | 31 | | /// своей плашкой в <see cref="GetTilesAsync"/>. |
| | | 32 | | /// </summary> |
| | 4 | 33 | | public sealed class SettlementSuppliersOverview(ApplicationDbContext db, IConfiguration configuration) |
| | | 34 | | { |
| | | 35 | | /// <summary>Расписание автозагрузки для подписи на страницах: «каждый день в 07:00 (МСК)» или выражение cron как ес |
| | | 36 | | public string ScheduleText |
| | | 37 | | { |
| | | 38 | | get |
| | | 39 | | { |
| | 4 | 40 | | if (!configuration.GetValue("ArmtekEtp:Enabled", false)) |
| | 1 | 41 | | return "автозагрузка выключена"; |
| | 3 | 42 | | var cron = configuration["ArmtekEtp:Cron"] is { Length: > 0 } c ? c : Services.ArmtekEtpServices.ArmtekEtpSy |
| | 3 | 43 | | var parts = cron.Split(' ', StringSplitOptions.RemoveEmptyEntries); |
| | 3 | 44 | | return parts.Length == 5 && parts[2] == "*" && parts[3] == "*" && parts[4] == "*" |
| | 3 | 45 | | && int.TryParse(parts[0], out var minute) && int.TryParse(parts[1], out var hour) |
| | 3 | 46 | | ? $"каждый день в {hour:00}:{minute:00} (МСК)" |
| | 3 | 47 | | : $"по расписанию {cron} (МСК)"; |
| | | 48 | | } |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | public async Task<SettlementSupplierSummary> GetArmtekSummaryAsync(CancellationToken ct = default) |
| | | 52 | | { |
| | 0 | 53 | | var last = await db.ArmtekSettlements.AsNoTracking() |
| | 0 | 54 | | .Where(x => x.Balance != null) |
| | 0 | 55 | | .OrderByDescending(x => x.DocDate).ThenByDescending(x => x.Id) |
| | 0 | 56 | | .Select(x => new { x.Balance, x.Currency, x.DocDate }) |
| | 0 | 57 | | .FirstOrDefaultAsync(ct); |
| | | 58 | | |
| | | 59 | | // «Обновлено» — время последней успешной загрузки, даже если новых документов не было. |
| | 0 | 60 | | var lastSuccess = await db.ArmtekEtpSyncRuns.AsNoTracking() |
| | 0 | 61 | | .Where(x => x.Success == true) |
| | 0 | 62 | | .MaxAsync(x => x.FinishedAtUtc, ct); |
| | | 63 | | // Предупреждение — по последнему завершённому запуску: его ошибка или то, что взаиморасчёты из кабинета |
| | | 64 | | // не обновились (фактуры по API при этом загружены). |
| | 0 | 65 | | var lastRun = await db.ArmtekEtpSyncRuns.AsNoTracking() |
| | 0 | 66 | | .Where(x => x.FinishedAtUtc != null) |
| | 0 | 67 | | .OrderByDescending(x => x.FinishedAtUtc) |
| | 0 | 68 | | .Select(x => new { x.Success, x.Message, x.SettlementsError, x.FinishedAtUtc }) |
| | 0 | 69 | | .FirstOrDefaultAsync(ct); |
| | 0 | 70 | | var lastFailure = lastRun == null ? null |
| | 0 | 71 | | : lastRun.Success == false ? new { lastRun.Message, lastRun.FinishedAtUtc } |
| | 0 | 72 | | : lastRun.SettlementsError != null ? new { Message = (string?)("Взаиморасчёты из кабинета не обновлены: " + |
| | 0 | 73 | | : null; |
| | | 74 | | |
| | 0 | 75 | | return new SettlementSupplierSummary( |
| | 0 | 76 | | await db.ArmtekSettlements.CountAsync(ct), |
| | 0 | 77 | | await db.ArmtekInvoices.CountAsync(ct), |
| | 0 | 78 | | await db.ArmtekInvoiceItems.CountAsync(ct), |
| | 0 | 79 | | lastSuccess ?? await db.ArmtekSettlements.MaxAsync(x => (DateTime?)x.UpdatedAtUtc, ct), |
| | 0 | 80 | | lastFailure?.Message, |
| | 0 | 81 | | lastFailure?.FinishedAtUtc, |
| | 0 | 82 | | last?.Balance, |
| | 0 | 83 | | last?.Currency, |
| | 0 | 84 | | last?.DocDate); |
| | 0 | 85 | | } |
| | | 86 | | |
| | | 87 | | public async Task<IReadOnlyList<SettlementSupplierTile>> GetTilesAsync(CancellationToken ct = default) |
| | | 88 | | { |
| | | 89 | | const string armtek = "/Settlements/Armtek"; |
| | 0 | 90 | | return |
| | 0 | 91 | | [ |
| | 0 | 92 | | new SettlementSupplierTile( |
| | 0 | 93 | | "Armtek", "Армтек", "/images/armtek-logo.svg", "BY", |
| | 0 | 94 | | "Взаиморасчёты, фактуры и позиции из личного кабинета etp.armtek.ru, автозагрузка " + ScheduleText, |
| | 0 | 95 | | armtek, |
| | 0 | 96 | | await GetArmtekSummaryAsync(ct), |
| | 0 | 97 | | [ |
| | 0 | 98 | | ("Взаиморасчёты", "fa-scale-balanced", armtek + "?tab=settlements"), |
| | 0 | 99 | | ("Фактуры", "fa-file-invoice", armtek + "?tab=invoices"), |
| | 0 | 100 | | ("Позиции фактур", "fa-list", armtek + "?tab=items") |
| | 0 | 101 | | ]) |
| | 0 | 102 | | ]; |
| | 0 | 103 | | } |
| | | 104 | | } |