]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/acq/funds/fund-transfer-dialog.component.ts
LP#1904244: Angular funds interface
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / acq / funds / fund-transfer-dialog.component.ts
1 import {Component, Input, ViewChild, TemplateRef, OnInit} from '@angular/core';
2 import {DialogComponent} from '@eg/share/dialog/dialog.component';
3 import {NgForm} from '@angular/forms';
4 import {IdlService, IdlObject} from '@eg/core/idl.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 {GridDataSource} from '@eg/share/grid/grid';
10 import {Pager} from '@eg/share/util/pager';
11 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
12 import {StringComponent} from '@eg/share/string/string.component';
13 import {ToastService} from '@eg/share/toast/toast.service';
14 import {PermService} from '@eg/core/perm.service';
15 import {ComboboxComponent} from '@eg/share/combobox/combobox.component';
16 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
17 import {Observable} from 'rxjs';
18 import {map} from 'rxjs/operators';
19
20 @Component({
21   selector: 'eg-fund-transfer-dialog',
22   templateUrl: './fund-transfer-dialog.component.html'
23 })
24
25 export class FundTransferDialogComponent
26   extends DialogComponent implements OnInit {
27
28     @Input() sourceFund: IdlObject;
29     doneLoading = false;
30
31     @ViewChild('successString', { static: true }) successString: StringComponent;
32     @ViewChild('updateFailedString', { static: false }) updateFailedString: StringComponent;
33     @ViewChild('fundSelector', { static: false }) tagSelector: ComboboxComponent;
34
35     fundDataSource: (term: string) => Observable<ComboboxEntry>;
36     destFund: ComboboxEntry = null;
37     sourceAmount = null;
38     note = null;
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 perm: PermService,
47         private toast: ToastService,
48         private modal: NgbModal
49     ) {
50         super(modal);
51     }
52
53     ngOnInit() {
54         this.destFund = null;
55         this.onOpen$.subscribe(() => this._initRecord());
56         this.fundDataSource = term => {
57             const field = 'code';
58             const args = {};
59             const extra_args = { order_by : {} };
60             args[field] = {'ilike': `%${term}%`}; // could -or search on label
61             args['active'] = 't';
62             extra_args['order_by']['acqf'] = field;
63             extra_args['limit'] = 100;
64             extra_args['flesh'] = 1;
65             const flesh_fields: Object = {};
66             flesh_fields['acqf'] = ['org'];
67             extra_args['flesh_fields'] = flesh_fields;
68             return this.pcrud.search('acqf', args, extra_args).pipe(map(data => {
69                 return {
70                     id: data.id(),
71                     label: data.code()
72                            + ' (' + data.year() + ')'
73                            + ' (' + data.org().shortname() + ')',
74                     fm: data
75                 };
76             }));
77         };
78     }
79
80     private _initRecord() {
81         this.doneLoading = false;
82         this.destFund = { id: null }; // destFund is a ComoboxEntry, so
83                                       // we need to clear it like this
84         this.sourceAmount = null;
85         this.note = null;
86         this.doneLoading = true;
87     }
88
89     transfer() {
90         this.net.request(
91             'open-ils.acq',
92             'open-ils.acq.funds.transfer_money',
93             this.auth.token(),
94             this.sourceFund.id(),
95             this.sourceAmount,
96             this.destFund.id,
97             null,
98             this.note
99         ).subscribe(
100             res => {
101                 this.successString.current()
102                     .then(str => this.toast.success(str));
103                 this.close(true);
104             },
105             res => {
106                 this.updateFailedString.current()
107                     .then(str => this.toast.danger(str));
108                 this.close(false);
109             }
110         );
111     }
112
113 }