/** Angular Imports */ import { Component , OnInit } from '@angular/core' ; import { FormGroup , FormBuilder , Validators , FormArray } from '@angular/forms' ; import { Router , ActivatedRoute } from '@angular/router' ; /** Custom Services */ import { AccountingService } from '../accounting.service' ; import { SettingsService } from 'app/settings/settings.service' ; /**
 * Frequent Postings component.
 */ @ Component ( { selector : 'mifosx-frequent-postings' , templateUrl : './frequent-postings.component.html' , styleUrls : [ './frequent-postings.component.scss' ] } ) export class FrequentPostingsComponent implements OnInit { /** Minimum transaction date allowed. */ minDate = new Date ( 2000 , 0 , 1 ) ; /** Maximum transaction date allowed. */ maxDate = new Date ( ) ; /** Frequent postings form. */ frequentPostingsForm : FormGroup ; /** Office data. */ officeData : any ; /** Accounting rule data. */ accountingRuleData : any ; /** Currency data. */ currencyData : any ; /** Payment type data. */ paymentTypeData : any ; /** Debit account data. */ debitAccountData : any ; /** Credit account data. */ creditAccountData : any ; /** True if multiple credit entries are allowed. */ allowMultipleCreditEntries : boolean ; /** True if multiple debit entries are allowed. */ allowMultipleDebitEntries : boolean ; param { FormBuilder } formBuilder param { AccountingService } accountingService param { SettingsService } settingsService param { ActivatedRoute } route param { Router } router constructor ( private formBuilder , private accountingService , private settingsService , private route , private router ) { this . route . data . subscribe ( ( data ) => { this . officeData = data . offices ; this . accountingRuleData = data . accountingRules ; this . currencyData = data . currencies . selectedCurrencyOptions ; this . paymentTypeData = data . paymentTypes ; } ) ; } /**
     * Creates the frequent postings form and sets the affected gl entry form array.
     */ ngOnInit ( ) { this . createFrequentPostingsForm ( ) ; this . setAffectedGLEntryForm ( ) ; } /**
     * Creates the frequent postings form.
     */ createFrequentPostingsForm ( ) { this . frequentPostingsForm = this . formBuilder . group ( { 'officeId' : [ '' , Validators . required ] , 'accountingRule' : [ '' , Validators . required ] , 'currencyCode' : [ '' , Validators . required ] , 'debits' : this . formBuilder . array ( [ ] ) , 'credits' : this . formBuilder . array ( [ ] ) , 'referenceNumber' : [ '' ] , 'transactionDate' : [ '' , Validators . required ] , 'paymentTypeId' : [ '' ] , 'accountNumber' : [ '' ] , 'checkNumber' : [ '' ] , 'routingCode' : [ '' ] , 'receiptNumber' : [ '' ] , 'bankNumber' : [ '' ] , 'comments' : [ '' ] } ) ; } /**
     * Sets the affected gl entry form array.
     */ setAffectedGLEntryForm ( ) { this . frequentPostingsForm . get ( 'accountingRule' ) . valueChanges . subscribe ( accountingRule => { while ( this . debits . length ) { this . debits . removeAt ( 0 ) ; } while ( this . credits . length ) { this . credits . removeAt ( 0 ) ; } this . allowMultipleDebitEntries = accountingRule . allowMultipleDebitEntries ; this . allowMultipleCreditEntries = accountingRule . allowMultipleCreditEntries ; this . debitAccountData = accountingRule . debitAccounts ; this . creditAccountData = accountingRule . creditAccounts ; this . addAffectedGLEntry ( this . debits ) ; this . addAffectedGLEntry ( this . credits ) ; } ) ; } returns { FormGroup } createAffectedGLEntryForm ( ) { return this . formBuilder . group ( { 'glAccountId' : [ '' , Validators . required ] , 'amount' : [ '' , Validators . required ] } ) ; } returns { FormArray } get debits ( ) : FormArray { return this . frequentPostingsForm . get ( 'debits' ) as FormArray ; } returns { FormArray } get credits ( ) : FormArray { return this . frequentPostingsForm . get ( 'credits' ) as FormArray ; } param { FormArray } affectedGLEntryFormArray addAffectedGLEntry ( affectedGLEntryFormArray ) { affectedGLEntryFormArray . push ( this . createAffectedGLEntryForm ( ) ) ; } param { FormArray } affectedGLEntryFormArray param { number } index removeAffectedGLEntry ( affectedGLEntryFormArray , index ) { affectedGLEntryFormArray . removeAt ( index ) ; } /**
     * Submits the frequent postings form and creates journal entry,
     * if successful redirects to view created transaction.
     */ submit ( ) { const journalEntry = this . frequentPostingsForm . value ; journalEntry . accountingRule = journalEntry . accountingRule . id ; journalEntry . locale = this . settingsService . language . code ; journalEntry . dateFormat = this . settingsService . dateFormat ; if ( journalEntry . transactionDate instanceof Date ) { let day = journalEntry . transactionDate . getDate ( ) ; let month = journalEntry . transactionDate . getMonth ( ) + 1 ; const year = journalEntry . transactionDate . getFullYear ( ) ; if ( day < 10 ) { day = `0${ day }` ; } if ( month < 10 ) { month = `0${ month }` ; } journalEntry . transactionDate = `${ year }-${ month }-${ day }` ; } this . accountingService . createJournalEntry ( journalEntry ) . subscribe ( response => { this . router . navigate ( [ '../transactions/view' , response . transactionId ] , { relativeTo : this . route } ) ; } ) ; } }