5 angular.module('egPatronApp').controller('PatronHoldsCtrl',
7 ['$scope','$q','$routeParams','egCore','egUser','patronSvc',
8 'egGridDataProvider','egHolds','$window','$location','egCirc',
9 function($scope, $q, $routeParams, egCore, egUser, patronSvc,
10 egGridDataProvider , egHolds , $window , $location , egCirc) {
12 $scope.initTab('holds', $routeParams.id);
13 $scope.holds_display = 'main';
14 $scope.detail_hold_id = $routeParams.hold_id;
16 function refresh_all() {
17 patronSvc.refreshPrimary();
19 patronSvc.hold_ids = [];
23 $scope.show_main_list = function() {
24 // don't need a full reset_page() to swap tabs
25 $scope.holds_display = 'main';
27 patronSvc.hold_ids = [];
31 $scope.show_alt_list = function() {
32 // don't need a full reset_page() to swap tabs
33 $scope.holds_display = 'alt';
35 patronSvc.hold_ids = [];
39 var provider = egGridDataProvider.instance({});
40 $scope.gridDataProvider = provider;
42 function fetchHolds(offset, count) {
43 var ids = patronSvc.hold_ids.slice(offset, offset + count);
44 return egHolds.fetch_holds(ids).then(
45 function() { $scope.loading = false; },
47 function(hold_data) { patronSvc.holds.push(hold_data) }
51 provider.get = function(offset, count) {
53 // see if we have the requested range cached
54 if (patronSvc.holds[offset]) {
55 return provider.arrayNotifier(patronSvc.holds, offset, count);
58 // see if we have the holds IDs for this range already loaded
59 if (patronSvc.hold_ids[offset]) {
60 return fetchHolds(offset, count);
63 var deferred = $q.defer();
64 patronSvc.hold_ids = [];
66 var method = 'open-ils.circ.holds.id_list.retrieve.authoritative';
67 if ($scope.holds_display == 'alt')
68 method = 'open-ils.circ.holds.canceled.id_list.retrieve.authoritative';
71 'open-ils.circ', method,
72 egCore.auth.token(), $scope.patron_id
74 ).then(function(hold_ids) {
75 if (!hold_ids.length) { deferred.resolve(); return; }
77 patronSvc.hold_ids = hold_ids;
78 fetchHolds(offset, count)
79 .then(deferred.resolve, null, deferred.notify);
82 return deferred.promise;
85 $scope.cancel_hold = function(items) {
86 var hold_ids = items.filter(function(item) {
87 return !item.hold.cancel_time();
88 }).map(function(item) {return item.hold.id()});
90 return egHolds.cancel_holds(hold_ids).then(refresh_all);
93 // jump to circ list for either 1) the targeted copy or
94 // 2) the hold target copy for copy-level holds
95 $scope.show_recent_circs = function(items) {
96 if (items.length && (copy = items[0].copy)) {
97 var url = $location.path(
98 '/cat/item/' + copy.id() + '/circ_list').absUrl();
99 $window.open(url, '_blank').focus();
103 function generic_update(items, action) {
104 if (!items.length) return $q.when();
105 var hold_ids = items.map(function(item) {return item.hold.id()});
106 return egHolds[action](hold_ids).then(refresh_all);
109 $scope.set_copy_quality = function(items) {
110 generic_update(items, 'set_copy_quality'); }
111 $scope.edit_pickup_lib = function(items) {
112 generic_update(items, 'edit_pickup_lib'); }
113 $scope.edit_notify_prefs = function(items) {
114 generic_update(items, 'edit_notify_prefs'); }
115 $scope.edit_dates = function(items) {
116 generic_update(items, 'edit_dates'); }
117 $scope.suspend = function(items) {
118 generic_update(items, 'suspend_holds'); }
119 $scope.activate = function(items) {
120 generic_update(items, 'activate_holds'); }
121 $scope.set_top_of_queue = function(items) {
122 generic_update(items, 'set_top_of_queue'); }
123 $scope.clear_top_of_queue = function(items) {
124 generic_update(items, 'clear_top_of_queue'); }
125 $scope.transfer_to_marked_title = function(items) {
126 generic_update(items, 'transfer_to_marked_title'); }
128 $scope.mark_damaged = function(items) {
130 .filter(function(item) { return Boolean(item.copy) })
131 .map(function(item) { return item.copy.id() });
133 egCirc.mark_damaged(copy_ids).then(refresh_all);
135 $scope.mark_missing = function(items) {
137 .filter(function(item) { return Boolean(item.copy) })
138 .map(function(item) { return item.copy.id() });
140 egCirc.mark_missing(copy_ids).then(refresh_all);
143 $scope.retarget = function(items) {
144 var hold_ids = items.map(function(item) { return item.hold.id() });
145 egHolds.retarget(hold_ids).then(refresh_all);
148 $scope.print = function() {
150 angular.forEach(patronSvc.holds, function(item) {
152 hold : egCore.idl.toHash(item.hold),
153 copy : egCore.idl.toHash(item.copy),
154 volume : egCore.idl.toHash(item.volume),
155 title : item.mvr.title(),
156 author : item.mvr.author()
162 template : 'holds_for_patron',
163 scope : {holds : holds}
167 $scope.detail_view = function(action, user_data, items) {
169 $location.path('/circ/patron/' +
170 $scope.patron_id + '/holds/' + h.hold.id());
174 $scope.list_view = function(items) {
175 $location.path('/circ/patron/' + $scope.patron_id + '/holds');
178 // when the detail hold is fetched (and updated), update the bib
179 // record summary display record id.
180 $scope.set_hold = function(hold_data) {
181 $scope.detail_hold_record_id = hold_data.mvr.doc_id();