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.minDate = new Date();
23 $scope.outOfRange = false;
24 $scope.gridDataProvider = egGridDataProvider.instance({
25 get : function(offset, count) {
26 return this.arrayNotifier($scope.checkouts, offset, count);
30 $scope.disable_checkout = function() {
33 patronSvc.current.active() == 'f' ||
34 patronSvc.current.deleted() == 't' ||
35 patronSvc.current.card().active() == 'f' ||
36 patronSvc.fetchedWithInactiveCard() ||
37 $scope.outOfRange == true
41 function setting_value (user, setting) {
43 var list = user.settings().filter(function(s){
44 return s.name() == setting;
47 if (list.length) return list[0].value();
51 $scope.date_options = {
52 due_date : egCore.hatch.getSessionItem('eg.circ.checkout.due_date'),
53 has_sticky_date : egCore.hatch.getSessionItem('eg.circ.checkout.is_until_logout'),
54 is_until_logout : egCore.hatch.getSessionItem('eg.circ.checkout.is_until_logout')
57 if ($scope.date_options.is_until_logout) { // If until_logout is set there should also be a date set.
58 $scope.checkoutArgs.due_date = new Date($scope.date_options.due_date);
59 $scope.checkoutArgs.sticky_date = true;
62 $scope.toggle_opt = function(opt) {
63 if ($scope.date_options[opt]) {
64 $scope.date_options[opt] = false;
66 $scope.date_options[opt] = true;
70 // The interactions between these options are complicated enough that $watch'ing them all is the only safe way to keep things sane.
71 $scope.$watch('date_options.has_sticky_date', function(newval) {
72 if ( newval ) { // was false, is true
73 // $scope.date_options.due_date = checkoutArgs.due_date;
75 $scope.date_options.is_until_logout = false;
77 $scope.checkoutArgs.sticky_date = newval;
80 $scope.$watch('date_options.is_until_logout', function(newval) {
81 if ( newval ) { // was false, is true
82 $scope.date_options.has_sticky_date = true;
83 $scope.date_options.due_date = $scope.checkoutArgs.due_date;
84 egCore.hatch.setSessionItem('eg.circ.checkout.is_until_logout', true);
85 egCore.hatch.setSessionItem('eg.circ.checkout.due_date', $scope.checkoutArgs.due_date);
87 egCore.hatch.removeSessionItem('eg.circ.checkout.is_until_logout');
88 egCore.hatch.removeSessionItem('eg.circ.checkout.due_date');
92 $scope.$watch('checkoutArgs.due_date', function(newval) {
93 if ( $scope.date_options.is_until_logout && !isNaN(newval)) {
94 if (!$scope.outOfRange) {
95 egCore.hatch.setSessionItem('eg.circ.checkout.due_date', newval);
97 egCore.hatch.setSessionItem('eg.circ.checkout.due_date', $scope.checkoutArgs.due_date);
102 $scope.has_email_address = function() {
105 patronSvc.current.email() &&
106 patronSvc.current.email().match(/.*@.*/)
110 $scope.may_email_receipt = function() {
112 $scope.has_email_address() &&
115 'circ.send_email_checkout_receipts'
120 egCore.hatch.usePrinting().then(function(useHatch) {
121 $scope.using_hatch_printer = useHatch;
124 egCore.hatch.getItem('circ.checkout.strict_barcode')
125 .then(function(sb){ $scope.strict_barcode = sb });
127 // avoid multiple, in-flight attempts on the same barcode
128 var pending_barcodes = {};
130 var printOnComplete = true;
131 egCore.org.settings([
132 'circ.staff_client.do_not_auto_attempt_print'
133 ]).then(function(settings) {
134 printOnComplete = !Boolean(
135 angular.isArray(settings['circ.staff_client.do_not_auto_attempt_print']) &&
136 (settings['circ.staff_client.do_not_auto_attempt_print'].indexOf('Checkout') > -1)
140 egCirc.get_noncat_types().then(function(list) {
141 $scope.nonCatTypes = list;
144 $scope.selectedNcType = function() {
145 if (!egCore.env.cnct) return null; // too soon
146 var type = egCore.env.cnct.map[$scope.checkoutArgs.noncat_type];
147 return type ? type.name() : null;
150 $scope.checkout = function(args) {
151 var params = angular.copy(args);
152 params.patron_id = patronSvc.current.id();
154 if (args.sticky_date) {
155 params.due_date = args.due_date.toISOString();
157 delete params.due_date;
159 delete params.sticky_date;
161 if (params.noncat_type == 'barcode') {
162 if (!args.copy_barcode) return;
164 args.copy_barcode = ''; // reset UI input
165 params.noncat_type = ''; // "barcode"
167 if (pending_barcodes[params.copy_barcode]) {
169 "Skipping checkout of redundant barcode "
170 + params.copy_barcode
175 pending_barcodes[params.copy_barcode] = true;
176 send_checkout(params);
179 egCirc.noncat_dialog(params).then(function() {
180 send_checkout(params)
184 $scope.focusMe = true; // return focus to barcode input
187 function send_checkout(params) {
189 params.noncat_type = params.noncat ? params.noncat_type : '';
191 // populate the grid row before we send the request so that the
192 // order of actions is maintained and so the user gets an
193 // immediate reaction to their barcode input action.
195 index : $scope.checkouts.length,
196 input_barcode : params.copy_barcode,
197 noncat_type : params.noncat_type
200 $scope.checkouts.unshift(row_item);
201 $scope.gridDataProvider.prepend();
203 egCore.hatch.setItem('circ.checkout.strict_barcode', $scope.strict_barcode);
204 var options = {check_barcode : $scope.strict_barcode};
206 egCirc.checkout(params, options).then(
208 // update stats locally so we don't have to fetch them w/
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++;
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) {
225 if (row_item.acp) { // unset for non-cat items.
226 row_item['copy_barcode'] = row_item.acp.barcode();
229 munge_checkout_resp(co_resp, row_item);
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.
237 angular.forEach($scope.checkouts, function(co, idx) {
238 if (co.index == row_item.index) pos = idx;
240 $scope.checkouts.splice(pos, 1);
241 $scope.gridDataProvider.refresh();
244 ).finally(function() {
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];
251 $scope.focusMe = true; // return focus to barcode input
255 // add some checkout-specific additions for display
256 function munge_checkout_resp(co_resp, row_item) {
257 var params = co_resp.params;
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;
268 row_item.copy_alert_count = 0;
271 { copy : co_resp.data.acp.id(), ack_time : null },
274 ).then(function(list) {
275 row_item.copy_alert_count = list.length;
280 $scope.addCopyAlerts = function(items) {
282 angular.forEach(items, function(item) {
283 if (item.acp) copy_ids.push(item.acp.id());
285 egCirc.add_copy_alerts(copy_ids).then(function() {
286 // update grid items?
290 $scope.manageCopyAlerts = function(items) {
292 angular.forEach(items, function(item) {
293 if (item.acp) copy_ids.push(item.acp.id());
295 egCirc.manage_copy_alerts(copy_ids).then(function() {
296 // update grid items?
300 $scope.gridCellHandlers = {};
301 $scope.gridCellHandlers.copyAlertsEdit = function(id) {
302 egCirc.manage_copy_alerts([id]).then(function() {
303 // update grid items?
307 $scope.print_receipt = function() {
308 var print_data = {circulations : []};
309 var cusr = patronSvc.current;
311 if ($scope.checkouts.length == 0) return $q.when();
313 angular.forEach($scope.checkouts, function(co) {
315 print_data.circulations.push({
316 circ : egCore.idl.toHash(co.circ),
317 copy : egCore.idl.toHash(co.acp),
321 // Flesh selected fields of this circulation
322 print_data.circulations[0].copy.call_number =
323 egCore.idl.toHash(co.acn);
324 print_data.circulations[0].copy.owning_lib =
325 egCore.ils.toHash(co.aou);
329 // This is repeated in patron.* so everything is in one place but left here so existing templates don't break.
330 print_data.patron_money = patronSvc.patron_stats.fines;
331 print_data.patron = {
332 prefix : cusr.prefix(),
333 first_given_name : cusr.first_given_name(),
334 second_given_name : cusr.second_given_name(),
335 family_name : cusr.family_name(),
336 suffix : cusr.suffix(),
337 pref_prefix : cusr.pref_prefix(),
338 pref_first_given_name : cusr.pref_first_given_name(),
339 pref_secondg_given_name : cusr.second_given_name(),
340 pref_family_name : cusr.pref_family_name(),
341 suffix : cusr.suffix(),
342 card : { barcode : cusr.card().barcode() },
343 money_summary : patronSvc.patron_stats.fines,
344 expire_date : cusr.expire_date(),
345 alias : cusr.alias(),
346 has_email : Boolean($scope.has_email_address()),
347 has_phone : Boolean(cusr.day_phone() || cusr.evening_phone() || cusr.other_phone())
350 return egCore.print.print({
352 template : 'checkout',
354 show_dialog : $scope.show_print_dialog
358 $scope.email_receipt = function() {
359 if ($scope.has_email_address() && $scope.checkouts.length) {
360 return egCore.net.request(
362 'open-ils.circ.checkout.batch_notify.session.atomic',
364 patronSvc.current.id(),
365 $scope.checkouts.map(function (c) { return c.circ.id() })
367 ngToast.create(egCore.strings.EMAILED_CHECKOUT_RECEIPT);
374 $scope.print_or_email_receipt = function() {
375 if ($scope.may_email_receipt()) return $scope.email_receipt();
376 $scope.print_receipt();
379 // set of functions to issue a receipt (if desired), then
381 $scope.done_auto_receipt = function() {
382 if ($scope.may_email_receipt()) {
383 $scope.email_receipt().then(function() {
384 $scope.done_redirect();
387 if (printOnComplete) {
389 $scope.print_receipt().then(function() {
390 $scope.done_redirect();
394 $scope.done_redirect();
398 $scope.done_print_receipt = function() {
399 $scope.print_receipt().then( function () {
400 $scope.done_redirect();
403 $scope.done_email_receipt = function() {
404 $scope.email_receipt().then( function () {
405 $scope.done_redirect();
408 $scope.done_no_receipt = function() {
409 $scope.done_redirect();
412 // Redirect the user to the barcode entry page to load a new patron.
413 $scope.done_redirect = function() {
414 $location.path('/circ/patron/bcsearch');