/** * Copyright since 2025 Mifos Initiative * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ /** Angular Imports */ import { Injectable, inject } from '@angular/core'; import { HttpClient, HttpParams } from '@angular/common/http'; /** rxjs Imports */ import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; /** Custom Models */ import { ReportParameter } from './common-models/report-parameter.model'; import { SelectOption } from './common-models/select-option.model'; import { ChartData } from './common-models/chart-data.model'; /** * Reports service. */ @Injectable({ providedIn: 'root' }) export class ReportsService { private http = inject(HttpClient); /** * @returns {Observable} Reports data */ getReports(): Observable { return this.http.get('/reports'); } /** * @param {string} reportName Report name for which parameters are needed. * @returns {Observable} */ getReportParams(reportName: string): Observable { const httpParams = new HttpParams().set('R_reportListing', `'${reportName}'`).set('parameterType', 'true'); return this.http .get(`/runreports/FullParameterList`, { params: httpParams }) .pipe(map((response: any) => response.data.map((entry: any) => new ReportParameter(entry.row)))); } /** * @param {string} inputString URL substring containing object details. * @returns {Observable} */ getSelectOptions(inputString: string): Observable { const httpParams = new HttpParams().set('parameterType', 'true'); return this.http .get(`/runreports/${inputString}`, { params: httpParams }) .pipe(map((response: any) => response.data.map((entry: any) => new SelectOption(entry.row)))); } /** * @param {number} reportId Report id for which pentaho parameters are needed. * @returns {Observable} */ getPentahoParams(reportId: number): Observable { const httpParams = new HttpParams().set('fields', 'reportParameters'); return this.http .get(`/reports/${reportId}`, { params: httpParams }) .pipe(map((response: any) => response.reportParameters)); } /** * Run Report Data for Table and SMS. * @param {any} reportName report name * @param {object} formData Form Data. * @returns {Observable} */ getRunReportData(reportName: string, formData: object): Observable { let httpParams = new HttpParams(); for (const [ key, value ] of Object.entries(formData)) { httpParams = httpParams.set(key, value); } return this.http.get(`/runreports/${reportName}`, { params: httpParams }); } /** * Run Report Data for Charts. * @param {any} reportName report name * @param {object} formData Form Data. * @returns {Observable} */ getChartRunReportData(reportName: string, formData: object): Observable { let httpParams = new HttpParams(); for (const [ key, value ] of Object.entries(formData)) { httpParams = httpParams.set(key, value); } return this.http .get(`/runreports/${reportName}`, { params: httpParams }) .pipe(map((response: any) => new ChartData(response))); } /** * Run Report Data for Pentaho. * @param {any} reportName report name * @param {object} formData Form Data. * @returns {Observable} */ getPentahoRunReportData( reportName: string, formData: object, tenantIdentifier: string, locale: string, dateFormat: string ): Observable { let httpParams = new HttpParams() .set('tenantIdentifier', tenantIdentifier) .set('locale', locale) .set('dateFormat', dateFormat); for (const [ key, value ] of Object.entries(formData)) { httpParams = httpParams.set(key, value); } return this.http.get(`/runreports/${reportName}`, { responseType: 'arraybuffer', observe: 'response', params: httpParams }); } }