5 angular.module('egPatronSearchMod', ['ngRoute', 'ui.bootstrap',
6 'egCoreMod', 'egUiMod', 'egGridMod', 'egUserMod'])
12 ['$q','$timeout','$location','egCore','egUser','egConfirmDialog','$locale',
13 function($q , $timeout , $location , egCore, egUser , egConfirmDialog , $locale) {
16 // cached patron search results
19 // currently selected patron object
22 // patron circ stats (overdues, fines, holds)
25 // event types manually overridden, which should always be
26 // overridden for checkouts to this patron for this instance of
28 checkout_overrides : {},
29 //holds the searched barcode
30 search_barcode : null,
33 // when we change the default patron, we need to clear out any
34 // data collected on that patron
35 service.resetPatronLists = function() {
36 service.checkouts = [];
37 service.items_out = []
38 service.items_out_ids = [];
40 service.hold_ids = [];
41 service.checkout_overrides = {};
42 service.patron_stats = null;
43 service.noncat_ids = [];
44 service.hasAlerts = false;
45 service.patronExpired = false;
46 service.patronExpiresSoon = false;
47 service.invalidAddresses = false;
49 service.resetPatronLists(); // initialize
51 // Returns true if the last alerted patron matches the current
52 // patron. Otherwise, the last alerted patron is set to the
53 // current patron and false is returned.
54 service.alertsShown = function() {
55 var key = 'eg.circ.last_alerted_patron';
56 var last_id = egCore.hatch.getSessionItem(key);
57 if (last_id && last_id == service.current.id()) return true;
58 egCore.hatch.setSessionItem(key, service.current.id());
62 // shortcut to force-reload the current primary
63 service.refreshPrimary = function() {
64 if (!service.current) return $q.when();
65 return service.setPrimary(service.current.id(), null, true);
68 // clear the currently focused user
69 service.clearPrimary = function() {
70 // reset with no patron
71 service.resetPatronLists();
72 service.current = null;
73 service.patron_stats = null;
77 service.getRecentPatrons = function() {
78 // avoid getting stuck in a show-recent loop
79 service.showRecent = false;
81 if (service.maxRecentPatrons < 1) return $q.when();
83 egCore.hatch.getLoginSessionItem('eg.circ.recent_patrons') || [];
85 // Ensure the cached list is no bigger than the current config.
86 // This can happen if the setting changes while logged in.
87 patrons = patrons.slice(0, service.maxRecentPatrons);
89 // add home_ou to the list of fleshed fields for recent patrons
90 var fleshFields = egUser.defaultFleshFields.slice(0);
91 fleshFields.push('home_ou');
93 var deferred = $q.defer();
95 if (patrons.length == 0) {
99 egUser.get(patrons[0], {useFields : fleshFields}).then(
100 function(usr) { // fetch first user
101 deferred.notify(usr);
102 patrons.splice(0, 1); // remove first user from list
109 return deferred.promise;
112 service.addRecentPatron = function(user_id) {
113 if (service.maxRecentPatrons < 1) return;
115 // no need to re-track same user
116 if (service.current && service.current.id() == user_id) return;
119 egCore.hatch.getLoginSessionItem('eg.circ.recent_patrons') || [];
120 patrons.splice(0, 0, user_id); // put this user at front
121 patrons.splice(service.maxRecentPatrons, 1); // remove excess
123 // remove any other occurrences of this user, which may have been
124 // added before the most recent user.
125 var idx = patrons.indexOf(user_id, 1);
126 if (idx > 0) patrons.splice(idx, 1);
128 egCore.hatch.setLoginSessionItem('eg.circ.recent_patrons', patrons);
131 // sets the primary display user, fetching data as necessary.
132 service.setPrimary = function(id, user, force) {
133 var user_id = id ? id : (user ? user.id() : null);
135 console.debug('setting primary user to: ' + user_id);
137 if (!user_id) return $q.reject();
139 service.addRecentPatron(user_id);
141 // avoid running multiple retrievals for the same patron, which
142 // can happen during dbl-click by maintaining a single running
143 // data retrieval promise
144 if (service.primaryUserPromise) {
145 if (service.primaryUserId == user_id) {
146 return service.primaryUserPromise.promise;
148 service.primaryUserPromise = null;
152 service.primaryUserPromise = $q.defer();
153 service.primaryUserId = user_id;
155 service.getPrimary(id, user, force)
157 service.checkAlerts();
158 var p = service.primaryUserPromise;
159 service.primaryUserId = null;
160 // clear before resolution just to be safe.
161 service.primaryUserPromise = null;
165 return service.primaryUserPromise.promise;
168 service.getPrimary = function(id, user, force) {
171 if (!force && service.current &&
172 service.current.id() == user.id()) {
173 if (service.patron_stats) {
176 return service.fetchUserStats();
180 service.resetPatronLists();
182 return service.checkOptIn(user).then(
184 service.current = user;
185 service.localFlesh(user);
186 return service.fetchUserStats();
194 if (!force && service.current && service.current.id() == id) {
195 if (service.patron_stats) {
198 return service.fetchUserStats();
202 service.resetPatronLists();
204 return egUser.get(id).then(
206 return service.checkOptIn(user).then(
208 service.current = user;
209 service.localFlesh(user);
210 return service.fetchUserStats();
219 "unable to fetch user "+id+': '+js2JSON(err))
224 // fetching a null user clears the primary user.
225 // NOTE: this should probably reject() and log an error,
226 // but calling clear for backwards compat for now.
227 return service.clearPrimary();
231 // flesh some additional user fields locally
232 service.localFlesh = function(user) {
233 if (!angular.isObject(typeof user.home_ou()))
234 user.home_ou(egCore.org.get(user.home_ou()));
237 user.standing_penalties(),
239 if (!angular.isObject(penalty.org_unit()))
240 penalty.org_unit(egCore.org.get(penalty.org_unit()));
244 // stat_cat_entries == stat_cat_entry_user_map
245 angular.forEach(user.stat_cat_entries(), function(map) {
246 if (angular.isObject(map.stat_cat())) return;
247 // At page load, we only retrieve org-visible stat cats.
248 // For the common case, ignore entries for remote stat cats.
249 var cat = egCore.env.actsc.map[map.stat_cat()];
252 cat.owner(egCore.org.get(cat.owner()));
257 // resolves to true if the patron account has expired or will
258 // expire soon, based on YAOUS circ.patron_expires_soon_warning
259 // note: returning a promise is no longer strictly necessary
260 // (no more async activity) if the calling function is changed too.
261 service.testExpire = function() {
263 var expire = Date.parse(service.current.expire_date());
264 if (expire < new Date()) {
265 return $q.when(service.patronExpired = true);
268 var soon = egCore.env.aous['circ.patron_expires_soon_warning'];
270 var preExpire = new Date();
271 preExpire.setDate(preExpire.getDate() + Number(soon));
272 if (expire < preExpire)
273 return $q.when(service.patronExpiresSoon = true);
276 return $q.when(false);
279 // resolves to true if the patron account has any invalid addresses.
280 service.testInvalidAddrs = function() {
282 if (service.invalidAddresses)
283 return $q.when(true);
288 service.current.addresses(),
289 function(addr) { if (addr.valid() == 'f') fail = true }
292 return $q.when(fail);
294 //resolves to true if the patron was fetched with an inactive card
295 service.fetchedWithInactiveCard = function() {
296 var bc = service.search_barcode
297 var cards = service.current.cards();
298 var card = cards.filter(function(c) { return c.barcode() == bc })[0];
299 return (card && card.active() == 'f');
301 // resolves to true if there is any aspect of the patron account
302 // which should produce a message in the alerts panel
303 service.checkAlerts = function() {
305 if (service.hasAlerts) // already checked
306 return $q.when(true);
308 var deferred = $q.defer();
309 var p = service.current;
311 if (service.alert_penalties.length ||
315 service.patron_stats.holds.ready) {
317 service.hasAlerts = true;
320 // see if the user was retrieved with an inactive card
321 if(service.fetchedWithInactiveCard()){
322 service.hasAlerts = true;
325 // regardless of whether we know of alerts, we still need
326 // to test/fetch the expire data for display
327 service.testExpire().then(function(bool) {
328 if (bool) service.hasAlerts = true;
329 deferred.resolve(service.hasAlerts);
332 service.testInvalidAddrs().then(function(bool) {
333 if (bool) service.invalidAddresses = true;
334 deferred.resolve(service.invalidAddresses);
337 return deferred.promise;
340 service.fetchGroupFines = function() {
341 return egCore.net.request(
343 'open-ils.actor.usergroup.members.balance_owed',
344 egCore.auth.token(), service.current.usrgroup()
345 ).then(function(list) {
347 angular.forEach(list, function(u) {
348 total += 100 * Number(u.balance_owed)
350 service.patron_stats.fines.group_balance_owed = total / 100;
354 service.getUserStats = function(id) {
355 return egCore.net.request(
357 'open-ils.actor.user.opac.vital_stats.authoritative',
358 egCore.auth.token(), id
361 // force numeric to ensure correct boolean handling in templates
362 stats.fines.balance_owed = Number(stats.fines.balance_owed);
363 stats.checkouts.overdue = Number(stats.checkouts.overdue);
364 stats.checkouts.claims_returned =
365 Number(stats.checkouts.claims_returned);
366 stats.checkouts.lost = Number(stats.checkouts.lost);
367 stats.checkouts.out = Number(stats.checkouts.out);
368 stats.checkouts.total_out =
369 stats.checkouts.out + stats.checkouts.overdue;
371 stats.checkouts.total_out += Number(stats.checkouts.long_overdue);
373 if (!egCore.env.aous['circ.do_not_tally_claims_returned'])
374 stats.checkouts.total_out += stats.checkouts.claims_returned;
376 if (egCore.env.aous['circ.tally_lost'])
377 stats.checkouts.total_out += stats.checkouts.lost
384 // Fetches the IDs of any active non-cat checkouts for the current
385 // user. Also sets the patron_stats non_cat count value to match.
386 service.getUserNonCats = function(id) {
387 return egCore.net.request(
389 'open-ils.circ.open_non_cataloged_circulation.user.authoritative',
390 egCore.auth.token(), id
391 ).then(function(noncat_ids) {
392 service.noncat_ids = noncat_ids;
393 service.patron_stats.checkouts.noncat = noncat_ids.length;
397 // grab additional circ info
398 service.fetchUserStats = function() {
399 return service.getUserStats(service.current.id())
400 .then(function(stats) {
401 service.patron_stats = stats
402 service.alert_penalties = service.current.standing_penalties()
403 .filter(function(pen) {
404 return pen.standing_penalty().staff_alert() == 't'
407 service.summary_stat_cats = [];
408 angular.forEach(service.current.stat_cat_entries(),
410 if (angular.isObject(map.stat_cat()) &&
411 map.stat_cat().usr_summary() == 't') {
412 service.summary_stat_cats.push(map);
417 // run these two in parallel
418 var p1 = service.getUserNonCats(service.current.id());
419 var p2 = service.fetchGroupFines();
420 return $q.all([p1, p2]);
424 service.createOptIn = function(user_id) {
425 return egCore.net.request(
427 'open-ils.actor.user.org_unit_opt_in.create',
428 egCore.auth.token(), user_id);
431 service.checkOptIn = function(user) {
432 var deferred = $q.defer();
435 'open-ils.actor.user.org_unit_opt_in.check',
436 egCore.auth.token(), user.id())
437 .then(function(optInResp) {
438 if (eg_evt = egCore.evt.parse(optInResp)) {
440 console.log('error on opt-in check: ' + eg_evt);
441 } else if (optInResp == 2) {
442 // opt-in disallowed at this location by patron's home library
444 alert(egCore.strings.OPT_IN_RESTRICTED);
445 } else if (optInResp == 1) {
446 // opt-in handled or not needed, do nothing
449 // opt-in needed, show the opt-in dialog
450 var org = egCore.org.get(user.home_ou());
451 egConfirmDialog.open(
452 egCore.strings.OPT_IN_DIALOG_TITLE,
453 egCore.strings.OPT_IN_DIALOG,
454 { family_name : user.family_name(),
455 first_given_name : user.first_given_name(),
456 org_name : org.name(),
457 org_shortname : org.shortname(),
459 service.createOptIn(user.id())
460 .then(function(resp) {
461 if (evt = egCore.evt.parse(resp)) {
469 cancel : function() { deferred.reject(); }
474 return deferred.promise;
477 // Avoid using parens [e.g. (1.23)] to indicate negative numbers,
478 // which is the Angular default.
479 // http://stackoverflow.com/questions/17441254/why-angularjs-currency-filter-formats-negative-numbers-with-parenthesis
480 // FIXME: This change needs to be moved into a project-wide collection
481 // of locale overrides.
482 $locale.NUMBER_FORMATS.PATTERNS[1].negPre = '-';
483 $locale.NUMBER_FORMATS.PATTERNS[1].negSuf = '';
489 * Manages patron search
491 .controller('BasePatronSearchCtrl',
492 ['$scope','$q','$routeParams','$timeout','$window','$location','egCore',
493 '$filter','egUser', 'patronSvc','egGridDataProvider','$document',
495 function($scope, $q, $routeParams, $timeout, $window, $location, egCore,
496 $filter, egUser, patronSvc , egGridDataProvider , $document,
499 $scope.focusMe = true;
500 $scope.searchArgs = {
501 // default to searching globally
502 home_ou : egCore.org.tree()
505 // last used patron search form element
508 $scope.gridControls = {
509 selectedItems : function() {return []}
512 // Max recents setting is loaded and scrubbed during egStartup.
513 // Copy it to a local variable here for ease of local access.
514 egCore.org.settings('ui.staff.max_recent_patrons').then(function(s) {
515 patronSvc.maxRecentPatrons = s['ui.staff.max_recent_patrons'];
518 // The first time we encounter the show-recent CGI param, put the
519 // service into show-recent mode. The first time recents are shown,
520 // the service is taken out of show-recent mode so the page does not
521 // get stuck in a show-recent loop.
522 if (patronSvc.showRecent === undefined
523 && Boolean($location.path().match(/search/))
524 && Boolean($location.search().show_recent)) {
525 patronSvc.showRecent = true;
528 // Handle URL-encoded searches
529 if ($location.search().search) {
530 console.log('URL search = ' + $location.search().search);
531 patronSvc.urlSearch = {search : JSON2js($location.search().search)};
533 // why the double-JSON encoded sort?
534 if (patronSvc.urlSearch.search.search_sort) {
535 patronSvc.urlSearch.sort =
536 JSON2js(patronSvc.urlSearch.search.search_sort);
538 patronSvc.urlSearch.sort = [];
540 delete patronSvc.urlSearch.search.search_sort;
542 // include inactive patrons if "inactive" param
543 if ($location.search().inactive) {
544 patronSvc.urlSearch.inactive = $location.search().inactive;
549 var propagate_inactive;
550 if (patronSvc.lastSearch && !patronSvc.showRecent) {
551 propagate = patronSvc.lastSearch.search;
552 // home_ou needs to be treated specially
553 propagate.home_ou = {
554 value : patronSvc.lastSearch.home_ou,
557 } else if (patronSvc.urlSearch) {
558 propagate = patronSvc.urlSearch.search;
559 if (patronSvc.urlSearch.inactive) {
560 propagate_inactive = patronSvc.urlSearch.inactive;
564 if (egCore.env.pgt) {
565 $scope.profiles = egCore.env.pgt.list;
567 egCore.pcrud.search('pgt', {parent : null},
568 {flesh : -1, flesh_fields : {pgt : ['children']}}
571 egCore.env.absorbTree(tree, 'pgt')
572 $scope.profiles = egCore.env.pgt.list;
578 // populate the search form with our cached / preexisting search info
579 angular.forEach(propagate, function(val, key) {
580 if (key == 'profile')
581 val.value = $scope.profiles.filter(function(p) { return p.id() == val.value })[0];
582 if (key == 'home_ou')
583 val.value = egCore.org.get(val.value);
584 $scope.searchArgs[key] = val.value;
586 if (propagate_inactive) {
587 $scope.searchArgs.inactive = propagate_inactive;
591 var provider = egGridDataProvider.instance({});
593 provider.get = function(offset, count) {
594 var deferred = $q.defer();
596 if (patronSvc.showRecent) {
597 // avoid getting stuck in show-recent mode
598 return patronSvc.getRecentPatrons();
602 if (patronSvc.urlSearch) {
603 fullSearch = patronSvc.urlSearch;
604 // enusre the urlSearch only runs once.
605 delete patronSvc.urlSearch;
608 patronSvc.search_barcode = $scope.searchArgs.card;
610 var search = compileSearch($scope.searchArgs);
611 if (Object.keys(search) == 0) return $q.when();
613 var home_ou = search.home_ou;
614 delete search.home_ou;
615 var inactive = search.inactive;
616 delete search.inactive;
620 sort : compileSort(),
626 fullSearch.count = count;
627 fullSearch.offset = offset;
629 if (patronSvc.lastSearch) {
630 // search repeated, return the cached results
631 if (angular.equals(fullSearch, patronSvc.lastSearch)) {
632 console.log('patron search returning ' +
633 patronSvc.patrons.length + ' cached results');
635 // notify has to happen after returning the promise
638 angular.forEach(patronSvc.patrons, function(user) {
639 deferred.notify(user);
644 return deferred.promise;
648 patronSvc.lastSearch = fullSearch;
650 if (fullSearch.search.id) {
651 // search by user id performs a direct ID lookup
652 var userId = fullSearch.search.id.value;
655 egUser.get(userId).then(function(user) {
656 patronSvc.localFlesh(user);
657 patronSvc.patrons = [user];
658 deferred.notify(user);
663 return deferred.promise;
666 if (!Object.keys(fullSearch.search).length) {
667 // Empty searches are rejected by the server. Avoid
668 // running the the empty search that runs on page load.
672 egProgressDialog.open(); // Indeterminate
674 patronSvc.patrons = [];
675 var which_sound = 'success';
678 'open-ils.actor.patron.search.advanced.fleshed',
685 egUser.defaultFleshFields,
692 function() { // onerror
693 which_sound = 'error';
696 // hide progress bar as soon as the first result appears.
697 egProgressDialog.close();
698 patronSvc.localFlesh(user); // inline
699 patronSvc.patrons.push(user);
700 deferred.notify(user);
702 )['finally'](function() { // close on 0-hits or error
703 if (which_sound == 'success' && patronSvc.patrons.length == 0) {
704 which_sound = 'warning';
706 egCore.audio.play(which_sound + '.patron.by_search');
707 egProgressDialog.close();
710 return deferred.promise;
713 $scope.patronSearchGridProvider = provider;
715 // determine the tree depth of the profile group
716 $scope.pgt_depth = function(grp) {
718 while (grp = egCore.env.pgt.map[grp.parent()]) d++;
722 $scope.clearForm = function () {
723 $scope.searchArgs={};
724 if (lastFormElement) lastFormElement.focus();
727 $scope.applyShowExtras = function($event, bool) {
729 $scope.showExtras = true;
730 egCore.hatch.setItem('eg.circ.patron.search.show_extras', true);
732 $scope.showExtras = false;
733 egCore.hatch.removeItem('eg.circ.patron.search.show_extras');
735 if (lastFormElement) lastFormElement.focus();
736 $event.preventDefault();
739 egCore.hatch.getItem('eg.circ.patron.search.show_extras')
740 .then(function(val) {$scope.showExtras = val});
742 // map form arguments into search params
743 function compileSearch(args) {
745 angular.forEach(args, function(val, key) {
747 if (key == 'profile' && args.profile) {
748 search.profile = {value : args.profile.id(), group : 0};
749 } else if (key == 'home_ou' && args.home_ou) {
750 search.home_ou = args.home_ou.id(); // passed separately
751 } else if (key == 'inactive') {
752 search.inactive = val;
754 search[key] = {value : val, group : 0};
756 if (key.match(/phone|ident/)) {
757 search[key].group = 2;
759 if (key.match(/street|city|state|post_code/)) {
760 search[key].group = 1;
761 } else if (key == 'card') {
762 search[key].group = 3
763 } else if (key.match(/dob_/)) {
764 // DOB should always be numeric
765 search[key].value = search[key].value.replace(/\D/g,'');
766 if (search[key].value.length == 0) {
770 if (!key.match(/year/)) {
771 search[key].value = ('0'+search[key].value).slice(-2);
773 search[key].group = 4;
782 function compileSort() {
784 if (!provider.sort.length) {
787 "first_given_name ASC",
788 "second_given_name ASC",
797 if (angular.isObject(sortdef)) {
798 var name = Object.keys(sortdef)[0];
799 var dir = sortdef[name];
800 sort.push(name + ' ' + dir);
810 $scope.setLastFormElement = function() {
811 lastFormElement = $document[0].activeElement;
814 // search form submit action; tells the results grid to
816 $scope.search = function(args) { // args === $scope.searchArgs
817 if (args && Object.keys(args).length)
818 $scope.gridControls.refresh();
819 if (lastFormElement) lastFormElement.focus();
822 // TODO: move this into the (forthcoming) grid row activate action
823 $scope.onPatronDblClick = function($event, user) {
824 $location.path('/circ/patron/' + user.id() + '/checkout');
827 if (patronSvc.urlSearch) {
828 // force the grid to load the url-based search on page load
832 $scope.need_two_selected = function() {
833 var items = $scope.gridControls.selectedItems();
834 return (items.length == 2) ? false : true;