/** * 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/. */ import { Component, OnInit, inject } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; import { ActivatedRoute } from '@angular/router'; import { FaIconComponent } from '@fortawesome/angular-fontawesome'; import { of } from 'rxjs'; import { catchError } from 'rxjs/operators'; import { SharesService } from '../../shares.service'; import { SHARE_DIVIDEND_STATUS, shareReportCsv, shareReportPrint } from '../share-report.util'; /** * Dividend Statement — every dividend declared to one member, with what has been paid and what is * still owed. * *
* Handed to a member as their record, so declared and paid stay separate columns: "declared" and * "paid" are different answers to "have I had my dividend", and a statement showing one amount * cannot tell them apart. */ @Component({ selector: 'mifosx-share-dividend-statement', templateUrl: './share-dividend-statement.component.html', styleUrls: [ '../../../letter-mgmt/certificates/certificates.shared.scss', '../share-report.shared.scss' ], imports: [ CommonModule, FormsModule, FaIconComponent ] }) export class ShareDividendStatementComponent implements OnInit { private sharesService = inject(SharesService); private route = inject(ActivatedRoute); search = ''; searching = false; searched = false; loadError = ''; matches: any[] = []; member: any = null; rows: any[] = []; totals: any = null; ngOnInit(): void { const clientId = this.route.snapshot.queryParamMap.get('clientId'); if (clientId) { this.loadByClient(Number(clientId)); } } runSearch(): void { const term = this.search.trim(); if (!term) { return; } this.fetch({ search: term }); } loadByClient(clientId: number): void { this.fetch({ clientId }); } private fetch(params: { search?: string; clientId?: number }): void { this.searching = true; this.loadError = ''; this.reset(); this.sharesService .getShareDividendStatement(params) .pipe( catchError((err) => { this.loadError = this.readError(err, 'The dividend statement could not be loaded.'); return of(null); }) ) .subscribe((res: any) => { this.matches = res?.matches || []; this.member = res?.member || null; this.rows = res?.rows || []; this.totals = res?.totals || null; this.searching = false; this.searched = true; }); } private reset(): void { this.matches = []; this.member = null; this.rows = []; this.totals = null; } statusLabel(status: number): string { return SHARE_DIVIDEND_STATUS[status]?.label || String(status ?? ''); } statusClass(status: number): string { return SHARE_DIVIDEND_STATUS[status]?.cls || 'srep-pill--grey'; } exportCsv(): void { if (!this.member) { return; } shareReportCsv( `dividend-statement-${this.member.cifNumber || this.member.clientId}`, [ 'Period From', 'Period To', 'Share A/c No', 'Product', 'Rate %', 'Amount', 'Status' ], this.rows.map((r) => [ r.periodStart, r.periodEnd, r.accountNo, r.productName, r.ratePercentage, r.amount, this.statusLabel(r.statusEnum) ]) ); } print(): void { shareReportPrint(); } private readError(err: any, fallback: string): string { return ( err?.error?.errors?.[0]?.defaultUserMessage || err?.error?.defaultUserMessage || err?.error?.developerMessage || fallback ); } }