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?|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',
440 function($scope , $location , egCore , egConfirmDialog , egUser , patronSvc) {
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;
478 'open-ils.actor.get_barcodes',
479 egCore.auth.token(), egCore.auth.user().ws_ou(),
480 'actor', args.barcode)
482 .then(function(resp) { // get_barcodes
484 if (evt = egCore.evt.parse(resp)) {
489 if (!resp || !resp[0]) {
490 $scope.bcNotFound = args.barcode;
491 $scope.selectMe = true;
492 egCore.audio.play('warning.patron.not_found');
496 // see if an opt-in request is needed
497 user_id = resp[0].id;
498 return egCore.net.request(
500 'open-ils.actor.user.org_unit_opt_in.check',
501 egCore.auth.token(), user_id);
503 }).then(function(optInResp) { // opt_in_check
505 if (evt = egCore.evt.parse(optInResp)) {
510 if (optInResp == 2) {
511 // opt-in disallowed at this location by patron's home library
512 $scope.optInRestricted = true;
513 $scope.selectMe = true;
514 egCore.audio.play('warning.patron.opt_in_restricted');
518 if (optInResp == 1) {
519 // opt-in handled or not needed
520 return loadPatron(user_id);
523 // opt-in needed, show the opt-in dialog
524 egUser.get(user_id, {useFields : []})
526 .then(function(user) { // retrieve user
527 var org = egCore.org.get(user.home_ou());
528 egConfirmDialog.open(
529 egCore.strings.OPT_IN_DIALOG_TITLE,
530 egCore.strings.OPT_IN_DIALOG,
531 { family_name : user.family_name(),
532 first_given_name : user.first_given_name(),
533 org_name : org.name(),
534 org_shortname : org.shortname(),
535 ok : function() { createOptIn(user.id()) },
536 cancel : function() {}
546 * Manages patron search
548 .controller('PatronSearchCtrl',
549 ['$scope','$q','$routeParams','$timeout','$window','$location','egCore','ngToast',
550 '$filter','egUser', 'patronSvc','egGridDataProvider','$document','bucketSvc',
551 'egPatronMerge','egProgressDialog','$controller','$interpolate','$uibModal',
552 function($scope, $q, $routeParams, $timeout, $window, $location, egCore , ngToast,
553 $filter, egUser, patronSvc , egGridDataProvider , $document , bucketSvc,
554 egPatronMerge , egProgressDialog , $controller , $interpolate , $uibModal) {
556 angular.extend(this, $controller('BasePatronSearchCtrl', {$scope : $scope}));
557 $scope.initTab('search');
559 $scope.gridControls = {
560 activateItem : function(item) {
561 $location.path('/circ/patron/' + item.id() + '/checkout');
563 selectedItems : function() { return [] }
566 $scope.bucketSvc = bucketSvc;
567 $scope.bucketSvc.fetchUserBuckets();
568 $scope.addToBucket = function(item, data, recs) {
569 if (recs.length == 0) return;
571 var failed_count = 0;
573 angular.forEach(recs,
575 var item = new egCore.idl.cubi();
576 item.bucket(data.id());
577 item.target_user(rec.id());
578 p.push(egCore.net.request(
580 'open-ils.actor.container.item.create',
581 egCore.auth.token(), 'user', item
583 function(){ added_count++ },
584 function(){ failed_count++ }
589 $q.all(p).then( function () {
590 if (added_count) ngToast.create($interpolate(egCore.strings.BUCKET_ADD_SUCCESS)({ count: ''+added_count, name: data.name()} ));
591 if (failed_count) ngToast.warning($interpolate(egCore.strings.BUCKET_ADD_FAIL)({ count: ''+failed_count, name: data.name() } ));
595 var temp_scope = $scope;
596 $scope.openCreateBucketDialog = function() {
598 templateUrl: './circ/patron/bucket/t_bucket_create',
601 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
602 $scope.focusMe = true;
603 $scope.ok = function(args) { $uibModalInstance.close(args) }
604 $scope.cancel = function () { $uibModalInstance.dismiss() }
606 }).result.then(function (args) {
607 if (!args || !args.name) return;
608 bucketSvc.createBucket(args.name, args.desc).then(
611 $scope.bucketSvc.fetchBucket(id).then(function (b) {
615 $scope.gridControls.selectedItems()
617 $scope.bucketSvc.fetchUserBuckets(true);
626 function() {return $scope.gridControls.selectedItems()},
629 patronSvc.setPrimary(null, list[0]);
634 $scope.need_one_selected = function() {
635 var items = $scope.gridControls.selectedItems();
636 return (items.length > 0) ? false : true;
638 $scope.need_two_selected = function() {
639 var items = $scope.gridControls.selectedItems();
640 return (items.length == 2) ? false : true;
642 $scope.merge_patrons = function() {
643 var items = $scope.gridControls.selectedItems();
644 if (items.length != 2) return false;
647 angular.forEach(items, function(i) {
648 patron_ids.push(i.id());
650 egPatronMerge.do_merge(patron_ids).then(function() {
651 // ensure that we're not drawing from cached
652 // resuts, as a successful merge just deleted a
654 delete patronSvc.lastSearch;
655 $scope.gridControls.refresh();
664 .controller('PatronMessagesCtrl',
665 ['$scope','$q','$routeParams','egCore','$uibModal','patronSvc','egCirc',
666 function($scope , $q , $routeParams, egCore , $uibModal , patronSvc , egCirc) {
667 $scope.initTab('messages', $routeParams.id);
668 var usr_id = $routeParams.id;
670 // setup date filters
671 var start = new Date(); // now - 1 year
672 start.setFullYear(start.getFullYear() - 1),
675 end_date : new Date()
678 function date_range() {
679 var start = $scope.dates.start_date.toISOString().replace(/T.*/,'');
680 var end = $scope.dates.end_date.toISOString().replace(/T.*/,'');
681 var today = new Date().toISOString().replace(/T.*/,'');
682 if (end == today) end = 'now';
688 var activeGrid = $scope.activeGridControls = {
689 setSort : function() {
692 setQuery : function() {
697 {stop_date : {'>' : 'now'}}
703 var archiveGrid = $scope.archiveGridControls = {
704 setSort : function() {
707 setQuery : function() {
710 stop_date : {'<=' : 'now'},
711 set_date : {between : date_range()}
716 $scope.removePenalty = function(selected) {
717 // the grid stores flattened penalties. Fetch penalty objects first
719 var ids = selected.map(function(s){ return s.id });
720 egCore.pcrud.search('ausp',
722 {atomic : true, authoritative : true}
725 ).then(function(penalties) {
726 return egCore.pcrud.remove(penalties);
728 // then refresh the grid
730 activeGrid.refresh();
734 $scope.archivePenalty = function(selected) {
735 // the grid stores flattened penalties. Fetch penalty objects first
737 var ids = selected.map(function(s){ return s.id });
738 egCore.pcrud.search('ausp',
740 {atomic : true, authoritative : true}
743 ).then(function(penalties) {
744 angular.forEach(penalties, function(p){ p.stop_date('now') });
745 return egCore.pcrud.update(penalties);
747 // then refresh the grid
749 activeGrid.refresh();
750 archiveGrid.refresh();
754 // leverage egEnv for caching
755 function fetchPenaltyTypes() {
757 return $q.when(egCore.env.csp.list);
758 return egCore.pcrud.search(
759 // id <= 100 are reserved for system use
760 'csp', {id : {'>': 100}}, {}, {atomic : true})
761 .then(function(penalties) {
762 egCore.env.absorbList(penalties, 'csp');
767 $scope.createPenalty = function() {
768 egCirc.create_penalty(usr_id).then(function() {
769 activeGrid.refresh();
770 // force a refresh of the user, since they may now
771 // have blocking penalties, etc.
772 patronSvc.setPrimary(patronSvc.current.id(), null, true);
776 $scope.editPenalty = function(selected) {
777 if (selected.length == 0) return;
779 // grab the penalty from the user object
780 var penalty = patronSvc.current.standing_penalties().filter(
781 function(p) {return p.id() == selected[0].id})[0];
783 egCirc.edit_penalty(penalty).then(function() {
784 activeGrid.refresh();
785 // force a refresh of the user, since they may now
786 // have blocking penalties, etc.
787 patronSvc.setPrimary(patronSvc.current.id(), null, true);
796 .controller('PatronVerifyCredentialsCtrl',
797 ['$scope','$routeParams','$location','egCore',
798 function($scope, $routeParams , $location , egCore) {
799 $scope.verified = null;
800 $scope.focusMe = true;
802 // called with a patron, pre-populate the form args
803 $scope.initTab('other', $routeParams.id).then(
805 if ($routeParams.id && $scope.patron()) {
806 $scope.prepop = true;
807 $scope.username = $scope.patron().usrname();
808 $scope.barcode = $scope.patron().card().barcode();
810 $scope.username = '';
812 $scope.password = '';
817 // verify login credentials
818 $scope.verify = function() {
819 $scope.verified = null;
820 $scope.notFound = false;
824 'open-ils.actor.verify_user_password',
825 egCore.auth.token(), $scope.barcode,
826 $scope.username, hex_md5($scope.password || '')
828 ).then(function(resp) {
829 $scope.focusMe = true;
830 if (evt = egCore.evt.parse(resp)) {
832 } else if (resp == 1) {
833 $scope.verified = true;
835 $scope.verified = false;
840 // load the main patron UI for the provided username or barcode
841 $scope.load = function($event) {
842 $scope.notFound = false;
843 $scope.verified = null;
847 'open-ils.actor.user.retrieve_id_by_barcode_or_username',
848 egCore.auth.token(), $scope.barcode, $scope.username
850 ).then(function(resp) {
853 $location.path('/circ/patron/' + resp + '/checkout');
857 // something went wrong...
858 $scope.focusMe = true;
859 if (evt = egCore.evt.parse(resp)) {
860 if (evt.textcode == 'ACTOR_USR_NOT_FOUND') {
861 $scope.notFound = true;
870 // load() button sits within the verify form.
871 // avoid submitting the verify() form action on load()
872 $event.preventDefault();
876 .controller('PatronAlertsCtrl',
877 ['$scope','$routeParams','$location','egCore','patronSvc',
878 function($scope, $routeParams , $location , egCore , patronSvc) {
880 $scope.initTab('other', $routeParams.id)
882 $scope.patronExpired = patronSvc.patronExpired;
883 $scope.patronExpiresSoon = patronSvc.patronExpiresSoon;
884 $scope.retrievedWithInactive = patronSvc.fetchedWithInactiveCard();
885 $scope.invalidAddresses = patronSvc.invalidAddresses;
890 .controller('PatronNotesCtrl',
891 ['$scope','$filter','$routeParams','$location','egCore','patronSvc','$uibModal',
893 function($scope, $filter , $routeParams , $location , egCore , patronSvc , $uibModal,
895 $scope.initTab('other', $routeParams.id);
896 var usr_id = $routeParams.id;
899 function refreshPage() {
901 egCore.pcrud.search('aun',
903 {flesh : 1, flesh_fields : {aun : ['creator']}},
904 {authoritative : true})
905 .then(null, null, function(note) {
906 $scope.notes.push(note);
910 // open the new-note dialog and create the note
911 $scope.newNote = function() {
913 templateUrl: './circ/patron/t_new_note_dialog',
916 ['$scope', '$uibModalInstance',
917 function($scope, $uibModalInstance) {
918 $scope.focusNote = true;
920 $scope.require_initials = egCore.env.aous['ui.staff.require_initials.patron_info_notes'];
921 $scope.ok = function(count) { $uibModalInstance.close($scope.args) }
922 $scope.cancel = function () { $uibModalInstance.dismiss() }
926 if (!args.value) return;
927 var note = new egCore.idl.aun();
929 note.title(args.title);
930 note.value(args.value);
931 note.pub(args.pub ? 't' : 'f');
932 note.creator(egCore.auth.user().id());
934 note.value(note.value() + ' [' + args.initials + ']');
935 egCore.pcrud.create(note).then(function() {refreshPage()});
940 // delete the selected note
941 $scope.deleteNote = function(note) {
942 egConfirmDialog.open(
943 egCore.strings.PATRON_NOTE_DELETE_CONFIRM_TITLE, egCore.strings.PATRON_NOTE_DELETE_CONFIRM,
945 egCore.pcrud.remove(note).then(function() {refreshPage()});
947 note_title : note.title(),
948 create_date : note.create_date()
952 // print the selected note
953 $scope.printNote = function(note) {
954 var hash = egCore.idl.toHash(note);
955 hash.usr = egCore.idl.toHash($scope.patron());
958 template : 'patron_note',
959 scope : {note : hash}
963 // perform the initial note fetch
967 .controller('PatronGroupCtrl',
968 ['$scope','$routeParams','$q','$window','$timeout','$location','egCore',
969 'patronSvc','$uibModal','egPromptDialog','egConfirmDialog',
970 function($scope, $routeParams , $q , $window , $timeout, $location , egCore ,
971 patronSvc , $uibModal , egPromptDialog , egConfirmDialog) {
973 var usr_id = $routeParams.id;
975 $scope.totals = {owed : 0, total_out : 0, overdue : 0}
977 var grid = $scope.gridControls = {
978 activateItem : function(item) {
979 $location.path('/circ/patron/' + item.id + '/checkout');
981 itemRetrieved : function(item) {
983 if (item.id == patronSvc.current.id()) {
984 item.stats = patronSvc.patron_stats;
987 // flesh stats for other group members
988 patronSvc.getUserStats(item.id).then(function(stats) {
990 $scope.totals.total_out += stats.checkouts.total_out;
991 $scope.totals.overdue += stats.checkouts.overdue;
995 setSort : function() {
996 return ['create_date'];
1000 $scope.initTab('other', $routeParams.id)
1001 .then(function(redirect) {
1002 // if we are redirecting to the alerts page, avoid updating the
1004 if (redirect) return;
1005 // let initTab() fetch the user first so we can know the usrgroup
1008 usrgroup : patronSvc.current.usrgroup(),
1011 $scope.totals.owed = patronSvc.patron_stats.fines.group_balance_owed;
1014 $scope.removeFromGroup = function(selected) {
1016 angular.forEach(selected, function(user) {
1017 console.debug('removing user ' + user.id + ' from group');
1022 'open-ils.actor.usergroup.new',
1023 egCore.auth.token(), user.id, true
1028 $q.all(promises).then(function() {grid.refresh()});
1031 function addUserToGroup(user) {
1032 user.usrgroup(patronSvc.current.usrgroup());
1033 user.ischanged(true);
1036 'open-ils.actor.patron.update',
1037 egCore.auth.token(), user
1039 ).then(function() {grid.refresh()});
1042 // fetch each user ("selected" has flattened users)
1043 // update the usrgroup, then update the user object
1044 // After all updates are complete, refresh the grid.
1045 function moveUsersToGroup(target_user, selected) {
1048 angular.forEach(selected, function(user) {
1050 egCore.pcrud.retrieve('au', user.id)
1052 u.usrgroup(target_user.usrgroup());
1054 return egCore.net.request(
1056 'open-ils.actor.patron.update',
1057 egCore.auth.token(), u
1063 $q.all(promises).then(function() {grid.refresh()});
1066 function showMoveToGroupConfirm(barcode, selected, outbound) {
1069 egCore.pcrud.search('ac', {barcode : barcode})
1071 // fetch the fleshed user
1072 .then(function(card) {
1074 if (!card) return; // TODO: warn user
1076 egCore.pcrud.retrieve('au', card.usr())
1077 .then(function(user) {
1080 templateUrl: './circ/patron/t_move_to_group_dialog',
1083 '$scope','$uibModalInstance',
1084 function($scope , $uibModalInstance) {
1086 $scope.selected = selected;
1087 $scope.outbound = outbound;
1089 function(count) { $uibModalInstance.close() }
1091 function () { $uibModalInstance.dismiss() }
1094 }).result.then(function() {
1096 moveUsersToGroup(user, selected);
1098 addUserToGroup(user);
1105 // selected == move selected patrons to another patron's group
1106 // !selected == patron from a different group moves into our group
1107 function moveToGroup(selected, outbound) {
1108 egPromptDialog.open(
1109 egCore.strings.GROUP_ADD_USER, '',
1110 {ok : function(value) {
1112 showMoveToGroupConfirm(value, selected, outbound);
1117 $scope.moveToGroup = function() { moveToGroup([], false) };
1118 $scope.moveToAnotherGroup = function(selected) { moveToGroup(selected, true) };
1120 $scope.cloneUser = function(selected) {
1121 if (!selected.length) return;
1122 var url = $location.absUrl().replace(
1124 '/patron/register/clone/' + selected[0].id);
1125 $window.open(url, '_blank').focus();
1128 $scope.retrieveSelected = function(selected) {
1129 if (!selected.length) return;
1130 angular.forEach(selected, function(usr) {
1131 $timeout(function() {
1132 var url = $location.absUrl().replace(
1134 '/patron/' + usr.id + '/checkout');
1135 $window.open(url, '_blank')
1142 .controller('PatronStatCatsCtrl',
1143 ['$scope','$routeParams','$q','egCore','patronSvc',
1144 function($scope, $routeParams , $q , egCore , patronSvc) {
1145 $scope.initTab('other', $routeParams.id)
1146 .then(function(redirect) {
1147 // Entries for org-visible stat cats are fleshed. Any others
1148 // have to be fleshed within.
1151 angular.forEach(patronSvc.current.stat_cat_entries(),
1153 if (!angular.isObject(entry.stat_cat())) {
1154 to_flesh[entry.stat_cat()] = entry;
1159 if (!Object.keys(to_flesh).length) return;
1161 egCore.pcrud.search('actsc', {id : Object.keys(to_flesh)})
1162 .then(null, null, function(cat) { // stream
1163 cat.owner(egCore.org.get(cat.owner())); // owner flesh
1164 to_flesh[cat.id()].stat_cat(cat);
1169 .controller('PatronSurveyCtrl',
1170 ['$scope','$routeParams','$location','egCore','patronSvc',
1171 function($scope, $routeParams , $location , egCore , patronSvc) {
1172 $scope.initTab('other', $routeParams.id);
1173 var usr_id = $routeParams.id;
1174 var org_ids = egCore.org.fullPath(egCore.auth.user().ws_ou(), true);
1176 $scope.surveys = [];
1177 var svr_responses = {};
1179 // fetch all survey responses for this user.
1180 egCore.pcrud.search('asvr',
1182 {flesh : 2, flesh_fields : {asvr : ['survey','question','answer']}}
1185 // All responses collected and deduplicated.
1186 // Create one collection of responses per survey.
1188 angular.forEach(svr_responses, function(questions, survey_id) {
1189 var collection = {responses : []};
1190 angular.forEach(questions, function(response) {
1191 collection.survey = response.survey(); // same for one.
1192 collection.responses.push(response);
1194 $scope.surveys.push(collection);
1198 function(response) {
1200 // Discard responses for out-of-scope surveys.
1201 if (org_ids.indexOf(response.survey().owner()) < 0)
1204 // survey_id => question_id => response
1205 var svr_id = response.survey().id();
1206 var qst_id = response.question().id();
1208 if (!svr_responses[svr_id])
1209 svr_responses[svr_id] = [];
1211 if (!svr_responses[svr_id][qst_id]) {
1212 svr_responses[svr_id][qst_id] = response;
1215 // We have multiple responses for the same question.
1216 // For this UI we only care about the most recent response.
1217 if (response.effective_date() >
1218 svr_responses[svr_id][qst_id].effective_date())
1219 svr_responses[svr_id][qst_id] = response;
1225 .controller('PatronFetchLastCtrl',
1226 ['$scope','$location','egCore',
1227 function($scope , $location , egCore) {
1229 var ids = egCore.hatch.getLoginSessionItem('eg.circ.recent_patrons') || [];
1231 return $location.path('/circ/patron/' + ids[0] + '/checkout');
1233 $scope.no_last = true;
1236 .controller('PatronTriggeredEventsCtrl',
1237 ['$scope','$routeParams','$location','egCore','patronSvc',
1238 function($scope, $routeParams, $location , egCore , patronSvc) {
1239 $scope.initTab('other', $routeParams.id);
1241 var url = $location.absUrl().replace(/\/staff.*/, '/actor/user/event_log');
1242 url += '?patron_id=' + encodeURIComponent($routeParams.id);
1244 $scope.triggered_events_url = url;
1248 .controller('PatronMessageCenterCtrl',
1249 ['$scope','$routeParams','$location','egCore','patronSvc',
1250 function($scope, $routeParams, $location , egCore , patronSvc) {
1251 $scope.initTab('other', $routeParams.id);
1253 var url = $location.protocol() + '://' + $location.host()
1254 + egCore.env.basePath.replace(/\/staff.*/, '/actor/user/message');
1255 url += '/' + encodeURIComponent($routeParams.id);
1257 $scope.message_center_url = url;
1261 .controller('PatronPermsCtrl',
1262 ['$scope','$routeParams','$window','$location','egCore',
1263 function($scope , $routeParams , $window , $location , egCore) {
1264 $scope.initTab('other', $routeParams.id);
1266 var url = $location.absUrl().replace(
1267 /\/eg\/staff.*/, '/xul/server/patron/user_edit.xhtml');
1269 url += '?usr=' + encodeURIComponent($routeParams.id);
1271 // user_edit does not load the session via cookie. It uses URL
1272 // params or xulG instead. Pass via xulG.
1274 ses : egCore.auth.token(),
1275 on_patron_save : function() {
1276 $scope.funcs.reload();
1280 $scope.user_perms_url = url;