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', 'editor', 'circ_as_type', 'last_copy_inventory'],
20 acn : ['record','prefix','suffix','label_class'],
21 bre : ['simple_record','creator','editor'],
22 alci : ['inventory_workstation']
25 // avoid fleshing MARC on the bre
26 // note: don't add simple_record.. not sure why
27 bre : ['id','tcn_value','creator','editor'],
37 'checkin_workstation',
45 order_by : {combcirc : 'xact_start desc'},
49 //Retrieve separate copy, aacs, and accs information
50 service.getCopy = function(barcode, id) {
52 // handle barcode completion
53 return egCirc.handle_barcode_completion(barcode)
54 .then(function(actual_barcode) {
55 return egCore.pcrud.search(
56 'acp', {barcode : actual_barcode, deleted : 'f'},
57 service.flesh).then(function(copy) {return copy});
61 return egCore.pcrud.retrieve( 'acp', id, service.flesh)
62 .then(function(copy) {return copy});
65 service.getCirc = function(id) {
66 return egCore.pcrud.search('combcirc', { target_copy : id },
67 service.circFlesh).then(function(circ) {return circ});
70 service.getSummary = function(id) {
71 return circ_summary = egCore.net.request(
73 'open-ils.circ.renewal_chain.retrieve_by_circ.summary',
74 egCore.auth.token(), id).then(
75 function(circ_summary) {return circ_summary});
78 //Combine copy, circ, and accs information
79 service.retrieveCopyData = function(barcode, id) {
82 var fetchCopy = function(barcode, id) {
83 return service.getCopy(barcode, id)
84 .then(function(copy) {
89 var fetchCirc = function(copy) {
90 return service.getCirc(copy.id())
91 .then(function(circ) {
96 var fetchSummary = function(circ) {
97 return service.getSummary(circ.id())
98 .then(function(summary) {
99 copyData.circ_summary = summary;
104 return fetchCopy(barcode, id).then(function(res) {
106 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();
155 flatCopy._circ_ws = flatCopy._circ_summary.last_renewal_workstation ?
156 flatCopy._circ_summary.last_renewal_workstation :
157 flatCopy._circ_summary.checkout_workstation ?
158 flatCopy._circ_summary.checkout_workstation :
161 flatCopy.index = service.index++;
162 flatCopy.copy_alert_count = copyData.copy.copy_alerts().filter(function(aca) {
163 return !aca.ack_time();
166 service.copies.unshift(flatCopy);
169 //Get in-house use count
170 egCore.pcrud.search('aihu',
171 {item : flatCopy.id}, {}, {idlist : true, atomic : true})
172 .then(function(uses) {
173 flatCopy._inHouseUseCount = uses.length;
174 copyData.copy._inHouseUseCount = uses.length;
177 //Get Monograph Parts
178 egCore.pcrud.search('acpm',
179 {target_copy: flatCopy.id},
180 { flesh : 1, flesh_fields : { acpm : ['part'] } },
182 .then(function(acpm_array) {
183 angular.forEach(acpm_array, function(acpm) {
184 flatCopy.parts = egCore.idl.toHash(acpm.part());
185 copyData.copy.parts = egCore.idl.toHash(acpm.part());
190 copy : copyData.copy,
191 index : flatCopy.index
198 service.updateInventory = function(copy_list, all_items, refresh) {
199 if (copy_list.length == 0) return;
200 return egCore.net.request(
202 'open-ils.circ.circulation.update_last_copy_inventory',
203 egCore.auth.token(), {copy_list: copy_list}
204 ).then(function(res) {
206 if (all_items) angular.forEach(copy_list, function(copy) {
207 angular.forEach(all_items, function(item) {
208 if (copy == item.id) {
209 egCore.pcrud.search('alci', {copy: copy},
210 {flesh: 1, flesh_fields:
211 {alci: ['inventory_workstation']}
212 }).then(function(alci) {
213 item._last_copy_inventory.inventory_date = alci.inventory_date();
214 item._last_copy_inventory._inventory_workstation_name =
215 alci.inventory_workstation().name();
220 return all_items || res;
225 service.add_copies_to_bucket = function(copy_list) {
226 if (copy_list.length == 0) return;
228 return $uibModal.open({
229 templateUrl: './cat/catalog/t_add_to_bucket',
234 ['$scope','$uibModalInstance',
235 function($scope , $uibModalInstance) {
237 $scope.bucket_id = 0;
238 $scope.newBucketName = '';
239 $scope.allBuckets = [];
243 'open-ils.actor.container.retrieve_by_class.authoritative',
244 egCore.auth.token(), egCore.auth.user().id(),
245 'copy', 'staff_client'
246 ).then(function(buckets) { $scope.allBuckets = buckets; });
248 $scope.add_to_bucket = function() {
250 angular.forEach(copy_list, function (cp) {
251 var item = new egCore.idl.ccbi()
252 item.bucket($scope.bucket_id);
253 item.target_copy(cp);
257 'open-ils.actor.container.item.create',
258 egCore.auth.token(), 'copy', item
262 return $q.all(promises).then(function() {
263 $uibModalInstance.close();
268 $scope.add_to_new_bucket = function() {
269 var bucket = new egCore.idl.ccb();
270 bucket.owner(egCore.auth.user().id());
271 bucket.name($scope.newBucketName);
272 bucket.description('');
273 bucket.btype('staff_client');
275 return egCore.net.request(
277 'open-ils.actor.container.create',
278 egCore.auth.token(), 'copy', bucket
279 ).then(function(bucket) {
280 $scope.bucket_id = bucket;
281 $scope.add_to_bucket();
285 $scope.cancel = function() {
286 $uibModalInstance.dismiss();
292 service.make_copies_bookable = function(items) {
294 var copies_by_record = {};
295 var record_list = [];
299 var record_id = item['call_number.record.id'];
300 if (typeof copies_by_record[ record_id ] == 'undefined') {
301 copies_by_record[ record_id ] = [];
302 record_list.push( record_id );
304 copies_by_record[ record_id ].push(item.id);
309 var combined_results = [];
310 angular.forEach(record_list, function(record_id) {
314 'open-ils.booking.resources.create_from_copies',
316 copies_by_record[record_id]
317 ).then(function(results) {
318 if (results && results['brsrc']) {
319 combined_results = combined_results.concat(results['brsrc']);
325 $q.all(promises).then(function() {
326 if (combined_results.length > 0) {
328 template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
333 ['$scope','$location','egCore','$uibModalInstance',
334 function($scope , $location , egCore , $uibModalInstance) {
337 ses : egCore.auth.token(),
338 resultant_brsrc : combined_results.map(function(o) { return o[0]; })
341 var booking_path = '/eg/conify/global/booking/resource';
343 $scope.booking_admin_url =
344 $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
351 service.book_copies_now = function(items) {
352 var copies_by_record = {};
353 var record_list = [];
357 var record_id = item['call_number.record.id'];
358 if (typeof copies_by_record[ record_id ] == 'undefined') {
359 copies_by_record[ record_id ] = [];
360 record_list.push( record_id );
362 copies_by_record[ record_id ].push(item.id);
367 var combined_brt = [];
368 var combined_brsrc = [];
369 angular.forEach(record_list, function(record_id) {
373 'open-ils.booking.resources.create_from_copies',
375 copies_by_record[record_id]
376 ).then(function(results) {
377 if (results && results['brt']) {
378 combined_brt = combined_brt.concat(results['brt']);
380 if (results && results['brsrc']) {
381 combined_brsrc = combined_brsrc.concat(results['brsrc']);
387 $q.all(promises).then(function() {
388 if (combined_brt.length > 0 || combined_brsrc.length > 0) {
390 template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
395 ['$scope','$location','egCore','$uibModalInstance',
396 function($scope , $location , egCore , $uibModalInstance) {
399 ses : egCore.auth.token(),
400 bresv_interface_opts : {
403 ,brsrc : combined_brsrc
408 var booking_path = '/eg/booking/reservation';
410 $scope.booking_admin_url =
411 $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
419 service.requestItems = function(copy_list) {
420 if (copy_list.length == 0) return;
422 return $uibModal.open({
423 templateUrl: './cat/catalog/t_request_items',
427 ['$scope','$uibModalInstance','egUser',
428 function($scope , $uibModalInstance , egUser) {
430 $scope.first_user_fetch = true;
434 copy_list : copy_list,
435 pickup_lib: egCore.org.get(egCore.auth.user().ws_ou()),
436 user : egCore.auth.user().id()
439 egUser.get( $scope.hold_data.user ).then(function(u) {
441 $scope.barcode = u.card().barcode();
442 $scope.user_name = egUser.format_name(u);
443 $scope.hold_data.user = u.id();
446 $scope.user_name = '';
448 $scope.$watch('barcode', function (n) {
449 if (!$scope.first_user_fetch) {
450 egUser.getByBarcode(n).then(function(u) {
452 $scope.user_name = egUser.format_name(u);
453 $scope.hold_data.user = u.id();
456 $scope.user_name = '';
457 delete $scope.hold_data.user;
460 $scope.first_user_fetch = false;
463 $scope.ok = function(h) {
466 hold_type : h.hold_type,
467 pickup_lib: h.pickup_lib.id(),
473 'open-ils.circ.holds.test_and_create.batch.override',
474 egCore.auth.token(), args, h.copy_list
477 $uibModalInstance.close();
480 $scope.cancel = function($event) {
481 $uibModalInstance.dismiss();
482 $event.preventDefault();
488 service.attach_to_peer_bib = function(items) {
489 if (items.length == 0) return;
491 egCore.hatch.getItem('eg.cat.marked_conjoined_record').then(function(target_record) {
492 if (!target_record) return;
494 return $uibModal.open({
495 templateUrl: './cat/catalog/t_conjoined_selector',
499 ['$scope','$uibModalInstance',
500 function($scope , $uibModalInstance) {
501 $scope.update = false;
503 $scope.peer_type = null;
504 $scope.peer_type_list = [];
506 get_peer_types = function() {
508 return $q.when(egCore.env.bpt.list);
510 return egCore.pcrud.retrieveAll('bpt', null, {atomic : true})
511 .then(function(list) {
512 egCore.env.absorbList(list, 'bpt');
517 get_peer_types().then(function(list){
518 $scope.peer_type_list = list;
521 $scope.ok = function(type) {
524 angular.forEach(items, function (cp) {
525 var n = new egCore.idl.bpbcm();
527 n.peer_record(target_record);
528 n.target_copy(cp.id);
530 promises.push(egCore.pcrud.create(n).then(function(){service.add_barcode_to_list(cp.barcode)}));
533 return $q.all(promises).then(function(){$uibModalInstance.close()});
536 $scope.cancel = function($event) {
537 $uibModalInstance.dismiss();
538 $event.preventDefault();
545 service.selectedHoldingsCopyDelete = function (items) {
546 if (items.length == 0) return;
548 var copy_objects = [];
549 egCore.pcrud.search('acp',
550 {deleted : 'f', id : items.map(function(el){return el.id;}) },
551 { flesh : 1, flesh_fields : { acp : ['call_number'] } }
555 var perCnCopies = {};
565 var cn_id = cp.call_number().id();
566 if (!cnHash[cn_id]) {
567 cnHash[cn_id] = cp.call_number();
568 perCnCopies[cn_id] = [cp];
570 perCnCopies[cn_id].push(cp);
572 cp.call_number(cn_id); // prevent loops in JSON-ification
576 angular.forEach(perCnCopies, function (v, k) {
581 angular.forEach(cnHash, function (v, k) {
585 if (cnList.length == 0) return;
589 egConfirmDialog.open(
590 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES,
591 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES_MESSAGE,
592 {copies : cp_count, volumes : cn_count}
593 ).result.then(function() {
596 'open-ils.cat.asset.volume.fleshed.batch.update.override',
597 egCore.auth.token(), cnList, 1, flags
599 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
605 copy_objects.push(copy);
609 service.checkin = function (items) {
610 angular.forEach(items, function (cp) {
611 egCirc.checkin({copy_barcode:cp.barcode}).then(
612 function() { service.add_barcode_to_list(cp.barcode) }
617 service.renew = function (items) {
618 angular.forEach(items, function (cp) {
619 egCirc.renew({copy_barcode:cp.barcode}).then(
620 function() { service.add_barcode_to_list(cp.barcode) }
625 service.cancel_transit = function (items) {
626 angular.forEach(items, function(cp) {
627 egCirc.find_copy_transit(null, {copy_barcode:cp.barcode})
628 .then(function(t) { return egCirc.abort_transit(t.id()) })
629 .then(function() { return service.add_barcode_to_list(cp.barcode) });
633 service.selectedHoldingsDamaged = function (items) {
634 angular.forEach(items, function(cp) {
636 egCirc.mark_damaged({
645 service.selectedHoldingsMissing = function (items) {
646 egCirc.mark_missing(items.map(function(el){return el.id;})).then(function(){
647 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
651 service.gatherSelectedRecordIds = function (items) {
656 if (rid_list.indexOf(item['call_number.record.id']) == -1)
657 rid_list.push(item['call_number.record.id'])
663 service.gatherSelectedVolumeIds = function (items,rid) {
668 if (rid && item['call_number.record.id'] != rid) return;
669 if (cn_id_list.indexOf(item['call_number.id']) == -1)
670 cn_id_list.push(item['call_number.id'])
676 service.gatherSelectedHoldingsIds = function (items,rid) {
681 if (rid && item['call_number.record.id'] != rid) return;
682 cp_id_list.push(item.id)
688 service.spawnHoldingsAdd = function (items,use_vols,use_copies){
689 angular.forEach(service.gatherSelectedRecordIds(items), function (r) {
691 if (use_copies) { // just a copy on existing volumes
692 angular.forEach(service.gatherSelectedVolumeIds(items,r), function (v) {
693 raw.push( {callnumber : v} );
695 } else if (use_vols) {
697 service.gatherSelectedHoldingsIds(items,r),
699 angular.forEach(items, function(item) {
700 if (i == item.id) raw.push({owner : item['call_number.owning_lib']});
706 if (raw.length == 0) raw.push({});
710 'open-ils.actor.anon_cache.set_value',
711 null, 'edit-these-copies', {
717 ).then(function(key) {
719 var url = egCore.env.basePath + 'cat/volcopy/' + key;
720 $timeout(function() { $window.open(url, '_blank') });
722 alert('Could not create anonymous cache key!');
728 service.spawnHoldingsEdit = function (items,hide_vols,hide_copies){
730 angular.forEach(items, function(i){
736 'open-ils.actor.anon_cache.set_value',
740 record_id: 0, // disables record summary
743 hide_vols : hide_vols,
744 hide_copies : hide_copies
745 }).then(function(key) {
747 var url = egCore.env.basePath + 'cat/volcopy/' + key;
748 $timeout(function() { $window.open(url, '_blank') });
750 alert('Could not create anonymous cache key!');
755 service.replaceBarcodes = function(items) {
756 angular.forEach(items, function (cp) {
758 templateUrl: './cat/share/t_replace_barcode',
762 ['$scope','$uibModalInstance',
763 function($scope , $uibModalInstance) {
764 $scope.isModal = true;
765 $scope.focusBarcode = false;
766 $scope.focusBarcode2 = true;
767 $scope.barcode1 = cp.barcode;
769 $scope.updateBarcode = function() {
770 $scope.copyNotFound = false;
771 $scope.updateOK = false;
773 egCore.pcrud.search('acp',
774 {deleted : 'f', barcode : $scope.barcode1})
775 .then(function(copy) {
778 $scope.focusBarcode = true;
779 $scope.copyNotFound = true;
783 $scope.copyId = copy.id();
784 copy.barcode($scope.barcode2);
786 egCore.pcrud.update(copy).then(function(stat) {
787 $scope.updateOK = stat;
788 $scope.focusBarcode = true;
789 if (stat) service.add_barcode_to_list(copy.barcode());
793 $uibModalInstance.close();
796 $scope.cancel = function($event) {
797 $uibModalInstance.dismiss();
798 $event.preventDefault();
806 // this "transfers" selected copies to a new owning library,
807 // auto-creating volumes and deleting unused volumes as required.
808 service.changeItemOwningLib = function(items) {
809 var xfer_target = egCore.hatch.getLocalItem('eg.cat.transfer_target_lib');
810 if (!xfer_target || !items.length) {
813 var vols_to_move = {};
814 var copies_to_move = {};
815 angular.forEach(items, function(item) {
816 if (item['call_number.owning_lib'] != xfer_target) {
817 if (item['call_number.id'] in vols_to_move) {
818 copies_to_move[item['call_number.id']].push(item.id);
820 vols_to_move[item['call_number.id']] = {
821 label : item['call_number.label'],
822 label_class : item['call_number.label_class'],
823 record : item['call_number.record.id'],
824 prefix : item['call_number.prefix.id'],
825 suffix : item['call_number.suffix.id']
827 copies_to_move[item['call_number.id']] = new Array;
828 copies_to_move[item['call_number.id']].push(item.id);
834 angular.forEach(vols_to_move, function(vol, vol_id) {
835 promises.push(egCore.net.request(
837 'open-ils.cat.call_number.find_or_create',
845 ).then(function(resp) {
846 var evt = egCore.evt.parse(resp);
848 return egCore.net.request(
850 'open-ils.cat.transfer_copies_to_volume',
853 copies_to_move[vol_id]
861 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
866 service.transferItems = function (items){
867 var xfer_target = egCore.hatch.getLocalItem('eg.cat.transfer_target_vol');
868 var copy_ids = service.gatherSelectedHoldingsIds(items);
869 if (xfer_target && copy_ids.length > 0) {
872 'open-ils.cat.transfer_copies_to_volume',
877 function(resp) { // oncomplete
878 var evt = egCore.evt.parse(resp);
880 egConfirmDialog.open(
881 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_TITLE,
882 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_BODY,
884 ).result.then(function() {
887 'open-ils.cat.transfer_copies_to_volume.override',
891 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
894 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
897 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
907 service.mark_missing_pieces = function(copy,outer_scope) {
908 var b = copy.barcode();
909 var t = egCore.idl.toHash(copy.call_number()).record.title;
910 egConfirmDialog.open(
911 egCore.strings.CONFIRM_MARK_MISSING_TITLE,
912 egCore.strings.CONFIRM_MARK_MISSING_BODY,
913 { barcode : b, title : t }
914 ).result.then(function() {
916 // kick off mark missing
917 return egCore.net.request(
919 'open-ils.circ.mark_item_missing_pieces',
920 egCore.auth.token(), copy.id()
923 }).then(function(resp) {
924 var evt = egCore.evt.parse(resp); // should always produce event
926 if (evt.textcode == 'ACTION_CIRCULATION_NOT_FOUND') {
927 return egAlertDialog.open(
928 egCore.strings.CIRC_NOT_FOUND, {barcode : copy.barcode()});
931 var payload = evt.payload;
933 // TODO: open copy editor inline? new tab?
935 // print the missing pieces slip
936 var promise = $q.when();
938 // wait for completion, since it may spawn a confirm dialog
939 promise = egCore.print.print({
941 content_type : 'text/html',
942 content : payload.slip.template_output().data()
946 if (payload.letter) {
947 outer_scope.letter = payload.letter.template_output().data();
950 // apply patron penalty
952 promise.then(function() {
953 egCirc.create_penalty(payload.circ.usr())
960 service.print_spine_labels = function(copy_ids){
963 'open-ils.actor.anon_cache.set_value',
964 null, 'print-labels-these-copies', {
967 ).then(function(key) {
969 var url = egCore.env.basePath + 'cat/printlabels/' + key;
970 $timeout(function() { $window.open(url, '_blank') });
972 alert('Could not create anonymous cache key!');
979 .filter('string_pick', function() { return function(i){ return arguments[i] || ''; }; })