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('aacs', { 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()));
134 copyData.circ.checkin_workstation(
135 egCore.org.get(copyData.circ.checkin_workstation()));
140 // use the existing copy if possible
141 flatCopy = service.copies.filter(
142 function(c) {return c.id == copyData.copy.id()})[0];
146 flatCopy = egCore.idl.toHash(copyData.copy, true);
149 flatCopy._circ = egCore.idl.toHash(copyData.circ, true);
150 flatCopy._circ_summary = egCore.idl.toHash(copyData.circ_summary, true);
151 flatCopy._circ_lib = copyData.circ.circ_lib();
152 flatCopy._duration = copyData.circ.duration();
154 flatCopy.index = service.index++;
155 flatCopy.copy_alert_count = copyData.copy.copy_alerts().filter(function(aca) {
156 return !aca.ack_time();
159 service.copies.unshift(flatCopy);
162 //Get in-house use count
163 egCore.pcrud.search('aihu',
164 {item : flatCopy.id}, {}, {idlist : true, atomic : true})
165 .then(function(uses) {
166 flatCopy._inHouseUseCount = uses.length;
167 copyData.copy._inHouseUseCount = uses.length;
171 copy : copyData.copy,
172 index : flatCopy.index
179 service.add_copies_to_bucket = function(copy_list) {
180 if (copy_list.length == 0) return;
182 return $uibModal.open({
183 templateUrl: './cat/catalog/t_add_to_bucket',
188 ['$scope','$uibModalInstance',
189 function($scope , $uibModalInstance) {
191 $scope.bucket_id = 0;
192 $scope.newBucketName = '';
193 $scope.allBuckets = [];
197 'open-ils.actor.container.retrieve_by_class.authoritative',
198 egCore.auth.token(), egCore.auth.user().id(),
199 'copy', 'staff_client'
200 ).then(function(buckets) { $scope.allBuckets = buckets; });
202 $scope.add_to_bucket = function() {
204 angular.forEach(copy_list, function (cp) {
205 var item = new egCore.idl.ccbi()
206 item.bucket($scope.bucket_id);
207 item.target_copy(cp);
211 'open-ils.actor.container.item.create',
212 egCore.auth.token(), 'copy', item
216 return $q.all(promises).then(function() {
217 $uibModalInstance.close();
222 $scope.add_to_new_bucket = function() {
223 var bucket = new egCore.idl.ccb();
224 bucket.owner(egCore.auth.user().id());
225 bucket.name($scope.newBucketName);
226 bucket.description('');
227 bucket.btype('staff_client');
229 return egCore.net.request(
231 'open-ils.actor.container.create',
232 egCore.auth.token(), 'copy', bucket
233 ).then(function(bucket) {
234 $scope.bucket_id = bucket;
235 $scope.add_to_bucket();
239 $scope.cancel = function() {
240 $uibModalInstance.dismiss();
246 service.make_copies_bookable = function(items) {
248 var copies_by_record = {};
249 var record_list = [];
253 var record_id = item['call_number.record.id'];
254 if (typeof copies_by_record[ record_id ] == 'undefined') {
255 copies_by_record[ record_id ] = [];
256 record_list.push( record_id );
258 copies_by_record[ record_id ].push(item.id);
263 var combined_results = [];
264 angular.forEach(record_list, function(record_id) {
268 'open-ils.booking.resources.create_from_copies',
270 copies_by_record[record_id]
271 ).then(function(results) {
272 if (results && results['brsrc']) {
273 combined_results = combined_results.concat(results['brsrc']);
279 $q.all(promises).then(function() {
280 if (combined_results.length > 0) {
282 template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
287 ['$scope','$location','egCore','$uibModalInstance',
288 function($scope , $location , egCore , $uibModalInstance) {
291 ses : egCore.auth.token(),
292 resultant_brsrc : combined_results.map(function(o) { return o[0]; })
295 var booking_path = '/eg/conify/global/booking/resource';
297 $scope.booking_admin_url =
298 $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
305 service.book_copies_now = function(items) {
306 var copies_by_record = {};
307 var record_list = [];
311 var record_id = item['call_number.record.id'];
312 if (typeof copies_by_record[ record_id ] == 'undefined') {
313 copies_by_record[ record_id ] = [];
314 record_list.push( record_id );
316 copies_by_record[ record_id ].push(item.id);
321 var combined_brt = [];
322 var combined_brsrc = [];
323 angular.forEach(record_list, function(record_id) {
327 'open-ils.booking.resources.create_from_copies',
329 copies_by_record[record_id]
330 ).then(function(results) {
331 if (results && results['brt']) {
332 combined_brt = combined_brt.concat(results['brt']);
334 if (results && results['brsrc']) {
335 combined_brsrc = combined_brsrc.concat(results['brsrc']);
341 $q.all(promises).then(function() {
342 if (combined_brt.length > 0 || combined_brsrc.length > 0) {
344 template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
349 ['$scope','$location','egCore','$uibModalInstance',
350 function($scope , $location , egCore , $uibModalInstance) {
353 ses : egCore.auth.token(),
354 bresv_interface_opts : {
357 ,brsrc : combined_brsrc
362 var booking_path = '/eg/booking/reservation';
364 $scope.booking_admin_url =
365 $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
373 service.requestItems = function(copy_list) {
374 if (copy_list.length == 0) return;
376 return $uibModal.open({
377 templateUrl: './cat/catalog/t_request_items',
381 ['$scope','$uibModalInstance','egUser',
382 function($scope , $uibModalInstance , egUser) {
384 $scope.first_user_fetch = true;
388 copy_list : copy_list,
389 pickup_lib: egCore.org.get(egCore.auth.user().ws_ou()),
390 user : egCore.auth.user().id()
393 egUser.get( $scope.hold_data.user ).then(function(u) {
395 $scope.barcode = u.card().barcode();
396 $scope.user_name = egUser.format_name(u);
397 $scope.hold_data.user = u.id();
400 $scope.user_name = '';
402 $scope.$watch('barcode', function (n) {
403 if (!$scope.first_user_fetch) {
404 egUser.getByBarcode(n).then(function(u) {
406 $scope.user_name = egUser.format_name(u);
407 $scope.hold_data.user = u.id();
410 $scope.user_name = '';
411 delete $scope.hold_data.user;
414 $scope.first_user_fetch = false;
417 $scope.ok = function(h) {
420 hold_type : h.hold_type,
421 pickup_lib: h.pickup_lib.id(),
427 'open-ils.circ.holds.test_and_create.batch.override',
428 egCore.auth.token(), args, h.copy_list
431 $uibModalInstance.close();
434 $scope.cancel = function($event) {
435 $uibModalInstance.dismiss();
436 $event.preventDefault();
442 service.attach_to_peer_bib = function(items) {
443 if (items.length == 0) return;
445 egCore.hatch.getItem('eg.cat.marked_conjoined_record').then(function(target_record) {
446 if (!target_record) return;
448 return $uibModal.open({
449 templateUrl: './cat/catalog/t_conjoined_selector',
453 ['$scope','$uibModalInstance',
454 function($scope , $uibModalInstance) {
455 $scope.update = false;
457 $scope.peer_type = null;
458 $scope.peer_type_list = [];
460 get_peer_types = function() {
462 return $q.when(egCore.env.bpt.list);
464 return egCore.pcrud.retrieveAll('bpt', null, {atomic : true})
465 .then(function(list) {
466 egCore.env.absorbList(list, 'bpt');
471 get_peer_types().then(function(list){
472 $scope.peer_type_list = list;
475 $scope.ok = function(type) {
478 angular.forEach(items, function (cp) {
479 var n = new egCore.idl.bpbcm();
481 n.peer_record(target_record);
482 n.target_copy(cp.id);
484 promises.push(egCore.pcrud.create(n).then(function(){service.add_barcode_to_list(cp.barcode)}));
487 return $q.all(promises).then(function(){$uibModalInstance.close()});
490 $scope.cancel = function($event) {
491 $uibModalInstance.dismiss();
492 $event.preventDefault();
499 service.selectedHoldingsCopyDelete = function (items) {
500 if (items.length == 0) return;
502 var copy_objects = [];
503 egCore.pcrud.search('acp',
504 {deleted : 'f', id : items.map(function(el){return el.id;}) },
505 { flesh : 1, flesh_fields : { acp : ['call_number'] } }
506 ).then(function(copy) {
507 copy_objects.push(copy);
511 var perCnCopies = {};
521 var cn_id = cp.call_number().id();
522 if (!cnHash[cn_id]) {
523 cnHash[cn_id] = cp.call_number();
524 perCnCopies[cn_id] = [cp];
526 perCnCopies[cn_id].push(cp);
528 cp.call_number(cn_id); // prevent loops in JSON-ification
532 angular.forEach(perCnCopies, function (v, k) {
537 angular.forEach(cnHash, function (v, k) {
541 if (cnList.length == 0) return;
545 egConfirmDialog.open(
546 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES,
547 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES_MESSAGE,
548 {copies : cp_count, volumes : cn_count}
549 ).result.then(function() {
552 'open-ils.cat.asset.volume.fleshed.batch.update.override',
553 egCore.auth.token(), cnList, 1, flags
555 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
561 service.checkin = function (items) {
562 angular.forEach(items, function (cp) {
563 egCirc.checkin({copy_barcode:cp.barcode}).then(
564 function() { service.add_barcode_to_list(cp.barcode) }
569 service.renew = function (items) {
570 angular.forEach(items, function (cp) {
571 egCirc.renew({copy_barcode:cp.barcode}).then(
572 function() { service.add_barcode_to_list(cp.barcode) }
577 service.cancel_transit = function (items) {
578 angular.forEach(items, function(cp) {
579 egCirc.find_copy_transit(null, {copy_barcode:cp.barcode})
580 .then(function(t) { return egCirc.abort_transit(t.id()) })
581 .then(function() { return service.add_barcode_to_list(cp.barcode) });
585 service.selectedHoldingsDamaged = function (items) {
586 angular.forEach(items, function(cp) {
588 egCirc.mark_damaged({
597 service.selectedHoldingsMissing = function (items) {
598 egCirc.mark_missing(items.map(function(el){return el.id;})).then(function(){
599 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
603 service.gatherSelectedRecordIds = function (items) {
608 if (rid_list.indexOf(item['call_number.record.id']) == -1)
609 rid_list.push(item['call_number.record.id'])
615 service.gatherSelectedVolumeIds = function (items,rid) {
620 if (rid && item['call_number.record.id'] != rid) return;
621 if (cn_id_list.indexOf(item['call_number.id']) == -1)
622 cn_id_list.push(item['call_number.id'])
628 service.gatherSelectedHoldingsIds = function (items,rid) {
633 if (rid && item['call_number.record.id'] != rid) return;
634 cp_id_list.push(item.id)
640 service.spawnHoldingsAdd = function (items,use_vols,use_copies){
641 angular.forEach(service.gatherSelectedRecordIds(items), function (r) {
643 if (use_copies) { // just a copy on existing volumes
644 angular.forEach(service.gatherSelectedVolumeIds(items,r), function (v) {
645 raw.push( {callnumber : v} );
647 } else if (use_vols) {
649 service.gatherSelectedHoldingsIds(items,r),
651 angular.forEach(items, function(item) {
652 if (i == item.id) raw.push({owner : item['call_number.owning_lib']});
658 if (raw.length == 0) raw.push({});
662 'open-ils.actor.anon_cache.set_value',
663 null, 'edit-these-copies', {
669 ).then(function(key) {
671 var url = egCore.env.basePath + 'cat/volcopy/' + key;
672 $timeout(function() { $window.open(url, '_blank') });
674 alert('Could not create anonymous cache key!');
680 service.spawnHoldingsEdit = function (items,hide_vols,hide_copies){
682 angular.forEach(items, function(i){
688 'open-ils.actor.anon_cache.set_value',
692 record_id: 0, // disables record summary
695 hide_vols : hide_vols,
696 hide_copies : hide_copies
697 }).then(function(key) {
699 var url = egCore.env.basePath + 'cat/volcopy/' + key;
700 $timeout(function() { $window.open(url, '_blank') });
702 alert('Could not create anonymous cache key!');
707 service.replaceBarcodes = function(items) {
708 angular.forEach(items, function (cp) {
710 templateUrl: './cat/share/t_replace_barcode',
714 ['$scope','$uibModalInstance',
715 function($scope , $uibModalInstance) {
716 $scope.isModal = true;
717 $scope.focusBarcode = false;
718 $scope.focusBarcode2 = true;
719 $scope.barcode1 = cp.barcode;
721 $scope.updateBarcode = function() {
722 $scope.copyNotFound = false;
723 $scope.updateOK = false;
725 egCore.pcrud.search('acp',
726 {deleted : 'f', barcode : $scope.barcode1})
727 .then(function(copy) {
730 $scope.focusBarcode = true;
731 $scope.copyNotFound = true;
735 $scope.copyId = copy.id();
736 copy.barcode($scope.barcode2);
738 egCore.pcrud.update(copy).then(function(stat) {
739 $scope.updateOK = stat;
740 $scope.focusBarcode = true;
741 if (stat) service.add_barcode_to_list(copy.barcode());
745 $uibModalInstance.close();
748 $scope.cancel = function($event) {
749 $uibModalInstance.dismiss();
750 $event.preventDefault();
758 // this "transfers" selected copies to a new owning library,
759 // auto-creating volumes and deleting unused volumes as required.
760 service.changeItemOwningLib = function(items) {
761 var xfer_target = egCore.hatch.getLocalItem('eg.cat.volume_transfer_target');
762 if (!xfer_target || !items.length) {
765 var vols_to_move = {};
766 var copies_to_move = {};
767 angular.forEach(items, function(item) {
768 if (item['call_number.owning_lib'] != xfer_target) {
769 if (item['call_number.id'] in vols_to_move) {
770 copies_to_move[item['call_number.id']].push(item.id);
772 vols_to_move[item['call_number.id']] = {
773 label : item['call_number.label'],
774 label_class : item['call_number.label_class'],
775 record : item['call_number.record.id'],
776 prefix : item['call_number.prefix.id'],
777 suffix : item['call_number.suffix.id']
779 copies_to_move[item['call_number.id']] = new Array;
780 copies_to_move[item['call_number.id']].push(item.id);
786 angular.forEach(vols_to_move, function(vol) {
787 promises.push(egCore.net.request(
789 'open-ils.cat.call_number.find_or_create',
797 ).then(function(resp) {
798 var evt = egCore.evt.parse(resp);
800 return egCore.net.request(
802 'open-ils.cat.transfer_copies_to_volume',
805 copies_to_move[vol.id]
814 function(){ service.add_barcode_to_list(cp.barcode) }
821 service.transferItems = function (items){
822 var xfer_target = egCore.hatch.getLocalItem('eg.cat.item_transfer_target');
823 var copy_ids = service.gatherSelectedHoldingsIds(items);
824 if (xfer_target && copy_ids.length > 0) {
827 'open-ils.cat.transfer_copies_to_volume',
832 function(resp) { // oncomplete
833 var evt = egCore.evt.parse(resp);
835 egConfirmDialog.open(
836 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_TITLE,
837 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_BODY,
839 ).result.then(function() {
842 'open-ils.cat.transfer_copies_to_volume.override',
846 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
849 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
852 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
862 service.mark_missing_pieces = function(copy) {
863 var b = copy.barcode();
864 var t = egCore.idl.toHash(copy.call_number()).record.title;
865 egConfirmDialog.open(
866 egCore.strings.CONFIRM_MARK_MISSING_TITLE,
867 egCore.strings.CONFIRM_MARK_MISSING_BODY,
868 { barcode : b, title : t }
869 ).result.then(function() {
871 // kick off mark missing
872 return egCore.net.request(
874 'open-ils.circ.mark_item_missing_pieces',
875 egCore.auth.token(), copy.id()
878 }).then(function(resp) {
879 var evt = egCore.evt.parse(resp); // should always produce event
881 if (evt.textcode == 'ACTION_CIRCULATION_NOT_FOUND') {
882 return egAlertDialog.open(
883 egCore.strings.CIRC_NOT_FOUND, {barcode : copy.barcode()});
886 var payload = evt.payload;
888 // TODO: open copy editor inline? new tab?
890 // print the missing pieces slip
891 var promise = $q.when();
893 // wait for completion, since it may spawn a confirm dialog
894 promise = egCore.print.print({
896 content_type : 'text/html',
897 content : payload.slip.template_output().data()
901 if (payload.letter) {
902 $scope.letter = payload.letter.template_output().data();
905 // apply patron penalty
907 promise.then(function() {
908 egCirc.create_penalty(payload.circ.usr())
915 service.print_spine_labels = function(copy_ids){
918 'open-ils.actor.anon_cache.set_value',
919 null, 'print-labels-these-copies', {
922 ).then(function(key) {
924 var url = egCore.env.basePath + 'cat/printlabels/' + key;
925 $timeout(function() { $window.open(url, '_blank') });
927 alert('Could not create anonymous cache key!');