@@ -4,6 +4,7 @@ import { ActivatedRoute, Router, RouterLink } from '@angular/router';
44import { TranslateService } from '@ngx-translate/core' ;
55import { Dates } from 'app/core/utils/dates' ;
66import { LoansService } from 'app/loans/loans.service' ;
7+ import { EditableRepaymentSchedule , EditablePeriod , ScheduleChangeRecord } from 'app/loans/models/loan-account.model' ;
78import { SettingsService } from 'app/settings/settings.service' ;
89import { ConfirmationDialogComponent } from 'app/shared/confirmation-dialog/confirmation-dialog.component' ;
910import { FormDialogComponent } from 'app/shared/form-dialog/form-dialog.component' ;
@@ -30,11 +31,9 @@ export class EditRepaymentScheduleComponent implements OnInit {
3031 /** Indicates If the Schedule has been validated */
3132 wasValidated = false ;
3233 /** Stores the Repayment Schedule data */
33- repaymentScheduleDetails : {
34- periods : { period ?: number ; dueDate : string ; totalDueForPeriod ?: number ; changed ?: boolean } [ ] ;
35- } | null = null ;
34+ repaymentScheduleDetails : EditableRepaymentSchedule | null = null ;
3635 /** Stores the Installments changed */
37- repaymentScheduleChanges : any = { } ;
36+ repaymentScheduleChanges : Record < string , ScheduleChangeRecord > = { } ;
3837
3938 /**
4039 * @param {LoansService } systemService Loan Service.
@@ -62,14 +61,23 @@ export class EditRepaymentScheduleComponent implements OnInit {
6261 }
6362
6463 getRepaymentSchedule ( ) : void {
65- this . loansService . getLoanAccountResource ( this . loanId , 'repaymentSchedule' ) . subscribe ( ( response : any ) => {
66- this . repaymentScheduleDetails = response . repaymentSchedule ;
64+ this . loansService . getLoanAccountResource ( this . loanId , 'repaymentSchedule' ) . subscribe ( {
65+ next : ( response : { repaymentSchedule : EditableRepaymentSchedule } ) => {
66+ this . repaymentScheduleDetails = response . repaymentSchedule ;
67+ } ,
68+ error : ( err ) => {
69+ console . error ( 'Failed to load repayment schedule:' , err ) ;
70+ }
6771 } ) ;
6872 }
6973
7074 applyPattern ( ) : void {
71- const periods : any = [ ] ;
72- this . repaymentScheduleDetails [ 'periods' ] . forEach ( ( period : any ) => {
75+ if ( ! this . repaymentScheduleDetails ) {
76+ return ;
77+ }
78+
79+ const periods : Array < { idx : number ; dueDate : string } > = [ ] ;
80+ this . repaymentScheduleDetails . periods . forEach ( ( period : EditablePeriod ) => {
7381 if ( period . period ) {
7482 periods . push ( {
7583 idx : period . period ,
@@ -106,27 +114,29 @@ export class EditRepaymentScheduleComponent implements OnInit {
106114 formfields : formfields
107115 } ;
108116 const addDialogRef = this . dialog . open ( FormDialogComponent , { data } ) ;
109- addDialogRef . afterClosed ( ) . subscribe ( ( response : any ) => {
110- if ( response . data ) {
111- const fromPeriod = response . data . value . fromPeriod ;
112- const toPeriod = response . data . value . toPeriod ;
113- const amount = response . data . value . amount ;
114- const periodsVariation : any = [ ] ;
115- this . repaymentScheduleDetails [ 'periods' ] . forEach ( ( period : any ) => {
116- const dueDate = this . dateUtils . formatDate ( period . dueDate , this . settingsService . dateFormat ) ;
117- if ( period . period && fromPeriod <= period . period && toPeriod >= period . period ) {
118- if ( period . totalDueForPeriod !== amount ) {
119- period . totalDueForPeriod = amount ;
120- this . repaymentScheduleChanges [ dueDate ] = { dueDate : dueDate , installmentAmount : amount } ;
121- this . wasChanged = true ;
122- period [ 'changed' ] = true ;
117+ addDialogRef
118+ . afterClosed ( )
119+ . subscribe ( ( response : { data ?: { value ?: { fromPeriod : number ; toPeriod : number ; amount : number } } } ) => {
120+ if ( response . data ?. value && this . repaymentScheduleDetails ) {
121+ const fromPeriod = response . data . value . fromPeriod ;
122+ const toPeriod = response . data . value . toPeriod ;
123+ const amount = response . data . value . amount ;
124+ const periodsVariation : EditablePeriod [ ] = [ ] ;
125+ this . repaymentScheduleDetails . periods . forEach ( ( period : EditablePeriod ) => {
126+ const dueDate = this . dateUtils . formatDate ( period . dueDate , this . settingsService . dateFormat ) ;
127+ if ( period . period && fromPeriod <= period . period && toPeriod >= period . period ) {
128+ if ( period . totalDueForPeriod !== amount ) {
129+ period . totalDueForPeriod = amount ;
130+ this . repaymentScheduleChanges [ dueDate ] = { dueDate : dueDate , installmentAmount : amount } ;
131+ this . wasChanged = true ;
132+ period . changed = true ;
133+ }
123134 }
124- }
125- periodsVariation . push ( period ) ;
126- } ) ;
127- this . repaymentScheduleDetails [ 'periods' ] = periodsVariation ;
128- }
129- } ) ;
135+ periodsVariation . push ( period ) ;
136+ } ) ;
137+ this . repaymentScheduleDetails . periods = periodsVariation ;
138+ }
139+ } ) ;
130140 }
131141
132142 reset ( ) : void {
@@ -138,42 +148,63 @@ export class EditRepaymentScheduleComponent implements OnInit {
138148 )
139149 }
140150 } ) ;
141- recoverScheduleDialogRef . afterClosed ( ) . subscribe ( ( responseConfirmation : any ) => {
151+ recoverScheduleDialogRef . afterClosed ( ) . subscribe ( ( responseConfirmation : { confirm ?: boolean } ) => {
142152 if ( responseConfirmation . confirm ) {
143- this . loansService
144- . applyCommandLoanScheduleVariations ( this . loanId , 'deleteVariations' , { } )
145- . subscribe ( ( response : any ) => {
153+ this . loansService . applyCommandLoanScheduleVariations ( this . loanId , 'deleteVariations' , { } ) . subscribe ( {
154+ next : ( ) => {
146155 this . getRepaymentSchedule ( ) ;
147156 this . wasChanged = false ;
148157 this . wasValidated = false ;
149- } ) ;
158+ } ,
159+ error : ( err ) => {
160+ console . error ( 'Failed to delete schedule variations:' , err ) ;
161+ }
162+ } ) ;
150163 }
151164 } ) ;
152165 }
153166
154167 validate ( ) : void {
168+ if ( ! this . repaymentScheduleDetails ) {
169+ return ;
170+ }
171+
155172 this . loansService
156173 . applyCommandLoanScheduleVariations ( this . loanId , 'calculateLoanSchedule' , this . getPayload ( ) )
157- . subscribe ( ( response : any ) => {
158- this . repaymentScheduleDetails [ 'periods' ] = [ ] ;
159- response [ 'periods' ] . forEach ( ( period : any ) => {
160- period [ 'changed' ] = true ;
161- this . repaymentScheduleDetails [ 'periods' ] . push ( period ) ;
162- this . wasValidated = true ;
163- } ) ;
174+ . subscribe ( {
175+ next : ( response : EditableRepaymentSchedule ) => {
176+ if ( this . repaymentScheduleDetails ) {
177+ this . repaymentScheduleDetails . periods = [ ] ;
178+ response . periods . forEach ( ( period : EditablePeriod ) => {
179+ period . changed = true ;
180+ this . repaymentScheduleDetails ! . periods . push ( period ) ;
181+ this . wasValidated = true ;
182+ } ) ;
183+ }
184+ } ,
185+ error : ( err ) => {
186+ console . error ( 'Failed to calculate loan schedule:' , err ) ;
187+ }
164188 } ) ;
165189 }
166190
167191 submit ( ) : void {
168- this . loansService
169- . applyCommandLoanScheduleVariations ( this . loanId , 'addVariations' , this . getPayload ( ) )
170- . subscribe ( ( response : any ) => {
192+ this . loansService . applyCommandLoanScheduleVariations ( this . loanId , 'addVariations' , this . getPayload ( ) ) . subscribe ( {
193+ next : ( ) => {
171194 this . router . navigate ( [ '../../repayment-schedule' ] , { relativeTo : this . route } ) ;
172- } ) ;
195+ } ,
196+ error : ( err ) => {
197+ console . error ( 'Failed to add schedule variations:' , err ) ;
198+ }
199+ } ) ;
173200 }
174201
175- private getPayload ( ) : any {
176- const modifiedinstallments : any = [ ] ;
202+ private getPayload ( ) : {
203+ exceptions : { modifiedinstallments : ScheduleChangeRecord [ ] } ;
204+ dateFormat : string ;
205+ locale : string ;
206+ } {
207+ const modifiedinstallments : ScheduleChangeRecord [ ] = [ ] ;
177208 Object . keys ( this . repaymentScheduleChanges ) . forEach ( ( key : string ) => {
178209 modifiedinstallments . push ( this . repaymentScheduleChanges [ key ] ) ;
179210 } ) ;
0 commit comments