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();
155 flatCopy._checkout_ws = copyData.circ.workstation().name();
157 flatCopy.index = service.index++;
158 flatCopy.copy_alert_count = copyData.copy.copy_alerts().filter(function(aca) {
159 return !aca.ack_time();
162 service.copies.unshift(flatCopy);
165 //Get in-house use count
166 egCore.pcrud.search('aihu',
167 {item : flatCopy.id}, {}, {idlist : true, atomic : true})
168 .then(function(uses) {
169 flatCopy._inHouseUseCount = uses.length;
170 copyData.copy._inHouseUseCount = uses.length;
174 copy : copyData.copy,
175 index : flatCopy.index
182 service.add_copies_to_bucket = function(copy_list) {
183 if (copy_list.length == 0) return;
185 return $uibModal.open({
186 templateUrl: './cat/catalog/t_add_to_bucket',
191 ['$scope','$uibModalInstance',
192 function($scope , $uibModalInstance) {
194 $scope.bucket_id = 0;
195 $scope.newBucketName = '';
196 $scope.allBuckets = [];
200 'open-ils.actor.container.retrieve_by_class.authoritative',
201 egCore.auth.token(), egCore.auth.user().id(),
202 'copy', 'staff_client'
203 ).then(function(buckets) { $scope.allBuckets = buckets; });
205 $scope.add_to_bucket = function() {
207 angular.forEach(copy_list, function (cp) {
208 var item = new egCore.idl.ccbi()
209 item.bucket($scope.bucket_id);
210 item.target_copy(cp);
214 'open-ils.actor.container.item.create',
215 egCore.auth.token(), 'copy', item
219 return $q.all(promises).then(function() {
220 $uibModalInstance.close();
225 $scope.add_to_new_bucket = function() {
226 var bucket = new egCore.idl.ccb();
227 bucket.owner(egCore.auth.user().id());
228 bucket.name($scope.newBucketName);
229 bucket.description('');
230 bucket.btype('staff_client');
232 return egCore.net.request(
234 'open-ils.actor.container.create',
235 egCore.auth.token(), 'copy', bucket
236 ).then(function(bucket) {
237 $scope.bucket_id = bucket;
238 $scope.add_to_bucket();
242 $scope.cancel = function() {
243 $uibModalInstance.dismiss();
249 service.make_copies_bookable = function(items) {
251 var copies_by_record = {};
252 var record_list = [];
256 var record_id = item['call_number.record.id'];
257 if (typeof copies_by_record[ record_id ] == 'undefined') {
258 copies_by_record[ record_id ] = [];
259 record_list.push( record_id );
261 copies_by_record[ record_id ].push(item.id);
266 var combined_results = [];
267 angular.forEach(record_list, function(record_id) {
271 'open-ils.booking.resources.create_from_copies',
273 copies_by_record[record_id]
274 ).then(function(results) {
275 if (results && results['brsrc']) {
276 combined_results = combined_results.concat(results['brsrc']);
282 $q.all(promises).then(function() {
283 if (combined_results.length > 0) {
285 template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
290 ['$scope','$location','egCore','$uibModalInstance',
291 function($scope , $location , egCore , $uibModalInstance) {
294 ses : egCore.auth.token(),
295 resultant_brsrc : combined_results.map(function(o) { return o[0]; })
298 var booking_path = '/eg/conify/global/booking/resource';
300 $scope.booking_admin_url =
301 $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
308 service.book_copies_now = function(items) {
309 var copies_by_record = {};
310 var record_list = [];
314 var record_id = item['call_number.record.id'];
315 if (typeof copies_by_record[ record_id ] == 'undefined') {
316 copies_by_record[ record_id ] = [];
317 record_list.push( record_id );
319 copies_by_record[ record_id ].push(item.id);
324 var combined_brt = [];
325 var combined_brsrc = [];
326 angular.forEach(record_list, function(record_id) {
330 'open-ils.booking.resources.create_from_copies',
332 copies_by_record[record_id]
333 ).then(function(results) {
334 if (results && results['brt']) {
335 combined_brt = combined_brt.concat(results['brt']);
337 if (results && results['brsrc']) {
338 combined_brsrc = combined_brsrc.concat(results['brsrc']);
344 $q.all(promises).then(function() {
345 if (combined_brt.length > 0 || combined_brsrc.length > 0) {
347 template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
352 ['$scope','$location','egCore','$uibModalInstance',
353 function($scope , $location , egCore , $uibModalInstance) {
356 ses : egCore.auth.token(),
357 bresv_interface_opts : {
360 ,brsrc : combined_brsrc
365 var booking_path = '/eg/booking/reservation';
367 $scope.booking_admin_url =
368 $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
376 service.requestItems = function(copy_list) {
377 if (copy_list.length == 0) return;
379 return $uibModal.open({
380 templateUrl: './cat/catalog/t_request_items',
384 ['$scope','$uibModalInstance','egUser',
385 function($scope , $uibModalInstance , egUser) {
387 $scope.first_user_fetch = true;
391 copy_list : copy_list,
392 pickup_lib: egCore.org.get(egCore.auth.user().ws_ou()),
393 user : egCore.auth.user().id()
396 egUser.get( $scope.hold_data.user ).then(function(u) {
398 $scope.barcode = u.card().barcode();
399 $scope.user_name = egUser.format_name(u);
400 $scope.hold_data.user = u.id();
403 $scope.user_name = '';
405 $scope.$watch('barcode', function (n) {
406 if (!$scope.first_user_fetch) {
407 egUser.getByBarcode(n).then(function(u) {
409 $scope.user_name = egUser.format_name(u);
410 $scope.hold_data.user = u.id();
413 $scope.user_name = '';
414 delete $scope.hold_data.user;
417 $scope.first_user_fetch = false;
420 $scope.ok = function(h) {
423 hold_type : h.hold_type,
424 pickup_lib: h.pickup_lib.id(),
430 'open-ils.circ.holds.test_and_create.batch.override',
431 egCore.auth.token(), args, h.copy_list
434 $uibModalInstance.close();
437 $scope.cancel = function($event) {
438 $uibModalInstance.dismiss();
439 $event.preventDefault();
445 service.attach_to_peer_bib = function(items) {
446 if (items.length == 0) return;
448 egCore.hatch.getItem('eg.cat.marked_conjoined_record').then(function(target_record) {
449 if (!target_record) return;
451 return $uibModal.open({
452 templateUrl: './cat/catalog/t_conjoined_selector',
456 ['$scope','$uibModalInstance',
457 function($scope , $uibModalInstance) {
458 $scope.update = false;
460 $scope.peer_type = null;
461 $scope.peer_type_list = [];
463 get_peer_types = function() {
465 return $q.when(egCore.env.bpt.list);
467 return egCore.pcrud.retrieveAll('bpt', null, {atomic : true})
468 .then(function(list) {
469 egCore.env.absorbList(list, 'bpt');
474 get_peer_types().then(function(list){
475 $scope.peer_type_list = list;
478 $scope.ok = function(type) {
481 angular.forEach(items, function (cp) {
482 var n = new egCore.idl.bpbcm();
484 n.peer_record(target_record);
485 n.target_copy(cp.id);
487 promises.push(egCore.pcrud.create(n).then(function(){service.add_barcode_to_list(cp.barcode)}));
490 return $q.all(promises).then(function(){$uibModalInstance.close()});
493 $scope.cancel = function($event) {
494 $uibModalInstance.dismiss();
495 $event.preventDefault();
502 service.selectedHoldingsCopyDelete = function (items) {
503 if (items.length == 0) return;
505 var copy_objects = [];
506 egCore.pcrud.search('acp',
507 {deleted : 'f', id : items.map(function(el){return el.id;}) },
508 { flesh : 1, flesh_fields : { acp : ['call_number'] } }
512 var perCnCopies = {};
522 var cn_id = cp.call_number().id();
523 if (!cnHash[cn_id]) {
524 cnHash[cn_id] = cp.call_number();
525 perCnCopies[cn_id] = [cp];
527 perCnCopies[cn_id].push(cp);
529 cp.call_number(cn_id); // prevent loops in JSON-ification
533 angular.forEach(perCnCopies, function (v, k) {
538 angular.forEach(cnHash, function (v, k) {
542 if (cnList.length == 0) return;
546 egConfirmDialog.open(
547 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES,
548 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES_MESSAGE,
549 {copies : cp_count, volumes : cn_count}
550 ).result.then(function() {
553 'open-ils.cat.asset.volume.fleshed.batch.update.override',
554 egCore.auth.token(), cnList, 1, flags
556 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
562 copy_objects.push(copy);
566 service.checkin = function (items) {
567 angular.forEach(items, function (cp) {
568 egCirc.checkin({copy_barcode:cp.barcode}).then(
569 function() { service.add_barcode_to_list(cp.barcode) }
574 service.renew = function (items) {
575 angular.forEach(items, function (cp) {
576 egCirc.renew({copy_barcode:cp.barcode}).then(
577 function() { service.add_barcode_to_list(cp.barcode) }
582 service.cancel_transit = function (items) {
583 angular.forEach(items, function(cp) {
584 egCirc.find_copy_transit(null, {copy_barcode:cp.barcode})
585 .then(function(t) { return egCirc.abort_transit(t.id()) })
586 .then(function() { return service.add_barcode_to_list(cp.barcode) });
590 service.selectedHoldingsDamaged = function (items) {
591 angular.forEach(items, function(cp) {
593 egCirc.mark_damaged({
602 service.selectedHoldingsMissing = function (items) {
603 egCirc.mark_missing(items.map(function(el){return el.id;})).then(function(){
604 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
608 service.gatherSelectedRecordIds = function (items) {
613 if (rid_list.indexOf(item['call_number.record.id']) == -1)
614 rid_list.push(item['call_number.record.id'])
620 service.gatherSelectedVolumeIds = function (items,rid) {
625 if (rid && item['call_number.record.id'] != rid) return;
626 if (cn_id_list.indexOf(item['call_number.id']) == -1)
627 cn_id_list.push(item['call_number.id'])
633 service.gatherSelectedHoldingsIds = function (items,rid) {
638 if (rid && item['call_number.record.id'] != rid) return;
639 cp_id_list.push(item.id)
645 service.spawnHoldingsAdd = function (items,use_vols,use_copies){
646 angular.forEach(service.gatherSelectedRecordIds(items), function (r) {
648 if (use_copies) { // just a copy on existing volumes
649 angular.forEach(service.gatherSelectedVolumeIds(items,r), function (v) {
650 raw.push( {callnumber : v} );
652 } else if (use_vols) {
654 service.gatherSelectedHoldingsIds(items,r),
656 angular.forEach(items, function(item) {
657 if (i == item.id) raw.push({owner : item['call_number.owning_lib']});
663 if (raw.length == 0) raw.push({});
667 'open-ils.actor.anon_cache.set_value',
668 null, 'edit-these-copies', {
674 ).then(function(key) {
676 var url = egCore.env.basePath + 'cat/volcopy/' + key;
677 $timeout(function() { $window.open(url, '_blank') });
679 alert('Could not create anonymous cache key!');
685 service.spawnHoldingsEdit = function (items,hide_vols,hide_copies){
687 angular.forEach(items, function(i){
693 'open-ils.actor.anon_cache.set_value',
697 record_id: 0, // disables record summary
700 hide_vols : hide_vols,
701 hide_copies : hide_copies
702 }).then(function(key) {
704 var url = egCore.env.basePath + 'cat/volcopy/' + key;
705 $timeout(function() { $window.open(url, '_blank') });
707 alert('Could not create anonymous cache key!');
712 service.replaceBarcodes = function(items) {
713 angular.forEach(items, function (cp) {
715 templateUrl: './cat/share/t_replace_barcode',
719 ['$scope','$uibModalInstance',
720 function($scope , $uibModalInstance) {
721 $scope.isModal = true;
722 $scope.focusBarcode = false;
723 $scope.focusBarcode2 = true;
724 $scope.barcode1 = cp.barcode;
726 $scope.updateBarcode = function() {
727 $scope.copyNotFound = false;
728 $scope.updateOK = false;
730 egCore.pcrud.search('acp',
731 {deleted : 'f', barcode : $scope.barcode1})
732 .then(function(copy) {
735 $scope.focusBarcode = true;
736 $scope.copyNotFound = true;
740 $scope.copyId = copy.id();
741 copy.barcode($scope.barcode2);
743 egCore.pcrud.update(copy).then(function(stat) {
744 $scope.updateOK = stat;
745 $scope.focusBarcode = true;
746 if (stat) service.add_barcode_to_list(copy.barcode());
750 $uibModalInstance.close();
753 $scope.cancel = function($event) {
754 $uibModalInstance.dismiss();
755 $event.preventDefault();
763 // this "transfers" selected copies to a new owning library,
764 // auto-creating volumes and deleting unused volumes as required.
765 service.changeItemOwningLib = function(items) {
766 var xfer_target = egCore.hatch.getLocalItem('eg.cat.volume_transfer_target');
767 if (!xfer_target || !items.length) {
770 var vols_to_move = {};
771 var copies_to_move = {};
772 angular.forEach(items, function(item) {
773 if (item['call_number.owning_lib'] != xfer_target) {
774 if (item['call_number.id'] in vols_to_move) {
775 copies_to_move[item['call_number.id']].push(item.id);
777 vols_to_move[item['call_number.id']] = {
778 label : item['call_number.label'],
779 label_class : item['call_number.label_class'],
780 record : item['call_number.record.id'],
781 prefix : item['call_number.prefix.id'],
782 suffix : item['call_number.suffix.id']
784 copies_to_move[item['call_number.id']] = new Array;
785 copies_to_move[item['call_number.id']].push(item.id);
791 angular.forEach(vols_to_move, function(vol) {
792 promises.push(egCore.net.request(
794 'open-ils.cat.call_number.find_or_create',
802 ).then(function(resp) {
803 var evt = egCore.evt.parse(resp);
805 return egCore.net.request(
807 'open-ils.cat.transfer_copies_to_volume',
810 copies_to_move[vol.id]
819 function(){ service.add_barcode_to_list(cp.barcode) }
826 service.transferItems = function (items){
827 var xfer_target = egCore.hatch.getLocalItem('eg.cat.item_transfer_target');
828 var copy_ids = service.gatherSelectedHoldingsIds(items);
829 if (xfer_target && copy_ids.length > 0) {
832 'open-ils.cat.transfer_copies_to_volume',
837 function(resp) { // oncomplete
838 var evt = egCore.evt.parse(resp);
840 egConfirmDialog.open(
841 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_TITLE,
842 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_BODY,
844 ).result.then(function() {
847 'open-ils.cat.transfer_copies_to_volume.override',
851 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
854 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
857 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
867 service.mark_missing_pieces = function(copy) {
868 var b = copy.barcode();
869 var t = egCore.idl.toHash(copy.call_number()).record.title;
870 egConfirmDialog.open(
871 egCore.strings.CONFIRM_MARK_MISSING_TITLE,
872 egCore.strings.CONFIRM_MARK_MISSING_BODY,
873 { barcode : b, title : t }
874 ).result.then(function() {
876 // kick off mark missing
877 return egCore.net.request(
879 'open-ils.circ.mark_item_missing_pieces',
880 egCore.auth.token(), copy.id()
883 }).then(function(resp) {
884 var evt = egCore.evt.parse(resp); // should always produce event
886 if (evt.textcode == 'ACTION_CIRCULATION_NOT_FOUND') {
887 return egAlertDialog.open(
888 egCore.strings.CIRC_NOT_FOUND, {barcode : copy.barcode()});
891 var payload = evt.payload;
893 // TODO: open copy editor inline? new tab?
895 // print the missing pieces slip
896 var promise = $q.when();
898 // wait for completion, since it may spawn a confirm dialog
899 promise = egCore.print.print({
901 content_type : 'text/html',
902 content : payload.slip.template_output().data()
906 if (payload.letter) {
907 $scope.letter = payload.letter.template_output().data();
910 // apply patron penalty
912 promise.then(function() {
913 egCirc.create_penalty(payload.circ.usr())
920 service.print_spine_labels = function(copy_ids){
923 'open-ils.actor.anon_cache.set_value',
924 null, 'print-labels-these-copies', {
927 ).then(function(key) {
929 var url = egCore.env.basePath + 'cat/printlabels/' + key;
930 $timeout(function() { $window.open(url, '_blank') });
932 alert('Could not create anonymous cache key!');