]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/patron/items_out.js
Lp 1350916: marc_export --uris option release notes.
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / circ / patron / items_out.js
1 /**
2  * List of patron items checked out
3  */
4
5 angular.module('egPatronApp')
6
7 .controller('PatronItemsOutCtrl',
8        ['$scope','$q','$routeParams','$timeout','egCore','egUser','patronSvc',
9         '$location','egGridDataProvider','$uibModal','egCirc','egConfirmDialog',
10         'egBilling','$window','egBibDisplay',
11 function($scope , $q , $routeParams , $timeout , egCore , egUser , patronSvc , 
12          $location , egGridDataProvider , $uibModal , egCirc , egConfirmDialog , 
13          egBilling , $window , egBibDisplay) {
14
15     // list of noncatatloged circulations. Define before initTab to 
16     // avoid any possibility of race condition, since they are loaded
17     // during init, but may be referenced before init completes.
18     $scope.noncat_list = [];
19
20     $scope.initTab('items_out', $routeParams.id).then(function() {
21         // sort inline to support paging
22         $scope.noncat_list = patronSvc.noncat_ids.sort();
23     });
24
25     // cache of circ objects for grid display
26     patronSvc.items_out = [];
27
28     // main list of checked out items
29     $scope.main_list = [];
30
31     // list of alt circs (lost, etc.) and/or check-in with fines circs
32     $scope.alt_list = []; 
33
34     // these are fetched during startup (i.e. .configure())
35     // By default, show lost/lo/cr items in the alt list
36     var display_lost = Number(
37         egCore.env.aous['ui.circ.items_out.lost']) || 2;
38     var display_lo = Number(
39         egCore.env.aous['ui.circ.items_out.longoverdue']) || 2;
40     var display_cr = Number(
41         egCore.env.aous['ui.circ.items_out.claimsreturned']) || 2;
42
43     var fetch_checked_in = true;
44     $scope.show_alt_circs = true;
45     if (display_lost & 4 && display_lo & 4 && display_cr & 4) {
46         // all special types are configured to be hidden once
47         // checked in, so there's no need to fetch checked-in circs.
48         fetch_checked_in = false;
49
50         if (display_lost & 1 && display_lo & 1 && display_cr & 1) {                 
51             // additionally, if all types are configured to display    
52             // in the main list while checked out, nothing will         
53             // ever appear in the alternate list, so we can hide          
54             // the alternate list from the UI.  
55             $scope.show_alt_circs = false;
56         }
57     }
58
59     $scope.items_out_display = 'main';
60     $scope.show_main_list = function(refresh_grid) {
61         // don't need a full reset_page() to swap tabs
62         $scope.items_out_display = 'main';
63         patronSvc.items_out = [];
64         // only refresh the grid when navigating from a tab that 
65         // shares the same grid.
66         if (refresh_grid) provider.refresh();
67     }
68
69     $scope.show_alt_list = function(refresh_grid) {
70         // don't need a full reset_page() to swap tabs
71         $scope.items_out_display = 'alt';
72         patronSvc.items_out = [];
73         // only refresh the grid when navigating from a tab that 
74         // shares the same grid.
75         if (refresh_grid) provider.refresh();
76     }
77
78     $scope.show_noncat_list = function() {
79         // don't need a full reset_page() to swap tabs
80         $scope.items_out_display = 'noncat';
81         patronSvc.items_out = [];
82         // Grid refresh is not necessary because switching to the
83         // noncat_list always involves instantiating a new grid.
84     }
85
86     // Reload the user to pick up changes in items out, fines, etc.
87     // Reload circs since the contents of the main vs. alt list may
88     // have changed.
89     function reset_page() {
90         patronSvc.refreshPrimary();
91         patronSvc.items_out = []; 
92         $scope.main_list = [];
93         $scope.alt_list = [];
94         $timeout(provider.refresh);  // allow scope changes to propagate
95     }
96
97     var provider = egGridDataProvider.instance({});
98     $scope.gridDataProvider = provider;
99
100     function fetch_circs(id_list, offset, count) {
101         if (!id_list.length || id_list.length < offset + 1) return $q.when();
102
103         var deferred = $q.defer();
104         var rendered = 0;
105
106         // fetch the lot of circs and stream the results back via notify
107         egCore.pcrud.search('circ', {id : id_list},
108             {   flesh : 4,
109                 flesh_fields : {
110                     circ : ['target_copy', 'workstation', 'checkin_workstation'],
111                     acp : ['call_number', 'holds_count', 'status', 'circ_lib'],
112                     acn : ['record', 'owning_lib', 'prefix', 'suffix'],
113                     bre : ['wide_display_entry']
114                 },
115                 // avoid fetching the MARC blob by specifying which 
116                 // fields on the bre to select.  More may be needed.
117                 // note that fleshed fields are explicitly selected.
118                 select : { bre : ['id'] },
119                 // TODO: LP#1697954 Fetch all circs on grid render 
120                 // to support client-side sorting.  Migrate to server-side
121                 // sorting to avoid the need for fetching all items.
122                 //limit  : count,
123                 //offset : offset,
124                 // we need an order-by to support paging
125                 order_by : {circ : ['xact_start']} 
126
127         }).then(deferred.resolve, null, function(circ) {
128             circ.circ_lib(egCore.org.get(circ.circ_lib())); // local fleshing
129
130             // Translate bib display field JSON blobs to JS.
131             // Collapse multi/array fields down to comma-separated strings.
132             egBibDisplay.mwdeJSONToJS(
133                 circ.target_copy().call_number().record().wide_display_entry(), true);
134
135             if (circ.target_copy().call_number().id() == -1) {
136                 // dummy-up a record for precat items
137                 circ.target_copy().call_number().record().wide_display_entry({
138                     title : function() {return circ.target_copy().dummy_title()},
139                     author : function() {return circ.target_copy().dummy_author()},
140                     isbn : function() {return circ.target_copy().dummy_isbn()}
141                 })
142             }
143
144             patronSvc.items_out.push(circ); // toss it into the cache
145
146             // We fetch all circs for client-side sorting, but only
147             // notify the caller for the page of requested circs.  
148             if (rendered++ >= offset && rendered <= count)
149                 deferred.notify(circ);
150         });
151
152         return deferred.promise;
153     }
154
155     function fetch_noncat_circs(id_list, offset, count) {
156         if (!id_list.length) return $q.when();
157
158         var deferred = $q.defer();
159         var rendered = 0;
160
161         egCore.pcrud.search('ancc', {id : id_list},
162             {   flesh : 1,
163                 flesh_fields : {ancc : ['item_type','staff']},
164                 // TODO: LP#1697954 Fetch all circs on grid render 
165                 // to support client-side sorting.  Migrate to server-side
166                 // sorting to avoid the need for fetching all items.
167                 //limit  : count,
168                 //offset : offset,
169                 // we need an order-by to support paging
170                 order_by : {circ : ['circ_time']} 
171
172         }).then(deferred.resolve, null, function(noncat_circ) {
173
174             // calculate the virtual due date from the item type duration
175             var seconds = egCore.date.intervalToSeconds(
176                 noncat_circ.item_type().circ_duration());
177             var d = new Date(Date.parse(noncat_circ.circ_time()));
178             d.setSeconds(d.getSeconds() + seconds);
179             noncat_circ.duedate(d.toISOString());
180
181             // local flesh org unit
182             noncat_circ.circ_lib(egCore.org.get(noncat_circ.circ_lib()));
183
184             patronSvc.items_out.push(noncat_circ); // cache it
185
186             // We fetch all noncat circs for client-side sorting, but
187             // only notify the caller for the page of requested circs.  
188             if (rendered++ >= offset && rendered <= count)
189                 deferred.notify(noncat_circ);
190         });
191
192         return deferred.promise;
193     }
194
195
196     // decide which list each circ belongs to
197     function promote_circs(list, display_code, open) {
198         if (open) {                                                    
199             if (1 & display_code) { // bitflag 1 == top list                   
200                 $scope.main_list = $scope.main_list.concat(list);
201             } else {                                                   
202                 $scope.alt_list = $scope.alt_list.concat(list);
203             }                                                          
204         } else {                                                       
205             if (4 & display_code) return;  // bitflag 4 == hide on checkin     
206             $scope.alt_list = $scope.alt_list.concat(list);
207         } 
208     }
209
210     // fetch IDs for circs we care about
211     function get_circ_ids() {
212         $scope.main_list = [];
213         $scope.alt_list = [];
214
215         // we can fetch these in parallel
216         var promise1 = egCore.net.request(
217             'open-ils.actor',
218             'open-ils.actor.user.checked_out.authoritative',
219             egCore.auth.token(), $scope.patron_id
220         ).then(function(outs) {
221             $scope.main_list = outs.overdue.concat(outs.out);
222             promote_circs(outs.lost, display_lost, true);                            
223             promote_circs(outs.long_overdue, display_lo, true);             
224             promote_circs(outs.claims_returned, display_cr, true);
225         });
226
227         // only fetched checked-in-with-bills circs if configured to display
228         var promise2 = !fetch_checked_in ? $q.when() : egCore.net.request(
229             'open-ils.actor',
230             'open-ils.actor.user.checked_in_with_fines.authoritative',
231             egCore.auth.token(), $scope.patron_id
232         ).then(function(outs) {
233             promote_circs(outs.lost, display_lost);
234             promote_circs(outs.long_overdue, display_lo);
235             promote_circs(outs.claims_returned, display_cr);
236         });
237
238         return $q.all([promise1, promise2]);
239     }
240
241     provider.get = function(offset, count) {
242
243         var id_list = $scope[$scope.items_out_display + '_list'];
244
245         // see if we have the requested range cached
246         // Note this items_out list is reset w/ each items-out tab change
247         if (patronSvc.items_out[offset]) {
248             return provider.arrayNotifier(
249                 patronSvc.items_out, offset, count);
250         }
251
252         if ($scope.items_out_display == 'noncat') {
253             // if there are any noncat circ IDs, we already have them
254             return fetch_noncat_circs(id_list, offset, count);
255         }
256
257         // See if we have the circ IDs for this range already loaded.
258         // this would happen navigating to a subsequent page.
259         if (id_list[offset]) {
260             return fetch_circs(id_list, offset, count);
261         }
262
263         // avoid returning the request directly to the caller so the
264         // notify()'s from egCore.net.request don't leak into the 
265         // final set of notifies (i.e. the real responses);
266
267         var deferred = $q.defer();
268         get_circ_ids().then(function() {
269
270             id_list = $scope[$scope.items_out_display + '_list'];
271             $scope.gridDataProvider.grid.totalCount = id_list.length;
272             // relay the notified circs back to the grid through our promise
273             fetch_circs(id_list, offset, count).then(
274                 deferred.resolve, null, deferred.notify);
275         });
276
277         return deferred.promise;
278     }
279
280
281     // true if circ is overdue, false otherwise
282     $scope.circIsOverdue = function(circ) {
283         // circ may not exist yet for rendered row
284         if (!circ) return false;
285
286         var date = new Date();
287         date.setTime(Date.parse(circ.due_date()));
288         return date < new Date();
289     }
290
291     $scope.edit_due_date = function(items) {
292         if (!items.length) return;
293
294         $uibModal.open({
295             templateUrl : './circ/patron/t_edit_due_date_dialog',
296             backdrop: 'static',
297             controller : [
298                         '$scope','$uibModalInstance',
299                 function($scope , $uibModalInstance) {
300
301                     // if there is only one circ, default to the due date
302                     // of that circ.  Otherwise, default to today.
303                     var due_date = items.length == 1 ? 
304                         Date.parse(items[0].due_date()) : new Date();
305
306                     $scope.args = {
307                         num_circs : items.length,
308                         due_date : due_date
309                     }
310
311                     // Fire off the due-date updater for each circ.
312                     // When all is done, close the dialog
313                     $scope.ok = function(args) {
314                         // toISOString gives us Zulu time, so
315                         // adjust for that before truncating to date
316                         var adjust_date = new Date( $scope.args.due_date );
317                         adjust_date.setMinutes(
318                             $scope.args.due_date.getMinutes() - adjust_date.getTimezoneOffset()
319                         );
320                         var due = adjust_date.toISOString().replace(/T.*/,'');
321                         console.debug("applying due date of " + due);
322
323                         var promises = [];
324                         angular.forEach(items, function(circ) {
325                             promises.push(
326                                 egCore.net.request(
327                                     'open-ils.circ',
328                                     'open-ils.circ.circulation.due_date.update',
329                                     egCore.auth.token(), circ.id(), due
330
331                                 ).then(function(new_circ) {
332                                     // update the grid circ with the canonical 
333                                     // date from the modified circulation.
334                                     circ.due_date(new_circ.due_date());
335                                 })
336                             );
337                         });
338
339                         $q.all(promises).then(function() {
340                             $uibModalInstance.close();
341                             provider.refresh();
342                         });
343                     }
344                     $scope.cancel = function($event) {
345                         $uibModalInstance.dismiss();
346                         $event.preventDefault();
347                     }
348                 }
349             ]
350         });
351     }
352
353     $scope.print_receipt = function(items) {
354         if (items.length == 0) return $q.when();
355         var print_data = {circulations : []};
356         var cusr = patronSvc.current;
357
358         angular.forEach(patronSvc.items_out, function(circ) {
359             print_data.circulations.push({
360                 circ : egCore.idl.toHash(circ),
361                 copy : egCore.idl.toHash(circ.target_copy()),
362                 call_number : egCore.idl.toHash(circ.target_copy().call_number()),
363                 title : circ.target_copy().call_number().record().wide_display_entry().title(),
364                 author : circ.target_copy().call_number().record().wide_display_entry().author()
365             })
366         });
367
368         print_data.patron = {
369             prefix : cusr.prefix(),
370             first_given_name : cusr.first_given_name(),
371             second_given_name : cusr.second_given_name(),
372             family_name : cusr.family_name(),
373             suffix : cusr.suffix(),
374             card : { barcode : cusr.card().barcode() },
375             money_summary : patronSvc.patron_stats.fines,
376             expire_date : cusr.expire_date(),
377             alias : cusr.alias(),
378             has_email : Boolean(patronSvc.current.email() && patronSvc.current.email().match(/.*@.*/).length),
379             has_phone : Boolean(cusr.day_phone() || cusr.evening_phone() || cusr.other_phone())
380         };
381
382         return egCore.print.print({
383             context : 'default', 
384             template : 'items_out', 
385             scope : print_data,
386         });
387     }
388
389     function batch_action_with_barcodes(items, action) {
390         if (!items.length) return;
391         var barcodes = items.map(function(circ) 
392             { return circ.target_copy().barcode() });
393         action(barcodes).then(reset_page);
394     }
395     $scope.mark_lost = function(items) {
396         batch_action_with_barcodes(items, egCirc.mark_lost);
397     }
398     $scope.mark_claims_returned = function(items) {
399         batch_action_with_barcodes(items, egCirc.mark_claims_returned_dialog);
400     }
401     $scope.mark_claims_never_checked_out = function(items) {
402         batch_action_with_barcodes(items, egCirc.mark_claims_never_checked_out);
403     }
404
405     $scope.show_recent_circs = function(items) {
406         var focus = items.length == 1;
407         angular.forEach(items, function(item) {
408             var url = egCore.env.basePath +
409                       '/cat/item/' +
410                       item.target_copy().id() +
411                       '/circ_list';
412             $timeout(function() { var x = $window.open(url, '_blank'); if (focus) x.focus() });
413         });
414     }
415
416     $scope.show_triggered_events = function(items) {
417         var focus = items.length == 1;
418         angular.forEach(items, function(item) {
419             var url = egCore.env.basePath +
420                       '/cat/item/' +
421                       item.target_copy().id() +
422                       '/triggered_events';
423             $timeout(function() { var x = $window.open(url, '_blank'); if (focus) x.focus() });
424         });
425     }
426
427     $scope.renew = function(items, msg) {
428         if (!items.length) return;
429         var barcodes = items.map(function(circ) 
430             { return circ.target_copy().barcode() });
431
432         if (!msg) msg = egCore.strings.RENEW_ITEMS;
433
434         return egConfirmDialog.open(msg, barcodes.join(' '), {}).result
435         .then(function() {
436             function do_one() {
437                 var bc = barcodes.pop();
438                 if (!bc) { reset_page(); return }
439                 // finally -> continue even when one fails
440                 egCirc.renew({copy_barcode : bc}).finally(do_one);
441             }
442             do_one();
443         });
444     }
445
446     $scope.renew_all = function() {
447         var circs = patronSvc.items_out.filter(function(circ) {
448             return (
449                 // all others will be rejected at the server
450                 !circ.stop_fines() ||
451                 circ.stop_fines() == 'MAXFINES'
452             );
453         });
454         $scope.renew(circs, egCore.strings.RENEW_ALL_ITEMS);
455     }
456
457     $scope.renew_with_date = function(items) {
458         if (!items.length) return;
459         var barcodes = items.map(function(circ) 
460             { return circ.target_copy().barcode() });
461
462         return $uibModal.open({
463             templateUrl : './circ/patron/t_renew_with_date_dialog',
464             backdrop: 'static',
465             controller : [
466                         '$scope','$uibModalInstance',
467                 function($scope , $uibModalInstance) {
468                     $scope.args = {
469                         barcodes : barcodes,
470                         date : new Date()
471                     }
472                     $scope.cancel = function() {$uibModalInstance.dismiss()}
473
474                     // Fire off the due-date updater for each circ.
475                     // When all is done, close the dialog
476                     $scope.ok = function() {
477                         var due = $scope.args.date.toISOString().replace(/T.*/,'');
478                         console.debug("renewing with due date: " + due);
479
480                         function do_one() {
481                             if (bc = barcodes.pop()) {
482                                 egCirc.renew({copy_barcode : bc, due_date : due})
483                                 .finally(do_one);
484                             } else {
485                                 $uibModalInstance.close(); 
486                                 reset_page();
487                             }
488                         }
489                        do_one(); // kick it off
490                     }
491                 }
492             ]
493         }).result;
494     }
495
496     $scope.checkin = function(items) {
497         if (!items.length) return;
498         var barcodes = items.map(function(circ) 
499             { return circ.target_copy().barcode() });
500
501         return egConfirmDialog.open(
502             egCore.strings.CHECK_IN_CONFIRM, barcodes.join(' '), {
503
504         }).result.then(function() {
505             function do_one() {
506                 if (bc = barcodes.pop()) {
507                     egCirc.checkin({copy_barcode : bc})
508                     .finally(do_one);
509                 } else {
510                     reset_page();
511                 }
512             }
513             do_one(); // kick it off
514         });
515     }
516
517     $scope.add_billing = function(items) {
518         if (!items.length) return;
519         var circs = items.concat(); // don't pop from grid array
520         function do_one() {
521             var circ; // don't clobber window.circ!
522             if (circ = circs.pop()) {
523                 egBilling.showBillDialog({
524                     // let the dialog fetch the transaction, since it's
525                     // not sufficiently fleshed here.
526                     xact_id : circ.id(),
527                     patron : patronSvc.current
528                 }).finally(do_one);
529             } else {
530                 reset_page();
531             }
532         }
533         do_one();
534     }
535
536 }]);
537