]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/acq/lineitem/worksheet.component.ts
LP1929741 ACQ Selection List & PO Angluar Port
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / acq / lineitem / worksheet.component.ts
1 import {Component, OnInit, AfterViewInit} from '@angular/core';
2 import {map, take} from 'rxjs/operators';
3 import {ActivatedRoute, ParamMap} from '@angular/router';
4 import {IdlObject} from '@eg/core/idl.service';
5 import {NetService} from '@eg/core/net.service';
6 import {AuthService} from '@eg/core/auth.service';
7 import {PcrudService} from '@eg/core/pcrud.service';
8 import {OrgService} from '@eg/core/org.service';
9 import {LineitemService} from './lineitem.service';
10 import {PrintService} from '@eg/share/print/print.service';
11
12 @Component({
13   templateUrl: 'worksheet.component.html'
14 })
15 export class LineitemWorksheetComponent implements OnInit, AfterViewInit {
16
17     outlet: Element;
18     lineitemId: number;
19     lineitem: IdlObject;
20     holdCount: number;
21     printing: boolean;
22     closing: boolean;
23
24     constructor(
25         private route: ActivatedRoute,
26         private org: OrgService,
27         private net: NetService,
28         private auth: AuthService,
29         private pcrud: PcrudService,
30         private printer: PrintService,
31         private liService: LineitemService
32     ) { }
33
34     ngOnInit() {
35
36         this.route.paramMap.subscribe((params: ParamMap) => {
37             const id = +params.get('lineitemId');
38             if (id !== this.lineitemId) {
39                 this.lineitemId = id;
40                 if (id) { this.load(); }
41             }
42         });
43     }
44
45     ngAfterViewInit() {
46         this.outlet = document.getElementById('worksheet-outlet');
47     }
48
49     load() {
50         if (!this.lineitemId) { return; }
51
52         this.net.request(
53             'open-ils.acq', 'open-ils.acq.lineitem.retrieve',
54             this.auth.token(), this.lineitemId, {
55                 flesh_attrs: true,
56                 flesh_notes: true,
57                 flesh_cancel_reason: true,
58                 flesh_li_details: true,
59                 flesh_fund: true,
60                 flesh_li_details_copy: true,
61                 flesh_li_details_location: true,
62                 flesh_li_details_receiver: true,
63                 distribution_formulas: true
64             }
65         ).toPromise()
66         .then(li => this.lineitem = li)
67         .then(_ => this.getRemainingData())
68         .then(_ => this.populatePreview());
69     }
70
71     getRemainingData(): Promise<any> {
72
73         // Flesh owning lib
74         this.lineitem.lineitem_details().forEach(lid => {
75             lid.owning_lib(this.org.get(lid.owning_lib()));
76         });
77
78         return this.net.request(
79             'open-ils.circ',
80             'open-ils.circ.bre.holds.count', this.lineitem.eg_bib_id()
81         ).toPromise().then(count => this.holdCount = count);
82
83     }
84
85     populatePreview(): Promise<any> {
86
87         return this.printer.compileRemoteTemplate({
88             templateName: 'lineitem_worksheet',
89             printContext: 'default',
90             contextData: {
91                 lineitem: this.lineitem,
92                 hold_count: this.holdCount
93             }
94
95         }).then(response => {
96             this.outlet.innerHTML = response.content;
97         });
98     }
99
100     printWorksheet(closeTab?: boolean) {
101
102         if (closeTab || this.closing) {
103             const sub: any = this.printer.printJobQueued$.subscribe(
104                 req => {
105                     if (req.templateName === 'lineitem_worksheet') {
106                         setTimeout(() => {
107                             window.close();
108                             sub.unsubscribe();
109                         }, 2000); // allow for a time cushion past queueing.
110                     }
111                 }
112             );
113         }
114
115         this.printer.print({
116             templateName: 'lineitem_worksheet',
117             contextData: {
118                 lineitem: this.lineitem,
119                 hold_count: this.holdCount
120             },
121             printContext: 'default'
122         });
123     }
124 }
125