]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/acq/funds/funds-manager.component.ts
LP#1904244: Angular funds interface
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / acq / funds / funds-manager.component.ts
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';
19
20 @Component({
21     selector: 'eg-funds-manager',
22     templateUrl: './funds-manager.component.html'
23 })
24
25 export class FundsManagerComponent extends AdminPageComponent implements OnInit, AfterViewInit {
26     idlClass = 'acqf';
27     classLabel: string;
28
29     @Input() startId: number;
30
31     @ViewChild('fundDetailsDialog', { static: false }) fundDetailsDialog: FundDetailsDialogComponent;
32     @ViewChild('fundRolloverDialog', { static: false }) fundRolloverDialog: FundRolloverDialogComponent;
33     @ViewChild('grid', { static: true }) grid: GridComponent;
34
35     cellTextGenerator: GridCellTextGenerator;
36     canRollover = false;
37
38     constructor(
39         route: ActivatedRoute,
40         ngLocation: Location,
41         format: FormatService,
42         idl: IdlService,
43         org: OrgService,
44         auth: AuthService,
45         pcrud: PcrudService,
46         perm: PermService,
47         private perm2: PermService, // need copy because perm is private to base
48                                     // component
49         toast: ToastService,
50         private net: NetService
51     ) {
52         super(route, ngLocation, format, idl, org, auth, pcrud, perm, toast);
53         this.dataSource = new GridDataSource();
54     }
55
56     ngOnInit() {
57         this.cellTextGenerator = {
58             name: row => row.name()
59         };
60         this.checkRolloverPerms();
61         this.fieldOrder = 'name,code,year,org,active,currency_type,balance_stop_percentage,balance_warning_percentage,propagate,rollover';
62         this.fieldOptions = {
63             year: {
64                 min: new Date().getFullYear() - 10,
65                 max: new Date().getFullYear() + 10
66             }
67         };
68         this.defaultNewRecord = this.idl.create('acqf');
69         this.defaultNewRecord.active(true);
70         this.defaultNewRecord.org(this.auth.user().ws_ou());
71
72         this.dataSource.getRows = (pager: Pager, sort: any[]) => {
73             const orderBy: any = {};
74             if (sort.length) {
75                 // Sort specified from grid
76                 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
77             } else if (this.sortField) {
78                 // Default sort field
79                 orderBy[this.idlClass] = this.sortField;
80             }
81
82             const searchOps = {
83                 offset: pager.offset,
84                 limit: pager.limit,
85                 order_by: orderBy,
86                 flesh_fields: {
87                     acqf: [
88                         'spent_balance',
89                         'combined_balance',
90                         'spent_total',
91                         'encumbrance_total',
92                         'debit_total',
93                         'allocation_total'
94                     ]
95                 }
96             };
97             const reqOps = {
98                 fleshSelectors: true,
99             };
100
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);
105             }
106
107             const search: any = new Array();
108             const orgFilter: any = {};
109
110             if (this.orgField && (this.searchOrgs || this.contextOrg)) {
111                 orgFilter[this.orgField] =
112                     this.searchOrgs.orgIds || [this.contextOrg.id()];
113                 search.push(orgFilter);
114             }
115
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]);
119                 });
120             });
121
122             return this.pcrud.search(
123                 this.idlClass, search, searchOps, reqOps);
124         };
125
126         super.ngOnInit();
127
128         this.classLabel = this.idlClassDef.label;
129         this.includeOrgDescendants = true;
130     }
131
132     ngAfterViewInit() {
133         if (this.startId) {
134             this.pcrud.retrieve('acqf', this.startId).subscribe(
135                 acqf => this.openFundDetailsDialog([acqf]),
136                 err => {},
137                 () => this.startId = null
138             );
139         }
140     }
141
142     checkRolloverPerms() {
143         this.canRollover = false;
144
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;
149                 }
150             });
151         });
152     }
153
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()
161             );
162         }
163     }
164
165     getDefaultYear(): string {
166         return new Date().getFullYear().toString();
167     }
168
169     doRollover() {
170         this.fundRolloverDialog.contextOrgId = this.searchOrgs.primaryOrgId;
171         this.fundRolloverDialog.open({size: 'lg'}).subscribe(
172             ok => {},
173             err => {},
174             () => this.grid.reload()
175         );
176     }
177 }