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('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 service.copies.unshift(flatCopy);
158 //Get in-house use count
159 egCore.pcrud.search('aihu',
160 {item : flatCopy.id}, {}, {idlist : true, atomic : true})
161 .then(function(uses) {
162 flatCopy._inHouseUseCount = uses.length;
163 copyData.copy._inHouseUseCount = uses.length;
167 copy : copyData.copy,
168 index : flatCopy.index
175 service.add_copies_to_bucket = function(copy_list) {
176 if (copy_list.length == 0) return;
178 return $uibModal.open({
179 templateUrl: './cat/catalog/t_add_to_bucket',
184 ['$scope','$uibModalInstance',
185 function($scope , $uibModalInstance) {
187 $scope.bucket_id = 0;
188 $scope.newBucketName = '';
189 $scope.allBuckets = [];
193 'open-ils.actor.container.retrieve_by_class.authoritative',
194 egCore.auth.token(), egCore.auth.user().id(),
195 'copy', 'staff_client'
196 ).then(function(buckets) { $scope.allBuckets = buckets; });
198 $scope.add_to_bucket = function() {
200 angular.forEach(copy_list, function (cp) {
201 var item = new egCore.idl.ccbi()
202 item.bucket($scope.bucket_id);
203 item.target_copy(cp);
207 'open-ils.actor.container.item.create',
208 egCore.auth.token(), 'copy', item
212 return $q.all(promises).then(function() {
213 $uibModalInstance.close();
218 $scope.add_to_new_bucket = function() {
219 var bucket = new egCore.idl.ccb();
220 bucket.owner(egCore.auth.user().id());
221 bucket.name($scope.newBucketName);
222 bucket.description('');
223 bucket.btype('staff_client');
225 return egCore.net.request(
227 'open-ils.actor.container.create',
228 egCore.auth.token(), 'copy', bucket
229 ).then(function(bucket) {
230 $scope.bucket_id = bucket;
231 $scope.add_to_bucket();
235 $scope.cancel = function() {
236 $uibModalInstance.dismiss();
242 service.make_copies_bookable = function(items) {
244 var copies_by_record = {};
245 var record_list = [];
249 var record_id = item['call_number.record.id'];
250 if (typeof copies_by_record[ record_id ] == 'undefined') {
251 copies_by_record[ record_id ] = [];
252 record_list.push( record_id );
254 copies_by_record[ record_id ].push(item.id);
259 var combined_results = [];
260 angular.forEach(record_list, function(record_id) {
264 'open-ils.booking.resources.create_from_copies',
266 copies_by_record[record_id]
267 ).then(function(results) {
268 if (results && results['brsrc']) {
269 combined_results = combined_results.concat(results['brsrc']);
275 $q.all(promises).then(function() {
276 if (combined_results.length > 0) {
278 template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
283 ['$scope','$location','egCore','$uibModalInstance',
284 function($scope , $location , egCore , $uibModalInstance) {
287 ses : egCore.auth.token(),
288 resultant_brsrc : combined_results.map(function(o) { return o[0]; })
291 var booking_path = '/eg/conify/global/booking/resource';
293 $scope.booking_admin_url =
294 $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
301 service.book_copies_now = function(items) {
302 var copies_by_record = {};
303 var record_list = [];
307 var record_id = item['call_number.record.id'];
308 if (typeof copies_by_record[ record_id ] == 'undefined') {
309 copies_by_record[ record_id ] = [];
310 record_list.push( record_id );
312 copies_by_record[ record_id ].push(item.id);
317 var combined_brt = [];
318 var combined_brsrc = [];
319 angular.forEach(record_list, function(record_id) {
323 'open-ils.booking.resources.create_from_copies',
325 copies_by_record[record_id]
326 ).then(function(results) {
327 if (results && results['brt']) {
328 combined_brt = combined_brt.concat(results['brt']);
330 if (results && results['brsrc']) {
331 combined_brsrc = combined_brsrc.concat(results['brsrc']);
337 $q.all(promises).then(function() {
338 if (combined_brt.length > 0 || combined_brsrc.length > 0) {
340 template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
345 ['$scope','$location','egCore','$uibModalInstance',
346 function($scope , $location , egCore , $uibModalInstance) {
349 ses : egCore.auth.token(),
350 bresv_interface_opts : {
353 ,brsrc : combined_brsrc
358 var booking_path = '/eg/booking/reservation';
360 $scope.booking_admin_url =
361 $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
369 service.requestItems = function(copy_list) {
370 if (copy_list.length == 0) return;
372 return $uibModal.open({
373 templateUrl: './cat/catalog/t_request_items',
377 ['$scope','$uibModalInstance','egUser',
378 function($scope , $uibModalInstance , egUser) {
380 $scope.first_user_fetch = true;
384 copy_list : copy_list,
385 pickup_lib: egCore.org.get(egCore.auth.user().ws_ou()),
386 user : egCore.auth.user().id()
389 egUser.get( $scope.hold_data.user ).then(function(u) {
391 $scope.barcode = u.card().barcode();
392 $scope.user_name = egUser.format_name(u);
393 $scope.hold_data.user = u.id();
396 $scope.user_name = '';
398 $scope.$watch('barcode', function (n) {
399 if (!$scope.first_user_fetch) {
400 egUser.getByBarcode(n).then(function(u) {
402 $scope.user_name = egUser.format_name(u);
403 $scope.hold_data.user = u.id();
406 $scope.user_name = '';
407 delete $scope.hold_data.user;
410 $scope.first_user_fetch = false;
413 $scope.ok = function(h) {
416 hold_type : h.hold_type,
417 pickup_lib: h.pickup_lib.id(),
423 'open-ils.circ.holds.test_and_create.batch.override',
424 egCore.auth.token(), args, h.copy_list
427 $uibModalInstance.close();
430 $scope.cancel = function($event) {
431 $uibModalInstance.dismiss();
432 $event.preventDefault();
438 service.attach_to_peer_bib = function(items) {
439 if (items.length == 0) return;
441 egCore.hatch.getItem('eg.cat.marked_conjoined_record').then(function(target_record) {
442 if (!target_record) return;
444 return $uibModal.open({
445 templateUrl: './cat/catalog/t_conjoined_selector',
449 ['$scope','$uibModalInstance',
450 function($scope , $uibModalInstance) {
451 $scope.update = false;
453 $scope.peer_type = null;
454 $scope.peer_type_list = [];
456 get_peer_types = function() {
458 return $q.when(egCore.env.bpt.list);
460 return egCore.pcrud.retrieveAll('bpt', null, {atomic : true})
461 .then(function(list) {
462 egCore.env.absorbList(list, 'bpt');
467 get_peer_types().then(function(list){
468 $scope.peer_type_list = list;
471 $scope.ok = function(type) {
474 angular.forEach(items, function (cp) {
475 var n = new egCore.idl.bpbcm();
477 n.peer_record(target_record);
478 n.target_copy(cp.id);
480 promises.push(egCore.pcrud.create(n).then(function(){service.add_barcode_to_list(cp.barcode)}));
483 return $q.all(promises).then(function(){$uibModalInstance.close()});
486 $scope.cancel = function($event) {
487 $uibModalInstance.dismiss();
488 $event.preventDefault();
495 service.selectedHoldingsCopyDelete = function (items) {
496 if (items.length == 0) return;
498 var copy_objects = [];
499 egCore.pcrud.search('acp',
500 {deleted : 'f', id : items.map(function(el){return el.id;}) },
501 { flesh : 1, flesh_fields : { acp : ['call_number'] } }
502 ).then(function(copy) {
503 copy_objects.push(copy);
507 var perCnCopies = {};
517 var cn_id = cp.call_number().id();
518 if (!cnHash[cn_id]) {
519 cnHash[cn_id] = cp.call_number();
520 perCnCopies[cn_id] = [cp];
522 perCnCopies[cn_id].push(cp);
524 cp.call_number(cn_id); // prevent loops in JSON-ification
528 angular.forEach(perCnCopies, function (v, k) {
533 angular.forEach(cnHash, function (v, k) {
537 if (cnList.length == 0) return;
541 egConfirmDialog.open(
542 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES,
543 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES_MESSAGE,
544 {copies : cp_count, volumes : cn_count}
545 ).result.then(function() {
548 'open-ils.cat.asset.volume.fleshed.batch.update.override',
549 egCore.auth.token(), cnList, 1, flags
551 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
557 service.checkin = function (items) {
558 angular.forEach(items, function (cp) {
559 egCirc.checkin({copy_barcode:cp.barcode}).then(
560 function() { service.add_barcode_to_list(cp.barcode) }
565 service.renew = function (items) {
566 angular.forEach(items, function (cp) {
567 egCirc.renew({copy_barcode:cp.barcode}).then(
568 function() { service.add_barcode_to_list(cp.barcode) }
573 service.cancel_transit = function (items) {
574 angular.forEach(items, function(cp) {
575 egCirc.find_copy_transit(null, {copy_barcode:cp.barcode})
576 .then(function(t) { return egCirc.abort_transit(t.id()) })
577 .then(function() { return service.add_barcode_to_list(cp.barcode) });
581 service.selectedHoldingsDamaged = function (items) {
582 egCirc.mark_damaged(items.map(function(el){return el.id;})).then(function(){
583 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
587 service.selectedHoldingsMissing = function (items) {
588 egCirc.mark_missing(items.map(function(el){return el.id;})).then(function(){
589 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
593 service.gatherSelectedRecordIds = function (items) {
598 if (rid_list.indexOf(item['call_number.record.id']) == -1)
599 rid_list.push(item['call_number.record.id'])
605 service.gatherSelectedVolumeIds = function (items,rid) {
610 if (rid && item['call_number.record.id'] != rid) return;
611 if (cn_id_list.indexOf(item['call_number.id']) == -1)
612 cn_id_list.push(item['call_number.id'])
618 service.gatherSelectedHoldingsIds = function (items,rid) {
623 if (rid && item['call_number.record.id'] != rid) return;
624 cp_id_list.push(item.id)
630 service.spawnHoldingsAdd = function (items,use_vols,use_copies){
631 angular.forEach(service.gatherSelectedRecordIds(items), function (r) {
633 if (use_copies) { // just a copy on existing volumes
634 angular.forEach(service.gatherSelectedVolumeIds(items,r), function (v) {
635 raw.push( {callnumber : v} );
637 } else if (use_vols) {
639 service.gatherSelectedHoldingsIds(items,r),
641 angular.forEach(items, function(item) {
642 if (i == item.id) raw.push({owner : item['call_number.owning_lib']});
648 if (raw.length == 0) raw.push({});
652 'open-ils.actor.anon_cache.set_value',
653 null, 'edit-these-copies', {
659 ).then(function(key) {
661 var url = egCore.env.basePath + 'cat/volcopy/' + key;
662 $timeout(function() { $window.open(url, '_blank') });
664 alert('Could not create anonymous cache key!');
670 service.spawnHoldingsEdit = function (items,hide_vols,hide_copies){
671 angular.forEach(service.gatherSelectedRecordIds(items), function (r) {
674 'open-ils.actor.anon_cache.set_value',
675 null, 'edit-these-copies', {
677 copies: service.gatherSelectedHoldingsIds(items,r),
679 hide_vols : hide_vols,
680 hide_copies : hide_copies
682 ).then(function(key) {
684 var url = egCore.env.basePath + 'cat/volcopy/' + key;
685 $timeout(function() { $window.open(url, '_blank') });
687 alert('Could not create anonymous cache key!');
693 service.replaceBarcodes = function(items) {
694 angular.forEach(items, function (cp) {
696 templateUrl: './cat/share/t_replace_barcode',
700 ['$scope','$uibModalInstance',
701 function($scope , $uibModalInstance) {
702 $scope.isModal = true;
703 $scope.focusBarcode = false;
704 $scope.focusBarcode2 = true;
705 $scope.barcode1 = cp.barcode;
707 $scope.updateBarcode = function() {
708 $scope.copyNotFound = false;
709 $scope.updateOK = false;
711 egCore.pcrud.search('acp',
712 {deleted : 'f', barcode : $scope.barcode1})
713 .then(function(copy) {
716 $scope.focusBarcode = true;
717 $scope.copyNotFound = true;
721 $scope.copyId = copy.id();
722 copy.barcode($scope.barcode2);
724 egCore.pcrud.update(copy).then(function(stat) {
725 $scope.updateOK = stat;
726 $scope.focusBarcode = true;
727 if (stat) service.add_barcode_to_list(copy.barcode());
731 $uibModalInstance.close();
734 $scope.cancel = function($event) {
735 $uibModalInstance.dismiss();
736 $event.preventDefault();
744 // this "transfers" selected copies to a new owning library,
745 // auto-creating volumes and deleting unused volumes as required.
746 service.changeItemOwningLib = function(items) {
747 var xfer_target = egCore.hatch.getLocalItem('eg.cat.volume_transfer_target');
748 if (!xfer_target || !items.length) {
751 var vols_to_move = {};
752 var copies_to_move = {};
753 angular.forEach(items, function(item) {
754 if (item['call_number.owning_lib'] != xfer_target) {
755 if (item['call_number.id'] in vols_to_move) {
756 copies_to_move[item['call_number.id']].push(item.id);
758 vols_to_move[item['call_number.id']] = {
759 label : item['call_number.label'],
760 label_class : item['call_number.label_class'],
761 record : item['call_number.record.id'],
762 prefix : item['call_number.prefix.id'],
763 suffix : item['call_number.suffix.id']
765 copies_to_move[item['call_number.id']] = new Array;
766 copies_to_move[item['call_number.id']].push(item.id);
772 angular.forEach(vols_to_move, function(vol) {
773 promises.push(egCore.net.request(
775 'open-ils.cat.call_number.find_or_create',
783 ).then(function(resp) {
784 var evt = egCore.evt.parse(resp);
786 return egCore.net.request(
788 'open-ils.cat.transfer_copies_to_volume',
791 copies_to_move[vol.id]
800 function(){ service.add_barcode_to_list(cp.barcode) }
807 service.transferItems = function (items){
808 var xfer_target = egCore.hatch.getLocalItem('eg.cat.item_transfer_target');
809 var copy_ids = service.gatherSelectedHoldingsIds(items);
810 if (xfer_target && copy_ids.length > 0) {
813 'open-ils.cat.transfer_copies_to_volume',
818 function(resp) { // oncomplete
819 var evt = egCore.evt.parse(resp);
821 egConfirmDialog.open(
822 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_TITLE,
823 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_BODY,
825 ).result.then(function() {
828 'open-ils.cat.transfer_copies_to_volume.override',
832 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
835 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
838 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
848 service.mark_missing_pieces = function(copy) {
849 var b = copy.barcode();
850 var t = egCore.idl.toHash(copy.call_number()).record.title;
851 egConfirmDialog.open(
852 egCore.strings.CONFIRM_MARK_MISSING_TITLE,
853 egCore.strings.CONFIRM_MARK_MISSING_BODY,
854 { barcode : b, title : t }
855 ).result.then(function() {
857 // kick off mark missing
858 return egCore.net.request(
860 'open-ils.circ.mark_item_missing_pieces',
861 egCore.auth.token(), copy.id()
864 }).then(function(resp) {
865 var evt = egCore.evt.parse(resp); // should always produce event
867 if (evt.textcode == 'ACTION_CIRCULATION_NOT_FOUND') {
868 return egAlertDialog.open(
869 egCore.strings.CIRC_NOT_FOUND, {barcode : copy.barcode()});
872 var payload = evt.payload;
874 // TODO: open copy editor inline? new tab?
876 // print the missing pieces slip
877 var promise = $q.when();
879 // wait for completion, since it may spawn a confirm dialog
880 promise = egCore.print.print({
882 content_type : 'text/html',
883 content : payload.slip.template_output().data()
887 if (payload.letter) {
888 $scope.letter = payload.letter.template_output().data();
891 // apply patron penalty
893 promise.then(function() {
894 egCirc.create_penalty(payload.circ.usr())
901 service.print_spine_labels = function(copy_ids){
904 'open-ils.actor.anon_cache.set_value',
905 null, 'print-labels-these-copies', {
908 ).then(function(key) {
910 var url = egCore.env.basePath + 'cat/printlabels/' + key;
911 $timeout(function() { $window.open(url, '_blank') });
913 alert('Could not create anonymous cache key!');