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().then(function() {
66 // This call requires orgs to be loaded, because it
67 // calls egCore.org.ancestors(), so call it after startup
68 return egCore.pcrud.search('actsc',
69 {owner : egCore.org.ancestors(
70 egCore.auth.user().ws_ou(), true)},
72 ).then(function(cats) {
73 egCore.env.absorbList(cats, 'actsc');
78 $routeProvider.when('/circ/patron/search', {
79 templateUrl: './circ/patron/t_search',
80 controller: 'PatronSearchCtrl',
84 $routeProvider.when('/circ/patron/bcsearch', {
85 templateUrl: './circ/patron/t_bcsearch',
86 controller: 'PatronBarcodeSearchCtrl',
90 $routeProvider.when('/circ/patron/credentials', {
91 templateUrl: './circ/patron/t_credentials',
92 controller: 'PatronVerifyCredentialsCtrl',
96 $routeProvider.when('/circ/patron/last', {
97 templateUrl: './circ/patron/t_last_patron',
98 controller: 'PatronFetchLastCtrl',
102 // the following require a patron ID
104 $routeProvider.when('/circ/patron/:id/alerts', {
105 templateUrl: './circ/patron/t_alerts',
106 controller: 'PatronAlertsCtrl',
110 $routeProvider.when('/circ/patron/:id/checkout', {
111 templateUrl: './circ/patron/t_checkout',
112 controller: 'PatronCheckoutCtrl',
116 $routeProvider.when('/circ/patron/:id/items_out', {
117 templateUrl: './circ/patron/t_items_out',
118 controller: 'PatronItemsOutCtrl',
122 $routeProvider.when('/circ/patron/:id/holds', {
123 templateUrl: './circ/patron/t_holds',
124 controller: 'PatronHoldsCtrl',
128 $routeProvider.when('/circ/patron/:id/holds/create', {
129 templateUrl: './circ/patron/t_holds_create',
130 controller: 'PatronHoldsCreateCtrl',
134 $routeProvider.when('/circ/patron/:id/holds/:hold_id', {
135 templateUrl: './circ/patron/t_holds',
136 controller: 'PatronHoldsCtrl',
140 $routeProvider.when('/circ/patron/:id/hold/:hold_id', {
141 templateUrl: './circ/patron/t_hold_details',
142 controller: 'PatronHoldDetailsCtrl',
146 $routeProvider.when('/circ/patron/:id/bills', {
147 templateUrl: './circ/patron/t_bills',
148 controller: 'PatronBillsCtrl',
152 $routeProvider.when('/circ/patron/:id/bill/:xact_id', {
153 templateUrl: './circ/patron/t_xact_details',
154 controller: 'XactDetailsCtrl',
158 $routeProvider.when('/circ/patron/:id/bill_history/:history_tab', {
159 templateUrl: './circ/patron/t_bill_history',
160 controller: 'BillHistoryCtrl',
164 $routeProvider.when('/circ/patron/:id/messages', {
165 templateUrl: './circ/patron/t_messages',
166 controller: 'PatronMessagesCtrl',
170 $routeProvider.when('/circ/patron/:id/edit', {
171 templateUrl: './circ/patron/t_edit',
172 controller: 'PatronRegCtrl',
176 $routeProvider.when('/circ/patron/:id/credentials', {
177 templateUrl: './circ/patron/t_credentials',
178 controller: 'PatronVerifyCredentialsCtrl',
182 $routeProvider.when('/circ/patron/:id/notes', {
183 templateUrl: './circ/patron/t_notes',
184 controller: 'PatronNotesCtrl',
188 $routeProvider.when('/circ/patron/:id/triggered_events', {
189 templateUrl: './circ/patron/t_triggered_events',
190 controller: 'PatronTriggeredEventsCtrl',
194 $routeProvider.when('/circ/patron/:id/message_center', {
195 templateUrl: './circ/patron/t_message_center',
196 controller: 'PatronMessageCenterCtrl',
200 $routeProvider.when('/circ/patron/:id/edit_perms', {
201 templateUrl: './circ/patron/t_edit_perms',
202 controller: 'PatronPermsCtrl',
206 $routeProvider.when('/circ/patron/:id/group', {
207 templateUrl: './circ/patron/t_group',
208 controller: 'PatronGroupCtrl',
212 $routeProvider.when('/circ/patron/:id/stat_cats', {
213 templateUrl: './circ/patron/t_stat_cats',
214 controller: 'PatronStatCatsCtrl',
218 $routeProvider.when('/circ/patron/:id/surveys', {
219 templateUrl: './circ/patron/t_surveys',
220 controller: 'PatronSurveyCtrl',
224 $routeProvider.otherwise({redirectTo : '/circ/patron/search'});
228 * Manages tabbed patron view.
229 * This is the parent scope of all patron tab scopes.
232 .controller('PatronCtrl',
233 ['$scope','$q','$location','$filter','egCore','egNet','egUser','egAlertDialog','egConfirmDialog','egPromptDialog','patronSvc',
234 function($scope, $q , $location , $filter , egCore , egNet , egUser , egAlertDialog , egConfirmDialog , egPromptDialog , patronSvc) {
236 $scope.is_patron_edit = function() {
237 return Boolean($location.path().match(/patron\/\d+\/edit$/));
240 // To support the fixed position patron edit actions bar,
241 // its markup has to live outside the scope of the patron
242 // edit controller. Insert a scope blob here that can be
243 // modifed from within the patron edit controller.
244 $scope.edit_passthru = {};
246 // returns true if a redirect occurs
247 function redirectToAlertPanel() {
249 $scope.alert_penalties =
250 function() {return patronSvc.alert_penalties}
252 if (patronSvc.alertsShown()) return false;
254 // if the patron has any unshown alerts, show them now
255 if (patronSvc.hasAlerts &&
256 !$location.path().match(/alerts$/)) {
259 .path('/circ/patron/' + patronSvc.current.id() + '/alerts')
260 .search('card', null);
264 // no alert required. If the patron has fines and the show-bills
265 // OUS is applied, direct to the bills page.
266 if ($scope.patron_stats().fines.balance_owed > 0 // TODO: != 0 ?
267 && egCore.env.aous['ui.circ.show_billing_tab_on_bills']
268 && !$location.path().match(/bills$/)) {
270 $scope.tab = 'bills';
272 .path('/circ/patron/' + patronSvc.current.id() + '/bills')
273 .search('card', null);
281 // called after each route-specified controller is instantiated.
282 // this doubles as a way to inform the top-level controller that
283 // egStartup.go() has completed, which means we are clear to
284 // fetch the patron, etc.
285 $scope.initTab = function(tab, patron_id) {
286 console.log('init tab ' + tab);
288 $scope.aous = egCore.env.aous;
289 $scope.auth_user_id = egCore.auth.user().id();
292 $scope.patron_id = patron_id;
293 return patronSvc.setPrimary($scope.patron_id)
295 // the page title context label comes from the tab.
296 egCore.strings.setPageTitle(
297 egCore.strings.PAGE_TITLE_PATRON_NAME,
298 egCore.strings['PAGE_TITLE_PATRON_' + tab.toUpperCase()],
299 { lname : patronSvc.current.family_name(),
300 fname : patronSvc.current.first_given_name(),
301 mname : patronSvc.current.second_given_name()
305 .then(function() {return patronSvc.checkAlerts()})
306 .then(redirectToAlertPanel)
308 $scope.ident_type_name = $scope.patron().ident_type().name()
309 $scope.hasIdentTypeName = $scope.ident_type_name.length > 0;
312 // No patron, use the tab name as the page title.
313 egCore.strings.setPageTitle(
314 egCore.strings['PAGE_TITLE_PATRON_' + tab.toUpperCase()]);
319 $scope._show_dob = {};
320 $scope.show_dob = function (val) {
321 if ($scope.patron()) {
322 if (typeof val != 'undefined') $scope._show_dob[$scope.patron().id()] = val;
323 return $scope._show_dob[$scope.patron().id()];
325 return !egCore.env.aous['circ.obscure_dob'];
328 $scope.obscure_dob = function() {
329 return egCore.env.aous && egCore.env.aous['circ.obscure_dob'];
331 $scope.now_show_dob = function() {
332 return egCore.env.aous && egCore.env.aous['circ.obscure_dob'] ?
333 $scope.show_dob() : true;
336 $scope.patron = function() { return patronSvc.current }
337 $scope.patron_stats = function() { return patronSvc.patron_stats }
338 $scope.summary_stat_cats = function() { return patronSvc.summary_stat_cats }
339 $scope.hasAlerts = function() { return patronSvc.hasAlerts }
340 $scope.isPatronExpired = function() { return patronSvc.patronExpired }
342 $scope.print_address = function(addr) {
345 template : 'patron_address',
347 patron : egCore.idl.toHash(patronSvc.current),
348 address : egCore.idl.toHash(addr)
353 $scope.toggle_expand_summary = function() {
354 if ($scope.collapsePatronSummary) {
355 $scope.collapsePatronSummary = false;
356 egCore.hatch.removeItem('eg.circ.patron.summary.collapse');
358 $scope.collapsePatronSummary = true;
359 egCore.hatch.setItem('eg.circ.patron.summary.collapse', true);
363 // always expand the patron summary in the search UI, regardless
364 // of stored preference.
365 $scope.collapse_summary = function() {
366 return $scope.tab != 'search' && $scope.collapsePatronSummary;
369 function _purge_account(dest_usr,override) {
372 'open-ils.actor.user.delete' + (override ? '.override' : ''),
374 $scope.patron().id(),
376 ).then(function(resp){
377 if (evt = egCore.evt.parse(resp)) {
378 if (evt.code == '2004' /* ACTOR_USER_DELETE_OPEN_XACTS */) {
379 egConfirmDialog.open(
380 egCore.strings.PATRON_PURGE_CONFIRM_TITLE, egCore.strings.PATRON_PURGE_OVERRIDE_PROMPT,
382 _purge_account(dest_usr,true);
389 location.href = egCore.env.basePath + '/circ/patron/search';
394 function _purge_account_with_destination(dest_barcode) {
395 egCore.pcrud.search('ac', {barcode : dest_barcode})
396 .then(function(card) {
398 egAlertDialog.open(egCore.strings.PATRON_PURGE_STAFF_BAD_BARCODE);
400 _purge_account(card.usr());
405 $scope.purge_account = function() {
406 egConfirmDialog.open(
407 egCore.strings.PATRON_PURGE_CONFIRM_TITLE, egCore.strings.PATRON_PURGE_CONFIRM,
409 egConfirmDialog.open(
410 egCore.strings.PATRON_PURGE_CONFIRM_TITLE, egCore.strings.PATRON_PURGE_LAST_CHANCE,
414 'open-ils.actor.user.has_work_perm_at',
415 egCore.auth.token(), 'STAFF_LOGIN', $scope.patron().id()
416 ).then(function(resp) {
417 var is_staff = resp.length > 0;
420 egCore.strings.PATRON_PURGE_STAFF_PROMPT,
421 null, // TODO: this would be cool if it worked: egCore.auth.user().card().barcode(),
422 {ok : function(barcode) {_purge_account_with_destination(barcode)}}
434 egCore.hatch.getItem('eg.circ.patron.summary.collapse')
435 .then(function(val) {$scope.collapsePatronSummary = Boolean(val)});
438 .controller('PatronBarcodeSearchCtrl',
439 ['$scope','$location','egCore','egConfirmDialog','egUser','patronSvc','$uibModal','$q',
440 function($scope , $location , egCore , egConfirmDialog , egUser , patronSvc , $uibModal , $q) {
441 $scope.selectMe = true; // focus text input
442 patronSvc.clearPrimary(); // clear the default user
444 // jump to the patron checkout UI
445 function loadPatron(user_id) {
446 egCore.audio.play('success.patron.by_barcode');
448 .path('/circ/patron/' + user_id + '/checkout')
449 .search('card', $scope.args.barcode);
450 patronSvc.search_barcode = $scope.args.barcode;
453 // create an opt-in=yes response for the loaded user
454 function createOptIn(user_id) {
457 'open-ils.actor.user.org_unit_opt_in.create',
458 egCore.auth.token(), user_id).then(function(resp) {
459 if (evt = egCore.evt.parse(resp)) return alert(evt);
465 $scope.submitBarcode = function(args) {
466 $scope.bcNotFound = null;
467 $scope.optInRestricted = false;
468 if (!args.barcode) return;
469 args.barcode = args.barcode.replace(/\s/g,'');
470 // blur so next time it's set to true it will re-apply select()
471 $scope.selectMe = false;
475 // given a scanned barcode, this function finds any matching users
476 // and handles multiple matches due to barcode completion
477 function handleBarcodeCompletion(scanned_barcode) {
478 var deferred = $q.defer();
482 'open-ils.actor.get_barcodes',
483 egCore.auth.token(), egCore.auth.user().ws_ou(),
484 'actor', scanned_barcode)
486 .then(function(resp) { // get_barcodes
488 if (evt = egCore.evt.parse(resp)) {
494 if (!resp || !resp[0]) {
495 $scope.bcNotFound = args.barcode;
496 $scope.selectMe = true;
497 egCore.audio.play('warning.patron.not_found');
502 if (resp.length == 1) {
503 // exactly one matching barcode: return it
505 user_id = resp[0].id;
507 // multiple matching barcodes: let the user pick one
508 var barcode_map = {};
511 var selected_barcode;
512 angular.forEach(resp, function(match) {
514 egUser.get(match.id, {useFields : ['home_ou']}).then(function(user) {
515 barcode_map[match.barcode] = user.id();
517 barcode: match.barcode,
518 title: user.first_given_name() + ' ' + user.family_name(),
519 org_name: user.home_ou().name(),
520 org_shortname: user.home_ou().shortname()
525 return $q.all(promises)
528 templateUrl: './circ/share/t_barcode_choice_dialog',
530 ['$scope', '$uibModalInstance',
531 function($scope, $uibModalInstance) {
532 $scope.matches = matches;
533 $scope.ok = function(barcode) {
534 $uibModalInstance.close();
535 selected_barcode = barcode;
537 $scope.cancel = function() {$uibModalInstance.dismiss()}
539 }).result.then(function() {
541 user_id = barcode_map[selected_barcode];
546 return deferred.promise;
549 // call our function to lookup matching users for the scanned barcode
550 handleBarcodeCompletion(args.barcode).then(function() {
552 // see if an opt-in request is needed
553 return egCore.net.request(
555 'open-ils.actor.user.org_unit_opt_in.check',
556 egCore.auth.token(), user_id
557 ).then(function(optInResp) { // opt_in_check
559 if (evt = egCore.evt.parse(optInResp)) {
564 if (optInResp == 2) {
565 // opt-in disallowed at this location by patron's home library
566 $scope.optInRestricted = true;
567 $scope.selectMe = true;
568 egCore.audio.play('warning.patron.opt_in_restricted');
572 if (optInResp == 1) {
573 // opt-in handled or not needed
574 return loadPatron(user_id);
577 // opt-in needed, show the opt-in dialog
578 egUser.get(user_id, {useFields : []})
580 .then(function(user) { // retrieve user
581 var org = egCore.org.get(user.home_ou());
582 egConfirmDialog.open(
583 egCore.strings.OPT_IN_DIALOG_TITLE,
584 egCore.strings.OPT_IN_DIALOG,
585 { family_name : user.family_name(),
586 first_given_name : user.first_given_name(),
587 org_name : org.name(),
588 org_shortname : org.shortname(),
589 ok : function() { createOptIn(user.id()) },
590 cancel : function() {}
601 * Manages patron search
603 .controller('PatronSearchCtrl',
604 ['$scope','$q','$routeParams','$timeout','$window','$location','egCore','ngToast',
605 '$filter','egUser', 'patronSvc','egGridDataProvider','$document','bucketSvc',
606 'egPatronMerge','egProgressDialog','$controller','$interpolate','$uibModal',
607 function($scope, $q, $routeParams, $timeout, $window, $location, egCore , ngToast,
608 $filter, egUser, patronSvc , egGridDataProvider , $document , bucketSvc,
609 egPatronMerge , egProgressDialog , $controller , $interpolate , $uibModal) {
611 angular.extend(this, $controller('BasePatronSearchCtrl', {$scope : $scope}));
612 $scope.initTab('search');
614 $scope.gridControls = {
615 activateItem : function(item) {
616 $location.path('/circ/patron/' + item.id() + '/checkout');
618 selectedItems : function() { return [] }
621 $scope.bucketSvc = bucketSvc;
622 $scope.bucketSvc.fetchUserBuckets();
623 $scope.addToBucket = function(item, data, recs) {
624 if (recs.length == 0) return;
626 var failed_count = 0;
628 angular.forEach(recs,
630 var item = new egCore.idl.cubi();
631 item.bucket(data.id());
632 item.target_user(rec.id());
633 p.push(egCore.net.request(
635 'open-ils.actor.container.item.create',
636 egCore.auth.token(), 'user', item
638 function(){ added_count++ },
639 function(){ failed_count++ }
644 $q.all(p).then( function () {
645 if (added_count) ngToast.create($interpolate(egCore.strings.BUCKET_ADD_SUCCESS)({ count: ''+added_count, name: data.name()} ));
646 if (failed_count) ngToast.warning($interpolate(egCore.strings.BUCKET_ADD_FAIL)({ count: ''+failed_count, name: data.name() } ));
650 var temp_scope = $scope;
651 $scope.openCreateBucketDialog = function() {
653 templateUrl: './circ/patron/bucket/t_bucket_create',
656 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
657 $scope.focusMe = true;
658 $scope.ok = function(args) { $uibModalInstance.close(args) }
659 $scope.cancel = function () { $uibModalInstance.dismiss() }
661 }).result.then(function (args) {
662 if (!args || !args.name) return;
663 bucketSvc.createBucket(args.name, args.desc).then(
666 $scope.bucketSvc.fetchBucket(id).then(function (b) {
670 $scope.gridControls.selectedItems()
672 $scope.bucketSvc.fetchUserBuckets(true);
681 function() {return $scope.gridControls.selectedItems()},
684 patronSvc.setPrimary(null, list[0]);
689 $scope.need_one_selected = function() {
690 var items = $scope.gridControls.selectedItems();
691 return (items.length > 0) ? false : true;
693 $scope.need_two_selected = function() {
694 var items = $scope.gridControls.selectedItems();
695 return (items.length == 2) ? false : true;
697 $scope.merge_patrons = function() {
698 var items = $scope.gridControls.selectedItems();
699 if (items.length != 2) return false;
702 angular.forEach(items, function(i) {
703 patron_ids.push(i.id());
705 egPatronMerge.do_merge(patron_ids).then(function() {
706 // ensure that we're not drawing from cached
707 // resuts, as a successful merge just deleted a
709 delete patronSvc.lastSearch;
710 $scope.gridControls.refresh();
719 .controller('PatronMessagesCtrl',
720 ['$scope','$q','$routeParams','egCore','$uibModal','patronSvc','egCirc',
721 function($scope , $q , $routeParams, egCore , $uibModal , patronSvc , egCirc) {
722 $scope.initTab('messages', $routeParams.id);
723 var usr_id = $routeParams.id;
725 // setup date filters
726 var start = new Date(); // now - 1 year
727 start.setFullYear(start.getFullYear() - 1),
730 end_date : new Date()
733 function date_range() {
734 var start = $scope.dates.start_date.toISOString().replace(/T.*/,'');
735 var end = $scope.dates.end_date.toISOString().replace(/T.*/,'');
736 var today = new Date().toISOString().replace(/T.*/,'');
737 if (end == today) end = 'now';
743 var activeGrid = $scope.activeGridControls = {
744 setSort : function() {
747 setQuery : function() {
752 {stop_date : {'>' : 'now'}}
758 var archiveGrid = $scope.archiveGridControls = {
759 setSort : function() {
762 setQuery : function() {
765 stop_date : {'<=' : 'now'},
766 set_date : {between : date_range()}
771 $scope.removePenalty = function(selected) {
772 // the grid stores flattened penalties. Fetch penalty objects first
774 var ids = selected.map(function(s){ return s.id });
775 egCore.pcrud.search('ausp',
777 {atomic : true, authoritative : true}
780 ).then(function(penalties) {
781 return egCore.pcrud.remove(penalties);
783 // then refresh the grid
785 activeGrid.refresh();
789 $scope.archivePenalty = function(selected) {
790 // the grid stores flattened penalties. Fetch penalty objects first
792 var ids = selected.map(function(s){ return s.id });
793 egCore.pcrud.search('ausp',
795 {atomic : true, authoritative : true}
798 ).then(function(penalties) {
799 angular.forEach(penalties, function(p){ p.stop_date('now') });
800 return egCore.pcrud.update(penalties);
802 // then refresh the grid
804 activeGrid.refresh();
805 archiveGrid.refresh();
809 // leverage egEnv for caching
810 function fetchPenaltyTypes() {
812 return $q.when(egCore.env.csp.list);
813 return egCore.pcrud.search(
814 // id <= 100 are reserved for system use
815 'csp', {id : {'>': 100}}, {}, {atomic : true})
816 .then(function(penalties) {
817 egCore.env.absorbList(penalties, 'csp');
822 $scope.createPenalty = function() {
823 egCirc.create_penalty(usr_id).then(function() {
824 activeGrid.refresh();
825 // force a refresh of the user, since they may now
826 // have blocking penalties, etc.
827 patronSvc.setPrimary(patronSvc.current.id(), null, true);
831 $scope.editPenalty = function(selected) {
832 if (selected.length == 0) return;
834 // grab the penalty from the user object
835 var penalty = patronSvc.current.standing_penalties().filter(
836 function(p) {return p.id() == selected[0].id})[0];
838 egCirc.edit_penalty(penalty).then(function() {
839 activeGrid.refresh();
840 // force a refresh of the user, since they may now
841 // have blocking penalties, etc.
842 patronSvc.setPrimary(patronSvc.current.id(), null, true);
851 .controller('PatronVerifyCredentialsCtrl',
852 ['$scope','$routeParams','$location','egCore',
853 function($scope, $routeParams , $location , egCore) {
854 $scope.verified = null;
855 $scope.focusMe = true;
857 // called with a patron, pre-populate the form args
858 $scope.initTab('other', $routeParams.id).then(
860 if ($routeParams.id && $scope.patron()) {
861 $scope.prepop = true;
862 $scope.username = $scope.patron().usrname();
863 $scope.barcode = $scope.patron().card().barcode();
865 $scope.username = '';
867 $scope.password = '';
872 // verify login credentials
873 $scope.verify = function() {
874 $scope.verified = null;
875 $scope.notFound = false;
879 'open-ils.actor.verify_user_password',
880 egCore.auth.token(), $scope.barcode,
881 $scope.username, hex_md5($scope.password || '')
883 ).then(function(resp) {
884 $scope.focusMe = true;
885 if (evt = egCore.evt.parse(resp)) {
887 } else if (resp == 1) {
888 $scope.verified = true;
890 $scope.verified = false;
895 // load the main patron UI for the provided username or barcode
896 $scope.load = function($event) {
897 $scope.notFound = false;
898 $scope.verified = null;
902 'open-ils.actor.user.retrieve_id_by_barcode_or_username',
903 egCore.auth.token(), $scope.barcode, $scope.username
905 ).then(function(resp) {
908 $location.path('/circ/patron/' + resp + '/checkout');
912 // something went wrong...
913 $scope.focusMe = true;
914 if (evt = egCore.evt.parse(resp)) {
915 if (evt.textcode == 'ACTOR_USR_NOT_FOUND') {
916 $scope.notFound = true;
925 // load() button sits within the verify form.
926 // avoid submitting the verify() form action on load()
927 $event.preventDefault();
931 .controller('PatronAlertsCtrl',
932 ['$scope','$routeParams','$location','egCore','patronSvc',
933 function($scope, $routeParams , $location , egCore , patronSvc) {
935 $scope.initTab('other', $routeParams.id)
937 $scope.patronExpired = patronSvc.patronExpired;
938 $scope.patronExpiresSoon = patronSvc.patronExpiresSoon;
939 $scope.retrievedWithInactive = patronSvc.fetchedWithInactiveCard();
940 $scope.invalidAddresses = patronSvc.invalidAddresses;
945 .controller('PatronNotesCtrl',
946 ['$scope','$filter','$routeParams','$location','egCore','patronSvc','$uibModal',
948 function($scope, $filter , $routeParams , $location , egCore , patronSvc , $uibModal,
950 $scope.initTab('other', $routeParams.id);
951 var usr_id = $routeParams.id;
954 function refreshPage() {
956 egCore.pcrud.search('aun',
958 {flesh : 1, flesh_fields : {aun : ['creator']}},
959 {authoritative : true})
960 .then(null, null, function(note) {
961 $scope.notes.push(note);
965 // open the new-note dialog and create the note
966 $scope.newNote = function() {
968 templateUrl: './circ/patron/t_new_note_dialog',
971 ['$scope', '$uibModalInstance',
972 function($scope, $uibModalInstance) {
973 $scope.focusNote = true;
975 $scope.require_initials = egCore.env.aous['ui.staff.require_initials.patron_info_notes'];
976 $scope.ok = function(count) { $uibModalInstance.close($scope.args) }
977 $scope.cancel = function () { $uibModalInstance.dismiss() }
981 if (!args.value) return;
982 var note = new egCore.idl.aun();
984 note.title(args.title);
985 note.value(args.value);
986 note.pub(args.pub ? 't' : 'f');
987 note.creator(egCore.auth.user().id());
989 note.value(note.value() + ' [' + args.initials + ']');
990 egCore.pcrud.create(note).then(function() {refreshPage()});
995 // delete the selected note
996 $scope.deleteNote = function(note) {
997 egConfirmDialog.open(
998 egCore.strings.PATRON_NOTE_DELETE_CONFIRM_TITLE, egCore.strings.PATRON_NOTE_DELETE_CONFIRM,
1000 egCore.pcrud.remove(note).then(function() {refreshPage()});
1002 note_title : note.title(),
1003 create_date : note.create_date()
1007 // print the selected note
1008 $scope.printNote = function(note) {
1009 var hash = egCore.idl.toHash(note);
1010 hash.usr = egCore.idl.toHash($scope.patron());
1011 egCore.print.print({
1012 context : 'default',
1013 template : 'patron_note',
1014 scope : {note : hash}
1018 // perform the initial note fetch
1022 .controller('PatronGroupCtrl',
1023 ['$scope','$routeParams','$q','$window','$timeout','$location','egCore',
1024 'patronSvc','$uibModal','egPromptDialog','egConfirmDialog',
1025 function($scope, $routeParams , $q , $window , $timeout, $location , egCore ,
1026 patronSvc , $uibModal , egPromptDialog , egConfirmDialog) {
1028 var usr_id = $routeParams.id;
1030 $scope.totals = {owed : 0, total_out : 0, overdue : 0}
1032 var grid = $scope.gridControls = {
1033 activateItem : function(item) {
1034 $location.path('/circ/patron/' + item.id + '/checkout');
1036 itemRetrieved : function(item) {
1038 if (item.id == patronSvc.current.id()) {
1039 item.stats = patronSvc.patron_stats;
1042 // flesh stats for other group members
1043 patronSvc.getUserStats(item.id).then(function(stats) {
1045 $scope.totals.total_out += stats.checkouts.total_out;
1046 $scope.totals.overdue += stats.checkouts.overdue;
1050 setSort : function() {
1051 return ['create_date'];
1055 $scope.initTab('other', $routeParams.id)
1056 .then(function(redirect) {
1057 // if we are redirecting to the alerts page, avoid updating the
1059 if (redirect) return;
1060 // let initTab() fetch the user first so we can know the usrgroup
1063 usrgroup : patronSvc.current.usrgroup(),
1066 $scope.totals.owed = patronSvc.patron_stats.fines.group_balance_owed;
1069 $scope.removeFromGroup = function(selected) {
1071 angular.forEach(selected, function(user) {
1072 console.debug('removing user ' + user.id + ' from group');
1077 'open-ils.actor.usergroup.new',
1078 egCore.auth.token(), user.id, true
1083 $q.all(promises).then(function() {grid.refresh()});
1086 function addUserToGroup(user) {
1087 user.usrgroup(patronSvc.current.usrgroup());
1088 user.ischanged(true);
1091 'open-ils.actor.patron.update',
1092 egCore.auth.token(), user
1094 ).then(function() {grid.refresh()});
1097 // fetch each user ("selected" has flattened users)
1098 // update the usrgroup, then update the user object
1099 // After all updates are complete, refresh the grid.
1100 function moveUsersToGroup(target_user, selected) {
1103 angular.forEach(selected, function(user) {
1105 egCore.pcrud.retrieve('au', user.id)
1107 u.usrgroup(target_user.usrgroup());
1109 return egCore.net.request(
1111 'open-ils.actor.patron.update',
1112 egCore.auth.token(), u
1118 $q.all(promises).then(function() {grid.refresh()});
1121 function showMoveToGroupConfirm(barcode, selected, outbound) {
1124 egCore.pcrud.search('ac', {barcode : barcode})
1126 // fetch the fleshed user
1127 .then(function(card) {
1129 if (!card) return; // TODO: warn user
1131 egCore.pcrud.retrieve('au', card.usr())
1132 .then(function(user) {
1135 templateUrl: './circ/patron/t_move_to_group_dialog',
1138 '$scope','$uibModalInstance',
1139 function($scope , $uibModalInstance) {
1141 $scope.selected = selected;
1142 $scope.outbound = outbound;
1144 function(count) { $uibModalInstance.close() }
1146 function () { $uibModalInstance.dismiss() }
1149 }).result.then(function() {
1151 moveUsersToGroup(user, selected);
1153 addUserToGroup(user);
1160 // selected == move selected patrons to another patron's group
1161 // !selected == patron from a different group moves into our group
1162 function moveToGroup(selected, outbound) {
1163 egPromptDialog.open(
1164 egCore.strings.GROUP_ADD_USER, '',
1165 {ok : function(value) {
1167 showMoveToGroupConfirm(value, selected, outbound);
1172 $scope.moveToGroup = function() { moveToGroup([], false) };
1173 $scope.moveToAnotherGroup = function(selected) { moveToGroup(selected, true) };
1175 $scope.cloneUser = function(selected) {
1176 if (!selected.length) return;
1177 var url = $location.absUrl().replace(
1179 '/patron/register/clone/' + selected[0].id);
1180 $window.open(url, '_blank').focus();
1183 $scope.retrieveSelected = function(selected) {
1184 if (!selected.length) return;
1185 angular.forEach(selected, function(usr) {
1186 $timeout(function() {
1187 var url = $location.absUrl().replace(
1189 '/patron/' + usr.id + '/checkout');
1190 $window.open(url, '_blank')
1197 .controller('PatronStatCatsCtrl',
1198 ['$scope','$routeParams','$q','egCore','patronSvc',
1199 function($scope, $routeParams , $q , egCore , patronSvc) {
1200 $scope.initTab('other', $routeParams.id)
1201 .then(function(redirect) {
1202 // Entries for org-visible stat cats are fleshed. Any others
1203 // have to be fleshed within.
1206 angular.forEach(patronSvc.current.stat_cat_entries(),
1208 if (!angular.isObject(entry.stat_cat())) {
1209 to_flesh[entry.stat_cat()] = entry;
1214 if (!Object.keys(to_flesh).length) return;
1216 egCore.pcrud.search('actsc', {id : Object.keys(to_flesh)})
1217 .then(null, null, function(cat) { // stream
1218 cat.owner(egCore.org.get(cat.owner())); // owner flesh
1219 to_flesh[cat.id()].stat_cat(cat);
1224 .controller('PatronSurveyCtrl',
1225 ['$scope','$routeParams','$location','egCore','patronSvc',
1226 function($scope, $routeParams , $location , egCore , patronSvc) {
1227 $scope.initTab('other', $routeParams.id);
1228 var usr_id = $routeParams.id;
1229 var org_ids = egCore.org.fullPath(egCore.auth.user().ws_ou(), true);
1231 $scope.surveys = [];
1232 var svr_responses = {};
1234 // fetch all survey responses for this user.
1235 egCore.pcrud.search('asvr',
1237 {flesh : 2, flesh_fields : {asvr : ['survey','question','answer']}}
1240 // All responses collected and deduplicated.
1241 // Create one collection of responses per survey.
1243 angular.forEach(svr_responses, function(questions, survey_id) {
1244 var collection = {responses : []};
1245 angular.forEach(questions, function(response) {
1246 collection.survey = response.survey(); // same for one.
1247 collection.responses.push(response);
1249 $scope.surveys.push(collection);
1253 function(response) {
1255 // Discard responses for out-of-scope surveys.
1256 if (org_ids.indexOf(response.survey().owner()) < 0)
1259 // survey_id => question_id => response
1260 var svr_id = response.survey().id();
1261 var qst_id = response.question().id();
1263 if (!svr_responses[svr_id])
1264 svr_responses[svr_id] = [];
1266 if (!svr_responses[svr_id][qst_id]) {
1267 svr_responses[svr_id][qst_id] = response;
1270 // We have multiple responses for the same question.
1271 // For this UI we only care about the most recent response.
1272 if (response.effective_date() >
1273 svr_responses[svr_id][qst_id].effective_date())
1274 svr_responses[svr_id][qst_id] = response;
1280 .controller('PatronFetchLastCtrl',
1281 ['$scope','$location','egCore',
1282 function($scope , $location , egCore) {
1284 var ids = egCore.hatch.getLoginSessionItem('eg.circ.recent_patrons') || [];
1286 return $location.path('/circ/patron/' + ids[0] + '/checkout');
1288 $scope.no_last = true;
1291 .controller('PatronTriggeredEventsCtrl',
1292 ['$scope','$routeParams','$location','egCore','patronSvc',
1293 function($scope, $routeParams, $location , egCore , patronSvc) {
1294 $scope.initTab('other', $routeParams.id);
1296 var url = $location.absUrl().replace(/\/staff.*/, '/actor/user/event_log');
1297 url += '?patron_id=' + encodeURIComponent($routeParams.id);
1299 $scope.triggered_events_url = url;
1303 .controller('PatronMessageCenterCtrl',
1304 ['$scope','$routeParams','$location','egCore','patronSvc',
1305 function($scope, $routeParams, $location , egCore , patronSvc) {
1306 $scope.initTab('other', $routeParams.id);
1308 var url = $location.protocol() + '://' + $location.host()
1309 + egCore.env.basePath.replace(/\/staff.*/, '/actor/user/message');
1310 url += '/' + encodeURIComponent($routeParams.id);
1312 $scope.message_center_url = url;
1316 .controller('PatronPermsCtrl',
1317 ['$scope','$routeParams','$window','$location','egCore',
1318 function($scope , $routeParams , $window , $location , egCore) {
1319 $scope.initTab('other', $routeParams.id);
1321 var url = $location.absUrl().replace(
1322 /\/eg\/staff.*/, '/xul/server/patron/user_edit.xhtml');
1324 url += '?usr=' + encodeURIComponent($routeParams.id);
1326 // user_edit does not load the session via cookie. It uses URL
1327 // params or xulG instead. Pass via xulG.
1329 ses : egCore.auth.token(),
1330 on_patron_save : function() {
1331 $scope.funcs.reload();
1335 $scope.user_perms_url = url;