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';
11 export class PoService {
15 poRetrieved: EventEmitter<IdlObject> = new EventEmitter<IdlObject>();
18 private evt: EventService,
19 private net: NetService,
20 private auth: AuthService
23 getFleshedPo(id: number, fleshMore?: any, noCache?: boolean): Promise<IdlObject> {
26 if (this.currentPo && id === this.currentPo.id()) {
27 // Set poService.currentPo = null to bypass the cache
28 return Promise.resolve(this.currentPo);
32 const flesh = Object.assign({
36 flesh_price_summary: true,
37 flesh_lineitem_count: true
40 return this.net.request(
42 'open-ils.acq.purchase_order.retrieve',
43 this.auth.token(), id, flesh
44 ).toPromise().then(po => {
46 const evt = this.evt.parse(po);
47 if (evt) { return Promise.reject(evt + ''); }
49 if (!noCache) { this.currentPo = po; }
51 this.poRetrieved.emit(po);
56 // Fetch the PO again (with less fleshing) and update the
57 // order summary totals our main fully-fleshed PO.
58 refreshOrderSummary(): Promise<any> {
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}
65 ).toPromise().then(po => {
67 this.currentPo.amount_encumbered(po.amount_encumbered());
68 this.currentPo.amount_spent(po.amount_spent());
69 this.currentPo.amount_estimated(po.amount_estimated());