/** Angular Imports */ import { Component , OnInit , Input , OnChanges } from '@angular/core' ; import { FormControl } from '@angular/forms' ; import { MatDialog } from '@angular/material/dialog' ; /** Dialog Components */ import { DeleteDialogComponent } from 'app/shared/delete-dialog/delete-dialog.component' ; import { FormDialogComponent } from 'app/shared/form-dialog/form-dialog.component' ; import { LoansAccountAddCollateralDialogComponent } from 'app/loans/custom-dialog/loans-account-add-collateral-dialog/loans-account-add-collateral-dialog.component' ; /** Custom Services */ import { DatePipe } from '@angular/common' ; import { DatepickerBase } from 'app/shared/form-dialog/formfield/model/datepicker-base' ; import { FormfieldBase } from 'app/shared/form-dialog/formfield/model/formfield-base' ; import { InputBase } from 'app/shared/form-dialog/formfield/model/input-base' ; import { SettingsService } from 'app/settings/settings.service' ; /**
 * Recurring Deposit Account Charges Step
 */ @ Component ( { selector : 'mifosx-loans-account-charges-step' , templateUrl : './loans-account-charges-step.component.html' , styleUrls : [ './loans-account-charges-step.component.scss' ] } ) export class LoansAccountChargesStepComponent implements OnInit , OnChanges { @ Input ( ) loansAccountProductTemplate : any ; @ Input ( ) loansAccountTemplate : any ; @ Input ( ) loansAccountFormValid : boolean ; @ Input ( ) collateralOptions : any ; /** Charges Data */ chargeData : any ; /** Charges Data Source */ chargesDataSource : { } [ ] = [ ] ; /** Overdue Charges Data Source */ overDueChargesDataSource : { } [ ] = [ ] ; /** Collateral Data Source */ collateralDataSource : { } [ ] = [ ] ; /** Charges table columns */ chargesDisplayedColumns : string [ ] = [ 'name' , 'chargeCalculationType' , 'amount' , 'chargeTimeType' , 'date' , 'action' ] ; /** Columns to be displayed in overdue charges table. */ overdueChargesDisplayedColumns : string [ ] = [ 'name' , 'type' , 'amount' , 'collectedon' ] ; /** Columns to be displayed in collateral table. */ loanCollateralDisplayedColumns : string [ ] = [ 'type' , 'value' , 'description' , 'action' ] ; /** Component is pristine if there has been no changes by user interaction */ pristine = true ; param { dialog } MatDialog param { datePipe } DatePipe param { SettingsService } settingsService constructor ( public dialog , private datePipe , private settingsService ) { } ngOnInit ( ) { if ( this . loansAccountTemplate . charges ) { this . chargesDataSource = this . loansAccountTemplate . charges . map ( ( charge ) => ( { ... charge , id : charge . chargeId } ) ) || [ ] ; } } /**
     * Executes on change of input values
     */ ngOnChanges ( ) { if ( this . loansAccountProductTemplate ) { this . chargeData = this . loansAccountProductTemplate . chargeOptions ; if ( this . loansAccountProductTemplate . overdueCharges ) { this . overDueChargesDataSource = this . loansAccountProductTemplate . overdueCharges ; } } } /**
     * Add a charge
     */ addCharge ( charge ) { this . chargesDataSource = this . chargesDataSource . concat ( [ charge . value ] ) ; charge . value = '' ; this . pristine = false ; } param { any } charge editChargeAmount ( charge ) { const formfields = [ new InputBase ( { controlName : 'amount' , label : 'Amount' , value : charge . amount , type : 'number' , required : false } ) , ] ; const data = { title : 'Edit Charge Amount' , layout : { addButtonText : 'Confirm' } , formfields : formfields } ; const editNoteDialogRef = this . dialog . open ( FormDialogComponent , { data } ) ; editNoteDialogRef . afterClosed ( ) . subscribe ( ( response ) => { if ( response . data ) { const newCharge = { ... charge , amount : response . data . value . amount } ; this . chargesDataSource . splice ( this . chargesDataSource . indexOf ( charge ) , 1 , newCharge ) ; this . chargesDataSource = this . chargesDataSource . concat ( [ ] ) ; } } ) ; this . pristine = false ; } param { any } charge editChargeDate ( charge ) { const formfields = [ new DatepickerBase ( { controlName : 'date' , label : 'Date' , value : charge . dueDate || charge . feeOnMonthDay || '' , type : 'datetime-local' , required : false } ) , ] ; const data = { title : 'Edit Charge Date' , layout : { addButtonText : 'Confirm' } , formfields : formfields } ; const editNoteDialogRef = this . dialog . open ( FormDialogComponent , { data } ) ; editNoteDialogRef . afterClosed ( ) . subscribe ( ( response ) => { if ( response . data ) { let newCharge ; const dateFormat = this . settingsService . dateFormat ; const date = this . datePipe . transform ( response . data . value . date , dateFormat ) ; switch ( charge . chargeTimeType . value ) { case 'Specified due date' : case 'Weekly Fee' : newCharge = { ... charge , dueDate : date } ; break ; case 'Annual Fee' : newCharge = { ... charge , feeOnMonthDay : date } ; break ; } this . chargesDataSource . splice ( this . chargesDataSource . indexOf ( charge ) , 1 , newCharge ) ; this . chargesDataSource = this . chargesDataSource . concat ( [ ] ) ; } } ) ; this . pristine = false ; } param { any } charge editChargeFeeInterval ( charge ) { const formfields = [ new InputBase ( { controlName : 'feeInterval' , label : 'Fee Interval' , value : charge . feeInterval , type : 'text' , required : false } ) , ] ; const data = { title : 'Edit Charge Fee Interval' , layout : { addButtonText : 'Confirm' } , formfields : formfields } ; const editNoteDialogRef = this . dialog . open ( FormDialogComponent , { data } ) ; editNoteDialogRef . afterClosed ( ) . subscribe ( ( response ) => { if ( response . data ) { const newCharge = { ... charge , feeInterval : response . data . value . feeInterval } ; this . chargesDataSource . splice ( this . chargesDataSource . indexOf ( charge ) , 1 , newCharge ) ; this . chargesDataSource = this . chargesDataSource . concat ( [ ] ) ; } } ) ; this . pristine = false ; } param charge deleteCharge ( charge ) { const deleteChargeDialogRef = this . dialog . open ( DeleteDialogComponent , { data : { deleteContext : `charge ${ charge . name }` } } ) ; deleteChargeDialogRef . afterClosed ( ) . subscribe ( ( response ) => { if ( response . delete ) { this . chargesDataSource . splice ( this . chargesDataSource . indexOf ( charge ) , 1 ) ; this . chargesDataSource = this . chargesDataSource . concat ( [ ] ) ; this . pristine = false ; } } ) ; } addCollateral ( ) { const addCollateralDialogRef = this . dialog . open ( LoansAccountAddCollateralDialogComponent , { data : { collateralOptions : this . collateralOptions } } ) ; addCollateralDialogRef . afterClosed ( ) . subscribe ( ( response ) => { if ( response . addCollateralForm ) { const collateralData = { type : response . addCollateralForm . value . type , value : response . addCollateralForm . value . value , description : response . addCollateralForm . value . description } ; this . collateralDataSource = this . collateralDataSource . concat ( collateralData ) ; } } ) ; } deleteCollateral ( id ) { const deleteCollateralDialogRef = this . dialog . open ( DeleteDialogComponent , { data : { deleteContext : `collateral` } } ) ; deleteCollateralDialogRef . afterClosed ( ) . subscribe ( ( response ) => { if ( response . delete ) { this . collateralDataSource . splice ( this . collateralDataSource . indexOf ( id ) , 1 ) ; this . collateralDataSource = this . collateralDataSource . concat ( [ ] ) ; this . pristine = false ; } } ) ; } /**
     * Returns Loans Account Charges and Collateral Form
     */ get loansAccountCharges ( ) { return { charges : this . chargesDataSource , collateral : this . collateralDataSource } ; } }