]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/patron/checkout.js
LP2042879 Shelving Location Groups Admin accessibility
[Evergreen.git] / Open-ILS / web / js / ui / default / staff / circ / patron / checkout.js
1 /**
2  * Checkout items to patrons
3  */
4
5 angular.module('egPatronApp').controller('PatronCheckoutCtrl',
6
7        ['$scope','$q','$routeParams','egCore','egUser','patronSvc',
8         'egGridDataProvider','$location','$timeout','egCirc','ngToast',
9
10 function($scope , $q , $routeParams , egCore , egUser , patronSvc , 
11          egGridDataProvider , $location , $timeout , egCirc , ngToast) {
12     var now = new Date();
13
14     $scope.initTab('checkout', $routeParams.id).finally(function(){
15         $scope.focusMe = true;
16     });
17     $scope.checkouts = patronSvc.checkouts;
18     $scope.checkoutArgs = {
19         noncat_type : 'barcode',
20         due_date : new Date(now)
21     };
22
23     $scope.minDate = new Date(now);
24     $scope.outOfRange = false;
25     $scope.gridDataProvider = egGridDataProvider.instance({
26         get : function(offset, count) {
27             return this.arrayNotifier($scope.checkouts, offset, count);
28         }
29     });
30
31     $scope.disable_checkout = function() {
32         return (
33             !patronSvc.current ||
34             patronSvc.current.active() == 'f' ||
35             patronSvc.current.deleted() == 't' ||
36             patronSvc.current.card().active() == 'f' ||
37             patronSvc.fetchedWithInactiveCard() ||
38             $scope.outOfRange == true
39         );
40     }
41
42     function setting_value (user, setting) {
43         if (user) {
44             var list = user.settings().filter(function(s){
45                 return s.name() == setting;
46             });
47
48             if (list.length) return list[0].value();
49         }
50     }
51
52     $scope.date_options = {
53         due_date : egCore.hatch.getSessionItem('eg.circ.checkout.due_date'),
54         has_sticky_date : egCore.hatch.getSessionItem('eg.circ.checkout.is_until_logout'),
55         is_until_logout : egCore.hatch.getSessionItem('eg.circ.checkout.is_until_logout')
56     };
57
58     if ($scope.date_options.is_until_logout) { // If until_logout is set there should also be a date set.
59         $scope.checkoutArgs.due_date = new Date($scope.date_options.due_date);
60         $scope.checkoutArgs.sticky_date = true;
61     }
62
63     $scope.toggle_opt = function(opt) {
64         if ($scope.date_options[opt]) {
65             $scope.date_options[opt] = false;
66         } else {
67             $scope.date_options[opt] = true;
68         }
69     };
70
71     // The interactions between these options are complicated enough that $watch'ing them all is the only safe way to keep things sane.
72     $scope.$watch('date_options.has_sticky_date', function(newval) {
73         if ( newval ) { // was false, is true
74             // $scope.date_options.due_date = checkoutArgs.due_date;
75         } else {
76             $scope.date_options.is_until_logout = false;
77         }
78         $scope.checkoutArgs.sticky_date = newval;
79     });
80
81     $scope.$watch('date_options.is_until_logout', function(newval) {
82         if ( newval ) { // was false, is true
83             $scope.date_options.has_sticky_date = true;
84             $scope.date_options.due_date = $scope.checkoutArgs.due_date;
85             egCore.hatch.setSessionItem('eg.circ.checkout.is_until_logout', true);
86             egCore.hatch.setSessionItem('eg.circ.checkout.due_date', $scope.checkoutArgs.due_date);
87         } else {
88             egCore.hatch.removeSessionItem('eg.circ.checkout.is_until_logout');
89             egCore.hatch.removeSessionItem('eg.circ.checkout.due_date');
90         }
91     });
92
93     $scope.$watch('checkoutArgs.due_date', function(newval) {
94         if ( $scope.date_options.is_until_logout && !isNaN(newval)) {
95             if (!$scope.outOfRange) {
96                 egCore.hatch.setSessionItem('eg.circ.checkout.due_date', newval);
97             } else {
98                 egCore.hatch.setSessionItem('eg.circ.checkout.due_date', $scope.checkoutArgs.due_date);
99             }
100         }
101     });
102
103     $scope.has_email_address = function() {
104         return (
105             patronSvc.current &&
106             patronSvc.current.email() &&
107             patronSvc.current.email().match(/.*@.*/)
108         );
109     }
110
111     $scope.may_email_receipt = function() {
112         return (
113             $scope.has_email_address() &&
114             setting_value(
115                 patronSvc.current,
116                 'circ.send_email_checkout_receipts'
117             ) == 'true'
118         );
119     }
120
121     egCore.hatch.usePrinting().then(function(useHatch) {
122         $scope.using_hatch_printer = useHatch;
123     });
124
125     egCore.hatch.getItem('circ.checkout.strict_barcode')
126         .then(function(sb){ $scope.strict_barcode = sb });
127
128     // avoid multiple, in-flight attempts on the same barcode
129     var pending_barcodes = {};
130
131     var printOnComplete = true;
132     egCore.org.settings([
133         'circ.staff_client.do_not_auto_attempt_print'
134     ]).then(function(settings) { 
135         printOnComplete = !Boolean(
136             angular.isArray(settings['circ.staff_client.do_not_auto_attempt_print']) &&
137             (settings['circ.staff_client.do_not_auto_attempt_print'].indexOf('Checkout') > -1)
138         );
139     });
140
141     egCirc.get_noncat_types().then(function(list) {
142         $scope.nonCatTypes = list;
143     });
144
145     $scope.selectedNcType = function() {
146         if (!egCore.env.cnct) return null; // too soon
147         var type = egCore.env.cnct.map[$scope.checkoutArgs.noncat_type];
148         return type ? type.name() : null;
149     }
150
151     $scope.checkout = function(args) {
152         var params = angular.copy(args);
153         params.patron_id = patronSvc.current.id();
154
155         if (args.sticky_date) {
156             params.due_date = args.due_date.toISOString();
157         } else {
158             delete params.due_date;
159         }
160         delete params.sticky_date;
161
162         if (params.noncat_type == 'barcode') {
163             if (!args.copy_barcode) return;
164
165             args.copy_barcode = ''; // reset UI input
166             params.noncat_type = ''; // "barcode"
167
168             if (pending_barcodes[params.copy_barcode]) {
169                 console.log(
170                     "Skipping checkout of redundant barcode " 
171                     + params.copy_barcode
172                 );
173                 return;
174             }
175
176             pending_barcodes[params.copy_barcode] = true;
177             send_checkout(params);
178
179         } else {
180             egCirc.noncat_dialog(params).then(function() {
181                 send_checkout(params)
182             });
183         }
184
185         $scope.focusMe = true; // return focus to barcode input
186     }
187
188     function send_checkout(params) {
189
190         params.noncat_type = params.noncat ? params.noncat_type : '';
191
192         // populate the grid row before we send the request so that the
193         // order of actions is maintained and so the user gets an 
194         // immediate reaction to their barcode input action.
195         var row_item = {
196             index : $scope.checkouts.length,
197             input_barcode : params.copy_barcode,
198             noncat_type : params.noncat_type
199         };
200
201         $scope.checkouts.unshift(row_item);
202         $scope.gridDataProvider.prepend();
203
204         var options = {check_barcode : $scope.strict_barcode};
205
206         egCirc.checkout(params, options).then(
207             function(co_resp) {
208                 // update stats locally so we don't have to fetch them w/
209                 // each checkout.
210
211                 // Avoid updating checkout counts when a checkout turns
212                 // into a renewal via auto_renew.
213                 if (!co_resp.auto_renew && !params.noncat && !options.sameCopyCheckout) {
214                     patronSvc.patron_stats.checkouts.out++;
215                     patronSvc.patron_stats.checkouts.total_out++;
216                 }
217
218                 // copy the response event into the original grid row item
219                 // note: angular.copy clobbers the destination
220                 row_item.evt = co_resp.evt;
221                 angular.forEach(co_resp.data, function(val, key) {
222                     row_item[key] = val;
223                 });
224                
225                 if (row_item.acp) { // unset for non-cat items.
226                     row_item['copy_barcode'] = row_item.acp.barcode();
227                 }
228
229                 munge_checkout_resp(co_resp, row_item);
230             },
231             function() {
232                 // Circ was rejected somewhere along the way.
233                 // Remove the copy from the grid since there was no action.
234                 // note: since checkouts are unshifted onto the array, the
235                 // index value does not (generally) match the array position.
236                 var pos = -1;
237                 angular.forEach($scope.checkouts, function(co, idx) {
238                     if (co.index == row_item.index) pos = idx;
239                 });
240                 $scope.checkouts.splice(pos, 1);
241                 $scope.gridDataProvider.refresh();
242             }
243
244         ).finally(function() {
245
246             // regardless of the outcome of the circ, remove the 
247             // barcode from the pending list.
248             if (params.copy_barcode)
249                 delete pending_barcodes[params.copy_barcode];
250
251             $scope.focusMe = true; // return focus to barcode input
252         });
253     }
254
255     // add some checkout-specific additions for display
256     function munge_checkout_resp(co_resp, row_item) {
257         var params = co_resp.params;
258         if (params.noncat) {
259             row_item.title = egCore.env.cnct.map[params.noncat_type].name();
260             row_item.noncat_count = params.noncat_count;
261             row_item.circ = new egCore.idl.circ();
262             row_item.circ.due_date(co_resp.evt[0].payload.noncat_circ.duedate());
263             // Non-cat circs don't return the full list of circs.
264             // Refresh the list of non-cat circs from the server.
265             patronSvc.getUserNonCats(patronSvc.current.id());
266             row_item.copy_alert_count = 0;
267         } else {
268             row_item.copy_alert_count = 0;
269             egCore.pcrud.search(
270                 'aca',
271                 { copy : co_resp.data.acp.id(), ack_time : null },
272                 null,
273                 { atomic : true }
274             ).then(function(list) {
275                 row_item.copy_alert_count = list.length;
276             });
277         }
278     }
279
280     $scope.addCopyAlerts = function(items) {
281         var copy_ids = [];
282         angular.forEach(items, function(item) {
283             if (item.acp) copy_ids.push(item.acp.id());
284         });
285         egCirc.add_copy_alerts(copy_ids).then(function() {
286             // update grid items?
287         });
288     }
289
290     $scope.manageCopyAlerts = function(items) {
291         var copy_ids = [];
292         angular.forEach(items, function(item) {
293             if (item.acp) copy_ids.push(item.acp.id());
294         });
295         egCirc.manage_copy_alerts(copy_ids).then(function() {
296             // update grid items?
297         });
298     }
299
300     $scope.gridCellHandlers = {};
301     $scope.gridCellHandlers.copyAlertsEdit = function(id) {
302         egCirc.manage_copy_alerts([id]).then(function() {
303             // update grid items?
304         });
305     };
306
307     $scope.onStrictBarcodeChange = function() {
308         egCore.hatch.setItem(
309             'circ.checkout.strict_barcode',
310             $scope.strict_barcode
311         );
312     };
313
314     $scope.print_receipt = function() {
315         var print_data = {circulations : []};
316         var cusr = patronSvc.current;
317
318         if ($scope.checkouts.length == 0) return $q.when();
319
320         angular.forEach($scope.checkouts, function(co) {
321             if (co.circ) {
322                 print_data.circulations.push({
323                     circ : egCore.idl.toHash(co.circ),
324                     copy : egCore.idl.toHash(co.acp),
325                     call_number : egCore.idl.toHash(co.acn), // Wrong?
326                     owning_lib : egCore.idl.toHash(co.aou), // Wrong?
327                     title : co.title,
328                     author : co.author
329                 });
330             };
331         });
332
333         // This is repeated in patron.* so everything is in one place but left here so existing templates don't break.
334         print_data.patron_money = patronSvc.patron_stats.fines;
335         print_data.patron = {
336             prefix : cusr.prefix(),
337             first_given_name : cusr.first_given_name(),
338             second_given_name : cusr.second_given_name(),
339             family_name : cusr.family_name(),
340             suffix : cusr.suffix(),
341             pref_prefix : cusr.pref_prefix(),
342             pref_first_given_name : cusr.pref_first_given_name(),
343             pref_secondg_given_name : cusr.second_given_name(),
344             pref_family_name : cusr.pref_family_name(),
345             suffix : cusr.suffix(),
346             card : { barcode : cusr.card().barcode() },
347             money_summary : patronSvc.patron_stats.fines,
348             expire_date : cusr.expire_date(),
349             alias : cusr.alias(),
350             has_email : Boolean($scope.has_email_address()),
351             has_phone : Boolean(cusr.day_phone() || cusr.evening_phone() || cusr.other_phone()),
352             juvenile : cusr.juvenile()
353         };
354
355         return egCore.print.print({
356             context : 'default', 
357             template : 'checkout', 
358             scope : print_data,
359             show_dialog : $scope.show_print_dialog
360         });
361     }
362
363     $scope.email_receipt = function() {
364         if ($scope.has_email_address() && $scope.checkouts.length) {
365             return egCore.net.request(
366                 'open-ils.circ',
367                 'open-ils.circ.checkout.batch_notify.session.atomic',
368                 egCore.auth.token(),
369                 patronSvc.current.id(),
370                 $scope.checkouts.map(function (c) { return c.circ.id() })
371             ).then(function() {
372                 ngToast.create(egCore.strings.EMAILED_CHECKOUT_RECEIPT);
373                 return $q.when();
374             });
375         }
376         return $q.when();
377     }
378
379     $scope.print_or_email_receipt = function() {
380         if ($scope.may_email_receipt()) return $scope.email_receipt();
381         $scope.print_receipt();
382     }
383
384     // set of functions to issue a receipt (if desired), then
385     // redirect
386     $scope.done_auto_receipt = function() {
387         if ($scope.may_email_receipt()) {
388             $scope.email_receipt().then(function() {
389                 $scope.done_redirect();
390             });
391         } else {
392             if (printOnComplete) {
393
394                 $scope.print_receipt().then(function() {
395                     $scope.done_redirect();
396                 });
397
398             } else {
399                 $scope.done_redirect();
400             }
401         }
402     }
403     $scope.done_print_receipt = function() {
404         $scope.print_receipt().then( function () {
405             $scope.done_redirect();
406         });
407     }
408     $scope.done_email_receipt = function() {
409         $scope.email_receipt().then( function () {
410             $scope.done_redirect();
411         });
412     }
413     $scope.done_no_receipt = function() {
414         $scope.done_redirect();
415     }
416
417     // Redirect the user to the barcode entry page to load a new patron.
418     $scope.done_redirect = function() {
419         $location.path('/circ/patron/bcsearch');
420     }
421 }])
422