2 * Checkin, checkout, and renew
5 angular.module('egCoreMod')
9 ['$modal','$q','egCore','egAlertDialog','egConfirmDialog',
10 function($modal , $q , egCore , egAlertDialog , egConfirmDialog) {
13 // auto-override these events after the first override
14 auto_override_checkout_events : {},
17 service.reset = function() {
18 service.auto_override_checkout_events = {};
21 // these events can be overridden by staff during checkout
22 service.checkout_overridable_events = [
23 'PATRON_EXCEEDS_OVERDUE_COUNT',
24 'PATRON_EXCEEDS_CHECKOUT_COUNT',
25 'PATRON_EXCEEDS_FINES',
27 'CIRC_EXCEEDS_COPY_RANGE',
28 'ITEM_DEPOSIT_REQUIRED',
29 'ITEM_RENTAL_FEE_REQUIRED',
30 'PATRON_EXCEEDS_LOST_COUNT',
31 'COPY_CIRC_NOT_ALLOWED',
38 // after the first override of any of these events,
39 // auto-override them in subsequent calls.
40 service.checkout_auto_override_after_first = [
41 'PATRON_EXCEEDS_OVERDUE_COUNT',
43 'PATRON_EXCEEDS_LOST_COUNT',
44 'PATRON_EXCEEDS_CHECKOUT_COUNT',
45 'PATRON_EXCEEDS_FINES'
49 // overridable during renewal
50 service.renew_overridable_events = [
51 'PATRON_EXCEEDS_OVERDUE_COUNT',
52 'PATRON_EXCEEDS_LOST_COUNT',
53 'PATRON_EXCEEDS_CHECKOUT_COUNT',
54 'PATRON_EXCEEDS_FINES',
55 'CIRC_EXCEEDS_COPY_RANGE',
56 'ITEM_DEPOSIT_REQUIRED',
57 'ITEM_RENTAL_FEE_REQUIRED',
59 'COPY_CIRC_NOT_ALLOWED',
62 'COPY_NEEDED_FOR_HOLD',
63 'MAX_RENEWALS_REACHED',
64 'CIRC_CLAIMS_RETURNED'
67 // these checkin events do not produce alerts when
68 // options.suppress_alerts is in effect.
69 service.checkin_suppress_overrides = [
73 'PATRON_ACCOUNT_EXPIRED',
75 'CIRC_CLAIMS_RETURNED',
78 'COPY_STATUS_LONG_OVERDUE',
79 'COPY_STATUS_MISSING',
80 'PATRON_EXCEEDS_FINES'
83 // these events can be overridden by staff during checkin
84 service.checkin_overridable_events =
85 service.checkin_suppress_overrides.concat([
86 'TRANSIT_CHECKIN_INTERVAL_BLOCK'
89 // Performs a checkout.
90 // Returns a promise resolved with the original params and options
91 // and the final checkout event (e.g. in the case of override).
92 // Rejected if the checkout cannot be completed.
94 // params : passed directly as arguments to the server API
95 // options : non-parameter controls. e.g. "override", "check_barcode"
96 service.checkout = function(params, options) {
97 if (!options) options = {};
99 console.debug('egCirc.checkout() : '
100 + js2JSON(params) + ' : ' + js2JSON(options));
102 var promise = options.check_barcode ?
103 service.test_barcode(params.copy_barcode) : $q.when();
105 // avoid re-check on override, etc.
106 delete options.check_barcode;
108 return promise.then(function() {
110 var method = 'open-ils.circ.checkout.full';
111 if (options.override) method += '.override';
113 return egCore.net.request(
114 'open-ils.circ', method, egCore.auth.token(), params
116 ).then(function(evt) {
118 if (angular.isArray(evt)) evt = evt[0];
120 return service.flesh_response_data('checkout', evt, params, options)
122 return service.handle_checkout_resp(evt, params, options);
124 .then(function(final_resp) {
125 return service.munge_resp_data(final_resp)
131 // Performs a renewal.
132 // Returns a promise resolved with the original params and options
133 // and the final checkout event (e.g. in the case of override)
134 // Rejected if the renewal cannot be completed.
135 service.renew = function(params, options) {
136 if (!options) options = {};
138 console.debug('egCirc.renew() : '
139 + js2JSON(params) + ' : ' + js2JSON(options));
141 var promise = options.check_barcode ?
142 service.test_barcode(params.copy_barcode) : $q.when();
144 // avoid re-check on override, etc.
145 delete options.check_barcode;
147 return promise.then(function() {
149 var method = 'open-ils.circ.renew';
150 if (options.override) method += '.override';
152 return egCore.net.request(
153 'open-ils.circ', method, egCore.auth.token(), params
155 ).then(function(evt) {
157 if (angular.isArray(evt)) evt = evt[0];
159 return service.flesh_response_data(
160 'renew', evt, params, options)
162 return service.handle_renew_resp(evt, params, options);
164 .then(function(final_resp) {
165 return service.munge_resp_data(final_resp)
171 // Performs a checkin
172 // Returns a promise resolved with the original params and options,
173 // plus the final checkin event (e.g. in the case of override).
174 // Rejected if the checkin cannot be completed.
175 service.checkin = function(params, options) {
176 if (!options) options = {};
178 console.debug('egCirc.checkin() : '
179 + js2JSON(params) + ' : ' + js2JSON(options));
181 var promise = options.check_barcode ?
182 service.test_barcode(params.copy_barcode) : $q.when();
184 // avoid re-check on override, etc.
185 delete options.check_barcode;
187 return promise.then(function() {
189 var method = 'open-ils.circ.checkin';
190 if (options.override) method += '.override';
192 return egCore.net.request(
193 'open-ils.circ', method, egCore.auth.token(), params
195 ).then(function(evt) {
197 if (angular.isArray(evt)) evt = evt[0];
198 return service.flesh_response_data(
199 'checkin', evt, params, options)
201 return service.handle_checkin_resp(evt, params, options);
203 .then(function(final_resp) {
204 return service.munge_resp_data(final_resp)
210 // provide consistent formatting of the final response data
211 service.munge_resp_data = function(final_resp) {
212 var data = final_resp.data = {};
214 if (!final_resp.evt) return;
216 var payload = final_resp.evt.payload;
217 if (!payload) return;
219 data.circ = payload.circ;
220 data.parent_circ = payload.parent_circ;
221 data.hold = payload.hold;
222 data.record = payload.record;
223 data.acp = payload.copy;
224 data.acn = payload.volume ? payload.volume : payload.copy.call_number();
225 data.au = payload.patron;
226 data.transit = payload.transit;
227 data.status = payload.status;
228 data.message = payload.message;
229 data.title = final_resp.evt.title;
230 data.author = final_resp.evt.author;
231 data.isbn = final_resp.evt.isbn;
232 data.route_to = final_resp.evt.route_to;
234 // for checkin, the mbts lives on the main circ
235 if (payload.circ && payload.circ.billable_transaction())
236 data.mbts = payload.circ.billable_transaction().summary();
238 // on renewals, the mbts lives on the parent circ
239 if (payload.parent_circ && payload.parent_circ.billable_transaction())
240 data.mbts = payload.parent_circ.billable_transaction().summary();
242 if (!data.route_to) {
244 data.route_to = data.transit.dest().shortname();
245 } else if (data.acp) {
246 data.route_to = data.acp.location().name();
253 service.handle_overridable_checkout_event = function(evt, params, options) {
255 if (options.override) {
256 // override attempt already made and failed.
257 // NOTE: I don't think we'll ever get here, since the
258 // override attempt should produce a perm failure...
259 console.debug('override failed: ' + evt.textcode);
264 if (service.auto_override_checkout_events[evt.textcode]) {
265 // user has already opted to override this type
266 // of event. Re-run the checkout w/ override.
267 options.override = true;
268 return service.checkout(params, options);
271 // Ask the user if they would like to override this event.
272 // Some events offer a stock override dialog, while others
273 // require additional context.
275 switch(evt.textcode) {
276 case 'COPY_NOT_AVAILABLE':
277 return service.copy_not_avail_dialog(evt, params, options);
278 case 'COPY_ALERT_MESSAGE':
279 return service.copy_alert_dialog(evt, params, options, 'checkout');
281 return service.override_dialog(evt, params, options, 'checkout');
285 service.handle_overridable_renew_event = function(evt, params, options) {
287 if (options.override) {
288 // override attempt already made and failed.
289 // NOTE: I don't think we'll ever get here, since the
290 // override attempt should produce a perm failure...
291 console.debug('override failed: ' + evt.textcode);
296 // renewal auto-overrides are the same as checkout
297 if (service.auto_override_checkout_events[evt.textcode]) {
298 // user has already opted to override this type
299 // of event. Re-run the renew w/ override.
300 options.override = true;
301 return service.renew(params, options);
304 // Ask the user if they would like to override this event.
305 // Some events offer a stock override dialog, while others
306 // require additional context.
308 switch(evt.textcode) {
309 case 'COPY_ALERT_MESSAGE':
310 return service.copy_alert_dialog(evt, params, options, 'renew');
312 return service.override_dialog(evt, params, options, 'renew');
317 service.handle_overridable_checkin_event = function(evt, params, options) {
319 if (options.override) {
320 // override attempt already made and failed.
321 // NOTE: I don't think we'll ever get here, since the
322 // override attempt should produce a perm failure...
323 console.debug('override failed: ' + evt.textcode);
328 if (options.suppress_checkin_popups
329 && service.checkin_suppress_overrides.indexOf(evt.textcode) > -1) {
330 // Event is suppressed. Re-run the checkin w/ override.
331 options.override = true;
332 return service.checkin(params, options);
335 // Ask the user if they would like to override this event.
336 // Some events offer a stock override dialog, while others
337 // require additional context.
339 switch(evt.textcode) {
340 case 'COPY_ALERT_MESSAGE':
341 return service.copy_alert_dialog(evt, params, options, 'checkin');
343 return service.override_dialog(evt, params, options, 'checkin');
348 service.handle_renew_resp = function(evt, params, options) {
350 var final_resp = {evt : evt, params : params, options : options};
352 // track the barcode regardless of whether it refers to a copy
353 evt.copy_barcode = params.copy_barcode;
355 // Overridable Events
356 if (service.renew_overridable_events.indexOf(evt.textcode) > -1)
357 return service.handle_overridable_renew_event(evt, params, options);
360 switch (evt.textcode) {
362 return $q.when(final_resp);
364 case 'COPY_IN_TRANSIT':
365 case 'PATRON_CARD_INACTIVE':
366 case 'PATRON_INACTIVE':
367 case 'PATRON_ACCOUNT_EXPIRED':
368 case 'CIRC_CLAIMS_RETURNED':
369 return service.exit_alert(
370 egCore.strings[evt.textcode],
371 {barcode : params.copy_barcode}
375 return service.exit_alert(
376 egCore.strings[evt.textcode],
377 {permission : evt.ilsperm}
381 return service.exit_alert(
382 egCore.strings.CHECKOUT_FAILED_GENERIC, {
383 barcode : params.copy_barcode,
384 textcode : evt.textcode,
392 service.handle_checkout_resp = function(evt, params, options) {
394 var final_resp = {evt : evt, params : params, options : options};
396 // track the barcode regardless of whether it refers to a copy
397 evt.copy_barcode = params.copy_barcode;
399 // Overridable Events
400 if (service.checkout_overridable_events.indexOf(evt.textcode) > -1)
401 return service.handle_overridable_checkout_event(evt, params, options);
404 switch (evt.textcode) {
406 return $q.when(final_resp);
408 case 'ITEM_NOT_CATALOGED':
409 return service.precat_dialog(params, options);
411 case 'OPEN_CIRCULATION_EXISTS':
412 return service.circ_exists_dialog(evt, params, options);
414 case 'COPY_IN_TRANSIT':
415 return service.copy_in_transit_dialog(evt, params, options);
417 case 'PATRON_CARD_INACTIVE':
418 case 'PATRON_INACTIVE':
419 case 'PATRON_ACCOUNT_EXPIRED':
420 case 'CIRC_CLAIMS_RETURNED':
421 return service.exit_alert(
422 egCore.strings[evt.textcode],
423 {barcode : params.copy_barcode}
427 return service.exit_alert(
428 egCore.strings[evt.textcode],
429 {permission : evt.ilsperm}
433 return service.exit_alert(
434 egCore.strings.CHECKOUT_FAILED_GENERIC, {
435 barcode : params.copy_barcode,
436 textcode : evt.textcode,
443 // returns a promise resolved with the list of circ mods
444 service.get_circ_mods = function() {
446 return $q.when(egCore.env.ccm.list);
448 return egCore.pcrud.retrieveAll('ccm', null, {atomic : true})
449 .then(function(list) {
450 egCore.env.absorbList(list, 'ccm');
455 // returns a promise resolved with the list of noncat types
456 service.get_noncat_types = function() {
458 return $q.when(egCore.env.cnct.list);
460 return egCore.pcrud.search('cnct',
462 egCore.org.fullPath(egCore.auth.user().ws_ou(), true)},
463 null, {atomic : true}
464 ).then(function(list) {
465 egCore.env.absorbList(list, 'cnct');
470 service.get_staff_penalty_types = function() {
472 return $q.when(egCore.env.csp.list);
473 return egCore.pcrud.search(
474 // id <= 100 are reserved for system use
475 'csp', {id : {'>': 100}}, {}, {atomic : true})
476 .then(function(penalties) {
477 return egCore.env.absorbList(penalties, 'csp').list;
481 // ideally all of these data should be returned with the response,
482 // but until then, grab what we need.
483 service.flesh_response_data = function(action, evt, params, options) {
486 if (!evt || !(payload = evt.payload)) return $q.when();
488 promises.push(service.flesh_copy_location(payload.copy));
491 service.flesh_copy_status(payload.copy)
494 // copy is in transit, but no transit was delivered
495 // in the payload. Do this here instead of below to
496 // ensure consistent copy status fleshiness
497 if (!payload.transit && payload.copy.status().id() == 6) { // in-transit
498 return service.find_copy_transit(evt, params, options)
499 .then(function(trans) {
501 trans.source(egCore.org.get(trans.source()));
502 trans.dest(egCore.org.get(trans.dest()));
503 payload.transit = trans;
511 // local flesh transit
512 if (transit = payload.transit) {
513 transit.source(egCore.org.get(transit.source()));
514 transit.dest(egCore.org.get(transit.dest()));
517 // TODO: renewal responses should include the patron
518 if (!payload.patron && payload.circ) {
520 egCore.pcrud.retrieve('au', payload.circ.usr())
521 .then(function(user) {payload.patron = user})
525 // extract precat values
526 evt.title = payload.record ? payload.record.title() :
527 (payload.copy ? payload.copy.dummy_title() : null);
529 evt.author = payload.record ? payload.record.author() :
530 (payload.copy ? payload.copy.dummy_author() : null);
532 evt.isbn = payload.record ? payload.record.isbn() :
533 (payload.copy ? payload.copy.dummy_isbn() : null);
535 return $q.all(promises);
538 // fetches the full list of copy statuses
539 service.flesh_copy_status = function(copy) {
540 if (!copy) return $q.when();
542 return $q.when(copy.status(egCore.env.ccs.map[copy.status()]));
543 return egCore.pcrud.retrieveAll('ccs', {}, {atomic : true}).then(
545 egCore.env.absorbList(list, 'ccs');
546 copy.status(egCore.env.ccs.map[copy.status()]);
551 // there may be *many* copy locations and we may be handling items
552 // for other locations. Fetch copy locations as-needed and cache.
553 service.flesh_copy_location = function(copy) {
554 if (!copy) return $q.when();
555 if (angular.isObject(copy.location())) return $q.when(copy);
556 if (egCore.env.acpl) {
557 if (egCore.env.acpl.map[copy.location()]) {
558 copy.location(egCore.env.acpl.map[copy.location()]);
559 return $q.when(copy);
562 return egCore.pcrud.retrieve('acpl', copy.location())
563 .then(function(loc) {
564 egCore.env.absorbList([loc], 'acpl'); // append to cache
571 // fetch org unit addresses as needed.
572 service.get_org_addr = function(org_id, addr_type) {
573 var org = egCore.org.get(org_id);
574 var addr_id = org[addr_type]();
576 if (egCore.env.aoa && egCore.env.aoa.map[addr_id])
577 return $q.when(egCore.env.aoa.map[addr_id]);
579 return egCore.pcrud.retrieve('aoa', addr_id).then(function(addr) {
580 egCore.env.absorbList([addr], 'aoa');
581 return egCore.env.aoa.map[addr_id];
585 service.exit_alert = function(msg, scope) {
586 return egAlertDialog.open(msg, scope).result.then(
587 function() {return $q.reject()});
590 // opens a dialog asking the user if they would like to override
591 // the returned event.
592 service.override_dialog = function(evt, params, options, action) {
594 templateUrl: './circ/share/t_event_override_dialog',
596 ['$scope', '$modalInstance',
597 function($scope, $modalInstance) {
599 $scope.auto_override =
600 service.checkout_auto_override_after_first.indexOf(evt.textcode) > -1;
601 $scope.copy_barcode = params.copy_barcode; // may be null
602 $scope.ok = function() { $modalInstance.close() }
603 $scope.cancel = function ($event) {
604 $modalInstance.dismiss();
605 $event.preventDefault();
610 options.override = true;
612 if (action == 'checkin') {
613 return service.checkin(params, options);
616 // checkout/renew support override-after-first
617 if (service.checkout_auto_override_after_first.indexOf(evt.textcode) > -1)
618 service.auto_override_checkout_events[evt.textcode] = true;
620 return service[action](params, options);
625 service.copy_not_avail_dialog = function(evt, params, options) {
627 templateUrl: './circ/share/t_copy_not_avail_dialog',
629 ['$scope','$modalInstance','copyStatus',
630 function($scope , $modalInstance , copyStatus) {
631 $scope.copyStatus = copyStatus;
632 $scope.ok = function() {$modalInstance.close()}
633 $scope.cancel = function() {$modalInstance.dismiss()}
636 copyStatus : function() {
637 return egCore.pcrud.retrieve(
638 'ccs', evt.payload.status());
643 options.override = true;
644 return service.checkout(params, options);
649 // Opens a dialog allowing the user to fill in the desired non-cat count.
650 // Unlike other dialogs, which kickoff circ actions internally
651 // as a result of events, this dialog does not kick off any circ
652 // actions. It just collects the count and and resolves the promise.
654 // This assumes the caller has already handled the noncat-type
655 // selection and just needs to collect the count info.
656 service.noncat_dialog = function(params, options) {
657 var noncatMax = 99; // hard-coded max
659 // the caller should presumably have fetched the noncat_types via
660 // our API already, but fetch them again (from cache) to be safe.
661 return service.get_noncat_types().then(function() {
663 params.noncat = true;
664 var type = egCore.env.cnct.map[params.noncat_type];
667 templateUrl: './circ/share/t_noncat_dialog',
669 ['$scope', '$modalInstance',
670 function($scope, $modalInstance) {
671 $scope.focusMe = true;
674 $scope.noncatMax = noncatMax;
675 $scope.ok = function(count) { $modalInstance.close(count) }
676 $scope.cancel = function ($event) {
677 $modalInstance.dismiss()
678 $event.preventDefault();
683 if (count && count > 0 && count <= noncatMax) {
684 // NOTE: in Chrome, form validation ensure a valid number
685 params.noncat_count = count;
686 return $q.when(params);
695 // Opens a dialog allowing the user to fill in pre-cat copy info.
696 service.precat_dialog = function(params, options) {
699 templateUrl: './circ/share/t_precat_dialog',
701 ['$scope', '$modalInstance', 'circMods',
702 function($scope, $modalInstance, circMods) {
703 $scope.focusMe = true;
704 $scope.precatArgs = {
705 copy_barcode : params.copy_barcode,
706 circ_modifier : circMods.length ? circMods[0].code() : null
708 $scope.circModifiers = circMods;
709 $scope.ok = function(args) { $modalInstance.close(args) }
710 $scope.cancel = function () { $modalInstance.dismiss() }
713 circMods : function() {
714 return service.get_circ_mods();
719 if (!args || !args.dummy_title) return $q.reject();
720 angular.forEach(args, function(val, key) {params[key] = val});
721 params.precat = true;
722 return service.checkout(params, options);
727 // find the open transit for the given copy barcode; flesh the org
729 service.find_copy_transit = function(evt, params, options) {
731 if (evt && evt.payload && evt.payload.transit)
732 return $q.when(evt.payload.transit);
734 return egCore.pcrud.search('atc',
735 { dest_recv_time : null},
737 flesh_fields : {atc : ['target_copy']},
741 barcode : params.copy_barcode,
747 order_by : {atc : 'source_send_time desc'},
749 ).then(function(transit) {
750 transit.source(egCore.org.get(transit.source()));
751 transit.dest(egCore.org.get(transit.dest()));
756 service.copy_in_transit_dialog = function(evt, params, options) {
758 templateUrl: './circ/share/t_copy_in_transit_dialog',
760 ['$scope','$modalInstance','transit',
761 function($scope , $modalInstance , transit) {
762 $scope.transit = transit;
763 $scope.ok = function() { $modalInstance.close(transit) }
764 $scope.cancel = function() { $modalInstance.dismiss() }
767 // fetch the conflicting open transit w/ fleshed copy
768 transit : function() {
769 return service.find_copy_transit(evt, params, options);
774 // user chose to abort the transit then checkout
775 return service.abort_transit(transit.id())
777 return service.checkout(params, options);
783 service.abort_transit = function(transit_id) {
784 return egCore.net.request(
786 'open-ils.circ.transit.abort',
787 egCore.auth.token(), {transitid : transit_id}
788 ).then(function(resp) {
789 if (evt = egCore.evt.parse(resp)) {
797 service.last_copy_circ = function(copy_id) {
798 return egCore.pcrud.search('circ',
799 {target_copy : copy_id},
800 {order_by : {circ : 'xact_start desc' }, limit : 1}
804 service.circ_exists_dialog = function(evt, params, options) {
806 if (!evt.payload.old_circ) {
807 return egCore.net.request(
809 'open-ils.search.asset.copy.fleshed2.find_by_barcode',
811 ).then(function(resp){
813 if (egCore.evt.parse(resp)) {
814 console.error(egCore.evt.parse(resp));
816 evt.payload.old_circ = resp.circulations()[0];
817 return service.circ_exists_dialog_impl( evt, params, options );
821 return service.circ_exists_dialog_impl( evt, params, options );
825 service.circ_exists_dialog_impl = function (evt, params, options) {
827 var openCirc = evt.payload.old_circ;
828 var sameUser = openCirc.usr() == params.patron_id;
831 templateUrl: './circ/share/t_circ_exists_dialog',
833 ['$scope','$modalInstance',
834 function($scope , $modalInstance) {
835 $scope.circDate = openCirc.xact_start();
836 $scope.sameUser = sameUser;
837 $scope.ok = function() { $modalInstance.close() }
838 $scope.cancel = function($event) {
839 $modalInstance.dismiss();
840 $event.preventDefault(); // form, avoid calling ok();
846 return service.checkin(
847 {barcode : params.copy_barcode, noop : true}
848 ).then(function(checkin_resp) {
849 if (checkin_resp.evt.textcode == 'SUCCESS') {
850 return service.checkout(params, options);
852 alert(egCore.evt.parse(evt));
860 service.batch_backdate = function(circ_ids, backdate) {
861 return egCore.net.request(
863 'open-ils.circ.post_checkin_backdate.batch',
864 egCore.auth.token(), circ_ids, backdate);
867 service.backdate_dialog = function(circ_ids) {
869 templateUrl: './circ/share/t_backdate_dialog',
871 ['$scope','$modalInstance',
872 function($scope , $modalInstance) {
874 var today = new Date();
876 num_circs : circ_ids.length,
881 $scope.$watch('dialog.backdate', function(newval) {
882 if (newval && newval > today)
883 $scope.dialog.backdate = today;
887 $scope.cancel = function() {
888 $modalInstance.dismiss();
891 $scope.ok = function() {
893 var bd = $scope.dialog.backdate.toISOString().replace(/T.*/,'');
894 service.batch_backdate(circ_ids, bd)
896 function() { // on complete
897 $modalInstance.close({backdate : bd});
900 function(resp) { // on response
901 console.debug('backdate returned ' + resp);
903 $scope.num_processed++;
905 console.error(egCore.evt.parse(resp));
914 service.mark_claims_returned = function(barcode, date, override) {
916 var method = 'open-ils.circ.circulation.set_claims_returned';
917 if (override) method += '.override';
919 console.debug('claims returned ' + method);
921 return egCore.net.request(
922 'open-ils.circ', method, egCore.auth.token(),
923 {barcode : barcode, backdate : date})
925 .then(function(resp) {
927 if (resp == 1) { // success
928 console.debug('claims returned succeeded for ' + barcode);
931 } else if (evt = egCore.evt.parse(resp)) {
932 console.debug('claims returned failed: ' + evt.toString());
934 if (evt.textcode == 'PATRON_EXCEEDS_CLAIMS_RETURN_COUNT') {
935 // TODO check perms before offering override option?
937 if (override) return;// just to be safe
939 return egConfirmDialog.open(
940 egCore.strings.TOO_MANY_CLAIMS_RETURNED, '', {}
941 ).result.then(function() {
942 return service.mark_claims_returned(barcode, date, true);
946 if (evt.textcode == 'PERM_FAILURE') {
947 console.error('claims returned permission denied')
948 // TODO: auth override dialog?
954 service.mark_claims_returned_dialog = function(copy_barcodes) {
955 if (!copy_barcodes.length) return;
958 templateUrl: './circ/share/t_mark_claims_returned_dialog',
960 ['$scope','$modalInstance',
961 function($scope , $modalInstance) {
963 var today = new Date();
965 barcodes : copy_barcodes,
969 $scope.$watch('args.date', function(newval) {
970 if (newval && newval > today)
971 $scope.args.backdate = today;
974 $scope.cancel = function() {$modalInstance.dismiss()}
975 $scope.ok = function() {
977 var date = $scope.args.date.toISOString().replace(/T.*/,'');
979 var deferred = $q.defer();
981 // serialize the action on each barcode so that the
982 // caller will never see multiple alerts at the same time.
983 function mark_one() {
984 var bc = copy_barcodes.pop();
987 $modalInstance.close();
991 // finally -> continue even when one fails
992 service.mark_claims_returned(bc, date)
993 .finally(function(barcode) {
994 if (barcode) deferred.notify(barcode);
998 mark_one(); // kick it off
999 return deferred.promise;
1005 // serially checks in each barcode with claims_never_checked_out set
1006 // returns promise, notified on each barcode, resolved after all
1007 // checkins are complete.
1008 service.mark_claims_never_checked_out = function(barcodes) {
1009 if (!barcodes.length) return;
1011 var deferred = $q.defer();
1012 egConfirmDialog.open(
1013 egCore.strings.MARK_NEVER_CHECKED_OUT, '', {barcodes : barcodes}
1015 ).result.then(function() {
1016 function mark_one() {
1017 var bc = barcodes.pop();
1019 if (!bc) { // all done
1025 {claims_never_checked_out : true, copy_barcode : bc})
1026 .finally(function() {
1027 deferred.notify(bc);
1034 return deferred.promise;
1037 service.mark_damaged = function(copy_ids) {
1038 return egConfirmDialog.open(
1039 egCore.strings.MARK_DAMAGED_CONFIRM, '',
1040 { num_items : copy_ids.length,
1042 cancel : function() {}
1045 ).result.then(function() {
1047 angular.forEach(copy_ids, function(copy_id) {
1051 'open-ils.circ.mark_item_damaged',
1052 egCore.auth.token(), copy_id
1053 ).then(function(resp) {
1054 if (evt = egCore.evt.parse(resp)) {
1055 console.error('mark damaged failed: ' + evt);
1061 return $q.all(promises);
1065 service.mark_missing = function(copy_ids) {
1066 return egConfirmDialog.open(
1067 egCore.strings.MARK_MISSING_CONFIRM, '',
1068 { num_items : copy_ids.length,
1070 cancel : function() {}
1072 ).result.then(function() {
1074 angular.forEach(copy_ids, function(copy_id) {
1078 'open-ils.circ.mark_item_missing',
1079 egCore.auth.token(), copy_id
1080 ).then(function(resp) {
1081 if (evt = egCore.evt.parse(resp)) {
1082 console.error('mark missing failed: ' + evt);
1088 return $q.all(promises);
1094 // Mark circulations as lost via copy barcode. As each item is
1095 // processed, the returned promise is notified of the barcode.
1096 // No confirmation dialog is presented.
1097 service.mark_lost = function(copy_barcodes) {
1098 var deferred = $q.defer();
1101 angular.forEach(copy_barcodes, function(barcode) {
1105 'open-ils.circ.circulation.set_lost',
1106 egCore.auth.token(), {barcode : barcode}
1107 ).then(function(resp) {
1108 if (evt = egCore.evt.parse(resp)) {
1109 console.error("Mark lost failed: " + evt.toString());
1112 // inform the caller as each item is processed
1113 deferred.notify(barcode);
1118 $q.all(promises).then(function() {deferred.resolve()});
1119 return deferred.promise;
1122 service.abort_transits = function(transit_ids) {
1123 return egConfirmDialog.open(
1124 egCore.strings.ABORT_TRANSIT_CONFIRM, '',
1125 { num_transits : transit_ids.length,
1127 cancel : function() {}
1130 ).result.then(function() {
1132 angular.forEach(transit_ids, function(transit_id) {
1136 'open-ils.circ.transit.abort',
1137 egCore.auth.token(), {transitid : transit_id}
1138 ).then(function(resp) {
1139 if (evt = egCore.evt.parse(resp)) {
1140 console.error('abort transit failed: ' + evt);
1146 return $q.all(promises);
1152 // alert when copy location alert_message is set.
1153 // This does not affect processing, it only produces a click-through
1154 service.handle_checkin_loc_alert = function(evt, params, options) {
1156 var copy = evt && evt.payload ? evt.payload.copy : null;
1158 if (copy && !options.suppress_checkin_popups
1159 && copy.location().checkin_alert() == 't') {
1161 return egAlertDialog.open(
1162 egCore.strings.LOCATION_ALERT_MSG, {copy : copy}).result;
1168 service.handle_checkin_resp = function(evt, params, options) {
1170 var final_resp = {evt : evt, params : params, options : options};
1172 var copy, hold, transit;
1174 copy = evt.payload.copy;
1175 hold = evt.payload.hold;
1176 transit = evt.payload.transit;
1179 // track the barcode regardless of whether it's valid
1180 evt.copy_barcode = params.copy_barcode;
1182 console.debug('checkin event ' + evt.textcode);
1184 if (service.checkin_overridable_events.indexOf(evt.textcode) > -1)
1185 return service.handle_overridable_checkin_event(evt, params, options);
1187 switch (evt.textcode) {
1192 switch(Number(copy.status().id())) {
1194 case 0: /* AVAILABLE */
1195 case 4: /* MISSING */
1196 case 7: /* RESHELVING */
1198 // see if the copy location requires an alert
1199 return service.handle_checkin_loc_alert(evt, params, options)
1200 .then(function() {return final_resp});
1202 case 8: /* ON HOLDS SHELF */
1207 if (hold.pickup_lib() == egCore.auth.user().ws_ou()) {
1208 // inform user if the item is on the local holds shelf
1210 evt.route_to = egCore.strings.ROUTE_TO_HOLDS_SHELF;
1211 return service.route_dialog(
1212 './circ/share/t_hold_shelf_dialog',
1213 evt, params, options
1214 ).then(function() { return final_resp });
1217 // normally, if the hold was on the shelf at a
1218 // different location, it would be put into
1219 // transit, resulting in a ROUTE_ITEM event.
1220 return $q.when(final_resp);
1224 console.error('checkin: item on holds shelf, '
1225 + 'but hold info not returned from checkin');
1226 return $q.when(final_resp);
1229 case 11: /* CATALOGING */
1230 evt.route_to = egCore.strings.ROUTE_TO_CATALOGING;
1231 return $q.when(final_resp);
1233 case 15: /* ON_RESERVATION_SHELF */
1234 // TODO: show booking reservation dialog
1235 return $q.when(final_resp);
1238 console.error('Unhandled checkin copy status: '
1239 + copy.status().id() + ' : ' + copy.status().name());
1240 return $q.when(final_resp);
1244 return service.route_dialog(
1245 './circ/share/t_transit_dialog',
1246 evt, params, options
1247 ).then(function() { return final_resp });
1249 case 'ASSET_COPY_NOT_FOUND':
1250 return egAlertDialog.open(
1251 egCore.strings.UNCAT_ALERT_DIALOG, params)
1252 .result.then(function() {return final_resp});
1254 case 'ITEM_NOT_CATALOGED':
1255 evt.route_to = egCore.strings.ROUTE_TO_CATALOGING;
1256 if (options.no_precat_alert)
1257 return $q.when(final_resp);
1258 return egAlertDialog.open(
1259 egCore.strings.PRECAT_CHECKIN_MSG, params)
1260 .result.then(function() {return final_resp});
1263 console.warn('unhandled checkin response : ' + evt.textcode);
1264 return $q.when(final_resp);
1268 // collect transit, address, and hold info that's not already
1269 // included in responses.
1270 service.collect_route_data = function(tmpl, evt, params, options) {
1274 if (evt.org && !tmpl.match(/hold_shelf/)) {
1276 service.get_org_addr(evt.org, 'holds_address')
1277 .then(function(addr) { data.address = addr })
1281 if (evt.payload.hold) {
1283 egCore.pcrud.retrieve('au',
1284 evt.payload.hold.usr(), {
1286 flesh_fields : {'au' : ['card']}
1288 ).then(function(patron) {data.patron = patron})
1292 if (!tmpl.match(/hold_shelf/)) {
1294 service.find_copy_transit(evt, params, options)
1295 .then(function(trans) {data.transit = trans})
1299 return $q.all(promises).then(function() { return data });
1302 service.route_dialog = function(tmpl, evt, params, options) {
1304 return service.collect_route_data(tmpl, evt, params, options)
1305 .then(function(data) {
1307 // All actions flow from the print data
1309 var print_context = {
1310 copy : egCore.idl.toHash(evt.payload.copy),
1316 // route_dialog includes the "route to holds shelf"
1317 // dialog, which has no transit
1318 print_context.transit = egCore.idl.toHash(data.transit);
1319 print_context.dest_address = egCore.idl.toHash(data.address);
1320 print_context.dest_location =
1321 egCore.idl.toHash(egCore.org.get(data.transit.dest()));
1325 print_context.hold = egCore.idl.toHash(evt.payload.hold);
1326 print_context.patron = egCore.idl.toHash(data.patron);
1329 function print_transit() {
1330 var template = data.transit ?
1331 (data.patron ? 'hold_transit_slip' : 'transit_slip') :
1334 return egCore.print.print({
1335 context : 'default',
1336 template : template,
1337 scope : print_context
1341 // when auto-print is on, skip the dialog and go straight
1343 if (options.auto_print_holds_transits)
1344 return print_transit();
1346 return $modal.open({
1349 '$scope','$modalInstance',
1350 function($scope , $modalInstance) {
1352 $scope.today = new Date();
1354 // copy the print scope into the dialog scope
1355 angular.forEach(print_context, function(val, key) {
1359 $scope.ok = function() {$modalInstance.close()}
1361 $scope.print = function() {
1362 $modalInstance.close();
1371 // action == what action to take if the user confirms the alert
1372 service.copy_alert_dialog = function(evt, params, options, action) {
1373 return egConfirmDialog.open(
1374 egCore.strings.COPY_ALERT_MSG_DIALOG_TITLE,
1375 evt.payload, // payload == alert message text
1376 { copy_barcode : params.copy_barcode,
1378 cancel : function() {}
1380 ).result.then(function() {
1381 options.override = true;
1382 return service[action](params, options);
1386 // check the barcode. If it's no good, show the warning dialog
1387 // Resolves on success, rejected on error
1388 service.test_barcode = function(bc) {
1390 var ok = service.check_barcode(bc);
1391 if (ok) return $q.when();
1393 return $modal.open({
1394 templateUrl: './circ/share/t_bad_barcode_dialog',
1396 ['$scope', '$modalInstance',
1397 function($scope, $modalInstance) {
1398 $scope.barcode = bc;
1399 $scope.ok = function() { $modalInstance.close() }
1400 $scope.cancel = function() { $modalInstance.dismiss() }
1405 // check() and checkdigit() copied directly
1406 // from chrome/content/util/barcode.js
1408 service.check_barcode = function(bc) {
1409 if (bc != Number(bc)) return false;
1411 // "16.00" == Number("16.00"), but the . is bad.
1412 // Throw out any barcode that isn't just digits
1413 if (bc.search(/\D/) != -1) return false;
1414 var last_digit = bc.substr(bc.length-1);
1415 var stripped_barcode = bc.substr(0,bc.length-1);
1416 return service.barcode_checkdigit(stripped_barcode).toString() == last_digit;
1419 service.barcode_checkdigit = function(bc) {
1420 var reverse_barcode = bc.toString().split('').reverse();
1421 var check_sum = 0; var multiplier = 2;
1422 for (var i = 0; i < reverse_barcode.length; i++) {
1423 var digit = reverse_barcode[i];
1424 var product = digit * multiplier; product = product.toString();
1426 for (var j = 0; j < product.length; j++) {
1427 temp_sum += Number( product[j] );
1429 check_sum += Number( temp_sum );
1430 multiplier = ( multiplier == 2 ? 1 : 2 );
1432 check_sum = check_sum.toString();
1433 var next_multiple_of_10 = (check_sum.match(/(\d*)\d$/)[1] * 10) + 10;
1434 var check_digit = next_multiple_of_10 - Number(check_sum);
1435 if (check_digit == 10) check_digit = 0;
1439 service.create_penalty = function(user_id) {
1440 return $modal.open({
1441 templateUrl: './circ/share/t_new_message_dialog',
1443 ['$scope','$modalInstance','staffPenalties',
1444 function($scope , $modalInstance , staffPenalties) {
1445 $scope.focusNote = true;
1446 $scope.penalties = staffPenalties;
1447 $scope.args = {penalty : 21}; // default to Note
1448 $scope.setPenalty = function(id) {
1451 $scope.ok = function(count) { $modalInstance.close($scope.args) }
1452 $scope.cancel = function($event) {
1453 $modalInstance.dismiss();
1454 $event.preventDefault();
1457 resolve : { staffPenalties : service.get_staff_penalty_types }
1460 var pen = new egCore.idl.ausp();
1462 pen.org_unit(egCore.auth.user().ws_ou());
1463 pen.note(args.note);
1464 if (args.custom_penalty) {
1465 pen.standing_penalty(args.custom_penalty);
1467 pen.standing_penalty(args.penalty);
1469 pen.staff(egCore.auth.user().id());
1470 pen.set_date('now');
1471 return egCore.pcrud.create(pen);
1476 // assumes, for now anyway, penalty type is fleshed onto usr_penalty.
1477 service.edit_penalty = function(usr_penalty) {
1478 return $modal.open({
1479 templateUrl: './circ/share/t_new_message_dialog',
1481 ['$scope','$modalInstance','staffPenalties',
1482 function($scope , $modalInstance , staffPenalties) {
1483 $scope.focusNote = true;
1484 $scope.penalties = staffPenalties;
1486 penalty : usr_penalty.standing_penalty().id(),
1487 note : usr_penalty.note()
1489 $scope.setPenalty = function(id) { args.penalty = id; }
1490 $scope.ok = function(count) { $modalInstance.close($scope.args) }
1491 $scope.cancel = function($event) {
1492 $modalInstance.dismiss();
1493 $event.preventDefault();
1496 resolve : { staffPenalties : service.get_staff_penalty_types }
1499 usr_penalty.note(args.note);
1500 usr_penalty.standing_penalty(args.penalty);
1501 return egCore.pcrud.update(usr_penalty);