import { Component, OnDestroy, OnInit } from '@angular/core'; import { SpinnerService } from '@core/services/spinner.service'; import { StatusService } from '@core/services/status.service'; import { ProgramService } from '@features/programs/program.service'; import { BetweenFilterOption, FilterHelpersService, FilterModalTypes, Last30DaysOption, Last365DaysOption, LastMonthOption, LastWeekOption, LastYearOption, TableDataDownloadFormat, ThisMonthOption, ThisWeekOption, ThisYearOption, TopLevelFilter, TypeaheadSelectOption, TypeSafeFormBuilder } from '@yourcause/common'; import { I18nService } from '@yourcause/common/i18n'; import moment from 'moment'; import { PaymentProcessingService } from '../payment-processing.service'; @Component({ templateUrl: './disbursement-report-page.component.html', styleUrls: ['./disbursement-report-page.component.scss'] }) export class DisbursementReportPageComponent implements OnInit, OnDestroy { SummaryFactory = this.paymentProcessingService.getDisbursementSummaryFactory(); ReportFactory = this.paymentProcessingService.getDisbursementReportFactory(); dateFilterOptions: TypeaheadSelectOption[] = [ LastWeekOption, ThisWeekOption, LastMonthOption, ThisMonthOption, LastYearOption, ThisYearOption, Last30DaysOption, Last365DaysOption, BetweenFilterOption ].map(option => { return { label: this.i18n.translate(option.displayKey, {}, option.display), value: option.api }; }); topLevelFilters: TopLevelFilter[] = [new TopLevelFilter( 'text', '$search', '', this.i18n.translate( 'MANAGE:textSearchByCheckNumberPaymentNumberOrRecipient', {}, 'Search by check number, payment number or recipient' ) )]; statusMap = this.statusService.paymentStatusMap; formGroup = this.formBuilder.group({ relativeOption: [FilterModalTypes.Last30Days], dateRange: [ this.filterHelpersService.getClientSideRelativeDates( FilterModalTypes.Last30Days ) ], programIds: [[] as number[]] }); // we show all published programs here as options since it is not a data-segmented area programOptions = this.programService.allPublishedPrograms .map(program => { return { label: program.grantProgramName, value: program.grantProgramId }; }); dateRangeEnabled = false; TableDataDownloadFormat = TableDataDownloadFormat; constructor ( private programService: ProgramService, private i18n: I18nService, private paymentProcessingService: PaymentProcessingService, private statusService: StatusService, private formBuilder: TypeSafeFormBuilder, private filterHelpersService: FilterHelpersService, private spinner: SpinnerService ) { } ngOnInit () { this.triggerFactories(); } async triggerFactories () { if (this.formGroup.valid) { this.spinner.startSpinner(); let startDate: moment.Moment; let endDate: moment.Moment; if (this.formGroup.value.relativeOption === FilterModalTypes.between) { startDate = this.formGroup.value.dateRange[0]; endDate = this.formGroup.value.dateRange[1]; } else { const result = this.filterHelpersService.getClientSideRelativeDates( this.formGroup.value.relativeOption ); startDate = result[0]; endDate = result[1]; } const payload = { startDate, endDate, programIds: this.formGroup.value.programIds }; await Promise.all([ this.SummaryFactory.invoke(() => payload), this.ReportFactory.invoke(() => payload) ]); this.spinner.stopSpinner(); } } download (format: TableDataDownloadFormat) { this.paymentProcessingService.downloadDisbursementReport( this.ReportFactory.currentResult, this.SummaryFactory.currentResult, format ); } handleRelativeOptionChange ( option: FilterModalTypes ) { this.dateRangeEnabled = option === FilterModalTypes.between; const filterType = this.dateRangeEnabled ? FilterModalTypes.Last30Days : option; this.formGroup.get('dateRange').setValue( this.filterHelpersService.getClientSideRelativeDates( filterType ) ); } ngOnDestroy () { this.ReportFactory.complete(); this.SummaryFactory.complete(); } }