LP1904036 Checkin grid shows circ user; routeTo updates
[evergreen-equinox.git] / Open-ILS / src / eg2 / src / app / staff / share / circ / route-dialog.component.ts
1 import {Component, OnInit, Output, Input, ViewChild, EventEmitter} from '@angular/core';
2 import {empty, of, from, Observable} from 'rxjs';
3 import {tap, concatMap} from 'rxjs/operators';
4 import {IdlService, IdlObject} from '@eg/core/idl.service';
5 import {PcrudService} from '@eg/core/pcrud.service';
6 import {OrgService} from '@eg/core/org.service';
7 import {CircService} from './circ.service';
8 import {StringComponent} from '@eg/share/string/string.component';
9 import {AlertDialogComponent} from '@eg/share/dialog/alert.component';
10 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
11 import {DialogComponent} from '@eg/share/dialog/dialog.component';
12 import {CheckinResult} from './circ.service';
13 import {ServerStoreService} from '@eg/core/server-store.service';
14 import {PrintService} from '@eg/share/print/print.service';
15
16 /** Route Item Dialog */
17
18 @Component({
19   templateUrl: 'route-dialog.component.html',
20   selector: 'eg-circ-route-dialog'
21 })
22 export class RouteDialogComponent extends DialogComponent {
23
24     checkin: CheckinResult;
25     noAutoPrint: {[template: string]: boolean} = {};
26     slip: string;
27     today = new Date();
28
29     constructor(
30         private modal: NgbModal,
31         private pcrud: PcrudService,
32         private org: OrgService,
33         private circ: CircService,
34         private printer: PrintService,
35         private serverStore: ServerStoreService) {
36         super(modal);
37     }
38
39     open(ops?: NgbModalOptions): Observable<any> {
40         // Depending on various settings, the dialog may never open.
41         // But in some cases we still have to collect the data
42         // for printing.
43
44         return from(this.applySettings())
45
46         .pipe(concatMap(exit => {
47             return from(
48                 this.collectData().then(exit2 => {
49                     // If either applySettings or collectData() tell us
50                     // to exit, make it so.
51                     return exit || exit2;
52                 })
53             );
54         }))
55
56         .pipe(concatMap(exit => {
57             if (exit) {
58                 return of(exit);
59             } else {
60                 return super.open(ops);
61             }
62         }));
63     }
64
65     collectData(): Promise<boolean> {
66         let promise = Promise.resolve(null);
67         const hold = this.checkin.hold;
68
69         console.debug('Route Dialog collecting data');
70
71         if (this.slip !== 'hold_shelf_slip') {
72
73             // Always fetch the most recent transit for the copy,
74             // regardless of what data the server returns in the payload.
75
76             promise = promise.then(_ => this.circ.findCopyTransit(this.checkin))
77             .then(transit => {
78                 this.checkin.transit = transit;
79                 this.checkin.destOrg = transit.dest();
80                 this.checkin.routeTo = transit.dest().shortname();
81                 return this.circ.getOrgAddr(this.checkin.destOrg.id(), 'holds_address');
82             })
83             .then(addr => {
84                 this.checkin.destAddress = addr;
85                 return this.org.settings('lib.courier_code', this.checkin.destOrg.id());
86             })
87
88             .then(sets => this.checkin.destCourierCode = sets['lib.courier_code']);
89         }
90
91         if (hold) {
92             promise = promise.then(_ => {
93                 return this.pcrud.retrieve('au', hold.usr(),
94                     {flesh: 1, flesh_fields : {'au' : ['card']}}).toPromise()
95                 .then(patron => this.checkin.patron = patron);
96             });
97         }
98
99         if (this.checkin.params.auto_print_holds_transits
100             || this.circ.suppressCheckinPopups) {
101             // Print and exit.
102             return promise.then(_ => this.print()).then(_ => true); // exit
103         }
104
105         return promise.then(_ => false); // keep going
106     }
107
108     applySettings(): Promise<boolean> {
109         console.debug('Route Dialog applying print settings');
110
111         if (this.checkin.transit) {
112             if (this.checkin.patron && this.checkin.hold &&
113                 // It's possible to recieve a fulfilled hold in the
114                 // checkin response when a checkin results in canceling
115                 // a hold transit for a hold that was fulfilled while
116                 // the item was in transit.
117                 !this.checkin.hold.fulfillment_time()) {
118                 this.slip = 'hold_transit_slip';
119             } else {
120                 this.slip = 'transit_slip';
121             }
122         } else {
123             this.slip = 'hold_shelf_slip';
124         }
125
126         const autoPrintSet = 'circ.staff_client.do_not_auto_attempt_print';
127
128         return this.serverStore.getItemBatch([autoPrintSet]).then(sets => {
129             const autoPrintArr = sets[autoPrintSet];
130
131             if (Array.isArray(autoPrintArr)) {
132                 this.noAutoPrint['hold_shelf_slip'] =
133                     autoPrintArr.includes('Hold Slip');
134
135                 this.noAutoPrint['hold_transit_slip'] =
136                     autoPrintArr.includes('Hold/Transit Slip');
137
138                 this.noAutoPrint['transit_slip'] =
139                     autoPrintArr.includes('Transit Slip');
140             }
141         })
142         .then(_ => this.noAutoPrint[this.slip]);
143     }
144
145     print(): Promise<any> {
146         this.printer.print({
147             templateName: this.slip,
148             contextData: {checkin: this.checkin},
149             printContext: 'default'
150         });
151
152         this.close();
153
154         // TODO printer.print() should return a promise
155         return Promise.resolve();
156     }
157 }
158