2 * List of patron items checked out
5 angular.module('egPatronApp')
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) {
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 = [];
20 $scope.initTab('items_out', $routeParams.id).then(function() {
21 // sort inline to support paging
22 $scope.noncat_list = patronSvc.noncat_ids.sort();
25 // cache of circ objects for grid display
26 patronSvc.items_out = [];
28 // main list of checked out items
29 $scope.main_list = [];
31 // list of alt circs (lost, etc.) and/or check-in with fines circs
35 'ui.circ.suppress_checkin_popups' // add other settings as needed
36 ]).then(function(set) {
37 $scope.suppress_popups = set['ui.circ.suppress_checkin_popups'];
40 // these are fetched during startup (i.e. .configure())
41 // By default, show lost/lo/cr items in the alt list
42 var display_lost = Number(
43 egCore.env.aous['ui.circ.items_out.lost']) || 2;
44 var display_lo = Number(
45 egCore.env.aous['ui.circ.items_out.longoverdue']) || 2;
46 var display_cr = Number(
47 egCore.env.aous['ui.circ.items_out.claimsreturned']) || 2;
49 var fetch_checked_in = true;
50 $scope.show_alt_circs = true;
51 if (display_lost & 4 && display_lo & 4 && display_cr & 4) {
52 // all special types are configured to be hidden once
53 // checked in, so there's no need to fetch checked-in circs.
54 fetch_checked_in = false;
56 if (display_lost & 1 && display_lo & 1 && display_cr & 1) {
57 // additionally, if all types are configured to display
58 // in the main list while checked out, nothing will
59 // ever appear in the alternate list, so we can hide
60 // the alternate list from the UI.
61 $scope.show_alt_circs = false;
65 $scope.items_out_display = 'main';
66 $scope.show_main_list = function(refresh_grid) {
67 // don't need a full reset_page() to swap tabs
68 $scope.items_out_display = 'main';
69 patronSvc.items_out = [];
70 // only refresh the grid when navigating from a tab that
71 // shares the same grid.
72 if (refresh_grid) provider.refresh();
75 $scope.show_alt_list = function(refresh_grid) {
76 // don't need a full reset_page() to swap tabs
77 $scope.items_out_display = 'alt';
78 patronSvc.items_out = [];
79 // only refresh the grid when navigating from a tab that
80 // shares the same grid.
81 if (refresh_grid) provider.refresh();
84 $scope.show_noncat_list = function() {
85 // don't need a full reset_page() to swap tabs
86 $scope.items_out_display = 'noncat';
87 patronSvc.items_out = [];
88 // Grid refresh is not necessary because switching to the
89 // noncat_list always involves instantiating a new grid.
92 $scope.colorizeItemsOutList = {
93 apply: function(item) {
94 var duedate = item.due_date();
95 if (duedate && duedate < new Date().toISOString()) {
101 // Reload the user to pick up changes in items out, fines, etc.
102 // Reload circs since the contents of the main vs. alt list may
104 function reset_page() {
105 patronSvc.refreshPrimary();
106 patronSvc.items_out = [];
107 $scope.main_list = [];
108 $scope.alt_list = [];
109 $timeout(provider.refresh); // allow scope changes to propagate
112 var provider = egGridDataProvider.instance({});
113 $scope.gridDataProvider = provider;
115 function fetch_circs(id_list, offset, count) {
116 if (!id_list.length || id_list.length < offset + 1) return $q.when();
118 var deferred = $q.defer();
121 // fetch the lot of circs and stream the results back via notify
122 egCore.pcrud.search('circ', {id : id_list},
125 circ : ['target_copy', 'workstation', 'checkin_workstation'],
126 acp : ['call_number', 'holds_count', 'status', 'circ_lib', 'location', 'floating', 'age_protect', 'parts'],
128 acn : ['record', 'owning_lib', 'prefix', 'suffix'],
129 bre : ['wide_display_entry']
131 // avoid fetching the MARC blob by specifying which
132 // fields on the bre to select. More may be needed.
133 // note that fleshed fields are explicitly selected.
134 select : { bre : ['id'] },
135 // TODO: LP#1697954 Fetch all circs on grid render
136 // to support client-side sorting. Migrate to server-side
137 // sorting to avoid the need for fetching all items.
140 // we need an order-by to support paging
141 order_by : {circ : ['xact_start']}
143 }).then(deferred.resolve, null, function(circ) {
144 circ.circ_lib(egCore.org.get(circ.circ_lib())); // local fleshing
146 // Translate bib display field JSON blobs to JS.
147 // Collapse multi/array fields down to comma-separated strings.
148 egBibDisplay.mwdeJSONToJS(
149 circ.target_copy().call_number().record().wide_display_entry(), true);
151 if (circ.target_copy().call_number().id() == -1) {
152 // dummy-up a record for precat items
153 circ.target_copy().call_number().record().wide_display_entry({
154 title : function() {return circ.target_copy().dummy_title()},
155 author : function() {return circ.target_copy().dummy_author()},
156 isbn : function() {return circ.target_copy().dummy_isbn()}
159 circ._parts = circ.target_copy().parts().map(function(part) {
163 // call open-ils to get overdue notice count and Last notice date
167 'open-ils.actor.user.itemsout.notices',
168 egCore.auth.token(), circ.id(), $scope.patron_id)
169 .then(function(notice){
170 if (notice.numNotices){
171 circ.action_trigger_event_count = notice.numNotices;
172 circ.action_trigger_latest_event_date = notice.lastDt;
174 patronSvc.items_out.push(circ);
177 if (rendered++ >= offset && rendered <= count){ deferred.notify(circ) };
180 return deferred.promise;
183 function fetch_noncat_circs(id_list, offset, count) {
184 if (!id_list.length) return $q.when();
186 var deferred = $q.defer();
189 egCore.pcrud.search('ancc', {id : id_list},
191 flesh_fields : {ancc : ['item_type','staff']},
192 // TODO: LP#1697954 Fetch all circs on grid render
193 // to support client-side sorting. Migrate to server-side
194 // sorting to avoid the need for fetching all items.
197 // we need an order-by to support paging
198 order_by : {circ : ['circ_time']}
200 }).then(deferred.resolve, null, function(noncat_circ) {
202 // calculate the virtual due date from the item type duration
203 var seconds = egCore.date.intervalToSeconds(
204 noncat_circ.item_type().circ_duration());
205 var d = new Date(Date.parse(noncat_circ.circ_time()));
206 d.setSeconds(d.getSeconds() + seconds);
207 noncat_circ.duedate(d.toISOString());
209 // local flesh org unit
210 noncat_circ.circ_lib(egCore.org.get(noncat_circ.circ_lib()));
212 patronSvc.items_out.push(noncat_circ); // cache it
214 // We fetch all noncat circs for client-side sorting, but
215 // only notify the caller for the page of requested circs.
216 if (rendered++ >= offset && rendered <= count)
217 deferred.notify(noncat_circ);
220 return deferred.promise;
224 // decide which list each circ belongs to
225 function promote_circs(list, display_code, open) {
227 if (1 & display_code) { // bitflag 1 == top list
228 $scope.main_list = $scope.main_list.concat(list);
230 $scope.alt_list = $scope.alt_list.concat(list);
233 if (4 & display_code) return; // bitflag 4 == hide on checkin
234 $scope.alt_list = $scope.alt_list.concat(list);
238 // fetch IDs for circs we care about
239 function get_circ_ids() {
240 $scope.main_list = [];
241 $scope.alt_list = [];
243 // we can fetch these in parallel
244 var promise1 = egCore.net.request(
246 'open-ils.actor.user.checked_out.authoritative',
247 egCore.auth.token(), $scope.patron_id
248 ).then(function(outs) {
249 $scope.main_list = outs.overdue.concat(outs.out);
250 promote_circs(outs.lost, display_lost, true);
251 promote_circs(outs.long_overdue, display_lo, true);
252 promote_circs(outs.claims_returned, display_cr, true);
255 // only fetched checked-in-with-bills circs if configured to display
256 var promise2 = !fetch_checked_in ? $q.when() : egCore.net.request(
258 'open-ils.actor.user.checked_in_with_fines.authoritative',
259 egCore.auth.token(), $scope.patron_id
260 ).then(function(outs) {
261 promote_circs(outs.lost, display_lost);
262 promote_circs(outs.long_overdue, display_lo);
263 promote_circs(outs.claims_returned, display_cr);
266 return $q.all([promise1, promise2]);
269 provider.get = function(offset, count) {
271 var id_list = $scope[$scope.items_out_display + '_list'];
273 // see if we have the requested range cached
274 // Note this items_out list is reset w/ each items-out tab change
275 if (patronSvc.items_out[offset]) {
276 return provider.arrayNotifier(
277 patronSvc.items_out, offset, count);
280 if ($scope.items_out_display == 'noncat') {
281 // if there are any noncat circ IDs, we already have them
282 return fetch_noncat_circs(id_list, offset, count);
285 // See if we have the circ IDs for this range already loaded.
286 // this would happen navigating to a subsequent page.
287 if (id_list[offset]) {
288 return fetch_circs(id_list, offset, count);
291 // avoid returning the request directly to the caller so the
292 // notify()'s from egCore.net.request don't leak into the
293 // final set of notifies (i.e. the real responses);
295 var deferred = $q.defer();
296 get_circ_ids().then(function() {
298 id_list = $scope[$scope.items_out_display + '_list'];
299 $scope.gridDataProvider.grid.totalCount = id_list.length;
300 // relay the notified circs back to the grid through our promise
301 fetch_circs(id_list, offset, count).then(
302 deferred.resolve, null, deferred.notify);
305 return deferred.promise;
309 // true if circ is overdue, false otherwise
310 $scope.circIsOverdue = function(circ) {
311 // circ may not exist yet for rendered row
312 if (!circ) return false;
314 var date = new Date();
315 date.setTime(Date.parse(circ.due_date()));
316 return date < new Date();
319 $scope.edit_due_date = function(items) {
320 if (!items.length) return;
323 templateUrl : './circ/patron/t_edit_due_date_dialog',
326 '$scope','$uibModalInstance',
327 function($scope , $uibModalInstance) {
329 // if there is only one circ, default to the due date
330 // of that circ. Otherwise, default to today.
331 var due_date = items.length == 1 ?
332 Date.parse(items[0].due_date()) : new Date();
335 num_circs : items.length,
339 // Fire off the due-date updater for each circ.
340 // When all is done, close the dialog
341 $scope.ok = function(args) {
342 var due = $scope.args.due_date.toISOString();
343 console.debug("applying due date of " + due);
346 angular.forEach(items, function(circ) {
350 'open-ils.circ.circulation.due_date.update',
351 egCore.auth.token(), circ.id(), due
353 ).then(function(new_circ) {
354 // update the grid circ with the canonical
355 // date from the modified circulation.
356 circ.due_date(new_circ.due_date());
361 $q.all(promises).then(function() {
362 $uibModalInstance.close();
366 $scope.cancel = function($event) {
367 $uibModalInstance.dismiss();
368 $event.preventDefault();
375 $scope.print_receipt = function(items) {
376 if (items.length == 0) return $q.when();
377 var print_data = {circulations : []};
378 var cusr = patronSvc.current;
380 angular.forEach(items, function(circ) {
381 print_data.circulations.push({
382 circ : egCore.idl.toHash(circ),
383 copy : egCore.idl.toHash(circ.target_copy()),
384 call_number : egCore.idl.toHash(circ.target_copy().call_number()),
385 title : circ.target_copy().call_number().record().wide_display_entry().title(),
386 author : circ.target_copy().call_number().record().wide_display_entry().author()
390 print_data.patron = {
391 prefix : cusr.prefix(),
392 first_given_name : cusr.first_given_name(),
393 second_given_name : cusr.second_given_name(),
394 family_name : cusr.family_name(),
395 suffix : cusr.suffix(),
396 pref_prefix : cusr.pref_prefix(),
397 pref_first_given_name : cusr.pref_first_given_name(),
398 pref_second_given_name : cusr.pref_second_given_name(),
399 pref_family_name : cusr.pref_family_name(),
400 pref_suffix : cusr.pref_suffix(),
401 card : { barcode : cusr.card().barcode() },
402 money_summary : patronSvc.patron_stats.fines,
403 expire_date : cusr.expire_date(),
404 alias : cusr.alias(),
405 has_email : Boolean(patronSvc.current.email() && patronSvc.current.email().match(/.*@.*/)),
406 has_phone : Boolean(cusr.day_phone() || cusr.evening_phone() || cusr.other_phone())
409 return egCore.print.print({
411 template : 'items_out',
416 function batch_action_with_flat_copies(items, action) {
417 if (!items.length) return;
418 var copies = items.map(function(circ)
419 { return egCore.idl.toHash(circ.target_copy()) });
420 action(copies).then(reset_page);
422 function batch_action_with_barcodes(items, action) {
423 if (!items.length) return;
424 var barcodes = items.map(function(circ)
425 { return circ.target_copy().barcode() });
426 action(barcodes).then(reset_page);
428 $scope.mark_damaged = function(items) {
429 if (items.length == 0) return;
431 angular.forEach(items, function(circ) {
432 egCirc.mark_damaged({
433 id: circ.target_copy().id(),
434 barcode: circ.target_copy().barcode(),
435 circ_lib: circ.target_copy().circ_lib().id()
436 }).then(() => $timeout(reset_page,1000)) // reset after each, because rejecting one stops the $q.all() chain
439 $scope.mark_missing = function(items) {
440 batch_action_with_flat_copies(items, egCirc.mark_missing);
442 $scope.mark_lost = function(items) {
443 batch_action_with_barcodes(items, egCirc.mark_lost);
445 $scope.mark_claims_returned = function(items) {
446 batch_action_with_barcodes(items, egCirc.mark_claims_returned_dialog);
448 $scope.mark_claims_never_checked_out = function(items) {
449 batch_action_with_barcodes(items, egCirc.mark_claims_never_checked_out);
452 $scope.show_recent_circs = function(items) {
453 var focus = items.length == 1;
454 angular.forEach(items, function(item) {
455 var url = egCore.env.basePath +
457 item.target_copy().id() +
459 $timeout(function() { var x = $window.open(url, '_blank'); if (focus) x.focus() });
463 $scope.show_triggered_events = function(items) {
464 var focus = items.length == 1;
465 angular.forEach(items, function(item) {
466 var url = egCore.env.basePath +
468 item.target_copy().id() +
470 $timeout(function() { var x = $window.open(url, '_blank'); if (focus) x.focus() });
474 $scope.renew = function(items, msg) {
475 if (!items.length) return;
476 var barcodes = items.map(function(circ)
477 { return circ.target_copy().barcode() });
479 if (!msg) msg = egCore.strings.RENEW_ITEMS;
481 return egConfirmDialog.open(msg, barcodes.join(' '), {}).result
484 var bc = barcodes.pop();
485 if (!bc) { reset_page(); return }
486 // finally -> continue even when one fails
487 egCirc.renew({copy_barcode : bc}).finally(do_one);
493 $scope.renew_all = function() {
494 var circs = patronSvc.items_out.filter(function(circ) {
496 // all others will be rejected at the server
497 !circ.stop_fines() ||
498 circ.stop_fines() == 'MAXFINES'
501 $scope.renew(circs, egCore.strings.RENEW_ALL_ITEMS);
504 $scope.renew_with_date = function(items) {
505 if (!items.length) return;
506 var barcodes = items.map(function(circ)
507 { return circ.target_copy().barcode() });
509 return $uibModal.open({
510 templateUrl : './circ/patron/t_renew_with_date_dialog',
513 '$scope','$uibModalInstance',
514 function($scope , $uibModalInstance) {
515 var now = new Date();
516 $scope.outOfRange = false;
517 $scope.minDate = new Date(now);
522 $scope.cancel = function() {$uibModalInstance.dismiss()}
524 // Fire off the due-date updater for each circ.
525 // When all is done, close the dialog
526 $scope.ok = function() {
527 var due = $scope.args.date.toISOString().replace(/T.*/,'');
528 console.debug("renewing with due date: " + due);
531 if (bc = barcodes.pop()) {
532 egCirc.renew({copy_barcode : bc, due_date : due})
535 $uibModalInstance.close();
539 do_one(); // kick it off
546 $scope.checkin = function(items) {
547 if (!items.length) return;
548 var copies = items.map(function(circ) { return circ.target_copy() });
549 var barcodes = copies.map(function(copy) { return copy.barcode() });
553 if (copy = copies.pop()) {
554 // Checkin expects a barcode, but will pass other
555 // parameters too. Passing the copy ID allows
556 // for the checkin of deleted copies on the server.
558 {copy_barcode: copy.barcode(), copy_id: copy.id()},
559 {suppress_popups: $scope.suppress_popups})
565 if ($scope.suppress_popups) {
568 return egConfirmDialog.open(
569 egCore.strings.CHECK_IN_CONFIRM, barcodes.join(' '), {
571 }).result.then(function() {
572 do_one(); // kick it off
577 $scope.add_billing = function(items) {
578 if (!items.length) return;
579 var circs = items.concat(); // don't pop from grid array
581 var circ; // don't clobber window.circ!
582 if (circ = circs.pop()) {
583 egBilling.showBillDialog({
584 // let the dialog fetch the transaction, since it's
585 // not sufficiently fleshed here.
587 patron : patronSvc.current