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._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;
178 copy : copyData.copy,
179 index : flatCopy.index
186 service.add_copies_to_bucket = function(copy_list) {
187 if (copy_list.length == 0) return;
189 return $uibModal.open({
190 templateUrl: './cat/catalog/t_add_to_bucket',
195 ['$scope','$uibModalInstance',
196 function($scope , $uibModalInstance) {
198 $scope.bucket_id = 0;
199 $scope.newBucketName = '';
200 $scope.allBuckets = [];
204 'open-ils.actor.container.retrieve_by_class.authoritative',
205 egCore.auth.token(), egCore.auth.user().id(),
206 'copy', 'staff_client'
207 ).then(function(buckets) { $scope.allBuckets = buckets; });
209 $scope.add_to_bucket = function() {
211 angular.forEach(copy_list, function (cp) {
212 var item = new egCore.idl.ccbi()
213 item.bucket($scope.bucket_id);
214 item.target_copy(cp);
218 'open-ils.actor.container.item.create',
219 egCore.auth.token(), 'copy', item
223 return $q.all(promises).then(function() {
224 $uibModalInstance.close();
229 $scope.add_to_new_bucket = function() {
230 var bucket = new egCore.idl.ccb();
231 bucket.owner(egCore.auth.user().id());
232 bucket.name($scope.newBucketName);
233 bucket.description('');
234 bucket.btype('staff_client');
236 return egCore.net.request(
238 'open-ils.actor.container.create',
239 egCore.auth.token(), 'copy', bucket
240 ).then(function(bucket) {
241 $scope.bucket_id = bucket;
242 $scope.add_to_bucket();
246 $scope.cancel = function() {
247 $uibModalInstance.dismiss();
253 service.make_copies_bookable = function(items) {
255 var copies_by_record = {};
256 var record_list = [];
260 var record_id = item['call_number.record.id'];
261 if (typeof copies_by_record[ record_id ] == 'undefined') {
262 copies_by_record[ record_id ] = [];
263 record_list.push( record_id );
265 copies_by_record[ record_id ].push(item.id);
270 var combined_results = [];
271 angular.forEach(record_list, function(record_id) {
275 'open-ils.booking.resources.create_from_copies',
277 copies_by_record[record_id]
278 ).then(function(results) {
279 if (results && results['brsrc']) {
280 combined_results = combined_results.concat(results['brsrc']);
286 $q.all(promises).then(function() {
287 if (combined_results.length > 0) {
289 template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
294 ['$scope','$location','egCore','$uibModalInstance',
295 function($scope , $location , egCore , $uibModalInstance) {
298 ses : egCore.auth.token(),
299 resultant_brsrc : combined_results.map(function(o) { return o[0]; })
302 var booking_path = '/eg/conify/global/booking/resource';
304 $scope.booking_admin_url =
305 $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
312 service.book_copies_now = function(items) {
313 var copies_by_record = {};
314 var record_list = [];
318 var record_id = item['call_number.record.id'];
319 if (typeof copies_by_record[ record_id ] == 'undefined') {
320 copies_by_record[ record_id ] = [];
321 record_list.push( record_id );
323 copies_by_record[ record_id ].push(item.id);
328 var combined_brt = [];
329 var combined_brsrc = [];
330 angular.forEach(record_list, function(record_id) {
334 'open-ils.booking.resources.create_from_copies',
336 copies_by_record[record_id]
337 ).then(function(results) {
338 if (results && results['brt']) {
339 combined_brt = combined_brt.concat(results['brt']);
341 if (results && results['brsrc']) {
342 combined_brsrc = combined_brsrc.concat(results['brsrc']);
348 $q.all(promises).then(function() {
349 if (combined_brt.length > 0 || combined_brsrc.length > 0) {
351 template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
356 ['$scope','$location','egCore','$uibModalInstance',
357 function($scope , $location , egCore , $uibModalInstance) {
360 ses : egCore.auth.token(),
361 bresv_interface_opts : {
364 ,brsrc : combined_brsrc
369 var booking_path = '/eg/booking/reservation';
371 $scope.booking_admin_url =
372 $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
380 service.requestItems = function(copy_list) {
381 if (copy_list.length == 0) return;
383 return $uibModal.open({
384 templateUrl: './cat/catalog/t_request_items',
388 ['$scope','$uibModalInstance','egUser',
389 function($scope , $uibModalInstance , egUser) {
391 $scope.first_user_fetch = true;
395 copy_list : copy_list,
396 pickup_lib: egCore.org.get(egCore.auth.user().ws_ou()),
397 user : egCore.auth.user().id()
400 egUser.get( $scope.hold_data.user ).then(function(u) {
402 $scope.barcode = u.card().barcode();
403 $scope.user_name = egUser.format_name(u);
404 $scope.hold_data.user = u.id();
407 $scope.user_name = '';
409 $scope.$watch('barcode', function (n) {
410 if (!$scope.first_user_fetch) {
411 egUser.getByBarcode(n).then(function(u) {
413 $scope.user_name = egUser.format_name(u);
414 $scope.hold_data.user = u.id();
417 $scope.user_name = '';
418 delete $scope.hold_data.user;
421 $scope.first_user_fetch = false;
424 $scope.ok = function(h) {
427 hold_type : h.hold_type,
428 pickup_lib: h.pickup_lib.id(),
434 'open-ils.circ.holds.test_and_create.batch.override',
435 egCore.auth.token(), args, h.copy_list
438 $uibModalInstance.close();
441 $scope.cancel = function($event) {
442 $uibModalInstance.dismiss();
443 $event.preventDefault();
449 service.attach_to_peer_bib = function(items) {
450 if (items.length == 0) return;
452 egCore.hatch.getItem('eg.cat.marked_conjoined_record').then(function(target_record) {
453 if (!target_record) return;
455 return $uibModal.open({
456 templateUrl: './cat/catalog/t_conjoined_selector',
460 ['$scope','$uibModalInstance',
461 function($scope , $uibModalInstance) {
462 $scope.update = false;
464 $scope.peer_type = null;
465 $scope.peer_type_list = [];
467 get_peer_types = function() {
469 return $q.when(egCore.env.bpt.list);
471 return egCore.pcrud.retrieveAll('bpt', null, {atomic : true})
472 .then(function(list) {
473 egCore.env.absorbList(list, 'bpt');
478 get_peer_types().then(function(list){
479 $scope.peer_type_list = list;
482 $scope.ok = function(type) {
485 angular.forEach(items, function (cp) {
486 var n = new egCore.idl.bpbcm();
488 n.peer_record(target_record);
489 n.target_copy(cp.id);
491 promises.push(egCore.pcrud.create(n).then(function(){service.add_barcode_to_list(cp.barcode)}));
494 return $q.all(promises).then(function(){$uibModalInstance.close()});
497 $scope.cancel = function($event) {
498 $uibModalInstance.dismiss();
499 $event.preventDefault();
506 service.selectedHoldingsCopyDelete = function (items) {
507 if (items.length == 0) return;
509 var copy_objects = [];
510 egCore.pcrud.search('acp',
511 {deleted : 'f', id : items.map(function(el){return el.id;}) },
512 { flesh : 1, flesh_fields : { acp : ['call_number'] } }
516 var perCnCopies = {};
526 var cn_id = cp.call_number().id();
527 if (!cnHash[cn_id]) {
528 cnHash[cn_id] = cp.call_number();
529 perCnCopies[cn_id] = [cp];
531 perCnCopies[cn_id].push(cp);
533 cp.call_number(cn_id); // prevent loops in JSON-ification
537 angular.forEach(perCnCopies, function (v, k) {
542 angular.forEach(cnHash, function (v, k) {
546 if (cnList.length == 0) return;
550 egConfirmDialog.open(
551 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES,
552 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES_MESSAGE,
553 {copies : cp_count, volumes : cn_count}
554 ).result.then(function() {
557 'open-ils.cat.asset.volume.fleshed.batch.update.override',
558 egCore.auth.token(), cnList, 1, flags
560 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
566 copy_objects.push(copy);
570 service.checkin = function (items) {
571 angular.forEach(items, function (cp) {
572 egCirc.checkin({copy_barcode:cp.barcode}).then(
573 function() { service.add_barcode_to_list(cp.barcode) }
578 service.renew = function (items) {
579 angular.forEach(items, function (cp) {
580 egCirc.renew({copy_barcode:cp.barcode}).then(
581 function() { service.add_barcode_to_list(cp.barcode) }
586 service.cancel_transit = function (items) {
587 angular.forEach(items, function(cp) {
588 egCirc.find_copy_transit(null, {copy_barcode:cp.barcode})
589 .then(function(t) { return egCirc.abort_transit(t.id()) })
590 .then(function() { return service.add_barcode_to_list(cp.barcode) });
594 service.selectedHoldingsDamaged = function (items) {
595 angular.forEach(items, function(cp) {
597 egCirc.mark_damaged({
606 service.selectedHoldingsMissing = function (items) {
607 egCirc.mark_missing(items.map(function(el){return el.id;})).then(function(){
608 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
612 service.gatherSelectedRecordIds = function (items) {
617 if (rid_list.indexOf(item['call_number.record.id']) == -1)
618 rid_list.push(item['call_number.record.id'])
624 service.gatherSelectedVolumeIds = function (items,rid) {
629 if (rid && item['call_number.record.id'] != rid) return;
630 if (cn_id_list.indexOf(item['call_number.id']) == -1)
631 cn_id_list.push(item['call_number.id'])
637 service.gatherSelectedHoldingsIds = function (items,rid) {
642 if (rid && item['call_number.record.id'] != rid) return;
643 cp_id_list.push(item.id)
649 service.spawnHoldingsAdd = function (items,use_vols,use_copies){
650 angular.forEach(service.gatherSelectedRecordIds(items), function (r) {
652 if (use_copies) { // just a copy on existing volumes
653 angular.forEach(service.gatherSelectedVolumeIds(items,r), function (v) {
654 raw.push( {callnumber : v} );
656 } else if (use_vols) {
658 service.gatherSelectedHoldingsIds(items,r),
660 angular.forEach(items, function(item) {
661 if (i == item.id) raw.push({owner : item['call_number.owning_lib']});
667 if (raw.length == 0) raw.push({});
671 'open-ils.actor.anon_cache.set_value',
672 null, 'edit-these-copies', {
678 ).then(function(key) {
680 var url = egCore.env.basePath + 'cat/volcopy/' + key;
681 $timeout(function() { $window.open(url, '_blank') });
683 alert('Could not create anonymous cache key!');
689 service.spawnHoldingsEdit = function (items,hide_vols,hide_copies){
691 angular.forEach(items, function(i){
697 'open-ils.actor.anon_cache.set_value',
701 record_id: 0, // disables record summary
704 hide_vols : hide_vols,
705 hide_copies : hide_copies
706 }).then(function(key) {
708 var url = egCore.env.basePath + 'cat/volcopy/' + key;
709 $timeout(function() { $window.open(url, '_blank') });
711 alert('Could not create anonymous cache key!');
716 service.replaceBarcodes = function(items) {
717 angular.forEach(items, function (cp) {
719 templateUrl: './cat/share/t_replace_barcode',
723 ['$scope','$uibModalInstance',
724 function($scope , $uibModalInstance) {
725 $scope.isModal = true;
726 $scope.focusBarcode = false;
727 $scope.focusBarcode2 = true;
728 $scope.barcode1 = cp.barcode;
730 $scope.updateBarcode = function() {
731 $scope.copyNotFound = false;
732 $scope.updateOK = false;
734 egCore.pcrud.search('acp',
735 {deleted : 'f', barcode : $scope.barcode1})
736 .then(function(copy) {
739 $scope.focusBarcode = true;
740 $scope.copyNotFound = true;
744 $scope.copyId = copy.id();
745 copy.barcode($scope.barcode2);
747 egCore.pcrud.update(copy).then(function(stat) {
748 $scope.updateOK = stat;
749 $scope.focusBarcode = true;
750 if (stat) service.add_barcode_to_list(copy.barcode());
754 $uibModalInstance.close();
757 $scope.cancel = function($event) {
758 $uibModalInstance.dismiss();
759 $event.preventDefault();
767 // this "transfers" selected copies to a new owning library,
768 // auto-creating volumes and deleting unused volumes as required.
769 service.changeItemOwningLib = function(items) {
770 var xfer_target = egCore.hatch.getLocalItem('eg.cat.volume_transfer_target');
771 if (!xfer_target || !items.length) {
774 var vols_to_move = {};
775 var copies_to_move = {};
776 angular.forEach(items, function(item) {
777 if (item['call_number.owning_lib'] != xfer_target) {
778 if (item['call_number.id'] in vols_to_move) {
779 copies_to_move[item['call_number.id']].push(item.id);
781 vols_to_move[item['call_number.id']] = {
782 label : item['call_number.label'],
783 label_class : item['call_number.label_class'],
784 record : item['call_number.record.id'],
785 prefix : item['call_number.prefix.id'],
786 suffix : item['call_number.suffix.id']
788 copies_to_move[item['call_number.id']] = new Array;
789 copies_to_move[item['call_number.id']].push(item.id);
795 angular.forEach(vols_to_move, function(vol) {
796 promises.push(egCore.net.request(
798 'open-ils.cat.call_number.find_or_create',
806 ).then(function(resp) {
807 var evt = egCore.evt.parse(resp);
809 return egCore.net.request(
811 'open-ils.cat.transfer_copies_to_volume',
814 copies_to_move[vol.id]
823 function(){ service.add_barcode_to_list(cp.barcode) }
830 service.transferItems = function (items){
831 var xfer_target = egCore.hatch.getLocalItem('eg.cat.item_transfer_target');
832 var copy_ids = service.gatherSelectedHoldingsIds(items);
833 if (xfer_target && copy_ids.length > 0) {
836 'open-ils.cat.transfer_copies_to_volume',
841 function(resp) { // oncomplete
842 var evt = egCore.evt.parse(resp);
844 egConfirmDialog.open(
845 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_TITLE,
846 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_BODY,
848 ).result.then(function() {
851 'open-ils.cat.transfer_copies_to_volume.override',
855 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
858 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
861 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
871 service.mark_missing_pieces = function(copy) {
872 var b = copy.barcode();
873 var t = egCore.idl.toHash(copy.call_number()).record.title;
874 egConfirmDialog.open(
875 egCore.strings.CONFIRM_MARK_MISSING_TITLE,
876 egCore.strings.CONFIRM_MARK_MISSING_BODY,
877 { barcode : b, title : t }
878 ).result.then(function() {
880 // kick off mark missing
881 return egCore.net.request(
883 'open-ils.circ.mark_item_missing_pieces',
884 egCore.auth.token(), copy.id()
887 }).then(function(resp) {
888 var evt = egCore.evt.parse(resp); // should always produce event
890 if (evt.textcode == 'ACTION_CIRCULATION_NOT_FOUND') {
891 return egAlertDialog.open(
892 egCore.strings.CIRC_NOT_FOUND, {barcode : copy.barcode()});
895 var payload = evt.payload;
897 // TODO: open copy editor inline? new tab?
899 // print the missing pieces slip
900 var promise = $q.when();
902 // wait for completion, since it may spawn a confirm dialog
903 promise = egCore.print.print({
905 content_type : 'text/html',
906 content : payload.slip.template_output().data()
910 if (payload.letter) {
911 $scope.letter = payload.letter.template_output().data();
914 // apply patron penalty
916 promise.then(function() {
917 egCirc.create_penalty(payload.circ.usr())
924 service.print_spine_labels = function(copy_ids){
927 'open-ils.actor.anon_cache.set_value',
928 null, 'print-labels-these-copies', {
931 ).then(function(key) {
933 var url = egCore.env.basePath + 'cat/printlabels/' + key;
934 $timeout(function() { $window.open(url, '_blank') });
936 alert('Could not create anonymous cache key!');