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