File size: 2,747 Bytes
de8b914
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using Asp.Versioning;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SCM.Application.Controle;
using SCM.Application.Medicao;
using SCM.Application.Qualidade;
using SCM.Application.Relatorios;
using SCM.Shared.Api;

namespace SCM.Api.Controllers;

/// <summary>Geração de PDFs dos relatórios (faturamento, acompanhamento, relatório técnico).</summary>
[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}")]
[Authorize]
public sealed class RelatoriosPdfController : ControllerBase
{
    private readonly IPdfReportService _pdf;

    public RelatoriosPdfController(IPdfReportService pdf) => _pdf = pdf;

    /// <summary>PDF do faturamento por EAP do contrato.</summary>
    [HttpGet("contratos/{contratoId:guid}/faturamento/pdf")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)]
    public async Task<IActionResult> FaturamentoPdf(
        Guid contratoId, [FromServices] IFaturamentoService faturamento, CancellationToken ct = default)
    {
        var dados = await faturamento.GetFaturamentoAsync(contratoId, ct);
        var bytes = _pdf.Faturamento(dados);
        return File(bytes, "application/pdf", $"faturamento-{contratoId}.pdf");
    }

    /// <summary>PDF do resumo de acompanhamento da obra na data.</summary>
    [HttpGet("obras/{obraId:guid}/acompanhamento/pdf")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)]
    public async Task<IActionResult> AcompanhamentoPdf(
        Guid obraId, [FromQuery] DateOnly data,
        [FromServices] IAcompanhamentoService acompanhamento, CancellationToken ct = default)
    {
        var foto = await acompanhamento.GetFotoAsync(obraId, data, ct);
        var historico = await acompanhamento.GetHistoricoAsync(obraId, ct);
        var bytes = _pdf.Acompanhamento(foto, historico);
        return File(bytes, "application/pdf", $"acompanhamento-{obraId}.pdf");
    }

    /// <summary>PDF de um relatório técnico.</summary>
    [HttpGet("relatorios-tecnicos/{id:guid}/pdf")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)]
    public async Task<IActionResult> RelatorioTecnicoPdf(
        Guid id, [FromServices] IRelatorioTecnicoService relatorios, CancellationToken ct = default)
    {
        // GetByProducao/GetByObra retornam listas; aqui buscamos o RT específico pela obra dele.
        var rt = await relatorios.GetByIdAsync(id, ct);
        var bytes = _pdf.RelatorioTecnico(rt);
        return File(bytes, "application/pdf", $"relatorio-tecnico-{rt.Numero}.pdf");
    }
}