1 import {Component, Input, ViewChild, OnInit, AfterViewInit} from '@angular/core';
2 import {Location} from '@angular/common';
3 import {FormatService} from '@eg/core/format.service';
4 import {GridDataSource, GridCellTextGenerator} from '@eg/share/grid/grid';
5 import {GridComponent} from '@eg/share/grid/grid.component';
6 import {AdminPageComponent} from '@eg/staff/share/admin-page/admin-page.component';
7 import {Pager} from '@eg/share/util/pager';
8 import {ActivatedRoute} from '@angular/router';
9 import {IdlService, IdlObject} from '@eg/core/idl.service';
10 import {ToastService} from '@eg/share/toast/toast.service';
11 import {PcrudService} from '@eg/core/pcrud.service';
12 import {OrgService} from '@eg/core/org.service';
13 import {PermService} from '@eg/core/perm.service';
14 import {AuthService} from '@eg/core/auth.service';
15 import {NetService} from '@eg/core/net.service';
16 import {StringComponent} from '@eg/share/string/string.component';
17 import {FundDetailsDialogComponent} from './fund-details-dialog.component';
18 import {FundRolloverDialogComponent} from './fund-rollover-dialog.component';
21 selector: 'eg-funds-manager',
22 templateUrl: './funds-manager.component.html'
25 export class FundsManagerComponent extends AdminPageComponent implements OnInit, AfterViewInit {
29 @Input() startId: number;
31 @ViewChild('fundDetailsDialog', { static: false }) fundDetailsDialog: FundDetailsDialogComponent;
32 @ViewChild('fundRolloverDialog', { static: false }) fundRolloverDialog: FundRolloverDialogComponent;
33 @ViewChild('grid', { static: true }) grid: GridComponent;
35 cellTextGenerator: GridCellTextGenerator;
39 route: ActivatedRoute,
41 format: FormatService,
47 private perm2: PermService, // need copy because perm is private to base
50 private net: NetService
52 super(route, ngLocation, format, idl, org, auth, pcrud, perm, toast);
53 this.dataSource = new GridDataSource();
57 this.cellTextGenerator = {
58 name: row => row.name()
60 this.checkRolloverPerms();
61 this.fieldOrder = 'name,code,year,org,active,currency_type,balance_stop_percentage,balance_warning_percentage,propagate,rollover';
64 min: new Date().getFullYear() - 10,
65 max: new Date().getFullYear() + 10
68 this.defaultNewRecord = this.idl.create('acqf');
69 this.defaultNewRecord.active(true);
70 this.defaultNewRecord.org(this.auth.user().ws_ou());
72 this.dataSource.getRows = (pager: Pager, sort: any[]) => {
73 const orderBy: any = {};
75 // Sort specified from grid
76 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
77 } else if (this.sortField) {
79 orderBy[this.idlClass] = this.sortField;
101 if (!this.contextOrg && !Object.keys(this.dataSource.filters).length) {
102 // No org filter -- fetch all rows
103 return this.pcrud.retrieveAll(
104 this.idlClass, searchOps, reqOps);
107 const search: any = new Array();
108 const orgFilter: any = {};
110 if (this.orgField && (this.searchOrgs || this.contextOrg)) {
111 orgFilter[this.orgField] =
112 this.searchOrgs.orgIds || [this.contextOrg.id()];
113 search.push(orgFilter);
116 Object.keys(this.dataSource.filters).forEach(key => {
117 Object.keys(this.dataSource.filters[key]).forEach(key2 => {
118 search.push(this.dataSource.filters[key][key2]);
122 return this.pcrud.search(
123 this.idlClass, search, searchOps, reqOps);
128 this.classLabel = this.idlClassDef.label;
129 this.includeOrgDescendants = true;
134 this.pcrud.retrieve('acqf', this.startId).subscribe(
135 acqf => this.openFundDetailsDialog([acqf]),
137 () => this.startId = null
142 checkRolloverPerms() {
143 this.canRollover = false;
145 this.perm2.hasWorkPermAt(['ADMIN_FUND'], true).then(permMap => {
146 Object.keys(permMap).forEach(key => {
147 if (permMap[key].length > 0) {
148 this.canRollover = true;
154 openFundDetailsDialog(rows: IdlObject[]) {
155 if (rows.length > 0) {
156 this.fundDetailsDialog.fundId = rows[0].id();
157 this.fundDetailsDialog.open({size: 'xl'}).subscribe(
158 result => this.grid.reload(),
159 error => this.grid.reload(),
160 () => this.grid.reload()
165 getDefaultYear(): string {
166 return new Date().getFullYear().toString();
170 this.fundRolloverDialog.contextOrgId = this.searchOrgs.primaryOrgId;
171 this.fundRolloverDialog.open({size: 'lg'}).subscribe(
174 () => this.grid.reload()