2 * Checkin, checkout, and renew
5 angular.module('egCoreMod')
8 ['$uibModal','$q','egCore','egAlertDialog','egConfirmDialog',
10 function($uibModal , $q , egCore , egAlertDialog , egConfirmDialog,
14 // auto-override these events after the first override
15 auto_override_checkout_events : {},
16 require_initials : false,
18 hold_shelf_slip : false,
19 hold_transit_slip : false,
24 egCore.startup.go().finally(function() {
26 'ui.staff.require_initials.patron_standing_penalty',
27 'ui.admin.work_log.max_entries',
28 'ui.admin.patron_log.max_entries',
29 'circ.staff_client.do_not_auto_attempt_print'
30 ]).then(function(set) {
31 service.require_initials = Boolean(set['ui.staff.require_initials.patron_standing_penalty']);
32 if (angular.isArray(set['circ.staff_client.do_not_auto_attempt_print'])) {
33 if (set['circ.staff_client.do_not_auto_attempt_print'].indexOf('Hold Slip') > 1)
34 service.never_auto_print['hold_shelf_slip'] = true;
35 if (set['circ.staff_client.do_not_auto_attempt_print'].indexOf('Hold/Transit Slip') > 1)
36 service.never_auto_print['hold_transit_slip'] = true;
37 if (set['circ.staff_client.do_not_auto_attempt_print'].indexOf('Transit Slip') > 1)
38 service.never_auto_print['transit_slip'] = true;
43 service.reset = function() {
44 service.auto_override_checkout_events = {};
47 // these events can be overridden by staff during checkout
48 service.checkout_overridable_events = [
49 'PATRON_EXCEEDS_OVERDUE_COUNT',
50 'PATRON_EXCEEDS_CHECKOUT_COUNT',
51 'PATRON_EXCEEDS_FINES',
53 'CIRC_EXCEEDS_COPY_RANGE',
54 'ITEM_DEPOSIT_REQUIRED',
55 'ITEM_RENTAL_FEE_REQUIRED',
56 'PATRON_EXCEEDS_LOST_COUNT',
57 'COPY_CIRC_NOT_ALLOWED',
64 // after the first override of any of these events,
65 // auto-override them in subsequent calls.
66 service.checkout_auto_override_after_first = [
67 'PATRON_EXCEEDS_OVERDUE_COUNT',
69 'PATRON_EXCEEDS_LOST_COUNT',
70 'PATRON_EXCEEDS_CHECKOUT_COUNT',
71 'PATRON_EXCEEDS_FINES'
75 // overridable during renewal
76 service.renew_overridable_events = [
77 'PATRON_EXCEEDS_OVERDUE_COUNT',
78 'PATRON_EXCEEDS_LOST_COUNT',
79 'PATRON_EXCEEDS_CHECKOUT_COUNT',
80 'PATRON_EXCEEDS_FINES',
81 'CIRC_EXCEEDS_COPY_RANGE',
82 'ITEM_DEPOSIT_REQUIRED',
83 'ITEM_RENTAL_FEE_REQUIRED',
85 'COPY_CIRC_NOT_ALLOWED',
88 'COPY_NEEDED_FOR_HOLD',
89 'MAX_RENEWALS_REACHED',
90 'CIRC_CLAIMS_RETURNED'
93 // these checkin events do not produce alerts when
94 // options.suppress_alerts is in effect.
95 service.checkin_suppress_overrides = [
99 'PATRON_ACCOUNT_EXPIRED',
101 'CIRC_CLAIMS_RETURNED',
102 'COPY_ALERT_MESSAGE',
104 'COPY_STATUS_LONG_OVERDUE',
105 'COPY_STATUS_MISSING',
106 'PATRON_EXCEEDS_FINES'
109 // these events can be overridden by staff during checkin
110 service.checkin_overridable_events =
111 service.checkin_suppress_overrides.concat([
112 'TRANSIT_CHECKIN_INTERVAL_BLOCK'
115 // Performs a checkout.
116 // Returns a promise resolved with the original params and options
117 // and the final checkout event (e.g. in the case of override).
118 // Rejected if the checkout cannot be completed.
120 // params : passed directly as arguments to the server API
121 // options : non-parameter controls. e.g. "override", "check_barcode"
122 service.checkout = function(params, options) {
123 if (!options) options = {};
125 console.debug('egCirc.checkout() : '
126 + js2JSON(params) + ' : ' + js2JSON(options));
128 var promise = options.check_barcode ?
129 service.test_barcode(params.copy_barcode) : $q.when();
131 // avoid re-check on override, etc.
132 delete options.check_barcode;
134 return promise.then(function() {
136 var method = 'open-ils.circ.checkout.full';
137 if (options.override) method += '.override';
139 return egCore.net.request(
140 'open-ils.circ', method, egCore.auth.token(), params
142 ).then(function(evt) {
144 if (!angular.isArray(evt)) evt = [evt];
146 return service.flesh_response_data('checkout', evt, params, options)
148 return service.handle_checkout_resp(evt, params, options);
150 .then(function(final_resp) {
151 return service.munge_resp_data(final_resp,'checkout',method)
157 // Performs a renewal.
158 // Returns a promise resolved with the original params and options
159 // and the final checkout event (e.g. in the case of override)
160 // Rejected if the renewal cannot be completed.
161 service.renew = function(params, options) {
162 if (!options) options = {};
164 console.debug('egCirc.renew() : '
165 + js2JSON(params) + ' : ' + js2JSON(options));
167 var promise = options.check_barcode ?
168 service.test_barcode(params.copy_barcode) : $q.when();
170 // avoid re-check on override, etc.
171 delete options.check_barcode;
173 return promise.then(function() {
175 var method = 'open-ils.circ.renew';
176 if (options.override) method += '.override';
178 return egCore.net.request(
179 'open-ils.circ', method, egCore.auth.token(), params
181 ).then(function(evt) {
183 if (!angular.isArray(evt)) evt = [evt];
185 return service.flesh_response_data(
186 'renew', evt, params, options)
188 return service.handle_renew_resp(evt, params, options);
190 .then(function(final_resp) {
191 return service.munge_resp_data(final_resp,'renew',method)
197 // Performs a checkin
198 // Returns a promise resolved with the original params and options,
199 // plus the final checkin event (e.g. in the case of override).
200 // Rejected if the checkin cannot be completed.
201 service.checkin = function(params, options) {
202 if (!options) options = {};
204 console.debug('egCirc.checkin() : '
205 + js2JSON(params) + ' : ' + js2JSON(options));
207 var promise = options.check_barcode ?
208 service.test_barcode(params.copy_barcode) : $q.when();
210 // avoid re-check on override, etc.
211 delete options.check_barcode;
213 return promise.then(function() {
215 var method = 'open-ils.circ.checkin';
216 if (options.override) method += '.override';
218 return egCore.net.request(
219 'open-ils.circ', method, egCore.auth.token(), params
221 ).then(function(evt) {
223 if (!angular.isArray(evt)) evt = [evt];
224 return service.flesh_response_data(
225 'checkin', evt, params, options)
227 return service.handle_checkin_resp(evt, params, options);
229 .then(function(final_resp) {
230 return service.munge_resp_data(final_resp,'checkin',method)
236 // provide consistent formatting of the final response data
237 service.munge_resp_data = function(final_resp,worklog_action,worklog_method) {
238 var data = final_resp.data = {};
240 if (!final_resp.evt[0]) {
241 egCore.audio.play('error.unknown.no_event');
245 var payload = final_resp.evt[0].payload;
247 egCore.audio.play('error.unknown.no_payload');
251 data.circ = payload.circ;
252 data.parent_circ = payload.parent_circ;
253 data.hold = payload.hold;
254 data.record = payload.record;
255 data.acp = payload.copy;
256 data.acn = payload.volume ? payload.volume : payload.copy ? payload.copy.call_number() : null;
257 data.au = payload.patron;
258 data.transit = payload.transit;
259 data.status = payload.status;
260 data.message = payload.message;
261 data.title = final_resp.evt[0].title;
262 data.author = final_resp.evt[0].author;
263 data.isbn = final_resp.evt[0].isbn;
264 data.route_to = final_resp.evt[0].route_to;
266 // for checkin, the mbts lives on the main circ
267 if (payload.circ && payload.circ.billable_transaction())
268 data.mbts = payload.circ.billable_transaction().summary();
270 // on renewals, the mbts lives on the parent circ
271 if (payload.parent_circ && payload.parent_circ.billable_transaction())
272 data.mbts = payload.parent_circ.billable_transaction().summary();
274 if (!data.route_to) {
276 data.route_to = data.transit.dest().shortname();
277 } else if (data.acp) {
278 data.route_to = data.acp.location().name();
283 worklog_action == 'checkout'
284 ? egCore.strings.EG_WORK_LOG_CHECKOUT
285 : (worklog_action == 'renew'
286 ? egCore.strings.EG_WORK_LOG_RENEW
287 : egCore.strings.EG_WORK_LOG_CHECKIN // worklog_action == 'checkin'
289 'action' : worklog_action,
290 'method' : worklog_method,
291 'response' : final_resp
298 service.handle_overridable_checkout_event = function(evt, params, options) {
300 if (options.override) {
301 // override attempt already made and failed.
302 // NOTE: I don't think we'll ever get here, since the
303 // override attempt should produce a perm failure...
304 angular.forEach(evt, function(e){ console.debug('override failed: ' + e.textcode); });
309 if (evt.filter(function(e){return !service.auto_override_checkout_events[e.textcode];}).length == 0) {
310 // user has already opted to override these type
311 // of events. Re-run the checkout w/ override.
312 options.override = true;
313 return service.checkout(params, options);
316 // Ask the user if they would like to override this event.
317 // Some events offer a stock override dialog, while others
318 // require additional context.
320 switch(evt[0].textcode) {
321 case 'COPY_NOT_AVAILABLE':
322 return service.copy_not_avail_dialog(evt[0], params, options);
323 case 'COPY_ALERT_MESSAGE':
324 return service.copy_alert_dialog(evt[0], params, options, 'checkout');
326 return service.override_dialog(evt, params, options, 'checkout');
330 service.handle_overridable_renew_event = function(evt, params, options) {
332 if (options.override) {
333 // override attempt already made and failed.
334 // NOTE: I don't think we'll ever get here, since the
335 // override attempt should produce a perm failure...
336 angular.forEach(evt, function(e){ console.debug('override failed: ' + e.textcode); });
341 // renewal auto-overrides are the same as checkout
342 if (evt.filter(function(e){return !service.auto_override_checkout_events[e.textcode];}).length == 0) {
343 // user has already opted to override these type
344 // of events. Re-run the renew w/ override.
345 options.override = true;
346 return service.renew(params, options);
349 // Ask the user if they would like to override this event.
350 // Some events offer a stock override dialog, while others
351 // require additional context.
353 switch(evt[0].textcode) {
354 case 'COPY_ALERT_MESSAGE':
355 return service.copy_alert_dialog(evt[0], params, options, 'renew');
357 return service.override_dialog(evt, params, options, 'renew');
362 service.handle_overridable_checkin_event = function(evt, params, options) {
364 if (options.override) {
365 // override attempt already made and failed.
366 // NOTE: I don't think we'll ever get here, since the
367 // override attempt should produce a perm failure...
368 angular.forEach(evt, function(e){ console.debug('override failed: ' + e.textcode); });
373 if (options.suppress_checkin_popups
374 && evt.filter(function(e){return service.checkin_suppress_overrides.indexOf(e.textcode) == -1;}).length == 0) {
375 // Events are suppressed. Re-run the checkin w/ override.
376 options.override = true;
377 return service.checkin(params, options);
380 // Ask the user if they would like to override this event.
381 // Some events offer a stock override dialog, while others
382 // require additional context.
384 switch(evt[0].textcode) {
385 case 'COPY_ALERT_MESSAGE':
386 return service.copy_alert_dialog(evt[0], params, options, 'checkin');
388 return service.override_dialog(evt, params, options, 'checkin');
393 service.handle_renew_resp = function(evt, params, options) {
395 var final_resp = {evt : evt, params : params, options : options};
397 // track the barcode regardless of whether it refers to a copy
398 angular.forEach(evt, function(e){ e.copy_barcode = params.copy_barcode; });
400 // Overridable Events
401 if (evt.filter(function(e){return service.renew_overridable_events.indexOf(e.textcode) > -1;}).length > 0)
402 return service.handle_overridable_renew_event(evt, params, options);
405 switch (evt[0].textcode) {
407 egCore.audio.play('success.renew');
408 return $q.when(final_resp);
410 case 'COPY_IN_TRANSIT':
411 case 'PATRON_CARD_INACTIVE':
412 case 'PATRON_INACTIVE':
413 case 'PATRON_ACCOUNT_EXPIRED':
414 case 'CIRC_CLAIMS_RETURNED':
415 egCore.audio.play('warning.renew');
416 return service.exit_alert(
417 egCore.strings[evt[0].textcode],
418 {barcode : params.copy_barcode}
422 egCore.audio.play('warning.renew.permission');
423 return service.exit_alert(
424 egCore.strings[evt[0].textcode],
425 {permission : evt[0].ilsperm}
429 egCore.audio.play('warning.renew.unknown');
430 return service.exit_alert(
431 egCore.strings.CHECKOUT_FAILED_GENERIC, {
432 barcode : params.copy_barcode,
433 textcode : evt[0].textcode,
441 service.handle_checkout_resp = function(evt, params, options) {
443 var final_resp = {evt : evt, params : params, options : options};
445 // track the barcode regardless of whether it refers to a copy
446 angular.forEach(evt, function(e){ e.copy_barcode = params.copy_barcode; });
448 // Overridable Events
449 if (evt.filter(function(e){return service.checkout_overridable_events.indexOf(e.textcode) > -1;}).length > 0)
450 return service.handle_overridable_checkout_event(evt, params, options);
453 switch (evt[0].textcode) {
455 egCore.audio.play('success.checkout');
456 return $q.when(final_resp);
458 case 'ITEM_NOT_CATALOGED':
459 egCore.audio.play('error.checkout.no_cataloged');
460 return service.precat_dialog(params, options);
462 case 'OPEN_CIRCULATION_EXISTS':
463 if(evt[0].payload.auto_renew == 1){
464 return service.renew(params, options);
466 egCore.audio.play('error.checkout.open_circ');
467 return service.circ_exists_dialog(evt, params, options);
469 case 'COPY_IN_TRANSIT':
470 egCore.audio.play('warning.checkout.in_transit');
471 return service.copy_in_transit_dialog(evt, params, options);
473 case 'PATRON_CARD_INACTIVE':
474 case 'PATRON_INACTIVE':
475 case 'PATRON_ACCOUNT_EXPIRED':
476 case 'CIRC_CLAIMS_RETURNED':
477 egCore.audio.play('warning.checkout');
478 return service.exit_alert(
479 egCore.strings[evt[0].textcode],
480 {barcode : params.copy_barcode}
484 egCore.audio.play('warning.checkout.permission');
485 return service.exit_alert(
486 egCore.strings[evt[0].textcode],
487 {permission : evt[0].ilsperm}
491 egCore.audio.play('error.checkout.unknown');
492 return service.exit_alert(
493 egCore.strings.CHECKOUT_FAILED_GENERIC, {
494 barcode : params.copy_barcode,
495 textcode : evt[0].textcode,
502 // returns a promise resolved with the list of circ mods
503 service.get_circ_mods = function() {
505 return $q.when(egCore.env.ccm.list);
507 return egCore.pcrud.retrieveAll('ccm', null, {atomic : true})
508 .then(function(list) {
509 egCore.env.absorbList(list, 'ccm');
514 // returns a promise resolved with the list of noncat types
515 service.get_noncat_types = function() {
517 return $q.when(egCore.env.cnct.list);
519 return egCore.pcrud.search('cnct',
521 egCore.org.fullPath(egCore.auth.user().ws_ou(), true)},
522 null, {atomic : true}
523 ).then(function(list) {
524 egCore.env.absorbList(list, 'cnct');
529 service.get_staff_penalty_types = function() {
531 return $q.when(egCore.env.csp.list);
532 return egCore.pcrud.search(
533 // id <= 100 are reserved for system use
534 'csp', {id : {'>': 100}}, {}, {atomic : true})
535 .then(function(penalties) {
536 return egCore.env.absorbList(penalties, 'csp').list;
540 // ideally all of these data should be returned with the response,
541 // but until then, grab what we need.
542 service.flesh_response_data = function(action, evt, params, options) {
545 if (!evt[0] || !(payload = evt[0].payload)) return $q.when();
547 promises.push(service.flesh_copy_location(payload.copy));
550 service.flesh_copy_status(payload.copy)
553 // copy is in transit, but no transit was delivered
554 // in the payload. Do this here instead of below to
555 // ensure consistent copy status fleshiness
556 if (!payload.transit && payload.copy.status().id() == 6) { // in-transit
557 return service.find_copy_transit(evt, params, options)
558 .then(function(trans) {
560 trans.source(egCore.org.get(trans.source()));
561 trans.dest(egCore.org.get(trans.dest()));
562 payload.transit = trans;
570 // local flesh transit
571 if (transit = payload.transit) {
572 transit.source(egCore.org.get(transit.source()));
573 transit.dest(egCore.org.get(transit.dest()));
576 // TODO: renewal responses should include the patron
577 if (!payload.patron && payload.circ) {
579 egCore.pcrud.retrieve('au', payload.circ.usr())
580 .then(function(user) {payload.patron = user})
584 // extract precat values
585 angular.forEach(evt, function(e){ e.title = payload.record ? payload.record.title() :
586 (payload.copy ? payload.copy.dummy_title() : null);});
588 angular.forEach(evt, function(e){ e.author = payload.record ? payload.record.author() :
589 (payload.copy ? payload.copy.dummy_author() : null);});
591 angular.forEach(evt, function(e){ e.isbn = payload.record ? payload.record.isbn() :
592 (payload.copy ? payload.copy.dummy_isbn() : null);});
594 return $q.all(promises);
597 // fetches the full list of copy statuses
598 service.flesh_copy_status = function(copy) {
599 if (!copy) return $q.when();
601 return $q.when(copy.status(egCore.env.ccs.map[copy.status()]));
602 return egCore.pcrud.retrieveAll('ccs', {}, {atomic : true}).then(
604 egCore.env.absorbList(list, 'ccs');
605 copy.status(egCore.env.ccs.map[copy.status()]);
610 // there may be *many* copy locations and we may be handling items
611 // for other locations. Fetch copy locations as-needed and cache.
612 service.flesh_copy_location = function(copy) {
613 if (!copy) return $q.when();
614 if (angular.isObject(copy.location())) return $q.when(copy);
615 if (egCore.env.acpl) {
616 if (egCore.env.acpl.map[copy.location()]) {
617 copy.location(egCore.env.acpl.map[copy.location()]);
618 return $q.when(copy);
621 return egCore.pcrud.retrieve('acpl', copy.location())
622 .then(function(loc) {
623 egCore.env.absorbList([loc], 'acpl'); // append to cache
630 // fetch org unit addresses as needed.
631 service.get_org_addr = function(org_id, addr_type) {
632 var org = egCore.org.get(org_id);
633 var addr_id = org[addr_type]();
635 if (egCore.env.aoa && egCore.env.aoa.map[addr_id])
636 return $q.when(egCore.env.aoa.map[addr_id]);
638 return egCore.pcrud.retrieve('aoa', addr_id).then(function(addr) {
639 egCore.env.absorbList([addr], 'aoa');
640 return egCore.env.aoa.map[addr_id];
644 service.exit_alert = function(msg, scope) {
645 return egAlertDialog.open(msg, scope).result.then(
646 function() {return $q.reject()});
649 // opens a dialog asking the user if they would like to override
650 // the returned event.
651 service.override_dialog = function(evt, params, options, action) {
652 if (!angular.isArray(evt)) evt = [evt];
653 return $uibModal.open({
654 templateUrl: './circ/share/t_event_override_dialog',
656 ['$scope', '$uibModalInstance',
657 function($scope, $uibModalInstance) {
659 $scope.auto_override =
660 evt.filter(function(e){
661 return service.checkout_auto_override_after_first.indexOf(evt.textcode) > -1;
663 $scope.copy_barcode = params.copy_barcode; // may be null
664 $scope.ok = function() { $uibModalInstance.close() }
665 $scope.cancel = function ($event) {
666 $uibModalInstance.dismiss();
667 $event.preventDefault();
672 options.override = true;
674 if (action == 'checkin') {
675 return service.checkin(params, options);
678 // checkout/renew support override-after-first
679 angular.forEach(evt, function(e){
680 if (service.checkout_auto_override_after_first.indexOf(e.textcode) > -1)
681 service.auto_override_checkout_events[e.textcode] = true;
684 return service[action](params, options);
689 service.copy_not_avail_dialog = function(evt, params, options) {
690 if (angular.isArray(evt)) evt = evt[0];
691 return $uibModal.open({
692 templateUrl: './circ/share/t_copy_not_avail_dialog',
694 ['$scope','$uibModalInstance','copyStatus',
695 function($scope , $uibModalInstance , copyStatus) {
696 $scope.copyStatus = copyStatus;
697 $scope.ok = function() {$uibModalInstance.close()}
698 $scope.cancel = function() {$uibModalInstance.dismiss()}
701 copyStatus : function() {
702 return egCore.pcrud.retrieve(
703 'ccs', evt.payload.status());
708 options.override = true;
709 return service.checkout(params, options);
714 // Opens a dialog allowing the user to fill in the desired non-cat count.
715 // Unlike other dialogs, which kickoff circ actions internally
716 // as a result of events, this dialog does not kick off any circ
717 // actions. It just collects the count and and resolves the promise.
719 // This assumes the caller has already handled the noncat-type
720 // selection and just needs to collect the count info.
721 service.noncat_dialog = function(params, options) {
722 var noncatMax = 99; // hard-coded max
724 // the caller should presumably have fetched the noncat_types via
725 // our API already, but fetch them again (from cache) to be safe.
726 return service.get_noncat_types().then(function() {
728 params.noncat = true;
729 var type = egCore.env.cnct.map[params.noncat_type];
731 return $uibModal.open({
732 templateUrl: './circ/share/t_noncat_dialog',
734 ['$scope', '$uibModalInstance',
735 function($scope, $uibModalInstance) {
736 $scope.focusMe = true;
739 $scope.noncatMax = noncatMax;
740 $scope.ok = function(count) { $uibModalInstance.close(count) }
741 $scope.cancel = function ($event) {
742 $uibModalInstance.dismiss()
743 $event.preventDefault();
748 if (count && count > 0 && count <= noncatMax) {
749 // NOTE: in Chrome, form validation ensure a valid number
750 params.noncat_count = count;
751 return $q.when(params);
760 // Opens a dialog allowing the user to fill in pre-cat copy info.
761 service.precat_dialog = function(params, options) {
763 return $uibModal.open({
764 templateUrl: './circ/share/t_precat_dialog',
766 ['$scope', '$uibModalInstance', 'circMods',
767 function($scope, $uibModalInstance, circMods) {
768 $scope.focusMe = true;
769 $scope.precatArgs = {
770 copy_barcode : params.copy_barcode
772 $scope.circModifiers = circMods;
773 $scope.ok = function(args) { $uibModalInstance.close(args) }
774 $scope.cancel = function () { $uibModalInstance.dismiss() }
777 circMods : function() {
778 return service.get_circ_mods();
783 if (!args || !args.dummy_title) return $q.reject();
784 if(args.circ_modifier == "") args.circ_modifier = null;
785 angular.forEach(args, function(val, key) {params[key] = val});
786 params.precat = true;
787 return service.checkout(params, options);
792 // find the open transit for the given copy barcode; flesh the org
794 service.find_copy_transit = function(evt, params, options) {
795 if (angular.isArray(evt)) evt = evt[0];
797 if (evt && evt.payload && evt.payload.transit)
798 return $q.when(evt.payload.transit);
800 return egCore.pcrud.search('atc',
801 { dest_recv_time : null},
803 flesh_fields : {atc : ['target_copy']},
807 barcode : params.copy_barcode,
813 order_by : {atc : 'source_send_time desc'},
815 ).then(function(transit) {
816 transit.source(egCore.org.get(transit.source()));
817 transit.dest(egCore.org.get(transit.dest()));
822 service.copy_in_transit_dialog = function(evt, params, options) {
823 if (angular.isArray(evt)) evt = evt[0];
824 return $uibModal.open({
825 templateUrl: './circ/share/t_copy_in_transit_dialog',
827 ['$scope','$uibModalInstance','transit',
828 function($scope , $uibModalInstance , transit) {
829 $scope.transit = transit;
830 $scope.ok = function() { $uibModalInstance.close(transit) }
831 $scope.cancel = function() { $uibModalInstance.dismiss() }
834 // fetch the conflicting open transit w/ fleshed copy
835 transit : function() {
836 return service.find_copy_transit(evt, params, options);
841 // user chose to abort the transit then checkout
842 return service.abort_transit(transit.id())
844 return service.checkout(params, options);
850 service.abort_transit = function(transit_id) {
851 return egCore.net.request(
853 'open-ils.circ.transit.abort',
854 egCore.auth.token(), {transitid : transit_id}
855 ).then(function(resp) {
856 if (evt = egCore.evt.parse(resp)) {
864 service.last_copy_circ = function(copy_id) {
865 return egCore.pcrud.search('circ',
866 {target_copy : copy_id},
867 {order_by : {circ : 'xact_start desc' }, limit : 1}
871 service.circ_exists_dialog = function(evt, params, options) {
872 if (angular.isArray(evt)) evt = evt[0];
874 if (!evt.payload.old_circ) {
875 return egCore.pcrud.search('circ',
876 {target_copy : evt.payload.copy.id(), checkin_time : null},
877 {limit : 1} // should only ever be 1
878 ).then(function(old_circ) {
879 evt.payload.old_circ = old_circ;
880 return service.circ_exists_dialog_impl(evt, params, options);
883 return service.circ_exists_dialog_impl( evt, params, options );
887 service.circ_exists_dialog_impl = function (evt, params, options) {
889 var openCirc = evt.payload.old_circ;
890 var sameUser = openCirc.usr() == params.patron_id;
892 return $uibModal.open({
893 templateUrl: './circ/share/t_circ_exists_dialog',
895 ['$scope','$uibModalInstance',
896 function($scope , $uibModalInstance) {
897 $scope.args = {forgive_fines : false};
898 $scope.circDate = openCirc.xact_start();
899 $scope.sameUser = sameUser;
900 $scope.ok = function() { $uibModalInstance.close($scope.args) }
901 $scope.cancel = function($event) {
902 $uibModalInstance.dismiss();
903 $event.preventDefault(); // form, avoid calling ok();
909 params.void_overdues = args.forgive_fines;
910 options.override = true;
911 return service.renew(params, options);
914 return service.checkin({
915 barcode : params.copy_barcode,
918 void_overdues : args.forgive_fines
919 }).then(function(checkin_resp) {
920 if (checkin_resp.evt[0].textcode == 'SUCCESS') {
921 return service.checkout(params, options);
923 alert(egCore.evt.parse(checkin_resp.evt[0]));
931 service.batch_backdate = function(circ_ids, backdate) {
932 return egCore.net.request(
934 'open-ils.circ.post_checkin_backdate.batch',
935 egCore.auth.token(), circ_ids, backdate);
938 service.backdate_dialog = function(circ_ids) {
939 return $uibModal.open({
940 templateUrl: './circ/share/t_backdate_dialog',
942 ['$scope','$uibModalInstance',
943 function($scope , $uibModalInstance) {
945 var today = new Date();
947 num_circs : circ_ids.length,
952 $scope.$watch('dialog.backdate', function(newval) {
953 if (newval && newval > today)
954 $scope.dialog.backdate = today;
958 $scope.cancel = function() {
959 $uibModalInstance.dismiss();
962 $scope.ok = function() {
964 var bd = $scope.dialog.backdate.toISOString().replace(/T.*/,'');
965 service.batch_backdate(circ_ids, bd)
967 function() { // on complete
968 $uibModalInstance.close({backdate : bd});
971 function(resp) { // on response
972 console.debug('backdate returned ' + resp);
974 $scope.num_processed++;
976 console.error(egCore.evt.parse(resp));
985 service.mark_claims_returned = function(barcode, date, override) {
987 var method = 'open-ils.circ.circulation.set_claims_returned';
988 if (override) method += '.override';
990 console.debug('claims returned ' + method);
992 return egCore.net.request(
993 'open-ils.circ', method, egCore.auth.token(),
994 {barcode : barcode, backdate : date})
996 .then(function(resp) {
998 if (resp == 1) { // success
999 console.debug('claims returned succeeded for ' + barcode);
1002 } else if (evt = egCore.evt.parse(resp)) {
1003 console.debug('claims returned failed: ' + evt.toString());
1005 if (evt.textcode == 'PATRON_EXCEEDS_CLAIMS_RETURN_COUNT') {
1006 // TODO check perms before offering override option?
1008 if (override) return;// just to be safe
1010 return egConfirmDialog.open(
1011 egCore.strings.TOO_MANY_CLAIMS_RETURNED, '', {}
1012 ).result.then(function() {
1013 return service.mark_claims_returned(barcode, date, true);
1017 if (evt.textcode == 'PERM_FAILURE') {
1018 console.error('claims returned permission denied')
1019 // TODO: auth override dialog?
1025 service.mark_claims_returned_dialog = function(copy_barcodes) {
1026 if (!copy_barcodes.length) return;
1028 return $uibModal.open({
1029 templateUrl: './circ/share/t_mark_claims_returned_dialog',
1031 ['$scope','$uibModalInstance',
1032 function($scope , $uibModalInstance) {
1034 var today = new Date();
1036 barcodes : copy_barcodes,
1040 $scope.$watch('args.date', function(newval) {
1041 if (newval && newval > today)
1042 $scope.args.backdate = today;
1045 $scope.cancel = function() {$uibModalInstance.dismiss()}
1046 $scope.ok = function() {
1048 var date = $scope.args.date.toISOString().replace(/T.*/,'');
1050 var deferred = $q.defer();
1052 // serialize the action on each barcode so that the
1053 // caller will never see multiple alerts at the same time.
1054 function mark_one() {
1055 var bc = copy_barcodes.pop();
1058 $uibModalInstance.close();
1062 // finally -> continue even when one fails
1063 service.mark_claims_returned(bc, date)
1064 .finally(function(barcode) {
1065 if (barcode) deferred.notify(barcode);
1069 mark_one(); // kick it off
1070 return deferred.promise;
1076 // serially checks in each barcode with claims_never_checked_out set
1077 // returns promise, notified on each barcode, resolved after all
1078 // checkins are complete.
1079 service.mark_claims_never_checked_out = function(barcodes) {
1080 if (!barcodes.length) return;
1082 var deferred = $q.defer();
1083 egConfirmDialog.open(
1084 egCore.strings.MARK_NEVER_CHECKED_OUT, '', {barcodes : barcodes}
1086 ).result.then(function() {
1087 function mark_one() {
1088 var bc = barcodes.pop();
1090 if (!bc) { // all done
1096 {claims_never_checked_out : true, copy_barcode : bc})
1097 .finally(function() {
1098 deferred.notify(bc);
1105 return deferred.promise;
1108 service.mark_damaged = function(copy_ids) {
1109 return egConfirmDialog.open(
1110 egCore.strings.MARK_DAMAGED_CONFIRM, '',
1111 { num_items : copy_ids.length,
1113 cancel : function() {}
1116 ).result.then(function() {
1118 angular.forEach(copy_ids, function(copy_id) {
1122 'open-ils.circ.mark_item_damaged',
1123 egCore.auth.token(), copy_id
1124 ).then(function(resp) {
1125 if (evt = egCore.evt.parse(resp)) {
1126 console.error('mark damaged failed: ' + evt);
1132 return $q.all(promises);
1136 service.mark_missing = function(copy_ids) {
1137 return egConfirmDialog.open(
1138 egCore.strings.MARK_MISSING_CONFIRM, '',
1139 { num_items : copy_ids.length,
1141 cancel : function() {}
1143 ).result.then(function() {
1145 angular.forEach(copy_ids, function(copy_id) {
1149 'open-ils.circ.mark_item_missing',
1150 egCore.auth.token(), copy_id
1151 ).then(function(resp) {
1152 if (evt = egCore.evt.parse(resp)) {
1153 console.error('mark missing failed: ' + evt);
1159 return $q.all(promises);
1165 // Mark circulations as lost via copy barcode. As each item is
1166 // processed, the returned promise is notified of the barcode.
1167 // No confirmation dialog is presented.
1168 service.mark_lost = function(copy_barcodes) {
1169 var deferred = $q.defer();
1172 angular.forEach(copy_barcodes, function(barcode) {
1176 'open-ils.circ.circulation.set_lost',
1177 egCore.auth.token(), {barcode : barcode}
1178 ).then(function(resp) {
1179 if (evt = egCore.evt.parse(resp)) {
1180 console.error("Mark lost failed: " + evt.toString());
1183 // inform the caller as each item is processed
1184 deferred.notify(barcode);
1189 $q.all(promises).then(function() {deferred.resolve()});
1190 return deferred.promise;
1193 service.abort_transits = function(transit_ids) {
1194 return egConfirmDialog.open(
1195 egCore.strings.ABORT_TRANSIT_CONFIRM, '',
1196 { num_transits : transit_ids.length,
1198 cancel : function() {}
1201 ).result.then(function() {
1203 angular.forEach(transit_ids, function(transit_id) {
1207 'open-ils.circ.transit.abort',
1208 egCore.auth.token(), {transitid : transit_id}
1209 ).then(function(resp) {
1210 if (evt = egCore.evt.parse(resp)) {
1211 console.error('abort transit failed: ' + evt);
1217 return $q.all(promises);
1223 // alert when copy location alert_message is set.
1224 // This does not affect processing, it only produces a click-through
1225 service.handle_checkin_loc_alert = function(evt, params, options) {
1226 if (angular.isArray(evt)) evt = evt[0];
1228 var copy = evt && evt.payload ? evt.payload.copy : null;
1230 if (copy && !options.suppress_checkin_popups
1231 && copy.location().checkin_alert() == 't') {
1233 return egAlertDialog.open(
1234 egCore.strings.LOCATION_ALERT_MSG, {copy : copy}).result;
1240 service.handle_checkin_resp = function(evt, params, options) {
1241 if (!angular.isArray(evt)) evt = [evt];
1243 var final_resp = {evt : evt, params : params, options : options};
1245 var copy, hold, transit;
1246 if (evt[0].payload) {
1247 copy = evt[0].payload.copy;
1248 hold = evt[0].payload.hold;
1249 transit = evt[0].payload.transit;
1252 // track the barcode regardless of whether it's valid
1253 angular.forEach(evt, function(e){ e.copy_barcode = params.copy_barcode; });
1255 angular.forEach(evt, function(e){ console.debug('checkin event ' + e.textcode); });
1257 if (evt.filter(function(e){return service.checkin_overridable_events.indexOf(e.textcode) > -1;}).length > 0)
1258 return service.handle_overridable_checkin_event(evt, params, options);
1260 switch (evt[0].textcode) {
1265 switch(Number(copy.status().id())) {
1267 case 0: /* AVAILABLE */
1268 case 4: /* MISSING */
1269 case 7: /* RESHELVING */
1271 egCore.audio.play('success.checkin');
1273 // see if the copy location requires an alert
1274 return service.handle_checkin_loc_alert(evt, params, options)
1275 .then(function() {return final_resp});
1277 case 8: /* ON HOLDS SHELF */
1278 egCore.audio.play('info.checkin.holds_shelf');
1282 if (hold.pickup_lib() == egCore.auth.user().ws_ou()) {
1283 // inform user if the item is on the local holds shelf
1285 evt[0].route_to = egCore.strings.ROUTE_TO_HOLDS_SHELF;
1286 return service.route_dialog(
1287 './circ/share/t_hold_shelf_dialog',
1288 evt[0], params, options
1289 ).then(function() { return final_resp });
1292 // normally, if the hold was on the shelf at a
1293 // different location, it would be put into
1294 // transit, resulting in a ROUTE_ITEM event.
1295 egCore.audio.play('warning.checkin.wrong_shelf');
1296 return $q.when(final_resp);
1300 console.error('checkin: item on holds shelf, '
1301 + 'but hold info not returned from checkin');
1302 return $q.when(final_resp);
1305 case 11: /* CATALOGING */
1306 egCore.audio.play('info.checkin.cataloging');
1307 evt[0].route_to = egCore.strings.ROUTE_TO_CATALOGING;
1308 return $q.when(final_resp);
1310 case 15: /* ON_RESERVATION_SHELF */
1311 egCore.audio.play('info.checkin.reservation');
1312 // TODO: show booking reservation dialog
1313 return $q.when(final_resp);
1316 egCore.audio.play('error.checkin.unknown');
1317 console.error('Unhandled checkin copy status: '
1318 + copy.status().id() + ' : ' + copy.status().name());
1319 return $q.when(final_resp);
1323 return service.route_dialog(
1324 './circ/share/t_transit_dialog',
1325 evt[0], params, options
1326 ).then(function() { return final_resp });
1328 case 'ASSET_COPY_NOT_FOUND':
1329 egCore.audio.play('error.checkin.not_found');
1330 return egAlertDialog.open(
1331 egCore.strings.UNCAT_ALERT_DIALOG, params)
1332 .result.then(function() {return final_resp});
1334 case 'ITEM_NOT_CATALOGED':
1335 egCore.audio.play('error.checkin.not_cataloged');
1336 evt[0].route_to = egCore.strings.ROUTE_TO_CATALOGING;
1337 if (options.no_precat_alert)
1338 return $q.when(final_resp);
1339 return egAlertDialog.open(
1340 egCore.strings.PRECAT_CHECKIN_MSG, params)
1341 .result.then(function() {return final_resp});
1344 egCore.audio.play('error.checkin.unknown');
1345 console.warn('unhandled checkin response : ' + evt[0].textcode);
1346 return $q.when(final_resp);
1350 // collect transit, address, and hold info that's not already
1351 // included in responses.
1352 service.collect_route_data = function(tmpl, evt, params, options) {
1353 if (angular.isArray(evt)) evt = evt[0];
1357 if (evt.org && !tmpl.match(/hold_shelf/)) {
1359 service.get_org_addr(evt.org, 'holds_address')
1360 .then(function(addr) { data.address = addr })
1364 if (evt.payload.hold) {
1366 egCore.pcrud.retrieve('au',
1367 evt.payload.hold.usr(), {
1369 flesh_fields : {'au' : ['card']}
1371 ).then(function(patron) {data.patron = patron})
1375 if (!tmpl.match(/hold_shelf/)) {
1377 service.find_copy_transit(evt, params, options)
1378 .then(function(trans) {data.transit = trans})
1382 return $q.all(promises).then(function() { return data });
1385 service.route_dialog = function(tmpl, evt, params, options) {
1386 if (angular.isArray(evt)) evt = evt[0];
1388 return service.collect_route_data(tmpl, evt, params, options)
1389 .then(function(data) {
1391 var template = data.transit ?
1392 (data.patron ? 'hold_transit_slip' : 'transit_slip') :
1394 if (service.never_auto_print[template]) {
1395 // do not show the dialog or print if the
1396 // disabled automatic print attempt type list includes
1397 // the specified template
1401 // All actions flow from the print data
1403 var print_context = {
1404 copy : egCore.idl.toHash(evt.payload.copy),
1410 // route_dialog includes the "route to holds shelf"
1411 // dialog, which has no transit
1412 print_context.transit = egCore.idl.toHash(data.transit);
1413 print_context.dest_address = egCore.idl.toHash(data.address);
1414 print_context.dest_location =
1415 egCore.idl.toHash(egCore.org.get(data.transit.dest()));
1419 print_context.hold = egCore.idl.toHash(evt.payload.hold);
1420 print_context.patron = egCore.idl.toHash(data.patron);
1423 var sound = 'info.checkin.transit';
1424 if (evt.payload.hold) sound += '.hold';
1425 egCore.audio.play(sound);
1427 function print_transit(template) {
1428 return egCore.print.print({
1429 context : 'default',
1430 template : template,
1431 scope : print_context
1435 // when auto-print is on, skip the dialog and go straight
1437 if (options.auto_print_holds_transits)
1438 return print_transit(template);
1440 return $uibModal.open({
1443 '$scope','$uibModalInstance',
1444 function($scope , $uibModalInstance) {
1446 $scope.today = new Date();
1448 // copy the print scope into the dialog scope
1449 angular.forEach(print_context, function(val, key) {
1453 $scope.ok = function() {$uibModalInstance.close()}
1455 $scope.print = function() {
1456 $uibModalInstance.close();
1457 print_transit(template);
1465 // action == what action to take if the user confirms the alert
1466 service.copy_alert_dialog = function(evt, params, options, action) {
1467 if (angular.isArray(evt)) evt = evt[0];
1468 return egConfirmDialog.open(
1469 egCore.strings.COPY_ALERT_MSG_DIALOG_TITLE,
1470 evt.payload, // payload == alert message text
1471 { copy_barcode : params.copy_barcode,
1473 cancel : function() {}
1475 ).result.then(function() {
1476 options.override = true;
1477 return service[action](params, options);
1481 // check the barcode. If it's no good, show the warning dialog
1482 // Resolves on success, rejected on error
1483 service.test_barcode = function(bc) {
1485 var ok = service.check_barcode(bc);
1486 if (ok) return $q.when();
1488 return $uibModal.open({
1489 templateUrl: './circ/share/t_bad_barcode_dialog',
1491 ['$scope', '$uibModalInstance',
1492 function($scope, $uibModalInstance) {
1493 $scope.barcode = bc;
1494 $scope.ok = function() { $uibModalInstance.close() }
1495 $scope.cancel = function() { $uibModalInstance.dismiss() }
1500 // check() and checkdigit() copied directly
1501 // from chrome/content/util/barcode.js
1503 service.check_barcode = function(bc) {
1504 if (bc != Number(bc)) return false;
1506 // "16.00" == Number("16.00"), but the . is bad.
1507 // Throw out any barcode that isn't just digits
1508 if (bc.search(/\D/) != -1) return false;
1509 var last_digit = bc.substr(bc.length-1);
1510 var stripped_barcode = bc.substr(0,bc.length-1);
1511 return service.barcode_checkdigit(stripped_barcode).toString() == last_digit;
1514 service.barcode_checkdigit = function(bc) {
1515 var reverse_barcode = bc.toString().split('').reverse();
1516 var check_sum = 0; var multiplier = 2;
1517 for (var i = 0; i < reverse_barcode.length; i++) {
1518 var digit = reverse_barcode[i];
1519 var product = digit * multiplier; product = product.toString();
1521 for (var j = 0; j < product.length; j++) {
1522 temp_sum += Number( product[j] );
1524 check_sum += Number( temp_sum );
1525 multiplier = ( multiplier == 2 ? 1 : 2 );
1527 check_sum = check_sum.toString();
1528 var next_multiple_of_10 = (check_sum.match(/(\d*)\d$/)[1] * 10) + 10;
1529 var check_digit = next_multiple_of_10 - Number(check_sum);
1530 if (check_digit == 10) check_digit = 0;
1534 service.create_penalty = function(user_id) {
1535 return $uibModal.open({
1536 templateUrl: './circ/share/t_new_message_dialog',
1538 ['$scope','$uibModalInstance','staffPenalties',
1539 function($scope , $uibModalInstance , staffPenalties) {
1540 $scope.focusNote = true;
1541 $scope.penalties = staffPenalties;
1542 $scope.require_initials = service.require_initials;
1543 $scope.args = {penalty : 21}; // default to Note
1544 $scope.setPenalty = function(id) {
1547 $scope.ok = function(count) { $uibModalInstance.close($scope.args) }
1548 $scope.cancel = function($event) {
1549 $uibModalInstance.dismiss();
1550 $event.preventDefault();
1553 resolve : { staffPenalties : service.get_staff_penalty_types }
1556 var pen = new egCore.idl.ausp();
1558 pen.org_unit(egCore.auth.user().ws_ou());
1559 pen.note(args.note);
1560 if (args.initials) pen.note(args.note + ' [' + args.initials + ']');
1561 if (args.custom_penalty) {
1562 pen.standing_penalty(args.custom_penalty);
1564 pen.standing_penalty(args.penalty);
1566 pen.staff(egCore.auth.user().id());
1567 pen.set_date('now');
1568 return egCore.pcrud.create(pen);
1573 // assumes, for now anyway, penalty type is fleshed onto usr_penalty.
1574 service.edit_penalty = function(usr_penalty) {
1575 return $uibModal.open({
1576 templateUrl: './circ/share/t_new_message_dialog',
1578 ['$scope','$uibModalInstance','staffPenalties',
1579 function($scope , $uibModalInstance , staffPenalties) {
1580 $scope.focusNote = true;
1581 $scope.penalties = staffPenalties;
1582 $scope.require_initials = service.require_initials;
1584 penalty : usr_penalty.standing_penalty().id(),
1585 note : usr_penalty.note()
1587 $scope.setPenalty = function(id) { args.penalty = id; }
1588 $scope.ok = function(count) { $uibModalInstance.close($scope.args) }
1589 $scope.cancel = function($event) {
1590 $uibModalInstance.dismiss();
1591 $event.preventDefault();
1594 resolve : { staffPenalties : service.get_staff_penalty_types }
1597 usr_penalty.note(args.note);
1598 if (args.initials) usr_penalty.note(args.note + ' [' + args.initials + ']');
1599 usr_penalty.standing_penalty(args.penalty);
1600 return egCore.pcrud.update(usr_penalty);