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 : {},
15 require_initials : false
18 egCore.startup.go().finally(function() {
20 'ui.staff.require_initials.patron_standing_penalty'
21 ]).then(function(set) {
22 service.require_initials = Boolean(set['ui.staff.require_initials.patron_standing_penalty']);
26 service.reset = function() {
27 service.auto_override_checkout_events = {};
30 // these events can be overridden by staff during checkout
31 service.checkout_overridable_events = [
32 'PATRON_EXCEEDS_OVERDUE_COUNT',
33 'PATRON_EXCEEDS_CHECKOUT_COUNT',
34 'PATRON_EXCEEDS_FINES',
36 'CIRC_EXCEEDS_COPY_RANGE',
37 'ITEM_DEPOSIT_REQUIRED',
38 'ITEM_RENTAL_FEE_REQUIRED',
39 'PATRON_EXCEEDS_LOST_COUNT',
40 'COPY_CIRC_NOT_ALLOWED',
47 // after the first override of any of these events,
48 // auto-override them in subsequent calls.
49 service.checkout_auto_override_after_first = [
50 'PATRON_EXCEEDS_OVERDUE_COUNT',
52 'PATRON_EXCEEDS_LOST_COUNT',
53 'PATRON_EXCEEDS_CHECKOUT_COUNT',
54 'PATRON_EXCEEDS_FINES'
58 // overridable during renewal
59 service.renew_overridable_events = [
60 'PATRON_EXCEEDS_OVERDUE_COUNT',
61 'PATRON_EXCEEDS_LOST_COUNT',
62 'PATRON_EXCEEDS_CHECKOUT_COUNT',
63 'PATRON_EXCEEDS_FINES',
64 'CIRC_EXCEEDS_COPY_RANGE',
65 'ITEM_DEPOSIT_REQUIRED',
66 'ITEM_RENTAL_FEE_REQUIRED',
68 'COPY_CIRC_NOT_ALLOWED',
71 'COPY_NEEDED_FOR_HOLD',
72 'MAX_RENEWALS_REACHED',
73 'CIRC_CLAIMS_RETURNED'
76 // these checkin events do not produce alerts when
77 // options.suppress_alerts is in effect.
78 service.checkin_suppress_overrides = [
82 'PATRON_ACCOUNT_EXPIRED',
84 'CIRC_CLAIMS_RETURNED',
87 'COPY_STATUS_LONG_OVERDUE',
88 'COPY_STATUS_MISSING',
89 'PATRON_EXCEEDS_FINES'
92 // these events can be overridden by staff during checkin
93 service.checkin_overridable_events =
94 service.checkin_suppress_overrides.concat([
95 'TRANSIT_CHECKIN_INTERVAL_BLOCK'
98 // Performs a checkout.
99 // Returns a promise resolved with the original params and options
100 // and the final checkout event (e.g. in the case of override).
101 // Rejected if the checkout cannot be completed.
103 // params : passed directly as arguments to the server API
104 // options : non-parameter controls. e.g. "override", "check_barcode"
105 service.checkout = function(params, options) {
106 if (!options) options = {};
108 console.debug('egCirc.checkout() : '
109 + js2JSON(params) + ' : ' + js2JSON(options));
111 var promise = options.check_barcode ?
112 service.test_barcode(params.copy_barcode) : $q.when();
114 // avoid re-check on override, etc.
115 delete options.check_barcode;
117 return promise.then(function() {
119 var method = 'open-ils.circ.checkout.full';
120 if (options.override) method += '.override';
122 return egCore.net.request(
123 'open-ils.circ', method, egCore.auth.token(), params
125 ).then(function(evt) {
127 if (!angular.isArray(evt)) evt = [evt];
129 return service.flesh_response_data('checkout', evt, params, options)
131 return service.handle_checkout_resp(evt, params, options);
133 .then(function(final_resp) {
134 return service.munge_resp_data(final_resp)
140 // Performs a renewal.
141 // Returns a promise resolved with the original params and options
142 // and the final checkout event (e.g. in the case of override)
143 // Rejected if the renewal cannot be completed.
144 service.renew = function(params, options) {
145 if (!options) options = {};
147 console.debug('egCirc.renew() : '
148 + js2JSON(params) + ' : ' + js2JSON(options));
150 var promise = options.check_barcode ?
151 service.test_barcode(params.copy_barcode) : $q.when();
153 // avoid re-check on override, etc.
154 delete options.check_barcode;
156 return promise.then(function() {
158 var method = 'open-ils.circ.renew';
159 if (options.override) method += '.override';
161 return egCore.net.request(
162 'open-ils.circ', method, egCore.auth.token(), params
164 ).then(function(evt) {
166 if (!angular.isArray(evt)) evt = [evt];
168 return service.flesh_response_data(
169 'renew', evt, params, options)
171 return service.handle_renew_resp(evt, params, options);
173 .then(function(final_resp) {
174 return service.munge_resp_data(final_resp)
180 // Performs a checkin
181 // Returns a promise resolved with the original params and options,
182 // plus the final checkin event (e.g. in the case of override).
183 // Rejected if the checkin cannot be completed.
184 service.checkin = function(params, options) {
185 if (!options) options = {};
187 console.debug('egCirc.checkin() : '
188 + js2JSON(params) + ' : ' + js2JSON(options));
190 var promise = options.check_barcode ?
191 service.test_barcode(params.copy_barcode) : $q.when();
193 // avoid re-check on override, etc.
194 delete options.check_barcode;
196 return promise.then(function() {
198 var method = 'open-ils.circ.checkin';
199 if (options.override) method += '.override';
201 return egCore.net.request(
202 'open-ils.circ', method, egCore.auth.token(), params
204 ).then(function(evt) {
206 if (!angular.isArray(evt)) evt = [evt];
207 return service.flesh_response_data(
208 'checkin', evt, params, options)
210 return service.handle_checkin_resp(evt, params, options);
212 .then(function(final_resp) {
213 return service.munge_resp_data(final_resp)
219 // provide consistent formatting of the final response data
220 service.munge_resp_data = function(final_resp) {
221 var data = final_resp.data = {};
223 if (!final_resp.evt[0]) return;
225 var payload = final_resp.evt[0].payload;
226 if (!payload) return;
228 data.circ = payload.circ;
229 data.parent_circ = payload.parent_circ;
230 data.hold = payload.hold;
231 data.record = payload.record;
232 data.acp = payload.copy;
233 data.acn = payload.volume ? payload.volume : payload.copy ? payload.copy.call_number() : null;
234 data.au = payload.patron;
235 data.transit = payload.transit;
236 data.status = payload.status;
237 data.message = payload.message;
238 data.title = final_resp.evt[0].title;
239 data.author = final_resp.evt[0].author;
240 data.isbn = final_resp.evt[0].isbn;
241 data.route_to = final_resp.evt[0].route_to;
243 // for checkin, the mbts lives on the main circ
244 if (payload.circ && payload.circ.billable_transaction())
245 data.mbts = payload.circ.billable_transaction().summary();
247 // on renewals, the mbts lives on the parent circ
248 if (payload.parent_circ && payload.parent_circ.billable_transaction())
249 data.mbts = payload.parent_circ.billable_transaction().summary();
251 if (!data.route_to) {
253 data.route_to = data.transit.dest().shortname();
254 } else if (data.acp) {
255 data.route_to = data.acp.location().name();
262 service.handle_overridable_checkout_event = function(evt, params, options) {
264 if (options.override) {
265 // override attempt already made and failed.
266 // NOTE: I don't think we'll ever get here, since the
267 // override attempt should produce a perm failure...
268 angular.forEach(evt, function(e){ console.debug('override failed: ' + e.textcode); });
273 if (evt.filter(function(e){return !service.auto_override_checkout_events[e.textcode];}).length == 0) {
274 // user has already opted to override these type
275 // of events. Re-run the checkout w/ override.
276 options.override = true;
277 return service.checkout(params, options);
280 // Ask the user if they would like to override this event.
281 // Some events offer a stock override dialog, while others
282 // require additional context.
284 switch(evt[0].textcode) {
285 case 'COPY_NOT_AVAILABLE':
286 return service.copy_not_avail_dialog(evt[0], params, options);
287 case 'COPY_ALERT_MESSAGE':
288 return service.copy_alert_dialog(evt[0], params, options, 'checkout');
290 return service.override_dialog(evt, params, options, 'checkout');
294 service.handle_overridable_renew_event = function(evt, params, options) {
296 if (options.override) {
297 // override attempt already made and failed.
298 // NOTE: I don't think we'll ever get here, since the
299 // override attempt should produce a perm failure...
300 angular.forEach(evt, function(e){ console.debug('override failed: ' + e.textcode); });
305 // renewal auto-overrides are the same as checkout
306 if (evt.filter(function(e){return !service.auto_override_checkout_events[e.textcode];}).length == 0) {
307 // user has already opted to override these type
308 // of events. Re-run the renew w/ override.
309 options.override = true;
310 return service.renew(params, options);
313 // Ask the user if they would like to override this event.
314 // Some events offer a stock override dialog, while others
315 // require additional context.
317 switch(evt[0].textcode) {
318 case 'COPY_ALERT_MESSAGE':
319 return service.copy_alert_dialog(evt[0], params, options, 'renew');
321 return service.override_dialog(evt, params, options, 'renew');
326 service.handle_overridable_checkin_event = function(evt, params, options) {
328 if (options.override) {
329 // override attempt already made and failed.
330 // NOTE: I don't think we'll ever get here, since the
331 // override attempt should produce a perm failure...
332 angular.forEach(evt, function(e){ console.debug('override failed: ' + e.textcode); });
337 if (options.suppress_checkin_popups
338 && evt.filter(function(e){return service.checkin_suppress_overrides.indexOf(e.textcode) == -1;}).length == 0) {
339 // Events are suppressed. Re-run the checkin w/ override.
340 options.override = true;
341 return service.checkin(params, options);
344 // Ask the user if they would like to override this event.
345 // Some events offer a stock override dialog, while others
346 // require additional context.
348 switch(evt[0].textcode) {
349 case 'COPY_ALERT_MESSAGE':
350 return service.copy_alert_dialog(evt[0], params, options, 'checkin');
352 return service.override_dialog(evt, params, options, 'checkin');
357 service.handle_renew_resp = function(evt, params, options) {
359 var final_resp = {evt : evt, params : params, options : options};
361 // track the barcode regardless of whether it refers to a copy
362 angular.forEach(evt, function(e){ e.copy_barcode = params.copy_barcode; });
364 // Overridable Events
365 if (evt.filter(function(e){return service.renew_overridable_events.indexOf(e.textcode) > -1;}).length > 0)
366 return service.handle_overridable_renew_event(evt, params, options);
369 switch (evt[0].textcode) {
371 return $q.when(final_resp);
373 case 'COPY_IN_TRANSIT':
374 case 'PATRON_CARD_INACTIVE':
375 case 'PATRON_INACTIVE':
376 case 'PATRON_ACCOUNT_EXPIRED':
377 case 'CIRC_CLAIMS_RETURNED':
378 return service.exit_alert(
379 egCore.strings[evt[0].textcode],
380 {barcode : params.copy_barcode}
384 return service.exit_alert(
385 egCore.strings[evt[0].textcode],
386 {permission : evt[0].ilsperm}
390 return service.exit_alert(
391 egCore.strings.CHECKOUT_FAILED_GENERIC, {
392 barcode : params.copy_barcode,
393 textcode : evt[0].textcode,
401 service.handle_checkout_resp = function(evt, params, options) {
403 var final_resp = {evt : evt, params : params, options : options};
405 // track the barcode regardless of whether it refers to a copy
406 angular.forEach(evt, function(e){ e.copy_barcode = params.copy_barcode; });
408 // Overridable Events
409 if (evt.filter(function(e){return service.checkout_overridable_events.indexOf(e.textcode) > -1;}).length > 0)
410 return service.handle_overridable_checkout_event(evt, params, options);
413 switch (evt[0].textcode) {
415 return $q.when(final_resp);
417 case 'ITEM_NOT_CATALOGED':
418 return service.precat_dialog(params, options);
420 case 'OPEN_CIRCULATION_EXISTS':
421 return service.circ_exists_dialog(evt, params, options);
423 case 'COPY_IN_TRANSIT':
424 return service.copy_in_transit_dialog(evt, params, options);
426 case 'PATRON_CARD_INACTIVE':
427 case 'PATRON_INACTIVE':
428 case 'PATRON_ACCOUNT_EXPIRED':
429 case 'CIRC_CLAIMS_RETURNED':
430 return service.exit_alert(
431 egCore.strings[evt[0].textcode],
432 {barcode : params.copy_barcode}
436 return service.exit_alert(
437 egCore.strings[evt[0].textcode],
438 {permission : evt[0].ilsperm}
442 return service.exit_alert(
443 egCore.strings.CHECKOUT_FAILED_GENERIC, {
444 barcode : params.copy_barcode,
445 textcode : evt[0].textcode,
452 // returns a promise resolved with the list of circ mods
453 service.get_circ_mods = function() {
455 return $q.when(egCore.env.ccm.list);
457 return egCore.pcrud.retrieveAll('ccm', null, {atomic : true})
458 .then(function(list) {
459 egCore.env.absorbList(list, 'ccm');
464 // returns a promise resolved with the list of noncat types
465 service.get_noncat_types = function() {
467 return $q.when(egCore.env.cnct.list);
469 return egCore.pcrud.search('cnct',
471 egCore.org.fullPath(egCore.auth.user().ws_ou(), true)},
472 null, {atomic : true}
473 ).then(function(list) {
474 egCore.env.absorbList(list, 'cnct');
479 service.get_staff_penalty_types = function() {
481 return $q.when(egCore.env.csp.list);
482 return egCore.pcrud.search(
483 // id <= 100 are reserved for system use
484 'csp', {id : {'>': 100}}, {}, {atomic : true})
485 .then(function(penalties) {
486 return egCore.env.absorbList(penalties, 'csp').list;
490 // ideally all of these data should be returned with the response,
491 // but until then, grab what we need.
492 service.flesh_response_data = function(action, evt, params, options) {
495 if (!evt[0] || !(payload = evt[0].payload)) return $q.when();
497 promises.push(service.flesh_copy_location(payload.copy));
500 service.flesh_copy_status(payload.copy)
503 // copy is in transit, but no transit was delivered
504 // in the payload. Do this here instead of below to
505 // ensure consistent copy status fleshiness
506 if (!payload.transit && payload.copy.status().id() == 6) { // in-transit
507 return service.find_copy_transit(evt, params, options)
508 .then(function(trans) {
510 trans.source(egCore.org.get(trans.source()));
511 trans.dest(egCore.org.get(trans.dest()));
512 payload.transit = trans;
520 // local flesh transit
521 if (transit = payload.transit) {
522 transit.source(egCore.org.get(transit.source()));
523 transit.dest(egCore.org.get(transit.dest()));
526 // TODO: renewal responses should include the patron
527 if (!payload.patron && payload.circ) {
529 egCore.pcrud.retrieve('au', payload.circ.usr())
530 .then(function(user) {payload.patron = user})
534 // extract precat values
535 angular.forEach(evt, function(e){ e.title = payload.record ? payload.record.title() :
536 (payload.copy ? payload.copy.dummy_title() : null);});
538 angular.forEach(evt, function(e){ e.author = payload.record ? payload.record.author() :
539 (payload.copy ? payload.copy.dummy_author() : null);});
541 angular.forEach(evt, function(e){ e.isbn = payload.record ? payload.record.isbn() :
542 (payload.copy ? payload.copy.dummy_isbn() : null);});
544 return $q.all(promises);
547 // fetches the full list of copy statuses
548 service.flesh_copy_status = function(copy) {
549 if (!copy) return $q.when();
551 return $q.when(copy.status(egCore.env.ccs.map[copy.status()]));
552 return egCore.pcrud.retrieveAll('ccs', {}, {atomic : true}).then(
554 egCore.env.absorbList(list, 'ccs');
555 copy.status(egCore.env.ccs.map[copy.status()]);
560 // there may be *many* copy locations and we may be handling items
561 // for other locations. Fetch copy locations as-needed and cache.
562 service.flesh_copy_location = function(copy) {
563 if (!copy) return $q.when();
564 if (angular.isObject(copy.location())) return $q.when(copy);
565 if (egCore.env.acpl) {
566 if (egCore.env.acpl.map[copy.location()]) {
567 copy.location(egCore.env.acpl.map[copy.location()]);
568 return $q.when(copy);
571 return egCore.pcrud.retrieve('acpl', copy.location())
572 .then(function(loc) {
573 egCore.env.absorbList([loc], 'acpl'); // append to cache
580 // fetch org unit addresses as needed.
581 service.get_org_addr = function(org_id, addr_type) {
582 var org = egCore.org.get(org_id);
583 var addr_id = org[addr_type]();
585 if (egCore.env.aoa && egCore.env.aoa.map[addr_id])
586 return $q.when(egCore.env.aoa.map[addr_id]);
588 return egCore.pcrud.retrieve('aoa', addr_id).then(function(addr) {
589 egCore.env.absorbList([addr], 'aoa');
590 return egCore.env.aoa.map[addr_id];
594 service.exit_alert = function(msg, scope) {
595 return egAlertDialog.open(msg, scope).result.then(
596 function() {return $q.reject()});
599 // opens a dialog asking the user if they would like to override
600 // the returned event.
601 service.override_dialog = function(evt, params, options, action) {
602 if (!angular.isArray(evt)) evt = [evt];
604 templateUrl: './circ/share/t_event_override_dialog',
606 ['$scope', '$modalInstance',
607 function($scope, $modalInstance) {
609 $scope.auto_override =
610 evt.filter(function(e){
611 return service.checkout_auto_override_after_first.indexOf(evt.textcode) > -1;
613 $scope.copy_barcode = params.copy_barcode; // may be null
614 $scope.ok = function() { $modalInstance.close() }
615 $scope.cancel = function ($event) {
616 $modalInstance.dismiss();
617 $event.preventDefault();
622 options.override = true;
624 if (action == 'checkin') {
625 return service.checkin(params, options);
628 // checkout/renew support override-after-first
629 angular.forEach(evt, function(e){
630 if (service.checkout_auto_override_after_first.indexOf(e.textcode) > -1)
631 service.auto_override_checkout_events[e.textcode] = true;
634 return service[action](params, options);
639 service.copy_not_avail_dialog = function(evt, params, options) {
640 if (angular.isArray(evt)) evt = evt[0];
642 templateUrl: './circ/share/t_copy_not_avail_dialog',
644 ['$scope','$modalInstance','copyStatus',
645 function($scope , $modalInstance , copyStatus) {
646 $scope.copyStatus = copyStatus;
647 $scope.ok = function() {$modalInstance.close()}
648 $scope.cancel = function() {$modalInstance.dismiss()}
651 copyStatus : function() {
652 return egCore.pcrud.retrieve(
653 'ccs', evt.payload.status());
658 options.override = true;
659 return service.checkout(params, options);
664 // Opens a dialog allowing the user to fill in the desired non-cat count.
665 // Unlike other dialogs, which kickoff circ actions internally
666 // as a result of events, this dialog does not kick off any circ
667 // actions. It just collects the count and and resolves the promise.
669 // This assumes the caller has already handled the noncat-type
670 // selection and just needs to collect the count info.
671 service.noncat_dialog = function(params, options) {
672 var noncatMax = 99; // hard-coded max
674 // the caller should presumably have fetched the noncat_types via
675 // our API already, but fetch them again (from cache) to be safe.
676 return service.get_noncat_types().then(function() {
678 params.noncat = true;
679 var type = egCore.env.cnct.map[params.noncat_type];
682 templateUrl: './circ/share/t_noncat_dialog',
684 ['$scope', '$modalInstance',
685 function($scope, $modalInstance) {
686 $scope.focusMe = true;
689 $scope.noncatMax = noncatMax;
690 $scope.ok = function(count) { $modalInstance.close(count) }
691 $scope.cancel = function ($event) {
692 $modalInstance.dismiss()
693 $event.preventDefault();
698 if (count && count > 0 && count <= noncatMax) {
699 // NOTE: in Chrome, form validation ensure a valid number
700 params.noncat_count = count;
701 return $q.when(params);
710 // Opens a dialog allowing the user to fill in pre-cat copy info.
711 service.precat_dialog = function(params, options) {
714 templateUrl: './circ/share/t_precat_dialog',
716 ['$scope', '$modalInstance', 'circMods',
717 function($scope, $modalInstance, circMods) {
718 $scope.focusMe = true;
719 $scope.precatArgs = {
720 copy_barcode : params.copy_barcode,
721 circ_modifier : circMods.length ? circMods[0].code() : null
723 $scope.circModifiers = circMods;
724 $scope.ok = function(args) { $modalInstance.close(args) }
725 $scope.cancel = function () { $modalInstance.dismiss() }
728 circMods : function() {
729 return service.get_circ_mods();
734 if (!args || !args.dummy_title) return $q.reject();
735 angular.forEach(args, function(val, key) {params[key] = val});
736 params.precat = true;
737 return service.checkout(params, options);
742 // find the open transit for the given copy barcode; flesh the org
744 service.find_copy_transit = function(evt, params, options) {
745 if (angular.isArray(evt)) evt = evt[0];
747 if (evt && evt.payload && evt.payload.transit)
748 return $q.when(evt.payload.transit);
750 return egCore.pcrud.search('atc',
751 { dest_recv_time : null},
753 flesh_fields : {atc : ['target_copy']},
757 barcode : params.copy_barcode,
763 order_by : {atc : 'source_send_time desc'},
765 ).then(function(transit) {
766 transit.source(egCore.org.get(transit.source()));
767 transit.dest(egCore.org.get(transit.dest()));
772 service.copy_in_transit_dialog = function(evt, params, options) {
773 if (angular.isArray(evt)) evt = evt[0];
775 templateUrl: './circ/share/t_copy_in_transit_dialog',
777 ['$scope','$modalInstance','transit',
778 function($scope , $modalInstance , transit) {
779 $scope.transit = transit;
780 $scope.ok = function() { $modalInstance.close(transit) }
781 $scope.cancel = function() { $modalInstance.dismiss() }
784 // fetch the conflicting open transit w/ fleshed copy
785 transit : function() {
786 return service.find_copy_transit(evt, params, options);
791 // user chose to abort the transit then checkout
792 return service.abort_transit(transit.id())
794 return service.checkout(params, options);
800 service.abort_transit = function(transit_id) {
801 return egCore.net.request(
803 'open-ils.circ.transit.abort',
804 egCore.auth.token(), {transitid : transit_id}
805 ).then(function(resp) {
806 if (evt = egCore.evt.parse(resp)) {
814 service.last_copy_circ = function(copy_id) {
815 return egCore.pcrud.search('circ',
816 {target_copy : copy_id},
817 {order_by : {circ : 'xact_start desc' }, limit : 1}
821 service.circ_exists_dialog = function(evt, params, options) {
822 if (angular.isArray(evt)) evt = evt[0];
824 if (!evt.payload.old_circ) {
825 return egCore.net.request(
827 'open-ils.search.asset.copy.fleshed2.find_by_barcode',
829 ).then(function(resp){
831 if (egCore.evt.parse(resp)) {
832 console.error(egCore.evt.parse(resp));
834 evt.payload.old_circ = resp.circulations()[0];
835 return service.circ_exists_dialog_impl( evt, params, options );
839 return service.circ_exists_dialog_impl( evt, params, options );
843 service.circ_exists_dialog_impl = function (evt, params, options) {
845 var openCirc = evt.payload.old_circ;
846 var sameUser = openCirc.usr() == params.patron_id;
849 templateUrl: './circ/share/t_circ_exists_dialog',
851 ['$scope','$modalInstance',
852 function($scope , $modalInstance) {
853 $scope.args = {forgive_fines : false};
854 $scope.circDate = openCirc.xact_start();
855 $scope.sameUser = sameUser;
856 $scope.ok = function() { $modalInstance.close($scope.args) }
857 $scope.cancel = function($event) {
858 $modalInstance.dismiss();
859 $event.preventDefault(); // form, avoid calling ok();
865 params.void_overdues = args.forgive_fines;
866 options.override = true;
867 return service.renew(params, options);
870 return service.checkin({
871 barcode : params.copy_barcode,
873 void_overdues : args.forgive_fines
874 }).then(function(checkin_resp) {
875 if (checkin_resp.evt.textcode == 'SUCCESS') {
876 return service.checkout(params, options);
878 alert(egCore.evt.parse(evt));
886 service.batch_backdate = function(circ_ids, backdate) {
887 return egCore.net.request(
889 'open-ils.circ.post_checkin_backdate.batch',
890 egCore.auth.token(), circ_ids, backdate);
893 service.backdate_dialog = function(circ_ids) {
895 templateUrl: './circ/share/t_backdate_dialog',
897 ['$scope','$modalInstance',
898 function($scope , $modalInstance) {
900 var today = new Date();
902 num_circs : circ_ids.length,
907 $scope.$watch('dialog.backdate', function(newval) {
908 if (newval && newval > today)
909 $scope.dialog.backdate = today;
913 $scope.cancel = function() {
914 $modalInstance.dismiss();
917 $scope.ok = function() {
919 var bd = $scope.dialog.backdate.toISOString().replace(/T.*/,'');
920 service.batch_backdate(circ_ids, bd)
922 function() { // on complete
923 $modalInstance.close({backdate : bd});
926 function(resp) { // on response
927 console.debug('backdate returned ' + resp);
929 $scope.num_processed++;
931 console.error(egCore.evt.parse(resp));
940 service.mark_claims_returned = function(barcode, date, override) {
942 var method = 'open-ils.circ.circulation.set_claims_returned';
943 if (override) method += '.override';
945 console.debug('claims returned ' + method);
947 return egCore.net.request(
948 'open-ils.circ', method, egCore.auth.token(),
949 {barcode : barcode, backdate : date})
951 .then(function(resp) {
953 if (resp == 1) { // success
954 console.debug('claims returned succeeded for ' + barcode);
957 } else if (evt = egCore.evt.parse(resp)) {
958 console.debug('claims returned failed: ' + evt.toString());
960 if (evt.textcode == 'PATRON_EXCEEDS_CLAIMS_RETURN_COUNT') {
961 // TODO check perms before offering override option?
963 if (override) return;// just to be safe
965 return egConfirmDialog.open(
966 egCore.strings.TOO_MANY_CLAIMS_RETURNED, '', {}
967 ).result.then(function() {
968 return service.mark_claims_returned(barcode, date, true);
972 if (evt.textcode == 'PERM_FAILURE') {
973 console.error('claims returned permission denied')
974 // TODO: auth override dialog?
980 service.mark_claims_returned_dialog = function(copy_barcodes) {
981 if (!copy_barcodes.length) return;
984 templateUrl: './circ/share/t_mark_claims_returned_dialog',
986 ['$scope','$modalInstance',
987 function($scope , $modalInstance) {
989 var today = new Date();
991 barcodes : copy_barcodes,
995 $scope.$watch('args.date', function(newval) {
996 if (newval && newval > today)
997 $scope.args.backdate = today;
1000 $scope.cancel = function() {$modalInstance.dismiss()}
1001 $scope.ok = function() {
1003 var date = $scope.args.date.toISOString().replace(/T.*/,'');
1005 var deferred = $q.defer();
1007 // serialize the action on each barcode so that the
1008 // caller will never see multiple alerts at the same time.
1009 function mark_one() {
1010 var bc = copy_barcodes.pop();
1013 $modalInstance.close();
1017 // finally -> continue even when one fails
1018 service.mark_claims_returned(bc, date)
1019 .finally(function(barcode) {
1020 if (barcode) deferred.notify(barcode);
1024 mark_one(); // kick it off
1025 return deferred.promise;
1031 // serially checks in each barcode with claims_never_checked_out set
1032 // returns promise, notified on each barcode, resolved after all
1033 // checkins are complete.
1034 service.mark_claims_never_checked_out = function(barcodes) {
1035 if (!barcodes.length) return;
1037 var deferred = $q.defer();
1038 egConfirmDialog.open(
1039 egCore.strings.MARK_NEVER_CHECKED_OUT, '', {barcodes : barcodes}
1041 ).result.then(function() {
1042 function mark_one() {
1043 var bc = barcodes.pop();
1045 if (!bc) { // all done
1051 {claims_never_checked_out : true, copy_barcode : bc})
1052 .finally(function() {
1053 deferred.notify(bc);
1060 return deferred.promise;
1063 service.mark_damaged = function(copy_ids) {
1064 return egConfirmDialog.open(
1065 egCore.strings.MARK_DAMAGED_CONFIRM, '',
1066 { num_items : copy_ids.length,
1068 cancel : function() {}
1071 ).result.then(function() {
1073 angular.forEach(copy_ids, function(copy_id) {
1077 'open-ils.circ.mark_item_damaged',
1078 egCore.auth.token(), copy_id
1079 ).then(function(resp) {
1080 if (evt = egCore.evt.parse(resp)) {
1081 console.error('mark damaged failed: ' + evt);
1087 return $q.all(promises);
1091 service.mark_missing = function(copy_ids) {
1092 return egConfirmDialog.open(
1093 egCore.strings.MARK_MISSING_CONFIRM, '',
1094 { num_items : copy_ids.length,
1096 cancel : function() {}
1098 ).result.then(function() {
1100 angular.forEach(copy_ids, function(copy_id) {
1104 'open-ils.circ.mark_item_missing',
1105 egCore.auth.token(), copy_id
1106 ).then(function(resp) {
1107 if (evt = egCore.evt.parse(resp)) {
1108 console.error('mark missing failed: ' + evt);
1114 return $q.all(promises);
1120 // Mark circulations as lost via copy barcode. As each item is
1121 // processed, the returned promise is notified of the barcode.
1122 // No confirmation dialog is presented.
1123 service.mark_lost = function(copy_barcodes) {
1124 var deferred = $q.defer();
1127 angular.forEach(copy_barcodes, function(barcode) {
1131 'open-ils.circ.circulation.set_lost',
1132 egCore.auth.token(), {barcode : barcode}
1133 ).then(function(resp) {
1134 if (evt = egCore.evt.parse(resp)) {
1135 console.error("Mark lost failed: " + evt.toString());
1138 // inform the caller as each item is processed
1139 deferred.notify(barcode);
1144 $q.all(promises).then(function() {deferred.resolve()});
1145 return deferred.promise;
1148 service.abort_transits = function(transit_ids) {
1149 return egConfirmDialog.open(
1150 egCore.strings.ABORT_TRANSIT_CONFIRM, '',
1151 { num_transits : transit_ids.length,
1153 cancel : function() {}
1156 ).result.then(function() {
1158 angular.forEach(transit_ids, function(transit_id) {
1162 'open-ils.circ.transit.abort',
1163 egCore.auth.token(), {transitid : transit_id}
1164 ).then(function(resp) {
1165 if (evt = egCore.evt.parse(resp)) {
1166 console.error('abort transit failed: ' + evt);
1172 return $q.all(promises);
1178 // alert when copy location alert_message is set.
1179 // This does not affect processing, it only produces a click-through
1180 service.handle_checkin_loc_alert = function(evt, params, options) {
1181 if (angular.isArray(evt)) evt = evt[0];
1183 var copy = evt && evt.payload ? evt.payload.copy : null;
1185 if (copy && !options.suppress_checkin_popups
1186 && copy.location().checkin_alert() == 't') {
1188 return egAlertDialog.open(
1189 egCore.strings.LOCATION_ALERT_MSG, {copy : copy}).result;
1195 service.handle_checkin_resp = function(evt, params, options) {
1196 if (!angular.isArray(evt)) evt = [evt];
1198 var final_resp = {evt : evt, params : params, options : options};
1200 var copy, hold, transit;
1201 if (evt[0].payload) {
1202 copy = evt[0].payload.copy;
1203 hold = evt[0].payload.hold;
1204 transit = evt[0].payload.transit;
1207 // track the barcode regardless of whether it's valid
1208 angular.forEach(evt, function(e){ e.copy_barcode = params.copy_barcode; });
1210 angular.forEach(evt, function(e){ console.debug('checkin event ' + e.textcode); });
1212 if (evt.filter(function(e){return service.checkin_overridable_events.indexOf(e.textcode) > -1;}).length > 0)
1213 return service.handle_overridable_checkin_event(evt, params, options);
1215 switch (evt[0].textcode) {
1220 switch(Number(copy.status().id())) {
1222 case 0: /* AVAILABLE */
1223 case 4: /* MISSING */
1224 case 7: /* RESHELVING */
1226 // see if the copy location requires an alert
1227 return service.handle_checkin_loc_alert(evt, params, options)
1228 .then(function() {return final_resp});
1230 case 8: /* ON HOLDS SHELF */
1235 if (hold.pickup_lib() == egCore.auth.user().ws_ou()) {
1236 // inform user if the item is on the local holds shelf
1238 evt[0].route_to = egCore.strings.ROUTE_TO_HOLDS_SHELF;
1239 return service.route_dialog(
1240 './circ/share/t_hold_shelf_dialog',
1241 evt[0], params, options
1242 ).then(function() { return final_resp });
1245 // normally, if the hold was on the shelf at a
1246 // different location, it would be put into
1247 // transit, resulting in a ROUTE_ITEM event.
1248 return $q.when(final_resp);
1252 console.error('checkin: item on holds shelf, '
1253 + 'but hold info not returned from checkin');
1254 return $q.when(final_resp);
1257 case 11: /* CATALOGING */
1258 evt[0].route_to = egCore.strings.ROUTE_TO_CATALOGING;
1259 return $q.when(final_resp);
1261 case 15: /* ON_RESERVATION_SHELF */
1262 // TODO: show booking reservation dialog
1263 return $q.when(final_resp);
1266 console.error('Unhandled checkin copy status: '
1267 + copy.status().id() + ' : ' + copy.status().name());
1268 return $q.when(final_resp);
1272 return service.route_dialog(
1273 './circ/share/t_transit_dialog',
1274 evt[0], params, options
1275 ).then(function() { return final_resp });
1277 case 'ASSET_COPY_NOT_FOUND':
1278 return egAlertDialog.open(
1279 egCore.strings.UNCAT_ALERT_DIALOG, params)
1280 .result.then(function() {return final_resp});
1282 case 'ITEM_NOT_CATALOGED':
1283 evt[0].route_to = egCore.strings.ROUTE_TO_CATALOGING;
1284 if (options.no_precat_alert)
1285 return $q.when(final_resp);
1286 return egAlertDialog.open(
1287 egCore.strings.PRECAT_CHECKIN_MSG, params)
1288 .result.then(function() {return final_resp});
1291 console.warn('unhandled checkin response : ' + evt[0].textcode);
1292 return $q.when(final_resp);
1296 // collect transit, address, and hold info that's not already
1297 // included in responses.
1298 service.collect_route_data = function(tmpl, evt, params, options) {
1299 if (angular.isArray(evt)) evt = evt[0];
1303 if (evt.org && !tmpl.match(/hold_shelf/)) {
1305 service.get_org_addr(evt.org, 'holds_address')
1306 .then(function(addr) { data.address = addr })
1310 if (evt.payload.hold) {
1312 egCore.pcrud.retrieve('au',
1313 evt.payload.hold.usr(), {
1315 flesh_fields : {'au' : ['card']}
1317 ).then(function(patron) {data.patron = patron})
1321 if (!tmpl.match(/hold_shelf/)) {
1323 service.find_copy_transit(evt, params, options)
1324 .then(function(trans) {data.transit = trans})
1328 return $q.all(promises).then(function() { return data });
1331 service.route_dialog = function(tmpl, evt, params, options) {
1332 if (angular.isArray(evt)) evt = evt[0];
1334 return service.collect_route_data(tmpl, evt, params, options)
1335 .then(function(data) {
1337 // All actions flow from the print data
1339 var print_context = {
1340 copy : egCore.idl.toHash(evt.payload.copy),
1346 // route_dialog includes the "route to holds shelf"
1347 // dialog, which has no transit
1348 print_context.transit = egCore.idl.toHash(data.transit);
1349 print_context.dest_address = egCore.idl.toHash(data.address);
1350 print_context.dest_location =
1351 egCore.idl.toHash(egCore.org.get(data.transit.dest()));
1355 print_context.hold = egCore.idl.toHash(evt.payload.hold);
1356 print_context.patron = egCore.idl.toHash(data.patron);
1359 function print_transit() {
1360 var template = data.transit ?
1361 (data.patron ? 'hold_transit_slip' : 'transit_slip') :
1364 return egCore.print.print({
1365 context : 'default',
1366 template : template,
1367 scope : print_context
1371 // when auto-print is on, skip the dialog and go straight
1373 if (options.auto_print_holds_transits)
1374 return print_transit();
1376 return $modal.open({
1379 '$scope','$modalInstance',
1380 function($scope , $modalInstance) {
1382 $scope.today = new Date();
1384 // copy the print scope into the dialog scope
1385 angular.forEach(print_context, function(val, key) {
1389 $scope.ok = function() {$modalInstance.close()}
1391 $scope.print = function() {
1392 $modalInstance.close();
1401 // action == what action to take if the user confirms the alert
1402 service.copy_alert_dialog = function(evt, params, options, action) {
1403 if (angular.isArray(evt)) evt = evt[0];
1404 return egConfirmDialog.open(
1405 egCore.strings.COPY_ALERT_MSG_DIALOG_TITLE,
1406 evt.payload, // payload == alert message text
1407 { copy_barcode : params.copy_barcode,
1409 cancel : function() {}
1411 ).result.then(function() {
1412 options.override = true;
1413 return service[action](params, options);
1417 // check the barcode. If it's no good, show the warning dialog
1418 // Resolves on success, rejected on error
1419 service.test_barcode = function(bc) {
1421 var ok = service.check_barcode(bc);
1422 if (ok) return $q.when();
1424 return $modal.open({
1425 templateUrl: './circ/share/t_bad_barcode_dialog',
1427 ['$scope', '$modalInstance',
1428 function($scope, $modalInstance) {
1429 $scope.barcode = bc;
1430 $scope.ok = function() { $modalInstance.close() }
1431 $scope.cancel = function() { $modalInstance.dismiss() }
1436 // check() and checkdigit() copied directly
1437 // from chrome/content/util/barcode.js
1439 service.check_barcode = function(bc) {
1440 if (bc != Number(bc)) return false;
1442 // "16.00" == Number("16.00"), but the . is bad.
1443 // Throw out any barcode that isn't just digits
1444 if (bc.search(/\D/) != -1) return false;
1445 var last_digit = bc.substr(bc.length-1);
1446 var stripped_barcode = bc.substr(0,bc.length-1);
1447 return service.barcode_checkdigit(stripped_barcode).toString() == last_digit;
1450 service.barcode_checkdigit = function(bc) {
1451 var reverse_barcode = bc.toString().split('').reverse();
1452 var check_sum = 0; var multiplier = 2;
1453 for (var i = 0; i < reverse_barcode.length; i++) {
1454 var digit = reverse_barcode[i];
1455 var product = digit * multiplier; product = product.toString();
1457 for (var j = 0; j < product.length; j++) {
1458 temp_sum += Number( product[j] );
1460 check_sum += Number( temp_sum );
1461 multiplier = ( multiplier == 2 ? 1 : 2 );
1463 check_sum = check_sum.toString();
1464 var next_multiple_of_10 = (check_sum.match(/(\d*)\d$/)[1] * 10) + 10;
1465 var check_digit = next_multiple_of_10 - Number(check_sum);
1466 if (check_digit == 10) check_digit = 0;
1470 service.create_penalty = function(user_id) {
1471 return $modal.open({
1472 templateUrl: './circ/share/t_new_message_dialog',
1474 ['$scope','$modalInstance','staffPenalties',
1475 function($scope , $modalInstance , staffPenalties) {
1476 $scope.focusNote = true;
1477 $scope.penalties = staffPenalties;
1478 $scope.require_initials = service.require_initials;
1479 $scope.args = {penalty : 21}; // default to Note
1480 $scope.setPenalty = function(id) {
1483 $scope.ok = function(count) { $modalInstance.close($scope.args) }
1484 $scope.cancel = function($event) {
1485 $modalInstance.dismiss();
1486 $event.preventDefault();
1489 resolve : { staffPenalties : service.get_staff_penalty_types }
1492 var pen = new egCore.idl.ausp();
1494 pen.org_unit(egCore.auth.user().ws_ou());
1495 pen.note(args.note);
1496 if (args.initials) pen.note(args.note + ' [' + args.initials + ']');
1497 if (args.custom_penalty) {
1498 pen.standing_penalty(args.custom_penalty);
1500 pen.standing_penalty(args.penalty);
1502 pen.staff(egCore.auth.user().id());
1503 pen.set_date('now');
1504 return egCore.pcrud.create(pen);
1509 // assumes, for now anyway, penalty type is fleshed onto usr_penalty.
1510 service.edit_penalty = function(usr_penalty) {
1511 return $modal.open({
1512 templateUrl: './circ/share/t_new_message_dialog',
1514 ['$scope','$modalInstance','staffPenalties',
1515 function($scope , $modalInstance , staffPenalties) {
1516 $scope.focusNote = true;
1517 $scope.penalties = staffPenalties;
1518 $scope.require_initials = service.require_initials;
1520 penalty : usr_penalty.standing_penalty().id(),
1521 note : usr_penalty.note()
1523 $scope.setPenalty = function(id) { args.penalty = id; }
1524 $scope.ok = function(count) { $modalInstance.close($scope.args) }
1525 $scope.cancel = function($event) {
1526 $modalInstance.dismiss();
1527 $event.preventDefault();
1530 resolve : { staffPenalties : service.get_staff_penalty_types }
1533 usr_penalty.note(args.note);
1534 if (args.initials) usr_penalty.note(args.note + ' [' + args.initials + ']');
1535 usr_penalty.standing_penalty(args.penalty);
1536 return egCore.pcrud.update(usr_penalty);