/** Angular Imports */ import { Component , OnInit } from '@angular/core' ; import { ActivatedRoute , Router } from '@angular/router' ; import { SelectionModel } from '@angular/cdk/collections' ; import * as _ from 'lodash' ; import { DatePipe } from '@angular/common' ; import { MatTableDataSource } from '@angular/material/table' ; import { MatDialog } from '@angular/material/dialog' ; /** Dialog Imports */ import { ConfirmationDialogComponent } from 'app/shared/confirmation-dialog/confirmation-dialog.component' ; /** Custom Services */ import { TasksService } from '../../tasks.service' ; import { SettingsService } from 'app/settings/settings.service' ; @ Component ( { selector : 'mifosx-loan-approval' , templateUrl : './loan-approval.component.html' , styleUrls : [ './loan-approval.component.scss' ] } ) export class LoanApprovalComponent { /** Offices Data */ offices : any ; /** Loans Data */ loans : any ; /** Checks whether to show data or not */ showData = false ; /** Data source for loans approval table. */ dataSource : MatTableDataSource < any > ; /** Row Selection Data */ selection : SelectionModel < any > ; /** Map data */ idToNodeMap = { } ; /** Grouped Office Data */ officesArray : any [ ] ; /** List of Requests */ batchRequests : any [ ] ; /** Displayed Columns */ displayedColumns : string [ ] = [ 'select' , 'clientName' , 'loan' , 'amount' , 'loanPurpose' ] ; param { ActivatedRoute } route param { Dialog } dialog param { DatePipe } datePipe param { router } router param { SettingsService } settingsService param { TasksService } tasksService constructor ( private route , private dialog , private datePipe , private router , private settingsService , private tasksService ) { this . route . data . subscribe ( ( data ) => { this . offices = data . officesData ; this . loans = data . loansData . pageItems ; this . setOfficeData ( ) ; } ) ; } /** Group Office Data */ setOfficeData ( ) { this . offices . forEach ( ( officeEle ) => { officeEle . loans = [ ] ; this . idToNodeMap [ officeEle . id ] = officeEle ; } ) ; this . loans . forEach ( ( loanEle ) => { if ( loanEle . status . pendingApproval ) { let tempOffice = { } ; if ( loanEle . clientOfficeId ) { tempOffice = this . idToNodeMap [ loanEle . clientOfficeId ] ; tempOffice [ 'loans' ] . push ( loanEle ) ; } else { if ( loanEle . group ) { tempOffice = this . idToNodeMap [ loanEle . group . officeId ] ; tempOffice [ 'loans' ] . push ( loanEle ) ; } } } } ) ; const finalArray = [ ] ; this . offices . forEach ( ( officeEle ) => { if ( officeEle . loans && officeEle . loans . length > 0 ) { this . showData = true ; finalArray . push ( officeEle ) ; } } ) ; this . officesArray = finalArray ; this . dataSource = new MatTableDataSource ( this . officesArray ) ; this . selection = new SelectionModel ( true , [ ] ) ; } /** Whether the number of selected elements matches the total number of rows. */ isAllSelected ( dataSource2 ) { if ( dataSource2 ) { const numSelected = this . selection . selected ; return _ . difference ( dataSource2 , numSelected ) . length === 0 ; } } /** Selects all rows if they are not all selected; otherwise clear selection. */ masterToggle ( dataSource3 ) { if ( dataSource3 ) { this . isAllSelected ( dataSource3 ) ? dataSource3 . forEach ( ( row ) => this . selection . deselect ( row ) ) : dataSource3 . forEach ( ( row ) => this . selection . select ( row ) ) ; } } /** The label for the checkbox on the passed row */ checkboxLabel ( row ? ) { if ( ! row ) { return `${ this . isAllSelected ( row ) ? 'select' : 'deselect' } all` ; } return `${ this . selection . isSelected ( row ) ? 'deselect' : 'select' } row ${ row . position + 1 }` ; } approveLoan ( ) { const approveLoanDialogRef = this . dialog . open ( ConfirmationDialogComponent , { data : { heading : 'Approve Loan' , dialogContext : 'Are you sure you want to Approve Loan' } } ) ; approveLoanDialogRef . afterClosed ( ) . subscribe ( ( response ) => { if ( response . confirm ) { this . bulkLoanApproval ( ) ; } } ) ; } bulkLoanApproval ( ) { const dateFormat = this . settingsService . dateFormat ; const approvedOnDate = this . datePipe . transform ( new Date ( ) , dateFormat ) ; const locale = this . settingsService . language . code ; const formData = { dateFormat , approvedOnDate , locale } ; const selectedAccounts = this . selection . selected . length ; const listSelectedAccounts = this . selection . selected ; let approvedAccounts = 0 ; this . batchRequests = [ ] ; let reqId = 1 ; listSelectedAccounts . forEach ( ( element ) => { const url = 'loans/' + element . id + '?command=approve' ; const bodyData = JSON . stringify ( formData ) ; const batchData = { requestId : reqId ++ , relativeUrl : url , method : 'POST' , body : bodyData } ; this . batchRequests . push ( batchData ) ; } ) ; this . tasksService . submitBatchData ( this . batchRequests ) . subscribe ( ( response ) => { response . forEach ( ( responseEle ) => { if ( responseEle . statusCode = '200' ) { approvedAccounts ++ ; responseEle . body = JSON . parse ( responseEle . body ) ; if ( selectedAccounts === approvedAccounts ) { this . loanResource ( ) ; } } } ) ; this . reload ( ) ; } ) ; } applyFilter ( filterValue = '' ) { this . dataSource . filter = filterValue . trim ( ) . toLowerCase ( ) ; } loanResource ( ) { this . tasksService . getAllLoans ( ) . subscribe ( ( response ) => { this . loans = response . pageItems ; this . loans = this . loans . filter ( ( account ) => { return ( account . status . waitingForDisbursal === true ) ; } ) ; this . dataSource = new MatTableDataSource ( this . loans ) ; this . selection = new SelectionModel ( true , [ ] ) ; } ) ; } /**
     * Refetches data for the component
     * TODO: Replace by a custom reload component instead of hard-coded back-routing.
     */ reload ( ) { const url = this . router . url ; this . router . navigateByUrl ( `/checker-inbox-and-tasks` , { skipLocationChange : true } ) . then ( ( ) => this . router . navigate ( [ url ] ) ) ; } }