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;
99 return fetchCopy(barcode, id).then(function(res) {
101 if(!res.copy) { return $q.when(); }
103 return fetchCirc(copyData.copy).then(function(res) {
105 return fetchSummary(copyData.circ).then(function() {
116 // resolved with the last received copy
117 service.fetch = function(barcode, id, noListDupes) {
123 return service.retrieveCopyData(barcode, id)
124 .then(function(copyData) {
125 if(!copyData) { return $q.when(); }
126 //Make sure we're getting a completed copyData - no plain acp or circ objects
128 // flesh circ_lib locally
129 copyData.circ.circ_lib(egCore.org.get(copyData.circ.circ_lib()));
130 copyData.circ.checkin_workstation(
131 egCore.org.get(copyData.circ.checkin_workstation()));
136 // use the existing copy if possible
137 flatCopy = service.copies.filter(
138 function(c) {return c.id == copyData.copy.id()})[0];
142 flatCopy = egCore.idl.toHash(copyData.copy, true);
145 flatCopy._circ = egCore.idl.toHash(copyData.circ, true);
146 flatCopy._circ_summary = egCore.idl.toHash(copyData.circ_summary, true);
148 flatCopy.index = service.index++;
149 service.copies.unshift(flatCopy);
152 //Get in-house use count
153 egCore.pcrud.search('aihu',
154 {item : flatCopy.id}, {}, {idlist : true, atomic : true})
155 .then(function(uses) {
156 flatCopy._inHouseUseCount = uses.length;
157 copyData.copy._inHouseUseCount = uses.length;
161 copy : copyData.copy,
162 index : flatCopy.index
169 service.add_copies_to_bucket = function(copy_list) {
170 if (copy_list.length == 0) return;
172 return $uibModal.open({
173 templateUrl: './cat/catalog/t_add_to_bucket',
177 ['$scope','$uibModalInstance',
178 function($scope , $uibModalInstance) {
180 $scope.bucket_id = 0;
181 $scope.newBucketName = '';
182 $scope.allBuckets = [];
186 'open-ils.actor.container.retrieve_by_class.authoritative',
187 egCore.auth.token(), egCore.auth.user().id(),
188 'copy', 'staff_client'
189 ).then(function(buckets) { $scope.allBuckets = buckets; });
191 $scope.add_to_bucket = function() {
193 angular.forEach(copy_list, function (cp) {
194 var item = new egCore.idl.ccbi()
195 item.bucket($scope.bucket_id);
196 item.target_copy(cp);
200 'open-ils.actor.container.item.create',
201 egCore.auth.token(), 'copy', item
205 return $q.all(promises).then(function() {
206 $uibModalInstance.close();
211 $scope.add_to_new_bucket = function() {
212 var bucket = new egCore.idl.ccb();
213 bucket.owner(egCore.auth.user().id());
214 bucket.name($scope.newBucketName);
215 bucket.description('');
216 bucket.btype('staff_client');
218 return egCore.net.request(
220 'open-ils.actor.container.create',
221 egCore.auth.token(), 'copy', bucket
222 ).then(function(bucket) {
223 $scope.bucket_id = bucket;
224 $scope.add_to_bucket();
228 $scope.cancel = function() {
229 $uibModalInstance.dismiss();
235 service.make_copies_bookable = function(items) {
237 var copies_by_record = {};
238 var record_list = [];
242 var record_id = item['call_number.record.id'];
243 if (typeof copies_by_record[ record_id ] == 'undefined') {
244 copies_by_record[ record_id ] = [];
245 record_list.push( record_id );
247 copies_by_record[ record_id ].push(item.id);
252 var combined_results = [];
253 angular.forEach(record_list, function(record_id) {
257 'open-ils.booking.resources.create_from_copies',
259 copies_by_record[record_id]
260 ).then(function(results) {
261 if (results && results['brsrc']) {
262 combined_results = combined_results.concat(results['brsrc']);
268 $q.all(promises).then(function() {
269 if (combined_results.length > 0) {
271 template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
275 ['$scope','$location','egCore','$uibModalInstance',
276 function($scope , $location , egCore , $uibModalInstance) {
279 ses : egCore.auth.token(),
280 resultant_brsrc : combined_results.map(function(o) { return o[0]; })
283 var booking_path = '/eg/conify/global/booking/resource';
285 $scope.booking_admin_url =
286 $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
293 service.book_copies_now = 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_brt = [];
310 var combined_brsrc = [];
311 angular.forEach(record_list, function(record_id) {
315 'open-ils.booking.resources.create_from_copies',
317 copies_by_record[record_id]
318 ).then(function(results) {
319 if (results && results['brt']) {
320 combined_brt = combined_brt.concat(results['brt']);
322 if (results && results['brsrc']) {
323 combined_brsrc = combined_brsrc.concat(results['brsrc']);
329 $q.all(promises).then(function() {
330 if (combined_brt.length > 0 || combined_brsrc.length > 0) {
332 template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
336 ['$scope','$location','egCore','$uibModalInstance',
337 function($scope , $location , egCore , $uibModalInstance) {
340 ses : egCore.auth.token(),
341 bresv_interface_opts : {
344 ,brsrc : combined_brsrc
349 var booking_path = '/eg/booking/reservation';
351 $scope.booking_admin_url =
352 $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
360 service.requestItems = function(copy_list) {
361 if (copy_list.length == 0) return;
363 return $uibModal.open({
364 templateUrl: './cat/catalog/t_request_items',
367 ['$scope','$uibModalInstance','egUser',
368 function($scope , $uibModalInstance , egUser) {
370 $scope.first_user_fetch = true;
374 copy_list : copy_list,
375 pickup_lib: egCore.org.get(egCore.auth.user().ws_ou()),
376 user : egCore.auth.user().id()
379 egUser.get( $scope.hold_data.user ).then(function(u) {
381 $scope.barcode = u.card().barcode();
382 $scope.user_name = egUser.format_name(u);
383 $scope.hold_data.user = u.id();
386 $scope.user_name = '';
388 $scope.$watch('barcode', function (n) {
389 if (!$scope.first_user_fetch) {
390 egUser.getByBarcode(n).then(function(u) {
392 $scope.user_name = egUser.format_name(u);
393 $scope.hold_data.user = u.id();
396 $scope.user_name = '';
397 delete $scope.hold_data.user;
400 $scope.first_user_fetch = false;
403 $scope.ok = function(h) {
406 hold_type : h.hold_type,
407 pickup_lib: h.pickup_lib.id(),
413 'open-ils.circ.holds.test_and_create.batch.override',
414 egCore.auth.token(), args, h.copy_list
417 $uibModalInstance.close();
420 $scope.cancel = function($event) {
421 $uibModalInstance.dismiss();
422 $event.preventDefault();
428 service.attach_to_peer_bib = function(items) {
429 if (items.length == 0) return;
431 egCore.hatch.getItem('eg.cat.marked_conjoined_record').then(function(target_record) {
432 if (!target_record) return;
434 return $uibModal.open({
435 templateUrl: './cat/catalog/t_conjoined_selector',
438 ['$scope','$uibModalInstance',
439 function($scope , $uibModalInstance) {
440 $scope.update = false;
442 $scope.peer_type = null;
443 $scope.peer_type_list = [];
445 get_peer_types = function() {
447 return $q.when(egCore.env.bpt.list);
449 return egCore.pcrud.retrieveAll('bpt', null, {atomic : true})
450 .then(function(list) {
451 egCore.env.absorbList(list, 'bpt');
456 get_peer_types().then(function(list){
457 $scope.peer_type_list = list;
460 $scope.ok = function(type) {
463 angular.forEach(items, function (cp) {
464 var n = new egCore.idl.bpbcm();
466 n.peer_record(target_record);
467 n.target_copy(cp.id);
469 promises.push(egCore.pcrud.create(n).then(function(){service.add_barcode_to_list(cp.barcode)}));
472 return $q.all(promises).then(function(){$uibModalInstance.close()});
475 $scope.cancel = function($event) {
476 $uibModalInstance.dismiss();
477 $event.preventDefault();
484 service.selectedHoldingsCopyDelete = function (items) {
485 if (items.length == 0) return;
487 var copy_objects = [];
488 egCore.pcrud.search('acp',
489 {deleted : 'f', id : items.map(function(el){return el.id;}) },
490 { flesh : 1, flesh_fields : { acp : ['call_number'] } }
491 ).then(function(copy) {
492 copy_objects.push(copy);
496 var perCnCopies = {};
506 var cn_id = cp.call_number().id();
507 if (!cnHash[cn_id]) {
508 cnHash[cn_id] = cp.call_number();
509 perCnCopies[cn_id] = [cp];
511 perCnCopies[cn_id].push(cp);
513 cp.call_number(cn_id); // prevent loops in JSON-ification
517 angular.forEach(perCnCopies, function (v, k) {
522 angular.forEach(cnHash, function (v, k) {
526 if (cnList.length == 0) return;
530 egConfirmDialog.open(
531 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES,
532 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES_MESSAGE,
533 {copies : cp_count, volumes : cn_count}
534 ).result.then(function() {
537 'open-ils.cat.asset.volume.fleshed.batch.update.override',
538 egCore.auth.token(), cnList, 1, flags
540 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
546 service.checkin = function (items) {
547 angular.forEach(items, function (cp) {
548 egCirc.checkin({copy_barcode:cp.barcode}).then(
549 function() { service.add_barcode_to_list(cp.barcode) }
554 service.renew = function (items) {
555 angular.forEach(items, function (cp) {
556 egCirc.renew({copy_barcode:cp.barcode}).then(
557 function() { service.add_barcode_to_list(cp.barcode) }
562 service.cancel_transit = function (items) {
563 angular.forEach(items, function(cp) {
564 egCirc.find_copy_transit(null, {copy_barcode:cp.barcode})
565 .then(function(t) { return egCirc.abort_transit(t.id()) })
566 .then(function() { return service.add_barcode_to_list(cp.barcode) });
570 service.selectedHoldingsDamaged = function (items) {
571 egCirc.mark_damaged(items.map(function(el){return el.id;})).then(function(){
572 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
576 service.selectedHoldingsMissing = function (items) {
577 egCirc.mark_missing(items.map(function(el){return el.id;})).then(function(){
578 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
582 service.gatherSelectedRecordIds = function (items) {
587 if (rid_list.indexOf(item['call_number.record.id']) == -1)
588 rid_list.push(item['call_number.record.id'])
594 service.gatherSelectedVolumeIds = function (items,rid) {
599 if (rid && item['call_number.record.id'] != rid) return;
600 if (cn_id_list.indexOf(item['call_number.id']) == -1)
601 cn_id_list.push(item['call_number.id'])
607 service.gatherSelectedHoldingsIds = function (items,rid) {
612 if (rid && item['call_number.record.id'] != rid) return;
613 cp_id_list.push(item.id)
619 service.spawnHoldingsAdd = function (items,use_vols,use_copies){
620 angular.forEach(service.gatherSelectedRecordIds(items), function (r) {
622 if (use_copies) { // just a copy on existing volumes
623 angular.forEach(service.gatherSelectedVolumeIds(items,r), function (v) {
624 raw.push( {callnumber : v} );
626 } else if (use_vols) {
628 service.gatherSelectedHoldingsIds(items,r),
630 angular.forEach(items, function(item) {
631 if (i == item.id) raw.push({owner : item['call_number.owning_lib']});
637 if (raw.length == 0) raw.push({});
641 'open-ils.actor.anon_cache.set_value',
642 null, 'edit-these-copies', {
648 ).then(function(key) {
650 var url = egCore.env.basePath + 'cat/volcopy/' + key;
651 $timeout(function() { $window.open(url, '_blank') });
653 alert('Could not create anonymous cache key!');
659 service.spawnHoldingsEdit = function (items,hide_vols,hide_copies){
660 angular.forEach(service.gatherSelectedRecordIds(items), function (r) {
663 'open-ils.actor.anon_cache.set_value',
664 null, 'edit-these-copies', {
666 copies: service.gatherSelectedHoldingsIds(items,r),
668 hide_vols : hide_vols,
669 hide_copies : hide_copies
671 ).then(function(key) {
673 var url = egCore.env.basePath + 'cat/volcopy/' + key;
674 $timeout(function() { $window.open(url, '_blank') });
676 alert('Could not create anonymous cache key!');
682 service.replaceBarcodes = function(items) {
683 angular.forEach(items, function (cp) {
685 templateUrl: './cat/share/t_replace_barcode',
688 ['$scope','$uibModalInstance',
689 function($scope , $uibModalInstance) {
690 $scope.isModal = true;
691 $scope.focusBarcode = false;
692 $scope.focusBarcode2 = true;
693 $scope.barcode1 = cp.barcode;
695 $scope.updateBarcode = function() {
696 $scope.copyNotFound = false;
697 $scope.updateOK = false;
699 egCore.pcrud.search('acp',
700 {deleted : 'f', barcode : $scope.barcode1})
701 .then(function(copy) {
704 $scope.focusBarcode = true;
705 $scope.copyNotFound = true;
709 $scope.copyId = copy.id();
710 copy.barcode($scope.barcode2);
712 egCore.pcrud.update(copy).then(function(stat) {
713 $scope.updateOK = stat;
714 $scope.focusBarcode = true;
715 if (stat) service.add_barcode_to_list(copy.barcode());
719 $uibModalInstance.close();
722 $scope.cancel = function($event) {
723 $uibModalInstance.dismiss();
724 $event.preventDefault();
732 // this "transfers" selected copies to a new owning library,
733 // auto-creating volumes and deleting unused volumes as required.
734 service.changeItemOwningLib = function(items) {
735 var xfer_target = egCore.hatch.getLocalItem('eg.cat.volume_transfer_target');
736 if (!xfer_target || !items.length) {
739 var vols_to_move = {};
740 var copies_to_move = {};
741 angular.forEach(items, function(item) {
742 if (item['call_number.owning_lib'] != xfer_target) {
743 if (item['call_number.id'] in vols_to_move) {
744 copies_to_move[item['call_number.id']].push(item.id);
746 vols_to_move[item['call_number.id']] = {
747 label : item['call_number.label'],
748 label_class : item['call_number.label_class'],
749 record : item['call_number.record.id'],
750 prefix : item['call_number.prefix.id'],
751 suffix : item['call_number.suffix.id']
753 copies_to_move[item['call_number.id']] = new Array;
754 copies_to_move[item['call_number.id']].push(item.id);
760 angular.forEach(vols_to_move, function(vol) {
761 promises.push(egCore.net.request(
763 'open-ils.cat.call_number.find_or_create',
771 ).then(function(resp) {
772 var evt = egCore.evt.parse(resp);
774 return egCore.net.request(
776 'open-ils.cat.transfer_copies_to_volume',
779 copies_to_move[vol.id]
788 function(){ service.add_barcode_to_list(cp.barcode) }
795 service.transferItems = function (items){
796 var xfer_target = egCore.hatch.getLocalItem('eg.cat.item_transfer_target');
797 var copy_ids = service.gatherSelectedHoldingsIds(items);
798 if (xfer_target && copy_ids.length > 0) {
801 'open-ils.cat.transfer_copies_to_volume',
806 function(resp) { // oncomplete
807 var evt = egCore.evt.parse(resp);
809 egConfirmDialog.open(
810 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_TITLE,
811 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_BODY,
813 ).result.then(function() {
816 'open-ils.cat.transfer_copies_to_volume.override',
820 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
823 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
826 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
836 service.mark_missing_pieces = function(copy) {
837 var b = copy.barcode();
838 var t = egCore.idl.toHash(copy.call_number()).record.title;
839 egConfirmDialog.open(
840 egCore.strings.CONFIRM_MARK_MISSING_TITLE,
841 egCore.strings.CONFIRM_MARK_MISSING_BODY,
842 { barcode : b, title : t }
843 ).result.then(function() {
845 // kick off mark missing
846 return egCore.net.request(
848 'open-ils.circ.mark_item_missing_pieces',
849 egCore.auth.token(), copy.id()
852 }).then(function(resp) {
853 var evt = egCore.evt.parse(resp); // should always produce event
855 if (evt.textcode == 'ACTION_CIRCULATION_NOT_FOUND') {
856 return egAlertDialog.open(
857 egCore.strings.CIRC_NOT_FOUND, {barcode : copy.barcode()});
860 var payload = evt.payload;
862 // TODO: open copy editor inline? new tab?
864 // print the missing pieces slip
865 var promise = $q.when();
867 // wait for completion, since it may spawn a confirm dialog
868 promise = egCore.print.print({
870 content_type : 'text/html',
871 content : payload.slip.template_output().data()
875 if (payload.letter) {
876 $scope.letter = payload.letter.template_output().data();
879 // apply patron penalty
881 promise.then(function() {
882 egCirc.create_penalty(payload.circ.usr())
889 service.print_spine_labels = function(copies){
892 console.warn("Print Spine Labels is not implemented yet! See LP 1704873");
893 $timeout(function() { $window.open(egCore.env.basePath, '_blank') });
895 // TODO : LP# 1704873 needs to be in master
896 // for the below to work. Commented out until then
901 'open-ils.actor.anon_cache.set_value',
902 null, 'print-labels-these-copies', {
903 copies : service.gatherSelectedHoldingsIds(copies)
905 ).then(function(key) {
907 var url = egCore.env.basePath + 'cat/printlabels/' + key;
908 $timeout(function() { $window.open(url, '_blank') });
910 alert('Could not create anonymous cache key!');