/** * 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, Input, OnInit, inject } from '@angular/core'; import { UntypedFormGroup, UntypedFormBuilder, Validators } from '@angular/forms'; import { Dates } from 'app/core/utils/dates'; import { SettingsService } from 'app/settings/settings.service'; import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module'; @Component({ selector: 'mifosx-savings-account-term-step', templateUrl: './savings-account-term-step.component.html', styleUrls: ['./savings-account-term-step.component.scss'], imports: [...STANDALONE_SHARED_IMPORTS] }) export class SavingsAccountTermStepComponent implements OnInit { private formBuilder = inject(UntypedFormBuilder); private dateUtils = inject(Dates); private settingsService = inject(SettingsService); private _savingsAccountProductTemplate: any; @Input() set savingsAccountProductTemplate(value: any) { this._savingsAccountProductTemplate = value; if (value && this.savingsAccountTermForm) { this.applyTemplate(value); } } get savingsAccountProductTemplate(): any { return this._savingsAccountProductTemplate; } savingsAccountTermForm: UntypedFormGroup; interestCompoundingPeriodTypeData: any[] = []; interestPostingPeriodTypeData: any[] = []; interestCalculationTypeData: any[] = []; interestCalculationDaysInYearTypeData: any[] = []; interestCalculateSelectorData: any[] = []; periodFrequencyData: any[] = []; yesNoOptions = [ { value: true, label: 'Yes' }, { value: false, label: 'No' } ]; minDate = new Date(2000, 0, 1); maxDate = new Date(); constructor() { this.createForm(); } createForm() { this.savingsAccountTermForm = this.formBuilder.group({ currencyCode: [{ value: '', disabled: true }], nominalAnnualInterestRate: [ '', [ Validators.required, Validators.min(0) ] ], interestCompoundingPeriodType: [ '', Validators.required ], interestPostingPeriodType: [ '', Validators.required ], interestCalculationType: [ '', Validators.required ], interestCalculationDaysInYearType: [ '', Validators.required ], effectiveDate: [''], interestCalculateSelector: [1], averagePeriod: ['Monthly'], autoPost: [true], frequencyFixDay: [ 30, [Validators.min(0)] ], lastInterestDate: [''] }); } ngOnInit() { this.maxDate = this.settingsService.businessDate; // Apply template if it was set before the form was ready if (this._savingsAccountProductTemplate) { this.applyTemplate(this._savingsAccountProductTemplate); } } private applyTemplate(t: any) { this.maxDate = this.settingsService.businessDate; this.interestCompoundingPeriodTypeData = t.interestCompoundingPeriodTypeOptions || []; this.interestPostingPeriodTypeData = t.interestPostingPeriodTypeOptions || []; this.interestCalculationTypeData = t.interestCalculationTypeOptions || []; this.interestCalculateSelectorData = t.interestCalculationTypeOption || []; this.interestCalculationDaysInYearTypeData = t.interestCalculationDaysInYearTypeOptions || []; this.periodFrequencyData = t.periodFrequencyOptions?.length ? t.periodFrequencyOptions : [ { value: 'Monthly' }, { value: 'Quarterly' }, { value: 'Half Yearly' }, { value: 'Yearly' }]; const defaultPeriod = this.periodFrequencyData[0]?.value || 'Monthly'; const savedEnumTypeId = t.interestCalculationType?.id || this.interestCalculationTypeData?.[0]?.id || 1; const savedSelectorId = t.interestCalculateSelector ?? this.interestCalculateSelectorData?.[0]?.id ?? null; this.savingsAccountTermForm.patchValue({ currencyCode: t.currency?.code, nominalAnnualInterestRate: t.nominalAnnualInterestRate, interestCompoundingPeriodType: t.interestCompoundingPeriodType?.id, interestPostingPeriodType: t.interestPostingPeriodType?.id, interestCalculationType: savedEnumTypeId, interestCalculationDaysInYearType: t.interestCalculationDaysInYearType?.id, effectiveDate: t.effectiveDate ? new Date(t.effectiveDate) : '', interestCalculateSelector: savedSelectorId, averagePeriod: t.averagePeriod || defaultPeriod, autoPost: t.autoPost ?? true, frequencyFixDay: t.frequencyFixDay ?? 30, lastInterestDate: t.lastInterestDate ? new Date(t.lastInterestDate) : '' }); } get savingsAccountTerms() { const value = { ...this.savingsAccountTermForm.getRawValue() }; const dateFormat = this.settingsService.dateFormat; if (value.effectiveDate instanceof Date) { value.effectiveDate = this.dateUtils.formatDate(value.effectiveDate, dateFormat) || ''; } if (value.lastInterestDate instanceof Date) { value.lastInterestDate = this.dateUtils.formatDate(value.lastInterestDate, dateFormat) || ''; } delete value.currencyCode; return value; } }