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';
19 selector: 'eg-funding-source-transactions-dialog',
20 templateUrl: './funding-source-transactions-dialog.component.html'
23 export class FundingSourceTransactionsDialogComponent
24 extends DialogComponent implements OnInit {
26 @Input() fundingSourceId: number;
27 @Input() activeTab = 'credits';
28 fundingSource: IdlObject;
31 acqfaDataSource: GridDataSource;
32 acqfscredDataSource: GridDataSource;
33 cellTextGenerator: GridCellTextGenerator;
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;
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
55 this.cellTextGenerator = {
57 return row.code() + ' (' + row.year() + ') (' +
58 this.getOrgShortname(row.org()) + ')';
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';
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);
75 _getDataSource(idlClass: string, sortField: string): GridDataSource {
76 const gridSource = new GridDataSource();
78 gridSource.getRows = (pager: Pager, sort: any[]) => {
79 const orderBy: any = {};
81 // Sort specified from grid
82 orderBy[idlClass] = sort[0].name + ' ' + sort[0].dir;
83 } else if (sortField) {
85 orderBy[idlClass] = sortField;
97 const search: any = new Array();
98 search.push({ funding_source: this.fundingSourceId });
100 Object.keys(gridSource.filters).forEach(key => {
101 Object.keys(gridSource.filters[key]).forEach(key2 => {
102 search.push(gridSource.filters[key][key2]);
106 return this.pcrud.search(
107 idlClass, search, searchOps, reqOps);
113 formatCurrency(value: any) {
114 return this.format.transform({
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(
129 this.successString.current()
130 .then(str => this.toast.success(str));
134 this.updateFailedString.current()
135 .then(str => this.toast.danger(str));
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';
147 this.allocateToFundDialog.hiddenFieldsList = ['id', 'funding_source', 'allocator', 'create_time'];
148 this.allocateToFundDialog.fieldOrder = 'fund,amount,note';
149 this.allocateToFundDialog.open().subscribe(
151 this.successString.current()
152 .then(str => this.toast.success(str));
156 this.updateFailedString.current()
157 .then(str => this.toast.danger(str));
162 getOrgShortname(ou: any) {
163 if (typeof ou === 'object') {
164 return ou.shortname();
166 return this.org.get(ou).shortname();