/** Angular Imports */ import { Component , OnInit , ViewChild , AfterViewInit } from '@angular/core' ; import { ActivatedRoute , Router } from '@angular/router' ; import { FormControl } from '@angular/forms' ; import { MatPaginator } from '@angular/material/paginator' ; import { MatSort } from '@angular/material/sort' ; import { MatTableDataSource } from '@angular/material/table' ; /** rxjs Imports */ import { startWith , map , debounceTime , distinctUntilChanged , tap } from 'rxjs/operators' ; /** Custom Services */ import { AccountingService } from '../../accounting.service' ; /**
 * View provisioning entry component.
 */ @ Component ( { selector : 'mifosx-view-provisioning-entry' , templateUrl : './view-provisioning-entry.component.html' , styleUrls : [ './view-provisioning-entry.component.scss' ] } ) export class ViewProvisioningEntryComponent implements OnInit , AfterViewInit { /** Provisioning entry id. */ provisioningEntryId : string ; /** Provisioning entry. */ provisioningEntry : any ; /** Provisioning entry entries. */ provisioningEntryEntries : any ; /** Office name filter form control. */ officeName = new FormControl ( ) ; /** Office data. */ officeData : any ; /** Filtered office data for autocomplete. */ filteredOfficeData : any ; /** Loan product filter form control. */ loanProduct = new FormControl ( ) ; /** Loan product data. */ loanProductData : any ; /** Filtered loan product data for autocomplete. */ filteredLoanProductData : any ; /** Provisioning category filter form control. */ provisioningCategory = new FormControl ( ) ; /** Provisioning category data. */ provisioningCategoryData : any ; /** Filtered provisioning category data for autocomplete. */ filteredProvisioningCategoryData : any ; /** Columns to be displayed in provisioning entry entries table. */ displayedColumns : string [ ] = [ 'officeName' , 'productName' , 'currencyCode' , 'categoryName' , 'amountreserved' , 'liabilityAccountName' , 'expenseAccountName' ] ; /** Data source for provisioning entry entries table. */ dataSource : MatTableDataSource < any > ; /** Provisioning entry entries filter. */ filterValue : any = { officeName : '' , productName : '' , categoryName : '' } ; /** Paginator for provisioning entry entries table. */ @ ViewChild ( MatPaginator , { static : true } ) paginator : MatPaginator ; /** Sorter for provisioning entry entries table. */ @ ViewChild ( MatSort , { static : true } ) sort : MatSort ; param { AccountingService } accountingService param { ActivatedRoute } route param { Router } router constructor ( private accountingService , private route , private router ) { this . route . data . subscribe ( ( data ) => { this . provisioningEntryId = data . provisioningEntry . id ; this . provisioningEntry = data . provisioningEntry ; this . provisioningEntryEntries = data . provisioningEntryEntries ; this . officeData = data . offices ; this . loanProductData = data . loanProducts ; this . provisioningCategoryData = data . provisiningCategories ; } ) ; } /**
     * Sets filtered offices, loan products and provisioning categories for autocomplete
     * and provisioning entry entries table.
     */ ngOnInit ( ) { this . setProvisioningEntryEntries ( ) ; this . setFilteredOffices ( ) ; this . setFilteredLoanProducts ( ) ; this . setFilteredProvisioningCategories ( ) ; } /**
     * Subscribes to all search filters:
     * Office Name, Loan Product, Provisioning categories.
     */ ngAfterViewInit ( ) { this . officeName . valueChanges . pipe ( map ( value => value . toLowerCase ( ) ) , debounceTime ( 500 ) , distinctUntilChanged ( ) , tap ( ( filterValue ) => { this . applyFilter ( filterValue , 'officeName' ) ; } ) ) . subscribe ( ) ; this . loanProduct . valueChanges . pipe ( map ( value => value . toLowerCase ( ) ) , debounceTime ( 500 ) , distinctUntilChanged ( ) , tap ( ( filterValue ) => { this . applyFilter ( filterValue , 'productName' ) ; } ) ) . subscribe ( ) ; this . provisioningCategory . valueChanges . pipe ( map ( value => value . toLowerCase ( ) ) , debounceTime ( 500 ) , distinctUntilChanged ( ) , tap ( ( filterValue ) => { this . applyFilter ( filterValue , 'categoryName' ) ; } ) ) . subscribe ( ) ; } param { any } data param { any } filterValue filterPredicate ( data , filterValue ) { return data . officeName . toLowerCase ( ) . indexOf ( filterValue [ 'officeName' ] ) !== - 1 && data . productName . toLowerCase ( ) . indexOf ( filterValue [ 'productName' ] ) !== - 1 && data . categoryName . toLowerCase ( ) . indexOf ( filterValue [ 'categoryName' ] ) !== - 1 ; } /**
     * Initializes the data source, paginator, sorter and filter predicate for
     * provisioning entry entries table.
     */ setProvisioningEntryEntries ( ) { this . dataSource = new MatTableDataSource ( this . provisioningEntryEntries . pageItems ) ; this . dataSource . paginator = this . paginator ; this . dataSource . sort = this . sort ; this . dataSource . filterPredicate = this . filterPredicate ; } param { string } filterValue param { string } property applyFilter ( filterValue , property ) { this . filterValue [ property ] = filterValue ; this . dataSource . filter = this . filterValue ; } /**
     * Sets filtered offices for autocomplete.
     */ setFilteredOffices ( ) { this . filteredOfficeData = this . officeName . valueChanges . pipe ( startWith ( '' ) , map ( ( office ) => typeof office === 'string' ? office : office . name ) , map ( ( officeName ) => officeName ? this . filterOfficeAutocompleteData ( officeName ) : this . officeData ) ) ; } /**
     * Sets filtered loan products for autocomplete.
     */ setFilteredLoanProducts ( ) { this . filteredLoanProductData = this . loanProduct . valueChanges . pipe ( startWith ( '' ) , map ( ( loanProduct ) => typeof loanProduct === 'string' ? loanProduct : loanProduct . name ) , map ( ( loanProductName ) => loanProductName ? this . filterLoanProductAutocompleteData ( loanProductName ) : this . loanProductData ) ) ; } /**
     * Sets filtered provisioning categories for autocomplete.
     */ setFilteredProvisioningCategories ( ) { this . filteredProvisioningCategoryData = this . provisioningCategory . valueChanges . pipe ( startWith ( '' ) , map ( ( provisioningCategory ) => typeof provisioningCategory === 'string' ? provisioningCategory : provisioningCategory . categoryName ) , map ( ( provisioningCategoryName ) => provisioningCategoryName ? this . filterProvisioningCategoryAutocompleteData ( provisioningCategoryName ) : this . provisioningCategoryData ) ) ; } param { string } officeName returns { any } private filterOfficeAutocompleteData ( officeName ) { return this . officeData . filter ( ( office ) => office . name . toLowerCase ( ) . includes ( officeName . toLowerCase ( ) ) ) ; } param { string } loanProductName returns { any } private filterLoanProductAutocompleteData ( loanProductName ) { return this . loanProductData . filter ( ( loanProduct ) => loanProduct . name . toLowerCase ( ) . includes ( loanProductName . toLocaleLowerCase ( ) ) ) ; } param { string } provisioningCategoryName returns { any } private filterProvisioningCategoryAutocompleteData ( provisioningCategoryName ) { return this . provisioningCategoryData . filter ( ( provisioningCategory ) => provisioningCategory . categoryName . toLowerCase ( ) . includes ( provisioningCategoryName . toLocaleLowerCase ( ) ) ) ; } /**
     * Creates provisioning journal entries
     * and redirects to created entries.
     */ createProvisioningJournalEntries ( ) { this . accountingService . createProvisioningJournalEntries ( this . provisioningEntryId ) . subscribe ( ( response ) => { this . router . navigate ( [ '../../journal-entries/view' , response . resourceId ] , { relativeTo : this . route } ) ; } ) ; } }