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) {
14 $scope.initTab('checkout', $routeParams.id).finally(function(){
15 $scope.focusMe = true;
17 $scope.checkouts = patronSvc.checkouts;
18 $scope.checkoutArgs = {
19 noncat_type : 'barcode',
20 due_date : new Date(now)
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);
31 $scope.disable_checkout = function() {
34 patronSvc.current.active() == 'f' ||
35 patronSvc.current.deleted() == 't' ||
36 patronSvc.current.card().active() == 'f' ||
37 patronSvc.fetchedWithInactiveCard() ||
38 $scope.outOfRange == true
42 function setting_value (user, setting) {
44 var list = user.settings().filter(function(s){
45 return s.name() == setting;
48 if (list.length) return list[0].value();
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')
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;
63 $scope.toggle_opt = function(opt) {
64 if ($scope.date_options[opt]) {
65 $scope.date_options[opt] = false;
67 $scope.date_options[opt] = true;
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;
76 $scope.date_options.is_until_logout = false;
78 $scope.checkoutArgs.sticky_date = newval;
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);
88 egCore.hatch.removeSessionItem('eg.circ.checkout.is_until_logout');
89 egCore.hatch.removeSessionItem('eg.circ.checkout.due_date');
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);
98 egCore.hatch.setSessionItem('eg.circ.checkout.due_date', $scope.checkoutArgs.due_date);
103 $scope.has_email_address = function() {
106 patronSvc.current.email() &&
107 patronSvc.current.email().match(/.*@.*/)
111 $scope.may_email_receipt = function() {
113 $scope.has_email_address() &&
116 'circ.send_email_checkout_receipts'
121 egCore.hatch.usePrinting().then(function(useHatch) {
122 $scope.using_hatch_printer = useHatch;
125 egCore.hatch.getItem('circ.checkout.strict_barcode')
126 .then(function(sb){ $scope.strict_barcode = sb });
128 // avoid multiple, in-flight attempts on the same barcode
129 var pending_barcodes = {};
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)
141 egCirc.get_noncat_types().then(function(list) {
142 $scope.nonCatTypes = list;
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;
151 $scope.checkout = function(args) {
152 var params = angular.copy(args);
153 params.patron_id = patronSvc.current.id();
155 if (args.sticky_date) {
156 params.due_date = args.due_date.toISOString();
158 delete params.due_date;
160 delete params.sticky_date;
162 if (params.noncat_type == 'barcode') {
163 if (!args.copy_barcode) return;
165 args.copy_barcode = ''; // reset UI input
166 params.noncat_type = ''; // "barcode"
168 if (pending_barcodes[params.copy_barcode]) {
170 "Skipping checkout of redundant barcode "
171 + params.copy_barcode
176 pending_barcodes[params.copy_barcode] = true;
177 send_checkout(params);
180 egCirc.noncat_dialog(params).then(function() {
181 send_checkout(params)
185 $scope.focusMe = true; // return focus to barcode input
188 function send_checkout(params) {
190 params.noncat_type = params.noncat ? params.noncat_type : '';
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.
196 index : $scope.checkouts.length,
197 input_barcode : params.copy_barcode,
198 noncat_type : params.noncat_type
201 $scope.checkouts.unshift(row_item);
202 $scope.gridDataProvider.prepend();
204 egCore.hatch.setItem('circ.checkout.strict_barcode', $scope.strict_barcode);
205 var options = {check_barcode : $scope.strict_barcode};
207 egCirc.checkout(params, options).then(
209 // update stats locally so we don't have to fetch them w/
212 // Avoid updating checkout counts when a checkout turns
213 // into a renewal via auto_renew.
214 if (!co_resp.auto_renew && !params.noncat && !options.sameCopyCheckout) {
215 patronSvc.patron_stats.checkouts.out++;
216 patronSvc.patron_stats.checkouts.total_out++;
219 // copy the response event into the original grid row item
220 // note: angular.copy clobbers the destination
221 row_item.evt = co_resp.evt;
222 angular.forEach(co_resp.data, function(val, key) {
226 if (row_item.acp) { // unset for non-cat items.
227 row_item['copy_barcode'] = row_item.acp.barcode();
230 munge_checkout_resp(co_resp, row_item);
233 // Circ was rejected somewhere along the way.
234 // Remove the copy from the grid since there was no action.
235 // note: since checkouts are unshifted onto the array, the
236 // index value does not (generally) match the array position.
238 angular.forEach($scope.checkouts, function(co, idx) {
239 if (co.index == row_item.index) pos = idx;
241 $scope.checkouts.splice(pos, 1);
242 $scope.gridDataProvider.refresh();
245 ).finally(function() {
247 // regardless of the outcome of the circ, remove the
248 // barcode from the pending list.
249 if (params.copy_barcode)
250 delete pending_barcodes[params.copy_barcode];
252 $scope.focusMe = true; // return focus to barcode input
256 // add some checkout-specific additions for display
257 function munge_checkout_resp(co_resp, row_item) {
258 var params = co_resp.params;
260 row_item.title = egCore.env.cnct.map[params.noncat_type].name();
261 row_item.noncat_count = params.noncat_count;
262 row_item.circ = new egCore.idl.circ();
263 row_item.circ.due_date(co_resp.evt[0].payload.noncat_circ.duedate());
264 // Non-cat circs don't return the full list of circs.
265 // Refresh the list of non-cat circs from the server.
266 patronSvc.getUserNonCats(patronSvc.current.id());
267 row_item.copy_alert_count = 0;
269 row_item.copy_alert_count = 0;
272 { copy : co_resp.data.acp.id(), ack_time : null },
275 ).then(function(list) {
276 row_item.copy_alert_count = list.length;
281 $scope.addCopyAlerts = function(items) {
283 angular.forEach(items, function(item) {
284 if (item.acp) copy_ids.push(item.acp.id());
286 egCirc.add_copy_alerts(copy_ids).then(function() {
287 // update grid items?
291 $scope.manageCopyAlerts = function(items) {
293 angular.forEach(items, function(item) {
294 if (item.acp) copy_ids.push(item.acp.id());
296 egCirc.manage_copy_alerts(copy_ids).then(function() {
297 // update grid items?
301 $scope.gridCellHandlers = {};
302 $scope.gridCellHandlers.copyAlertsEdit = function(id) {
303 egCirc.manage_copy_alerts([id]).then(function() {
304 // update grid items?
308 $scope.print_receipt = function() {
309 var print_data = {circulations : []};
310 var cusr = patronSvc.current;
312 if ($scope.checkouts.length == 0) return $q.when();
314 angular.forEach($scope.checkouts, function(co) {
316 print_data.circulations.push({
317 circ : egCore.idl.toHash(co.circ),
318 copy : egCore.idl.toHash(co.acp),
319 call_number : egCore.idl.toHash(co.acn), // Wrong?
320 owning_lib : egCore.idl.toHash(co.aou), // Wrong?
327 // This is repeated in patron.* so everything is in one place but left here so existing templates don't break.
328 print_data.patron_money = patronSvc.patron_stats.fines;
329 print_data.patron = {
330 prefix : cusr.prefix(),
331 first_given_name : cusr.first_given_name(),
332 second_given_name : cusr.second_given_name(),
333 family_name : cusr.family_name(),
334 suffix : cusr.suffix(),
335 pref_prefix : cusr.pref_prefix(),
336 pref_first_given_name : cusr.pref_first_given_name(),
337 pref_secondg_given_name : cusr.second_given_name(),
338 pref_family_name : cusr.pref_family_name(),
339 suffix : cusr.suffix(),
340 card : { barcode : cusr.card().barcode() },
341 money_summary : patronSvc.patron_stats.fines,
342 expire_date : cusr.expire_date(),
343 alias : cusr.alias(),
344 has_email : Boolean($scope.has_email_address()),
345 has_phone : Boolean(cusr.day_phone() || cusr.evening_phone() || cusr.other_phone())
348 return egCore.print.print({
350 template : 'checkout',
352 show_dialog : $scope.show_print_dialog
356 $scope.email_receipt = function() {
357 if ($scope.has_email_address() && $scope.checkouts.length) {
358 return egCore.net.request(
360 'open-ils.circ.checkout.batch_notify.session.atomic',
362 patronSvc.current.id(),
363 $scope.checkouts.map(function (c) { return c.circ.id() })
365 ngToast.create(egCore.strings.EMAILED_CHECKOUT_RECEIPT);
372 $scope.print_or_email_receipt = function() {
373 if ($scope.may_email_receipt()) return $scope.email_receipt();
374 $scope.print_receipt();
377 // set of functions to issue a receipt (if desired), then
379 $scope.done_auto_receipt = function() {
380 if ($scope.may_email_receipt()) {
381 $scope.email_receipt().then(function() {
382 $scope.done_redirect();
385 if (printOnComplete) {
387 $scope.print_receipt().then(function() {
388 $scope.done_redirect();
392 $scope.done_redirect();
396 $scope.done_print_receipt = function() {
397 $scope.print_receipt().then( function () {
398 $scope.done_redirect();
401 $scope.done_email_receipt = function() {
402 $scope.email_receipt().then( function () {
403 $scope.done_redirect();
406 $scope.done_no_receipt = function() {
407 $scope.done_redirect();
410 // Redirect the user to the barcode entry page to load a new patron.
411 $scope.done_redirect = function() {
412 $location.path('/circ/patron/bcsearch');