/** * 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 { UntypedFormGroup, UntypedFormBuilder, Validators, ReactiveFormsModule } from '@angular/forms'; import { ActivatedRoute, Router, RouterLink } from '@angular/router'; import { Dates } from 'app/core/utils/dates'; /** Custom Services */ import { CurrentAccountsService } from 'app/current-accounts/current-accounts.service'; import { SettingsService } from 'app/settings/settings.service'; import { CdkTextareaAutosize } from '@angular/cdk/text-field'; import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module'; /** * Approve Current Account Component */ @Component({ selector: 'mifosx-approve-current-account', templateUrl: './approve-current-account.component.html', styleUrls: ['./approve-current-account.component.scss'], imports: [ ...STANDALONE_SHARED_IMPORTS, CdkTextareaAutosize ] }) export class ApproveCurrentAccountComponent implements OnInit { private formBuilder = inject(UntypedFormBuilder); private currentAccountsService = inject(CurrentAccountsService); private dateUtils = inject(Dates); private route = inject(ActivatedRoute); private router = inject(Router); private settingsService = inject(SettingsService); /** Minimum date allowed. */ minDate = new Date(2000, 0, 1); /** Maximum date allowed. */ maxDate = new Date(); /** Approve Current Account form. */ approveCurrentAccountForm: UntypedFormGroup; /** Current Account Id */ accountId: any; /** * @param {FormBuilder} formBuilder Form Builder * @param {CurrentAccountsService} currentAccountsService Current Accounts Service * @param {Dates} dateUtils Date Utils * @param {ActivatedRoute} route Activated Route * @param {Router} router Router * @param {SettingsService} settingsService Setting service */ constructor() { this.accountId = this.route.snapshot.params['currentAccountId']; } /** * Creates the approve current account form. */ ngOnInit() { this.maxDate = this.settingsService.businessDate; this.createApproveCurrentAccountForm(); } /** * Creates the approve current account form. */ createApproveCurrentAccountForm() { this.approveCurrentAccountForm = this.formBuilder.group({ approvedOnDate: [ '', Validators.required ], note: [''] }); } /** * Submits the form, approves the current account, then immediately activates it on the same * date -- Approve and Activate are still two separate Fineract commands underneath (each shows * up in the audit trail), but the officer only sees and clicks through one step. */ submit() { const approveCurrentAccountFormData = this.approveCurrentAccountForm.value; const locale = this.settingsService.language.code; const dateFormat = this.settingsService.dateFormat; const prevApprovedOnDate: Date = this.approveCurrentAccountForm.value.approvedOnDate; if (approveCurrentAccountFormData.approvedOnDate instanceof Date) { approveCurrentAccountFormData.approvedOnDate = this.dateUtils.formatDate(prevApprovedOnDate, dateFormat); } const data = { ...approveCurrentAccountFormData, dateFormat, locale }; this.currentAccountsService.executeCurrentAccountCommand(this.accountId, 'approve', data).subscribe(() => { const activationData = { activatedOnDate: approveCurrentAccountFormData.approvedOnDate, dateFormat, locale }; this.currentAccountsService .executeCurrentAccountCommand(this.accountId, 'activate', activationData) .subscribe(() => { this.router.navigate(['../../transactions'], { relativeTo: this.route }); }); }); } }