]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holds/grid.component.ts
LPLP1835982 Holds grid user barcode text generator; handle null
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / holds / grid.component.ts
1 import {Component, OnInit, Input, ViewChild} from '@angular/core';
2 import {Observable, Observer, of} from 'rxjs';
3 import {IdlObject} from '@eg/core/idl.service';
4 import {NetService} from '@eg/core/net.service';
5 import {OrgService} from '@eg/core/org.service';
6 import {AuthService} from '@eg/core/auth.service';
7 import {Pager} from '@eg/share/util/pager';
8 import {ServerStoreService} from '@eg/core/server-store.service';
9 import {GridDataSource, GridColumn, GridCellTextGenerator} from '@eg/share/grid/grid';
10 import {GridComponent} from '@eg/share/grid/grid.component';
11 import {ProgressDialogComponent} from '@eg/share/dialog/progress.component';
12 import {MarkDamagedDialogComponent
13     } from '@eg/staff/share/holdings/mark-damaged-dialog.component';
14 import {MarkMissingDialogComponent
15     } from '@eg/staff/share/holdings/mark-missing-dialog.component';
16 import {HoldRetargetDialogComponent
17     } from '@eg/staff/share/holds/retarget-dialog.component';
18 import {HoldTransferDialogComponent} from './transfer-dialog.component';
19 import {HoldCancelDialogComponent} from './cancel-dialog.component';
20 import {HoldManageDialogComponent} from './manage-dialog.component';
21 import {PrintService} from '@eg/share/print/print.service';
22
23 /** Holds grid with access to detail page and other actions */
24
25 @Component({
26   selector: 'eg-holds-grid',
27   templateUrl: 'grid.component.html'
28 })
29 export class HoldsGridComponent implements OnInit {
30
31     // If either are set/true, the pickup lib selector will display
32     @Input() initialPickupLib: number | IdlObject;
33     @Input() hidePickupLibFilter: boolean;
34
35     // Grid persist key
36     @Input() persistKey: string;
37
38     @Input() preFetchSetting: string;
39
40     @Input() printTemplate: string;
41
42     // If set, all holds are fetched on grid load and sorting/paging all
43     // happens in the client.  If false, sorting and paging occur on
44     // the server.
45     enablePreFetch: boolean;
46
47     // How to sort when no sort parameters have been applied
48     // via grid controls.  This uses the eg-grid sort format:
49     // [{name: fname, dir: 'asc'}, {name: fname2, dir: 'desc'}]
50     @Input() defaultSort: any[];
51
52     mode: 'list' | 'detail' | 'manage' = 'list';
53     initDone = false;
54     holdsCount: number;
55     pickupLib: IdlObject;
56     gridDataSource: GridDataSource;
57     detailHold: any;
58     editHolds: number[];
59     transferTarget: number;
60
61     @ViewChild('holdsGrid', { static: false }) private holdsGrid: GridComponent;
62     @ViewChild('progressDialog', { static: true })
63         private progressDialog: ProgressDialogComponent;
64     @ViewChild('transferDialog', { static: true })
65         private transferDialog: HoldTransferDialogComponent;
66     @ViewChild('markDamagedDialog', { static: true })
67         private markDamagedDialog: MarkDamagedDialogComponent;
68     @ViewChild('markMissingDialog', { static: true })
69         private markMissingDialog: MarkMissingDialogComponent;
70     @ViewChild('retargetDialog', { static: true })
71         private retargetDialog: HoldRetargetDialogComponent;
72     @ViewChild('cancelDialog', { static: true })
73         private cancelDialog: HoldCancelDialogComponent;
74     @ViewChild('manageDialog', { static: true })
75         private manageDialog: HoldManageDialogComponent;
76
77     // Bib record ID.
78     _recordId: number;
79     @Input() set recordId(id: number) {
80         this._recordId = id;
81         if (this.initDone) { // reload on update
82             this.holdsGrid.reload();
83         }
84     }
85
86     _userId: number;
87     @Input() set userId(id: number) {
88         this._userId = id;
89         if (this.initDone) {
90             this.holdsGrid.reload();
91         }
92     }
93
94     // Include holds canceled on or after the provided date.
95     // If no value is passed, canceled holds are not displayed.
96     _showCanceledSince: Date;
97     @Input() set showCanceledSince(show: Date) {
98         this._showCanceledSince = show;
99         if (this.initDone) { // reload on update
100             this.holdsGrid.reload();
101         }
102     }
103
104     // Include holds fulfilled on or after hte provided date.
105     // If no value is passed, fulfilled holds are not displayed.
106     _showFulfilledSince: Date;
107     @Input() set showFulfilledSince(show: Date) {
108         this._showFulfilledSince = show;
109         if (this.initDone) { // reload on update
110             this.holdsGrid.reload();
111         }
112     }
113
114     cellTextGenerator: GridCellTextGenerator;
115
116     constructor(
117         private net: NetService,
118         private org: OrgService,
119         private store: ServerStoreService,
120         private auth: AuthService,
121         private printer: PrintService
122     ) {
123         this.gridDataSource = new GridDataSource();
124         this.enablePreFetch = null;
125     }
126
127     ngOnInit() {
128         this.initDone = true;
129         this.pickupLib = this.org.get(this.initialPickupLib);
130
131         if (this.preFetchSetting) {
132
133                 this.store.getItem(this.preFetchSetting).then(
134                     applied => this.enablePreFetch = Boolean(applied)
135                 );
136
137         }
138
139         if (!this.defaultSort) {
140             this.defaultSort = [{name: 'request_time', dir: 'asc'}];
141         }
142
143         this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
144             sort = sort.length > 0 ? sort : this.defaultSort;
145             return this.fetchHolds(pager, sort);
146         };
147
148         // Text-ify function for cells that use display templates.
149         this.cellTextGenerator = {
150             title: row => row.title,
151             cp_barcode: row => (row.cp_barcode == null) ? '' : row.cp_barcode,
152             patron_barcode: row => row.ucard_barcode
153         };
154     }
155
156     // Returns true after all data/settings/etc required to render the
157     // grid have been fetched.
158     initComplete(): boolean {
159         return this.enablePreFetch !== null;
160     }
161
162     pickupLibChanged(org: IdlObject) {
163         this.pickupLib = org;
164         this.holdsGrid.reload();
165     }
166
167     preFetchHolds(apply: boolean) {
168         this.enablePreFetch = apply;
169
170         if (apply) {
171             setTimeout(() => this.holdsGrid.reload());
172         }
173
174         if (this.preFetchSetting) {
175             // fire and forget
176             this.store.setItem(this.preFetchSetting, apply);
177         }
178     }
179
180     applyFilters(): any {
181         const filters: any = {
182             is_staff_request: true,
183             fulfillment_time: this._showFulfilledSince ?
184                 this._showFulfilledSince.toISOString() : null,
185             cancel_time: this._showCanceledSince ?
186                 this._showCanceledSince.toISOString() : null,
187         };
188
189         if (this.pickupLib) {
190             filters.pickup_lib =
191                 this.org.descendants(this.pickupLib, true);
192         }
193
194         if (this._recordId) {
195             filters.record_id = this._recordId;
196         }
197
198         if (this._userId) {
199             filters.usr_id = this._userId;
200         }
201
202         return filters;
203     }
204
205     fetchHolds(pager: Pager, sort: any[]): Observable<any> {
206
207         // We need at least one filter.
208         if (!this._recordId && !this.pickupLib && !this._userId) {
209             return of([]);
210         }
211
212         const filters = this.applyFilters();
213
214         const orderBy: any = [];
215         if (sort.length > 0) {
216             sort.forEach(obj => {
217                 const subObj: any = {};
218                 subObj[obj.name] = {dir: obj.dir, nulls: 'last'};
219                 orderBy.push(subObj);
220             });
221         }
222
223         const limit = this.enablePreFetch ? null : pager.limit;
224         const offset = this.enablePreFetch ? 0 : pager.offset;
225
226         let observer: Observer<any>;
227         const observable = new Observable(obs => observer = obs);
228
229         this.progressDialog.open();
230         this.progressDialog.update({value: 0, max: 1});
231         let first = true;
232         let loadCount = 0;
233         this.net.request(
234             'open-ils.circ',
235             'open-ils.circ.hold.wide_hash.stream',
236             this.auth.token(), filters, orderBy, limit, offset
237         ).subscribe(
238             holdData => {
239
240                 if (first) { // First response is the hold count.
241                     this.holdsCount = Number(holdData);
242                     first = false;
243
244                 } else { // Subsequent responses are hold data blobs
245
246                     this.progressDialog.update(
247                         {value: ++loadCount, max: this.holdsCount});
248
249                     observer.next(holdData);
250                 }
251             },
252             err => {
253                 this.progressDialog.close();
254                 observer.error(err);
255             },
256             ()  => {
257                 this.progressDialog.close();
258                 observer.complete();
259             }
260         );
261
262         return observable;
263     }
264
265     showDetails(rows: any[]) {
266         this.showDetail(rows[0]);
267     }
268
269     showDetail(row: any) {
270         if (row) {
271             this.mode = 'detail';
272             this.detailHold = row;
273         }
274     }
275
276     showManager(rows: any[]) {
277         if (rows.length) {
278             this.mode = 'manage';
279             this.editHolds = rows.map(r => r.id);
280         }
281     }
282
283     handleModify(rowsModified: boolean) {
284         this.mode = 'list';
285
286         if (rowsModified) {
287             // give the grid a chance to render then ask it to reload
288             setTimeout(() => this.holdsGrid.reload());
289         }
290     }
291
292
293
294     showRecentCircs(rows: any[]) {
295         if (rows.length) {
296             const url =
297                 '/eg/staff/cat/item/' + rows[0].cp_id + '/circ_list';
298             window.open(url, '_blank');
299         }
300     }
301
302     showPatron(rows: any[]) {
303         if (rows.length) {
304             const url =
305                 '/eg/staff/circ/patron/' + rows[0].usr_id + '/checkout';
306             window.open(url, '_blank');
307         }
308     }
309
310     showManageDialog(rows: any[]) {
311         const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
312         if (holdIds.length > 0) {
313             this.manageDialog.holdIds = holdIds;
314             this.manageDialog.open({size: 'lg'}).subscribe(
315                 rowsModified => {
316                     if (rowsModified) {
317                         this.holdsGrid.reload();
318                     }
319                 }
320             );
321         }
322     }
323
324     showTransferDialog(rows: any[]) {
325         const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
326         if (holdIds.length > 0) {
327             this.transferDialog.holdIds = holdIds;
328             this.transferDialog.open({}).subscribe(
329                 rowsModified => {
330                     if (rowsModified) {
331                         this.holdsGrid.reload();
332                     }
333                 }
334             );
335         }
336     }
337
338     async showMarkDamagedDialog(rows: any[]) {
339         const copyIds = rows.map(r => r.cp_id).filter(id => Boolean(id));
340         if (copyIds.length === 0) { return; }
341
342         let rowsModified = false;
343
344         const markNext = async(ids: number[]) => {
345             if (ids.length === 0) {
346                 return Promise.resolve();
347             }
348
349             this.markDamagedDialog.copyId = ids.pop();
350             return this.markDamagedDialog.open({size: 'lg'}).subscribe(
351                 ok => {
352                     if (ok) { rowsModified = true; }
353                     return markNext(ids);
354                 },
355                 dismiss => markNext(ids)
356             );
357         };
358
359         await markNext(copyIds);
360         if (rowsModified) {
361             this.holdsGrid.reload();
362         }
363     }
364
365     showMarkMissingDialog(rows: any[]) {
366         const copyIds = rows.map(r => r.cp_id).filter(id => Boolean(id));
367         if (copyIds.length > 0) {
368             this.markMissingDialog.copyIds = copyIds;
369             this.markMissingDialog.open({}).subscribe(
370                 rowsModified => {
371                     if (rowsModified) {
372                         this.holdsGrid.reload();
373                     }
374                 }
375             );
376         }
377     }
378
379     showRetargetDialog(rows: any[]) {
380         const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
381         if (holdIds.length > 0) {
382             this.retargetDialog.holdIds = holdIds;
383             this.retargetDialog.open({}).subscribe(
384                 rowsModified => {
385                     if (rowsModified) {
386                         this.holdsGrid.reload();
387                     }
388                 }
389             );
390         }
391     }
392
393     showCancelDialog(rows: any[]) {
394         const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
395         if (holdIds.length > 0) {
396             this.cancelDialog.holdIds = holdIds;
397             this.cancelDialog.open({}).subscribe(
398                 rowsModified => {
399                     if (rowsModified) {
400                         this.holdsGrid.reload();
401                     }
402                 }
403             );
404         }
405     }
406
407     printHolds() {
408         // Request a page with no limit to get all of the wide holds for
409         // printing.  Call requestPage() directly instead of grid.reload()
410         // since we may already have the data.
411
412         const pager = new Pager();
413         pager.offset = 0;
414         pager.limit = null;
415
416         if (this.gridDataSource.sort.length === 0) {
417             this.gridDataSource.sort = this.defaultSort;
418         }
419
420         this.gridDataSource.requestPage(pager).then(() => {
421             if (this.gridDataSource.data.length > 0) {
422                 this.printer.print({
423                     templateName: this.printTemplate || 'holds_for_bib',
424                     contextData: this.gridDataSource.data,
425                     printContext: 'default'
426                 });
427             }
428         });
429     }
430 }
431
432
433
434