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'],
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 : {circ : 'xact_start desc'},
48 //Retrieve separate copy, aacs, and accs information
49 service.getCopy = function(barcode, id) {
52 .search('acp', {barcode : barcode, deleted : 'f'}, service.flesh)
53 .then(function(copy) {return copy});
56 return egCore.pcrud.retrieve( 'acp', id, service.flesh)
57 .then(function(copy) {return copy});
60 service.getCirc = function(id) {
61 return egCore.pcrud.search('aacs', { target_copy : id },
62 service.circFlesh).then(function(circ) {return circ});
65 service.getSummary = function(id) {
66 return circ_summary = egCore.net.request(
68 'open-ils.circ.renewal_chain.retrieve_by_circ.summary',
69 egCore.auth.token(), id).then(
70 function(circ_summary) {return circ_summary});
73 //Combine copy, circ, and accs information
74 service.retrieveCopyData = function(barcode, id) {
77 var fetchCopy = function(barcode, id) {
78 return service.getCopy(barcode, id)
79 .then(function(copy) {
84 var fetchCirc = function(copy) {
85 return service.getCirc(copy.id())
86 .then(function(circ) {
91 var fetchSummary = function(circ) {
92 return service.getSummary(circ.id())
93 .then(function(summary) {
94 copyData.circ_summary = summary;
98 return fetchCopy(barcode, id).then(function(res) {
99 return fetchCirc(copyData.copy).then(function(res) {
101 return fetchSummary(copyData.circ).then(function() {
112 // resolved with the last received copy
113 service.fetch = function(barcode, id, noListDupes) {
119 return service.retrieveCopyData(barcode, id)
120 .then(function(copyData) {
121 //Make sure we're getting a completed copyData - no plain acp or circ objects
123 // flesh circ_lib locally
124 copyData.circ.circ_lib(egCore.org.get(copyData.circ.circ_lib()));
125 copyData.circ.checkin_workstation(
126 egCore.org.get(copyData.circ.checkin_workstation()));
131 // use the existing copy if possible
132 flatCopy = service.copies.filter(
133 function(c) {return c.id == copyData.copy.id()})[0];
137 flatCopy = egCore.idl.toHash(copyData.copy, true);
140 flatCopy._circ = egCore.idl.toHash(copyData.circ, true);
141 flatCopy._circ_summary = egCore.idl.toHash(copyData.circ_summary, true);
143 flatCopy.index = service.index++;
144 service.copies.unshift(flatCopy);
147 //Get in-house use count
148 egCore.pcrud.search('aihu',
149 {item : flatCopy.id}, {}, {idlist : true, atomic : true})
150 .then(function(uses) {
151 flatCopy._inHouseUseCount = uses.length;
152 copyData.copy._inHouseUseCount = uses.length;
156 copy : copyData.copy,
157 index : flatCopy.index
164 service.add_copies_to_bucket = function(copy_list) {
165 if (copy_list.length == 0) return;
167 return $uibModal.open({
168 templateUrl: './cat/catalog/t_add_to_bucket',
172 ['$scope','$uibModalInstance',
173 function($scope , $uibModalInstance) {
175 $scope.bucket_id = 0;
176 $scope.newBucketName = '';
177 $scope.allBuckets = [];
181 'open-ils.actor.container.retrieve_by_class.authoritative',
182 egCore.auth.token(), egCore.auth.user().id(),
183 'copy', 'staff_client'
184 ).then(function(buckets) { $scope.allBuckets = buckets; });
186 $scope.add_to_bucket = function() {
188 angular.forEach(copy_list, function (cp) {
189 var item = new egCore.idl.ccbi()
190 item.bucket($scope.bucket_id);
191 item.target_copy(cp);
195 'open-ils.actor.container.item.create',
196 egCore.auth.token(), 'copy', item
200 return $q.all(promises).then(function() {
201 $uibModalInstance.close();
206 $scope.add_to_new_bucket = function() {
207 var bucket = new egCore.idl.ccb();
208 bucket.owner(egCore.auth.user().id());
209 bucket.name($scope.newBucketName);
210 bucket.description('');
211 bucket.btype('staff_client');
213 return egCore.net.request(
215 'open-ils.actor.container.create',
216 egCore.auth.token(), 'copy', bucket
217 ).then(function(bucket) {
218 $scope.bucket_id = bucket;
219 $scope.add_to_bucket();
223 $scope.cancel = function() {
224 $uibModalInstance.dismiss();
230 service.make_copies_bookable = function(items) {
232 var copies_by_record = {};
233 var record_list = [];
237 var record_id = item['call_number.record.id'];
238 if (typeof copies_by_record[ record_id ] == 'undefined') {
239 copies_by_record[ record_id ] = [];
240 record_list.push( record_id );
242 copies_by_record[ record_id ].push(item.id);
247 var combined_results = [];
248 angular.forEach(record_list, function(record_id) {
252 'open-ils.booking.resources.create_from_copies',
254 copies_by_record[record_id]
255 ).then(function(results) {
256 if (results && results['brsrc']) {
257 combined_results = combined_results.concat(results['brsrc']);
263 $q.all(promises).then(function() {
264 if (combined_results.length > 0) {
266 template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
270 ['$scope','$location','egCore','$uibModalInstance',
271 function($scope , $location , egCore , $uibModalInstance) {
274 ses : egCore.auth.token(),
275 resultant_brsrc : combined_results.map(function(o) { return o[0]; })
278 var booking_path = '/eg/conify/global/booking/resource';
280 $scope.booking_admin_url =
281 $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
288 service.book_copies_now = function(items) {
289 var copies_by_record = {};
290 var record_list = [];
294 var record_id = item['call_number.record.id'];
295 if (typeof copies_by_record[ record_id ] == 'undefined') {
296 copies_by_record[ record_id ] = [];
297 record_list.push( record_id );
299 copies_by_record[ record_id ].push(item.id);
304 var combined_brt = [];
305 var combined_brsrc = [];
306 angular.forEach(record_list, function(record_id) {
310 'open-ils.booking.resources.create_from_copies',
312 copies_by_record[record_id]
313 ).then(function(results) {
314 if (results && results['brt']) {
315 combined_brt = combined_brt.concat(results['brt']);
317 if (results && results['brsrc']) {
318 combined_brsrc = combined_brsrc.concat(results['brsrc']);
324 $q.all(promises).then(function() {
325 if (combined_brt.length > 0 || combined_brsrc.length > 0) {
327 template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
331 ['$scope','$location','egCore','$uibModalInstance',
332 function($scope , $location , egCore , $uibModalInstance) {
335 ses : egCore.auth.token(),
336 bresv_interface_opts : {
339 ,brsrc : combined_brsrc
344 var booking_path = '/eg/booking/reservation';
346 $scope.booking_admin_url =
347 $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
355 service.requestItems = function(copy_list) {
356 if (copy_list.length == 0) return;
358 return $uibModal.open({
359 templateUrl: './cat/catalog/t_request_items',
362 ['$scope','$uibModalInstance','egUser',
363 function($scope , $uibModalInstance , egUser) {
365 $scope.first_user_fetch = true;
369 copy_list : copy_list,
370 pickup_lib: egCore.org.get(egCore.auth.user().ws_ou()),
371 user : egCore.auth.user().id()
374 egUser.get( $scope.hold_data.user ).then(function(u) {
376 $scope.barcode = u.card().barcode();
377 $scope.user_name = egUser.format_name(u);
378 $scope.hold_data.user = u.id();
381 $scope.user_name = '';
383 $scope.$watch('barcode', function (n) {
384 if (!$scope.first_user_fetch) {
385 egUser.getByBarcode(n).then(function(u) {
387 $scope.user_name = egUser.format_name(u);
388 $scope.hold_data.user = u.id();
391 $scope.user_name = '';
392 delete $scope.hold_data.user;
395 $scope.first_user_fetch = false;
398 $scope.ok = function(h) {
401 hold_type : h.hold_type,
402 pickup_lib: h.pickup_lib.id(),
408 'open-ils.circ.holds.test_and_create.batch.override',
409 egCore.auth.token(), args, h.copy_list
412 $uibModalInstance.close();
415 $scope.cancel = function($event) {
416 $uibModalInstance.dismiss();
417 $event.preventDefault();
423 service.attach_to_peer_bib = function(items) {
424 if (items.length == 0) return;
426 egCore.hatch.getItem('eg.cat.marked_conjoined_record').then(function(target_record) {
427 if (!target_record) return;
429 return $uibModal.open({
430 templateUrl: './cat/catalog/t_conjoined_selector',
433 ['$scope','$uibModalInstance',
434 function($scope , $uibModalInstance) {
435 $scope.update = false;
437 $scope.peer_type = null;
438 $scope.peer_type_list = [];
440 get_peer_types = function() {
442 return $q.when(egCore.env.bpt.list);
444 return egCore.pcrud.retrieveAll('bpt', null, {atomic : true})
445 .then(function(list) {
446 egCore.env.absorbList(list, 'bpt');
451 get_peer_types().then(function(list){
452 $scope.peer_type_list = list;
455 $scope.ok = function(type) {
458 angular.forEach(items, function (cp) {
459 var n = new egCore.idl.bpbcm();
461 n.peer_record(target_record);
462 n.target_copy(cp.id);
464 promises.push(egCore.pcrud.create(n).then(function(){service.add_barcode_to_list(cp.barcode)}));
467 return $q.all(promises).then(function(){$uibModalInstance.close()});
470 $scope.cancel = function($event) {
471 $uibModalInstance.dismiss();
472 $event.preventDefault();
479 service.selectedHoldingsCopyDelete = function (items) {
480 if (items.length == 0) return;
482 var copy_objects = [];
483 egCore.pcrud.search('acp',
484 {deleted : 'f', id : items.map(function(el){return el.id;}) },
485 { flesh : 1, flesh_fields : { acp : ['call_number'] } }
486 ).then(function(copy) {
487 copy_objects.push(copy);
491 var perCnCopies = {};
501 var cn_id = cp.call_number().id();
502 if (!cnHash[cn_id]) {
503 cnHash[cn_id] = cp.call_number();
504 perCnCopies[cn_id] = [cp];
506 perCnCopies[cn_id].push(cp);
508 cp.call_number(cn_id); // prevent loops in JSON-ification
512 angular.forEach(perCnCopies, function (v, k) {
517 angular.forEach(cnHash, function (v, k) {
521 if (cnList.length == 0) return;
525 egConfirmDialog.open(
526 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES,
527 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES_MESSAGE,
528 {copies : cp_count, volumes : cn_count}
529 ).result.then(function() {
532 'open-ils.cat.asset.volume.fleshed.batch.update.override',
533 egCore.auth.token(), cnList, 1, flags
535 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
541 service.checkin = function (items) {
542 angular.forEach(items, function (cp) {
543 egCirc.checkin({copy_barcode:cp.barcode}).then(
544 function() { service.add_barcode_to_list(cp.barcode) }
549 service.renew = function (items) {
550 angular.forEach(items, function (cp) {
551 egCirc.renew({copy_barcode:cp.barcode}).then(
552 function() { service.add_barcode_to_list(cp.barcode) }
557 service.cancel_transit = function (items) {
558 angular.forEach(items, function(cp) {
559 egCirc.find_copy_transit(null, {copy_barcode:cp.barcode})
560 .then(function(t) { return egCirc.abort_transit(t.id()) })
561 .then(function() { return service.add_barcode_to_list(cp.barcode) });
565 service.selectedHoldingsDamaged = function (items) {
566 egCirc.mark_damaged(items.map(function(el){return el.id;})).then(function(){
567 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
571 service.selectedHoldingsMissing = function (items) {
572 egCirc.mark_missing(items.map(function(el){return el.id;})).then(function(){
573 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
577 service.gatherSelectedRecordIds = function (items) {
582 if (rid_list.indexOf(item['call_number.record.id']) == -1)
583 rid_list.push(item['call_number.record.id'])
589 service.gatherSelectedVolumeIds = function (items,rid) {
594 if (rid && item['call_number.record.id'] != rid) return;
595 if (cn_id_list.indexOf(item['call_number.id']) == -1)
596 cn_id_list.push(item['call_number.id'])
602 service.gatherSelectedHoldingsIds = function (items,rid) {
607 if (rid && item['call_number.record.id'] != rid) return;
608 cp_id_list.push(item.id)
614 service.spawnHoldingsAdd = function (items,use_vols,use_copies){
615 angular.forEach(service.gatherSelectedRecordIds(items), function (r) {
617 if (use_copies) { // just a copy on existing volumes
618 angular.forEach(service.gatherSelectedVolumeIds(items,r), function (v) {
619 raw.push( {callnumber : v} );
621 } else if (use_vols) {
623 service.gatherSelectedHoldingsIds(items,r),
625 angular.forEach(items, function(item) {
626 if (i == item.id) raw.push({owner : item['call_number.owning_lib']});
632 if (raw.length == 0) raw.push({});
636 'open-ils.actor.anon_cache.set_value',
637 null, 'edit-these-copies', {
643 ).then(function(key) {
645 var url = egCore.env.basePath + 'cat/volcopy/' + key;
646 $timeout(function() { $window.open(url, '_blank') });
648 alert('Could not create anonymous cache key!');
654 service.spawnHoldingsEdit = function (items,hide_vols,hide_copies){
655 angular.forEach(service.gatherSelectedRecordIds(items), function (r) {
658 'open-ils.actor.anon_cache.set_value',
659 null, 'edit-these-copies', {
661 copies: service.gatherSelectedHoldingsIds(items,r),
663 hide_vols : hide_vols,
664 hide_copies : hide_copies
666 ).then(function(key) {
668 var url = egCore.env.basePath + 'cat/volcopy/' + key;
669 $timeout(function() { $window.open(url, '_blank') });
671 alert('Could not create anonymous cache key!');
677 service.replaceBarcodes = function(items) {
678 angular.forEach(items, function (cp) {
680 templateUrl: './cat/share/t_replace_barcode',
683 ['$scope','$uibModalInstance',
684 function($scope , $uibModalInstance) {
685 $scope.isModal = true;
686 $scope.focusBarcode = false;
687 $scope.focusBarcode2 = true;
688 $scope.barcode1 = cp.barcode;
690 $scope.updateBarcode = function() {
691 $scope.copyNotFound = false;
692 $scope.updateOK = false;
694 egCore.pcrud.search('acp',
695 {deleted : 'f', barcode : $scope.barcode1})
696 .then(function(copy) {
699 $scope.focusBarcode = true;
700 $scope.copyNotFound = true;
704 $scope.copyId = copy.id();
705 copy.barcode($scope.barcode2);
707 egCore.pcrud.update(copy).then(function(stat) {
708 $scope.updateOK = stat;
709 $scope.focusBarcode = true;
710 if (stat) service.add_barcode_to_list(copy.barcode());
714 $uibModalInstance.close();
717 $scope.cancel = function($event) {
718 $uibModalInstance.dismiss();
719 $event.preventDefault();
727 // this "transfers" selected copies to a new owning library,
728 // auto-creating volumes and deleting unused volumes as required.
729 service.changeItemOwningLib = function(items) {
730 var xfer_target = egCore.hatch.getLocalItem('eg.cat.volume_transfer_target');
731 if (!xfer_target || !items.length) {
734 var vols_to_move = {};
735 var copies_to_move = {};
736 angular.forEach(items, function(item) {
737 if (item['call_number.owning_lib'] != xfer_target) {
738 if (item['call_number.id'] in vols_to_move) {
739 copies_to_move[item['call_number.id']].push(item.id);
741 vols_to_move[item['call_number.id']] = {
742 label : item['call_number.label'],
743 label_class : item['call_number.label_class'],
744 record : item['call_number.record.id'],
745 prefix : item['call_number.prefix.id'],
746 suffix : item['call_number.suffix.id']
748 copies_to_move[item['call_number.id']] = new Array;
749 copies_to_move[item['call_number.id']].push(item.id);
755 angular.forEach(vols_to_move, function(vol) {
756 promises.push(egCore.net.request(
758 'open-ils.cat.call_number.find_or_create',
766 ).then(function(resp) {
767 var evt = egCore.evt.parse(resp);
769 return egCore.net.request(
771 'open-ils.cat.transfer_copies_to_volume',
774 copies_to_move[vol.id]
783 function(){ service.add_barcode_to_list(cp.barcode) }
790 service.transferItems = function (items){
791 var xfer_target = egCore.hatch.getLocalItem('eg.cat.item_transfer_target');
792 var copy_ids = service.gatherSelectedHoldingsIds(items);
793 if (xfer_target && copy_ids.length > 0) {
796 'open-ils.cat.transfer_copies_to_volume',
801 function(resp) { // oncomplete
802 var evt = egCore.evt.parse(resp);
804 egConfirmDialog.open(
805 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_TITLE,
806 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_BODY,
808 ).result.then(function() {
811 'open-ils.cat.transfer_copies_to_volume.override',
815 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
818 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
821 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
831 service.mark_missing_pieces = function(copy) {
832 var b = copy.barcode();
833 var t = egCore.idl.toHash(copy.call_number()).record.title;
834 egConfirmDialog.open(
835 egCore.strings.CONFIRM_MARK_MISSING_TITLE,
836 egCore.strings.CONFIRM_MARK_MISSING_BODY,
837 { barcode : b, title : t }
838 ).result.then(function() {
840 // kick off mark missing
841 return egCore.net.request(
843 'open-ils.circ.mark_item_missing_pieces',
844 egCore.auth.token(), copy.id()
847 }).then(function(resp) {
848 var evt = egCore.evt.parse(resp); // should always produce event
850 if (evt.textcode == 'ACTION_CIRCULATION_NOT_FOUND') {
851 return egAlertDialog.open(
852 egCore.strings.CIRC_NOT_FOUND, {barcode : copy.barcode()});
855 var payload = evt.payload;
857 // TODO: open copy editor inline? new tab?
859 // print the missing pieces slip
860 var promise = $q.when();
862 // wait for completion, since it may spawn a confirm dialog
863 promise = egCore.print.print({
865 content_type : 'text/html',
866 content : payload.slip.template_output().data()
870 if (payload.letter) {
871 $scope.letter = payload.letter.template_output().data();
874 // apply patron penalty
876 promise.then(function() {
877 egCirc.create_penalty(payload.circ.usr())
884 service.print_spine_labels = function(copies){
887 console.warn("Print Spine Labels is not implemented yet! See LP 1704873");
888 $timeout(function() { $window.open(egCore.env.basePath, '_blank') });
890 // TODO : LP# 1704873 needs to be in master
891 // for the below to work. Commented out until then
896 'open-ils.actor.anon_cache.set_value',
897 null, 'print-labels-these-copies', {
898 copies : service.gatherSelectedHoldingsIds(copies)
900 ).then(function(key) {
902 var url = egCore.env.basePath + 'cat/printlabels/' + key;
903 $timeout(function() { $window.open(url, '_blank') });
905 alert('Could not create anonymous cache key!');