< Summary

Information
Class: OzonOrdersWeb.Areas.Studio2.Controllers.OneCDocumentController
Assembly: OzonOrdersWeb
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Areas/Studio2/Controllers/OneCDocumentController.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 90
Coverable lines: 90
Total lines: 137
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 24
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
ByNumber()0%4260%
Details()0%4260%
MapOperationKind(...)0%156120%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Areas/Studio2/Controllers/OneCDocumentController.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Authorization;
 2using Microsoft.AspNetCore.Mvc;
 3using OzonOrdersWeb.Areas.Studio2.ViewModels;
 4using OzonOrdersWeb.Authorization;
 5using Services.ApiServices._1CApi.OData;
 6
 7namespace OzonOrdersWeb.Areas.Studio2.Controllers;
 8
 9[Authorize(Roles = "User,Admin")]
 10[Area("Studio2")]
 11public class OneCDocumentController : Controller
 12{
 13    private readonly OneCDocumentViewService _documentViewService;
 14
 015    public OneCDocumentController(OneCDocumentViewService documentViewService)
 16    {
 017        _documentViewService = documentViewService;
 018    }
 19
 20    public async Task<IActionResult> ByNumber(string? number)
 21    {
 022        if (string.IsNullOrWhiteSpace(number))
 023            return View("Details", new OneCDocumentViewModel { ErrorMessage = "Номер документа не указан." });
 24
 025        var trimmed = number.Trim();
 26
 027        if (trimmed.StartsWith("STUB-", StringComparison.OrdinalIgnoreCase))
 028            return View("Details", new OneCDocumentViewModel
 029            {
 030                Number = trimmed,
 031                ErrorMessage = $"Документ «{trimmed}» является заглушкой — реального документа в 1С нет. " +
 032                               "Он был создан в системе, но не был отправлен в 1С."
 033            });
 34
 35        try
 36        {
 037            var found = await _documentViewService.FindByNumberAsync(trimmed);
 038            if (found == null)
 039                return View("Details", new OneCDocumentViewModel
 040                {
 041                    Number = trimmed,
 042                    ErrorMessage = $"Документ «{trimmed}» не найден в 1С (Расходная / Перемещение / Приходная накладная)
 043                });
 44
 045            return RedirectToAction(nameof(Details), new { refKey = found.Value.RefKey.ToString() });
 46        }
 047        catch (Exception ex)
 48        {
 049            return View("Details", new OneCDocumentViewModel
 050            {
 051                Number = trimmed,
 052                ErrorMessage = $"Ошибка поиска документа в 1С: {ex.Message}"
 053            });
 54        }
 055    }
 56
 57    [AllowAnonymous]
 58    [AssemblyListPublicAccess]
 59    public async Task<IActionResult> Details(string refKey, string? accessKey)
 60    {
 061        ViewBag.AccessKey = accessKey ?? string.Empty;
 62
 063        if (!Guid.TryParse(refKey, out var guid))
 64        {
 065            return View(new OneCDocumentViewModel
 066            {
 067                ErrorMessage = $"Некорректный GUID документа: «{refKey}»"
 068            });
 69        }
 70
 71        try
 72        {
 073            var doc = await _documentViewService.GetDocumentAsync(guid);
 074            if (doc == null)
 75            {
 076                return View(new OneCDocumentViewModel
 077                {
 078                    RefKey = refKey,
 079                    ErrorMessage = "Документ не найден в 1С."
 080                });
 81            }
 82
 083            var vm = new OneCDocumentViewModel
 084            {
 085                RefKey = refKey,
 086                DocTypeLabel = doc.DocTypeLabel,
 087                Number = doc.Number,
 088                Date = doc.Date,
 089                Posted = doc.Posted,
 090                DeletionMark = doc.DeletionMark,
 091                Counterparty = doc.CounterpartyName,
 092                Contract = doc.ContractName,
 093                Organization = doc.OrganizationName,
 094                Warehouse = doc.WarehouseName,
 095                WarehouseRecipient = doc.WarehouseRecipientName,
 096                OperationKind = MapOperationKind(doc.OperationKind),
 097                Comment = doc.Comment,
 098                TotalAmount = doc.TotalAmount,
 099                VatAmount = doc.VatAmount,
 0100                Lines = doc.Lines.Select(l => new OneCDocumentLineViewModel
 0101                {
 0102                    LineNumber = l.LineNumber,
 0103                    Nomenclature = l.NomenclatureName,
 0104                    Article = l.NomenclatureArticle,
 0105                    Characteristic = l.CharacteristicName,
 0106                    Batch = l.BatchName,
 0107                    Warehouse = l.WarehouseName,
 0108                    Quantity = l.Quantity,
 0109                    Price = l.Price,
 0110                    Amount = l.Amount,
 0111                    VatAmount = l.VatAmount,
 0112                }).ToList()
 0113            };
 114
 0115            return View(vm);
 116        }
 0117        catch (Exception ex)
 118        {
 0119            return View(new OneCDocumentViewModel
 0120            {
 0121                RefKey = refKey,
 0122                ErrorMessage = $"Ошибка загрузки документа из 1С: {ex.Message}"
 0123            });
 124        }
 0125    }
 126
 0127    private static string MapOperationKind(string kind) => kind switch
 0128    {
 0129        "РасходнаяНакладная" => "Расходная накладная",
 0130        "РеализацияТоваров" => "Реализация товаров",
 0131        "ВозвратПоставщику" => "Возврат поставщику",
 0132        "Перемещение" => "Перемещение",
 0133        "ПриходнаяНакладная" => "Приходная накладная",
 0134        "ПоступлениеТоваров" => "Поступление товаров",
 0135        _ => kind
 0136    };
 137}