/** * 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 { ClientsService } from 'app/clients/clients.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'; /** * Edit Fixed Deposit Account Component — same tab UI as Create, pre-populated with existing data. */ @Component({ selector: 'mifosx-edit-fixed-deposit-account', templateUrl: './edit-fixed-deposit-account.component.html', styleUrls: ['./edit-fixed-deposit-account.component.scss'], imports: [ ...STANDALONE_SHARED_IMPORTS, MatTabsModule, FaIconComponent, FixedDepositAccountDetailsStepComponent, FixedDepositAccountInterestRateChartStepComponent, FixedDepositAccountCurrencyStepComponent, FixedDepositAccountMaturityTermsStepComponent, FixedDepositAccountPreviewStepComponent ] }) export class EditFixedDepositAccountComponent { 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); private clientsService = inject(ClientsService); @ViewChild(FixedDepositAccountDetailsStepComponent, { static: true }) fixedDepositsAccountDetailsStep: FixedDepositAccountDetailsStepComponent; @ViewChild(FixedDepositAccountCurrencyStepComponent) fixedDepositAccountCurrencyStep: FixedDepositAccountCurrencyStepComponent; @ViewChild(FixedDepositAccountPreviewStepComponent) fixedDepositAccountPreviewStep: FixedDepositAccountPreviewStepComponent; @ViewChild(FixedDepositAccountMaturityTermsStepComponent) fixedDepositAccountMaturityTermsStep: FixedDepositAccountMaturityTermsStepComponent; /** Full account data + template from resolver */ fixedDepositsAccountAndTemplate: any; /** Product template emitted by details step */ fixedDepositsAccountProductTemplate: any; /** Currently visible wizard tab (0-indexed). */ selectedTabIndex = 0; 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; constructor() { this.route.data.subscribe((data: { fixedDepositsAccountAndTemplate: any }) => { const account = data.fixedDepositsAccountAndTemplate; this.fixedDepositsAccountAndTemplate = account; // Pre-populate tenure from existing account if (account?.depositPeriod) { this.selectedTenure = { depositPeriod: account.depositPeriod, depositPeriodFrequencyId: account.depositPeriodFrequency?.id ?? 2, depositAmount: account.depositAmount, isSeniorCitizen: !!account.isSeniorCitizen, appliedRate: account.seniorCitizenInterestRate ?? undefined }; } if (account?.clientId && account?.accountNo) { this.clientsService .getClientNominees(String(account.clientId), account.accountNo, 'FD') .subscribe((nominees: any[]) => { if (Array.isArray(nominees) && nominees.length > 0) { this.fixedDepositsAccountAndTemplate = { ...this.fixedDepositsAccountAndTemplate, nominees }; } }); } }); } next(): void { if (this.selectedTabIndex < this.totalTabs - 1) { this.selectedTabIndex += 1; } } prev(): void { if (this.selectedTabIndex > 0) { this.selectedTabIndex -= 1; } } 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; } get fixedDepositAccountDetailsForm() { return this.fixedDepositsAccountDetailsStep?.fixedDepositAccountDetailsForm; } get fixedDepositAccount() { return { ...this.fixedDepositsAccountDetailsStep?.fixedDepositAccountDetails, ...this.fixedDepositAccountCurrencyStep?.currencyStepData }; } get submittedOnDate(): Date | null { const val = this.fixedDepositsAccountDetailsStep?.fixedDepositAccountDetails?.submittedOnDate ?? this.fixedDepositsAccountAndTemplate?.timeline?.submittedOnDate; if (!val) return null; const d = Array.isArray(val) ? new Date(val[0], val[1] - 1, val[2]) : new Date(val); return isNaN(d.getTime()) ? null : d; } 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; 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; 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; 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; const rawCharges: any[] = (this.fixedDepositAccount.charges?.length ? this.fixedDepositAccount.charges : pt?.charges) ?? []; const fixedDepositAccount: any = { ...this.fixedDepositAccount, ...terms, ...settings, ...(this.fixedDepositAccountMaturityTermsStep?.closureSettings ?? {}), ...(this.selectedTenure ? { depositPeriod: this.selectedTenure.depositPeriod, depositPeriodFrequencyId: this.selectedTenure.depositPeriodFrequencyId } : {}), clientId: this.fixedDepositsAccountAndTemplate.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 })); } 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 .updateFixedDepositAccount(this.fixedDepositsAccountAndTemplate.id, fixedDepositAccount) .subscribe((response: any) => { this.router.navigate(['../'], { relativeTo: this.route }); }); } }