]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/patron/checkout.js
LP#1712686 - display completed barcode on copy grids not partial input
[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
13     $scope.initTab('checkout', $routeParams.id).finally(function(){
14         $scope.focusMe = true;
15     });
16     $scope.checkouts = patronSvc.checkouts;
17     $scope.checkoutArgs = {
18         noncat_type : 'barcode',
19         due_date : new Date()
20     };
21
22     $scope.gridDataProvider = egGridDataProvider.instance({
23         get : function(offset, count) {
24             return this.arrayNotifier($scope.checkouts, offset, count);
25         }
26     });
27
28     $scope.disable_checkout = function() {
29         return (
30             !patronSvc.current ||
31             patronSvc.current.active() == 'f' ||
32             patronSvc.current.deleted() == 't' ||
33             patronSvc.current.card().active() == 'f' ||
34             patronSvc.fetchedWithInactiveCard()
35         );
36     }
37
38     function setting_value (user, setting) {
39         if (user) {
40             var list = user.settings().filter(function(s){
41                 return s.name() == setting;
42             });
43
44             if (list.length) return list[0].value();
45         }
46     }
47
48     $scope.date_options = {
49         due_date : egCore.hatch.getSessionItem('eg.circ.checkout.due_date'),
50         has_sticky_date : egCore.hatch.getSessionItem('eg.circ.checkout.is_until_logout'),
51         is_until_logout : egCore.hatch.getSessionItem('eg.circ.checkout.is_until_logout')
52     };
53
54     if ($scope.date_options.is_until_logout) { // If until_logout is set there should also be a date set.
55         $scope.checkoutArgs.due_date = new Date($scope.date_options.due_date);
56         $scope.checkoutArgs.sticky_date = true;
57     }
58
59     $scope.toggle_opt = function(opt) {
60         if ($scope.date_options[opt]) {
61             $scope.date_options[opt] = false;
62         } else {
63             $scope.date_options[opt] = true;
64         }
65     };
66
67     // The interactions between these options are complicated enough that $watch'ing them all is the only safe way to keep things sane.
68     $scope.$watch('date_options.has_sticky_date', function(newval) {
69         if ( newval ) { // was false, is true
70             // $scope.date_options.due_date = checkoutArgs.due_date;
71         } else {
72             $scope.date_options.is_until_logout = false;
73         }
74         $scope.checkoutArgs.sticky_date = newval;
75     });
76
77     $scope.$watch('date_options.is_until_logout', function(newval) {
78         if ( newval ) { // was false, is true
79             $scope.date_options.has_sticky_date = true;
80             $scope.date_options.due_date = $scope.checkoutArgs.due_date;
81             egCore.hatch.setSessionItem('eg.circ.checkout.is_until_logout', true);
82             egCore.hatch.setSessionItem('eg.circ.checkout.due_date', $scope.checkoutArgs.due_date);
83         } else {
84             egCore.hatch.removeSessionItem('eg.circ.checkout.is_until_logout');
85             egCore.hatch.removeSessionItem('eg.circ.checkout.due_date');
86         }
87     });
88
89     $scope.$watch('checkoutArgs.due_date', function(newval) {
90         if ( $scope.date_options.is_until_logout ) {
91             egCore.hatch.setSessionItem('eg.circ.checkout.due_date', newval);
92         }
93     });
94
95     $scope.has_email_address = function() {
96         return (
97             patronSvc.current &&
98             patronSvc.current.email() &&
99             patronSvc.current.email().match(/.*@.*/).length
100         );
101     }
102
103     $scope.may_email_receipt = function() {
104         return (
105             $scope.has_email_address() &&
106             setting_value(
107                 patronSvc.current,
108                 'circ.send_email_checkout_receipts'
109             ) == 'true'
110         );
111     }
112
113     $scope.using_hatch_printer = egCore.hatch.usePrinting();
114
115     egCore.hatch.getItem('circ.checkout.strict_barcode')
116         .then(function(sb){ $scope.strict_barcode = sb });
117
118     // avoid multiple, in-flight attempts on the same barcode
119     var pending_barcodes = {};
120
121     var printOnComplete = true;
122     egCore.org.settings([
123         'circ.staff_client.do_not_auto_attempt_print'
124     ]).then(function(settings) { 
125         printOnComplete = !Boolean(
126             angular.isArray(settings['circ.staff_client.do_not_auto_attempt_print']) &&
127             (settings['circ.staff_client.do_not_auto_attempt_print'].indexOf('Checkout') > -1)
128         );
129     });
130
131     egCirc.get_noncat_types().then(function(list) {
132         $scope.nonCatTypes = list;
133     });
134
135     $scope.selectedNcType = function() {
136         if (!egCore.env.cnct) return null; // too soon
137         var type = egCore.env.cnct.map[$scope.checkoutArgs.noncat_type];
138         return type ? type.name() : null;
139     }
140
141     $scope.checkout = function(args) {
142         var params = angular.copy(args);
143         params.patron_id = patronSvc.current.id();
144
145         if (args.sticky_date) {
146             params.due_date = args.due_date.toISOString();
147         } else {
148             delete params.due_date;
149         }
150         delete params.sticky_date;
151
152         if (params.noncat_type == 'barcode') {
153             if (!args.copy_barcode) return;
154
155             args.copy_barcode = ''; // reset UI input
156             params.noncat_type = ''; // "barcode"
157
158             if (pending_barcodes[params.copy_barcode]) {
159                 console.log(
160                     "Skipping checkout of redundant barcode " 
161                     + params.copy_barcode
162                 );
163                 return;
164             }
165
166             pending_barcodes[params.copy_barcode] = true;
167             send_checkout(params);
168
169         } else {
170             egCirc.noncat_dialog(params).then(function() {
171                 send_checkout(params)
172             });
173         }
174
175         $scope.focusMe = true; // return focus to barcode input
176     }
177
178     function send_checkout(params) {
179
180         params.noncat_type = params.noncat ? params.noncat_type : '';
181
182         // populate the grid row before we send the request so that the
183         // order of actions is maintained and so the user gets an 
184         // immediate reaction to their barcode input action.
185         var row_item = {
186             index : $scope.checkouts.length,
187             input_barcode : params.copy_barcode,
188             noncat_type : params.noncat_type
189         };
190
191         $scope.checkouts.unshift(row_item);
192         $scope.gridDataProvider.refresh();
193
194         egCore.hatch.setItem('circ.checkout.strict_barcode', $scope.strict_barcode);
195         var options = {check_barcode : $scope.strict_barcode};
196
197         egCirc.checkout(params, options).then(
198             function(co_resp) {
199                 // update stats locally so we don't have to fetch them w/
200                 // each checkout.
201
202                 // Avoid updating checkout counts when a checkout turns
203                 // into a renewal via auto_renew.
204                 if (!co_resp.auto_renew && !params.noncat) {
205                     patronSvc.patron_stats.checkouts.out++;
206                     patronSvc.patron_stats.checkouts.total_out++;
207                 }
208
209                 // copy the response event into the original grid row item
210                 // note: angular.copy clobbers the destination
211                 row_item.evt = co_resp.evt;
212                 angular.forEach(co_resp.data, function(val, key) {
213                     row_item[key] = val;
214                 });
215                
216                 row_item['copy_barcode'] = row_item.acp.barcode();
217
218                 munge_checkout_resp(co_resp, row_item);
219             },
220             function() {
221                 // Circ was rejected somewhere along the way.
222                 // Remove the copy from the grid since there was no action.
223                 // note: since checkouts are unshifted onto the array, the
224                 // index value does not (generally) match the array position.
225                 var pos = -1;
226                 angular.forEach($scope.checkouts, function(co, idx) {
227                     if (co.index == row_item.index) pos = idx;
228                 });
229                 $scope.checkouts.splice(pos, 1);
230                 $scope.gridDataProvider.refresh();
231             }
232
233         ).finally(function() {
234
235             // regardless of the outcome of the circ, remove the 
236             // barcode from the pending list.
237             if (params.copy_barcode)
238                 delete pending_barcodes[params.copy_barcode];
239
240             $scope.focusMe = true; // return focus to barcode input
241         });
242     }
243
244     // add some checkout-specific additions for display
245     function munge_checkout_resp(co_resp, row_item) {
246         var params = co_resp.params;
247         if (params.noncat) {
248             row_item.title = egCore.env.cnct.map[params.noncat_type].name();
249             row_item.noncat_count = params.noncat_count;
250             row_item.circ = new egCore.idl.circ();
251             row_item.circ.due_date(co_resp.evt[0].payload.noncat_circ.duedate());
252             // Non-cat circs don't return the full list of circs.
253             // Refresh the list of non-cat circs from the server.
254             patronSvc.getUserNonCats(patronSvc.current.id());
255         }
256     }
257
258     $scope.print_receipt = function() {
259         var print_data = {circulations : []}
260
261         if ($scope.checkouts.length == 0) return $q.when();
262
263         angular.forEach($scope.checkouts, function(co) {
264             if (co.circ) {
265                 print_data.circulations.push({
266                     circ : egCore.idl.toHash(co.circ),
267                     copy : egCore.idl.toHash(co.acp),
268                     call_number : egCore.idl.toHash(co.acn),
269                     title : co.title,
270                     author : co.author
271                 })
272             };
273         });
274
275         return egCore.print.print({
276             context : 'default', 
277             template : 'checkout', 
278             scope : print_data,
279             show_dialog : $scope.show_print_dialog
280         });
281     }
282
283     $scope.email_receipt = function() {
284         if ($scope.has_email_address() && $scope.checkouts.length) {
285             return egCore.net.request(
286                 'open-ils.circ',
287                 'open-ils.circ.checkout.batch_notify.session.atomic',
288                 egCore.auth.token(),
289                 patronSvc.current.id(),
290                 $scope.checkouts.map(function (c) { return c.circ.id() })
291             ).then(function() {
292                 ngToast.create(egCore.strings.EMAILED_CHECKOUT_RECEIPT);
293                 return $q.when();
294             });
295         }
296         return $q.when();
297     }
298
299     $scope.print_or_email_receipt = function() {
300         if ($scope.may_email_receipt()) return $scope.email_receipt();
301         $scope.print_receipt();
302     }
303
304     // set of functions to issue a receipt (if desired), then
305     // redirect
306     $scope.done_auto_receipt = function() {
307         if ($scope.may_email_receipt()) {
308             $scope.email_receipt().then(function() {
309                 $scope.done_redirect();
310             });
311         } else {
312             if (printOnComplete) {
313
314                 $scope.print_receipt().then(function() {
315                     $scope.done_redirect();
316                 });
317
318             } else {
319                 $scope.done_redirect();
320             }
321         }
322     }
323     $scope.done_print_receipt = function() {
324         $scope.print_receipt().then( function () {
325             $scope.done_redirect();
326         });
327     }
328     $scope.done_email_receipt = function() {
329         $scope.email_receipt().then( function () {
330             $scope.done_redirect();
331         });
332     }
333     $scope.done_no_receipt = function() {
334         $scope.done_redirect();
335     }
336
337     // Redirect the user to the barcode entry page to load a new patron.
338     $scope.done_redirect = function() {
339         $location.path('/circ/patron/bcsearch');
340     }
341 }])
342