]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/acq/funds/funding-source-transactions-dialog.component.ts
LP#1904244: Angular funds interface
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / acq / funds / funding-source-transactions-dialog.component.ts
1 import {Component, Input, ViewChild, TemplateRef, OnInit} from '@angular/core';
2 import {DialogComponent} from '@eg/share/dialog/dialog.component';
3 import {IdlService, IdlObject} from '@eg/core/idl.service';
4 import {FormatService} from '@eg/core/format.service';
5 import {EventService} from '@eg/core/event.service';
6 import {NetService} from '@eg/core/net.service';
7 import {AuthService} from '@eg/core/auth.service';
8 import {PcrudService} from '@eg/core/pcrud.service';
9 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
10 import {GridDataSource, GridCellTextGenerator} from '@eg/share/grid/grid';
11 import {Pager} from '@eg/share/util/pager';
12 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
13 import {StringComponent} from '@eg/share/string/string.component';
14 import {ToastService} from '@eg/share/toast/toast.service';
15 import {GridComponent} from '@eg/share/grid/grid.component';
16 import {OrgService} from '@eg/core/org.service';
17
18 @Component({
19   selector: 'eg-funding-source-transactions-dialog',
20   templateUrl: './funding-source-transactions-dialog.component.html'
21 })
22
23 export class FundingSourceTransactionsDialogComponent
24   extends DialogComponent implements OnInit {
25
26     @Input() fundingSourceId: number;
27     @Input() activeTab = 'credits';
28     fundingSource: IdlObject;
29     idlDef: any;
30     fieldOrder: any;
31     acqfaDataSource: GridDataSource;
32     acqfscredDataSource: GridDataSource;
33     cellTextGenerator: GridCellTextGenerator;
34
35     @ViewChild('applyCreditDialog', { static: true }) applyCreditDialog: FmRecordEditorComponent;
36     @ViewChild('allocateToFundDialog', { static: true }) allocateToFundDialog: FmRecordEditorComponent;
37     @ViewChild('successString', { static: true }) successString: StringComponent;
38     @ViewChild('updateFailedString', { static: false }) updateFailedString: StringComponent;
39
40     constructor(
41         private idl: IdlService,
42         private evt: EventService,
43         private net: NetService,
44         private auth: AuthService,
45         private pcrud: PcrudService,
46         private org: OrgService,
47         private format: FormatService,
48         private toast: ToastService,
49         private modal: NgbModal
50     ) {
51         super(modal);
52     }
53
54     ngOnInit() {
55         this.cellTextGenerator = {
56             fund: row => {
57                 return row.code() + ' (' + row.year() + ') (' +
58                     this.getOrgShortname(row.org()) + ')';
59             }
60         };
61         this.fundingSource = null;
62         this.onOpen$.subscribe(() => this._initRecord());
63         this.idlDef = this.idl.classes['acqfs'];
64         this.fieldOrder = 'name,code,year,org,active,currency_type,balance_stop_percentage,balance_warning_percentage,propagate,rollover';
65     }
66
67     private _initRecord() {
68         this.fundingSource = null;
69         this.acqfaDataSource = this._getDataSource('acqfa', 'create_time DESC');
70         this.acqfscredDataSource = this._getDataSource('acqfscred', 'effective_date DESC');
71         this.pcrud.retrieve('acqfs', this.fundingSourceId, {}
72         ).subscribe(res => this.fundingSource = res);
73     }
74
75     _getDataSource(idlClass: string, sortField: string): GridDataSource {
76         const gridSource = new GridDataSource();
77
78         gridSource.getRows = (pager: Pager, sort: any[]) => {
79             const orderBy: any = {};
80             if (sort.length) {
81                 // Sort specified from grid
82                 orderBy[idlClass] = sort[0].name + ' ' + sort[0].dir;
83             } else if (sortField) {
84                 // Default sort field
85                 orderBy[idlClass] = sortField;
86             }
87
88             const searchOps = {
89                 offset: pager.offset,
90                 limit: pager.limit,
91                 order_by: orderBy,
92             };
93             const reqOps = {
94                 fleshSelectors: true,
95             };
96
97             const search: any = new Array();
98             search.push({ funding_source: this.fundingSourceId });
99
100             Object.keys(gridSource.filters).forEach(key => {
101                 Object.keys(gridSource.filters[key]).forEach(key2 => {
102                     search.push(gridSource.filters[key][key2]);
103                 });
104             });
105
106             return this.pcrud.search(
107                 idlClass, search, searchOps, reqOps);
108         };
109
110         return gridSource;
111     }
112
113     formatCurrency(value: any) {
114         return this.format.transform({
115             value: value,
116             datatype: 'money'
117         });
118     }
119
120     createCredit(grid: GridComponent) {
121         const credit = this.idl.create('acqfscred');
122         credit.funding_source(this.fundingSourceId);
123         this.applyCreditDialog.defaultNewRecord = credit;
124         this.applyCreditDialog.mode = 'create';
125         this.applyCreditDialog.hiddenFieldsList = ['id', 'funding_source'];
126         this.applyCreditDialog.fieldOrder = 'amount,note,effective_date,deadline_date';
127         this.applyCreditDialog.open().subscribe(
128              result => {
129                 this.successString.current()
130                     .then(str => this.toast.success(str));
131                 grid.reload();
132             },
133             error => {
134                 this.updateFailedString.current()
135                     .then(str => this.toast.danger(str));
136             }
137         );
138     }
139
140     allocateToFund(grid: GridComponent) {
141         const allocation = this.idl.create('acqfa');
142         allocation.funding_source(this.fundingSourceId);
143         allocation.allocator(this.auth.user().id());
144         this.allocateToFundDialog.defaultNewRecord = allocation;
145         this.allocateToFundDialog.mode = 'create';
146
147         this.allocateToFundDialog.hiddenFieldsList = ['id', 'funding_source', 'allocator', 'create_time'];
148         this.allocateToFundDialog.fieldOrder = 'fund,amount,note';
149         this.allocateToFundDialog.open().subscribe(
150              result => {
151                 this.successString.current()
152                     .then(str => this.toast.success(str));
153                 grid.reload();
154             },
155             error => {
156                 this.updateFailedString.current()
157                     .then(str => this.toast.danger(str));
158             }
159         );
160     }
161
162     getOrgShortname(ou: any) {
163         if (typeof ou === 'object') {
164             return ou.shortname();
165         } else {
166             return this.org.get(ou).shortname();
167         }
168     }
169 }