/** Angular Imports */ import { Component , OnInit } from '@angular/core' ; import { FormGroup , FormBuilder , Validators } from '@angular/forms' ; import { MatDialog } from '@angular/material/dialog' ; import { MatTableDataSource } from '@angular/material/table' ; import { Router , ActivatedRoute } from '@angular/router' ; /** Custom Models */ import { FormfieldBase } from 'app/shared/form-dialog/formfield/model/formfield-base' ; import { InputBase } from 'app/shared/form-dialog/formfield/model/input-base' ; import { SelectBase } from 'app/shared/form-dialog/formfield/model/select-base' ; /** Custom Dialogs */ import { FormDialogComponent } from 'app/shared/form-dialog/form-dialog.component' ; /** Custom Services */ import { OrganizationService } from '../../organization.service' ; import { SettingsService } from 'app/settings/settings.service' ; /**
 * Edit Loan Provisioning Criteria Component.
 */ @ Component ( { selector : 'mifosx-edit-loan-provisioning-criteria' , templateUrl : './edit-loan-provisioning-criteria.component.html' , styleUrls : [ './edit-loan-provisioning-criteria.component.scss' ] } ) export class EditLoanProvisioningCriteriaComponent implements OnInit { /** Loan Provisioning Criteria form. */ provisioningCriteriaForm : FormGroup ; /** Loan Provisioning Criteria Template */ loanProvisioningCriteriaAndTemplate : any ; /** Liability Accounts */ liabilityAccounts : any ; /** Expense Accounts */ expenseAccounts : any ; /** Loan Products */ loanProducts : any ; /** Columns to be displayed in definitions table. */ displayedColumns : string [ ] = [ 'category' , 'minAge' , 'maxAge' , 'percentage' , 'liabilityAccount' , 'expenseAccount' , 'edit' ] ; /** Criteria Definitions Array */ definitions : { categoryId : number ; categoryName : string ; maxAge ? : number ; minAge ? : number ; liabilityAccount ? : string ; expenseAccount ? : string ; provisioningPercentage ? : number ; } [ ] = [ ] ; param { FormBuilder } formBuilder param { OrganizationService } organizationService param { SettingsService } settingsService param { ActivatedRoute } route param { Router } router constructor ( private formBuilder , private organizationService , private router , private settingsService , public dialog , private route ) { this . route . data . subscribe ( ( data ) => { this . loanProvisioningCriteriaAndTemplate = data . loanProvisioningCriteriaAndTemplate ; this . definitions = this . loanProvisioningCriteriaAndTemplate . definitions ; this . loanProducts = this . loanProvisioningCriteriaAndTemplate . loanProducts . concat ( this . loanProvisioningCriteriaAndTemplate . selectedLoanProducts ) ; this . liabilityAccounts = this . loanProvisioningCriteriaAndTemplate . glAccounts . filter ( ( account ) => account . type . value === 'LIABILITY' ) ; this . expenseAccounts = this . loanProvisioningCriteriaAndTemplate . glAccounts . filter ( ( account ) => account . type . value === 'EXPENSE' ) ; } ) ; } ngOnInit ( ) { this . createProvisioningCriteriaForm ( ) ; } /**
     * Creates the provisioning criteria form
     */ createProvisioningCriteriaForm ( ) { this . provisioningCriteriaForm = this . formBuilder . group ( { 'criteriaName' : [ this . loanProvisioningCriteriaAndTemplate . criteriaName , Validators . required ] , 'loanProducts' : [ this . loanProvisioningCriteriaAndTemplate . selectedLoanProducts ] } ) ; } param { any } option1 param { any } option2 compareOptions ( option1 , option2 ) { return option1 && option2 && option1 . id === option2 . id ; } param { any } definition param { number } index editDefinition ( definition ) { const data = { title : 'Edit Criteria Definition' , formfields : this . getDefinitionFormFields ( definition ) , layout : { addButtonText : 'Confirm' } } ; const editDefinitionDialogRef = this . dialog . open ( FormDialogComponent , { data } ) ; editDefinitionDialogRef . afterClosed ( ) . subscribe ( ( response ) => { if ( response . data ) { const definitionData = { ... definition , ... response . data . value } ; this . definitions . splice ( this . definitions . indexOf ( definition ) , 1 , definitionData ) ; this . definitions = this . definitions . concat ( [ ] ) ; } } ) ; } param { any } definition getDefinitionFormFields ( definition ) { const formfields = [ ] ; formfields . push ( new InputBase ( { controlName : 'minAge' , label : 'Min Age' , value : definition ? definition . minAge : '' , type : 'number' , required : true , order : 1 } ) ) ; formfields . push ( new InputBase ( { controlName : 'maxAge' , label : 'Max Age' , value : definition ? definition . maxAge : '' , type : 'number' , required : true , order : 2 } ) ) ; formfields . push ( new InputBase ( { controlName : 'provisioningPercentage' , label : 'Percentage (%)' , value : definition ? definition . provisioningPercentage : '' , type : 'number' , required : true , order : 3 } ) ) ; formfields . push ( new SelectBase ( { controlName : 'liabilityAccount' , label : 'Liability Account' , value : definition ? definition . liabilityAccount : '' , options : { label : 'name' , value : 'id' , data : this . liabilityAccounts } , required : true , order : 4 } ) ) ; formfields . push ( new SelectBase ( { controlName : 'expenseAccount' , label : 'Expense Account' , value : definition ? definition . expenseAccount : '' , options : { label : 'name' , value : 'id' , data : this . expenseAccounts } , required : true , order : 5 } ) ) ; return formfields ; } /**
     * Returns validity of form.
     */ get provisioningCriteriaFormValid ( ) { return this . provisioningCriteriaForm . valid ; } /**
     * Edits the loan provisioning criteria.
     */ submit ( ) { const locale = this . settingsService . language . code ; const loanProvisioningCriteria = { ... this . provisioningCriteriaForm . value , loanProducts : this . provisioningCriteriaForm . get ( 'loanProducts' ) . value . map ( ( product ) => ( { id : product . id , name : product . name , includeInBorrowerCycle : product . includeInBorrowerCycle } ) ) , definitions : this . definitions , locale } ; this . organizationService . updateProvisioningCriteria ( this . loanProvisioningCriteriaAndTemplate . criteriaId , loanProvisioningCriteria ) . subscribe ( ( response ) => { this . router . navigate ( [ '../' ] , { relativeTo : this . route } ) ; } ) ; } }