/** Angular Imports */ import { Injectable } from '@angular/core' ; /** rxjs Imports */ import { BehaviorSubject } from 'rxjs' ; /** Custom Components */ import { GLAccountNode } from './gl-account-node.model' ; /**
 * GL Account tree service.
 */ @ Injectable ( { providedIn : 'root' } ) export class GlAccountTreeService { /** GL Account data. */ glAccountData : any ; /** Chart of accounts tree data behavior subject to represent chart of accounts tree nodes. */ treeDataChange = new BehaviorSubject < GLAccountNode [ ] > ( [ ] ) ; /**
     * Gets the chart of accounts tree nodes.
     */ get treeData ( ) : GLAccountNode [ ] { return this . treeDataChange . value ; } constructor ( ) { } param { any } glAccountData initialize ( glAccountData ) { const treeData = this . buildGLAccountTree ( glAccountData ) ; this . treeDataChange . next ( treeData ) ; } param { any } glAccountData returns { GLAccountNode [ ] } buildGLAccountTree ( glAccountData ) { const glAccountTree = [ ] ; glAccountTree . push ( new GLAccountNode ( 'GL ACCOUNTS' ) ) ; glAccountTree [ 0 ] . children . push ( new GLAccountNode ( 'ASSET' ) ) ; glAccountTree [ 0 ] . children . push ( new GLAccountNode ( 'EQUITY' ) ) ; glAccountTree [ 0 ] . children . push ( new GLAccountNode ( 'EXPENSE' ) ) ; glAccountTree [ 0 ] . children . push ( new GLAccountNode ( 'INCOME' ) ) ; glAccountTree [ 0 ] . children . push ( new GLAccountNode ( 'LIABILITY' ) ) ; glAccountData . sort ( ( glAccountOne , glAccountTwo ) => { if ( ! glAccountOne . parentId ) { glAccountOne . parentId = 0 ; } return glAccountOne . parentId - glAccountTwo . parentId ; } ) ; const glAccounts = [ ] ; for ( const glAccount of glAccountData ) { glAccounts [ glAccount . id ] = new GLAccountNode ( glAccount . name , glAccount . glCode , glAccount . type . value , glAccount . usage . value , glAccount . manualEntriesAllowed , glAccount . description ) ; } for ( const glAccount of glAccountData ) { if ( glAccount . parentId === 0 ) { if ( glAccount . type . value === 'ASSET' ) { glAccountTree [ 0 ] . children [ 0 ] . children . push ( glAccounts [ glAccount . id ] ) ; } else if ( glAccount . type . value === 'EQUITY' ) { glAccountTree [ 0 ] . children [ 1 ] . children . push ( glAccounts [ glAccount . id ] ) ; } else if ( glAccount . type . value === 'EXPENSE' ) { glAccountTree [ 0 ] . children [ 2 ] . children . push ( glAccounts [ glAccount . id ] ) ; } else if ( glAccount . type . value === 'INCOME' ) { glAccountTree [ 0 ] . children [ 3 ] . children . push ( glAccounts [ glAccount . id ] ) ; } else if ( glAccount . type . value === 'LIABILITY' ) { glAccountTree [ 0 ] . children [ 4 ] . children . push ( glAccounts [ glAccount . id ] ) ; } } else { glAccounts [ glAccount . parentId ] . children . push ( glAccounts [ glAccount . id ] ) ; } } return glAccountTree ; } }