]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/patron/checkout.js
LP#1621799: disable checkout for inactive patrons
[working/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.has_email_address = function() {
49         return (
50             patronSvc.current &&
51             patronSvc.current.email() &&
52             patronSvc.current.email().match(/.*@.*/).length
53         );
54     }
55
56     $scope.may_email_receipt = function() {
57         return (
58             $scope.has_email_address() &&
59             setting_value(
60                 patronSvc.current,
61                 'circ.send_email_checkout_receipts'
62             ) == 'true'
63         );
64     }
65
66     $scope.using_hatch = egCore.hatch.usingHatch();
67
68     egCore.hatch.getItem('circ.checkout.strict_barcode')
69         .then(function(sb){ $scope.strict_barcode = sb });
70
71     // avoid multiple, in-flight attempts on the same barcode
72     var pending_barcodes = {};
73
74     var printOnComplete = true;
75     egCore.org.settings([
76         'circ.staff_client.do_not_auto_attempt_print'
77     ]).then(function(settings) { 
78         printOnComplete = !Boolean(
79             settings['circ.staff_client.do_not_auto_attempt_print']);
80     });
81
82     egCirc.get_noncat_types().then(function(list) {
83         $scope.nonCatTypes = list;
84     });
85
86     $scope.selectedNcType = function() {
87         if (!egCore.env.cnct) return null; // too soon
88         var type = egCore.env.cnct.map[$scope.checkoutArgs.noncat_type];
89         return type ? type.name() : null;
90     }
91
92     $scope.checkout = function(args) {
93         var params = angular.copy(args);
94         params.patron_id = patronSvc.current.id();
95
96         if (args.sticky_date) {
97             params.due_date = args.due_date.toISOString();
98         } else {
99             delete params.due_date;
100         }
101         delete params.sticky_date;
102
103         if (params.noncat_type == 'barcode') {
104             if (!args.copy_barcode) return;
105
106             args.copy_barcode = ''; // reset UI input
107             params.noncat_type = ''; // "barcode"
108
109             if (pending_barcodes[params.copy_barcode]) {
110                 console.log(
111                     "Skipping checkout of redundant barcode " 
112                     + params.copy_barcode
113                 );
114                 return;
115             }
116
117             pending_barcodes[params.copy_barcode] = true;
118             send_checkout(params);
119
120         } else {
121             egCirc.noncat_dialog(params).then(function() {
122                 send_checkout(params)
123             });
124         }
125
126         $scope.focusMe = true; // return focus to barcode input
127     }
128
129     function send_checkout(params) {
130
131         params.noncat_type = params.noncat ? params.noncat_type : '';
132
133         // populate the grid row before we send the request so that the
134         // order of actions is maintained and so the user gets an 
135         // immediate reaction to their barcode input action.
136         var row_item = {
137             index : $scope.checkouts.length,
138             copy_barcode : params.copy_barcode,
139             noncat_type : params.noncat_type
140         };
141
142         $scope.checkouts.unshift(row_item);
143         $scope.gridDataProvider.refresh();
144
145         egCore.hatch.setItem('circ.checkout.strict_barcode', $scope.strict_barcode);
146         var options = {check_barcode : $scope.strict_barcode};
147
148         egCirc.checkout(params, options).then(
149             function(co_resp) {
150                 // update stats locally so we don't have to fetch them w/
151                 // each checkout.
152                 patronSvc.patron_stats.checkouts.out++;
153                 patronSvc.patron_stats.checkouts.total_out++;
154
155                 // copy the response event into the original grid row item
156                 // note: angular.copy clobbers the destination
157                 row_item.evt = co_resp.evt;
158                 angular.forEach(co_resp.data, function(val, key) {
159                     row_item[key] = val;
160                 });
161                 munge_checkout_resp(co_resp, row_item);
162             },
163             function() {
164                 // Circ was rejected somewhere along the way.
165                 // Remove the copy from the grid since there was no action.
166                 // note: since checkouts are unshifted onto the array, the
167                 // index value does not (generally) match the array position.
168                 var pos = -1;
169                 angular.forEach($scope.checkouts, function(co, idx) {
170                     if (co.index == row_item.index) pos = idx;
171                 });
172                 $scope.checkouts.splice(pos, 1);
173                 $scope.gridDataProvider.refresh();
174             }
175
176         ).finally(function() {
177
178             // regardless of the outcome of the circ, remove the 
179             // barcode from the pending list.
180             if (params.copy_barcode)
181                 delete pending_barcodes[params.copy_barcode];
182
183             $scope.focusMe = true; // return focus to barcode input
184         });
185     }
186
187     // add some checkout-specific additions for display
188     function munge_checkout_resp(co_resp, row_item) {
189         var params = co_resp.params;
190         if (params.noncat) {
191             row_item.title = egCore.env.cnct.map[params.noncat_type].name();
192             row_item.noncat_count = params.noncat_count;
193             row_item.circ = new egCore.idl.circ();
194             row_item.circ.due_date(co_resp.evt.payload.noncat_circ.duedate());
195         }
196     }
197
198     $scope.print_receipt = function() {
199         var print_data = {circulations : []}
200
201         if ($scope.checkouts.length == 0) return $q.when();
202
203         angular.forEach($scope.checkouts, function(co) {
204             if (co.circ) {
205                 print_data.circulations.push({
206                     circ : egCore.idl.toHash(co.circ),
207                     copy : egCore.idl.toHash(co.acp),
208                     call_number : egCore.idl.toHash(co.acn),
209                     title : co.title,
210                     author : co.author
211                 })
212             };
213         });
214
215         return egCore.print.print({
216             context : 'default', 
217             template : 'checkout', 
218             scope : print_data,
219             show_dialog : $scope.show_print_dialog
220         });
221     }
222
223     $scope.email_receipt = function() {
224         if ($scope.has_email_address() && $scope.checkouts.length) {
225             return egCore.net.request(
226                 'open-ils.circ',
227                 'open-ils.circ.checkout.batch_notify.session.atomic',
228                 egCore.auth.token(),
229                 patronSvc.current.id(),
230                 $scope.checkouts.map(function (c) { return c.circ.id() })
231             ).then(function() {
232                 ngToast.create(egCore.strings.EMAILED_CHECKOUT_RECEIPT);
233                 return $q.when();
234             });
235         }
236         return $q.when();
237     }
238
239     $scope.print_or_email_receipt = function() {
240         if ($scope.may_email_receipt()) return $scope.email_receipt();
241         $scope.print_receipt();
242     }
243
244     // set of functions to issue a receipt (if desired), then
245     // redirect
246     $scope.done_auto_receipt = function() {
247         if ($scope.may_email_receipt()) {
248             $scope.email_receipt().then(function() {
249                 $scope.done_redirect();
250             });
251         } else {
252             if (printOnComplete) {
253
254                 $scope.print_receipt().then(function() {
255                     $scope.done_redirect();
256                 });
257
258             } else {
259                 $scope.done_redirect();
260             }
261         }
262     }
263     $scope.done_print_receipt = function() {
264         $scope.print_receipt().then( function () {
265             $scope.done_redirect();
266         });
267     }
268     $scope.done_email_receipt = function() {
269         $scope.email_receipt().then( function () {
270             $scope.done_redirect();
271         });
272     }
273     $scope.done_no_receipt = function() {
274         $scope.done_redirect();
275     }
276
277     // Redirect the user to the barcode entry page to load a new patron.
278     $scope.done_redirect = function() {
279         $location.path('/circ/patron/bcsearch');
280     }
281 }])
282