Files
cbsnexora/src/app/current-accounts/current-account-view/current-account-view.component.ts
T

526 lines
18 KiB
TypeScript

/**
* 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 { Component, OnInit, inject } from '@angular/core';
import { ActivatedRoute, Router, RouterLinkActive, RouterLink, RouterOutlet } from '@angular/router';
import { MatDialog } from '@angular/material/dialog';
/** Custom Dialogs */
import { DeleteDialogComponent } from 'app/shared/delete-dialog/delete-dialog.component';
import { CalculateInterestDialogComponent } from './custom-dialogs/calculate-interest-dialog/calculate-interest-dialog.component';
import { PostInterestDialogComponent } from './custom-dialogs/post-interest-dialog/post-interest-dialog.component';
import { ToggleWithholdTaxDialogComponent } from './custom-dialogs/toggle-withhold-tax-dialog/toggle-withhold-tax-dialog.component';
/** Custom Buttons Configuration */
import { CurrentAccountButtonsConfiguration } from './current-accounts-buttons.config';
import { CurrentAccountsService } from '../current-accounts.service';
import { ClientsService } from 'app/clients/clients.service';
import { ConfirmationDialogComponent } from 'app/shared/confirmation-dialog/confirmation-dialog.component';
import { Currency } from 'app/shared/models/general.model';
import { TranslateService } from '@ngx-translate/core';
import { AccountsService } from 'app/accounts/accounts.service';
import { SettingsService } from 'app/settings/settings.service';
import { Dates } from 'app/core/utils/dates';
import { AuthenticationService } from '../../core/authentication/authentication.service';
/** Environment Configuration */
import { environment } from '../../../environments/environment';
import {
MatCard,
MatCardHeader,
MatCardTitleGroup,
MatCardMdImage,
MatCardTitle,
MatCardContent
} from '@angular/material/card';
import { MatTooltip } from '@angular/material/tooltip';
import { NgClass, CurrencyPipe } from '@angular/common';
import { LongTextComponent } from '../../shared/long-text/long-text.component';
import { AccountNumberComponent } from '../../shared/account-number/account-number.component';
import { MatIconButton } from '@angular/material/button';
import { MatMenuTrigger, MatMenu, MatMenuItem } from '@angular/material/menu';
import { FaIconComponent } from '@fortawesome/angular-fontawesome';
import { MatTabNav, MatTabLink, MatTabNavPanel } from '@angular/material/tabs';
import { StatusLookupPipe } from '../../pipes/status-lookup.pipe';
import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module';
import { DormancyAlertPanelComponent } from 'app/shared/dormancy-alert-panel/dormancy-alert-panel.component';
/**
* Current Account View Component
*/
@Component({
selector: 'mifosx-current-account-account-view',
templateUrl: './current-account-view.component.html',
styleUrls: ['./current-account-view.component.scss'],
imports: [
...STANDALONE_SHARED_IMPORTS,
MatCardHeader,
MatCardTitleGroup,
MatCardMdImage,
MatTooltip,
MatCardTitle,
NgClass,
LongTextComponent,
AccountNumberComponent,
MatIconButton,
MatMenuTrigger,
FaIconComponent,
MatMenu,
MatMenuItem,
MatTabNav,
MatTabLink,
RouterLinkActive,
MatTabNavPanel,
RouterOutlet,
CurrencyPipe,
StatusLookupPipe,
DormancyAlertPanelComponent
]
})
export class CurrentAccountViewComponent implements OnInit {
private route = inject(ActivatedRoute);
private router = inject(Router);
private currentAccountsService = inject(CurrentAccountsService);
private clientsService = inject(ClientsService);
private translateService = inject(TranslateService);
private accountsService = inject(AccountsService);
private settingsService = inject(SettingsService);
private dateUtils = inject(Dates);
private authenticationService = inject(AuthenticationService);
dialog = inject(MatDialog);
/** Current Account Data */
currentAccountData: any;
/** Current Account Data Tables */
currentAccountDatatables: any;
/** Button Configurations */
buttonConfig: CurrentAccountButtonsConfiguration;
/** Entity Type */
entityType: string;
isActive = false;
isNonIndividualClient = false;
currency: Currency;
private readonly ACTION_KIND: Record<string, 'transact' | 'block' | 'interest' | 'admin' | 'danger'> = {
Deposit: 'transact',
Withdrawal: 'transact',
'Hold Amount': 'transact',
'Transfer Funds': 'transact',
'Interbank Transfer': 'transact',
'Block Deposit': 'block',
'Unblock Deposit': 'block',
'Block Withdrawal': 'block',
'Unblock Withdrawal': 'block',
'Block Account': 'block',
'Unblock Account': 'block',
'Calculate Interest': 'interest',
'Post Interest': 'interest',
'Post Interest As On': 'interest',
'Enable Withhold Tax': 'interest',
'Disable Withhold Tax': 'interest',
'Add Charge': 'admin',
'Apply Annual Fees': 'admin',
'Modify Application': 'admin',
'Edit Nominee & Features': 'admin',
'Assign Staff': 'admin',
'Unassign Staff': 'admin',
Approve: 'admin',
Activate: 'admin',
'Undo Approval': 'admin',
Close: 'danger',
Reject: 'danger',
Delete: 'danger',
'Withdrawn by Client': 'danger',
Terminate: 'danger'
};
private readonly ACTION_ICON: Record<string, string> = {
Deposit: 'arrow-down',
Withdrawal: 'arrow-up',
'Hold Amount': 'pause',
'Transfer Funds': 'exchange-alt',
'Interbank Transfer': 'random',
'Block Deposit': 'lock',
'Unblock Deposit': 'lock-open',
'Block Withdrawal': 'lock',
'Unblock Withdrawal': 'lock-open',
'Block Account': 'lock',
'Unblock Account': 'lock-open',
'Calculate Interest': 'calculator',
'Post Interest': 'percentage',
'Post Interest As On': 'calendar-day',
'Enable Withhold Tax': 'percentage',
'Disable Withhold Tax': 'percentage',
'Add Charge': 'plus-circle',
'Apply Annual Fees': 'receipt',
'Modify Application': 'edit',
'Edit Nominee & Features': 'edit',
'Assign Staff': 'user-plus',
'Unassign Staff': 'user-slash',
Approve: 'check-circle',
Activate: 'check',
'Undo Approval': 'undo',
Close: 'times-circle',
Reject: 'ban',
Delete: 'trash',
'Withdrawn by Client': 'user-slash',
Terminate: 'times-circle'
};
kindOf(name: string): 'transact' | 'block' | 'interest' | 'admin' | 'danger' {
return this.ACTION_KIND[name] ?? 'admin';
}
iconOf(name: string, fallback?: string): string {
return this.ACTION_ICON[name] ?? fallback ?? 'circle';
}
hasKind(kind: 'transact' | 'block' | 'interest' | 'admin' | 'danger'): boolean {
const all = [
...(this.buttonConfig?.singleButtons ?? []),
...(this.buttonConfig?.options ?? [])
];
return all.some((item) => this.kindOf(item.name) === kind);
}
/**
* Fetches current account data from `resolve`
* @param {ActivatedRoute} route Activated Route
* @param {Router} router Router
* @param {CurrentAccountsService} currentAccountsService Current Accounts Service
*/
constructor() {
this.route.data.subscribe((data: { currentAccountData: any; currentAccountDatatables: any }) => {
this.currentAccountData = data.currentAccountData;
this.currency = this.currentAccountData.currency;
this.currentAccountDatatables = data.currentAccountDatatables;
});
if (this.router.url.includes('clients')) {
this.entityType = 'Client';
} else if (this.router.url.includes('groups')) {
this.entityType = 'Group';
} else if (this.router.url.includes('centers')) {
this.entityType = 'Center';
}
}
ngOnInit() {
this.setConditionalButtons();
if (this.currentAccountData.clientId) {
this.clientsService.getClientData(String(this.currentAccountData.clientId)).subscribe((client: any) => {
this.isNonIndividualClient = client?.legalForm?.id === 2;
});
}
}
/**
* Adds options to button config. conditionaly.
*/
setConditionalButtons() {
const status = this.currentAccountData.status.value;
this.isActive = status === 'Active' || status === 'active';
const subStatus = this.currentAccountData.subStatus;
this.buttonConfig = new CurrentAccountButtonsConfiguration(status, subStatus);
if (this.currentAccountData.clientId) {
this.buttonConfig.addOption({
name: 'Transfer Funds',
taskPermissionName: 'CREATE_ACCOUNTTRANSFER'
});
}
if (this.currentAccountData.externalId && environment.mifosInterbankTransfersEnabled) {
this.buttonConfig.addOption({
name: 'Interbank Transfer',
taskPermissionName: 'CREATE_ACCOUNTTRANSFER'
});
}
if (!this.currentAccountData.fieldOfficerId) {
this.buttonConfig.addOption({
name: 'Assign Staff',
taskPermissionName: 'UPDATESAVINGSOFFICER_CURRENTACCOUNT'
});
} else {
this.buttonConfig.addOption({
name: 'Unassign Staff',
taskPermissionName: 'REMOVESAVINGSOFFICER_CURRENTACCOUNT'
});
}
if (this.currentAccountData.charges) {
const charges: any[] = this.currentAccountData.charges;
charges.forEach((charge: any) => {
if (charge.name === 'Annual fee - INR') {
this.buttonConfig.addOption({
name: 'Apply Annual Fees',
taskPermissionName: 'APPLYANNUALFEE_CURRENTACCOUNT'
});
}
});
}
if (this.currentAccountData.taxGroup) {
if (this.currentAccountData.withHoldTax) {
this.buttonConfig.addOption({
name: 'Disable Withhold Tax',
taskPermissionName: 'UPDATEWITHHOLDTAX_CURRENTACCOUNT'
});
} else {
this.buttonConfig.addOption({
name: 'Enable Withhold Tax',
taskPermissionName: 'UPDATEWITHHOLDTAX_CURRENTACCOUNT'
});
}
}
if (!this.authenticationService.hasCashierRole()) {
this.buttonConfig.buttonsArray = this.buttonConfig.buttonsArray.filter(
(btn) => btn.name !== 'Deposit' && btn.name !== 'Withdrawal'
);
this.buttonConfig.optionArray = this.buttonConfig.optionArray.filter(
(opt) => opt.name !== 'Deposit' && opt.name !== 'Withdrawal'
);
}
}
/**
* Refetches data foe the component
* TODO: Replace by a custom reload component instead of hard-coded back-routing.
*/
private reload() {
const url: string = this.router.url;
const refreshUrl: string = this.router.url.slice(
0,
this.router.url.indexOf('current-accounts') + 'current-accounts'.length
);
this.router.navigateByUrl(refreshUrl, { skipLocationChange: true }).then(() => this.router.navigate([url]));
}
/**
* Performs action button/option action.
* @param {string} name action name.
*/
doAction(name: string) {
switch (name) {
case 'Approve':
case 'Reject':
case 'Deposit':
case 'Activate':
case 'Close':
case 'Undo Approval':
case 'Post Interest As On':
case 'Assign Staff':
case 'Add Charge':
case 'Hold Amount':
case 'Block Account':
case 'Block Deposit':
case 'Block Withdrawal':
case 'Unassign Staff':
case 'Withdrawn by Client':
case 'Apply Annual Fees':
this.router.navigate([`actions/${name}`], { relativeTo: this.route });
break;
case 'Withdrawal':
this.router.navigate([`actions/Withdrawal`], { relativeTo: this.route });
break;
case 'Modify Application':
this.router.navigate(['edit'], { relativeTo: this.route });
break;
case 'Edit Nominee & Features':
this.router.navigate(['edit-nominee-features'], { relativeTo: this.route });
break;
case 'Delete':
this.deleteCurrentAccount();
break;
case 'Terminate':
this.terminateCurrentAccount();
break;
case 'Calculate Interest':
this.calculateInterest();
break;
case 'Post Interest':
this.postInterest();
break;
case 'Enable Withhold Tax':
this.enableWithHoldTax();
break;
case 'Disable Withhold Tax':
this.disableWithHoldTax();
break;
case 'Transfer Funds':
const queryParams: any = {
interbank: false,
currentAccountId: this.currentAccountData.id,
accountType: 'fromcurrentaccount'
};
this.router.navigate(['transfer-funds/make-account-transfer'], {
relativeTo: this.route,
queryParams: queryParams,
state: { balance: this.currentAccountData.summary.availableBalance }
});
break;
case 'Interbank Transfer': {
const queryParams: any = {
interbank: true,
currentAccountId: this.currentAccountData.id,
accountType: 'interbank'
};
this.router.navigate(['transfer-funds/make-account-transfer'], {
relativeTo: this.route,
queryParams: queryParams,
state: { balance: this.currentAccountData.summary.availableBalance }
});
break;
}
case 'Unblock Account':
case 'Unblock Deposit':
case 'Unblock Withdrawal':
this.unblockCurrentAccount(name);
break;
}
}
/**
* Deletes Current Account.
*/
private deleteCurrentAccount() {
const deleteCurrentAccountDialogRef = this.dialog.open(DeleteDialogComponent, {
data: { deleteContext: `current account with id: ${this.currentAccountData.id}` }
});
deleteCurrentAccountDialogRef.afterClosed().subscribe((response: any) => {
if (response.delete) {
this.currentAccountsService.deleteCurrentAccount(this.currentAccountData.id).subscribe(() => {
this.router.navigate(['../../'], { relativeTo: this.route });
});
}
});
}
private terminateCurrentAccount() {
const terminateCurrentAccountDialogRef = this.dialog.open(ConfirmationDialogComponent, {
data: {
heading: 'Terminate Account',
dialogContext: 'Are you sure you want to terminate this account?',
type: 'Strong'
}
});
terminateCurrentAccountDialogRef.afterClosed().subscribe((response: any) => {
if (response && response.confirm) {
const locale = this.settingsService.language.code;
const dateFormat = this.settingsService.dateFormat;
const terminatedOnDate = this.dateUtils.formatDate(new Date(), dateFormat);
const data = {
accountType: 'CURRENT',
terminatedOnDate,
dateFormat,
locale
};
this.accountsService.terminateAccount(this.currentAccountData.id, data).subscribe(() => {
this.reload();
});
}
});
}
/**
* Calculates current account's interest
*/
private calculateInterest() {
const calculateInterestAccountDialogRef = this.dialog.open(CalculateInterestDialogComponent);
calculateInterestAccountDialogRef.afterClosed().subscribe((response: any) => {
if (response.confirm) {
this.currentAccountsService
.executeCurrentAccountCommand(this.currentAccountData.id, 'calculateInterest', {})
.subscribe(() => {
this.reload();
});
}
});
}
/**
* Posts current account's interest
*/
private postInterest() {
const postInterestAccountDialogRef = this.dialog.open(PostInterestDialogComponent);
postInterestAccountDialogRef.afterClosed().subscribe((response: any) => {
if (response.confirm) {
this.currentAccountsService
.executeCurrentAccountCommand(this.currentAccountData.id, 'postInterest', {})
.subscribe(() => {
this.reload();
});
}
});
}
/**
* Enables withhold tax for current account.
*/
private enableWithHoldTax() {
const deleteCurrentAccountDialogRef = this.dialog.open(ToggleWithholdTaxDialogComponent, {
data: { isEnable: true }
});
deleteCurrentAccountDialogRef.afterClosed().subscribe((response: any) => {
if (response.confirm) {
this.currentAccountsService
.executeCurrentAccountUpdateCommand(this.currentAccountData.id, 'updateWithHoldTax', { withHoldTax: true })
.subscribe(() => {
this.reload();
});
}
});
}
/**
* Disables withhold tac for current account
*/
private disableWithHoldTax() {
const disableWithHoldTaxDialogRef = this.dialog.open(ToggleWithholdTaxDialogComponent, {
data: { isEnable: false }
});
disableWithHoldTaxDialogRef.afterClosed().subscribe((response: any) => {
if (response.confirm) {
this.currentAccountsService
.executeCurrentAccountUpdateCommand(this.currentAccountData.id, 'updateWithHoldTax', { withHoldTax: false })
.subscribe(() => {
this.reload();
});
}
});
}
/**
* Unblock Current Account.
*/
private unblockCurrentAccount(action: string) {
const unblockCurrentAccountDialogRef = this.dialog.open(ConfirmationDialogComponent, {
data: {
heading: this.translateService.instant('labels.heading.Current Account'),
dialogContext:
this.translateService.instant('labels.dialogContext.Are you sure you want') +
action +
this.translateService.instant('this Current Account')
}
});
let command = 'unblock';
if (action === 'Unblock Deposit') {
command = 'unblockCredit';
}
if (action === 'Unblock Withdrawal') {
command = 'unblockDebit';
}
unblockCurrentAccountDialogRef.afterClosed().subscribe((response: { confirm: any }) => {
if (response.confirm) {
this.currentAccountsService
.executeCurrentAccountCommand(this.currentAccountData.id, command, {})
.subscribe(() => {
this.reload();
});
}
});
}
}