]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/patron/holds.js
LP2061136 - Stamping 1405 DB upgrade script
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / circ / patron / holds.js
1 /**
2  * List of patron holds
3  */
4
5 angular.module('egPatronApp').controller('PatronHoldsCtrl',
6
7        ['$scope','$q','$routeParams','egCore','egUser','patronSvc',
8         'egGridDataProvider','egHolds','$window','$location','egCirc','egHoldGridActions',
9 function($scope,  $q,  $routeParams,  egCore,  egUser,  patronSvc,  
10         egGridDataProvider , egHolds , $window , $location , egCirc, egHoldGridActions) {
11
12     $scope.initTab('holds', $routeParams.id);
13     $scope.holds_display = 'main';
14     $scope.detail_hold_id = $routeParams.hold_id;
15     $scope.grid_actions = egHoldGridActions;
16
17     function refresh_all() {
18         patronSvc.refreshPrimary();
19         patronSvc.holds = [];
20         patronSvc.hold_ids = [];
21         provider.refresh() 
22     }
23     $scope.grid_actions.refresh = refresh_all;
24
25     $scope.show_main_list = function() {
26         // don't need a full reset_page() to swap tabs
27         $scope.holds_display = 'main';
28         patronSvc.holds = [];
29         patronSvc.hold_ids = [];
30         provider.refresh();
31     }
32
33     $scope.show_alt_list = function() {
34         // don't need a full reset_page() to swap tabs
35         $scope.holds_display = 'alt';
36         patronSvc.holds = [];
37         patronSvc.hold_ids = [];
38         provider.refresh();
39     }
40
41     $scope.hide_cancel_hold = function(action) { 
42         return $scope.holds_display == 'alt';
43     }
44
45     $scope.hide_uncancel_hold = function(action) {
46         return !$scope.hide_cancel_hold();
47     }
48
49     var provider = egGridDataProvider.instance({});
50     $scope.gridDataProvider = provider;
51
52     function fetchHolds(offset, count) {
53         // TODO: LP#1697954 Fetch all holds on grid render to support
54         // client-side sorting.  Migrate to server-side sorting to avoid
55         // the need for fetching all items.
56
57         // we're going to just fetch all the holds up front
58         //var ids = patronSvc.hold_ids.slice(offset, offset + count); 
59         return egHolds.fetch_holds(patronSvc.hold_ids).then(null, null,
60             function(hold_data) { 
61                 egCirc.flesh_copy_circ_library(hold_data.copy);
62                 patronSvc.holds.push(hold_data);
63                 return hold_data;
64             }
65         );
66     }
67
68     provider.get = function(offset, count) {
69
70         // see if we have the requested range cached
71         if (patronSvc.holds[offset]) {
72             return provider.arrayNotifier(patronSvc.holds, offset, count);
73         }
74
75         // see if we have the holds IDs for this range already loaded
76         if (patronSvc.hold_ids[offset]) {
77             return fetchHolds(offset, count);
78         }
79
80         var deferred = $q.defer();
81         patronSvc.hold_ids = [];
82
83         var method = 'open-ils.circ.holds.id_list.retrieve.authoritative';
84         if ($scope.holds_display == 'alt')
85             method = 'open-ils.circ.holds.canceled.id_list.retrieve.authoritative';
86
87         var current = 0;
88         egCore.net.request(
89             'open-ils.circ', method,
90             egCore.auth.token(), $scope.patron_id
91
92         ).then(function(hold_ids) {
93             
94             if (!hold_ids.length || hold_ids.length < offset + 1)
95             {
96                 deferred.resolve();
97                 return;
98             }
99
100             $scope.gridDataProvider.grid.totalCount = hold_ids.length;
101
102             patronSvc.hold_ids = hold_ids;
103             fetchHolds(offset, count)
104             .then(deferred.resolve, null, function (data) {
105                 if (data) {
106                     if (current >= offset && current < count) {
107                         deferred.notify(data);
108                     }
109                     current++;
110                 }
111             });
112         });
113
114         return deferred.promise;
115     }
116
117     $scope.print = function() {
118         var holds = [];
119         angular.forEach(patronSvc.holds, function(item) {
120             holds.push({
121                 hold : egCore.idl.toHash(item.hold),
122                 copy : egCore.idl.toHash(item.copy),
123                 volume : egCore.idl.toHash(item.volume),
124                 title : item.mvr.title(),
125                 author : item.mvr.author()
126             });
127         });
128
129         egCore.print.print({
130             context : 'receipt', 
131             template : 'holds_for_patron', 
132             scope : {patron : egCore.idl.toHash(patronSvc.current), holds : holds}
133         });
134     }
135
136     $scope.detail_view = function(action, user_data, items) {
137         if (h = items[0]) {
138             $location.path('/circ/patron/' + 
139                 $scope.patron_id + '/holds/' + h.hold.id());
140         }
141     }
142
143     $scope.list_view = function(items) {
144         $location.path('/circ/patron/' + $scope.patron_id + '/holds');
145     }
146
147     $scope.place_hold = function() {
148
149         egCore.hatch.setLoginSessionItem(
150             'eg.circ.patron_hold_target', patronSvc.current.card().barcode());
151
152         $window.location.href = '/eg2/staff/catalog';
153     }
154
155     // when the detail hold is fetched (and updated), update the bib
156     // record summary display record id.
157     $scope.set_hold = function(hold_data) {
158         $scope.detail_hold_record_id = hold_data.mvr.doc_id();
159     }
160
161 }])
162
163
164 .controller('PatronHoldsCreateCtrl',
165        ['$scope','$routeParams','$location','egCore','egWorkLog','patronSvc','$cookies',
166 function($scope , $routeParams , $location , egCore , egWorkLog , patronSvc , $cookies) {
167
168     // set preferred and search library cookies
169     egCore.hatch.getItem('eg.search.pref_lib').then(function(lib) {
170         $cookies.put('eg_pref_lib', lib, {path: '/'});
171     });
172     egCore.hatch.getItem('eg.search.search_lib').then(function(lib) {
173         $cookies.put('eg_search_lib', lib, {path: '/'});
174     });
175
176     $scope.handlers = {
177         opac_hold_placed : function(hold) {
178             patronSvc.fetchUserStats(); // update hold counts
179             egWorkLog.record(
180                 egCore.strings.EG_WORK_LOG_REQUESTED_HOLD,{
181                     'action' : 'requested_hold',
182                     'patron_id' : patronSvc.current.id(),
183                     'hold_id' : hold
184                 }
185             );
186         }
187     }
188
189     $scope.initTab('holds', $routeParams.id).then(function(isAlert) {
190         if (isAlert) return;
191         // not guarenteed to have a barcode until init fetches the user
192         $scope.handlers.patron_barcode = patronSvc.current.card().barcode();
193     });
194
195     $scope.catalog_url = 
196         $location.absUrl().replace(/\/staff\/.*/, '/opac/advanced');
197
198     $scope.handle_page = function(url) {
199     }
200
201 }])
202