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 // Max recents setting is loaded and scrubbed during egStartup.
52 // Copy it to a local variable here for ease of local access
53 // after startup has run.
54 egCore.startup.go().then(
56 egCore.org.settings('ui.staff.max_recent_patrons')
58 service.maxRecentPatrons = s['ui.staff.max_recent_patrons'];
63 // Returns true if the last alerted patron matches the current
64 // patron. Otherwise, the last alerted patron is set to the
65 // current patron and false is returned.
66 service.alertsShown = function() {
67 var key = 'eg.circ.last_alerted_patron';
68 var last_id = egCore.hatch.getSessionItem(key);
69 if (last_id && last_id == service.current.id()) return true;
70 egCore.hatch.setSessionItem(key, service.current.id());
74 // shortcut to force-reload the current primary
75 service.refreshPrimary = function() {
76 if (!service.current) return $q.when();
77 return service.setPrimary(service.current.id(), null, true);
80 // clear the currently focused user
81 service.clearPrimary = function() {
82 // reset with no patron
83 service.resetPatronLists();
84 service.current = null;
85 service.patron_stats = null;
89 service.getRecentPatrons = function() {
90 // avoid getting stuck in a show-recent loop
91 service.showRecent = false;
93 if (service.maxRecentPatrons < 1) return $q.when();
95 egCore.hatch.getLoginSessionItem('eg.circ.recent_patrons') || [];
97 // Ensure the cached list is no bigger than the current config.
98 // This can happen if the setting changes while logged in.
99 patrons = patrons.slice(0, service.maxRecentPatrons);
101 // add home_ou to the list of fleshed fields for recent patrons
102 var fleshFields = egUser.defaultFleshFields.slice(0);
103 fleshFields.push('home_ou');
105 var deferred = $q.defer();
107 if (patrons.length == 0) {
111 egUser.get(patrons[0], {useFields : fleshFields}).then(
112 function(usr) { // fetch first user
113 deferred.notify(usr);
114 patrons.splice(0, 1); // remove first user from list
121 return deferred.promise;
124 service.addRecentPatron = function(user_id) {
125 if (service.maxRecentPatrons < 1) return;
127 // no need to re-track same user
128 if (service.current && service.current.id() == user_id) return;
131 egCore.hatch.getLoginSessionItem('eg.circ.recent_patrons') || [];
132 patrons.splice(0, 0, user_id); // put this user at front
133 patrons.splice(service.maxRecentPatrons, 1); // remove excess
135 // remove any other occurrences of this user, which may have been
136 // added before the most recent user.
137 var idx = patrons.indexOf(user_id, 1);
138 if (idx > 0) patrons.splice(idx, 1);
140 egCore.hatch.setLoginSessionItem('eg.circ.recent_patrons', patrons);
143 // sets the primary display user, fetching data as necessary.
144 service.setPrimary = function(id, user, force) {
145 var user_id = id ? id : (user ? user.id() : null);
147 console.debug('setting primary user to: ' + user_id);
149 if (!user_id) return $q.reject();
151 service.addRecentPatron(user_id);
153 // avoid running multiple retrievals for the same patron, which
154 // can happen during dbl-click by maintaining a single running
155 // data retrieval promise
156 if (service.primaryUserPromise) {
157 if (service.primaryUserId == user_id) {
158 return service.primaryUserPromise.promise;
160 service.primaryUserPromise = null;
164 service.primaryUserPromise = $q.defer();
165 service.primaryUserId = user_id;
167 service.getPrimary(id, user, force)
169 service.checkAlerts();
170 var p = service.primaryUserPromise;
171 service.primaryUserId = null;
172 // clear before resolution just to be safe.
173 service.primaryUserPromise = null;
177 return service.primaryUserPromise.promise;
180 service.getPrimary = function(id, user, force) {
183 if (!force && service.current &&
184 service.current.id() == user.id()) {
185 if (service.patron_stats) {
188 return service.fetchUserStats();
192 service.resetPatronLists();
194 return service.checkOptIn(user).then(
196 service.current = user;
197 service.localFlesh(user);
198 return service.fetchUserStats();
206 if (!force && service.current && service.current.id() == id) {
207 if (service.patron_stats) {
210 return service.fetchUserStats();
214 service.resetPatronLists();
216 return egUser.get(id).then(
218 return service.checkOptIn(user).then(
220 service.current = user;
221 service.localFlesh(user);
222 return service.fetchUserStats();
231 "unable to fetch user "+id+': '+js2JSON(err))
236 // fetching a null user clears the primary user.
237 // NOTE: this should probably reject() and log an error,
238 // but calling clear for backwards compat for now.
239 return service.clearPrimary();
243 // flesh some additional user fields locally
244 service.localFlesh = function(user) {
245 if (!angular.isObject(typeof user.home_ou()))
246 user.home_ou(egCore.org.get(user.home_ou()));
249 user.standing_penalties(),
251 if (!angular.isObject(penalty.org_unit()))
252 penalty.org_unit(egCore.org.get(penalty.org_unit()));
256 // stat_cat_entries == stat_cat_entry_user_map
257 angular.forEach(user.stat_cat_entries(), function(map) {
258 if (angular.isObject(map.stat_cat())) return;
259 // At page load, we only retrieve org-visible stat cats.
260 // For the common case, ignore entries for remote stat cats.
261 var cat = egCore.env.actsc.map[map.stat_cat()];
264 cat.owner(egCore.org.get(cat.owner()));
269 // resolves to true if the patron account has expired or will
270 // expire soon, based on YAOUS circ.patron_expires_soon_warning
271 // note: returning a promise is no longer strictly necessary
272 // (no more async activity) if the calling function is changed too.
273 service.testExpire = function() {
275 var expire = Date.parse(service.current.expire_date());
276 if (expire < new Date()) {
277 return $q.when(service.patronExpired = true);
280 var soon = egCore.env.aous['circ.patron_expires_soon_warning'];
282 var preExpire = new Date();
283 preExpire.setDate(preExpire.getDate() + Number(soon));
284 if (expire < preExpire)
285 return $q.when(service.patronExpiresSoon = true);
288 return $q.when(false);
291 // resolves to true if the patron account has any invalid addresses.
292 service.testInvalidAddrs = function() {
294 if (service.invalidAddresses)
295 return $q.when(true);
300 service.current.addresses(),
301 function(addr) { if (addr.valid() == 'f') fail = true }
304 return $q.when(fail);
306 //resolves to true if the patron was fetched with an inactive card
307 service.fetchedWithInactiveCard = function() {
308 var bc = service.search_barcode
309 var cards = service.current.cards();
310 var card = cards.filter(function(c) { return c.barcode() == bc })[0];
311 return (card && card.active() == 'f');
313 // resolves to true if there is any aspect of the patron account
314 // which should produce a message in the alerts panel
315 service.checkAlerts = function() {
317 if (service.hasAlerts) // already checked
318 return $q.when(true);
320 var deferred = $q.defer();
321 var p = service.current;
323 if (service.alert_penalties.length ||
327 service.patron_stats.holds.ready) {
329 service.hasAlerts = true;
332 // see if the user was retrieved with an inactive card
333 if(service.fetchedWithInactiveCard()){
334 service.hasAlerts = true;
337 // regardless of whether we know of alerts, we still need
338 // to test/fetch the expire data for display
339 service.testExpire().then(function(bool) {
340 if (bool) service.hasAlerts = true;
341 deferred.resolve(service.hasAlerts);
344 service.testInvalidAddrs().then(function(bool) {
345 if (bool) service.invalidAddresses = true;
346 deferred.resolve(service.invalidAddresses);
349 return deferred.promise;
352 service.fetchGroupFines = function() {
353 return egCore.net.request(
355 'open-ils.actor.usergroup.members.balance_owed',
356 egCore.auth.token(), service.current.usrgroup()
357 ).then(function(list) {
359 angular.forEach(list, function(u) {
360 total += 100 * Number(u.balance_owed)
362 service.patron_stats.fines.group_balance_owed = total / 100;
366 service.getUserStats = function(id) {
367 return egCore.net.request(
369 'open-ils.actor.user.opac.vital_stats.authoritative',
370 egCore.auth.token(), id
373 // force numeric to ensure correct boolean handling in templates
374 stats.fines.balance_owed = Number(stats.fines.balance_owed);
375 stats.checkouts.overdue = Number(stats.checkouts.overdue);
376 stats.checkouts.claims_returned =
377 Number(stats.checkouts.claims_returned);
378 stats.checkouts.lost = Number(stats.checkouts.lost);
379 stats.checkouts.out = Number(stats.checkouts.out);
380 stats.checkouts.total_out =
381 stats.checkouts.out + stats.checkouts.overdue;
383 stats.checkouts.total_out += Number(stats.checkouts.long_overdue);
385 if (!egCore.env.aous['circ.do_not_tally_claims_returned'])
386 stats.checkouts.total_out += stats.checkouts.claims_returned;
388 if (egCore.env.aous['circ.tally_lost'])
389 stats.checkouts.total_out += stats.checkouts.lost
396 // Fetches the IDs of any active non-cat checkouts for the current
397 // user. Also sets the patron_stats non_cat count value to match.
398 service.getUserNonCats = function(id) {
399 return egCore.net.request(
401 'open-ils.circ.open_non_cataloged_circulation.user.authoritative',
402 egCore.auth.token(), id
403 ).then(function(noncat_ids) {
404 service.noncat_ids = noncat_ids;
405 service.patron_stats.checkouts.noncat = noncat_ids.length;
409 // grab additional circ info
410 service.fetchUserStats = function() {
411 return service.getUserStats(service.current.id())
412 .then(function(stats) {
413 service.patron_stats = stats
414 service.alert_penalties = service.current.standing_penalties()
415 .filter(function(pen) {
416 return pen.standing_penalty().staff_alert() == 't'
419 service.summary_stat_cats = [];
420 angular.forEach(service.current.stat_cat_entries(),
422 if (angular.isObject(map.stat_cat()) &&
423 map.stat_cat().usr_summary() == 't') {
424 service.summary_stat_cats.push(map);
429 // run these two in parallel
430 var p1 = service.getUserNonCats(service.current.id());
431 var p2 = service.fetchGroupFines();
432 return $q.all([p1, p2]);
436 service.createOptIn = function(user_id) {
437 return egCore.net.request(
439 'open-ils.actor.user.org_unit_opt_in.create',
440 egCore.auth.token(), user_id);
443 service.checkOptIn = function(user) {
444 var deferred = $q.defer();
447 'open-ils.actor.user.org_unit_opt_in.check',
448 egCore.auth.token(), user.id())
449 .then(function(optInResp) {
450 if (eg_evt = egCore.evt.parse(optInResp)) {
452 console.log('error on opt-in check: ' + eg_evt);
453 } else if (optInResp == 2) {
454 // opt-in disallowed at this location by patron's home library
456 alert(egCore.strings.OPT_IN_RESTRICTED);
457 } else if (optInResp == 1) {
458 // opt-in handled or not needed, do nothing
461 // opt-in needed, show the opt-in dialog
462 var org = egCore.org.get(user.home_ou());
463 egConfirmDialog.open(
464 egCore.strings.OPT_IN_DIALOG_TITLE,
465 egCore.strings.OPT_IN_DIALOG,
466 { family_name : user.family_name(),
467 first_given_name : user.first_given_name(),
468 org_name : org.name(),
469 org_shortname : org.shortname(),
471 service.createOptIn(user.id())
472 .then(function(resp) {
473 if (evt = egCore.evt.parse(resp)) {
481 cancel : function() { deferred.reject(); }
486 return deferred.promise;
489 // Avoid using parens [e.g. (1.23)] to indicate negative numbers,
490 // which is the Angular default.
491 // http://stackoverflow.com/questions/17441254/why-angularjs-currency-filter-formats-negative-numbers-with-parenthesis
492 // FIXME: This change needs to be moved into a project-wide collection
493 // of locale overrides.
494 $locale.NUMBER_FORMATS.PATTERNS[1].negPre = '-';
495 $locale.NUMBER_FORMATS.PATTERNS[1].negSuf = '';
501 * Manages patron search
503 .controller('BasePatronSearchCtrl',
504 ['$scope','$q','$routeParams','$timeout','$window','$location','egCore',
505 '$filter','egUser', 'patronSvc','egGridDataProvider','$document',
507 function($scope, $q, $routeParams, $timeout, $window, $location, egCore,
508 $filter, egUser, patronSvc , egGridDataProvider , $document,
511 $scope.focusMe = true;
512 $scope.searchArgs = {
513 // default to searching globally
514 home_ou : egCore.org.tree()
517 // last used patron search form element
520 $scope.gridControls = {
521 selectedItems : function() {return []}
524 // The first time we encounter the show-recent CGI param, put the
525 // service into show-recent mode. The first time recents are shown,
526 // the service is taken out of show-recent mode so the page does not
527 // get stuck in a show-recent loop.
528 if (patronSvc.showRecent === undefined
529 && Boolean($location.path().match(/search/))
530 && Boolean($location.search().show_recent)) {
531 patronSvc.showRecent = true;
534 // Handle URL-encoded searches
535 if ($location.search().search) {
536 console.log('URL search = ' + $location.search().search);
537 patronSvc.urlSearch = {search : JSON2js($location.search().search)};
539 // why the double-JSON encoded sort?
540 if (patronSvc.urlSearch.search.search_sort) {
541 patronSvc.urlSearch.sort =
542 JSON2js(patronSvc.urlSearch.search.search_sort);
544 patronSvc.urlSearch.sort = [];
546 delete patronSvc.urlSearch.search.search_sort;
548 // include inactive patrons if "inactive" param
549 if ($location.search().inactive) {
550 patronSvc.urlSearch.inactive = $location.search().inactive;
555 var propagate_inactive;
556 if (patronSvc.lastSearch && !patronSvc.showRecent) {
557 propagate = patronSvc.lastSearch.search;
558 // home_ou needs to be treated specially
559 propagate.home_ou = {
560 value : patronSvc.lastSearch.home_ou,
563 } else if (patronSvc.urlSearch) {
564 propagate = patronSvc.urlSearch.search;
565 if (patronSvc.urlSearch.inactive) {
566 propagate_inactive = patronSvc.urlSearch.inactive;
570 if (egCore.env.pgt) {
571 $scope.profiles = egCore.env.pgt.list;
573 egCore.pcrud.search('pgt', {parent : null},
574 {flesh : -1, flesh_fields : {pgt : ['children']}}
577 egCore.env.absorbTree(tree, 'pgt')
578 $scope.profiles = egCore.env.pgt.list;
584 // populate the search form with our cached / preexisting search info
585 angular.forEach(propagate, function(val, key) {
586 if (key == 'profile')
587 val.value = $scope.profiles.filter(function(p) { return p.id() == val.value })[0];
588 if (key == 'home_ou')
589 val.value = egCore.org.get(val.value);
590 $scope.searchArgs[key] = val.value;
592 if (propagate_inactive) {
593 $scope.searchArgs.inactive = propagate_inactive;
597 var provider = egGridDataProvider.instance({});
599 provider.get = function(offset, count) {
600 var deferred = $q.defer();
602 if (patronSvc.showRecent) {
603 // avoid getting stuck in show-recent mode
604 return patronSvc.getRecentPatrons();
608 if (patronSvc.urlSearch) {
609 fullSearch = patronSvc.urlSearch;
610 // enusre the urlSearch only runs once.
611 delete patronSvc.urlSearch;
614 patronSvc.search_barcode = $scope.searchArgs.card;
616 var search = compileSearch($scope.searchArgs);
617 if (Object.keys(search) == 0) return $q.when();
619 var home_ou = search.home_ou;
620 delete search.home_ou;
621 var inactive = search.inactive;
622 delete search.inactive;
626 sort : compileSort(),
632 fullSearch.count = count;
633 fullSearch.offset = offset;
635 if (patronSvc.lastSearch) {
636 // search repeated, return the cached results
637 if (angular.equals(fullSearch, patronSvc.lastSearch)) {
638 console.log('patron search returning ' +
639 patronSvc.patrons.length + ' cached results');
641 // notify has to happen after returning the promise
644 angular.forEach(patronSvc.patrons, function(user) {
645 deferred.notify(user);
650 return deferred.promise;
654 patronSvc.lastSearch = fullSearch;
656 if (fullSearch.search.id) {
657 // search by user id performs a direct ID lookup
658 var userId = fullSearch.search.id.value;
661 egUser.get(userId).then(function(user) {
662 patronSvc.localFlesh(user);
663 patronSvc.patrons = [user];
664 deferred.notify(user);
669 return deferred.promise;
672 if (!Object.keys(fullSearch.search).length) {
673 // Empty searches are rejected by the server. Avoid
674 // running the the empty search that runs on page load.
678 egProgressDialog.open(); // Indeterminate
680 patronSvc.patrons = [];
681 var which_sound = 'success';
684 'open-ils.actor.patron.search.advanced.fleshed',
691 egUser.defaultFleshFields,
698 function() { // onerror
699 which_sound = 'error';
702 // hide progress bar as soon as the first result appears.
703 egProgressDialog.close();
704 patronSvc.localFlesh(user); // inline
705 patronSvc.patrons.push(user);
706 deferred.notify(user);
708 )['finally'](function() { // close on 0-hits or error
709 if (which_sound == 'success' && patronSvc.patrons.length == 0) {
710 which_sound = 'warning';
712 egCore.audio.play(which_sound + '.patron.by_search');
713 egProgressDialog.close();
716 return deferred.promise;
719 $scope.patronSearchGridProvider = provider;
721 // determine the tree depth of the profile group
722 $scope.pgt_depth = function(grp) {
724 while (grp = egCore.env.pgt.map[grp.parent()]) d++;
728 $scope.clearForm = function () {
729 $scope.searchArgs={};
730 if (lastFormElement) lastFormElement.focus();
733 $scope.applyShowExtras = function($event, bool) {
735 $scope.showExtras = true;
736 egCore.hatch.setItem('eg.circ.patron.search.show_extras', true);
738 $scope.showExtras = false;
739 egCore.hatch.removeItem('eg.circ.patron.search.show_extras');
741 if (lastFormElement) lastFormElement.focus();
742 $event.preventDefault();
745 egCore.hatch.getItem('eg.circ.patron.search.show_extras')
746 .then(function(val) {$scope.showExtras = val});
748 // map form arguments into search params
749 function compileSearch(args) {
751 angular.forEach(args, function(val, key) {
753 if (key == 'profile' && args.profile) {
754 search.profile = {value : args.profile.id(), group : 0};
755 } else if (key == 'home_ou' && args.home_ou) {
756 search.home_ou = args.home_ou.id(); // passed separately
757 } else if (key == 'inactive') {
758 search.inactive = val;
760 search[key] = {value : val, group : 0};
762 if (key.match(/phone|ident/)) {
763 search[key].group = 2;
765 if (key.match(/street|city|state|post_code/)) {
766 search[key].group = 1;
767 } else if (key == 'card') {
768 search[key].group = 3
769 } else if (key.match(/dob_/)) {
770 // DOB should always be numeric
771 search[key].value = search[key].value.replace(/\D/g,'');
772 if (search[key].value.length == 0) {
776 if (!key.match(/year/)) {
777 search[key].value = ('0'+search[key].value).slice(-2);
779 search[key].group = 4;
788 function compileSort() {
790 if (!provider.sort.length) {
793 "first_given_name ASC",
794 "second_given_name ASC",
803 if (angular.isObject(sortdef)) {
804 var name = Object.keys(sortdef)[0];
805 var dir = sortdef[name];
806 sort.push(name + ' ' + dir);
816 $scope.setLastFormElement = function() {
817 lastFormElement = $document[0].activeElement;
820 // search form submit action; tells the results grid to
822 $scope.search = function(args) { // args === $scope.searchArgs
823 if (args && Object.keys(args).length)
824 $scope.gridControls.refresh();
825 if (lastFormElement) lastFormElement.focus();
828 // TODO: move this into the (forthcoming) grid row activate action
829 $scope.onPatronDblClick = function($event, user) {
830 $location.path('/circ/patron/' + user.id() + '/checkout');
833 if (patronSvc.urlSearch) {
834 // force the grid to load the url-based search on page load
838 $scope.need_two_selected = function() {
839 var items = $scope.gridControls.selectedItems();
840 return (items.length == 2) ? false : true;