/** Angular Imports */ import { Component , OnInit } from '@angular/core' ; import { FormGroup , FormBuilder , Validators } from '@angular/forms' ; import { Router , ActivatedRoute } from '@angular/router' ; /** Custom Services */ import { AccountingService } from '../../accounting.service' ; /** Custom Validators */ import { oneOfTheFieldsIsRequiredValidator } from '../one-of-the-fields-is-required.validator' ; /**
 * Create accounting rule component.
 */ @ Component ( { selector : 'mifosx-create-rule' , templateUrl : './create-rule.component.html' , styleUrls : [ './create-rule.component.scss' ] } ) export class CreateRuleComponent implements OnInit { /** Accounting rule form. */ accountingRuleForm : FormGroup ; /** Office data. */ officeData : any ; /** GL Account data. */ glAccountData : any ; /** Debit tag data. */ debitTagData : any ; /** Credit tag data. */ creditTagData : any ; param { FormBuilder } formBuilder param { AccountingService } accountingService param { ActivatedRoute } route param { Router } router constructor ( private formBuilder , private accountingService , private route , private router ) { this . route . data . subscribe ( ( data ) => { this . officeData = data . accountingRulesTemplate . allowedOffices ; this . glAccountData = data . accountingRulesTemplate . allowedAccounts ; this . debitTagData = data . accountingRulesTemplate . allowedDebitTagOptions ; this . creditTagData = data . accountingRulesTemplate . allowedCreditTagOptions ; } ) ; } /**
     * Creates and sets accounting rules form.
     */ ngOnInit ( ) { this . createAccountingRuleForm ( ) ; this . setAccountingRulesForm ( ) ; } /**
     * Creates accounting rule form.
     */ createAccountingRuleForm ( ) { this . accountingRuleForm = this . formBuilder . group ( { 'name' : [ '' , Validators . required ] , 'officeId' : [ '' , Validators . required ] , 'debitRuleType' : [ 'fixedAccount' ] , 'accountToDebit' : [ '' ] , 'debitTags' : [ '' ] , 'allowMultipleDebitEntries' : [ '' ] , 'creditRuleType' : [ 'fixedAccount' ] , 'accountToCredit' : [ '' ] , 'creditTags' : [ '' ] , 'allowMultipleCreditEntries' : [ '' ] , 'description' : [ '' ] } , { validator : oneOfTheFieldsIsRequiredValidator } ) ; } /**
     * Sets accounting rule form for selected accounting rule type.
     */ setAccountingRulesForm ( ) { this . accountingRuleForm . get ( 'debitRuleType' ) . valueChanges . subscribe ( ( debitRuleType ) => { if ( debitRuleType === 'fixedAccount' ) { this . accountingRuleForm . get ( 'debitTags' ) . reset ( ) ; this . accountingRuleForm . get ( 'allowMultipleDebitEntries' ) . reset ( ) ; } else { this . accountingRuleForm . get ( 'accountToDebit' ) . reset ( ) ; this . accountingRuleForm . get ( 'allowMultipleDebitEntries' ) . setValue ( false ) ; } } ) ; this . accountingRuleForm . get ( 'creditRuleType' ) . valueChanges . subscribe ( ( creditRuleType ) => { if ( creditRuleType === 'fixedAccount' ) { this . accountingRuleForm . get ( 'creditTags' ) . reset ( ) ; this . accountingRuleForm . get ( 'allowMultipleCreditEntries' ) . reset ( ) ; } else { this . accountingRuleForm . get ( 'accountToCredit' ) . reset ( ) ; this . accountingRuleForm . get ( 'allowMultipleCreditEntries' ) . setValue ( false ) ; } } ) ; } /**
     * Submits the accounting rule form and creates accounting rule,
     * if successful redirects to view created rule.
     */ submit ( ) { const accountingRule = this . accountingRuleForm . value ; if ( accountingRule . debitRuleType === 'fixedAccount' ) { delete accountingRule . debitTags ; delete accountingRule . allowMultipleDebitEntries ; } else { delete accountingRule . accountToDebit ; } if ( accountingRule . creditRuleType === 'fixedAccount' ) { delete accountingRule . creditTags ; delete accountingRule . allowMultipleCreditEntries ; } else { delete accountingRule . accountToCredit ; } delete accountingRule . debitRuleType ; delete accountingRule . creditRuleType ; this . accountingService . createAccountingRule ( accountingRule ) . subscribe ( ( response ) => { this . router . navigate ( [ '../view' , response . resourceId ] , { relativeTo : this . route } ) ; } ) ; } }