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;
102 return fetchCopy(barcode, id).then(function(res) {
103 return fetchCirc(copyData.copy).then(function(res) {
105 return fetchSummary(copyData.circ).then(function() {
116 // resolved with the last received copy
117 service.fetch = function(barcode, id, noListDupes) {
123 return service.retrieveCopyData(barcode, id)
124 .then(function(copyData) {
125 //Make sure we're getting a completed copyData - no plain acp or circ objects
127 // flesh circ_lib locally
128 copyData.circ.circ_lib(egCore.org.get(copyData.circ.circ_lib()));
129 copyData.circ.checkin_workstation(
130 egCore.org.get(copyData.circ.checkin_workstation()));
135 // use the existing copy if possible
136 flatCopy = service.copies.filter(
137 function(c) {return c.id == copyData.copy.id()})[0];
141 flatCopy = egCore.idl.toHash(copyData.copy, true);
144 flatCopy._circ = egCore.idl.toHash(copyData.circ, true);
145 flatCopy._circ_summary = egCore.idl.toHash(copyData.circ_summary, true);
146 flatCopy._circ_lib = copyData.circ.circ_lib();
147 flatCopy._duration = copyData.circ.duration();
149 flatCopy.index = service.index++;
150 service.copies.unshift(flatCopy);
153 //Get in-house use count
154 egCore.pcrud.search('aihu',
155 {item : flatCopy.id}, {}, {idlist : true, atomic : true})
156 .then(function(uses) {
157 flatCopy._inHouseUseCount = uses.length;
158 copyData.copy._inHouseUseCount = uses.length;
162 copy : copyData.copy,
163 index : flatCopy.index
170 service.add_copies_to_bucket = function(copy_list) {
171 if (copy_list.length == 0) return;
173 return $uibModal.open({
174 templateUrl: './cat/catalog/t_add_to_bucket',
178 ['$scope','$uibModalInstance',
179 function($scope , $uibModalInstance) {
181 $scope.bucket_id = 0;
182 $scope.newBucketName = '';
183 $scope.allBuckets = [];
187 'open-ils.actor.container.retrieve_by_class.authoritative',
188 egCore.auth.token(), egCore.auth.user().id(),
189 'copy', 'staff_client'
190 ).then(function(buckets) { $scope.allBuckets = buckets; });
192 $scope.add_to_bucket = function() {
194 angular.forEach(copy_list, function (cp) {
195 var item = new egCore.idl.ccbi()
196 item.bucket($scope.bucket_id);
197 item.target_copy(cp);
201 'open-ils.actor.container.item.create',
202 egCore.auth.token(), 'copy', item
206 return $q.all(promises).then(function() {
207 $uibModalInstance.close();
212 $scope.add_to_new_bucket = function() {
213 var bucket = new egCore.idl.ccb();
214 bucket.owner(egCore.auth.user().id());
215 bucket.name($scope.newBucketName);
216 bucket.description('');
217 bucket.btype('staff_client');
219 return egCore.net.request(
221 'open-ils.actor.container.create',
222 egCore.auth.token(), 'copy', bucket
223 ).then(function(bucket) {
224 $scope.bucket_id = bucket;
225 $scope.add_to_bucket();
229 $scope.cancel = function() {
230 $uibModalInstance.dismiss();
236 service.make_copies_bookable = function(items) {
238 var copies_by_record = {};
239 var record_list = [];
243 var record_id = item['call_number.record.id'];
244 if (typeof copies_by_record[ record_id ] == 'undefined') {
245 copies_by_record[ record_id ] = [];
246 record_list.push( record_id );
248 copies_by_record[ record_id ].push(item.id);
253 var combined_results = [];
254 angular.forEach(record_list, function(record_id) {
258 'open-ils.booking.resources.create_from_copies',
260 copies_by_record[record_id]
261 ).then(function(results) {
262 if (results && results['brsrc']) {
263 combined_results = combined_results.concat(results['brsrc']);
269 $q.all(promises).then(function() {
270 if (combined_results.length > 0) {
272 template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
276 ['$scope','$location','egCore','$uibModalInstance',
277 function($scope , $location , egCore , $uibModalInstance) {
280 ses : egCore.auth.token(),
281 resultant_brsrc : combined_results.map(function(o) { return o[0]; })
284 var booking_path = '/eg/conify/global/booking/resource';
286 $scope.booking_admin_url =
287 $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
294 service.book_copies_now = function(items) {
295 var copies_by_record = {};
296 var record_list = [];
300 var record_id = item['call_number.record.id'];
301 if (typeof copies_by_record[ record_id ] == 'undefined') {
302 copies_by_record[ record_id ] = [];
303 record_list.push( record_id );
305 copies_by_record[ record_id ].push(item.id);
310 var combined_brt = [];
311 var combined_brsrc = [];
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['brt']) {
321 combined_brt = combined_brt.concat(results['brt']);
323 if (results && results['brsrc']) {
324 combined_brsrc = combined_brsrc.concat(results['brsrc']);
330 $q.all(promises).then(function() {
331 if (combined_brt.length > 0 || combined_brsrc.length > 0) {
333 template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
337 ['$scope','$location','egCore','$uibModalInstance',
338 function($scope , $location , egCore , $uibModalInstance) {
341 ses : egCore.auth.token(),
342 bresv_interface_opts : {
345 ,brsrc : combined_brsrc
350 var booking_path = '/eg/booking/reservation';
352 $scope.booking_admin_url =
353 $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
361 service.requestItems = function(copy_list) {
362 if (copy_list.length == 0) return;
364 return $uibModal.open({
365 templateUrl: './cat/catalog/t_request_items',
368 ['$scope','$uibModalInstance','egUser',
369 function($scope , $uibModalInstance , egUser) {
371 $scope.first_user_fetch = true;
375 copy_list : copy_list,
376 pickup_lib: egCore.org.get(egCore.auth.user().ws_ou()),
377 user : egCore.auth.user().id()
380 egUser.get( $scope.hold_data.user ).then(function(u) {
382 $scope.barcode = u.card().barcode();
383 $scope.user_name = egUser.format_name(u);
384 $scope.hold_data.user = u.id();
387 $scope.user_name = '';
389 $scope.$watch('barcode', function (n) {
390 if (!$scope.first_user_fetch) {
391 egUser.getByBarcode(n).then(function(u) {
393 $scope.user_name = egUser.format_name(u);
394 $scope.hold_data.user = u.id();
397 $scope.user_name = '';
398 delete $scope.hold_data.user;
401 $scope.first_user_fetch = false;
404 $scope.ok = function(h) {
407 hold_type : h.hold_type,
408 pickup_lib: h.pickup_lib.id(),
414 'open-ils.circ.holds.test_and_create.batch.override',
415 egCore.auth.token(), args, h.copy_list
418 $uibModalInstance.close();
421 $scope.cancel = function($event) {
422 $uibModalInstance.dismiss();
423 $event.preventDefault();
429 service.attach_to_peer_bib = function(items) {
430 if (items.length == 0) return;
432 egCore.hatch.getItem('eg.cat.marked_conjoined_record').then(function(target_record) {
433 if (!target_record) return;
435 return $uibModal.open({
436 templateUrl: './cat/catalog/t_conjoined_selector',
439 ['$scope','$uibModalInstance',
440 function($scope , $uibModalInstance) {
441 $scope.update = false;
443 $scope.peer_type = null;
444 $scope.peer_type_list = [];
446 get_peer_types = function() {
448 return $q.when(egCore.env.bpt.list);
450 return egCore.pcrud.retrieveAll('bpt', null, {atomic : true})
451 .then(function(list) {
452 egCore.env.absorbList(list, 'bpt');
457 get_peer_types().then(function(list){
458 $scope.peer_type_list = list;
461 $scope.ok = function(type) {
464 angular.forEach(items, function (cp) {
465 var n = new egCore.idl.bpbcm();
467 n.peer_record(target_record);
468 n.target_copy(cp.id);
470 promises.push(egCore.pcrud.create(n).then(function(){service.add_barcode_to_list(cp.barcode)}));
473 return $q.all(promises).then(function(){$uibModalInstance.close()});
476 $scope.cancel = function($event) {
477 $uibModalInstance.dismiss();
478 $event.preventDefault();
485 service.selectedHoldingsCopyDelete = function (items) {
486 if (items.length == 0) return;
488 var copy_objects = [];
489 egCore.pcrud.search('acp',
490 {deleted : 'f', id : items.map(function(el){return el.id;}) },
491 { flesh : 1, flesh_fields : { acp : ['call_number'] } }
492 ).then(function(copy) {
493 copy_objects.push(copy);
497 var perCnCopies = {};
507 var cn_id = cp.call_number().id();
508 if (!cnHash[cn_id]) {
509 cnHash[cn_id] = cp.call_number();
510 perCnCopies[cn_id] = [cp];
512 perCnCopies[cn_id].push(cp);
514 cp.call_number(cn_id); // prevent loops in JSON-ification
518 angular.forEach(perCnCopies, function (v, k) {
523 angular.forEach(cnHash, function (v, k) {
527 if (cnList.length == 0) return;
531 egConfirmDialog.open(
532 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES,
533 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES_MESSAGE,
534 {copies : cp_count, volumes : cn_count}
535 ).result.then(function() {
538 'open-ils.cat.asset.volume.fleshed.batch.update.override',
539 egCore.auth.token(), cnList, 1, flags
541 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
547 service.checkin = function (items) {
548 angular.forEach(items, function (cp) {
549 egCirc.checkin({copy_barcode:cp.barcode}).then(
550 function() { service.add_barcode_to_list(cp.barcode) }
555 service.renew = function (items) {
556 angular.forEach(items, function (cp) {
557 egCirc.renew({copy_barcode:cp.barcode}).then(
558 function() { service.add_barcode_to_list(cp.barcode) }
563 service.cancel_transit = function (items) {
564 angular.forEach(items, function(cp) {
565 egCirc.find_copy_transit(null, {copy_barcode:cp.barcode})
566 .then(function(t) { return egCirc.abort_transit(t.id()) })
567 .then(function() { return service.add_barcode_to_list(cp.barcode) });
571 service.selectedHoldingsDamaged = function (items) {
572 egCirc.mark_damaged(items.map(function(el){return el.id;})).then(function(){
573 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
577 service.selectedHoldingsMissing = function (items) {
578 egCirc.mark_missing(items.map(function(el){return el.id;})).then(function(){
579 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
583 service.gatherSelectedRecordIds = function (items) {
588 if (rid_list.indexOf(item['call_number.record.id']) == -1)
589 rid_list.push(item['call_number.record.id'])
595 service.gatherSelectedVolumeIds = function (items,rid) {
600 if (rid && item['call_number.record.id'] != rid) return;
601 if (cn_id_list.indexOf(item['call_number.id']) == -1)
602 cn_id_list.push(item['call_number.id'])
608 service.gatherSelectedHoldingsIds = function (items,rid) {
613 if (rid && item['call_number.record.id'] != rid) return;
614 cp_id_list.push(item.id)
620 service.spawnHoldingsAdd = function (items,use_vols,use_copies){
621 angular.forEach(service.gatherSelectedRecordIds(items), function (r) {
623 if (use_copies) { // just a copy on existing volumes
624 angular.forEach(service.gatherSelectedVolumeIds(items,r), function (v) {
625 raw.push( {callnumber : v} );
627 } else if (use_vols) {
629 service.gatherSelectedHoldingsIds(items,r),
631 angular.forEach(items, function(item) {
632 if (i == item.id) raw.push({owner : item['call_number.owning_lib']});
638 if (raw.length == 0) raw.push({});
642 'open-ils.actor.anon_cache.set_value',
643 null, 'edit-these-copies', {
649 ).then(function(key) {
651 var url = egCore.env.basePath + 'cat/volcopy/' + key;
652 $timeout(function() { $window.open(url, '_blank') });
654 alert('Could not create anonymous cache key!');
660 service.spawnHoldingsEdit = function (items,hide_vols,hide_copies){
661 angular.forEach(service.gatherSelectedRecordIds(items), function (r) {
664 'open-ils.actor.anon_cache.set_value',
665 null, 'edit-these-copies', {
667 copies: service.gatherSelectedHoldingsIds(items,r),
669 hide_vols : hide_vols,
670 hide_copies : hide_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.replaceBarcodes = function(items) {
684 angular.forEach(items, function (cp) {
686 templateUrl: './cat/share/t_replace_barcode',
689 ['$scope','$uibModalInstance',
690 function($scope , $uibModalInstance) {
691 $scope.isModal = true;
692 $scope.focusBarcode = false;
693 $scope.focusBarcode2 = true;
694 $scope.barcode1 = cp.barcode;
696 $scope.updateBarcode = function() {
697 $scope.copyNotFound = false;
698 $scope.updateOK = false;
700 egCore.pcrud.search('acp',
701 {deleted : 'f', barcode : $scope.barcode1})
702 .then(function(copy) {
705 $scope.focusBarcode = true;
706 $scope.copyNotFound = true;
710 $scope.copyId = copy.id();
711 copy.barcode($scope.barcode2);
713 egCore.pcrud.update(copy).then(function(stat) {
714 $scope.updateOK = stat;
715 $scope.focusBarcode = true;
716 if (stat) service.add_barcode_to_list(copy.barcode());
720 $uibModalInstance.close();
723 $scope.cancel = function($event) {
724 $uibModalInstance.dismiss();
725 $event.preventDefault();
733 // this "transfers" selected copies to a new owning library,
734 // auto-creating volumes and deleting unused volumes as required.
735 service.changeItemOwningLib = function(items) {
736 var xfer_target = egCore.hatch.getLocalItem('eg.cat.volume_transfer_target');
737 if (!xfer_target || !items.length) {
740 var vols_to_move = {};
741 var copies_to_move = {};
742 angular.forEach(items, function(item) {
743 if (item['call_number.owning_lib'] != xfer_target) {
744 if (item['call_number.id'] in vols_to_move) {
745 copies_to_move[item['call_number.id']].push(item.id);
747 vols_to_move[item['call_number.id']] = {
748 label : item['call_number.label'],
749 label_class : item['call_number.label_class'],
750 record : item['call_number.record.id'],
751 prefix : item['call_number.prefix.id'],
752 suffix : item['call_number.suffix.id']
754 copies_to_move[item['call_number.id']] = new Array;
755 copies_to_move[item['call_number.id']].push(item.id);
761 angular.forEach(vols_to_move, function(vol) {
762 promises.push(egCore.net.request(
764 'open-ils.cat.call_number.find_or_create',
772 ).then(function(resp) {
773 var evt = egCore.evt.parse(resp);
775 return egCore.net.request(
777 'open-ils.cat.transfer_copies_to_volume',
780 copies_to_move[vol.id]
789 function(){ service.add_barcode_to_list(cp.barcode) }
796 service.transferItems = function (items){
797 var xfer_target = egCore.hatch.getLocalItem('eg.cat.item_transfer_target');
798 var copy_ids = service.gatherSelectedHoldingsIds(items);
799 if (xfer_target && copy_ids.length > 0) {
802 'open-ils.cat.transfer_copies_to_volume',
807 function(resp) { // oncomplete
808 var evt = egCore.evt.parse(resp);
810 egConfirmDialog.open(
811 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_TITLE,
812 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_BODY,
814 ).result.then(function() {
817 'open-ils.cat.transfer_copies_to_volume.override',
821 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
824 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
827 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
837 service.mark_missing_pieces = function(copy) {
838 var b = copy.barcode();
839 var t = egCore.idl.toHash(copy.call_number()).record.title;
840 egConfirmDialog.open(
841 egCore.strings.CONFIRM_MARK_MISSING_TITLE,
842 egCore.strings.CONFIRM_MARK_MISSING_BODY,
843 { barcode : b, title : t }
844 ).result.then(function() {
846 // kick off mark missing
847 return egCore.net.request(
849 'open-ils.circ.mark_item_missing_pieces',
850 egCore.auth.token(), copy.id()
853 }).then(function(resp) {
854 var evt = egCore.evt.parse(resp); // should always produce event
856 if (evt.textcode == 'ACTION_CIRCULATION_NOT_FOUND') {
857 return egAlertDialog.open(
858 egCore.strings.CIRC_NOT_FOUND, {barcode : copy.barcode()});
861 var payload = evt.payload;
863 // TODO: open copy editor inline? new tab?
865 // print the missing pieces slip
866 var promise = $q.when();
868 // wait for completion, since it may spawn a confirm dialog
869 promise = egCore.print.print({
871 content_type : 'text/html',
872 content : payload.slip.template_output().data()
876 if (payload.letter) {
877 $scope.letter = payload.letter.template_output().data();
880 // apply patron penalty
882 promise.then(function() {
883 egCirc.create_penalty(payload.circ.usr())
890 service.print_spine_labels = function(copies){
893 'open-ils.actor.anon_cache.set_value',
894 null, 'print-labels-these-copies', {
895 copies : service.gatherSelectedHoldingsIds(copies)
897 ).then(function(key) {
899 var url = egCore.env.basePath + 'cat/printlabels/' + key;
900 $timeout(function() { $window.open(url, '_blank') });
902 alert('Could not create anonymous cache key!');