]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holds/grid.component.ts
029883b3b02674a35b0f231395eb5655c26441b5
[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
152         };
153     }
154
155     // Returns true after all data/settings/etc required to render the
156     // grid have been fetched.
157     initComplete(): boolean {
158         return this.enablePreFetch !== null;
159     }
160
161     pickupLibChanged(org: IdlObject) {
162         this.pickupLib = org;
163         this.holdsGrid.reload();
164     }
165
166     preFetchHolds(apply: boolean) {
167         this.enablePreFetch = apply;
168
169         if (apply) {
170             setTimeout(() => this.holdsGrid.reload());
171         }
172
173         if (this.preFetchSetting) {
174             // fire and forget
175             this.store.setItem(this.preFetchSetting, apply);
176         }
177     }
178
179     applyFilters(): any {
180         const filters: any = {
181             is_staff_request: true,
182             fulfillment_time: this._showFulfilledSince ?
183                 this._showFulfilledSince.toISOString() : null,
184             cancel_time: this._showCanceledSince ?
185                 this._showCanceledSince.toISOString() : null,
186         };
187
188         if (this.pickupLib) {
189             filters.pickup_lib =
190                 this.org.descendants(this.pickupLib, true);
191         }
192
193         if (this._recordId) {
194             filters.record_id = this._recordId;
195         }
196
197         if (this._userId) {
198             filters.usr_id = this._userId;
199         }
200
201         return filters;
202     }
203
204     fetchHolds(pager: Pager, sort: any[]): Observable<any> {
205
206         // We need at least one filter.
207         if (!this._recordId && !this.pickupLib && !this._userId) {
208             return of([]);
209         }
210
211         const filters = this.applyFilters();
212
213         const orderBy: any = [];
214         if (sort.length > 0) {
215             sort.forEach(obj => {
216                 const subObj: any = {};
217                 subObj[obj.name] = {dir: obj.dir, nulls: 'last'};
218                 orderBy.push(subObj);
219             });
220         }
221
222         const limit = this.enablePreFetch ? null : pager.limit;
223         const offset = this.enablePreFetch ? 0 : pager.offset;
224
225         let observer: Observer<any>;
226         const observable = new Observable(obs => observer = obs);
227
228         this.progressDialog.open();
229         this.progressDialog.update({value: 0, max: 1});
230         let first = true;
231         let loadCount = 0;
232         this.net.request(
233             'open-ils.circ',
234             'open-ils.circ.hold.wide_hash.stream',
235             this.auth.token(), filters, orderBy, limit, offset
236         ).subscribe(
237             holdData => {
238
239                 if (first) { // First response is the hold count.
240                     this.holdsCount = Number(holdData);
241                     first = false;
242
243                 } else { // Subsequent responses are hold data blobs
244
245                     this.progressDialog.update(
246                         {value: ++loadCount, max: this.holdsCount});
247
248                     observer.next(holdData);
249                 }
250             },
251             err => {
252                 this.progressDialog.close();
253                 observer.error(err);
254             },
255             ()  => {
256                 this.progressDialog.close();
257                 observer.complete();
258             }
259         );
260
261         return observable;
262     }
263
264     showDetails(rows: any[]) {
265         this.showDetail(rows[0]);
266     }
267
268     showDetail(row: any) {
269         if (row) {
270             this.mode = 'detail';
271             this.detailHold = row;
272         }
273     }
274
275     showManager(rows: any[]) {
276         if (rows.length) {
277             this.mode = 'manage';
278             this.editHolds = rows.map(r => r.id);
279         }
280     }
281
282     handleModify(rowsModified: boolean) {
283         this.mode = 'list';
284
285         if (rowsModified) {
286             // give the grid a chance to render then ask it to reload
287             setTimeout(() => this.holdsGrid.reload());
288         }
289     }
290
291
292
293     showRecentCircs(rows: any[]) {
294         if (rows.length) {
295             const url =
296                 '/eg/staff/cat/item/' + rows[0].cp_id + '/circ_list';
297             window.open(url, '_blank');
298         }
299     }
300
301     showPatron(rows: any[]) {
302         if (rows.length) {
303             const url =
304                 '/eg/staff/circ/patron/' + rows[0].usr_id + '/checkout';
305             window.open(url, '_blank');
306         }
307     }
308
309     showManageDialog(rows: any[]) {
310         const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
311         if (holdIds.length > 0) {
312             this.manageDialog.holdIds = holdIds;
313             this.manageDialog.open({size: 'lg'}).subscribe(
314                 rowsModified => {
315                     if (rowsModified) {
316                         this.holdsGrid.reload();
317                     }
318                 }
319             );
320         }
321     }
322
323     showTransferDialog(rows: any[]) {
324         const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
325         if (holdIds.length > 0) {
326             this.transferDialog.holdIds = holdIds;
327             this.transferDialog.open({}).subscribe(
328                 rowsModified => {
329                     if (rowsModified) {
330                         this.holdsGrid.reload();
331                     }
332                 }
333             );
334         }
335     }
336
337     async showMarkDamagedDialog(rows: any[]) {
338         const copyIds = rows.map(r => r.cp_id).filter(id => Boolean(id));
339         if (copyIds.length === 0) { return; }
340
341         let rowsModified = false;
342
343         const markNext = async(ids: number[]) => {
344             if (ids.length === 0) {
345                 return Promise.resolve();
346             }
347
348             this.markDamagedDialog.copyId = ids.pop();
349             return this.markDamagedDialog.open({size: 'lg'}).subscribe(
350                 ok => {
351                     if (ok) { rowsModified = true; }
352                     return markNext(ids);
353                 },
354                 dismiss => markNext(ids)
355             );
356         };
357
358         await markNext(copyIds);
359         if (rowsModified) {
360             this.holdsGrid.reload();
361         }
362     }
363
364     showMarkMissingDialog(rows: any[]) {
365         const copyIds = rows.map(r => r.cp_id).filter(id => Boolean(id));
366         if (copyIds.length > 0) {
367             this.markMissingDialog.copyIds = copyIds;
368             this.markMissingDialog.open({}).subscribe(
369                 rowsModified => {
370                     if (rowsModified) {
371                         this.holdsGrid.reload();
372                     }
373                 }
374             );
375         }
376     }
377
378     showRetargetDialog(rows: any[]) {
379         const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
380         if (holdIds.length > 0) {
381             this.retargetDialog.holdIds = holdIds;
382             this.retargetDialog.open({}).subscribe(
383                 rowsModified => {
384                     if (rowsModified) {
385                         this.holdsGrid.reload();
386                     }
387                 }
388             );
389         }
390     }
391
392     showCancelDialog(rows: any[]) {
393         const holdIds = rows.map(r => r.id).filter(id => Boolean(id));
394         if (holdIds.length > 0) {
395             this.cancelDialog.holdIds = holdIds;
396             this.cancelDialog.open({}).subscribe(
397                 rowsModified => {
398                     if (rowsModified) {
399                         this.holdsGrid.reload();
400                     }
401                 }
402             );
403         }
404     }
405
406     printHolds() {
407         // Request a page with no limit to get all of the wide holds for
408         // printing.  Call requestPage() directly instead of grid.reload()
409         // since we may already have the data.
410
411         const pager = new Pager();
412         pager.offset = 0;
413         pager.limit = null;
414
415         if (this.gridDataSource.sort.length === 0) {
416             this.gridDataSource.sort = this.defaultSort;
417         }
418
419         this.gridDataSource.requestPage(pager).then(() => {
420             if (this.gridDataSource.data.length > 0) {
421                 this.printer.print({
422                     templateName: this.printTemplate || 'holds_for_bib',
423                     contextData: this.gridDataSource.data,
424                     printContext: 'default'
425                 });
426             }
427         });
428     }
429 }
430
431
432
433