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) {
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 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'] } }
509 ).then(function(copy) {
510 copy_objects.push(copy);
514 var perCnCopies = {};
524 var cn_id = cp.call_number().id();
525 if (!cnHash[cn_id]) {
526 cnHash[cn_id] = cp.call_number();
527 perCnCopies[cn_id] = [cp];
529 perCnCopies[cn_id].push(cp);
531 cp.call_number(cn_id); // prevent loops in JSON-ification
535 angular.forEach(perCnCopies, function (v, k) {
540 angular.forEach(cnHash, function (v, k) {
544 if (cnList.length == 0) return;
548 egConfirmDialog.open(
549 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES,
550 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES_MESSAGE,
551 {copies : cp_count, volumes : cn_count}
552 ).result.then(function() {
555 'open-ils.cat.asset.volume.fleshed.batch.update.override',
556 egCore.auth.token(), cnList, 1, flags
558 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
564 service.checkin = function (items) {
565 angular.forEach(items, function (cp) {
566 egCirc.checkin({copy_barcode:cp.barcode}).then(
567 function() { service.add_barcode_to_list(cp.barcode) }
572 service.renew = function (items) {
573 angular.forEach(items, function (cp) {
574 egCirc.renew({copy_barcode:cp.barcode}).then(
575 function() { service.add_barcode_to_list(cp.barcode) }
580 service.cancel_transit = function (items) {
581 angular.forEach(items, function(cp) {
582 egCirc.find_copy_transit(null, {copy_barcode:cp.barcode})
583 .then(function(t) { return egCirc.abort_transit(t.id()) })
584 .then(function() { return service.add_barcode_to_list(cp.barcode) });
588 service.selectedHoldingsDamaged = function (items) {
589 angular.forEach(items, function(cp) {
591 egCirc.mark_damaged({
600 service.selectedHoldingsMissing = function (items) {
601 egCirc.mark_missing(items.map(function(el){return el.id;})).then(function(){
602 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
606 service.gatherSelectedRecordIds = function (items) {
611 if (rid_list.indexOf(item['call_number.record.id']) == -1)
612 rid_list.push(item['call_number.record.id'])
618 service.gatherSelectedVolumeIds = function (items,rid) {
623 if (rid && item['call_number.record.id'] != rid) return;
624 if (cn_id_list.indexOf(item['call_number.id']) == -1)
625 cn_id_list.push(item['call_number.id'])
631 service.gatherSelectedHoldingsIds = function (items,rid) {
636 if (rid && item['call_number.record.id'] != rid) return;
637 cp_id_list.push(item.id)
643 service.spawnHoldingsAdd = function (items,use_vols,use_copies){
644 angular.forEach(service.gatherSelectedRecordIds(items), function (r) {
646 if (use_copies) { // just a copy on existing volumes
647 angular.forEach(service.gatherSelectedVolumeIds(items,r), function (v) {
648 raw.push( {callnumber : v} );
650 } else if (use_vols) {
652 service.gatherSelectedHoldingsIds(items,r),
654 angular.forEach(items, function(item) {
655 if (i == item.id) raw.push({owner : item['call_number.owning_lib']});
661 if (raw.length == 0) raw.push({});
665 'open-ils.actor.anon_cache.set_value',
666 null, 'edit-these-copies', {
672 ).then(function(key) {
674 var url = egCore.env.basePath + 'cat/volcopy/' + key;
675 $timeout(function() { $window.open(url, '_blank') });
677 alert('Could not create anonymous cache key!');
683 service.spawnHoldingsEdit = function (items,hide_vols,hide_copies){
685 angular.forEach(items, function(i){
691 'open-ils.actor.anon_cache.set_value',
695 record_id: 0, // disables record summary
698 hide_vols : hide_vols,
699 hide_copies : hide_copies
700 }).then(function(key) {
702 var url = egCore.env.basePath + 'cat/volcopy/' + key;
703 $timeout(function() { $window.open(url, '_blank') });
705 alert('Could not create anonymous cache key!');
710 service.replaceBarcodes = function(items) {
711 angular.forEach(items, function (cp) {
713 templateUrl: './cat/share/t_replace_barcode',
717 ['$scope','$uibModalInstance',
718 function($scope , $uibModalInstance) {
719 $scope.isModal = true;
720 $scope.focusBarcode = false;
721 $scope.focusBarcode2 = true;
722 $scope.barcode1 = cp.barcode;
724 $scope.updateBarcode = function() {
725 $scope.copyNotFound = false;
726 $scope.updateOK = false;
728 egCore.pcrud.search('acp',
729 {deleted : 'f', barcode : $scope.barcode1})
730 .then(function(copy) {
733 $scope.focusBarcode = true;
734 $scope.copyNotFound = true;
738 $scope.copyId = copy.id();
739 copy.barcode($scope.barcode2);
741 egCore.pcrud.update(copy).then(function(stat) {
742 $scope.updateOK = stat;
743 $scope.focusBarcode = true;
744 if (stat) service.add_barcode_to_list(copy.barcode());
748 $uibModalInstance.close();
751 $scope.cancel = function($event) {
752 $uibModalInstance.dismiss();
753 $event.preventDefault();
761 // this "transfers" selected copies to a new owning library,
762 // auto-creating volumes and deleting unused volumes as required.
763 service.changeItemOwningLib = function(items) {
764 var xfer_target = egCore.hatch.getLocalItem('eg.cat.volume_transfer_target');
765 if (!xfer_target || !items.length) {
768 var vols_to_move = {};
769 var copies_to_move = {};
770 angular.forEach(items, function(item) {
771 if (item['call_number.owning_lib'] != xfer_target) {
772 if (item['call_number.id'] in vols_to_move) {
773 copies_to_move[item['call_number.id']].push(item.id);
775 vols_to_move[item['call_number.id']] = {
776 label : item['call_number.label'],
777 label_class : item['call_number.label_class'],
778 record : item['call_number.record.id'],
779 prefix : item['call_number.prefix.id'],
780 suffix : item['call_number.suffix.id']
782 copies_to_move[item['call_number.id']] = new Array;
783 copies_to_move[item['call_number.id']].push(item.id);
789 angular.forEach(vols_to_move, function(vol) {
790 promises.push(egCore.net.request(
792 'open-ils.cat.call_number.find_or_create',
800 ).then(function(resp) {
801 var evt = egCore.evt.parse(resp);
803 return egCore.net.request(
805 'open-ils.cat.transfer_copies_to_volume',
808 copies_to_move[vol.id]
817 function(){ service.add_barcode_to_list(cp.barcode) }
824 service.transferItems = function (items){
825 var xfer_target = egCore.hatch.getLocalItem('eg.cat.item_transfer_target');
826 var copy_ids = service.gatherSelectedHoldingsIds(items);
827 if (xfer_target && copy_ids.length > 0) {
830 'open-ils.cat.transfer_copies_to_volume',
835 function(resp) { // oncomplete
836 var evt = egCore.evt.parse(resp);
838 egConfirmDialog.open(
839 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_TITLE,
840 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_BODY,
842 ).result.then(function() {
845 'open-ils.cat.transfer_copies_to_volume.override',
849 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
852 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
855 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
865 service.mark_missing_pieces = function(copy) {
866 var b = copy.barcode();
867 var t = egCore.idl.toHash(copy.call_number()).record.title;
868 egConfirmDialog.open(
869 egCore.strings.CONFIRM_MARK_MISSING_TITLE,
870 egCore.strings.CONFIRM_MARK_MISSING_BODY,
871 { barcode : b, title : t }
872 ).result.then(function() {
874 // kick off mark missing
875 return egCore.net.request(
877 'open-ils.circ.mark_item_missing_pieces',
878 egCore.auth.token(), copy.id()
881 }).then(function(resp) {
882 var evt = egCore.evt.parse(resp); // should always produce event
884 if (evt.textcode == 'ACTION_CIRCULATION_NOT_FOUND') {
885 return egAlertDialog.open(
886 egCore.strings.CIRC_NOT_FOUND, {barcode : copy.barcode()});
889 var payload = evt.payload;
891 // TODO: open copy editor inline? new tab?
893 // print the missing pieces slip
894 var promise = $q.when();
896 // wait for completion, since it may spawn a confirm dialog
897 promise = egCore.print.print({
899 content_type : 'text/html',
900 content : payload.slip.template_output().data()
904 if (payload.letter) {
905 $scope.letter = payload.letter.template_output().data();
908 // apply patron penalty
910 promise.then(function() {
911 egCirc.create_penalty(payload.circ.usr())
918 service.print_spine_labels = function(copy_ids){
921 'open-ils.actor.anon_cache.set_value',
922 null, 'print-labels-these-copies', {
925 ).then(function(key) {
927 var url = egCore.env.basePath + 'cat/printlabels/' + key;
928 $timeout(function() { $window.open(url, '_blank') });
930 alert('Could not create anonymous cache key!');