/** * 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, ViewChild, inject } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; /** Custom Services */ import { FixedDepositsService } from '../fixed-deposits.service'; import { SettingsService } from 'app/settings/settings.service'; import { AlertService } from 'app/core/alert/alert.service'; import { ProductsService } from 'app/products/products.service'; /** Custom Components */ import { FixedDepositAccountDetailsStepComponent } from '../fixed-deposit-account-stepper/fixed-deposit-account-details-step/fixed-deposit-account-details-step.component'; import { FixedDepositAccountCurrencyStepComponent } from '../fixed-deposit-account-stepper/fixed-deposit-account-currency-step/fixed-deposit-account-currency-step.component'; import { Dates } from 'app/core/utils/dates'; import { MatTabsModule } from '@angular/material/tabs'; import { FaIconComponent } from '@fortawesome/angular-fontawesome'; import { FixedDepositAccountInterestRateChartStepComponent } from '../fixed-deposit-account-stepper/fixed-deposit-account-interest-rate-chart-step/fixed-deposit-account-interest-rate-chart-step.component'; import { FixedDepositAccountPreviewStepComponent } from '../fixed-deposit-account-stepper/fixed-deposit-account-preview-step/fixed-deposit-account-preview-step.component'; import { FixedDepositAccountMaturityTermsStepComponent } from '../fixed-deposit-account-stepper/fixed-deposit-account-maturity-terms-step/fixed-deposit-account-maturity-terms-step.component'; import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module'; /** * Create Fixed Deposit Account Component */ @Component({ selector: 'mifosx-create-fixed-deposit-account', templateUrl: './create-fixed-deposit-account.component.html', styleUrls: ['./create-fixed-deposit-account.component.scss'], imports: [ ...STANDALONE_SHARED_IMPORTS, MatTabsModule, FaIconComponent, FixedDepositAccountDetailsStepComponent, FixedDepositAccountInterestRateChartStepComponent, FixedDepositAccountCurrencyStepComponent, FixedDepositAccountMaturityTermsStepComponent, FixedDepositAccountPreviewStepComponent ] }) export class CreateFixedDepositAccountComponent { private route = inject(ActivatedRoute); private router = inject(Router); private dateUtils = inject(Dates); private fixedDepositsService = inject(FixedDepositsService); private settingsService = inject(SettingsService); private alertService = inject(AlertService); private productsService = inject(ProductsService); @ViewChild(FixedDepositAccountDetailsStepComponent, { static: true }) fixedDepositsAccountDetailsStep: FixedDepositAccountDetailsStepComponent; @ViewChild(FixedDepositAccountCurrencyStepComponent) fixedDepositAccountCurrencyStep: FixedDepositAccountCurrencyStepComponent; @ViewChild(FixedDepositAccountPreviewStepComponent) fixedDepositAccountPreviewStep: FixedDepositAccountPreviewStepComponent; @ViewChild(FixedDepositAccountMaturityTermsStepComponent) fixedDepositAccountMaturityTermsStep: FixedDepositAccountMaturityTermsStepComponent; /** Fixed Deposits Account Template */ fixedDepositsAccountTemplate: any; /** Fixed Deposit Account Product Template */ fixedDepositsAccountProductTemplate: any; /** Currently visible wizard tab (0-indexed). */ selectedTabIndex = 0; /** Total wizard tabs — Product, Currency, Chart, Terms, Preview. */ private readonly totalTabs = 5; /** Active CBS base interest rate versions for the selected product's FD type */ cbsBaseRates: any[] = []; /** Tenure selected from the interest rate chart step */ selectedTenure: { depositPeriod: number; depositPeriodFrequencyId: number; depositAmount?: number; maturityAmount?: number; interestEarned?: number; appliedRate?: number; isSeniorCitizen?: boolean; } | null = null; /** * Fetches FD account template from `resolve` * @param {ActivatedRoute} route Activated Route * @param {Router} router Router * @param {Dates} dateUtils Date Utils * @param {FixedDepositsService} fixedDepositsService Fixed Deposits Service * @param {SettingsService} settingsService Settings Service */ constructor() { this.route.data.subscribe((data: { fixedDepositsAccountTemplate: any }) => { this.fixedDepositsAccountTemplate = data.fixedDepositsAccountTemplate; }); } next(): void { if (this.selectedTabIndex < this.totalTabs - 1) { this.selectedTabIndex += 1; } } prev(): void { if (this.selectedTabIndex > 0) { this.selectedTabIndex -= 1; } } /** * Sets fixed deposits account product template. * @param {any} $event API response */ setTemplate($event: any) { this.fixedDepositsAccountProductTemplate = $event; const fdType = $event?.fdType; this.productsService.getFdBaseInterestRates(fdType).subscribe({ next: (rates: any[]) => (this.cbsBaseRates = rates || []), error: () => (this.cbsBaseRates = []) }); } onTenureSelected($event: { depositPeriod: number; depositPeriodFrequencyId: number; depositAmount?: number; maturityAmount?: number; interestEarned?: number; appliedRate?: number; isSeniorCitizen?: boolean; }) { this.selectedTenure = $event; } /** * Retrieves Fixed Deposit Account Details Form Data */ get fixedDepositAccountDetailsForm() { return this.fixedDepositsAccountDetailsStep?.fixedDepositAccountDetailsForm; } /** * Checks stepper validity. */ get fixedDepositAccountFormValid() { return !!this.fixedDepositAccountDetailsForm?.valid; } /** * Creates the fixed deposit account object. */ get fixedDepositAccount() { return { ...this.fixedDepositsAccountDetailsStep?.fixedDepositAccountDetails, ...this.fixedDepositAccountCurrencyStep?.currencyStepData }; } get submittedOnDate(): Date | null { const val = this.fixedDepositsAccountDetailsStep?.fixedDepositAccountDetails?.submittedOnDate; return val ? new Date(val) : null; } /** * Submits the fixed deposit form and creates a new fixed deposit account */ submit() { const detailsStep = this.fixedDepositsAccountDetailsStep; if (detailsStep?.selectedAccountStatus === 'JOINT' && detailsStep.totalJointSharePercentage !== 100) { this.alertService.alert({ type: 'Error', message: `Total ownership is ${detailsStep.totalJointSharePercentage}%. It must equal 100% before submitting.` }); return; } if ( this.fixedDepositsAccountProductTemplate?.nominationFacility && (this.fixedDepositAccountCurrencyStep?.nominees ?? []).length === 0 ) { this.alertService.alert({ type: 'Error', message: 'Nomination Facility is enabled for this product. Please add at least one nominee before submitting.' }); return; } if (!this.selectedTenure || !this.selectedTenure.depositPeriod) { this.alertService.alert({ type: 'Error', message: 'Please enter the Deposit Tenure and click "Find Rate" before submitting.' }); return; } if (!detailsStep?.selectedCustomerSegment) { this.alertService.alert({ type: 'Error', message: 'Please select a Customer Segment before submitting.' }); return; } const locale = this.settingsService.language.code; const dateFormat = this.settingsService.dateFormat; const monthDayFormat = 'dd MMMM'; const pt = this.fixedDepositsAccountProductTemplate; // Terms — pulled from product template const terms: any = {}; if (pt?.nominalAnnualInterestRate != null) terms.nominalAnnualInterestRate = pt.nominalAnnualInterestRate; if (pt?.interestCompoundingPeriodType?.id != null) terms.interestCompoundingPeriodType = pt.interestCompoundingPeriodType.id; if (pt?.interestPostingPeriodType?.id != null) terms.interestPostingPeriodType = pt.interestPostingPeriodType.id; if (pt?.interestCalculationType?.id != null) terms.interestCalculationType = pt.interestCalculationType.id; if (pt?.interestCalculationDaysInYearType?.id != null) terms.interestCalculationDaysInYearType = pt.interestCalculationDaysInYearType.id; // Deposit amount: Prefer user input from Rate Calculator, fallback to product template if (this.selectedTenure?.depositAmount != null) { terms.depositAmount = this.selectedTenure.depositAmount; } else if (pt?.depositAmount != null) { terms.depositAmount = pt.depositAmount; } if (pt?.minDepositAmount != null) terms.minDepositAmount = pt.minDepositAmount; if (pt?.maxDepositAmount != null) terms.maxDepositAmount = pt.maxDepositAmount; // Settings — pulled from product template const settings: any = {}; if (pt?.lockinPeriodFrequency != null) settings.lockinPeriodFrequency = pt.lockinPeriodFrequency; if (pt?.lockinPeriodFrequencyType?.id != null) settings.lockinPeriodFrequencyType = pt.lockinPeriodFrequencyType.id; if (pt?.minDepositTerm != null) settings.minDepositTerm = pt.minDepositTerm; if (pt?.minDepositTermType?.id != null) settings.minDepositTermTypeId = pt.minDepositTermType.id; if (pt?.inMultiplesOfDepositTerm != null) settings.inMultiplesOfDepositTerm = pt.inMultiplesOfDepositTerm; if (pt?.inMultiplesOfDepositTermType?.id != null) settings.inMultiplesOfDepositTermTypeId = pt.inMultiplesOfDepositTermType.id; if (pt?.maxDepositTerm != null) settings.maxDepositTerm = pt.maxDepositTerm; if (pt?.maxDepositTermType?.id != null) settings.maxDepositTermTypeId = pt.maxDepositTermType.id; if (pt?.preClosurePenalApplicable != null) settings.preClosurePenalApplicable = pt.preClosurePenalApplicable; if (pt?.preClosurePenalInterest != null) settings.preClosurePenalInterest = pt.preClosurePenalInterest; if (pt?.preClosurePenalInterestOnType?.id != null) settings.preClosurePenalInterestOnTypeId = pt.preClosurePenalInterestOnType.id; if (pt?.maturityInstructionId != null) settings.maturityInstructionId = pt.maturityInstructionId; if (pt?.transferInterestToSavings != null) settings.transferInterestToSavings = pt.transferInterestToSavings; if (pt?.withHoldTax != null) settings.withHoldTax = pt.withHoldTax; // Charges — from product template if not overridden in details const rawCharges: any[] = (this.fixedDepositAccount.charges?.length ? this.fixedDepositAccount.charges : pt?.charges) ?? []; const fixedDepositAccount = { ...this.fixedDepositAccount, ...terms, ...settings, ...(this.fixedDepositAccountMaturityTermsStep?.closureSettings ?? {}), ...(this.selectedTenure ? { depositPeriod: this.selectedTenure.depositPeriod, depositPeriodFrequencyId: this.selectedTenure.depositPeriodFrequencyId } : {}), clientId: this.fixedDepositsAccountTemplate.clientId, charges: rawCharges.map((charge: any) => ({ chargeId: charge.chargeId ?? charge.id, amount: charge.amount, dueDate: charge.dueDate && this.dateUtils.formatDate(charge.dueDate, dateFormat), feeOnMonthDay: charge.feeOnMonthDay && this.dateUtils.formatDate([2000].concat(charge.feeOnMonthDay), monthDayFormat), feeInterval: charge.feeInterval })), submittedOnDate: this.dateUtils.formatDate(this.fixedDepositAccount.submittedOnDate, dateFormat), charts: [{ chartSlabs: pt?.accountChart?.chartSlabs ?? [] }], dateFormat, monthDayFormat, locale }; const nominees = this.fixedDepositAccountCurrencyStep?.nominees ?? []; if (nominees.length > 0) { fixedDepositAccount.nominees = nominees.map((n: any) => ({ nomineeClientId: n.nomineeClientId, nominationType: n.nominationType, relationshipId: n.relationshipId, percentageShare: n.percentageShare, nomineeIsMinor: n.nomineeIsMinor, ...(n.guardianClientId ? { guardianClientId: n.guardianClientId } : {}), remarks: n.remarks })); } // Senior citizen rate override if ( this.selectedTenure?.isSeniorCitizen && this.selectedTenure.appliedRate != null && this.selectedTenure.appliedRate > 0 ) { fixedDepositAccount.isSeniorCitizen = true; fixedDepositAccount.seniorCitizenInterestRate = this.selectedTenure.appliedRate; fixedDepositAccount.nominalAnnualInterestRate = this.selectedTenure.appliedRate; } this.fixedDepositsService.createFixedDepositAccount(fixedDepositAccount).subscribe({ next: (response: any) => { this.router.navigate( [ '../', response.resourceId ], { relativeTo: this.route } ); }, error: (error: any) => { const errorCode = error?.error?.errors?.[0]?.userMessageGlobalisationCode ?? error?.error?.userMessageGlobalisationCode; if (errorCode === 'error.msg.savings.account.duplicate.product.for.client') { this.alertService.alert({ type: 'Error', message: 'This client already has an active Fixed Deposit account with the selected product. Please choose a different product.' }); } else if (errorCode === 'error.msg.savingsaccount.duplicate.externalId') { this.alertService.alert({ type: 'Error', message: 'An account with this External ID already exists. Please use a unique External ID.' }); } } }); } }