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';
16 /** Route Item Dialog */
19 templateUrl: 'route-dialog.component.html',
20 selector: 'eg-circ-route-dialog'
22 export class RouteDialogComponent extends DialogComponent {
24 checkin: CheckinResult;
25 noAutoPrint: {[template: string]: boolean} = {};
30 private modal: NgbModal,
31 private pcrud: PcrudService,
32 private org: OrgService,
33 private circ: CircService,
34 private printer: PrintService,
35 private serverStore: ServerStoreService) {
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
44 return from(this.applySettings())
46 .pipe(concatMap(exit => {
48 this.collectData().then(exit2 => {
49 // If either applySettings or collectData() tell us
50 // to exit, make it so.
56 .pipe(concatMap(exit => {
60 return super.open(ops);
65 collectData(): Promise<boolean> {
66 let promise = Promise.resolve(null);
67 const hold = this.checkin.hold;
69 console.debug('Route Dialog collecting data');
71 if (this.slip !== 'hold_shelf_slip') {
73 // Always fetch the most recent transit for the copy,
74 // regardless of what data the server returns in the payload.
76 promise = promise.then(_ => this.circ.findCopyTransit(this.checkin))
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');
84 this.checkin.destAddress = addr;
85 return this.org.settings('lib.courier_code', this.checkin.destOrg.id());
88 .then(sets => this.checkin.destCourierCode = sets['lib.courier_code']);
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);
99 if (this.checkin.params.auto_print_holds_transits
100 || this.circ.suppressCheckinPopups) {
102 return promise.then(_ => this.print()).then(_ => true); // exit
105 return promise.then(_ => false); // keep going
108 applySettings(): Promise<boolean> {
109 console.debug('Route Dialog applying print settings');
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';
120 this.slip = 'transit_slip';
123 this.slip = 'hold_shelf_slip';
126 const autoPrintSet = 'circ.staff_client.do_not_auto_attempt_print';
128 return this.serverStore.getItemBatch([autoPrintSet]).then(sets => {
129 const autoPrintArr = sets[autoPrintSet];
131 if (Array.isArray(autoPrintArr)) {
132 this.noAutoPrint['hold_shelf_slip'] =
133 autoPrintArr.includes('Hold Slip');
135 this.noAutoPrint['hold_transit_slip'] =
136 autoPrintArr.includes('Hold/Transit Slip');
138 this.noAutoPrint['transit_slip'] =
139 autoPrintArr.includes('Transit Slip');
142 .then(_ => this.noAutoPrint[this.slip]);
145 print(): Promise<any> {
147 templateName: this.slip,
148 contextData: {checkin: this.checkin},
149 printContext: 'default'
154 // TODO printer.print() should return a promise
155 return Promise.resolve();