using AccountsBLL.AccountReports.AccountTrialBalance; using FastEndpoints; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.Framework.ResponseStandard; using GB5Shared.DTO.Report; using GB5Shared.FastEndPoint; using static GB5Shared.GB5Constant.Constant; namespace AccountsSL.EndPoints.AccountReports { // Periodic Trial Balance, pivoted server-side — one row per Account (Type/Schedule/Group/Code/ // Name as plain scalar fields) with a dynamic column per period bucket (PeriodLabel), so the FE // can feed the response straight into gbslickgrid's own native grouping (same Type->Schedule-> // Group grouping/expand-collapse as the Hierarchical view) with dynamically-built period columns, // rather than the flat, non-collapsible rendering the generic ReportViewType=5 pivot path uses. // Returns GB5Shared.Export.Pivot.PivotResultDTO as a single object, not a paged row stream — // pivoting needs the complete dataset up front, so this is a plain BaseEndpoint, not a // BaseReportEndpoint. Excel/PDF export is available via the standard H-ReportFormat header // (BaseEndpoint's own built-in mechanism — see the ExecuteAsync override below for why this // endpoint branches its own Body shape around it, rather than a BaseReportEndpoint override). public class AccountTrialBalancePeriodicPivotReport : BaseEndpoint> { private readonly IAccountTrialBalanceReportBLL _bll; public AccountTrialBalancePeriodicPivotReport(IAccountTrialBalanceReportBLL bll) { _bll = bll; } public override void Configure() { Post("/AccountReports/AccountTrialBalancePeriodicPivotReport"); AllowAnonymous(); } public record AccountTrialBalancePeriodicPivotReportParameter( [property: FromHeader] string Login, [property: FromBody] ReportCallingDTO ReportCallingDTO ); // Dedicated MREPORTVIEW for this report, seeded by DB/Migrations/ // 20260829_AccountTrialBalance_PeriodicPivot_ReportView_Seed_SqlServer.sql — same constant // as AccountTrialBalanceReportBLL.PERIODIC_PIVOT_REPORT_VIEW_ID (private to that assembly, // so duplicated here rather than shared across the BLL/SL boundary). private const int PERIODIC_PIVOT_REPORT_VIEW_ID = 1000000805; protected override async Task> ExecuteAsync( AccountTrialBalancePeriodicPivotReportParameter req, LoginDTO login, CancellationToken ct) { try { // BaseEndpoint's own H-ReportFormat handling (see GB5Shared/FastEndPoint/ // BaseEndpoint.cs) fires AFTER this method returns, feeding freshResponse.Body through // IReportExport.ExportAsync — which for a Pivot-type ReportView re-derives the pivot // itself from FLAT rows (GetPivotFieldConfig + IDataPivotEngine), not from an // already-pivoted result. So when export is requested, Body must be the flat rows, // not the PivotResultDTO the normal grid path returns. if (HttpContext.Request.Headers.ContainsKey("ReportFormat")) { req.ReportCallingDTO.ReportViewId = PERIODIC_PIVOT_REPORT_VIEW_ID; var flatRows = await _bll.GetTrialBalancePeriodicPivotFlatRows( req.ReportCallingDTO.CriteriaDTO, req.ReportCallingDTO.MenuId, login, ct).ConfigureAwait(false); return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse( flatRows, CacheKeyLevel.NOT_REQUIRED, login).ConfigureAwait(false); } var result = await _bll.GetTrialBalancePeriodicPivotReport( req.ReportCallingDTO.CriteriaDTO, req.ReportCallingDTO.MenuId, login, ct).ConfigureAwait(false); return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse( result, CacheKeyLevel.NOT_REQUIRED, login).ConfigureAwait(false); } catch (Exception ex) { return await GB5Shared.ResponseStandard.Response.CreateExceptionError( ex, CacheKeyLevel.NOT_REQUIRED, login, ex.Message, 500).ConfigureAwait(false); } } } }