4 * Search, checkout, items out, holds, bills, edit, etc.
7 angular.module('egPatronApp', ['ngRoute', 'ui.bootstrap', 'egUserBucketMod',
8 'egCoreMod', 'egUiMod', 'egGridMod', 'egUserMod', 'ngToast',
11 .config(['ngToastProvider', function(ngToastProvider) {
12 ngToastProvider.configure({
13 verticalPosition: 'bottom',
18 .config(function($routeProvider, $locationProvider, $compileProvider) {
19 $locationProvider.html5Mode(true);
20 $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|mailto|blob):/); // grid export
22 // data loaded at startup which only requires an authtoken goes
23 // here. this allows the requests to be run in parallel instead of
24 // waiting until startup has completed.
25 var resolver = {delay : ['egCore','egUser', function(egCore , egUser) {
27 // fetch the org settings we care about during egStartup
28 // and toss them into egCore.env as egCore.env.aous[name] = value.
29 // note: only load settings here needed by all tabs; load tab-
30 // specific settings from within their respective controllers
31 egCore.env.classLoaders.aous = function() {
32 return egCore.org.settings([
33 'ui.staff.require_initials.patron_info_notes',
34 'circ.do_not_tally_claims_returned',
37 'ui.circ.show_billing_tab_on_bills',
38 'circ.patron_expires_soon_warning',
39 'ui.circ.items_out.lost',
40 'ui.circ.items_out.longoverdue',
41 'ui.circ.items_out.claimsreturned'
42 ]).then(function(settings) {
43 // local settings are cached within egOrg. Caching them
44 // again in egEnv just simplifies the syntax for access.
45 egCore.env.aous = settings;
49 egCore.env.loadClasses.push('aous');
51 // app-globally modify the default flesh fields for
52 // fleshed user retrieval.
53 if (egUser.defaultFleshFields.indexOf('profile') == -1) {
54 egUser.defaultFleshFields = egUser.defaultFleshFields.concat([
64 return egCore.startup.go();
67 $routeProvider.when('/circ/patron/search', {
68 templateUrl: './circ/patron/t_search',
69 controller: 'PatronSearchCtrl',
73 $routeProvider.when('/circ/patron/bcsearch', {
74 templateUrl: './circ/patron/t_bcsearch',
75 controller: 'PatronBarcodeSearchCtrl',
79 $routeProvider.when('/circ/patron/credentials', {
80 templateUrl: './circ/patron/t_credentials',
81 controller: 'PatronVerifyCredentialsCtrl',
85 $routeProvider.when('/circ/patron/last', {
86 templateUrl: './circ/patron/t_last_patron',
87 controller: 'PatronFetchLastCtrl',
91 // the following require a patron ID
93 $routeProvider.when('/circ/patron/:id/alerts', {
94 templateUrl: './circ/patron/t_alerts',
95 controller: 'PatronAlertsCtrl',
99 $routeProvider.when('/circ/patron/:id/checkout', {
100 templateUrl: './circ/patron/t_checkout',
101 controller: 'PatronCheckoutCtrl',
105 $routeProvider.when('/circ/patron/:id/items_out', {
106 templateUrl: './circ/patron/t_items_out',
107 controller: 'PatronItemsOutCtrl',
111 $routeProvider.when('/circ/patron/:id/holds', {
112 templateUrl: './circ/patron/t_holds',
113 controller: 'PatronHoldsCtrl',
117 $routeProvider.when('/circ/patron/:id/holds/create', {
118 templateUrl: './circ/patron/t_holds_create',
119 controller: 'PatronHoldsCreateCtrl',
123 $routeProvider.when('/circ/patron/:id/holds/:hold_id', {
124 templateUrl: './circ/patron/t_holds',
125 controller: 'PatronHoldsCtrl',
129 $routeProvider.when('/circ/patron/:id/hold/:hold_id', {
130 templateUrl: './circ/patron/t_hold_details',
131 controller: 'PatronHoldDetailsCtrl',
135 $routeProvider.when('/circ/patron/:id/bills', {
136 templateUrl: './circ/patron/t_bills',
137 controller: 'PatronBillsCtrl',
141 $routeProvider.when('/circ/patron/:id/bill/:xact_id/:xact_tab', {
142 templateUrl: './circ/patron/t_xact_details',
143 controller: 'XactDetailsCtrl',
147 $routeProvider.when('/circ/patron/:id/bill_history/:history_tab', {
148 templateUrl: './circ/patron/t_bill_history',
149 controller: 'BillHistoryCtrl',
153 $routeProvider.when('/circ/patron/:id/messages', {
154 templateUrl: './circ/patron/t_messages',
155 controller: 'PatronMessagesCtrl',
159 $routeProvider.when('/circ/patron/:id/edit', {
160 templateUrl: './circ/patron/t_edit',
161 controller: 'PatronRegCtrl',
165 $routeProvider.when('/circ/patron/:id/credentials', {
166 templateUrl: './circ/patron/t_credentials',
167 controller: 'PatronVerifyCredentialsCtrl',
171 $routeProvider.when('/circ/patron/:id/notes', {
172 templateUrl: './circ/patron/t_notes',
173 controller: 'PatronNotesCtrl',
177 $routeProvider.when('/circ/patron/:id/triggered_events', {
178 templateUrl: './circ/patron/t_triggered_events',
179 controller: 'PatronTriggeredEventsCtrl',
183 $routeProvider.when('/circ/patron/:id/message_center', {
184 templateUrl: './circ/patron/t_message_center',
185 controller: 'PatronMessageCenterCtrl',
189 $routeProvider.when('/circ/patron/:id/edit_perms', {
190 templateUrl: './circ/patron/t_edit_perms',
191 controller: 'PatronPermsCtrl',
195 $routeProvider.when('/circ/patron/:id/group', {
196 templateUrl: './circ/patron/t_group',
197 controller: 'PatronGroupCtrl',
201 $routeProvider.when('/circ/patron/:id/stat_cats', {
202 templateUrl: './circ/patron/t_stat_cats',
203 controller: 'PatronStatCatsCtrl',
207 $routeProvider.when('/circ/patron/:id/surveys', {
208 templateUrl: './circ/patron/t_surveys',
209 controller: 'PatronSurveyCtrl',
213 $routeProvider.otherwise({redirectTo : '/circ/patron/search'});
217 * Manages tabbed patron view.
218 * This is the parent scope of all patron tab scopes.
221 .controller('PatronCtrl',
222 ['$scope','$q','$location','$filter','egCore','egNet','egUser','egAlertDialog','egConfirmDialog','egPromptDialog','patronSvc',
223 function($scope, $q , $location , $filter , egCore , egNet , egUser , egAlertDialog , egConfirmDialog , egPromptDialog , patronSvc) {
225 $scope.is_patron_edit = function() {
226 return Boolean($location.path().match(/patron\/\d+\/edit$/));
229 // To support the fixed position patron edit actions bar,
230 // its markup has to live outside the scope of the patron
231 // edit controller. Insert a scope blob here that can be
232 // modifed from within the patron edit controller.
233 $scope.edit_passthru = {};
235 // returns true if a redirect occurs
236 function redirectToAlertPanel() {
238 $scope.alert_penalties =
239 function() {return patronSvc.alert_penalties}
241 if (patronSvc.alertsShown()) return false;
243 // if the patron has any unshown alerts, show them now
244 if (patronSvc.hasAlerts &&
245 !$location.path().match(/alerts$/)) {
248 .path('/circ/patron/' + patronSvc.current.id() + '/alerts')
249 .search('card', null);
253 // no alert required. If the patron has fines and the show-bills
254 // OUS is applied, direct to the bills page.
255 if ($scope.patron_stats().fines.balance_owed > 0 // TODO: != 0 ?
256 && egCore.env.aous['ui.circ.show_billing_tab_on_bills']
257 && !$location.path().match(/bills$/)) {
259 $scope.tab = 'bills';
261 .path('/circ/patron/' + patronSvc.current.id() + '/bills')
262 .search('card', null);
270 // called after each route-specified controller is instantiated.
271 // this doubles as a way to inform the top-level controller that
272 // egStartup.go() has completed, which means we are clear to
273 // fetch the patron, etc.
274 $scope.initTab = function(tab, patron_id) {
275 console.log('init tab ' + tab);
277 $scope.aous = egCore.env.aous;
278 $scope.auth_user_id = egCore.auth.user().id();
281 $scope.patron_id = patron_id;
282 return patronSvc.setPrimary($scope.patron_id)
284 // the page title context label comes from the tab.
285 egCore.strings.setPageTitle(
286 egCore.strings.PAGE_TITLE_PATRON_NAME,
287 egCore.strings['PAGE_TITLE_PATRON_' + tab.toUpperCase()],
288 { lname : patronSvc.current.family_name(),
289 fname : patronSvc.current.first_given_name(),
290 mname : patronSvc.current.second_given_name()
294 .then(function() {return patronSvc.checkAlerts()})
295 .then(redirectToAlertPanel)
297 $scope.ident_type_name = $scope.patron().ident_type().name()
298 $scope.hasIdentTypeName = $scope.ident_type_name.length > 0;
301 // No patron, use the tab name as the page title.
302 egCore.strings.setPageTitle(
303 egCore.strings['PAGE_TITLE_PATRON_' + tab.toUpperCase()]);
308 $scope._show_dob = {};
309 $scope.show_dob = function (val) {
310 if ($scope.patron()) {
311 if (typeof val != 'undefined') $scope._show_dob[$scope.patron().id()] = val;
312 return $scope._show_dob[$scope.patron().id()];
314 return !egCore.env.aous['circ.obscure_dob'];
317 $scope.obscure_dob = function() {
318 return egCore.env.aous && egCore.env.aous['circ.obscure_dob'];
320 $scope.now_show_dob = function() {
321 return egCore.env.aous && egCore.env.aous['circ.obscure_dob'] ?
322 $scope.show_dob() : true;
325 $scope.patron = function() { return patronSvc.current }
326 $scope.patron_stats = function() { return patronSvc.patron_stats }
327 $scope.summary_stat_cats = function() { return patronSvc.summary_stat_cats }
328 $scope.hasAlerts = function() { return patronSvc.hasAlerts }
329 $scope.isPatronExpired = function() { return patronSvc.patronExpired }
331 $scope.print_address = function(addr) {
334 template : 'patron_address',
336 patron : egCore.idl.toHash(patronSvc.current),
337 address : egCore.idl.toHash(addr)
342 $scope.toggle_expand_summary = function() {
343 if ($scope.collapsePatronSummary) {
344 $scope.collapsePatronSummary = false;
345 egCore.hatch.removeItem('eg.circ.patron.summary.collapse');
347 $scope.collapsePatronSummary = true;
348 egCore.hatch.setItem('eg.circ.patron.summary.collapse', true);
352 // always expand the patron summary in the search UI, regardless
353 // of stored preference.
354 $scope.collapse_summary = function() {
355 return $scope.tab != 'search' && $scope.collapsePatronSummary;
358 function _purge_account(dest_usr,override) {
361 'open-ils.actor.user.delete' + (override ? '.override' : ''),
363 $scope.patron().id(),
365 ).then(function(resp){
366 if (evt = egCore.evt.parse(resp)) {
367 if (evt.code == '2004' /* ACTOR_USER_DELETE_OPEN_XACTS */) {
368 egConfirmDialog.open(
369 egCore.strings.PATRON_PURGE_CONFIRM_TITLE, egCore.strings.PATRON_PURGE_OVERRIDE_PROMPT,
371 _purge_account(dest_usr,true);
378 location.href = egCore.env.basePath + '/circ/patron/search';
383 function _purge_account_with_destination(dest_barcode) {
384 egCore.pcrud.search('ac', {barcode : dest_barcode})
385 .then(function(card) {
387 egAlertDialog.open(egCore.strings.PATRON_PURGE_STAFF_BAD_BARCODE);
389 _purge_account(card.usr());
394 $scope.purge_account = function() {
395 egConfirmDialog.open(
396 egCore.strings.PATRON_PURGE_CONFIRM_TITLE, egCore.strings.PATRON_PURGE_CONFIRM,
398 egConfirmDialog.open(
399 egCore.strings.PATRON_PURGE_CONFIRM_TITLE, egCore.strings.PATRON_PURGE_LAST_CHANCE,
403 'open-ils.actor.user.has_work_perm_at',
404 egCore.auth.token(), 'STAFF_LOGIN', $scope.patron().id()
405 ).then(function(resp) {
406 var is_staff = resp.length > 0;
409 egCore.strings.PATRON_PURGE_STAFF_PROMPT,
410 null, // TODO: this would be cool if it worked: egCore.auth.user().card().barcode(),
411 {ok : function(barcode) {_purge_account_with_destination(barcode)}}
423 egCore.hatch.getItem('eg.circ.patron.summary.collapse')
424 .then(function(val) {$scope.collapsePatronSummary = Boolean(val)});
427 .controller('PatronBarcodeSearchCtrl',
428 ['$scope','$location','egCore','egConfirmDialog','egUser','patronSvc','$uibModal','$q',
429 function($scope , $location , egCore , egConfirmDialog , egUser , patronSvc , $uibModal , $q) {
430 $scope.selectMe = true; // focus text input
431 patronSvc.clearPrimary(); // clear the default user
433 // jump to the patron checkout UI
434 function loadPatron(user_id) {
435 egCore.audio.play('success.patron.by_barcode');
437 .path('/circ/patron/' + user_id + '/checkout')
438 .search('card', $scope.args.barcode);
439 patronSvc.search_barcode = $scope.args.barcode;
442 // create an opt-in=yes response for the loaded user
443 function createOptIn(user_id) {
446 'open-ils.actor.user.org_unit_opt_in.create',
447 egCore.auth.token(), user_id).then(function(resp) {
448 if (evt = egCore.evt.parse(resp)) return alert(evt);
454 $scope.submitBarcode = function(args) {
455 $scope.bcNotFound = null;
456 $scope.optInRestricted = false;
457 if (!args.barcode) return;
458 args.barcode = args.barcode.replace(/\s/g,'');
459 // blur so next time it's set to true it will re-apply select()
460 $scope.selectMe = false;
464 // given a scanned barcode, this function finds any matching users
465 // and handles multiple matches due to barcode completion
466 function handleBarcodeCompletion(scanned_barcode) {
467 var deferred = $q.defer();
471 'open-ils.actor.get_barcodes',
472 egCore.auth.token(), egCore.auth.user().ws_ou(),
473 'actor', scanned_barcode)
475 .then(function(resp) { // get_barcodes
477 if (evt = egCore.evt.parse(resp)) {
483 if (!resp || !resp[0]) {
484 $scope.bcNotFound = args.barcode;
485 $scope.selectMe = true;
486 egCore.audio.play('warning.patron.not_found');
491 if (resp.length == 1) {
492 // exactly one matching barcode: return it
494 user_id = resp[0].id;
496 // multiple matching barcodes: let the user pick one
497 var barcode_map = {};
500 var selected_barcode;
501 angular.forEach(resp, function(match) {
503 egUser.get(match.id, {useFields : ['home_ou']}).then(function(user) {
504 barcode_map[match.barcode] = user.id();
506 barcode: match.barcode,
507 title: user.first_given_name() + ' ' + user.family_name(),
508 org_name: user.home_ou().name(),
509 org_shortname: user.home_ou().shortname()
514 return $q.all(promises)
517 templateUrl: './circ/share/t_barcode_choice_dialog',
519 ['$scope', '$uibModalInstance',
520 function($scope, $uibModalInstance) {
521 $scope.matches = matches;
522 $scope.ok = function(barcode) {
523 $uibModalInstance.close();
524 selected_barcode = barcode;
526 $scope.cancel = function() {$uibModalInstance.dismiss()}
528 }).result.then(function() {
530 user_id = barcode_map[selected_barcode];
535 return deferred.promise;
538 // call our function to lookup matching users for the scanned barcode
539 handleBarcodeCompletion(args.barcode).then(function() {
541 // see if an opt-in request is needed
542 return egCore.net.request(
544 'open-ils.actor.user.org_unit_opt_in.check',
545 egCore.auth.token(), user_id
546 ).then(function(optInResp) { // opt_in_check
548 if (evt = egCore.evt.parse(optInResp)) {
553 if (optInResp == 2) {
554 // opt-in disallowed at this location by patron's home library
555 $scope.optInRestricted = true;
556 $scope.selectMe = true;
557 egCore.audio.play('warning.patron.opt_in_restricted');
561 if (optInResp == 1) {
562 // opt-in handled or not needed
563 return loadPatron(user_id);
566 // opt-in needed, show the opt-in dialog
567 egUser.get(user_id, {useFields : []})
569 .then(function(user) { // retrieve user
570 var org = egCore.org.get(user.home_ou());
571 egConfirmDialog.open(
572 egCore.strings.OPT_IN_DIALOG_TITLE,
573 egCore.strings.OPT_IN_DIALOG,
574 { family_name : user.family_name(),
575 first_given_name : user.first_given_name(),
576 org_name : org.name(),
577 org_shortname : org.shortname(),
578 ok : function() { createOptIn(user.id()) },
579 cancel : function() {}
590 * Manages patron search
592 .controller('PatronSearchCtrl',
593 ['$scope','$q','$routeParams','$timeout','$window','$location','egCore','ngToast',
594 '$filter','egUser', 'patronSvc','egGridDataProvider','$document','bucketSvc',
595 'egPatronMerge','egProgressDialog','$controller','$interpolate','$uibModal',
596 function($scope, $q, $routeParams, $timeout, $window, $location, egCore , ngToast,
597 $filter, egUser, patronSvc , egGridDataProvider , $document , bucketSvc,
598 egPatronMerge , egProgressDialog , $controller , $interpolate , $uibModal) {
600 angular.extend(this, $controller('BasePatronSearchCtrl', {$scope : $scope}));
601 $scope.initTab('search');
603 $scope.gridControls = {
604 activateItem : function(item) {
605 $location.path('/circ/patron/' + item.id() + '/checkout');
607 selectedItems : function() { return [] }
610 $scope.bucketSvc = bucketSvc;
611 $scope.bucketSvc.fetchUserBuckets();
612 $scope.addToBucket = function(item, data, recs) {
613 if (recs.length == 0) return;
615 var failed_count = 0;
617 angular.forEach(recs,
619 var item = new egCore.idl.cubi();
620 item.bucket(data.id());
621 item.target_user(rec.id());
622 p.push(egCore.net.request(
624 'open-ils.actor.container.item.create',
625 egCore.auth.token(), 'user', item
627 function(){ added_count++ },
628 function(){ failed_count++ }
633 $q.all(p).then( function () {
634 if (added_count) ngToast.create($interpolate(egCore.strings.BUCKET_ADD_SUCCESS)({ count: ''+added_count, name: data.name()} ));
635 if (failed_count) ngToast.warning($interpolate(egCore.strings.BUCKET_ADD_FAIL)({ count: ''+failed_count, name: data.name() } ));
639 var temp_scope = $scope;
640 $scope.openCreateBucketDialog = function() {
642 templateUrl: './circ/patron/bucket/t_bucket_create',
645 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
646 $scope.focusMe = true;
647 $scope.ok = function(args) { $uibModalInstance.close(args) }
648 $scope.cancel = function () { $uibModalInstance.dismiss() }
650 }).result.then(function (args) {
651 if (!args || !args.name) return;
652 bucketSvc.createBucket(args.name, args.desc).then(
655 $scope.bucketSvc.fetchBucket(id).then(function (b) {
659 $scope.gridControls.selectedItems()
661 $scope.bucketSvc.fetchUserBuckets(true);
670 function() {return $scope.gridControls.selectedItems()},
673 patronSvc.setPrimary(null, list[0]);
678 $scope.need_one_selected = function() {
679 var items = $scope.gridControls.selectedItems();
680 return (items.length > 0) ? false : true;
682 $scope.need_two_selected = function() {
683 var items = $scope.gridControls.selectedItems();
684 return (items.length == 2) ? false : true;
686 $scope.merge_patrons = function() {
687 var items = $scope.gridControls.selectedItems();
688 if (items.length != 2) return false;
691 angular.forEach(items, function(i) {
692 patron_ids.push(i.id());
694 egPatronMerge.do_merge(patron_ids).then(function() {
695 // ensure that we're not drawing from cached
696 // resuts, as a successful merge just deleted a
698 delete patronSvc.lastSearch;
699 $scope.gridControls.refresh();
708 .controller('PatronMessagesCtrl',
709 ['$scope','$q','$routeParams','egCore','$uibModal','patronSvc','egCirc',
710 function($scope , $q , $routeParams, egCore , $uibModal , patronSvc , egCirc) {
711 $scope.initTab('messages', $routeParams.id);
712 var usr_id = $routeParams.id;
713 var org_ids = egCore.org.fullPath(egCore.auth.user().ws_ou(), true);
715 // setup date filters
716 var start = new Date(); // now - 1 year
717 start.setFullYear(start.getFullYear() - 1),
720 end_date : new Date()
723 function date_range() {
724 var start = $scope.dates.start_date.toISOString().replace(/T.*/,'');
725 var end = $scope.dates.end_date.toISOString().replace(/T.*/,'');
726 var today = new Date().toISOString().replace(/T.*/,'');
727 if (end == today) end = 'now';
733 var activeGrid = $scope.activeGridControls = {
734 setSort : function() {
737 setQuery : function() {
743 {stop_date : {'>' : 'now'}}
749 var archiveGrid = $scope.archiveGridControls = {
750 setSort : function() {
753 setQuery : function() {
757 stop_date : {'<=' : 'now'},
758 set_date : {between : date_range()}
763 $scope.removePenalty = function(selected) {
764 // the grid stores flattened penalties. Fetch penalty objects first
766 var ids = selected.map(function(s){ return s.id });
767 egCore.pcrud.search('ausp',
769 {atomic : true, authoritative : true}
772 ).then(function(penalties) {
773 return egCore.pcrud.remove(penalties);
775 // then refresh the grid
777 activeGrid.refresh();
781 $scope.archivePenalty = function(selected) {
782 // the grid stores flattened penalties. Fetch penalty objects first
784 var ids = selected.map(function(s){ return s.id });
785 egCore.pcrud.search('ausp',
787 {atomic : true, authoritative : true}
790 ).then(function(penalties) {
791 angular.forEach(penalties, function(p){ p.stop_date('now') });
792 return egCore.pcrud.update(penalties);
794 // then refresh the grid
796 activeGrid.refresh();
797 archiveGrid.refresh();
801 // leverage egEnv for caching
802 function fetchPenaltyTypes() {
804 return $q.when(egCore.env.csp.list);
805 return egCore.pcrud.search(
806 // id <= 100 are reserved for system use
807 'csp', {id : {'>': 100}}, {}, {atomic : true})
808 .then(function(penalties) {
809 egCore.env.absorbList(penalties, 'csp');
814 $scope.createPenalty = function() {
815 egCirc.create_penalty(usr_id).then(function() {
816 activeGrid.refresh();
817 // force a refresh of the user, since they may now
818 // have blocking penalties, etc.
819 patronSvc.setPrimary(patronSvc.current.id(), null, true);
823 $scope.editPenalty = function(selected) {
824 if (selected.length == 0) return;
826 // grab the penalty from the user object
827 var penalty = patronSvc.current.standing_penalties().filter(
828 function(p) {return p.id() == selected[0].id})[0];
830 egCirc.edit_penalty(penalty).then(function() {
831 activeGrid.refresh();
832 // force a refresh of the user, since they may now
833 // have blocking penalties, etc.
834 patronSvc.setPrimary(patronSvc.current.id(), null, true);
843 .controller('PatronVerifyCredentialsCtrl',
844 ['$scope','$routeParams','$location','egCore',
845 function($scope, $routeParams , $location , egCore) {
846 $scope.verified = null;
847 $scope.focusMe = true;
849 // called with a patron, pre-populate the form args
850 $scope.initTab('other', $routeParams.id).then(
852 if ($routeParams.id && $scope.patron()) {
853 $scope.prepop = true;
854 $scope.username = $scope.patron().usrname();
855 $scope.barcode = $scope.patron().card().barcode();
857 $scope.username = '';
859 $scope.password = '';
864 // verify login credentials
865 $scope.verify = function() {
866 $scope.verified = null;
867 $scope.notFound = false;
871 'open-ils.actor.verify_user_password',
872 egCore.auth.token(), $scope.barcode,
873 $scope.username, hex_md5($scope.password || '')
875 ).then(function(resp) {
876 $scope.focusMe = true;
877 if (evt = egCore.evt.parse(resp)) {
879 } else if (resp == 1) {
880 $scope.verified = true;
882 $scope.verified = false;
887 // load the main patron UI for the provided username or barcode
888 $scope.load = function($event) {
889 $scope.notFound = false;
890 $scope.verified = null;
894 'open-ils.actor.user.retrieve_id_by_barcode_or_username',
895 egCore.auth.token(), $scope.barcode, $scope.username
897 ).then(function(resp) {
900 $location.path('/circ/patron/' + resp + '/checkout');
904 // something went wrong...
905 $scope.focusMe = true;
906 if (evt = egCore.evt.parse(resp)) {
907 if (evt.textcode == 'ACTOR_USR_NOT_FOUND') {
908 $scope.notFound = true;
917 // load() button sits within the verify form.
918 // avoid submitting the verify() form action on load()
919 $event.preventDefault();
923 .controller('PatronAlertsCtrl',
924 ['$scope','$routeParams','$location','egCore','patronSvc',
925 function($scope, $routeParams , $location , egCore , patronSvc) {
927 $scope.initTab('other', $routeParams.id)
929 $scope.patronExpired = patronSvc.patronExpired;
930 $scope.patronExpiresSoon = patronSvc.patronExpiresSoon;
931 $scope.retrievedWithInactive = patronSvc.fetchedWithInactiveCard();
932 $scope.invalidAddresses = patronSvc.invalidAddresses;
937 .controller('PatronNotesCtrl',
938 ['$scope','$filter','$routeParams','$location','egCore','patronSvc','$uibModal',
940 function($scope, $filter , $routeParams , $location , egCore , patronSvc , $uibModal,
942 $scope.initTab('other', $routeParams.id);
943 var usr_id = $routeParams.id;
946 function refreshPage() {
948 egCore.pcrud.search('aun',
950 {flesh : 1, flesh_fields : {aun : ['creator']}},
951 {authoritative : true})
952 .then(null, null, function(note) {
953 $scope.notes.push(note);
957 // open the new-note dialog and create the note
958 $scope.newNote = function() {
960 templateUrl: './circ/patron/t_new_note_dialog',
963 ['$scope', '$uibModalInstance',
964 function($scope, $uibModalInstance) {
965 $scope.focusNote = true;
967 $scope.require_initials = egCore.env.aous['ui.staff.require_initials.patron_info_notes'];
968 $scope.ok = function(count) { $uibModalInstance.close($scope.args) }
969 $scope.cancel = function () { $uibModalInstance.dismiss() }
973 if (!args.value) return;
974 var note = new egCore.idl.aun();
976 note.title(args.title);
977 note.value(args.value);
978 note.pub(args.pub ? 't' : 'f');
979 note.creator(egCore.auth.user().id());
981 note.value(note.value() + ' [' + args.initials + ']');
982 egCore.pcrud.create(note).then(function() {refreshPage()});
987 // delete the selected note
988 $scope.deleteNote = function(note) {
989 egConfirmDialog.open(
990 egCore.strings.PATRON_NOTE_DELETE_CONFIRM_TITLE, egCore.strings.PATRON_NOTE_DELETE_CONFIRM,
992 egCore.pcrud.remove(note).then(function() {refreshPage()});
994 note_title : note.title(),
995 create_date : note.create_date()
999 // print the selected note
1000 $scope.printNote = function(note) {
1001 var hash = egCore.idl.toHash(note);
1002 hash.usr = egCore.idl.toHash($scope.patron());
1003 egCore.print.print({
1004 context : 'default',
1005 template : 'patron_note',
1006 scope : {note : hash}
1010 // perform the initial note fetch
1014 .controller('PatronGroupCtrl',
1015 ['$scope','$routeParams','$q','$window','$timeout','$location','egCore',
1016 'patronSvc','$uibModal','egPromptDialog','egConfirmDialog',
1017 function($scope, $routeParams , $q , $window , $timeout, $location , egCore ,
1018 patronSvc , $uibModal , egPromptDialog , egConfirmDialog) {
1020 var usr_id = $routeParams.id;
1022 $scope.totals = {owed : 0, total_out : 0, overdue : 0}
1024 var grid = $scope.gridControls = {
1025 activateItem : function(item) {
1026 $location.path('/circ/patron/' + item.id + '/checkout');
1028 itemRetrieved : function(item) {
1030 if (item.id == patronSvc.current.id()) {
1031 item.stats = patronSvc.patron_stats;
1034 // flesh stats for other group members
1035 patronSvc.getUserStats(item.id).then(function(stats) {
1037 $scope.totals.total_out += stats.checkouts.total_out;
1038 $scope.totals.overdue += stats.checkouts.overdue;
1042 setSort : function() {
1043 return ['create_date'];
1047 $scope.initTab('other', $routeParams.id)
1048 .then(function(redirect) {
1049 // if we are redirecting to the alerts page, avoid updating the
1051 if (redirect) return;
1052 // let initTab() fetch the user first so we can know the usrgroup
1055 usrgroup : patronSvc.current.usrgroup(),
1058 $scope.totals.owed = patronSvc.patron_stats.fines.group_balance_owed;
1061 $scope.removeFromGroup = function(selected) {
1063 angular.forEach(selected, function(user) {
1064 console.debug('removing user ' + user.id + ' from group');
1069 'open-ils.actor.usergroup.new',
1070 egCore.auth.token(), user.id, true
1075 $q.all(promises).then(function() {grid.refresh()});
1078 function addUserToGroup(user) {
1079 user.usrgroup(patronSvc.current.usrgroup());
1080 user.ischanged(true);
1083 'open-ils.actor.patron.update',
1084 egCore.auth.token(), user
1086 ).then(function() {grid.refresh()});
1089 // fetch each user ("selected" has flattened users)
1090 // update the usrgroup, then update the user object
1091 // After all updates are complete, refresh the grid.
1092 function moveUsersToGroup(target_user, selected) {
1095 angular.forEach(selected, function(user) {
1097 egCore.pcrud.retrieve('au', user.id)
1099 u.usrgroup(target_user.usrgroup());
1101 return egCore.net.request(
1103 'open-ils.actor.patron.update',
1104 egCore.auth.token(), u
1110 $q.all(promises).then(function() {grid.refresh()});
1113 function showMoveToGroupConfirm(barcode, selected, outbound) {
1116 egCore.pcrud.search('ac', {barcode : barcode})
1118 // fetch the fleshed user
1119 .then(function(card) {
1121 if (!card) return; // TODO: warn user
1123 egCore.pcrud.retrieve('au', card.usr())
1124 .then(function(user) {
1127 templateUrl: './circ/patron/t_move_to_group_dialog',
1130 '$scope','$uibModalInstance',
1131 function($scope , $uibModalInstance) {
1133 $scope.selected = selected;
1134 $scope.outbound = outbound;
1136 function(count) { $uibModalInstance.close() }
1138 function () { $uibModalInstance.dismiss() }
1141 }).result.then(function() {
1143 moveUsersToGroup(user, selected);
1145 addUserToGroup(user);
1152 // selected == move selected patrons to another patron's group
1153 // !selected == patron from a different group moves into our group
1154 function moveToGroup(selected, outbound) {
1155 egPromptDialog.open(
1156 egCore.strings.GROUP_ADD_USER, '',
1157 {ok : function(value) {
1159 showMoveToGroupConfirm(value, selected, outbound);
1164 $scope.moveToGroup = function() { moveToGroup([], false) };
1165 $scope.moveToAnotherGroup = function(selected) { moveToGroup(selected, true) };
1167 $scope.cloneUser = function(selected) {
1168 if (!selected.length) return;
1169 var url = $location.absUrl().replace(
1171 '/patron/register/clone/' + selected[0].id);
1172 $window.open(url, '_blank').focus();
1175 $scope.retrieveSelected = function(selected) {
1176 if (!selected.length) return;
1177 angular.forEach(selected, function(usr) {
1178 $timeout(function() {
1179 var url = $location.absUrl().replace(
1181 '/patron/' + usr.id + '/checkout');
1182 $window.open(url, '_blank')
1189 .controller('PatronStatCatsCtrl',
1190 ['$scope','$routeParams','$q','egCore','patronSvc',
1191 function($scope, $routeParams , $q , egCore , patronSvc) {
1192 $scope.initTab('other', $routeParams.id)
1193 .then(function(redirect) {
1194 // Entries for org-visible stat cats are fleshed. Any others
1195 // have to be fleshed within.
1198 angular.forEach(patronSvc.current.stat_cat_entries(),
1200 if (!angular.isObject(entry.stat_cat())) {
1201 to_flesh[entry.stat_cat()] = entry;
1206 if (!Object.keys(to_flesh).length) return;
1208 egCore.pcrud.search('actsc', {id : Object.keys(to_flesh)})
1209 .then(null, null, function(cat) { // stream
1210 cat.owner(egCore.org.get(cat.owner())); // owner flesh
1211 to_flesh[cat.id()].stat_cat(cat);
1216 .controller('PatronSurveyCtrl',
1217 ['$scope','$routeParams','$location','egCore','patronSvc',
1218 function($scope, $routeParams , $location , egCore , patronSvc) {
1219 $scope.initTab('other', $routeParams.id);
1220 var usr_id = $routeParams.id;
1221 var org_ids = egCore.org.fullPath(egCore.auth.user().ws_ou(), true);
1223 $scope.surveys = [];
1224 var svr_responses = {};
1226 // fetch all survey responses for this user.
1227 egCore.pcrud.search('asvr',
1229 {flesh : 2, flesh_fields : {asvr : ['survey','question','answer']}}
1232 // All responses collected and deduplicated.
1233 // Create one collection of responses per survey.
1235 angular.forEach(svr_responses, function(questions, survey_id) {
1236 var collection = {responses : []};
1237 angular.forEach(questions, function(response) {
1238 collection.survey = response.survey(); // same for one.
1239 collection.responses.push(response);
1241 $scope.surveys.push(collection);
1245 function(response) {
1247 // Discard responses for out-of-scope surveys.
1248 if (org_ids.indexOf(response.survey().owner()) < 0)
1251 // survey_id => question_id => response
1252 var svr_id = response.survey().id();
1253 var qst_id = response.question().id();
1255 if (!svr_responses[svr_id])
1256 svr_responses[svr_id] = [];
1258 if (!svr_responses[svr_id][qst_id]) {
1259 svr_responses[svr_id][qst_id] = response;
1262 // We have multiple responses for the same question.
1263 // For this UI we only care about the most recent response.
1264 if (response.effective_date() >
1265 svr_responses[svr_id][qst_id].effective_date())
1266 svr_responses[svr_id][qst_id] = response;
1272 .controller('PatronFetchLastCtrl',
1273 ['$scope','$location','egCore',
1274 function($scope , $location , egCore) {
1276 var ids = egCore.hatch.getLoginSessionItem('eg.circ.recent_patrons') || [];
1278 return $location.path('/circ/patron/' + ids[0] + '/checkout');
1280 $scope.no_last = true;
1283 .controller('PatronTriggeredEventsCtrl',
1284 ['$scope','$routeParams','$location','egCore','patronSvc',
1285 function($scope, $routeParams, $location , egCore , patronSvc) {
1286 $scope.initTab('other', $routeParams.id);
1288 var url = $location.absUrl().replace(/\/staff.*/, '/actor/user/event_log');
1289 url += '?patron_id=' + encodeURIComponent($routeParams.id);
1291 $scope.triggered_events_url = url;
1295 .controller('PatronMessageCenterCtrl',
1296 ['$scope','$routeParams','$location','egCore','patronSvc',
1297 function($scope, $routeParams, $location , egCore , patronSvc) {
1298 $scope.initTab('other', $routeParams.id);
1300 var url = $location.protocol() + '://' + $location.host()
1301 + egCore.env.basePath.replace(/\/staff.*/, '/actor/user/message');
1302 url += '/' + encodeURIComponent($routeParams.id);
1304 $scope.message_center_url = url;
1308 .controller('PatronPermsCtrl',
1309 ['$scope','$routeParams','$window','$location','egCore',
1310 function($scope , $routeParams , $window , $location , egCore) {
1311 $scope.initTab('other', $routeParams.id);
1313 var url = $location.absUrl().replace(
1314 /\/eg\/staff.*/, '/xul/server/patron/user_edit.xhtml');
1316 url += '?usr=' + encodeURIComponent($routeParams.id);
1318 // user_edit does not load the session via cookie. It uses URL
1319 // params or xulG instead. Pass via xulG.
1321 ses : egCore.auth.token(),
1322 on_patron_save : function() {
1323 $scope.funcs.reload();
1327 $scope.user_perms_url = url;