2 * Checkout items to patrons
5 angular.module('egPatronApp').controller('PatronCheckoutCtrl',
7 ['$scope','$q','$routeParams','egCore','egUser','patronSvc',
8 'egGridDataProvider','$location','$timeout','egCirc','ngToast',
10 function($scope , $q , $routeParams , egCore , egUser , patronSvc ,
11 egGridDataProvider , $location , $timeout , egCirc , ngToast) {
13 $scope.initTab('checkout', $routeParams.id).finally(function(){
14 $scope.focusMe = true;
16 $scope.checkouts = patronSvc.checkouts;
17 $scope.checkoutArgs = {
18 noncat_type : 'barcode',
22 $scope.gridDataProvider = egGridDataProvider.instance({
23 get : function(offset, count) {
24 return this.arrayNotifier($scope.checkouts, offset, count);
28 $scope.disable_checkout = function() {
31 patronSvc.current.active() == 'f' ||
32 patronSvc.current.deleted() == 't' ||
33 patronSvc.current.card().active() == 'f' ||
34 patronSvc.fetchedWithInactiveCard()
38 function setting_value (user, setting) {
40 var list = user.settings().filter(function(s){
41 return s.name() == setting;
44 if (list.length) return list[0].value();
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')
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;
59 $scope.toggle_opt = function(opt) {
60 if ($scope.date_options[opt]) {
61 $scope.date_options[opt] = false;
63 $scope.date_options[opt] = true;
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;
72 $scope.date_options.is_until_logout = false;
74 $scope.checkoutArgs.sticky_date = newval;
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);
84 egCore.hatch.removeSessionItem('eg.circ.checkout.is_until_logout');
85 egCore.hatch.removeSessionItem('eg.circ.checkout.due_date');
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);
95 $scope.has_email_address = function() {
98 patronSvc.current.email() &&
99 patronSvc.current.email().match(/.*@.*/).length
103 $scope.may_email_receipt = function() {
105 $scope.has_email_address() &&
108 'circ.send_email_checkout_receipts'
113 $scope.using_hatch_printer = egCore.hatch.usePrinting();
115 egCore.hatch.getItem('circ.checkout.strict_barcode')
116 .then(function(sb){ $scope.strict_barcode = sb });
118 // avoid multiple, in-flight attempts on the same barcode
119 var pending_barcodes = {};
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)
131 egCirc.get_noncat_types().then(function(list) {
132 $scope.nonCatTypes = list;
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;
141 $scope.checkout = function(args) {
142 var params = angular.copy(args);
143 params.patron_id = patronSvc.current.id();
145 if (args.sticky_date) {
146 params.due_date = args.due_date.toISOString();
148 delete params.due_date;
150 delete params.sticky_date;
152 if (params.noncat_type == 'barcode') {
153 if (!args.copy_barcode) return;
155 args.copy_barcode = ''; // reset UI input
156 params.noncat_type = ''; // "barcode"
158 if (pending_barcodes[params.copy_barcode]) {
160 "Skipping checkout of redundant barcode "
161 + params.copy_barcode
166 pending_barcodes[params.copy_barcode] = true;
167 send_checkout(params);
170 egCirc.noncat_dialog(params).then(function() {
171 send_checkout(params)
175 $scope.focusMe = true; // return focus to barcode input
178 function send_checkout(params) {
180 params.noncat_type = params.noncat ? params.noncat_type : '';
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.
186 index : $scope.checkouts.length,
187 input_barcode : params.copy_barcode,
188 noncat_type : params.noncat_type
191 $scope.checkouts.unshift(row_item);
192 $scope.gridDataProvider.refresh();
194 egCore.hatch.setItem('circ.checkout.strict_barcode', $scope.strict_barcode);
195 var options = {check_barcode : $scope.strict_barcode};
197 egCirc.checkout(params, options).then(
199 // update stats locally so we don't have to fetch them w/
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++;
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) {
216 if (row_item.acp) { // unset for non-cat items.
217 row_item['copy_barcode'] = row_item.acp.barcode();
220 munge_checkout_resp(co_resp, row_item);
223 // Circ was rejected somewhere along the way.
224 // Remove the copy from the grid since there was no action.
225 // note: since checkouts are unshifted onto the array, the
226 // index value does not (generally) match the array position.
228 angular.forEach($scope.checkouts, function(co, idx) {
229 if (co.index == row_item.index) pos = idx;
231 $scope.checkouts.splice(pos, 1);
232 $scope.gridDataProvider.refresh();
235 ).finally(function() {
237 // regardless of the outcome of the circ, remove the
238 // barcode from the pending list.
239 if (params.copy_barcode)
240 delete pending_barcodes[params.copy_barcode];
242 $scope.focusMe = true; // return focus to barcode input
246 // add some checkout-specific additions for display
247 function munge_checkout_resp(co_resp, row_item) {
248 var params = co_resp.params;
250 row_item.title = egCore.env.cnct.map[params.noncat_type].name();
251 row_item.noncat_count = params.noncat_count;
252 row_item.circ = new egCore.idl.circ();
253 row_item.circ.due_date(co_resp.evt[0].payload.noncat_circ.duedate());
254 // Non-cat circs don't return the full list of circs.
255 // Refresh the list of non-cat circs from the server.
256 patronSvc.getUserNonCats(patronSvc.current.id());
257 row_item.copy_alert_count = 0;
259 row_item.copy_alert_count = 0;
262 { copy : co_resp.data.acp.id(), ack_time : null },
265 ).then(function(list) {
266 row_item.copy_alert_count = list.length;
271 $scope.addCopyAlerts = function(items) {
273 angular.forEach(items, function(item) {
274 if (item.acp) copy_ids.push(item.acp.id());
276 egCirc.add_copy_alerts(copy_ids).then(function() {
277 // update grid items?
281 $scope.manageCopyAlerts = function(items) {
283 angular.forEach(items, function(item) {
284 if (item.acp) copy_ids.push(item.acp.id());
286 egCirc.manage_copy_alerts(copy_ids).then(function() {
287 // update grid items?
291 $scope.gridCellHandlers = {};
292 $scope.gridCellHandlers.copyAlertsEdit = function(id) {
293 egCirc.manage_copy_alerts([id]).then(function() {
294 // update grid items?
298 $scope.print_receipt = function() {
299 var print_data = {circulations : []};
300 var cusr = patronSvc.current;
302 if ($scope.checkouts.length == 0) return $q.when();
304 angular.forEach($scope.checkouts, function(co) {
306 print_data.circulations.push({
307 circ : egCore.idl.toHash(co.circ),
308 copy : egCore.idl.toHash(co.acp),
309 call_number : egCore.idl.toHash(co.acn),
316 // This is repeated in patron.* so everyting is in one place but left here so existing templates don't break.
317 print_data.patron_money = patronSvc.patron_stats.fines;
318 print_data.patron = {
319 prefix : cusr.prefix(),
320 first_given_name : cusr.first_given_name(),
321 second_given_name : cusr.second_given_name(),
322 family_name : cusr.family_name(),
323 suffix : cusr.suffix(),
324 card : { barcode : cusr.card().barcode() },
325 money_summary : patronSvc.patron_stats.fines,
326 expire_date : cusr.expire_date(),
327 alias : cusr.alias(),
328 has_email : Boolean($scope.has_email_address()),
329 has_phone : Boolean(cusr.day_phone() || cusr.evening_phone() || cusr.other_phone())
332 return egCore.print.print({
334 template : 'checkout',
336 show_dialog : $scope.show_print_dialog
340 $scope.email_receipt = function() {
341 if ($scope.has_email_address() && $scope.checkouts.length) {
342 return egCore.net.request(
344 'open-ils.circ.checkout.batch_notify.session.atomic',
346 patronSvc.current.id(),
347 $scope.checkouts.map(function (c) { return c.circ.id() })
349 ngToast.create(egCore.strings.EMAILED_CHECKOUT_RECEIPT);
356 $scope.print_or_email_receipt = function() {
357 if ($scope.may_email_receipt()) return $scope.email_receipt();
358 $scope.print_receipt();
361 // set of functions to issue a receipt (if desired), then
363 $scope.done_auto_receipt = function() {
364 if ($scope.may_email_receipt()) {
365 $scope.email_receipt().then(function() {
366 $scope.done_redirect();
369 if (printOnComplete) {
371 $scope.print_receipt().then(function() {
372 $scope.done_redirect();
376 $scope.done_redirect();
380 $scope.done_print_receipt = function() {
381 $scope.print_receipt().then( function () {
382 $scope.done_redirect();
385 $scope.done_email_receipt = function() {
386 $scope.email_receipt().then( function () {
387 $scope.done_redirect();
390 $scope.done_no_receipt = function() {
391 $scope.done_redirect();
394 // Redirect the user to the barcode entry page to load a new patron.
395 $scope.done_redirect = function() {
396 $location.path('/circ/patron/bcsearch');