2 * Shared item services for circulation
5 angular.module('egCoreMod')
7 ['egCore','egOrg','egCirc','$uibModal','$q','$timeout','$window','ngToast','egConfirmDialog','egAlertDialog',
8 function(egCore , egOrg , egCirc , $uibModal , $q , $timeout , $window , ngToast , egConfirmDialog , egAlertDialog ) {
11 copies : [], // copy barcode search results
12 index : 0 // search grid index
18 acp : ['call_number','location','status','floating','circ_modifier',
19 'age_protect','circ_lib','copy_alerts', 'creator', 'editor', 'circ_as_type', 'latest_inventory'],
20 acn : ['record','prefix','suffix','label_class'],
21 bre : ['simple_record','creator','editor'],
22 alci : ['inventory_workstation']
25 // avoid fleshing MARC on the bre
26 // note: don't add simple_record.. not sure why
27 bre : ['id','tcn_value','creator','editor', 'create_date', 'edit_date'],
37 'checkin_workstation',
45 order_by : {combcirc : 'xact_start desc'},
49 //Retrieve separate copy, aacs, and accs information
50 service.getCopy = function(barcode, id) {
52 // handle barcode completion
53 return egCirc.handle_barcode_completion(barcode)
54 .then(function(actual_barcode) {
55 return egCore.pcrud.search(
56 'acp', {barcode : actual_barcode, deleted : 'f'},
57 service.flesh).then(function(copy) {return copy});
61 return egCore.pcrud.retrieve( 'acp', id, service.flesh)
62 .then(function(copy) {return copy});
65 service.getCirc = function(id) {
66 return egCore.pcrud.search('combcirc', { target_copy : id },
67 service.circFlesh).then(function(circ) {return circ});
70 service.getSummary = function(id) {
71 return circ_summary = egCore.net.request(
73 'open-ils.circ.renewal_chain.retrieve_by_circ.summary',
74 egCore.auth.token(), id).then(
75 function(circ_summary) {return circ_summary});
78 //Combine copy, circ, and accs information
79 service.retrieveCopyData = function(barcode, id) {
82 var fetchCopy = function(barcode, id) {
83 return service.getCopy(barcode, id)
84 .then(function(copy) {
89 var fetchCirc = function(copy) {
90 return service.getCirc(copy.id())
91 .then(function(circ) {
96 var fetchSummary = function(circ) {
97 return service.getSummary(circ.id())
98 .then(function(summary) {
99 copyData.circ_summary = summary;
104 return fetchCopy(barcode, id).then(function(res) {
106 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;
177 //Get Monograph Parts
178 egCore.pcrud.search('acpm',
179 {target_copy: flatCopy.id},
180 { flesh : 1, flesh_fields : { acpm : ['part'] } },
182 .then(function(acpm_array) {
183 angular.forEach(acpm_array, function(acpm) {
184 flatCopy.parts = egCore.idl.toHash(acpm.part());
185 copyData.copy.parts = egCore.idl.toHash(acpm.part());
190 copy : copyData.copy,
191 index : flatCopy.index
198 //all_items = selected grid rows, to be updated in place
199 service.updateInventory = function(copy_list, all_items) {
200 if (copy_list.length == 0) return;
201 return egCore.net.request(
203 'open-ils.circ.circulation.update_latest_inventory',
204 egCore.auth.token(), {copy_list: copy_list}
205 ).then(function(res) {
207 if (all_items) angular.forEach(copy_list, function(copy) {
208 angular.forEach(all_items, function(item) {
209 if (copy == item.id) {
210 egCore.pcrud.search('alci', {copy: copy},
211 {flesh: 1, flesh_fields:
212 {alci: ['inventory_workstation']}
213 }).then(function(alci) {
214 //update existing grid rows
215 item["latest_inventory.inventory_date"] = alci.inventory_date();
216 item["latest_inventory.inventory_workstation.name"] =
217 alci.inventory_workstation().name();
222 return all_items || res;
227 service.add_copies_to_bucket = function(copy_list) {
228 if (copy_list.length == 0) return;
230 return $uibModal.open({
231 templateUrl: './cat/catalog/t_add_to_bucket',
236 ['$scope','$uibModalInstance',
237 function($scope , $uibModalInstance) {
239 $scope.bucket_id = 0;
240 $scope.newBucketName = '';
241 $scope.allBuckets = [];
245 'open-ils.actor.container.retrieve_by_class.authoritative',
246 egCore.auth.token(), egCore.auth.user().id(),
247 'copy', 'staff_client'
248 ).then(function(buckets) { $scope.allBuckets = buckets; });
250 $scope.add_to_bucket = function() {
252 angular.forEach(copy_list, function (cp) {
253 var item = new egCore.idl.ccbi()
254 item.bucket($scope.bucket_id);
255 item.target_copy(cp);
259 'open-ils.actor.container.item.create',
260 egCore.auth.token(), 'copy', item
264 return $q.all(promises).then(function() {
265 $uibModalInstance.close();
270 $scope.add_to_new_bucket = function() {
271 var bucket = new egCore.idl.ccb();
272 bucket.owner(egCore.auth.user().id());
273 bucket.name($scope.newBucketName);
274 bucket.description('');
275 bucket.btype('staff_client');
277 return egCore.net.request(
279 'open-ils.actor.container.create',
280 egCore.auth.token(), 'copy', bucket
281 ).then(function(bucket) {
282 $scope.bucket_id = bucket;
283 $scope.add_to_bucket();
287 $scope.cancel = function() {
288 $uibModalInstance.dismiss();
294 service.make_copies_bookable = function(items) {
296 var copies_by_record = {};
297 var record_list = [];
301 var record_id = item['call_number.record.id'];
302 if (typeof copies_by_record[ record_id ] == 'undefined') {
303 copies_by_record[ record_id ] = [];
304 record_list.push( record_id );
306 copies_by_record[ record_id ].push(item.id);
311 var combined_results = [];
312 angular.forEach(record_list, function(record_id) {
316 'open-ils.booking.resources.create_from_copies',
318 copies_by_record[record_id]
319 ).then(function(results) {
320 if (results && results['brsrc']) {
321 combined_results = combined_results.concat(results['brsrc']);
327 $q.all(promises).then(function() {
328 if (combined_results.length > 0) {
330 template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
335 ['$scope','$location','egCore','$uibModalInstance',
336 function($scope , $location , egCore , $uibModalInstance) {
339 ses : egCore.auth.token(),
340 resultant_brsrc : combined_results.map(function(o) { return o[0]; })
343 var booking_path = '/eg/conify/global/booking/resource';
345 $scope.booking_admin_url =
346 $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
353 service.book_copies_now = function(barcode) {
354 location.href = "/eg2/staff/booking/create_reservation/for_resource/" + barcode;
357 service.manage_reservations = function(barcode) {
358 location.href = "/eg2/staff/booking/manage_reservations/by_resource/" + barcode;
361 service.requestItems = function(copy_list,record_list) {
362 if (copy_list.length == 0) return;
364 record_list = record_list.filter(function(v,i,s){ // dedup
365 return s.indexOf(v) == i;
369 return $uibModal.open({
370 templateUrl: './cat/catalog/t_request_items',
374 ['$scope','$uibModalInstance','egUser',
375 function($scope , $uibModalInstance , egUser) {
377 $scope.first_user_fetch = true;
381 copy_list : copy_list,
382 record_list : record_list,
383 pickup_lib: egCore.org.get(egCore.auth.user().ws_ou()),
384 user : egCore.auth.user().id(),
385 honor_user_settings :
386 egCore.hatch.getLocalItem('eg.cat.request_items.honor_user_settings')
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 function user_preferred_pickup_lib(u) {
399 var pickup_lib = u.home_ou();
400 angular.forEach(u.settings(), function (s) {
401 if (s.name() == "opac.default_pickup_location") {
402 pickup_lib = s.value();
405 return egOrg.get(pickup_lib);
407 $scope.$watch('barcode', function (n) {
408 if (!$scope.first_user_fetch) {
409 egUser.getByBarcode(n).then(function(u) {
411 $scope.user_name = egUser.format_name(u);
412 $scope.hold_data.user = u.id();
413 if ($scope.hold_data.honor_user_settings) {
414 $scope.hold_data.pickup_lib = user_preferred_pickup_lib(u);
418 $scope.user_name = '';
419 delete $scope.hold_data.user;
422 $scope.first_user_fetch = false;
424 $scope.$watch('hold_data.honor_user_settings', function (n) {
425 if (n && $scope.user) {
426 $scope.hold_data.pickup_lib = user_preferred_pickup_lib($scope.user);
428 $scope.hold_data.pickup_lib = egCore.org.get(egCore.auth.user().ws_ou());
430 egCore.hatch.setLocalItem('eg.cat.request_items.honor_user_settings',n);
433 $scope.ok = function(h) {
436 hold_type : h.hold_type,
437 pickup_lib: h.pickup_lib.id(),
443 'open-ils.circ.holds.test_and_create.batch.override',
444 egCore.auth.token(), args,
445 h.hold_type == 'T' ? h.record_list : h.copy_list,
446 { 'all' : 1, 'honor_user_settings' : h.honor_user_settings }
448 console.log('request result',r);
449 if (isNaN(r.result)) {
450 if (typeof r.result.desc != 'undefined') {
451 ngToast.danger(r.result.desc);
453 if (typeof r.result.last_event != 'undefined') {
454 ngToast.danger(r.result.last_event.desc);
456 ngToast.danger(egCore.strings.FAILURE_HOLD_REQUEST);
460 ngToast.success(egCore.strings.SUCCESS_HOLD_REQUEST);
464 $uibModalInstance.close();
467 $scope.cancel = function($event) {
468 $uibModalInstance.dismiss();
469 $event.preventDefault();
475 service.attach_to_peer_bib = function(items) {
476 if (items.length == 0) return;
478 egCore.hatch.getItem('eg.cat.marked_conjoined_record').then(function(target_record) {
479 if (!target_record) return;
481 return $uibModal.open({
482 templateUrl: './cat/catalog/t_conjoined_selector',
486 ['$scope','$uibModalInstance',
487 function($scope , $uibModalInstance) {
488 $scope.update = false;
490 $scope.peer_type = null;
491 $scope.peer_type_list = [];
493 get_peer_types = function() {
495 return $q.when(egCore.env.bpt.list);
497 return egCore.pcrud.retrieveAll('bpt', null, {atomic : true})
498 .then(function(list) {
499 egCore.env.absorbList(list, 'bpt');
504 get_peer_types().then(function(list){
505 $scope.peer_type_list = list;
508 $scope.ok = function(type) {
511 angular.forEach(items, function (cp) {
512 var n = new egCore.idl.bpbcm();
514 n.peer_record(target_record);
515 n.target_copy(cp.id);
517 promises.push(egCore.pcrud.create(n).then(function(){service.add_barcode_to_list(cp.barcode)}));
520 return $q.all(promises).then(function(){$uibModalInstance.close()});
523 $scope.cancel = function($event) {
524 $uibModalInstance.dismiss();
525 $event.preventDefault();
532 service.selectedHoldingsCopyDelete = function (items) {
533 if (items.length == 0) return;
535 var copy_objects = [];
536 egCore.pcrud.search('acp',
537 {deleted : 'f', id : items.map(function(el){return el.id;}) },
538 { flesh : 1, flesh_fields : { acp : ['call_number'] } }
542 var perCnCopies = {};
552 var cn_id = cp.call_number().id();
553 if (!cnHash[cn_id]) {
554 cnHash[cn_id] = cp.call_number();
555 perCnCopies[cn_id] = [cp];
557 perCnCopies[cn_id].push(cp);
559 cp.call_number(cn_id); // prevent loops in JSON-ification
563 angular.forEach(perCnCopies, function (v, k) {
568 angular.forEach(cnHash, function (v, k) {
572 if (cnList.length == 0) return;
576 egConfirmDialog.open(
577 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES,
578 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES_MESSAGE,
579 {copies : cp_count, volumes : cn_count}
580 ).result.then(function() {
583 'open-ils.cat.asset.volume.fleshed.batch.update.override',
584 egCore.auth.token(), cnList, 1, flags
586 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
592 copy_objects.push(copy);
596 service.checkin = function (items) {
597 angular.forEach(items, function (cp) {
598 egCirc.checkin({copy_barcode:cp.barcode}).then(
599 function() { service.add_barcode_to_list(cp.barcode) }
604 service.renew = function (items) {
605 angular.forEach(items, function (cp) {
606 egCirc.renew({copy_barcode:cp.barcode}).then(
607 function() { service.add_barcode_to_list(cp.barcode) }
612 service.cancel_transit = function (items) {
613 angular.forEach(items, function(cp) {
614 egCirc.find_copy_transit(null, {copy_barcode:cp.barcode})
615 .then(function(t) { return egCirc.abort_transit(t.id()) })
616 .then(function() { return service.add_barcode_to_list(cp.barcode) });
620 service.selectedHoldingsDamaged = function (items) {
621 angular.forEach(items, function(cp) {
623 egCirc.mark_damaged({
632 service.selectedHoldingsDiscard = function (items) {
633 egCirc.mark_discard(items.map(function(el){return {id : el.id, barcode : el.barcode};})).then(function(){
634 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
638 service.selectedHoldingsMissing = function (items) {
639 egCirc.mark_missing(items.map(function(el){return {id : el.id, barcode : el.barcode};})).then(function(){
640 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
644 service.gatherSelectedRecordIds = function (items) {
649 if (rid_list.indexOf(item['call_number.record.id']) == -1)
650 rid_list.push(item['call_number.record.id'])
656 service.gatherSelectedVolumeIds = function (items,rid) {
661 if (rid && item['call_number.record.id'] != rid) return;
662 if (cn_id_list.indexOf(item['call_number.id']) == -1)
663 cn_id_list.push(item['call_number.id'])
669 service.gatherSelectedHoldingsIds = function (items,rid) {
674 if (rid && item['call_number.record.id'] != rid) return;
675 cp_id_list.push(item.id)
681 service.spawnHoldingsAdd = function (items,use_vols,use_copies){
682 angular.forEach(service.gatherSelectedRecordIds(items), function (r) {
684 if (use_copies) { // just a copy on existing volumes
685 angular.forEach(service.gatherSelectedVolumeIds(items,r), function (v) {
686 raw.push( {callnumber : v} );
688 } else if (use_vols) {
690 service.gatherSelectedHoldingsIds(items,r),
692 angular.forEach(items, function(item) {
693 if (i == item.id) raw.push({owner : item['call_number.owning_lib']});
699 if (raw.length == 0) raw.push({});
703 'open-ils.actor.anon_cache.set_value',
704 null, 'edit-these-copies', {
710 ).then(function(key) {
712 var url = egCore.env.basePath + 'cat/volcopy/' + key;
713 $timeout(function() { $window.open(url, '_blank') });
715 alert('Could not create anonymous cache key!');
721 service.spawnHoldingsEdit = function (items,hide_vols,hide_copies){
723 angular.forEach(items, function(i){
727 // provide record_id iff one record is selected.
728 // 0 disables record summary
729 var record_ids = service.gatherSelectedRecordIds(items);
730 var record_id = record_ids.length === 1 ? record_ids[0] : 0;
733 'open-ils.actor.anon_cache.set_value',
737 record_id: record_id,
740 hide_vols : hide_vols,
741 hide_copies : hide_copies
742 }).then(function(key) {
744 var url = egCore.env.basePath + 'cat/volcopy/' + key;
745 $timeout(function() { $window.open(url, '_blank') });
747 alert('Could not create anonymous cache key!');
752 service.replaceBarcodes = function(items) {
753 angular.forEach(items, function (cp) {
755 templateUrl: './cat/share/t_replace_barcode',
759 ['$scope','$uibModalInstance',
760 function($scope , $uibModalInstance) {
761 $scope.isModal = true;
762 $scope.focusBarcode = false;
763 $scope.focusBarcode2 = true;
764 $scope.barcode1 = cp.barcode;
766 $scope.updateBarcode = function() {
767 $scope.copyNotFound = false;
768 $scope.updateOK = false;
770 egCore.pcrud.search('acp',
771 {deleted : 'f', barcode : $scope.barcode1})
772 .then(function(copy) {
775 $scope.focusBarcode = true;
776 $scope.copyNotFound = true;
780 $scope.copyId = copy.id();
781 copy.barcode($scope.barcode2);
783 egCore.pcrud.update(copy).then(function(stat) {
784 $scope.updateOK = stat;
785 $scope.focusBarcode = true;
786 if (stat) service.add_barcode_to_list(copy.barcode());
790 $uibModalInstance.close();
793 $scope.cancel = function($event) {
794 $uibModalInstance.dismiss();
795 $event.preventDefault();
803 // this "transfers" selected copies to a new owning library,
804 // auto-creating volumes and deleting unused volumes as required.
805 service.changeItemOwningLib = function(items) {
806 var xfer_target = egCore.hatch.getLocalItem('eg.cat.transfer_target_lib');
807 if (!xfer_target || !items.length) {
810 var vols_to_move = {};
811 var copies_to_move = {};
812 angular.forEach(items, function(item) {
813 if (item['call_number.owning_lib'] != xfer_target) {
814 if (item['call_number.id'] in vols_to_move) {
815 copies_to_move[item['call_number.id']].push(item.id);
817 vols_to_move[item['call_number.id']] = {
818 label : item['call_number.label'],
819 label_class : item['call_number.label_class'],
820 record : item['call_number.record.id'],
821 prefix : item['call_number.prefix.id'],
822 suffix : item['call_number.suffix.id']
824 copies_to_move[item['call_number.id']] = new Array;
825 copies_to_move[item['call_number.id']].push(item.id);
831 angular.forEach(vols_to_move, function(vol, vol_id) {
832 promises.push(egCore.net.request(
834 'open-ils.cat.call_number.find_or_create',
842 ).then(function(resp) {
843 var evt = egCore.evt.parse(resp);
845 return egCore.net.request(
847 'open-ils.cat.transfer_copies_to_volume',
850 copies_to_move[vol_id]
858 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
863 service.transferItems = function (items){
864 var xfer_target = egCore.hatch.getLocalItem('eg.cat.transfer_target_vol');
865 var copy_ids = service.gatherSelectedHoldingsIds(items);
866 if (xfer_target && copy_ids.length > 0) {
869 'open-ils.cat.transfer_copies_to_volume',
874 function(resp) { // oncomplete
875 var evt = egCore.evt.parse(resp);
877 egConfirmDialog.open(
878 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_TITLE,
879 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_BODY,
881 ).result.then(function() {
884 'open-ils.cat.transfer_copies_to_volume.override',
888 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
891 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
894 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
904 service.mark_missing_pieces = function(copy,outer_scope) {
905 var b = copy.barcode();
906 var t = egCore.idl.toHash(copy.call_number()).record.title;
907 egConfirmDialog.open(
908 egCore.strings.CONFIRM_MARK_MISSING_TITLE,
909 egCore.strings.CONFIRM_MARK_MISSING_BODY,
910 { barcode : b, title : t }
911 ).result.then(function() {
913 // kick off mark missing
914 return egCore.net.request(
916 'open-ils.circ.mark_item_missing_pieces',
917 egCore.auth.token(), copy.id()
920 }).then(function(resp) {
921 var evt = egCore.evt.parse(resp); // should always produce event
923 if (evt.textcode == 'ACTION_CIRCULATION_NOT_FOUND') {
924 return egAlertDialog.open(
925 egCore.strings.CIRC_NOT_FOUND, {barcode : copy.barcode()});
928 var payload = evt.payload;
930 // TODO: open copy editor inline? new tab?
932 // print the missing pieces slip
933 var promise = $q.when();
935 // wait for completion, since it may spawn a confirm dialog
936 promise = egCore.print.print({
938 content_type : 'text/html',
939 content : payload.slip.template_output().data()
943 if (payload.letter) {
944 outer_scope.letter = payload.letter.template_output().data();
947 // apply patron penalty
949 promise.then(function() {
950 egCirc.create_penalty(payload.circ.usr())
957 service.print_spine_labels = function(copy_ids){
960 'open-ils.actor.anon_cache.set_value',
961 null, 'print-labels-these-copies', {
964 ).then(function(key) {
966 var url = egCore.env.basePath + 'cat/printlabels/' + key;
967 $timeout(function() { $window.open(url, '_blank') });
969 alert('Could not create anonymous cache key!');
974 service.show_in_catalog = function(copy_list){
975 angular.forEach(copy_list, function(copy){
976 window.open('/eg/staff/cat/catalog/record/'+copy['call_number.record.id']+'/catalog', '_blank')
982 .filter('string_pick', function() { return function(i){ return arguments[i] || ''; }; })