/** Angular Imports */ import { Component , OnInit , ViewChild } from '@angular/core' ; import { ActivatedRoute , Router } from '@angular/router' ; import { DatePipe } from '@angular/common' ; /** Custom Services */ import { LoansService } from '../loans.service' ; import { SettingsService } from 'app/settings/settings.service' ; /** Step Components */ import { LoansAccountDetailsStepComponent } from '../loans-account-stepper/loans-account-details-step/loans-account-details-step.component' ; import { LoansAccountTermsStepComponent } from '../loans-account-stepper/loans-account-terms-step/loans-account-terms-step.component' ; import { LoansAccountChargesStepComponent } from '../loans-account-stepper/loans-account-charges-step/loans-account-charges-step.component' ; /**
 * Create loans account
 */ @ Component ( { selector : 'mifosx-create-loans-account' , templateUrl : './create-loans-account.component.html' , styleUrls : [ './create-loans-account.component.scss' ] } ) export class CreateLoansAccountComponent implements OnInit { /** Imports all the step component */ @ ViewChild ( LoansAccountDetailsStepComponent , { static : true } ) loansAccountDetailsStep : LoansAccountDetailsStepComponent ; @ ViewChild ( LoansAccountTermsStepComponent , { static : true } ) loansAccountTermsStep : LoansAccountTermsStepComponent ; @ ViewChild ( LoansAccountChargesStepComponent , { static : true } ) loansAccountChargesStep : LoansAccountChargesStepComponent ; /** Loans Account Template */ loansAccountTemplate : any ; /** Loans Account Product Template */ loansAccountProductTemplate : any ; /** Collateral Options */ collateralOptions : any ; param { route } ActivatedRoute param { router } Router param { datePipe } DatePipe param { loansService } LoansService param { SettingsService } settingsService constructor ( private route , private router , private datePipe , private loansService , private settingsService ) { this . route . data . subscribe ( ( data ) => { this . loansAccountTemplate = data . loansAccountTemplate ; } ) ; } ngOnInit ( ) { } param { any } $event setTemplate ( $event ) { this . loansAccountProductTemplate = $event ; this . loansService . getLoansCollateralTemplateResource ( this . loansAccountProductTemplate . loanProductId ) . subscribe ( ( response ) => { this . collateralOptions = response . loanCollateralOptions ; } ) ; } /** Get Loans Account Details Form Data */ get loansAccountDetailsForm ( ) { return this . loansAccountDetailsStep . loansAccountDetailsForm ; } /** Get Loans Account Terms Form Data */ get loansAccountTermsForm ( ) { return this . loansAccountTermsStep . loansAccountTermsForm ; } /** Checks wheter all the forms in different steps are valid or not */ get loansAccountFormValid ( ) { return ( this . loansAccountDetailsForm . valid && this . loansAccountTermsForm . valid ) ; } /** Retrieves Data of all forms except Currency to submit the data */ get loansAccount ( ) { return { ... this . loansAccountDetailsStep . loansAccountDetails , ... this . loansAccountTermsStep . loansAccountTerms , ... this . loansAccountChargesStep . loansAccountCharges , } ; } /**
     * Submits Data to create loan account
     */ submit ( ) { const locale = this . settingsService . language . code ; const dateFormat = this . settingsService . dateFormat ; const loanType = 'individual' ; const loansAccountData = { ... this . loansAccount , clientId : this . loansAccountTemplate . clientId , charges : this . loansAccount . charges . map ( ( charge ) => ( { chargeId : charge . id , amount : charge . amount , dueDate : charge . dueDate && this . datePipe . transform ( charge . dueDate , dateFormat ) , } ) ) , collateral : this . loansAccount . collateral . map ( ( collateralEle ) => ( { type : collateralEle . type , value : collateralEle . value , description : collateralEle . description } ) ) , interestChargedFromDate : this . datePipe . transform ( this . loansAccount . interestChargedFromDate , dateFormat ) , repaymentsStartingFromDate : this . datePipe . transform ( this . loansAccount . repaymentsStartingFromDate , dateFormat ) , submittedOnDate : this . datePipe . transform ( this . loansAccount . submittedOnDate , dateFormat ) , expectedDisbursementDate : this . datePipe . transform ( this . loansAccount . expectedDisbursementDate , dateFormat ) , dateFormat , locale , loanType } ; if ( loansAccountData . syncRepaymentsWithMeeting ) { loansAccountData . calendarId = this . loansAccountProductTemplate . calendarOptions [ 0 ] . id ; delete loansAccountData . syncRepaymentsWithMeeting ; } if ( loansAccountData . recalculationRestFrequencyDate ) { loansAccountData . recalculationRestFrequencyDate = this . datePipe . transform ( this . loansAccount . recalculationRestFrequencyDate , dateFormat ) ; } if ( loansAccountData . recalculationCompoundingFrequencyDate ) { loansAccountData . recalculationCompoundingFrequencyDate = this . datePipe . transform ( this . loansAccount . recalculationCompoundingFrequencyDate , dateFormat ) ; } if ( loansAccountData . interestCalculationPeriodType === 0 ) { loansAccountData . allowPartialPeriodInterestCalcualtion = false ; } if ( ! ( loansAccountData . isFloatingInterestRate === false ) ) { delete loansAccountData . isFloatingInterestRate ; } this . loansService . createLoansAccount ( loansAccountData ) . subscribe ( ( response ) => { this . router . navigate ( [ '../' , response . resourceId ] , { relativeTo : this . route } ) ; } ) ; } }