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 'HOLD_CAPTURE_DELAYED', // not technically overridable, but special prompt and param
113 'TRANSIT_CHECKIN_INTERVAL_BLOCK'
116 // Performs a checkout.
117 // Returns a promise resolved with the original params and options
118 // and the final checkout event (e.g. in the case of override).
119 // Rejected if the checkout cannot be completed.
121 // params : passed directly as arguments to the server API
122 // options : non-parameter controls. e.g. "override", "check_barcode"
123 service.checkout = function(params, options) {
124 if (!options) options = {};
126 console.debug('egCirc.checkout() : '
127 + js2JSON(params) + ' : ' + js2JSON(options));
129 var promise = options.check_barcode ?
130 service.test_barcode(params.copy_barcode) : $q.when();
132 // avoid re-check on override, etc.
133 delete options.check_barcode;
135 return promise.then(function() {
137 var method = 'open-ils.circ.checkout.full';
138 if (options.override) method += '.override';
140 return egCore.net.request(
141 'open-ils.circ', method, egCore.auth.token(), params
143 ).then(function(evt) {
145 if (!angular.isArray(evt)) evt = [evt];
147 if (evt[0].payload && evt[0].payload.auto_renew == 1) {
148 // open circulation found with auto-renew toggle on.
149 console.debug('Auto-renewing item ' + params.copy_barcode);
150 options.auto_renew = true;
151 return service.renew(params, options);
154 var action = params.noncat ? 'noncat_checkout' : 'checkout';
156 return service.flesh_response_data(action, evt, params, options)
158 return service.handle_checkout_resp(evt, params, options);
160 .then(function(final_resp) {
161 return service.munge_resp_data(final_resp,action,method)
167 // Performs a renewal.
168 // Returns a promise resolved with the original params and options
169 // and the final checkout event (e.g. in the case of override)
170 // Rejected if the renewal cannot be completed.
171 service.renew = function(params, options) {
172 if (!options) options = {};
174 console.debug('egCirc.renew() : '
175 + js2JSON(params) + ' : ' + js2JSON(options));
177 var promise = options.check_barcode ?
178 service.test_barcode(params.copy_barcode) : $q.when();
180 // avoid re-check on override, etc.
181 delete options.check_barcode;
183 return promise.then(function() {
185 var method = 'open-ils.circ.renew';
186 if (options.override) method += '.override';
188 return egCore.net.request(
189 'open-ils.circ', method, egCore.auth.token(), params
191 ).then(function(evt) {
193 if (!angular.isArray(evt)) evt = [evt];
195 return service.flesh_response_data(
196 'renew', evt, params, options)
198 return service.handle_renew_resp(evt, params, options);
200 .then(function(final_resp) {
201 final_resp.auto_renew = options.auto_renew;
202 return service.munge_resp_data(final_resp,'renew',method)
208 // Performs a checkin
209 // Returns a promise resolved with the original params and options,
210 // plus the final checkin event (e.g. in the case of override).
211 // Rejected if the checkin cannot be completed.
212 service.checkin = function(params, options) {
213 if (!options) options = {};
215 console.debug('egCirc.checkin() : '
216 + js2JSON(params) + ' : ' + js2JSON(options));
218 var promise = options.check_barcode ?
219 service.test_barcode(params.copy_barcode) : $q.when();
221 // avoid re-check on override, etc.
222 delete options.check_barcode;
224 return promise.then(function() {
226 var method = 'open-ils.circ.checkin';
227 if (options.override) method += '.override';
229 return egCore.net.request(
230 'open-ils.circ', method, egCore.auth.token(), params
232 ).then(function(evt) {
234 if (!angular.isArray(evt)) evt = [evt];
235 return service.flesh_response_data(
236 'checkin', evt, params, options)
238 return service.handle_checkin_resp(evt, params, options);
240 .then(function(final_resp) {
241 return service.munge_resp_data(final_resp,'checkin',method)
247 // provide consistent formatting of the final response data
248 service.munge_resp_data = function(final_resp,worklog_action,worklog_method) {
249 var data = final_resp.data = {};
251 if (!final_resp.evt[0]) {
252 egCore.audio.play('error.unknown.no_event');
256 var payload = final_resp.evt[0].payload;
258 egCore.audio.play('error.unknown.no_payload');
262 data.circ = payload.circ;
263 data.parent_circ = payload.parent_circ;
264 data.hold = payload.hold;
265 data.record = payload.record;
266 data.acp = payload.copy;
267 data.acn = payload.volume ? payload.volume : payload.copy ? payload.copy.call_number() : null;
268 data.au = payload.patron;
269 data.transit = payload.transit;
270 data.status = payload.status;
271 data.message = payload.message;
272 data.title = final_resp.evt[0].title;
273 data.author = final_resp.evt[0].author;
274 data.isbn = final_resp.evt[0].isbn;
275 data.route_to = final_resp.evt[0].route_to;
277 // for checkin, the mbts lives on the main circ
278 if (payload.circ && payload.circ.billable_transaction())
279 data.mbts = payload.circ.billable_transaction().summary();
281 // on renewals, the mbts lives on the parent circ
282 if (payload.parent_circ && payload.parent_circ.billable_transaction())
283 data.mbts = payload.parent_circ.billable_transaction().summary();
285 if (!data.route_to) {
287 data.route_to = data.transit.dest().shortname();
288 } else if (data.acp) {
289 data.route_to = data.acp.location().name();
294 (worklog_action == 'checkout' || worklog_action == 'noncat_checkout')
295 ? egCore.strings.EG_WORK_LOG_CHECKOUT
296 : (worklog_action == 'renew'
297 ? egCore.strings.EG_WORK_LOG_RENEW
298 : egCore.strings.EG_WORK_LOG_CHECKIN // worklog_action == 'checkin'
300 'action' : worklog_action,
301 'method' : worklog_method,
302 'response' : final_resp
309 service.handle_overridable_checkout_event = function(evt, params, options) {
311 if (options.override) {
312 // override attempt already made and failed.
313 // NOTE: I don't think we'll ever get here, since the
314 // override attempt should produce a perm failure...
315 angular.forEach(evt, function(e){ console.debug('override failed: ' + e.textcode); });
320 if (evt.filter(function(e){return !service.auto_override_checkout_events[e.textcode];}).length == 0) {
321 // user has already opted to override these type
322 // of events. Re-run the checkout w/ override.
323 options.override = true;
324 return service.checkout(params, options);
327 // Ask the user if they would like to override this event.
328 // Some events offer a stock override dialog, while others
329 // require additional context.
331 switch(evt[0].textcode) {
332 case 'COPY_NOT_AVAILABLE':
333 return service.copy_not_avail_dialog(evt[0], params, options);
334 case 'COPY_ALERT_MESSAGE':
335 return service.copy_alert_dialog(evt[0], params, options, 'checkout');
337 return service.override_dialog(evt, params, options, 'checkout');
341 service.handle_overridable_renew_event = function(evt, params, options) {
343 if (options.override) {
344 // override attempt already made and failed.
345 // NOTE: I don't think we'll ever get here, since the
346 // override attempt should produce a perm failure...
347 angular.forEach(evt, function(e){ console.debug('override failed: ' + e.textcode); });
352 // renewal auto-overrides are the same as checkout
353 if (evt.filter(function(e){return !service.auto_override_checkout_events[e.textcode];}).length == 0) {
354 // user has already opted to override these type
355 // of events. Re-run the renew w/ override.
356 options.override = true;
357 return service.renew(params, options);
360 // Ask the user if they would like to override this event.
361 // Some events offer a stock override dialog, while others
362 // require additional context.
364 switch(evt[0].textcode) {
365 case 'COPY_ALERT_MESSAGE':
366 return service.copy_alert_dialog(evt[0], params, options, 'renew');
368 return service.override_dialog(evt, params, options, 'renew');
373 service.handle_overridable_checkin_event = function(evt, params, options) {
375 if (options.override) {
376 // override attempt already made and failed.
377 // NOTE: I don't think we'll ever get here, since the
378 // override attempt should produce a perm failure...
379 angular.forEach(evt, function(e){ console.debug('override failed: ' + e.textcode); });
384 if (options.suppress_checkin_popups
385 && evt.filter(function(e){return service.checkin_suppress_overrides.indexOf(e.textcode) == -1;}).length == 0) {
386 // Events are suppressed. Re-run the checkin w/ override.
387 options.override = true;
388 return service.checkin(params, options);
391 // Ask the user if they would like to override this event.
392 // Some events offer a stock override dialog, while others
393 // require additional context.
395 switch(evt[0].textcode) {
396 case 'COPY_ALERT_MESSAGE':
397 return service.copy_alert_dialog(evt[0], params, options, 'checkin');
398 case 'HOLD_CAPTURE_DELAYED':
399 return service.hold_capture_delay_dialog(evt[0], params, options, 'checkin');
401 return service.override_dialog(evt, params, options, 'checkin');
406 service.handle_renew_resp = function(evt, params, options) {
408 var final_resp = {evt : evt, params : params, options : options};
410 // track the barcode regardless of whether it refers to a copy
411 angular.forEach(evt, function(e){ e.copy_barcode = params.copy_barcode; });
413 // Overridable Events
414 if (evt.filter(function(e){return service.renew_overridable_events.indexOf(e.textcode) > -1;}).length > 0)
415 return service.handle_overridable_renew_event(evt, params, options);
418 switch (evt[0].textcode) {
420 egCore.audio.play('info.renew');
421 return $q.when(final_resp);
423 case 'COPY_IN_TRANSIT':
424 case 'PATRON_CARD_INACTIVE':
425 case 'PATRON_INACTIVE':
426 case 'PATRON_ACCOUNT_EXPIRED':
427 case 'CIRC_CLAIMS_RETURNED':
428 egCore.audio.play('warning.renew');
429 return service.exit_alert(
430 egCore.strings[evt[0].textcode],
431 {barcode : params.copy_barcode}
435 egCore.audio.play('warning.renew.unknown');
436 return service.exit_alert(
437 egCore.strings.CHECKOUT_FAILED_GENERIC, {
438 barcode : params.copy_barcode,
439 textcode : evt[0].textcode,
447 service.handle_checkout_resp = function(evt, params, options) {
449 var final_resp = {evt : evt, params : params, options : options};
451 // track the barcode regardless of whether it refers to a copy
452 angular.forEach(evt, function(e){ e.copy_barcode = params.copy_barcode; });
454 // Overridable Events
455 if (evt.filter(function(e){return service.checkout_overridable_events.indexOf(e.textcode) > -1;}).length > 0)
456 return service.handle_overridable_checkout_event(evt, params, options);
459 switch (evt[0].textcode) {
461 egCore.audio.play('success.checkout');
462 return $q.when(final_resp);
464 case 'ITEM_NOT_CATALOGED':
465 egCore.audio.play('error.checkout.no_cataloged');
466 return service.precat_dialog(params, options);
468 case 'OPEN_CIRCULATION_EXISTS':
469 // auto_renew checked in service.checkout()
470 egCore.audio.play('error.checkout.open_circ');
471 return service.circ_exists_dialog(evt, params, options);
473 case 'COPY_IN_TRANSIT':
474 egCore.audio.play('warning.checkout.in_transit');
475 return service.copy_in_transit_dialog(evt, params, options);
477 case 'PATRON_CARD_INACTIVE':
478 case 'PATRON_INACTIVE':
479 case 'PATRON_ACCOUNT_EXPIRED':
480 case 'CIRC_CLAIMS_RETURNED':
481 egCore.audio.play('warning.checkout');
482 return service.exit_alert(
483 egCore.strings[evt[0].textcode],
484 {barcode : params.copy_barcode}
488 egCore.audio.play('error.checkout.unknown');
489 return service.exit_alert(
490 egCore.strings.CHECKOUT_FAILED_GENERIC, {
491 barcode : params.copy_barcode,
492 textcode : evt[0].textcode,
499 // returns a promise resolved with the list of circ mods
500 service.get_circ_mods = function() {
502 return $q.when(egCore.env.ccm.list);
504 return egCore.pcrud.retrieveAll('ccm', null, {atomic : true})
505 .then(function(list) {
506 egCore.env.absorbList(list, 'ccm');
511 // returns a promise resolved with the list of noncat types
512 service.get_noncat_types = function() {
514 return $q.when(egCore.env.cnct.list);
516 return egCore.pcrud.search('cnct',
518 egCore.org.fullPath(egCore.auth.user().ws_ou(), true)},
519 null, {atomic : true}
520 ).then(function(list) {
521 egCore.env.absorbList(list, 'cnct');
526 service.get_staff_penalty_types = function() {
528 return $q.when(egCore.env.csp.list);
529 return egCore.pcrud.search(
530 // id <= 100 are reserved for system use
531 'csp', {id : {'>': 100}}, {}, {atomic : true})
532 .then(function(penalties) {
533 return egCore.env.absorbList(penalties, 'csp').list;
537 // ideally all of these data should be returned with the response,
538 // but until then, grab what we need.
539 service.flesh_response_data = function(action, evt, params, options) {
542 if (!evt[0] || !(payload = evt[0].payload)) return $q.when();
544 promises.push(service.flesh_copy_location(payload.copy));
547 service.flesh_copy_status(payload.copy)
550 // copy is in transit, but no transit was delivered
551 // in the payload. Do this here instead of below to
552 // ensure consistent copy status fleshiness
553 if (!payload.transit && payload.copy.status().id() == 6) { // in-transit
554 return service.find_copy_transit(evt, params, options)
555 .then(function(trans) {
557 trans.source(egCore.org.get(trans.source()));
558 trans.dest(egCore.org.get(trans.dest()));
559 payload.transit = trans;
567 // local flesh transit
568 if (transit = payload.transit) {
569 transit.source(egCore.org.get(transit.source()));
570 transit.dest(egCore.org.get(transit.dest()));
573 // TODO: renewal responses should include the patron
574 if (!payload.patron) {
576 if (payload.circ) user_id = payload.circ.usr();
577 if (payload.noncat_circ) user_id = payload.noncat_circ.patron();
580 egCore.pcrud.retrieve('au', user_id)
581 .then(function(user) {payload.patron = user})
586 // extract precat values
587 angular.forEach(evt, function(e){ e.title = payload.record ? payload.record.title() :
588 (payload.copy ? payload.copy.dummy_title() : null);});
590 angular.forEach(evt, function(e){ e.author = payload.record ? payload.record.author() :
591 (payload.copy ? payload.copy.dummy_author() : null);});
593 angular.forEach(evt, function(e){ e.isbn = payload.record ? payload.record.isbn() :
594 (payload.copy ? payload.copy.dummy_isbn() : null);});
596 return $q.all(promises);
599 // fetches the full list of copy statuses
600 service.flesh_copy_status = function(copy) {
601 if (!copy) return $q.when();
603 return $q.when(copy.status(egCore.env.ccs.map[copy.status()]));
604 return egCore.pcrud.retrieveAll('ccs', {}, {atomic : true}).then(
606 egCore.env.absorbList(list, 'ccs');
607 copy.status(egCore.env.ccs.map[copy.status()]);
612 // there may be *many* copy locations and we may be handling items
613 // for other locations. Fetch copy locations as-needed and cache.
614 service.flesh_copy_location = function(copy) {
615 if (!copy) return $q.when();
616 if (angular.isObject(copy.location())) return $q.when(copy);
617 if (egCore.env.acpl) {
618 if (egCore.env.acpl.map[copy.location()]) {
619 copy.location(egCore.env.acpl.map[copy.location()]);
620 return $q.when(copy);
623 return egCore.pcrud.retrieve('acpl', copy.location())
624 .then(function(loc) {
625 egCore.env.absorbList([loc], 'acpl'); // append to cache
632 // fetch org unit addresses as needed.
633 service.get_org_addr = function(org_id, addr_type) {
634 var org = egCore.org.get(org_id);
635 var addr_id = org[addr_type]();
637 if (!addr_id) return $q.when(null);
639 if (egCore.env.aoa && egCore.env.aoa.map[addr_id])
640 return $q.when(egCore.env.aoa.map[addr_id]);
642 return egCore.pcrud.retrieve('aoa', addr_id).then(function(addr) {
643 egCore.env.absorbList([addr], 'aoa');
644 return egCore.env.aoa.map[addr_id];
648 service.exit_alert = function(msg, scope) {
649 return egAlertDialog.open(msg, scope).result.then(
650 function() {return $q.reject()});
653 // opens a dialog asking the user if they would like to override
654 // the returned event.
655 service.override_dialog = function(evt, params, options, action) {
656 if (!angular.isArray(evt)) evt = [evt];
658 egCore.audio.play('warning.circ.event_override');
659 return $uibModal.open({
660 templateUrl: './circ/share/t_event_override_dialog',
662 ['$scope', '$uibModalInstance',
663 function($scope, $uibModalInstance) {
665 $scope.auto_override =
666 evt.filter(function(e){
667 return service.checkout_auto_override_after_first.indexOf(evt.textcode) > -1;
669 $scope.copy_barcode = params.copy_barcode; // may be null
670 $scope.ok = function() { $uibModalInstance.close() }
671 $scope.cancel = function ($event) {
672 $uibModalInstance.dismiss();
673 $event.preventDefault();
678 options.override = true;
680 if (action == 'checkin') {
681 return service.checkin(params, options);
684 // checkout/renew support override-after-first
685 angular.forEach(evt, function(e){
686 if (service.checkout_auto_override_after_first.indexOf(e.textcode) > -1)
687 service.auto_override_checkout_events[e.textcode] = true;
690 return service[action](params, options);
695 service.copy_not_avail_dialog = function(evt, params, options) {
696 if (angular.isArray(evt)) evt = evt[0];
697 return $uibModal.open({
698 templateUrl: './circ/share/t_copy_not_avail_dialog',
700 ['$scope','$uibModalInstance','copyStatus',
701 function($scope , $uibModalInstance , copyStatus) {
702 $scope.copyStatus = copyStatus;
703 $scope.ok = function() {$uibModalInstance.close()}
704 $scope.cancel = function() {$uibModalInstance.dismiss()}
707 copyStatus : function() {
708 return egCore.pcrud.retrieve(
709 'ccs', evt.payload.status());
714 options.override = true;
715 return service.checkout(params, options);
720 // Opens a dialog allowing the user to fill in the desired non-cat count.
721 // Unlike other dialogs, which kickoff circ actions internally
722 // as a result of events, this dialog does not kick off any circ
723 // actions. It just collects the count and and resolves the promise.
725 // This assumes the caller has already handled the noncat-type
726 // selection and just needs to collect the count info.
727 service.noncat_dialog = function(params, options) {
728 var noncatMax = 99; // hard-coded max
730 // the caller should presumably have fetched the noncat_types via
731 // our API already, but fetch them again (from cache) to be safe.
732 return service.get_noncat_types().then(function() {
734 params.noncat = true;
735 var type = egCore.env.cnct.map[params.noncat_type];
737 return $uibModal.open({
738 templateUrl: './circ/share/t_noncat_dialog',
740 ['$scope', '$uibModalInstance',
741 function($scope, $uibModalInstance) {
742 $scope.focusMe = true;
745 $scope.noncatMax = noncatMax;
746 $scope.ok = function(count) { $uibModalInstance.close(count) }
747 $scope.cancel = function ($event) {
748 $uibModalInstance.dismiss()
749 $event.preventDefault();
754 if (count && count > 0 && count <= noncatMax) {
755 // NOTE: in Chrome, form validation ensure a valid number
756 params.noncat_count = count;
757 return $q.when(params);
766 // Opens a dialog allowing the user to fill in pre-cat copy info.
767 service.precat_dialog = function(params, options) {
769 return $uibModal.open({
770 templateUrl: './circ/share/t_precat_dialog',
772 ['$scope', '$uibModalInstance', 'circMods',
773 function($scope, $uibModalInstance, circMods) {
774 $scope.focusMe = true;
775 $scope.precatArgs = {
776 copy_barcode : params.copy_barcode
778 $scope.circModifiers = circMods;
779 $scope.ok = function(args) { $uibModalInstance.close(args) }
780 $scope.cancel = function () { $uibModalInstance.dismiss() }
783 circMods : function() {
784 return service.get_circ_mods();
789 if (!args || !args.dummy_title) return $q.reject();
790 if(args.circ_modifier == "") args.circ_modifier = null;
791 angular.forEach(args, function(val, key) {params[key] = val});
792 params.precat = true;
793 return service.checkout(params, options);
798 // find the open transit for the given copy barcode; flesh the org
800 service.find_copy_transit = function(evt, params, options) {
801 if (angular.isArray(evt)) evt = evt[0];
803 if (evt && evt.payload && evt.payload.transit)
804 return $q.when(evt.payload.transit);
806 return egCore.pcrud.search('atc',
807 { dest_recv_time : null},
809 flesh_fields : {atc : ['target_copy']},
813 barcode : params.copy_barcode,
819 order_by : {atc : 'source_send_time desc'},
821 ).then(function(transit) {
822 transit.source(egCore.org.get(transit.source()));
823 transit.dest(egCore.org.get(transit.dest()));
828 service.copy_in_transit_dialog = function(evt, params, options) {
829 if (angular.isArray(evt)) evt = evt[0];
830 return $uibModal.open({
831 templateUrl: './circ/share/t_copy_in_transit_dialog',
833 ['$scope','$uibModalInstance','transit',
834 function($scope , $uibModalInstance , transit) {
835 $scope.transit = transit;
836 $scope.ok = function() { $uibModalInstance.close(transit) }
837 $scope.cancel = function() { $uibModalInstance.dismiss() }
840 // fetch the conflicting open transit w/ fleshed copy
841 transit : function() {
842 return service.find_copy_transit(evt, params, options);
847 // user chose to abort the transit then checkout
848 return service.abort_transit(transit.id())
850 return service.checkout(params, options);
856 service.abort_transit = function(transit_id) {
857 return egCore.net.request(
859 'open-ils.circ.transit.abort',
860 egCore.auth.token(), {transitid : transit_id}
861 ).then(function(resp) {
862 if (evt = egCore.evt.parse(resp)) {
870 service.last_copy_circ = function(copy_id) {
871 return egCore.pcrud.search('circ',
872 {target_copy : copy_id},
873 {order_by : {circ : 'xact_start desc' }, limit : 1}
877 service.circ_exists_dialog = function(evt, params, options) {
878 if (angular.isArray(evt)) evt = evt[0];
880 if (!evt.payload.old_circ) {
881 return egCore.pcrud.search('circ',
882 {target_copy : evt.payload.copy.id(), checkin_time : null},
883 {limit : 1} // should only ever be 1
884 ).then(function(old_circ) {
885 evt.payload.old_circ = old_circ;
886 return service.circ_exists_dialog_impl(evt, params, options);
889 return service.circ_exists_dialog_impl( evt, params, options );
893 service.circ_exists_dialog_impl = function (evt, params, options) {
895 var openCirc = evt.payload.old_circ;
896 var sameUser = openCirc.usr() == params.patron_id;
898 return $uibModal.open({
899 templateUrl: './circ/share/t_circ_exists_dialog',
901 ['$scope','$uibModalInstance',
902 function($scope , $uibModalInstance) {
903 $scope.args = {forgive_fines : false};
904 $scope.circDate = openCirc.xact_start();
905 $scope.sameUser = sameUser;
906 $scope.ok = function() { $uibModalInstance.close($scope.args) }
907 $scope.cancel = function($event) {
908 $uibModalInstance.dismiss();
909 $event.preventDefault(); // form, avoid calling ok();
915 params.void_overdues = args.forgive_fines;
916 options.override = true;
917 return service.renew(params, options);
920 return service.checkin({
921 barcode : params.copy_barcode,
924 void_overdues : args.forgive_fines
925 }).then(function(checkin_resp) {
926 if (checkin_resp.evt[0].textcode == 'SUCCESS') {
927 return service.checkout(params, options);
929 alert(egCore.evt.parse(checkin_resp.evt[0]));
937 service.batch_backdate = function(circ_ids, backdate) {
938 return egCore.net.request(
940 'open-ils.circ.post_checkin_backdate.batch',
941 egCore.auth.token(), circ_ids, backdate);
944 service.backdate_dialog = function(circ_ids) {
945 return $uibModal.open({
946 templateUrl: './circ/share/t_backdate_dialog',
948 ['$scope','$uibModalInstance',
949 function($scope , $uibModalInstance) {
951 var today = new Date();
953 num_circs : circ_ids.length,
958 $scope.$watch('dialog.backdate', function(newval) {
959 if (newval && newval > today)
960 $scope.dialog.backdate = today;
964 $scope.cancel = function() {
965 $uibModalInstance.dismiss();
968 $scope.ok = function() {
970 var bd = $scope.dialog.backdate.toISOString().replace(/T.*/,'');
971 service.batch_backdate(circ_ids, bd)
973 function() { // on complete
974 $uibModalInstance.close({backdate : bd});
977 function(resp) { // on response
978 console.debug('backdate returned ' + resp);
980 $scope.num_processed++;
982 console.error(egCore.evt.parse(resp));
991 service.mark_claims_returned = function(barcode, date, override) {
993 var method = 'open-ils.circ.circulation.set_claims_returned';
994 if (override) method += '.override';
996 console.debug('claims returned ' + method);
998 return egCore.net.request(
999 'open-ils.circ', method, egCore.auth.token(),
1000 {barcode : barcode, backdate : date})
1002 .then(function(resp) {
1004 if (resp == 1) { // success
1005 console.debug('claims returned succeeded for ' + barcode);
1008 } else if (evt = egCore.evt.parse(resp)) {
1009 console.debug('claims returned failed: ' + evt.toString());
1011 if (evt.textcode == 'PATRON_EXCEEDS_CLAIMS_RETURN_COUNT') {
1012 // TODO check perms before offering override option?
1014 if (override) return;// just to be safe
1016 return egConfirmDialog.open(
1017 egCore.strings.TOO_MANY_CLAIMS_RETURNED, '', {}
1018 ).result.then(function() {
1019 return service.mark_claims_returned(barcode, date, true);
1023 if (evt.textcode == 'PERM_FAILURE') {
1024 console.error('claims returned permission denied')
1025 // TODO: auth override dialog?
1031 service.mark_claims_returned_dialog = function(copy_barcodes) {
1032 if (!copy_barcodes.length) return;
1034 return $uibModal.open({
1035 templateUrl: './circ/share/t_mark_claims_returned_dialog',
1037 ['$scope','$uibModalInstance',
1038 function($scope , $uibModalInstance) {
1040 var today = new Date();
1042 barcodes : copy_barcodes,
1046 $scope.$watch('args.date', function(newval) {
1047 if (newval && newval > today)
1048 $scope.args.backdate = today;
1051 $scope.cancel = function() {$uibModalInstance.dismiss()}
1052 $scope.ok = function() {
1054 var date = $scope.args.date.toISOString().replace(/T.*/,'');
1056 var deferred = $q.defer();
1058 // serialize the action on each barcode so that the
1059 // caller will never see multiple alerts at the same time.
1060 function mark_one() {
1061 var bc = copy_barcodes.pop();
1064 $uibModalInstance.close();
1068 // finally -> continue even when one fails
1069 service.mark_claims_returned(bc, date)
1070 .finally(function(barcode) {
1071 if (barcode) deferred.notify(barcode);
1075 mark_one(); // kick it off
1076 return deferred.promise;
1082 // serially checks in each barcode with claims_never_checked_out set
1083 // returns promise, notified on each barcode, resolved after all
1084 // checkins are complete.
1085 service.mark_claims_never_checked_out = function(barcodes) {
1086 if (!barcodes.length) return;
1088 var deferred = $q.defer();
1089 egConfirmDialog.open(
1090 egCore.strings.MARK_NEVER_CHECKED_OUT, '', {barcodes : barcodes}
1092 ).result.then(function() {
1093 function mark_one() {
1094 var bc = barcodes.pop();
1096 if (!bc) { // all done
1102 {claims_never_checked_out : true, copy_barcode : bc})
1103 .finally(function() {
1104 deferred.notify(bc);
1111 return deferred.promise;
1114 service.mark_damaged = function(copy_ids) {
1115 return egConfirmDialog.open(
1116 egCore.strings.MARK_DAMAGED_CONFIRM, '',
1117 { num_items : copy_ids.length,
1119 cancel : function() {}
1122 ).result.then(function() {
1124 angular.forEach(copy_ids, function(copy_id) {
1128 'open-ils.circ.mark_item_damaged',
1129 egCore.auth.token(), copy_id
1130 ).then(function(resp) {
1131 if (evt = egCore.evt.parse(resp)) {
1132 console.error('mark damaged failed: ' + evt);
1138 return $q.all(promises);
1142 service.mark_missing = function(copy_ids) {
1143 return egConfirmDialog.open(
1144 egCore.strings.MARK_MISSING_CONFIRM, '',
1145 { num_items : copy_ids.length,
1147 cancel : function() {}
1149 ).result.then(function() {
1151 angular.forEach(copy_ids, function(copy_id) {
1155 'open-ils.circ.mark_item_missing',
1156 egCore.auth.token(), copy_id
1157 ).then(function(resp) {
1158 if (evt = egCore.evt.parse(resp)) {
1159 console.error('mark missing failed: ' + evt);
1165 return $q.all(promises);
1171 // Mark circulations as lost via copy barcode. As each item is
1172 // processed, the returned promise is notified of the barcode.
1173 // No confirmation dialog is presented.
1174 service.mark_lost = function(copy_barcodes) {
1175 var deferred = $q.defer();
1178 angular.forEach(copy_barcodes, function(barcode) {
1182 'open-ils.circ.circulation.set_lost',
1183 egCore.auth.token(), {barcode : barcode}
1184 ).then(function(resp) {
1185 if (evt = egCore.evt.parse(resp)) {
1186 console.error("Mark lost failed: " + evt.toString());
1189 // inform the caller as each item is processed
1190 deferred.notify(barcode);
1195 $q.all(promises).then(function() {deferred.resolve()});
1196 return deferred.promise;
1199 service.abort_transits = function(transit_ids) {
1200 return egConfirmDialog.open(
1201 egCore.strings.ABORT_TRANSIT_CONFIRM, '',
1202 { num_transits : transit_ids.length,
1204 cancel : function() {}
1207 ).result.then(function() {
1209 angular.forEach(transit_ids, function(transit_id) {
1213 'open-ils.circ.transit.abort',
1214 egCore.auth.token(), {transitid : transit_id}
1215 ).then(function(resp) {
1216 if (evt = egCore.evt.parse(resp)) {
1217 console.error('abort transit failed: ' + evt);
1223 return $q.all(promises);
1229 // alert when copy location alert_message is set.
1230 // This does not affect processing, it only produces a click-through
1231 service.handle_checkin_loc_alert = function(evt, params, options) {
1232 if (angular.isArray(evt)) evt = evt[0];
1234 var copy = evt && evt.payload ? evt.payload.copy : null;
1236 if (copy && !options.suppress_checkin_popups
1237 && copy.location().checkin_alert() == 't') {
1239 return egAlertDialog.open(
1240 egCore.strings.LOCATION_ALERT_MSG, {copy : copy}).result;
1246 service.handle_checkin_resp = function(evt, params, options) {
1247 if (!angular.isArray(evt)) evt = [evt];
1249 var final_resp = {evt : evt, params : params, options : options};
1251 var copy, hold, transit;
1252 if (evt[0].payload) {
1253 copy = evt[0].payload.copy;
1254 hold = evt[0].payload.hold;
1255 transit = evt[0].payload.transit;
1258 // track the barcode regardless of whether it's valid
1259 angular.forEach(evt, function(e){ e.copy_barcode = params.copy_barcode; });
1261 angular.forEach(evt, function(e){ console.debug('checkin event ' + e.textcode); });
1263 if (evt.filter(function(e){return service.checkin_overridable_events.indexOf(e.textcode) > -1;}).length > 0)
1264 return service.handle_overridable_checkin_event(evt, params, options);
1266 switch (evt[0].textcode) {
1271 switch(Number(copy.status().id())) {
1273 case 0: /* AVAILABLE */
1274 case 4: /* MISSING */
1275 case 7: /* RESHELVING */
1277 egCore.audio.play('success.checkin');
1279 // see if the copy location requires an alert
1280 return service.handle_checkin_loc_alert(evt, params, options)
1281 .then(function() {return final_resp});
1283 case 8: /* ON HOLDS SHELF */
1284 egCore.audio.play('info.checkin.holds_shelf');
1288 if (hold.pickup_lib() == egCore.auth.user().ws_ou()) {
1289 // inform user if the item is on the local holds shelf
1291 evt[0].route_to = egCore.strings.ROUTE_TO_HOLDS_SHELF;
1292 return service.route_dialog(
1293 './circ/share/t_hold_shelf_dialog',
1294 evt[0], params, options
1295 ).then(function() { return final_resp });
1298 // normally, if the hold was on the shelf at a
1299 // different location, it would be put into
1300 // transit, resulting in a ROUTE_ITEM event.
1301 egCore.audio.play('warning.checkin.wrong_shelf');
1302 return $q.when(final_resp);
1306 console.error('checkin: item on holds shelf, '
1307 + 'but hold info not returned from checkin');
1308 return $q.when(final_resp);
1311 case 11: /* CATALOGING */
1312 egCore.audio.play('info.checkin.cataloging');
1313 evt[0].route_to = egCore.strings.ROUTE_TO_CATALOGING;
1314 return $q.when(final_resp);
1316 case 15: /* ON_RESERVATION_SHELF */
1317 egCore.audio.play('info.checkin.reservation');
1318 // TODO: show booking reservation dialog
1319 return $q.when(final_resp);
1322 egCore.audio.play('error.checkin.unknown');
1323 console.error('Unhandled checkin copy status: '
1324 + copy.status().id() + ' : ' + copy.status().name());
1325 return $q.when(final_resp);
1329 return service.route_dialog(
1330 './circ/share/t_transit_dialog',
1331 evt[0], params, options
1332 ).then(function() { return final_resp });
1334 case 'ASSET_COPY_NOT_FOUND':
1335 egCore.audio.play('error.checkin.not_found');
1336 return egAlertDialog.open(
1337 egCore.strings.UNCAT_ALERT_DIALOG, params)
1338 .result.then(function() {return final_resp});
1340 case 'ITEM_NOT_CATALOGED':
1341 egCore.audio.play('error.checkin.not_cataloged');
1342 evt[0].route_to = egCore.strings.ROUTE_TO_CATALOGING;
1343 if (options.no_precat_alert)
1344 return $q.when(final_resp);
1345 return egAlertDialog.open(
1346 egCore.strings.PRECAT_CHECKIN_MSG, params)
1347 .result.then(function() {return final_resp});
1350 egCore.audio.play('error.checkin.unknown');
1351 console.warn('unhandled checkin response : ' + evt[0].textcode);
1352 return $q.when(final_resp);
1356 // collect transit, address, and hold info that's not already
1357 // included in responses.
1358 service.collect_route_data = function(tmpl, evt, params, options) {
1359 if (angular.isArray(evt)) evt = evt[0];
1363 if (evt.org && !tmpl.match(/hold_shelf/)) {
1365 service.get_org_addr(evt.org, 'holds_address')
1366 .then(function(addr) { data.address = addr })
1370 if (evt.payload.hold) {
1372 egCore.pcrud.retrieve('au',
1373 evt.payload.hold.usr(), {
1375 flesh_fields : {'au' : ['card']}
1377 ).then(function(patron) {data.patron = patron})
1381 if (!tmpl.match(/hold_shelf/)) {
1383 service.find_copy_transit(evt, params, options)
1384 .then(function(trans) {data.transit = trans})
1388 return $q.all(promises).then(function() { return data });
1391 service.route_dialog = function(tmpl, evt, params, options) {
1392 if (angular.isArray(evt)) evt = evt[0];
1394 return service.collect_route_data(tmpl, evt, params, options)
1395 .then(function(data) {
1397 var template = data.transit ?
1398 (data.patron ? 'hold_transit_slip' : 'transit_slip') :
1400 if (service.never_auto_print[template]) {
1401 // do not show the dialog or print if the
1402 // disabled automatic print attempt type list includes
1403 // the specified template
1407 // All actions flow from the print data
1409 var print_context = {
1410 copy : egCore.idl.toHash(evt.payload.copy),
1416 // route_dialog includes the "route to holds shelf"
1417 // dialog, which has no transit
1418 print_context.transit = egCore.idl.toHash(data.transit);
1420 print_context.dest_address = egCore.idl.toHash(data.address);
1422 print_context.dest_location =
1423 egCore.idl.toHash(egCore.org.get(data.transit.dest()));
1427 print_context.hold = egCore.idl.toHash(evt.payload.hold);
1428 print_context.patron = egCore.idl.toHash(data.patron);
1431 var sound = 'info.checkin.transit';
1432 if (evt.payload.hold) sound += '.hold';
1433 egCore.audio.play(sound);
1435 function print_transit(template) {
1436 return egCore.print.print({
1437 context : 'default',
1438 template : template,
1439 scope : print_context
1443 // when auto-print is on, skip the dialog and go straight
1445 if (options.auto_print_holds_transits)
1446 return print_transit(template);
1448 return $uibModal.open({
1451 '$scope','$uibModalInstance',
1452 function($scope , $uibModalInstance) {
1454 $scope.today = new Date();
1456 // copy the print scope into the dialog scope
1457 angular.forEach(print_context, function(val, key) {
1461 $scope.ok = function() {$uibModalInstance.close()}
1463 $scope.print = function() {
1464 $uibModalInstance.close();
1465 print_transit(template);
1473 // action == what action to take if the user confirms the alert
1474 service.copy_alert_dialog = function(evt, params, options, action) {
1475 if (angular.isArray(evt)) evt = evt[0];
1476 return egConfirmDialog.open(
1477 egCore.strings.COPY_ALERT_MSG_DIALOG_TITLE,
1478 evt.payload, // payload == alert message text
1479 { copy_barcode : params.copy_barcode,
1481 cancel : function() {}
1483 ).result.then(function() {
1484 options.override = true;
1485 return service[action](params, options);
1489 // action == what action to take if the user confirms the alert
1490 service.hold_capture_delay_dialog = function(evt, params, options, action) {
1491 if (angular.isArray(evt)) evt = evt[0];
1492 return $uibModal.open({
1493 templateUrl: './circ/checkin/t_hold_verify',
1495 ['$scope','$uibModalInstance','params',
1496 function($scope , $uibModalInstance , params) {
1497 $scope.copy_barcode = params.copy_barcode;
1498 $scope.capture = function() {
1499 params.capture = 'capture';
1500 $uibModalInstance.close();
1502 $scope.nocapture = function() {
1503 params.capture = 'nocapture';
1504 $uibModalInstance.close();
1506 $scope.cancel = function() { $uibModalInstance.dismiss(); };
1509 params : function() {
1515 return service[action](params, options);
1520 // check the barcode. If it's no good, show the warning dialog
1521 // Resolves on success, rejected on error
1522 service.test_barcode = function(bc) {
1524 var ok = service.check_barcode(bc);
1525 if (ok) return $q.when();
1527 egCore.audio.play('warning.circ.bad_barcode');
1528 return $uibModal.open({
1529 templateUrl: './circ/share/t_bad_barcode_dialog',
1531 ['$scope', '$uibModalInstance',
1532 function($scope, $uibModalInstance) {
1533 $scope.barcode = bc;
1534 $scope.ok = function() { $uibModalInstance.close() }
1535 $scope.cancel = function() { $uibModalInstance.dismiss() }
1540 // check() and checkdigit() copied directly
1541 // from chrome/content/util/barcode.js
1543 service.check_barcode = function(bc) {
1544 if (bc != Number(bc)) return false;
1546 // "16.00" == Number("16.00"), but the . is bad.
1547 // Throw out any barcode that isn't just digits
1548 if (bc.search(/\D/) != -1) return false;
1549 var last_digit = bc.substr(bc.length-1);
1550 var stripped_barcode = bc.substr(0,bc.length-1);
1551 return service.barcode_checkdigit(stripped_barcode).toString() == last_digit;
1554 service.barcode_checkdigit = function(bc) {
1555 var reverse_barcode = bc.toString().split('').reverse();
1556 var check_sum = 0; var multiplier = 2;
1557 for (var i = 0; i < reverse_barcode.length; i++) {
1558 var digit = reverse_barcode[i];
1559 var product = digit * multiplier; product = product.toString();
1561 for (var j = 0; j < product.length; j++) {
1562 temp_sum += Number( product[j] );
1564 check_sum += Number( temp_sum );
1565 multiplier = ( multiplier == 2 ? 1 : 2 );
1567 check_sum = check_sum.toString();
1568 var next_multiple_of_10 = (check_sum.match(/(\d*)\d$/)[1] * 10) + 10;
1569 var check_digit = next_multiple_of_10 - Number(check_sum);
1570 if (check_digit == 10) check_digit = 0;
1574 service.create_penalty = function(user_id) {
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;
1583 $scope.args = {penalty : 21}; // default to Note
1584 $scope.setPenalty = function(id) {
1587 $scope.ok = function(count) { $uibModalInstance.close($scope.args) }
1588 $scope.cancel = function($event) {
1589 $uibModalInstance.dismiss();
1590 $event.preventDefault();
1593 resolve : { staffPenalties : service.get_staff_penalty_types }
1596 var pen = new egCore.idl.ausp();
1598 pen.org_unit(egCore.auth.user().ws_ou());
1599 pen.note(args.note);
1600 if (args.initials) pen.note(args.note + ' [' + args.initials + ']');
1601 if (args.custom_penalty) {
1602 pen.standing_penalty(args.custom_penalty);
1604 pen.standing_penalty(args.penalty);
1606 pen.staff(egCore.auth.user().id());
1607 pen.set_date('now');
1608 return egCore.pcrud.create(pen);
1613 // assumes, for now anyway, penalty type is fleshed onto usr_penalty.
1614 service.edit_penalty = function(usr_penalty) {
1615 return $uibModal.open({
1616 templateUrl: './circ/share/t_new_message_dialog',
1618 ['$scope','$uibModalInstance','staffPenalties',
1619 function($scope , $uibModalInstance , staffPenalties) {
1620 $scope.focusNote = true;
1621 $scope.penalties = staffPenalties;
1622 $scope.require_initials = service.require_initials;
1624 penalty : usr_penalty.standing_penalty().id(),
1625 note : usr_penalty.note()
1627 $scope.setPenalty = function(id) { args.penalty = id; }
1628 $scope.ok = function(count) { $uibModalInstance.close($scope.args) }
1629 $scope.cancel = function($event) {
1630 $uibModalInstance.dismiss();
1631 $event.preventDefault();
1634 resolve : { staffPenalties : service.get_staff_penalty_types }
1637 usr_penalty.note(args.note);
1638 if (args.initials) usr_penalty.note(args.note + ' [' + args.initials + ']');
1639 usr_penalty.standing_penalty(args.penalty);
1640 return egCore.pcrud.update(usr_penalty);