11import { Component , Input , OnInit , inject } from '@angular/core' ;
22import { UntypedFormBuilder , UntypedFormGroup } from '@angular/forms' ;
33import { ActivatedRoute , Router } from '@angular/router' ;
4+ import { MatDialog } from '@angular/material/dialog' ;
45import { LoansService } from 'app/loans/loans.service' ;
6+ import { RepaymentSchedule } from 'app/loans/models/loan-account.model' ;
57import { CodeValue } from 'app/shared/models/general.model' ;
68import { OptionData } from 'app/shared/models/option-data.model' ;
79import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module' ;
10+ import { SettingsService } from 'app/settings/settings.service' ;
11+ import { ReAmortizePreviewDialogComponent } from './re-amortize-preview-dialog/re-amortize-preview-dialog.component' ;
812
913@Component ( {
1014 selector : 'mifosx-loan-reamortize' ,
@@ -19,6 +23,8 @@ export class LoanReamortizeComponent implements OnInit {
1923 private route = inject ( ActivatedRoute ) ;
2024 private router = inject ( Router ) ;
2125 private loanService = inject ( LoansService ) ;
26+ private settingsService = inject ( SettingsService ) ;
27+ private dialog = inject ( MatDialog ) ;
2228
2329 @Input ( ) dataObject : any ;
2430 /** Loan Id */
@@ -50,8 +56,76 @@ export class LoanReamortizeComponent implements OnInit {
5056 } ) ;
5157 }
5258
53- submit ( ) : void {
59+ private prepareReAmortizeData ( ) {
5460 const data = this . reamortizeLoanForm . value ;
61+ const locale = this . settingsService . language . code ;
62+ const dateFormat = this . settingsService . dateFormat ;
63+
64+ return {
65+ ...data ,
66+ dateFormat,
67+ locale
68+ } ;
69+ }
70+
71+ private prepareReAmortizePreviewData ( ) {
72+ const reamortizeLoanFormData = { ...this . reamortizeLoanForm . value } ;
73+ const locale = this . settingsService . language . code ;
74+ const dateFormat = this . settingsService . dateFormat ;
75+
76+ // Prepare reAmortizationInterestHandling for preview API
77+ let reAmortizationInterestHandling = reamortizeLoanFormData . reAmortizationInterestHandling ;
78+ if ( reAmortizationInterestHandling && typeof reAmortizationInterestHandling === 'object' ) {
79+ reAmortizationInterestHandling = reAmortizationInterestHandling . id ;
80+ }
81+ // If no value selected, use "default" for preview
82+ if ( ! reAmortizationInterestHandling && reAmortizationInterestHandling !== 0 ) {
83+ reAmortizationInterestHandling = 'default' ;
84+ }
85+
86+ delete reamortizeLoanFormData . reAmortizationInterestHandling ;
87+
88+ return {
89+ ...reamortizeLoanFormData ,
90+ reAmortizationInterestHandling : reAmortizationInterestHandling ,
91+ dateFormat,
92+ locale
93+ } ;
94+ }
95+
96+ preview ( ) : void {
97+ if ( this . reamortizeLoanForm . invalid ) {
98+ return ;
99+ }
100+ const data = this . prepareReAmortizePreviewData ( ) ;
101+
102+ this . loanService . getReAmortizePreview ( this . loanId , data ) . subscribe ( {
103+ next : ( response : RepaymentSchedule ) => {
104+ const currencyCode = response . currency ?. code ;
105+
106+ if ( ! currencyCode ) {
107+ console . error ( 'Currency code is not available in API response' ) ;
108+ return ;
109+ }
110+
111+ this . dialog . open ( ReAmortizePreviewDialogComponent , {
112+ data : {
113+ repaymentSchedule : response ,
114+ currencyCode : currencyCode
115+ } ,
116+ width : '95%' ,
117+ maxWidth : '1400px' ,
118+ height : '90vh'
119+ } ) ;
120+ } ,
121+ error : ( error ) => {
122+ console . error ( 'Error loading re-amortize preview:' , error ) ;
123+ }
124+ } ) ;
125+ }
126+
127+ submit ( ) : void {
128+ const data = this . prepareReAmortizeData ( ) ;
55129 this . loanService . submitLoanActionButton ( this . loanId , data , 'reAmortize' ) . subscribe ( ( response : any ) => {
56130 this . router . navigate ( [ '../../transactions' ] , { relativeTo : this . route } ) ;
57131 } ) ;
0 commit comments