2 * Shared item services for circulation
5 angular.module('egCoreMod')
7 ['egCore','egCirc','$uibModal','$q','$timeout','$window','egConfirmDialog','egAlertDialog',
8 function(egCore , egCirc , $uibModal , $q , $timeout , $window , egConfirmDialog , egAlertDialog ) {
11 copies : [], // copy barcode search results
12 index : 0 // search grid index
18 acp : ['call_number','location','status','location','floating','circ_modifier',
19 'age_protect','circ_lib','copy_alerts'],
20 acn : ['record','prefix','suffix','label_class'],
21 bre : ['simple_record','creator','editor']
24 // avoid fleshing MARC on the bre
25 // note: don't add simple_record.. not sure why
26 bre : ['id','tcn_value','creator','editor'],
36 'checkin_workstation',
44 order_by : {combcirc : 'xact_start desc'},
48 //Retrieve separate copy, aacs, and accs information
49 service.getCopy = function(barcode, id) {
51 // handle barcode completion
52 return egCirc.handle_barcode_completion(barcode)
53 .then(function(actual_barcode) {
54 return egCore.pcrud.search(
55 'acp', {barcode : actual_barcode, deleted : 'f'},
56 service.flesh).then(function(copy) {return copy});
60 return egCore.pcrud.retrieve( 'acp', id, service.flesh)
61 .then(function(copy) {return copy});
64 service.getCirc = function(id) {
65 return egCore.pcrud.search('combcirc', { target_copy : id },
66 service.circFlesh).then(function(circ) {return circ});
69 service.getSummary = function(id) {
70 return circ_summary = egCore.net.request(
72 'open-ils.circ.renewal_chain.retrieve_by_circ.summary',
73 egCore.auth.token(), id).then(
74 function(circ_summary) {return circ_summary});
77 //Combine copy, circ, and accs information
78 service.retrieveCopyData = function(barcode, id) {
81 var fetchCopy = function(barcode, id) {
82 return service.getCopy(barcode, id)
83 .then(function(copy) {
88 var fetchCirc = function(copy) {
89 return service.getCirc(copy.id())
90 .then(function(circ) {
95 var fetchSummary = function(circ) {
96 return service.getSummary(circ.id())
97 .then(function(summary) {
98 copyData.circ_summary = summary;
103 return fetchCopy(barcode, id).then(function(res) {
105 if(!res.copy) { return $q.when(); }
107 return fetchCirc(copyData.copy).then(function(res) {
109 return fetchSummary(copyData.circ).then(function() {
120 // resolved with the last received copy
121 service.fetch = function(barcode, id, noListDupes) {
127 return service.retrieveCopyData(barcode, id)
128 .then(function(copyData) {
129 if(!copyData) { return $q.when(); }
130 //Make sure we're getting a completed copyData - no plain acp or circ objects
132 // flesh circ_lib locally
133 copyData.circ.circ_lib(egCore.org.get(copyData.circ.circ_lib()));
139 // use the existing copy if possible
140 flatCopy = service.copies.filter(
141 function(c) {return c.id == copyData.copy.id()})[0];
144 // flesh acn.owning_lib
145 copyData.copy.call_number().owning_lib(egCore.org.get(copyData.copy.call_number().owning_lib()));
148 flatCopy = egCore.idl.toHash(copyData.copy, true);
151 flatCopy._circ = egCore.idl.toHash(copyData.circ, true);
152 flatCopy._circ_summary = egCore.idl.toHash(copyData.circ_summary, true);
153 flatCopy._circ_lib = copyData.circ.circ_lib();
154 flatCopy._duration = copyData.circ.duration();
156 flatCopy.index = service.index++;
157 flatCopy.copy_alert_count = copyData.copy.copy_alerts().filter(function(aca) {
158 return !aca.ack_time();
161 service.copies.unshift(flatCopy);
164 //Get in-house use count
165 egCore.pcrud.search('aihu',
166 {item : flatCopy.id}, {}, {idlist : true, atomic : true})
167 .then(function(uses) {
168 flatCopy._inHouseUseCount = uses.length;
169 copyData.copy._inHouseUseCount = uses.length;
173 copy : copyData.copy,
174 index : flatCopy.index
181 service.add_copies_to_bucket = function(copy_list) {
182 if (copy_list.length == 0) return;
184 return $uibModal.open({
185 templateUrl: './cat/catalog/t_add_to_bucket',
190 ['$scope','$uibModalInstance',
191 function($scope , $uibModalInstance) {
193 $scope.bucket_id = 0;
194 $scope.newBucketName = '';
195 $scope.allBuckets = [];
199 'open-ils.actor.container.retrieve_by_class.authoritative',
200 egCore.auth.token(), egCore.auth.user().id(),
201 'copy', 'staff_client'
202 ).then(function(buckets) { $scope.allBuckets = buckets; });
204 $scope.add_to_bucket = function() {
206 angular.forEach(copy_list, function (cp) {
207 var item = new egCore.idl.ccbi()
208 item.bucket($scope.bucket_id);
209 item.target_copy(cp);
213 'open-ils.actor.container.item.create',
214 egCore.auth.token(), 'copy', item
218 return $q.all(promises).then(function() {
219 $uibModalInstance.close();
224 $scope.add_to_new_bucket = function() {
225 var bucket = new egCore.idl.ccb();
226 bucket.owner(egCore.auth.user().id());
227 bucket.name($scope.newBucketName);
228 bucket.description('');
229 bucket.btype('staff_client');
231 return egCore.net.request(
233 'open-ils.actor.container.create',
234 egCore.auth.token(), 'copy', bucket
235 ).then(function(bucket) {
236 $scope.bucket_id = bucket;
237 $scope.add_to_bucket();
241 $scope.cancel = function() {
242 $uibModalInstance.dismiss();
248 service.make_copies_bookable = function(items) {
250 var copies_by_record = {};
251 var record_list = [];
255 var record_id = item['call_number.record.id'];
256 if (typeof copies_by_record[ record_id ] == 'undefined') {
257 copies_by_record[ record_id ] = [];
258 record_list.push( record_id );
260 copies_by_record[ record_id ].push(item.id);
265 var combined_results = [];
266 angular.forEach(record_list, function(record_id) {
270 'open-ils.booking.resources.create_from_copies',
272 copies_by_record[record_id]
273 ).then(function(results) {
274 if (results && results['brsrc']) {
275 combined_results = combined_results.concat(results['brsrc']);
281 $q.all(promises).then(function() {
282 if (combined_results.length > 0) {
284 template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
289 ['$scope','$location','egCore','$uibModalInstance',
290 function($scope , $location , egCore , $uibModalInstance) {
293 ses : egCore.auth.token(),
294 resultant_brsrc : combined_results.map(function(o) { return o[0]; })
297 var booking_path = '/eg/conify/global/booking/resource';
299 $scope.booking_admin_url =
300 $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
307 service.book_copies_now = function(items) {
308 var copies_by_record = {};
309 var record_list = [];
313 var record_id = item['call_number.record.id'];
314 if (typeof copies_by_record[ record_id ] == 'undefined') {
315 copies_by_record[ record_id ] = [];
316 record_list.push( record_id );
318 copies_by_record[ record_id ].push(item.id);
323 var combined_brt = [];
324 var combined_brsrc = [];
325 angular.forEach(record_list, function(record_id) {
329 'open-ils.booking.resources.create_from_copies',
331 copies_by_record[record_id]
332 ).then(function(results) {
333 if (results && results['brt']) {
334 combined_brt = combined_brt.concat(results['brt']);
336 if (results && results['brsrc']) {
337 combined_brsrc = combined_brsrc.concat(results['brsrc']);
343 $q.all(promises).then(function() {
344 if (combined_brt.length > 0 || combined_brsrc.length > 0) {
346 template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
351 ['$scope','$location','egCore','$uibModalInstance',
352 function($scope , $location , egCore , $uibModalInstance) {
355 ses : egCore.auth.token(),
356 bresv_interface_opts : {
359 ,brsrc : combined_brsrc
364 var booking_path = '/eg/booking/reservation';
366 $scope.booking_admin_url =
367 $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
375 service.requestItems = function(copy_list) {
376 if (copy_list.length == 0) return;
378 return $uibModal.open({
379 templateUrl: './cat/catalog/t_request_items',
383 ['$scope','$uibModalInstance','egUser',
384 function($scope , $uibModalInstance , egUser) {
386 $scope.first_user_fetch = true;
390 copy_list : copy_list,
391 pickup_lib: egCore.org.get(egCore.auth.user().ws_ou()),
392 user : egCore.auth.user().id()
395 egUser.get( $scope.hold_data.user ).then(function(u) {
397 $scope.barcode = u.card().barcode();
398 $scope.user_name = egUser.format_name(u);
399 $scope.hold_data.user = u.id();
402 $scope.user_name = '';
404 $scope.$watch('barcode', function (n) {
405 if (!$scope.first_user_fetch) {
406 egUser.getByBarcode(n).then(function(u) {
408 $scope.user_name = egUser.format_name(u);
409 $scope.hold_data.user = u.id();
412 $scope.user_name = '';
413 delete $scope.hold_data.user;
416 $scope.first_user_fetch = false;
419 $scope.ok = function(h) {
422 hold_type : h.hold_type,
423 pickup_lib: h.pickup_lib.id(),
429 'open-ils.circ.holds.test_and_create.batch.override',
430 egCore.auth.token(), args, h.copy_list
433 $uibModalInstance.close();
436 $scope.cancel = function($event) {
437 $uibModalInstance.dismiss();
438 $event.preventDefault();
444 service.attach_to_peer_bib = function(items) {
445 if (items.length == 0) return;
447 egCore.hatch.getItem('eg.cat.marked_conjoined_record').then(function(target_record) {
448 if (!target_record) return;
450 return $uibModal.open({
451 templateUrl: './cat/catalog/t_conjoined_selector',
455 ['$scope','$uibModalInstance',
456 function($scope , $uibModalInstance) {
457 $scope.update = false;
459 $scope.peer_type = null;
460 $scope.peer_type_list = [];
462 get_peer_types = function() {
464 return $q.when(egCore.env.bpt.list);
466 return egCore.pcrud.retrieveAll('bpt', null, {atomic : true})
467 .then(function(list) {
468 egCore.env.absorbList(list, 'bpt');
473 get_peer_types().then(function(list){
474 $scope.peer_type_list = list;
477 $scope.ok = function(type) {
480 angular.forEach(items, function (cp) {
481 var n = new egCore.idl.bpbcm();
483 n.peer_record(target_record);
484 n.target_copy(cp.id);
486 promises.push(egCore.pcrud.create(n).then(function(){service.add_barcode_to_list(cp.barcode)}));
489 return $q.all(promises).then(function(){$uibModalInstance.close()});
492 $scope.cancel = function($event) {
493 $uibModalInstance.dismiss();
494 $event.preventDefault();
501 service.selectedHoldingsCopyDelete = function (items) {
502 if (items.length == 0) return;
504 var copy_objects = [];
505 egCore.pcrud.search('acp',
506 {deleted : 'f', id : items.map(function(el){return el.id;}) },
507 { flesh : 1, flesh_fields : { acp : ['call_number'] } }
511 var perCnCopies = {};
521 var cn_id = cp.call_number().id();
522 if (!cnHash[cn_id]) {
523 cnHash[cn_id] = cp.call_number();
524 perCnCopies[cn_id] = [cp];
526 perCnCopies[cn_id].push(cp);
528 cp.call_number(cn_id); // prevent loops in JSON-ification
532 angular.forEach(perCnCopies, function (v, k) {
537 angular.forEach(cnHash, function (v, k) {
541 if (cnList.length == 0) return;
545 egConfirmDialog.open(
546 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES,
547 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES_MESSAGE,
548 {copies : cp_count, volumes : cn_count}
549 ).result.then(function() {
552 'open-ils.cat.asset.volume.fleshed.batch.update.override',
553 egCore.auth.token(), cnList, 1, flags
555 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
561 copy_objects.push(copy);
565 service.checkin = function (items) {
566 angular.forEach(items, function (cp) {
567 egCirc.checkin({copy_barcode:cp.barcode}).then(
568 function() { service.add_barcode_to_list(cp.barcode) }
573 service.renew = function (items) {
574 angular.forEach(items, function (cp) {
575 egCirc.renew({copy_barcode:cp.barcode}).then(
576 function() { service.add_barcode_to_list(cp.barcode) }
581 service.cancel_transit = function (items) {
582 angular.forEach(items, function(cp) {
583 egCirc.find_copy_transit(null, {copy_barcode:cp.barcode})
584 .then(function(t) { return egCirc.abort_transit(t.id()) })
585 .then(function() { return service.add_barcode_to_list(cp.barcode) });
589 service.selectedHoldingsDamaged = function (items) {
590 angular.forEach(items, function(cp) {
592 egCirc.mark_damaged({
601 service.selectedHoldingsMissing = function (items) {
602 egCirc.mark_missing(items.map(function(el){return el.id;})).then(function(){
603 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
607 service.gatherSelectedRecordIds = function (items) {
612 if (rid_list.indexOf(item['call_number.record.id']) == -1)
613 rid_list.push(item['call_number.record.id'])
619 service.gatherSelectedVolumeIds = function (items,rid) {
624 if (rid && item['call_number.record.id'] != rid) return;
625 if (cn_id_list.indexOf(item['call_number.id']) == -1)
626 cn_id_list.push(item['call_number.id'])
632 service.gatherSelectedHoldingsIds = function (items,rid) {
637 if (rid && item['call_number.record.id'] != rid) return;
638 cp_id_list.push(item.id)
644 service.spawnHoldingsAdd = function (items,use_vols,use_copies){
645 angular.forEach(service.gatherSelectedRecordIds(items), function (r) {
647 if (use_copies) { // just a copy on existing volumes
648 angular.forEach(service.gatherSelectedVolumeIds(items,r), function (v) {
649 raw.push( {callnumber : v} );
651 } else if (use_vols) {
653 service.gatherSelectedHoldingsIds(items,r),
655 angular.forEach(items, function(item) {
656 if (i == item.id) raw.push({owner : item['call_number.owning_lib']});
662 if (raw.length == 0) raw.push({});
666 'open-ils.actor.anon_cache.set_value',
667 null, 'edit-these-copies', {
673 ).then(function(key) {
675 var url = egCore.env.basePath + 'cat/volcopy/' + key;
676 $timeout(function() { $window.open(url, '_blank') });
678 alert('Could not create anonymous cache key!');
684 service.spawnHoldingsEdit = function (items,hide_vols,hide_copies){
686 angular.forEach(items, function(i){
692 'open-ils.actor.anon_cache.set_value',
696 record_id: 0, // disables record summary
699 hide_vols : hide_vols,
700 hide_copies : hide_copies
701 }).then(function(key) {
703 var url = egCore.env.basePath + 'cat/volcopy/' + key;
704 $timeout(function() { $window.open(url, '_blank') });
706 alert('Could not create anonymous cache key!');
711 service.replaceBarcodes = function(items) {
712 angular.forEach(items, function (cp) {
714 templateUrl: './cat/share/t_replace_barcode',
718 ['$scope','$uibModalInstance',
719 function($scope , $uibModalInstance) {
720 $scope.isModal = true;
721 $scope.focusBarcode = false;
722 $scope.focusBarcode2 = true;
723 $scope.barcode1 = cp.barcode;
725 $scope.updateBarcode = function() {
726 $scope.copyNotFound = false;
727 $scope.updateOK = false;
729 egCore.pcrud.search('acp',
730 {deleted : 'f', barcode : $scope.barcode1})
731 .then(function(copy) {
734 $scope.focusBarcode = true;
735 $scope.copyNotFound = true;
739 $scope.copyId = copy.id();
740 copy.barcode($scope.barcode2);
742 egCore.pcrud.update(copy).then(function(stat) {
743 $scope.updateOK = stat;
744 $scope.focusBarcode = true;
745 if (stat) service.add_barcode_to_list(copy.barcode());
749 $uibModalInstance.close();
752 $scope.cancel = function($event) {
753 $uibModalInstance.dismiss();
754 $event.preventDefault();
762 // this "transfers" selected copies to a new owning library,
763 // auto-creating volumes and deleting unused volumes as required.
764 service.changeItemOwningLib = function(items) {
765 var xfer_target = egCore.hatch.getLocalItem('eg.cat.volume_transfer_target');
766 if (!xfer_target || !items.length) {
769 var vols_to_move = {};
770 var copies_to_move = {};
771 angular.forEach(items, function(item) {
772 if (item['call_number.owning_lib'] != xfer_target) {
773 if (item['call_number.id'] in vols_to_move) {
774 copies_to_move[item['call_number.id']].push(item.id);
776 vols_to_move[item['call_number.id']] = {
777 label : item['call_number.label'],
778 label_class : item['call_number.label_class'],
779 record : item['call_number.record.id'],
780 prefix : item['call_number.prefix.id'],
781 suffix : item['call_number.suffix.id']
783 copies_to_move[item['call_number.id']] = new Array;
784 copies_to_move[item['call_number.id']].push(item.id);
790 angular.forEach(vols_to_move, function(vol) {
791 promises.push(egCore.net.request(
793 'open-ils.cat.call_number.find_or_create',
801 ).then(function(resp) {
802 var evt = egCore.evt.parse(resp);
804 return egCore.net.request(
806 'open-ils.cat.transfer_copies_to_volume',
809 copies_to_move[vol.id]
818 function(){ service.add_barcode_to_list(cp.barcode) }
825 service.transferItems = function (items){
826 var xfer_target = egCore.hatch.getLocalItem('eg.cat.item_transfer_target');
827 var copy_ids = service.gatherSelectedHoldingsIds(items);
828 if (xfer_target && copy_ids.length > 0) {
831 'open-ils.cat.transfer_copies_to_volume',
836 function(resp) { // oncomplete
837 var evt = egCore.evt.parse(resp);
839 egConfirmDialog.open(
840 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_TITLE,
841 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_BODY,
843 ).result.then(function() {
846 'open-ils.cat.transfer_copies_to_volume.override',
850 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
853 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
856 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
866 service.mark_missing_pieces = function(copy) {
867 var b = copy.barcode();
868 var t = egCore.idl.toHash(copy.call_number()).record.title;
869 egConfirmDialog.open(
870 egCore.strings.CONFIRM_MARK_MISSING_TITLE,
871 egCore.strings.CONFIRM_MARK_MISSING_BODY,
872 { barcode : b, title : t }
873 ).result.then(function() {
875 // kick off mark missing
876 return egCore.net.request(
878 'open-ils.circ.mark_item_missing_pieces',
879 egCore.auth.token(), copy.id()
882 }).then(function(resp) {
883 var evt = egCore.evt.parse(resp); // should always produce event
885 if (evt.textcode == 'ACTION_CIRCULATION_NOT_FOUND') {
886 return egAlertDialog.open(
887 egCore.strings.CIRC_NOT_FOUND, {barcode : copy.barcode()});
890 var payload = evt.payload;
892 // TODO: open copy editor inline? new tab?
894 // print the missing pieces slip
895 var promise = $q.when();
897 // wait for completion, since it may spawn a confirm dialog
898 promise = egCore.print.print({
900 content_type : 'text/html',
901 content : payload.slip.template_output().data()
905 if (payload.letter) {
906 $scope.letter = payload.letter.template_output().data();
909 // apply patron penalty
911 promise.then(function() {
912 egCirc.create_penalty(payload.circ.usr())
919 service.print_spine_labels = function(copy_ids){
922 'open-ils.actor.anon_cache.set_value',
923 null, 'print-labels-these-copies', {
926 ).then(function(key) {
928 var url = egCore.env.basePath + 'cat/printlabels/' + key;
929 $timeout(function() { $window.open(url, '_blank') });
931 alert('Could not create anonymous cache key!');