]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/patron/items_out.js
LP#1402797 Add checkout/in workstation colums to the available set
[Evergreen.git] / Open-ILS / web / js / ui / default / staff / circ / patron / items_out.js
1 /**
2  * List of patron items checked out
3  */
4
5 angular.module('egPatronApp')
6
7 .controller('PatronItemsOutCtrl',
8        ['$scope','$q','$routeParams','egCore','egUser','patronSvc',
9         'egGridDataProvider','$modal','egCirc','egConfirmDialog','egBilling',
10 function($scope,  $q,  $routeParams,  egCore , egUser,  patronSvc , 
11          egGridDataProvider , $modal , egCirc , egConfirmDialog , egBilling) {
12     $scope.initTab('items_out', $routeParams.id);
13
14     // cache of circ objects for grid display
15     patronSvc.items_out = [];
16
17     // main list of checked out items
18     $scope.main_list = [];
19
20     // list of alt circs (lost, etc.) and/or check-in with fines circs
21     $scope.alt_list = []; 
22
23     // these are fetched during startup (i.e. .configure())
24     // By default, show lost/lo/cr items in the alt list
25     var display_lost = Number(
26         egCore.env.aous['ui.circ.items_out.lost']) || 2;
27     var display_lo = Number(
28         egCore.env.aous['ui.circ.items_out.longoverdue']) || 2;
29     var display_cr = Number(
30         egCore.env.aous['ui.circ.items_out.claimsreturned']) || 2;
31
32     var fetch_checked_in = true;
33     $scope.show_alt_circs = true;
34     if (display_lost & 4 && display_lo & 4 && display_cr & 4) {
35         // all special types are configured to be hidden once
36         // checked in, so there's no need to fetch checked-in circs.
37         fetch_checked_in = false;
38
39         if (display_lost & 1 && display_lo & 1 && display_cr & 1) {                 
40             // additionally, if all types are configured to display    
41             // in the main list while checked out, nothing will         
42             // ever appear in the alternate list, so we can hide          
43             // the alternate list from the UI.  
44             $scope.show_alt_circs = false;
45         }
46     }
47
48     $scope.items_out_display = 'main';
49     $scope.show_main_list = function() {
50         // don't need a full reset_page() to swap tabs
51         $scope.items_out_display = 'main';
52         patronSvc.items_out = [];
53         provider.refresh();
54     }
55
56     $scope.show_alt_list = function() {
57         // don't need a full reset_page() to swap tabs
58         $scope.items_out_display = 'alt';
59         patronSvc.items_out = [];
60         provider.refresh();
61     }
62
63     // Reload the user to pick up changes in items out, fines, etc.
64     // Reload circs since the contents of the main vs. alt list may
65     // have changed.
66     function reset_page() {
67         patronSvc.refreshPrimary();
68         patronSvc.items_out = []; 
69         $scope.main_list = [];
70         $scope.alt_list = [];
71         provider.refresh() 
72     }
73
74     var provider = egGridDataProvider.instance({});
75     $scope.gridDataProvider = provider;
76
77     function fetch_circs(id_list, offset, count) {
78         if (!id_list.length) return $q.when();
79
80         // fetch the lot of circs and stream the results back via notify
81         return egCore.pcrud.search('circ', {id : id_list},
82             {   flesh : 4,
83                 flesh_fields : {
84                     circ : ['target_copy', 'workstation', 'checkin_workstation'],
85                     acp : ['call_number'],
86                     acn : ['record'],
87                     bre : ['simple_record']
88                 },
89                 // avoid fetching the MARC blob by specifying which 
90                 // fields on the bre to select.  More may be needed.
91                 // note that fleshed fields are explicitly selected.
92                 select : { bre : ['id'] },
93                 limit  : count,
94                 offset : offset,
95                 // we need an order-by to support paging
96                 order_by : {circ : ['xact_start']} 
97
98         }).then(null, null, function(circ) {
99             circ.circ_lib(egCore.org.get(circ.circ_lib())); // local fleshing
100
101             if (circ.target_copy().call_number().id() == -1) {
102                 // dummy-up a record for precat items
103                 circ.target_copy().call_number().record().simple_record({
104                     title : function() {return circ.target_copy().dummy_title()},
105                     author : function() {return circ.target_copy().dummy_author()},
106                     isbn : function() {return circ.target_copy().dummy_isbn()}
107                 })
108             }
109
110             patronSvc.items_out.push(circ); // toss it into the cache
111             return circ;
112         });
113     }
114
115     // decide which list each circ belongs to
116     function promote_circs(list, display_code, open) {
117         if (open) {                                                    
118             if (1 & display_code) { // bitflag 1 == top list                   
119                 $scope.main_list = $scope.main_list.concat(list);
120             } else {                                                   
121                 $scope.alt_list = $scope.alt_list.concat(list);
122             }                                                          
123         } else {                                                       
124             if (4 & display_code) return;  // bitflag 4 == hide on checkin     
125             $scope.alt_list = $scope.alt_list.concat(list);
126         } 
127     }
128
129     // fetch IDs for circs we care about
130     function get_circ_ids() {
131         $scope.main_list = [];
132         $scope.alt_list = [];
133
134         // we can fetch these in parallel
135         var promise1 = egCore.net.request(
136             'open-ils.actor',
137             'open-ils.actor.user.checked_out.authoritative',
138             egCore.auth.token(), $scope.patron_id
139         ).then(function(outs) {
140             $scope.main_list = outs.out.concat(outs.overdue);
141             promote_circs(outs.lost, display_lost, true);                            
142             promote_circs(outs.long_overdue, display_lo, true);             
143             promote_circs(outs.claims_returned, display_cr, true);
144         });
145
146         // only fetched checked-in-with-bills circs if configured to display
147         var promise2 = !fetch_checked_in ? $q.when() : egCore.net.request(
148             'open-ils.actor',
149             'open-ils.actor.user.checked_in_with_fines.authoritative',
150             egCore.auth.token(), $scope.patron_id
151         ).then(function(outs) {
152             promote_circs(outs.lost, display_lost);
153             promote_circs(outs.long_overdue, display_lo);
154             promote_circs(outs.claims_returned, display_cr);
155         });
156
157         return $q.all([promise1, promise2]);
158     }
159
160     provider.get = function(offset, count) {
161
162         var id_list = $scope[$scope.items_out_display + '_list'];
163
164         // see if we have the requested range cached
165         if (patronSvc.items_out[offset]) {
166             return provider.arrayNotifier(
167                 patronSvc.items_out, offset, count);
168         }
169
170         // See if we have the circ IDs for this range already loaded.
171         // this would happen navigating to a subsequent page.
172         if (id_list[offset]) {
173             return fetch_circs(id_list, offset, count);
174         }
175
176         // avoid returning the request directly to the caller so the
177         // notify()'s from egCore.net.request don't leak into the 
178         // final set of notifies (i.e. the real responses);
179
180         var deferred = $q.defer();
181         get_circ_ids().then(function() {
182
183             id_list = $scope[$scope.items_out_display + '_list'];
184
185             // relay the notified circs back to the grid through our promise
186             fetch_circs(id_list, offset, count).then(
187                 deferred.resolve, null, deferred.notify);
188         });
189
190         return deferred.promise;
191     }
192
193
194     // true if circ is overdue, false otherwise
195     $scope.circIsOverdue = function(circ) {
196         // circ may not exist yet for rendered row
197         if (!circ) return false;
198
199         var date = new Date();
200         date.setTime(Date.parse(circ.due_date()));
201         return date < new Date();
202     }
203
204     $scope.edit_due_date = function(items) {
205         if (!items.length) return;
206
207         $modal.open({
208             templateUrl : './circ/patron/t_edit_due_date_dialog',
209             controller : [
210                         '$scope','$modalInstance',
211                 function($scope , $modalInstance) {
212
213                     // if there is only one circ, default to the due date
214                     // of that circ.  Otherwise, default to today.
215                     var due_date = items.length == 1 ? 
216                         Date.parse(items[0].due_date()) : new Date();
217
218                     $scope.args = {
219                         num_circs : items.length,
220                         due_date : due_date
221                     }
222
223                     // Fire off the due-date updater for each circ.
224                     // When all is done, close the dialog
225                     $scope.ok = function(args) {
226                         var due = args.due_date.toISOString().replace(/T.*/,'');
227                         console.debug("applying due date of " + due);
228
229                         var promises = [];
230                         angular.forEach(items, function(circ) {
231                             promises.push(
232                                 egCore.net.request(
233                                     'open-ils.circ',
234                                     'open-ils.circ.circulation.due_date.update',
235                                     egCore.auth.token(), circ.id(), due
236
237                                 ).then(function(new_circ) {
238                                     // update the grid circ with the canonical 
239                                     // date from the modified circulation.
240                                     circ.due_date(new_circ.due_date());
241                                 })
242                             );
243                         });
244
245                         $q.all(promises).then(function() {
246                             $modalInstance.close();
247                             provider.refresh();
248                         });
249                     }
250                     $scope.cancel = function($event) {
251                         $modalInstance.dismiss();
252                         $event.preventDefault();
253                     }
254                 }
255             ]
256         });
257     }
258
259     $scope.print_receipt = function(items) {
260         if (items.length == 0) return $q.when();
261         var print_data = {circulations : []}
262
263         angular.forEach(patronSvc.items_out, function(circ) {
264             print_data.circulations.push({
265                 circ : egCore.idl.toHash(circ),
266                 copy : egCore.idl.toHash(circ.target_copy()),
267                 call_number : egCore.idl.toHash(circ.target_copy().call_number()),
268                 title : circ.target_copy().call_number().record().simple_record().title(),
269                 author : circ.target_copy().call_number().record().simple_record().author(),
270             })
271         });
272
273         return egCore.print.print({
274             context : 'default', 
275             template : 'items_out', 
276             scope : print_data,
277         });
278     }
279
280     function batch_action_with_barcodes(items, action) {
281         if (!items.length) return;
282         var barcodes = items.map(function(circ) 
283             { return circ.target_copy().barcode() });
284         action(barcodes).then(reset_page);
285     }
286     $scope.mark_lost = function(items) {
287         batch_action_with_barcodes(items, egCirc.mark_lost);
288     }
289     $scope.mark_claims_returned = function(items) {
290         batch_action_with_barcodes(items, egCirc.mark_claims_returned_dialog);
291     }
292     $scope.mark_claims_never_checked_out = function(items) {
293         batch_action_with_barcodes(items, egCirc.mark_claims_never_checked_out);
294     }
295
296     $scope.renew = function(items, msg) {
297         if (!items.length) return;
298         var barcodes = items.map(function(circ) 
299             { return circ.target_copy().barcode() });
300
301         if (!msg) msg = egCore.strings.RENEW_ITEMS;
302
303         return egConfirmDialog.open(msg, barcodes.join(' '), {}).result
304         .then(function() {
305             function do_one() {
306                 var bc = barcodes.pop();
307                 if (!bc) { reset_page(); return }
308                 // finally -> continue even when one fails
309                 egCirc.renew({copy_barcode : bc}).finally(do_one);
310             }
311             do_one();
312         });
313     }
314
315     $scope.renew_all = function() {
316         var circs = patronSvc.items_out.filter(function(circ) {
317             return (
318                 // all others will be rejected at the server
319                 !circ.stop_fines() ||
320                 circ.stop_fines() == 'MAXFINES'
321             );
322         });
323         $scope.renew(circs, egCore.strings.RENEW_ALL_ITEMS);
324     }
325
326     $scope.renew_with_date = function(items) {
327         if (!items.length) return;
328         var barcodes = items.map(function(circ) 
329             { return circ.target_copy().barcode() });
330
331         return $modal.open({
332             templateUrl : './circ/patron/t_edit_due_date_dialog',
333             templateUrl : './circ/patron/t_renew_with_date_dialog',
334             controller : [
335                         '$scope','$modalInstance',
336                 function($scope , $modalInstance) {
337                     $scope.args = {
338                         barcodes : barcodes,
339                         date : new Date()
340                     }
341                     $scope.cancel = function() {$modalInstance.dismiss()}
342
343                     // Fire off the due-date updater for each circ.
344                     // When all is done, close the dialog
345                     $scope.ok = function() {
346                         var due = $scope.args.date.toISOString().replace(/T.*/,'');
347                         console.debug("renewing with due date: " + due);
348
349                         function do_one() {
350                             if (bc = barcodes.pop()) {
351                                 egCirc.renew({copy_barcode : bc, due_date : due})
352                                 .finally(do_one);
353                             } else {
354                                 $modalInstance.close(); 
355                                 reset_page();
356                             }
357                         }
358                        do_one(); // kick it off
359                     }
360                 }
361             ]
362         }).result;
363     }
364
365     $scope.checkin = function(items) {
366         if (!items.length) return;
367         var barcodes = items.map(function(circ) 
368             { return circ.target_copy().barcode() });
369
370         return egConfirmDialog.open(
371             egCore.strings.CHECK_IN_CONFIRM, barcodes.join(' '), {
372
373         }).result.then(function() {
374             function do_one() {
375                 if (bc = barcodes.pop()) {
376                     egCirc.checkin({copy_barcode : bc})
377                     .finally(do_one);
378                 } else {
379                     reset_page();
380                 }
381             }
382             do_one(); // kick it off
383         });
384     }
385
386     $scope.add_billing = function(items) {
387         if (!items.length) return;
388         var circs = items.concat(); // don't pop from grid array
389         function do_one() {
390             var circ; // don't clobber window.circ!
391             if (circ = circs.pop()) {
392                 egBilling.showBillDialog({
393                     // let the dialog fetch the transaction, since it's
394                     // not sufficiently fleshed here.
395                     xact_id : circ.id(),
396                     patron : patronSvc.current
397                 }).finally(do_one);
398             } else {
399                 reset_page();
400             }
401         }
402         do_one();
403     }
404
405 }]);
406