]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/acq/po/po.service.ts
LP1929741 ACQ Selection List & PO Angluar Port
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / acq / po / po.service.ts
1 import {Injectable, EventEmitter} from '@angular/core';
2 import {Observable, from} from 'rxjs';
3 import {switchMap, map, tap, merge} from 'rxjs/operators';
4 import {IdlObject, IdlService} 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
10 @Injectable()
11 export class PoService {
12
13     currentPo: IdlObject;
14
15     poRetrieved: EventEmitter<IdlObject> = new EventEmitter<IdlObject>();
16
17     constructor(
18         private evt: EventService,
19         private net: NetService,
20         private auth: AuthService
21     ) {}
22
23     getFleshedPo(id: number, fleshMore?: any, noCache?: boolean): Promise<IdlObject> {
24
25         if (!noCache) {
26             if (this.currentPo && id === this.currentPo.id()) {
27                 // Set poService.currentPo = null to bypass the cache
28                 return Promise.resolve(this.currentPo);
29             }
30         }
31
32         const flesh = Object.assign({
33             flesh_provider: true,
34             flesh_notes: true,
35             flesh_po_items: true,
36             flesh_price_summary: true,
37             flesh_lineitem_count: true
38         }, fleshMore || {});
39
40         return this.net.request(
41             'open-ils.acq',
42             'open-ils.acq.purchase_order.retrieve',
43             this.auth.token(), id, flesh
44         ).toPromise().then(po => {
45
46             const evt = this.evt.parse(po);
47             if (evt) { return Promise.reject(evt + ''); }
48
49             if (!noCache) { this.currentPo = po; }
50
51             this.poRetrieved.emit(po);
52             return po;
53         });
54     }
55
56     // Fetch the PO again (with less fleshing) and update the
57     // order summary totals our main fully-fleshed PO.
58     refreshOrderSummary(): Promise<any> {
59
60         return this.net.request('open-ils.acq',
61             'open-ils.acq.purchase_order.retrieve.authoritative',
62             this.auth.token(), this.currentPo.id(),
63             {flesh_price_summary: true}
64
65         ).toPromise().then(po => {
66
67             this.currentPo.amount_encumbered(po.amount_encumbered());
68             this.currentPo.amount_spent(po.amount_spent());
69             this.currentPo.amount_estimated(po.amount_estimated());
70         });
71     }
72 }
73
74