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

182 lines
6.7 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, ViewChild, inject } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
/** Custom Components */
import { CurrentAccountDetailsStepComponent } from '../current-account-stepper/current-account-details-step/current-account-details-step.component';
import { CurrentAccountTermsStepComponent } from '../current-account-stepper/current-account-terms-step/current-account-terms-step.component';
import { CurrentAccountTermStepComponent } from '../current-account-stepper/current-account-term-step/current-account-term-step.component';
import { CurrentAccountSettingsStepComponent } from '../current-account-stepper/current-account-settings-step/current-account-settings-step.component';
import { CurrentAccountChargesStepComponent } from '../current-account-stepper/current-account-charges-step/current-account-charges-step.component';
import { CurrentAccountCurrencyStepComponent } from '../current-account-stepper/current-account-currency-step/current-account-currency-step.component';
import { CurrentAccountDigitalStepComponent } from '../current-account-stepper/current-account-digital-step/current-account-digital-step.component';
/** Custom Services */
import { CurrentAccountsService } from '../current-accounts.service';
import { SettingsService } from 'app/settings/settings.service';
import { Dates } from 'app/core/utils/dates';
import { MatStepper, MatStepperIcon, MatStep, MatStepLabel } from '@angular/material/stepper';
import { FaIconComponent } from '@fortawesome/angular-fontawesome';
import { CurrentAccountPreviewStepComponent } from '../current-account-stepper/current-account-preview-step/current-account-preview-step.component';
import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module';
/**
* Create Current Account Component
*/
@Component({
selector: 'mifosx-create-current-account-account',
templateUrl: './create-current-account.component.html',
styleUrls: ['./create-current-account.component.scss'],
imports: [
...STANDALONE_SHARED_IMPORTS,
MatStepper,
MatStepperIcon,
FaIconComponent,
MatStep,
MatStepLabel,
CurrentAccountDetailsStepComponent,
CurrentAccountCurrencyStepComponent,
CurrentAccountTermStepComponent,
CurrentAccountSettingsStepComponent,
CurrentAccountDigitalStepComponent,
CurrentAccountTermsStepComponent,
CurrentAccountChargesStepComponent,
CurrentAccountPreviewStepComponent
]
})
export class CreateCurrentAccountComponent {
private route = inject(ActivatedRoute);
private router = inject(Router);
private dateUtils = inject(Dates);
private currentAccountsService = inject(CurrentAccountsService);
private settingsService = inject(SettingsService);
/** Current Account Template */
currentAccountTemplate: any;
/** Current Account Product Template */
currentAccountProductTemplate: any;
/** Current Account Details Step */
@ViewChild(CurrentAccountDetailsStepComponent, { static: true })
currentAccountDetailsStep: CurrentAccountDetailsStepComponent;
/** Current Account Term Step (new) */
@ViewChild(CurrentAccountTermStepComponent, { static: true })
currentAccountTermStep: CurrentAccountTermStepComponent;
/** Current Account Settings Step */
@ViewChild(CurrentAccountSettingsStepComponent, { static: true })
currentAccountSettingsStep: CurrentAccountSettingsStepComponent;
/** Current Account Terms Step (old — kept for currencyCode access) */
@ViewChild(CurrentAccountTermsStepComponent, { static: true })
currentAccountTermsStep: CurrentAccountTermsStepComponent;
/** Current Account Charges Step */
@ViewChild(CurrentAccountChargesStepComponent, { static: true })
currentAccountChargesStep: CurrentAccountChargesStepComponent;
constructor() {
this.route.data.subscribe((data: { currentAccountTemplate: any }) => {
this.currentAccountTemplate = data.currentAccountTemplate;
});
}
/**
* Sets current account product template.
*/
setTemplate($event: any) {
this.currentAccountProductTemplate = $event;
}
/**
* Retrieves current account details form.
*/
get currentAccountDetailsForm() {
return this.currentAccountDetailsStep.currentAccountDetailsForm;
}
/**
* Retrieves new term form (interest rate).
*/
get currentAccountTermForm() {
return this.currentAccountTermStep.currentAccountTermForm;
}
/**
* Retrieves settings form.
*/
get currentAccountSettingsForm() {
return this.currentAccountSettingsStep.currentAccountSettingsForm;
}
/**
* Retrieves old terms form — still used for currencyCode by charges step.
*/
get currentAccountTermsForm() {
return this.currentAccountTermsStep.currentAccountTermsForm;
}
/**
* Checks validity of overall current account form.
*/
get currentAccountFormValid() {
return this.currentAccountDetailsForm.valid && this.currentAccountTermForm.valid;
}
/**
* Retrieves current account object.
*/
get currentAccount() {
return {
...this.currentAccountDetailsStep.currentAccountDetails,
...this.currentAccountTermStep.currentAccountTerms,
...this.currentAccountSettingsStep.currentAccountSettings,
...this.currentAccountChargesStep.currentAccountCharges
};
}
/**
* Creates a new current account.
*/
submit() {
const locale = this.settingsService.language.code;
const dateFormat = this.settingsService.dateFormat;
const monthDayFormat = 'dd MMMM';
const currentAccount = {
...this.currentAccount,
charges: this.currentAccount.charges.map((charge: any) => ({
chargeId: charge.id,
amount: charge.amount,
dueDate: charge.dueDate ? this.dateUtils.formatDate(charge.dueDate, dateFormat) : charge.dueDate,
feeOnMonthDay: charge.feeOnMonthDay
? this.dateUtils.formatDate(charge.feeOnMonthDay, monthDayFormat)
: charge.feeOnMonthDay,
feeInterval: charge.feeInterval
})),
submittedOnDate: this.dateUtils.formatDate(this.currentAccount.submittedOnDate, dateFormat),
dateFormat,
monthDayFormat,
locale
};
if (this.currentAccountTemplate.clientId) {
currentAccount.clientId = this.currentAccountTemplate.clientId;
} else {
currentAccount.groupId = this.currentAccountTemplate.groupId;
}
this.currentAccountsService.createCurrentAccount(currentAccount).subscribe((response: any) => {
this.router.navigate(
[
'../',
response.resourceId
],
{ relativeTo: this.route }
);
});
}
}