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',
183 ['$scope','$uibModalInstance',
184 function($scope , $uibModalInstance) {
186 $scope.bucket_id = 0;
187 $scope.newBucketName = '';
188 $scope.allBuckets = [];
192 'open-ils.actor.container.retrieve_by_class.authoritative',
193 egCore.auth.token(), egCore.auth.user().id(),
194 'copy', 'staff_client'
195 ).then(function(buckets) { $scope.allBuckets = buckets; });
197 $scope.add_to_bucket = function() {
199 angular.forEach(copy_list, function (cp) {
200 var item = new egCore.idl.ccbi()
201 item.bucket($scope.bucket_id);
202 item.target_copy(cp);
206 'open-ils.actor.container.item.create',
207 egCore.auth.token(), 'copy', item
211 return $q.all(promises).then(function() {
212 $uibModalInstance.close();
217 $scope.add_to_new_bucket = function() {
218 var bucket = new egCore.idl.ccb();
219 bucket.owner(egCore.auth.user().id());
220 bucket.name($scope.newBucketName);
221 bucket.description('');
222 bucket.btype('staff_client');
224 return egCore.net.request(
226 'open-ils.actor.container.create',
227 egCore.auth.token(), 'copy', bucket
228 ).then(function(bucket) {
229 $scope.bucket_id = bucket;
230 $scope.add_to_bucket();
234 $scope.cancel = function() {
235 $uibModalInstance.dismiss();
241 service.make_copies_bookable = function(items) {
243 var copies_by_record = {};
244 var record_list = [];
248 var record_id = item['call_number.record.id'];
249 if (typeof copies_by_record[ record_id ] == 'undefined') {
250 copies_by_record[ record_id ] = [];
251 record_list.push( record_id );
253 copies_by_record[ record_id ].push(item.id);
258 var combined_results = [];
259 angular.forEach(record_list, function(record_id) {
263 'open-ils.booking.resources.create_from_copies',
265 copies_by_record[record_id]
266 ).then(function(results) {
267 if (results && results['brsrc']) {
268 combined_results = combined_results.concat(results['brsrc']);
274 $q.all(promises).then(function() {
275 if (combined_results.length > 0) {
277 template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
281 ['$scope','$location','egCore','$uibModalInstance',
282 function($scope , $location , egCore , $uibModalInstance) {
285 ses : egCore.auth.token(),
286 resultant_brsrc : combined_results.map(function(o) { return o[0]; })
289 var booking_path = '/eg/conify/global/booking/resource';
291 $scope.booking_admin_url =
292 $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
299 service.book_copies_now = function(items) {
300 var copies_by_record = {};
301 var record_list = [];
305 var record_id = item['call_number.record.id'];
306 if (typeof copies_by_record[ record_id ] == 'undefined') {
307 copies_by_record[ record_id ] = [];
308 record_list.push( record_id );
310 copies_by_record[ record_id ].push(item.id);
315 var combined_brt = [];
316 var combined_brsrc = [];
317 angular.forEach(record_list, function(record_id) {
321 'open-ils.booking.resources.create_from_copies',
323 copies_by_record[record_id]
324 ).then(function(results) {
325 if (results && results['brt']) {
326 combined_brt = combined_brt.concat(results['brt']);
328 if (results && results['brsrc']) {
329 combined_brsrc = combined_brsrc.concat(results['brsrc']);
335 $q.all(promises).then(function() {
336 if (combined_brt.length > 0 || combined_brsrc.length > 0) {
338 template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
342 ['$scope','$location','egCore','$uibModalInstance',
343 function($scope , $location , egCore , $uibModalInstance) {
346 ses : egCore.auth.token(),
347 bresv_interface_opts : {
350 ,brsrc : combined_brsrc
355 var booking_path = '/eg/booking/reservation';
357 $scope.booking_admin_url =
358 $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
366 service.requestItems = function(copy_list) {
367 if (copy_list.length == 0) return;
369 return $uibModal.open({
370 templateUrl: './cat/catalog/t_request_items',
373 ['$scope','$uibModalInstance','egUser',
374 function($scope , $uibModalInstance , egUser) {
376 $scope.first_user_fetch = true;
380 copy_list : copy_list,
381 pickup_lib: egCore.org.get(egCore.auth.user().ws_ou()),
382 user : egCore.auth.user().id()
385 egUser.get( $scope.hold_data.user ).then(function(u) {
387 $scope.barcode = u.card().barcode();
388 $scope.user_name = egUser.format_name(u);
389 $scope.hold_data.user = u.id();
392 $scope.user_name = '';
394 $scope.$watch('barcode', function (n) {
395 if (!$scope.first_user_fetch) {
396 egUser.getByBarcode(n).then(function(u) {
398 $scope.user_name = egUser.format_name(u);
399 $scope.hold_data.user = u.id();
402 $scope.user_name = '';
403 delete $scope.hold_data.user;
406 $scope.first_user_fetch = false;
409 $scope.ok = function(h) {
412 hold_type : h.hold_type,
413 pickup_lib: h.pickup_lib.id(),
419 'open-ils.circ.holds.test_and_create.batch.override',
420 egCore.auth.token(), args, h.copy_list
423 $uibModalInstance.close();
426 $scope.cancel = function($event) {
427 $uibModalInstance.dismiss();
428 $event.preventDefault();
434 service.attach_to_peer_bib = function(items) {
435 if (items.length == 0) return;
437 egCore.hatch.getItem('eg.cat.marked_conjoined_record').then(function(target_record) {
438 if (!target_record) return;
440 return $uibModal.open({
441 templateUrl: './cat/catalog/t_conjoined_selector',
444 ['$scope','$uibModalInstance',
445 function($scope , $uibModalInstance) {
446 $scope.update = false;
448 $scope.peer_type = null;
449 $scope.peer_type_list = [];
451 get_peer_types = function() {
453 return $q.when(egCore.env.bpt.list);
455 return egCore.pcrud.retrieveAll('bpt', null, {atomic : true})
456 .then(function(list) {
457 egCore.env.absorbList(list, 'bpt');
462 get_peer_types().then(function(list){
463 $scope.peer_type_list = list;
466 $scope.ok = function(type) {
469 angular.forEach(items, function (cp) {
470 var n = new egCore.idl.bpbcm();
472 n.peer_record(target_record);
473 n.target_copy(cp.id);
475 promises.push(egCore.pcrud.create(n).then(function(){service.add_barcode_to_list(cp.barcode)}));
478 return $q.all(promises).then(function(){$uibModalInstance.close()});
481 $scope.cancel = function($event) {
482 $uibModalInstance.dismiss();
483 $event.preventDefault();
490 service.selectedHoldingsCopyDelete = function (items) {
491 if (items.length == 0) return;
493 var copy_objects = [];
494 egCore.pcrud.search('acp',
495 {deleted : 'f', id : items.map(function(el){return el.id;}) },
496 { flesh : 1, flesh_fields : { acp : ['call_number'] } }
497 ).then(function(copy) {
498 copy_objects.push(copy);
502 var perCnCopies = {};
512 var cn_id = cp.call_number().id();
513 if (!cnHash[cn_id]) {
514 cnHash[cn_id] = cp.call_number();
515 perCnCopies[cn_id] = [cp];
517 perCnCopies[cn_id].push(cp);
519 cp.call_number(cn_id); // prevent loops in JSON-ification
523 angular.forEach(perCnCopies, function (v, k) {
528 angular.forEach(cnHash, function (v, k) {
532 if (cnList.length == 0) return;
536 egConfirmDialog.open(
537 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES,
538 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES_MESSAGE,
539 {copies : cp_count, volumes : cn_count}
540 ).result.then(function() {
543 'open-ils.cat.asset.volume.fleshed.batch.update.override',
544 egCore.auth.token(), cnList, 1, flags
546 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
552 service.checkin = function (items) {
553 angular.forEach(items, function (cp) {
554 egCirc.checkin({copy_barcode:cp.barcode}).then(
555 function() { service.add_barcode_to_list(cp.barcode) }
560 service.renew = function (items) {
561 angular.forEach(items, function (cp) {
562 egCirc.renew({copy_barcode:cp.barcode}).then(
563 function() { service.add_barcode_to_list(cp.barcode) }
568 service.cancel_transit = function (items) {
569 angular.forEach(items, function(cp) {
570 egCirc.find_copy_transit(null, {copy_barcode:cp.barcode})
571 .then(function(t) { return egCirc.abort_transit(t.id()) })
572 .then(function() { return service.add_barcode_to_list(cp.barcode) });
576 service.selectedHoldingsDamaged = function (items) {
577 egCirc.mark_damaged(items.map(function(el){return el.id;})).then(function(){
578 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
582 service.selectedHoldingsMissing = function (items) {
583 egCirc.mark_missing(items.map(function(el){return el.id;})).then(function(){
584 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
588 service.gatherSelectedRecordIds = function (items) {
593 if (rid_list.indexOf(item['call_number.record.id']) == -1)
594 rid_list.push(item['call_number.record.id'])
600 service.gatherSelectedVolumeIds = function (items,rid) {
605 if (rid && item['call_number.record.id'] != rid) return;
606 if (cn_id_list.indexOf(item['call_number.id']) == -1)
607 cn_id_list.push(item['call_number.id'])
613 service.gatherSelectedHoldingsIds = function (items,rid) {
618 if (rid && item['call_number.record.id'] != rid) return;
619 cp_id_list.push(item.id)
625 service.spawnHoldingsAdd = function (items,use_vols,use_copies){
626 angular.forEach(service.gatherSelectedRecordIds(items), function (r) {
628 if (use_copies) { // just a copy on existing volumes
629 angular.forEach(service.gatherSelectedVolumeIds(items,r), function (v) {
630 raw.push( {callnumber : v} );
632 } else if (use_vols) {
634 service.gatherSelectedHoldingsIds(items,r),
636 angular.forEach(items, function(item) {
637 if (i == item.id) raw.push({owner : item['call_number.owning_lib']});
643 if (raw.length == 0) raw.push({});
647 'open-ils.actor.anon_cache.set_value',
648 null, 'edit-these-copies', {
654 ).then(function(key) {
656 var url = egCore.env.basePath + 'cat/volcopy/' + key;
657 $timeout(function() { $window.open(url, '_blank') });
659 alert('Could not create anonymous cache key!');
665 service.spawnHoldingsEdit = function (items,hide_vols,hide_copies){
666 angular.forEach(service.gatherSelectedRecordIds(items), function (r) {
669 'open-ils.actor.anon_cache.set_value',
670 null, 'edit-these-copies', {
672 copies: service.gatherSelectedHoldingsIds(items,r),
674 hide_vols : hide_vols,
675 hide_copies : hide_copies
677 ).then(function(key) {
679 var url = egCore.env.basePath + 'cat/volcopy/' + key;
680 $timeout(function() { $window.open(url, '_blank') });
682 alert('Could not create anonymous cache key!');
688 service.replaceBarcodes = function(items) {
689 angular.forEach(items, function (cp) {
691 templateUrl: './cat/share/t_replace_barcode',
694 ['$scope','$uibModalInstance',
695 function($scope , $uibModalInstance) {
696 $scope.isModal = true;
697 $scope.focusBarcode = false;
698 $scope.focusBarcode2 = true;
699 $scope.barcode1 = cp.barcode;
701 $scope.updateBarcode = function() {
702 $scope.copyNotFound = false;
703 $scope.updateOK = false;
705 egCore.pcrud.search('acp',
706 {deleted : 'f', barcode : $scope.barcode1})
707 .then(function(copy) {
710 $scope.focusBarcode = true;
711 $scope.copyNotFound = true;
715 $scope.copyId = copy.id();
716 copy.barcode($scope.barcode2);
718 egCore.pcrud.update(copy).then(function(stat) {
719 $scope.updateOK = stat;
720 $scope.focusBarcode = true;
721 if (stat) service.add_barcode_to_list(copy.barcode());
725 $uibModalInstance.close();
728 $scope.cancel = function($event) {
729 $uibModalInstance.dismiss();
730 $event.preventDefault();
738 // this "transfers" selected copies to a new owning library,
739 // auto-creating volumes and deleting unused volumes as required.
740 service.changeItemOwningLib = function(items) {
741 var xfer_target = egCore.hatch.getLocalItem('eg.cat.volume_transfer_target');
742 if (!xfer_target || !items.length) {
745 var vols_to_move = {};
746 var copies_to_move = {};
747 angular.forEach(items, function(item) {
748 if (item['call_number.owning_lib'] != xfer_target) {
749 if (item['call_number.id'] in vols_to_move) {
750 copies_to_move[item['call_number.id']].push(item.id);
752 vols_to_move[item['call_number.id']] = {
753 label : item['call_number.label'],
754 label_class : item['call_number.label_class'],
755 record : item['call_number.record.id'],
756 prefix : item['call_number.prefix.id'],
757 suffix : item['call_number.suffix.id']
759 copies_to_move[item['call_number.id']] = new Array;
760 copies_to_move[item['call_number.id']].push(item.id);
766 angular.forEach(vols_to_move, function(vol) {
767 promises.push(egCore.net.request(
769 'open-ils.cat.call_number.find_or_create',
777 ).then(function(resp) {
778 var evt = egCore.evt.parse(resp);
780 return egCore.net.request(
782 'open-ils.cat.transfer_copies_to_volume',
785 copies_to_move[vol.id]
794 function(){ service.add_barcode_to_list(cp.barcode) }
801 service.transferItems = function (items){
802 var xfer_target = egCore.hatch.getLocalItem('eg.cat.item_transfer_target');
803 var copy_ids = service.gatherSelectedHoldingsIds(items);
804 if (xfer_target && copy_ids.length > 0) {
807 'open-ils.cat.transfer_copies_to_volume',
812 function(resp) { // oncomplete
813 var evt = egCore.evt.parse(resp);
815 egConfirmDialog.open(
816 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_TITLE,
817 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_BODY,
819 ).result.then(function() {
822 'open-ils.cat.transfer_copies_to_volume.override',
826 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
829 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
832 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
842 service.mark_missing_pieces = function(copy) {
843 var b = copy.barcode();
844 var t = egCore.idl.toHash(copy.call_number()).record.title;
845 egConfirmDialog.open(
846 egCore.strings.CONFIRM_MARK_MISSING_TITLE,
847 egCore.strings.CONFIRM_MARK_MISSING_BODY,
848 { barcode : b, title : t }
849 ).result.then(function() {
851 // kick off mark missing
852 return egCore.net.request(
854 'open-ils.circ.mark_item_missing_pieces',
855 egCore.auth.token(), copy.id()
858 }).then(function(resp) {
859 var evt = egCore.evt.parse(resp); // should always produce event
861 if (evt.textcode == 'ACTION_CIRCULATION_NOT_FOUND') {
862 return egAlertDialog.open(
863 egCore.strings.CIRC_NOT_FOUND, {barcode : copy.barcode()});
866 var payload = evt.payload;
868 // TODO: open copy editor inline? new tab?
870 // print the missing pieces slip
871 var promise = $q.when();
873 // wait for completion, since it may spawn a confirm dialog
874 promise = egCore.print.print({
876 content_type : 'text/html',
877 content : payload.slip.template_output().data()
881 if (payload.letter) {
882 $scope.letter = payload.letter.template_output().data();
885 // apply patron penalty
887 promise.then(function() {
888 egCirc.create_penalty(payload.circ.usr())
895 service.print_spine_labels = function(copy_ids){
898 'open-ils.actor.anon_cache.set_value',
899 null, 'print-labels-these-copies', {
902 ).then(function(key) {
904 var url = egCore.env.basePath + 'cat/printlabels/' + key;
905 $timeout(function() { $window.open(url, '_blank') });
907 alert('Could not create anonymous cache key!');