5 angular.module('egItemStatus',
6 ['ngRoute', 'ui.bootstrap', 'egCoreMod', 'egUiMod', 'egGridMod', 'egUserMod'])
8 .filter('boolText', function(){
14 .config(function($routeProvider, $locationProvider, $compileProvider) {
15 $locationProvider.html5Mode(true);
16 $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/); // grid export
18 var resolver = {delay : function(egStartup) {return egStartup.go()}};
20 // search page shows the list view by default
21 $routeProvider.when('/cat/item/search', {
22 templateUrl: './cat/item/t_list',
23 controller: 'ListCtrl',
27 // search page shows the list view by default
28 $routeProvider.when('/cat/item/search/:idList', {
29 templateUrl: './cat/item/t_list',
30 controller: 'ListCtrl',
34 $routeProvider.when('/cat/item/:id', {
35 templateUrl: './cat/item/t_view',
36 controller: 'ViewCtrl',
40 $routeProvider.when('/cat/item/:id/:tab', {
41 templateUrl: './cat/item/t_view',
42 controller: 'ViewCtrl',
46 // default page / bucket view
47 $routeProvider.otherwise({redirectTo : '/cat/item/search'});
55 copies : [], // copy barcode search results
56 index : 0 // search grid index
62 acp : ['call_number','location','status','location','floating','circ_modifier','age_protect'],
63 acn : ['record','prefix','suffix'],
64 bre : ['simple_record','creator','editor']
67 // avoid fleshing MARC on the bre
68 // note: don't add simple_record.. not sure why
69 bre : ['id','tcn_value','creator','editor'],
73 // resolved with the last received copy
74 service.fetch = function(barcode, id, noListDupes) {
78 promise = egCore.pcrud.search('acp',
79 {barcode : barcode, deleted : 'f'}, service.flesh);
81 promise = egCore.pcrud.retrieve('acp', id, service.flesh);
86 function() {return lastRes},
89 // notify reads the stream of copies, one at a time.
94 // use the existing copy if possible
95 flatCopy = service.copies.filter(
96 function(c) {return c.id == copy.id()})[0];
100 flatCopy = egCore.idl.toHash(copy, true);
101 flatCopy.index = service.index++;
102 service.copies.unshift(flatCopy);
107 index : flatCopy.index
117 * Search bar along the top of the page.
118 * Parent scope for list and detail views
120 .controller('SearchCtrl',
121 ['$scope','$location','egCore','egGridDataProvider','itemSvc',
122 function($scope , $location , egCore , egGridDataProvider , itemSvc) {
123 $scope.args = {}; // search args
125 // sub-scopes (search / detail-view) apply their version
126 // of retrieval function to $scope.context.search
127 // and display toggling via $scope.context.toggleDisplay
132 $scope.toggleView = function($event) {
133 $scope.context.toggleDisplay();
134 $event.preventDefault(); // avoid form submission
139 * List view - grid stuff
141 .controller('ListCtrl',
142 ['$scope','$q','$routeParams','$location','$timeout','$window','egCore','egGridDataProvider','itemSvc','egUser','$uibModal','egCirc','egConfirmDialog',
143 function($scope , $q , $routeParams , $location , $timeout , $window , egCore , egGridDataProvider , itemSvc , egUser , $uibModal , egCirc , egConfirmDialog) {
145 var cp_list = $routeParams.idList;
147 copyId = cp_list.split(',');
150 $scope.context.page = 'list';
153 var provider = egGridDataProvider.instance();
154 provider.get = function(offset, count) {
158 $scope.gridDataProvider = egGridDataProvider.instance({
159 get : function(offset, count) {
160 //return provider.arrayNotifier(itemSvc.copies, offset, count);
161 return this.arrayNotifier(itemSvc.copies, offset, count);
165 // If a copy was just displayed in the detail view, ensure it's
166 // focused in the list view.
167 var selected = false;
168 var copyGrid = $scope.gridControls = {
169 itemRetrieved : function(item) {
170 if (selected || !itemSvc.copy) return;
171 if (itemSvc.copy.id() == item.id) {
172 copyGrid.selectItems([item.index]);
178 $scope.$watch('barcodesFromFile', function(newVal, oldVal) {
179 if (newVal && newVal != oldVal) {
180 $scope.args.barcode = '';
183 angular.forEach(newVal.split(/\n/), function(line) {
185 // scrub any trailing spaces or commas from the barcode
186 line = line.replace(/(.*?)($|\s.*|,.*)/,'$1');
190 itemSvc.fetch(barcodes).then(
193 copyGrid.selectItems([itemSvc.copies[0].index]);
199 $scope.context.search = function(args) {
200 if (!args.barcode) return;
201 $scope.context.itemNotFound = false;
202 itemSvc.fetch(args.barcode).then(function(res) {
205 copyGrid.selectItems([res.index]);
206 $scope.args.barcode = '';
208 $scope.context.itemNotFound = true;
210 $scope.context.selectBarcode = true;
214 var add_barcode_to_list = function (b) {
215 $scope.context.search({barcode:b});
218 $scope.context.toggleDisplay = function() {
219 var item = copyGrid.selectedItems()[0];
221 $location.path('/cat/item/' + item.id);
224 $scope.context.show_triggered_events = function() {
225 var item = copyGrid.selectedItems()[0];
227 $location.path('/cat/item/' + item.id + '/triggered_events');
230 function gatherSelectedRecordIds () {
233 copyGrid.selectedItems(),
235 if (rid_list.indexOf(item['call_number.record.id']) == -1)
236 rid_list.push(item['call_number.record.id'])
242 function gatherSelectedVolumeIds (rid) {
245 copyGrid.selectedItems(),
247 if (rid && item['call_number.record.id'] != rid) return;
248 if (cn_id_list.indexOf(item['call_number.id']) == -1)
249 cn_id_list.push(item['call_number.id'])
255 function gatherSelectedHoldingsIds (rid) {
258 copyGrid.selectedItems(),
260 if (rid && item['call_number.record.id'] != rid) return;
261 cp_id_list.push(item.id)
267 $scope.add_copies_to_bucket = function() {
268 var copy_list = gatherSelectedHoldingsIds();
269 if (copy_list.length == 0) return;
271 return $uibModal.open({
272 templateUrl: './cat/catalog/t_add_to_bucket',
276 ['$scope','$uibModalInstance',
277 function($scope , $uibModalInstance) {
279 $scope.bucket_id = 0;
280 $scope.newBucketName = '';
281 $scope.allBuckets = [];
285 'open-ils.actor.container.retrieve_by_class.authoritative',
286 egCore.auth.token(), egCore.auth.user().id(),
287 'copy', 'staff_client'
288 ).then(function(buckets) { $scope.allBuckets = buckets; });
290 $scope.add_to_bucket = function() {
292 angular.forEach(copy_list, function (cp) {
293 var item = new egCore.idl.ccbi()
294 item.bucket($scope.bucket_id);
295 item.target_copy(cp);
299 'open-ils.actor.container.item.create',
300 egCore.auth.token(), 'copy', item
304 return $q.all(promises).then(function() {
305 $uibModalInstance.close();
310 $scope.add_to_new_bucket = function() {
311 var bucket = new egCore.idl.ccb();
312 bucket.owner(egCore.auth.user().id());
313 bucket.name($scope.newBucketName);
314 bucket.description('');
315 bucket.btype('staff_client');
317 return egCore.net.request(
319 'open-ils.actor.container.create',
320 egCore.auth.token(), 'copy', bucket
321 ).then(function(bucket) {
322 $scope.bucket_id = bucket;
323 $scope.add_to_bucket();
327 $scope.cancel = function() {
328 $uibModalInstance.dismiss();
334 $scope.need_one_selected = function() {
335 var items = $scope.gridControls.selectedItems();
336 if (items.length == 1) return false;
340 $scope.make_copies_bookable = function() {
342 var copies_by_record = {};
343 var record_list = [];
345 copyGrid.selectedItems(),
347 var record_id = item['call_number.record.id'];
348 if (typeof copies_by_record[ record_id ] == 'undefined') {
349 copies_by_record[ record_id ] = [];
350 record_list.push( record_id );
352 copies_by_record[ record_id ].push(item.id);
357 var combined_results = [];
358 angular.forEach(record_list, function(record_id) {
362 'open-ils.booking.resources.create_from_copies',
364 copies_by_record[record_id]
365 ).then(function(results) {
366 if (results && results['brsrc']) {
367 combined_results = combined_results.concat(results['brsrc']);
373 $q.all(promises).then(function() {
374 if (combined_results.length > 0) {
376 template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
380 ['$scope','$location','egCore','$uibModalInstance',
381 function($scope , $location , egCore , $uibModalInstance) {
384 ses : egCore.auth.token(),
385 resultant_brsrc : combined_results.map(function(o) { return o[0]; })
388 var booking_path = '/eg/conify/global/booking/resource';
390 $scope.booking_admin_url =
391 $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
398 $scope.book_copies_now = function() {
399 var copies_by_record = {};
400 var record_list = [];
402 copyGrid.selectedItems(),
404 var record_id = item['call_number.record.id'];
405 if (typeof copies_by_record[ record_id ] == 'undefined') {
406 copies_by_record[ record_id ] = [];
407 record_list.push( record_id );
409 copies_by_record[ record_id ].push(item.id);
414 var combined_brt = [];
415 var combined_brsrc = [];
416 angular.forEach(record_list, function(record_id) {
420 'open-ils.booking.resources.create_from_copies',
422 copies_by_record[record_id]
423 ).then(function(results) {
424 if (results && results['brt']) {
425 combined_brt = combined_brt.concat(results['brt']);
427 if (results && results['brsrc']) {
428 combined_brsrc = combined_brsrc.concat(results['brsrc']);
434 $q.all(promises).then(function() {
435 if (combined_brt.length > 0 || combined_brsrc.length > 0) {
437 template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
441 ['$scope','$location','egCore','$uibModalInstance',
442 function($scope , $location , egCore , $uibModalInstance) {
445 ses : egCore.auth.token(),
446 bresv_interface_opts : {
449 ,brsrc : combined_brsrc
454 var booking_path = '/eg/booking/reservation';
456 $scope.booking_admin_url =
457 $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
465 $scope.requestItems = function() {
466 var copy_list = gatherSelectedHoldingsIds();
467 if (copy_list.length == 0) return;
469 return $uibModal.open({
470 templateUrl: './cat/catalog/t_request_items',
473 ['$scope','$uibModalInstance','egUser',
474 function($scope , $uibModalInstance , egUser) {
476 $scope.first_user_fetch = true;
480 copy_list : copy_list,
481 pickup_lib: egCore.org.get(egCore.auth.user().ws_ou()),
482 user : egCore.auth.user().id()
485 egUser.get( $scope.hold_data.user ).then(function(u) {
487 $scope.barcode = u.card().barcode();
488 $scope.user_name = egUser.format_name(u);
489 $scope.hold_data.user = u.id();
492 $scope.user_name = '';
494 $scope.$watch('barcode', function (n) {
495 if (!$scope.first_user_fetch) {
496 egUser.getByBarcode(n).then(function(u) {
498 $scope.user_name = egUser.format_name(u);
499 $scope.hold_data.user = u.id();
502 $scope.user_name = '';
503 delete $scope.hold_data.user;
506 $scope.first_user_fetch = false;
509 $scope.ok = function(h) {
512 hold_type : h.hold_type,
513 pickup_lib: h.pickup_lib.id(),
519 'open-ils.circ.holds.test_and_create.batch.override',
520 egCore.auth.token(), args, h.copy_list
523 $uibModalInstance.close();
526 $scope.cancel = function($event) {
527 $uibModalInstance.dismiss();
528 $event.preventDefault();
534 $scope.replaceBarcodes = function() {
535 angular.forEach(copyGrid.selectedItems(), function (cp) {
537 templateUrl: './cat/share/t_replace_barcode',
540 ['$scope','$uibModalInstance',
541 function($scope , $uibModalInstance) {
542 $scope.isModal = true;
543 $scope.focusBarcode = false;
544 $scope.focusBarcode2 = true;
545 $scope.barcode1 = cp.barcode;
547 $scope.updateBarcode = function() {
548 $scope.copyNotFound = false;
549 $scope.updateOK = false;
551 egCore.pcrud.search('acp',
552 {deleted : 'f', barcode : $scope.barcode1})
553 .then(function(copy) {
556 $scope.focusBarcode = true;
557 $scope.copyNotFound = true;
561 $scope.copyId = copy.id();
562 copy.barcode($scope.barcode2);
564 egCore.pcrud.update(copy).then(function(stat) {
565 $scope.updateOK = stat;
566 $scope.focusBarcode = true;
567 if (stat) add_barcode_to_list(copy.barcode());
571 $uibModalInstance.close();
574 $scope.cancel = function($event) {
575 $uibModalInstance.dismiss();
576 $event.preventDefault();
584 $scope.attach_to_peer_bib = function() {
585 if (copyGrid.selectedItems().length == 0) return;
587 egCore.hatch.getItem('eg.cat.marked_conjoined_record').then(function(target_record) {
588 if (!target_record) return;
590 return $uibModal.open({
591 templateUrl: './cat/catalog/t_conjoined_selector',
594 ['$scope','$uibModalInstance',
595 function($scope , $uibModalInstance) {
596 $scope.update = false;
598 $scope.peer_type = null;
599 $scope.peer_type_list = [];
601 get_peer_types = function() {
603 return $q.when(egCore.env.bpt.list);
605 return egCore.pcrud.retrieveAll('bpt', null, {atomic : true})
606 .then(function(list) {
607 egCore.env.absorbList(list, 'bpt');
612 get_peer_types().then(function(list){
613 $scope.peer_type_list = list;
616 $scope.ok = function(type) {
619 angular.forEach(copyGrid.selectedItems(), function (cp) {
620 var n = new egCore.idl.bpbcm();
622 n.peer_record(target_record);
623 n.target_copy(cp.id);
625 promises.push(egCore.pcrud.create(n).then(function(){add_barcode_to_list(cp.barcode)}));
628 return $q.all(promises).then(function(){$uibModalInstance.close()});
631 $scope.cancel = function($event) {
632 $uibModalInstance.dismiss();
633 $event.preventDefault();
640 $scope.selectedHoldingsCopyDelete = function () {
641 var copy_list = gatherSelectedHoldingsIds();
642 if (copy_list.length == 0) return;
644 var copy_objects = [];
645 egCore.pcrud.search('acp',
646 {deleted : 'f', id : copy_list},
647 { flesh : 1, flesh_fields : { acp : ['call_number'] } }
648 ).then(function(copy) {
649 copy_objects.push(copy);
653 var perCnCopies = {};
663 var cn_id = cp.call_number().id();
664 if (!cnHash[cn_id]) {
665 cnHash[cn_id] = cp.call_number();
666 perCnCopies[cn_id] = [cp];
668 perCnCopies[cn_id].push(cp);
670 cp.call_number(cn_id); // prevent loops in JSON-ification
674 angular.forEach(perCnCopies, function (v, k) {
679 angular.forEach(cnHash, function (v, k) {
683 if (cnList.length == 0) return;
687 egConfirmDialog.open(
688 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES,
689 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES_MESSAGE,
690 {copies : cp_count, volumes : cn_count}
691 ).result.then(function() {
694 'open-ils.cat.asset.volume.fleshed.batch.update.override',
695 egCore.auth.token(), cnList, 1, flags
697 angular.forEach(copyGrid.selectedItems(), function(cp){add_barcode_to_list(cp.barcode)});
703 $scope.selectedHoldingsItemStatusTgrEvt= function() {
704 var item = copyGrid.selectedItems()[0];
706 $location.path('/cat/item/' + item.id + '/triggered_events');
709 $scope.selectedHoldingsItemStatusHolds= function() {
710 var item = copyGrid.selectedItems()[0];
712 $location.path('/cat/item/' + item.id + '/holds');
715 $scope.cancel_transit = function () {
716 var initial_list = copyGrid.selectedItems();
717 angular.forEach(copyGrid.selectedItems(), function(cp) {
718 egCirc.find_copy_transit(null, {copy_barcode:cp.barcode})
719 .then(function(t) { return egCirc.abort_transit(t.id()) })
720 .then(function() { return add_barcode_to_list(cp.barcode) });
724 $scope.selectedHoldingsDamaged = function () {
725 var initial_list = copyGrid.selectedItems();
726 egCirc.mark_damaged(gatherSelectedHoldingsIds()).then(function(){
727 angular.forEach(initial_list, function(cp){add_barcode_to_list(cp.barcode)});
731 $scope.selectedHoldingsMissing = function () {
732 var initial_list = copyGrid.selectedItems();
733 egCirc.mark_missing(gatherSelectedHoldingsIds()).then(function(){
734 angular.forEach(initial_list, function(cp){add_barcode_to_list(cp.barcode)});
738 $scope.checkin = function () {
739 angular.forEach(copyGrid.selectedItems(), function (cp) {
740 egCirc.checkin({copy_barcode:cp.barcode}).then(
741 function() { add_barcode_to_list(cp.barcode) }
746 $scope.renew = function () {
747 angular.forEach(copyGrid.selectedItems(), function (cp) {
748 egCirc.renew({copy_barcode:cp.barcode}).then(
749 function() { add_barcode_to_list(cp.barcode) }
755 var spawnHoldingsAdd = function (vols,copies){
756 angular.forEach(gatherSelectedRecordIds(), function (r) {
758 if (copies) { // just a copy on existing volumes
759 angular.forEach(gatherSelectedVolumeIds(r), function (v) {
760 raw.push( {callnumber : v} );
764 gatherSelectedHoldingsIds(r),
766 angular.forEach(copyGrid.selectedItems(), function(item) {
767 if (i == item.id) raw.push({owner : item['call_number.owning_lib']});
773 if (raw.length == 0) raw.push({});
777 'open-ils.actor.anon_cache.set_value',
778 null, 'edit-these-copies', {
784 ).then(function(key) {
786 var url = egCore.env.basePath + 'cat/volcopy/' + key;
787 $timeout(function() { $window.open(url, '_blank') });
789 alert('Could not create anonymous cache key!');
794 $scope.selectedHoldingsVolCopyAdd = function () { spawnHoldingsAdd(true,false) }
795 $scope.selectedHoldingsCopyAdd = function () { spawnHoldingsAdd(false,true) }
797 $scope.showBibHolds = function () {
798 angular.forEach(gatherSelectedRecordIds(), function (r) {
799 var url = egCore.env.basePath + 'cat/catalog/record/' + r + '/holds';
800 $timeout(function() { $window.open(url, '_blank') });
804 var spawnHoldingsEdit = function (hide_vols,hide_copies){
805 angular.forEach(gatherSelectedRecordIds(), function (r) {
808 'open-ils.actor.anon_cache.set_value',
809 null, 'edit-these-copies', {
811 copies: gatherSelectedHoldingsIds(r),
813 hide_vols : hide_vols,
814 hide_copies : hide_copies
816 ).then(function(key) {
818 var url = egCore.env.basePath + 'cat/volcopy/' + key;
819 $timeout(function() { $window.open(url, '_blank') });
821 alert('Could not create anonymous cache key!');
826 $scope.selectedHoldingsVolCopyEdit = function () { spawnHoldingsEdit(false,false) }
827 $scope.selectedHoldingsVolEdit = function () { spawnHoldingsEdit(false,true) }
828 $scope.selectedHoldingsCopyEdit = function () { spawnHoldingsEdit(true,false) }
830 // this "transfers" selected copies to a new owning library,
831 // auto-creating volumes and deleting unused volumes as required.
832 $scope.changeItemOwningLib = function() {
833 var xfer_target = egCore.hatch.getLocalItem('eg.cat.volume_transfer_target');
834 var items = copyGrid.selectedItems();
835 if (!xfer_target || !items.length) {
838 var vols_to_move = {};
839 var copies_to_move = {};
840 angular.forEach(items, function(item) {
841 if (item['call_number.owning_lib'] != xfer_target) {
842 if (item['call_number.id'] in vols_to_move) {
843 copies_to_move[item['call_number.id']].push(item.id);
845 vols_to_move[item['call_number.id']] = {
846 label : item['call_number.label'],
847 label_class : item['call_number.label_class'],
848 record : item['call_number.record.id'],
849 prefix : item['call_number.prefix.id'],
850 suffix : item['call_number.suffix.id']
852 copies_to_move[item['call_number.id']] = new Array;
853 copies_to_move[item['call_number.id']].push(item.id);
859 angular.forEach(vols_to_move, function(vol) {
860 promises.push(egCore.net.request(
862 'open-ils.cat.call_number.find_or_create',
870 ).then(function(resp) {
871 var evt = egCore.evt.parse(resp);
873 return egCore.net.request(
875 'open-ils.cat.transfer_copies_to_volume',
878 copies_to_move[vol.id]
884 copyGrid.selectedItems(),
887 function(){ add_barcode_to_list(cp.barcode) }
894 $scope.transferItems = function (){
895 var xfer_target = egCore.hatch.getLocalItem('eg.cat.item_transfer_target');
896 var copy_ids = gatherSelectedHoldingsIds();
897 if (xfer_target && copy_ids.length > 0) {
900 'open-ils.cat.transfer_copies_to_volume',
905 function(resp) { // oncomplete
906 var evt = egCore.evt.parse(resp);
907 egConfirmDialog.open(
908 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_TITLE,
909 egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_BODY,
910 {'evt_desc': evt.desc}
911 ).result.then(function() {
914 'open-ils.cat.transfer_copies_to_volume.override',
918 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
925 angular.forEach(copyGrid.selectedItems(), function(cp){add_barcode_to_list(cp.barcode)});
930 $scope.print_list = function() {
931 var print_data = { copies : copyGrid.allItems() };
933 if (print_data.copies.length == 0) return $q.when();
935 return egCore.print.print({
936 template : 'item_status',
941 if (copyId.length > 0) {
942 itemSvc.fetch(null,copyId).then(
952 * Detail view -- shows one copy
954 .controller('ViewCtrl',
955 ['$scope','$q','$location','$routeParams','$timeout','$window','egCore','itemSvc','egBilling',
956 function($scope , $q , $location , $routeParams , $timeout , $window , egCore , itemSvc , egBilling) {
957 var copyId = $routeParams.id;
958 $scope.tab = $routeParams.tab || 'summary';
959 $scope.context.page = 'detail';
960 $scope.summaryRecord = null;
963 if ($scope.tab == 'edit') {
964 $scope.tab = 'summary';
969 // use the cached record info
971 $scope.recordId = itemSvc.copy.call_number().record().id();
973 function loadCopy(barcode) {
974 $scope.context.itemNotFound = false;
976 // Avoid re-fetching the same copy while jumping tabs.
977 // In addition to being quicker, this helps to avoid flickering
978 // of the top panel which is always visible in the detail view.
980 // 'barcode' represents the loading of a new item - refetch it
981 // regardless of whether it matches the current item.
982 if (!barcode && itemSvc.copy && itemSvc.copy.id() == copyId) {
983 $scope.copy = itemSvc.copy;
984 $scope.recordId = itemSvc.copy.call_number().record().id();
991 var deferred = $q.defer();
992 itemSvc.fetch(barcode, copyId, true).then(function(res) {
993 $scope.context.selectBarcode = true;
997 $scope.context.itemNotFound = true;
998 deferred.reject(); // avoid propagation of data fetch calls
1002 var copy = res.copy;
1003 itemSvc.copy = copy;
1007 $scope.recordId = copy.call_number().record().id();
1008 $scope.args.barcode = '';
1010 // locally flesh org units
1011 copy.circ_lib(egCore.org.get(copy.circ_lib()));
1012 copy.call_number().owning_lib(
1013 egCore.org.get(copy.call_number().owning_lib()));
1015 var r = copy.call_number().record();
1016 if (r.owner()) r.owner(egCore.org.get(r.owner()));
1018 // make boolean for auto-magic true/false display
1020 ['ref','opac_visible','holdable','circulate'],
1021 function(field) { copy[field](Boolean(copy[field]() == 't')) }
1024 // finally, if this is a different copy, redirect.
1025 // Note that we flesh first since the copy we just
1026 // fetched will be used after the redirect.
1027 if (copyId && copyId != copy.id()) {
1028 // if a new barcode is scanned in the detail view,
1029 // update the url to match the ID of the new copy
1030 $location.path('/cat/item/' + copy.id() + '/' + $scope.tab);
1031 deferred.reject(); // avoid propagation of data fetch calls
1039 return deferred.promise;
1042 // if loadPrev load the two most recent circulations
1043 function loadCurrentCirc(loadPrev) {
1045 delete $scope.circ_summary;
1046 delete $scope.prev_circ_summary;
1047 delete $scope.prev_circ_usr;
1048 if (!copyId) return;
1050 egCore.pcrud.search('circ',
1051 {target_copy : copyId},
1057 'checkin_workstation',
1060 'recurring_fine_rule'
1064 order_by : {circ : 'xact_start desc'},
1068 ).then(null, null, function(circ) {
1071 // load the chain for this circ
1074 'open-ils.circ.renewal_chain.retrieve_by_circ.summary',
1075 egCore.auth.token(), $scope.circ.id()
1076 ).then(function(summary) {
1077 $scope.circ_summary = summary.summary;
1080 if (!loadPrev) return;
1082 // load the chain for the previous circ, plus the user
1085 'open-ils.circ.prev_renewal_chain.retrieve_by_circ.summary',
1086 egCore.auth.token(), $scope.circ.id()
1088 ).then(null, null, function(summary) {
1089 $scope.prev_circ_summary = summary.summary;
1091 if (summary.usr) { // aged circs have no 'usr'.
1092 egCore.pcrud.retrieve('au', summary.usr,
1093 {flesh : 1, flesh_fields : {au : ['card']}})
1095 .then(function(user) { $scope.prev_circ_usr = user });
1102 function fetchMaxCircHistory() {
1103 if (maxHistory) return $q.when(maxHistory);
1104 return egCore.org.settings(
1105 'circ.item_checkout_history.max')
1106 .then(function(set) {
1107 maxHistory = set['circ.item_checkout_history.max'] || 4;
1112 $scope.addBilling = function(circ) {
1113 egBilling.showBillDialog({
1114 xact_id : circ.id(),
1119 $scope.retrieveAllPatrons = function() {
1120 var users = new Set();
1121 angular.forEach($scope.circ_list.map(function(circ) { return circ.usr(); }),function(usr) {
1122 // aged circs have no 'usr'.
1123 if (usr) users.add(usr);
1125 users.forEach(function(usr) {
1126 $timeout(function() {
1127 var url = $location.absUrl().replace(
1129 '/circ/patron/' + usr.id() + '/checkout');
1130 $window.open(url, '_blank')
1135 function loadCircHistory() {
1136 $scope.circ_list = [];
1139 itemSvc.copy.call_number().id() == -1 ?
1140 itemSvc.copy.circ_lib().id() :
1141 itemSvc.copy.call_number().owning_lib().id()
1143 // there is an extra layer of permissibility over circ
1145 egCore.perm.hasPermAt('VIEW_COPY_CHECKOUT_HISTORY', true)
1146 .then(function(orgIds) {
1148 if (orgIds.indexOf(copy_org) == -1) {
1149 console.log('User is not allowed to view circ history');
1153 return fetchMaxCircHistory();
1155 }).then(function(count) {
1157 egCore.pcrud.search('combcirc',
1158 {target_copy : copyId},
1164 'checkin_workstation',
1165 'recurring_fine_rule'
1169 order_by : {combcirc : 'xact_start desc'},
1173 ).then(null, null, function(circ) {
1175 // flesh circ_lib locally
1176 circ.circ_lib(egCore.org.get(circ.circ_lib()));
1177 circ.checkin_lib(egCore.org.get(circ.checkin_lib()));
1178 $scope.circ_list.push(circ);
1184 function loadCircCounts() {
1186 delete $scope.circ_counts;
1187 $scope.total_circs = 0;
1188 $scope.total_circs_this_year = 0;
1189 $scope.total_circs_prev_year = 0;
1190 if (!copyId) return;
1192 egCore.pcrud.search('circbyyr',
1193 {copy : copyId}, null, {atomic : true})
1195 .then(function(counts) {
1196 $scope.circ_counts = counts;
1198 angular.forEach(counts, function(count) {
1199 $scope.total_circs += Number(count.count());
1202 var this_year = counts.filter(function(c) {
1203 return c.year() == new Date().getFullYear();
1206 $scope.total_circs_this_year =
1207 this_year.length ? this_year[0].count() : 0;
1209 var prev_year = counts.filter(function(c) {
1210 return c.year() == new Date().getFullYear() - 1;
1213 $scope.total_circs_prev_year =
1214 prev_year.length ? prev_year[0].count() : 0;
1219 function loadHolds() {
1221 if (!copyId) return;
1223 egCore.pcrud.search('ahr',
1224 { current_copy : copyId,
1226 fulfillment_time : null,
1227 capture_time : {'<>' : null}
1231 ahr : ['requestor', 'usr'],
1235 ).then(null, null, function(hold) {
1237 hold.pickup_lib(egCore.org.get(hold.pickup_lib()));
1238 if (hold.current_shelf_lib()) {
1239 hold.current_shelf_lib(
1240 egCore.org.get(hold.current_shelf_lib()));
1242 hold.behind_desk(Boolean(hold.behind_desk() == 't'));
1246 function loadTransits() {
1247 delete $scope.transit;
1248 delete $scope.hold_transit;
1249 if (!copyId) return;
1251 egCore.pcrud.search('atc',
1252 {target_copy : copyId},
1253 {order_by : {atc : 'source_send_time DESC'}}
1255 ).then(null, null, function(transit) {
1256 $scope.transit = transit;
1257 transit.source(egCore.org.get(transit.source()));
1258 transit.dest(egCore.org.get(transit.dest()));
1263 // we don't need all data on all tabs, so fetch what's needed when needed.
1264 function loadTabData() {
1265 switch($scope.tab) {
1272 loadCurrentCirc(true);
1284 case 'triggered_events':
1285 var url = $location.absUrl().replace(/\/staff.*/, '/actor/user/event_log');
1286 url += '?copy_id=' + encodeURIComponent(copyId);
1287 $scope.triggered_events_url = url;
1294 'open-ils.actor.anon_cache.set_value',
1295 null, 'edit-these-copies', {
1296 record_id: $scope.recordId,
1301 ).then(function(key) {
1303 var url = egCore.env.basePath + 'cat/volcopy/' + key;
1304 $window.location.href = url;
1306 alert('Could not create anonymous cache key!');
1314 $scope.context.toggleDisplay = function() {
1315 $location.path('/cat/item/search');
1318 // handle the barcode scan box, which will replace our current copy
1319 $scope.context.search = function(args) {
1320 loadCopy(args.barcode).then(loadTabData);
1323 $scope.context.show_triggered_events = function() {
1324 $location.path('/cat/item/' + copyId + '/triggered_events');
1327 loadCopy().then(loadTabData);