]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/acq/funds/fund-rollover-dialog.component.ts
LP#1904244: Angular funds interface
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / acq / funds / fund-rollover-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 {OrgService} from '@eg/core/org.service';
16 import {Observable} from 'rxjs';
17 import {map} from 'rxjs/operators';
18 import {ProgressInlineComponent} from '@eg/share/dialog/progress-inline.component';
19 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
20
21 @Component({
22   selector: 'eg-fund-rollover-dialog',
23   templateUrl: './fund-rollover-dialog.component.html'
24 })
25
26 export class FundRolloverDialogComponent
27   extends DialogComponent implements OnInit {
28
29     doneLoading = false;
30
31     @Input() contextOrgId: number;
32
33     @ViewChild('successString', { static: true }) successString: StringComponent;
34     @ViewChild('updateFailedString', { static: false }) updateFailedString: StringComponent;
35     @ViewChild('rolloverProgress', { static: true })
36         private rolloverProgress: ProgressInlineComponent;
37
38     includeDescendants = false;
39     doCloseout = false;
40     showEncumbOnly = false;
41     limitToEncumbrances = false;
42     dryRun = true;
43     contextOrg: IdlObject;
44     isProcessing = false;
45     showResults = false;
46     years: ComboboxEntry[];
47     year: number;
48
49     count: number;
50     amount_rolled: number;
51
52     constructor(
53         private idl: IdlService,
54         private evt: EventService,
55         private net: NetService,
56         private auth: AuthService,
57         private pcrud: PcrudService,
58         private perm: PermService,
59         private toast: ToastService,
60         private org: OrgService,
61         private modal: NgbModal
62     ) {
63         super(modal);
64     }
65
66     ngOnInit() {
67         this.onOpen$.subscribe(() => this._initDialog());
68         this.doneLoading = true;
69     }
70
71     private _initDialog() {
72         this.contextOrg = this.org.get(this.contextOrgId);
73         this.includeDescendants = false;
74         this.doCloseout = false;
75         this.limitToEncumbrances = false;
76         this.showResults = false;
77         this.dryRun = true;
78         this.years = null;
79         this.year = null;
80         let maxYear = 0;
81         this.net.request(
82             'open-ils.acq',
83             'open-ils.acq.fund.org.years.retrieve',
84             this.auth.token(),
85             {},
86             { limit_perm: 'VIEW_FUND' }
87         ).subscribe(
88             years => {
89                 this.years = years.map(y => {
90                     if (maxYear < y) { maxYear = y; }
91                     return { id: y, label: y };
92                 });
93                 this.year = maxYear;
94             }
95         );
96         this.showEncumbOnly = false;
97         this.org.settings('acq.fund.allow_rollover_without_money', this.contextOrgId).then((ous) => {
98             this.showEncumbOnly = ous['acq.fund.allow_rollover_without_money'];
99         });
100     }
101
102     rollover() {
103         this.isProcessing = true;
104
105         const rolloverResponses: any = [];
106
107         let method = 'open-ils.acq.fiscal_rollover';
108         if (this.doCloseout) {
109             method += '.combined';
110         } else {
111             method += '.propagate';
112         }
113         if (this.dryRun) { method += '.dry_run'; }
114
115         this.count = 0;
116         this.amount_rolled = 0;
117
118         this.net.request(
119             'open-ils.acq',
120             method,
121             this.auth.token(),
122             this.year,
123             this.contextOrgId,
124             this.includeDescendants,
125             { encumb_only : this.limitToEncumbrances }
126         ).subscribe(
127             r => {
128                 rolloverResponses.push(r.fund);
129                 this.count++;
130                 this.amount_rolled += Number(r.rollover_amount);
131             },
132             err => {},
133             () => {
134                 this.isProcessing = false;
135                 this.showResults = true;
136                 if (!this.dryRun) {
137                     this.successString.current()
138                         .then(str => this.toast.success(str));
139                 }
140                 // note that we're intentionally not closing the dialog
141                 // so that user can view the results
142             }
143         );
144     }
145
146 }