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?|mailto|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'});
51 * Search bar along the top of the page.
52 * Parent scope for list and detail views
54 .controller('SearchCtrl',
55 ['$scope','$q','$window','$location','$timeout','egCore','egNet','egGridDataProvider','egItem', 'egCirc', 'ngToast',
56 function($scope , $q , $window , $location , $timeout , egCore , egNet , egGridDataProvider , itemSvc , egCirc, ngToast) {
57 $scope.args = {}; // search args
59 // sub-scopes (search / detail-view) apply their version
60 // of retrieval function to $scope.context.search
61 // and display toggling via $scope.context.toggleDisplay
66 $scope.toggleView = function($event) {
67 $scope.context.toggleDisplay();
68 $event.preventDefault(); // avoid form submission
71 // The functions that follow in this controller are never called
72 // when the List View is active, only the Detail View.
74 // In this context, we're only ever dealing with 1 item, so
75 // we can simply refresh the page. These various itemSvc
76 // functions used to live in the ListCtrl, but they're now
77 // shared between SearchCtrl (for Actions for the Detail View)
78 // and ListCtrl (Actions in the egGrid)
79 itemSvc.add_barcode_to_list = function(b) {
80 //console.log('SearchCtrl: add_barcode_to_list',b);
81 // timeout so audible can happen upon checkin
82 $timeout(function() { location.href = location.href; }, 1000);
85 $scope.add_copies_to_bucket = function() {
86 itemSvc.add_copies_to_bucket([$scope.args.copyId]);
89 $scope.add_records_to_bucket = function() {
90 itemSvc.add_records_to_bucket([$scope.args.recordId], 'biblio');
93 $scope.show_in_catalog = function() {
94 window.open('/eg2/staff/catalog/record/' + $scope.args.recordId, '_blank');
97 $scope.print_labels = function() {
98 itemSvc.print_spine_labels([$scope.args.copyId]);
102 $scope.make_copies_bookable = function() {
103 itemSvc.make_copies_bookable([{
104 id : $scope.args.copyId,
105 'call_number.record.id' : $scope.args.recordId
109 $scope.book_copies_now = function() {
110 itemSvc.book_copies_now([$scope.args.copyBarcode]);
113 $scope.findAcquisition = function() {
116 $scope.openAcquisitionLineItem([$scope.args.copyId]);
119 $scope.openAcquisitionLineItem = function (cp_list) {
120 var hasResults = false;
123 angular.forEach(cp_list, function (copyId) {
127 'open-ils.acq.lineitem.retrieve.by_copy_id',
130 ).then(function (acqData) {
133 acqData = egCore.idl.toHash(acqData);
134 var url = '/eg2/staff/acq/po/' + acqData.purchase_order + '#' + acqData.id;
135 $timeout(function () { $window.open(url, '_blank') });
143 $q.all(promises).then(function () {
144 !hasResults ? alert('There is no corresponding purchase order for this item.') : false;
148 $scope.manage_reservations = function() {
149 itemSvc.manage_reservations([$scope.args.copyBarcode]);
152 $scope.requestItems = function() {
153 itemSvc.requestItems([$scope.args.copyId],[$scope.args.recordId]);
156 $scope.update_inventory = function() {
157 itemSvc.updateInventory([$scope.args.copyId], null)
158 .then(function(res) {
160 ngToast.create(egCore.strings.SUCCESS_UPDATE_INVENTORY_SINGLE);
162 ngToast.warning(egCore.strings.FAIL_UPDATE_INVENTORY_SINGLE);
164 $timeout(function() { location.href = location.href; }, 1500);
168 $scope.show_triggered_events = function() {
169 window.open('/eg2/staff/circ/item/event-log/' + $scope.args.copyId, '_blank');
172 $scope.show_item_holds = function() {
173 $location.path('/cat/item/' + $scope.args.copyId + '/holds');
176 $scope.show_record_holds = function() {
177 window.open('/eg2/staff/catalog/record/' + $scope.args.recordId + '/holds', '_blank');
180 $scope.add_item_alerts = function() {
181 egCirc.add_copy_alerts([$scope.args.copyId]);
184 $scope.manage_item_alerts = function() {
185 egCirc.manage_copy_alerts([$scope.args.copyId]);
189 $scope.attach_to_peer_bib = function() {
190 itemSvc.attach_to_peer_bib([{
191 id : $scope.args.copyId,
192 barcode : $scope.args.copyBarcode
196 $scope.selectedHoldingsCopyDelete = function () {
197 itemSvc.selectedHoldingsCopyDelete([{
198 id : $scope.args.copyId,
199 barcode : $scope.args.copyBarcode
203 $scope.checkin = function () {
205 id : $scope.args.copyId,
206 barcode : $scope.args.copyBarcode
210 $scope.renew = function () {
212 id : $scope.args.copyId,
213 barcode : $scope.args.copyBarcode
217 $scope.cancel_transit = function () {
218 itemSvc.cancel_transit([{
219 id : $scope.args.copyId,
220 barcode : $scope.args.copyBarcode
224 $scope.selectedHoldingsDamaged = function () {
225 itemSvc.selectedHoldingsDamaged([{
226 id : $scope.args.copyId,
227 barcode : $scope.args.copyBarcode,
232 $scope.selectedHoldingsDiscard = function () {
233 itemSvc.selectedHoldingsDiscard([{
234 id : $scope.args.copyId,
235 barcode : $scope.args.barcode
239 $scope.selectedHoldingsMissing = function () {
240 itemSvc.selectedHoldingsMissing([{
241 id : $scope.args.copyId,
242 barcode : $scope.args.barcode
243 }]).catch(function(){});
246 $scope.selectedHoldingsVolCopyAdd = function () {
247 itemSvc.spawnHoldingsAdd([{
248 id : $scope.args.copyId,
249 'call_number.owning_lib' : $scope.args.cnOwningLib,
250 'call_number.record.id' : $scope.args.recordId,
251 barcode : $scope.args.copyBarcode
254 $scope.selectedHoldingsCopyAdd = function () {
255 itemSvc.spawnHoldingsAdd([{
256 id : $scope.args.copyId,
257 'call_number.id' : $scope.args.cnId,
258 'call_number.owning_lib' : $scope.args.cnOwningLib,
259 'call_number.record.id' : $scope.args.recordId,
260 barcode : $scope.args.copyBarcode
264 $scope.selectedHoldingsVolCopyEdit = function () {
265 itemSvc.spawnHoldingsEdit([{
266 id : $scope.args.copyId,
267 'call_number.id' : $scope.args.cnId,
268 'call_number.owning_lib' : $scope.args.cnOwningLib,
269 'call_number.record.id' : $scope.args.recordId,
270 barcode : $scope.args.copyBarcode
273 $scope.selectedHoldingsVolEdit = function () {
274 itemSvc.spawnHoldingsEdit([{
275 id : $scope.args.copyId,
276 'call_number.id' : $scope.args.cnId,
277 'call_number.owning_lib' : $scope.args.cnOwningLib,
278 'call_number.record.id' : $scope.args.recordId,
279 barcode : $scope.args.copyBarcode
282 $scope.selectedHoldingsCopyEdit = function () {
283 itemSvc.spawnHoldingsEdit([{
284 id : $scope.args.copyId,
285 'call_number.id' : $scope.args.cnId,
286 'call_number.owning_lib' : $scope.args.cnOwningLib,
287 'call_number.record.id' : $scope.args.recordId,
288 barcode : $scope.args.copyBarcode
292 $scope.replaceBarcodes = function() {
293 itemSvc.replaceBarcodes([{
294 id : $scope.args.copyId,
295 barcode : $scope.args.copyBarcode
299 $scope.changeItemOwningLib = function() {
300 itemSvc.changeItemOwningLib([{
301 id : $scope.args.copyId,
302 'call_number.id' : $scope.args.cnId,
303 'call_number.owning_lib' : $scope.args.cnOwningLib,
304 'call_number.record.id' : $scope.args.recordId,
305 'call_number.label' : $scope.args.cnLabel,
306 'call_number.label_class' : $scope.args.cnLabelClass,
307 'call_number.prefix.id' : $scope.args.cnPrefixId,
308 'call_number.suffix.id' : $scope.args.cnSuffixId,
309 barcode : $scope.args.copyBarcode
313 $scope.transferItems = function (){
314 itemSvc.transferItems([{
315 id : $scope.args.copyId,
316 barcode : $scope.args.copyBarcode
323 * List view - grid stuff
325 .controller('ListCtrl',
326 ['$scope','$q','$routeParams','$location','$timeout','$window','egCore',
327 'egGridDataProvider','egItem','egUser','$uibModal','egCirc','egConfirmDialog',
328 'egProgressDialog', 'ngToast',
329 // function($scope , $q , $routeParams , $location , $timeout , $window , egCore ,
330 // egGridDataProvider , itemSvc , egUser , $uibModal , egCirc , egConfirmDialog,
331 // egProgressDialog, ngToast) {
332 function($scope , $q , $routeParams , $location , $timeout , $window , egCore , egGridDataProvider , itemSvc , egUser , $uibModal , egCirc , egConfirmDialog,
333 egProgressDialog, ngToast) {
335 var cp_list = $routeParams.idList;
337 copyId = cp_list.split(',');
340 var modified_items = new Set();
342 $scope.context.page = 'list';
345 var provider = egGridDataProvider.instance();
346 provider.get = function(offset, count) {
350 $scope.gridDataProvider = egGridDataProvider.instance({
351 get : function(offset, count) {
352 //return provider.arrayNotifier(itemSvc.copies, offset, count);
353 return this.arrayNotifier(itemSvc.copies, offset, count);
357 // If a copy was just displayed in the detail view, ensure it's
358 // focused in the list view.
359 var selected = false;
360 var copyGrid = $scope.gridControls = {
361 itemRetrieved : function(item) {
362 if (selected || !itemSvc.copy) return;
363 if (itemSvc.copy.id() == item.id) {
364 copyGrid.selectItems([item.index]);
370 $scope.$watch('barcodesFromFile', function(newVal, oldVal) {
371 $scope.context.itemsNotFound = [];
372 $scope.context.fileDoneLoading = false;
373 $scope.context.numBarcodesInFile = 0;
374 if (newVal && newVal != oldVal) {
375 $scope.args.barcode = '';
378 angular.forEach(newVal.split(/\r?\n/), function(line) {
379 //remove all whitespace and commas
380 line = line.replace(/[\s,]+/g,'');
382 //Or remove leading/trailing whitespace
383 //line = line.replace(/(^[\s,]+|[\s,]+$/g,'');
389 // Serialize copy retrieval since there may be many, many copies.
390 function fetch_next_copy() {
391 var barcode = barcodes.pop();
392 egProgressDialog.increment();
394 if (barcode == undefined) { // All done here.
395 egProgressDialog.close();
397 if(itemSvc.copies[0]){ // Were any copies actually retrieved
398 copyGrid.selectItems([itemSvc.copies[0].index]);
400 $scope.context.fileDoneLoading = true;
404 itemSvc.fetch(barcode).then(function(item) {
406 $scope.context.itemsNotFound.push(barcode);
412 if (barcodes.length) {
413 $scope.context.numBarcodesInFile = barcodes.length;
414 egProgressDialog.open({value: 0, max: barcodes.length});
420 $scope.context.search = function(args, noGridRefresh) {
421 if (!args.barcode) return $q.when();
422 $scope.context.itemNotFound = false;
424 //check to see if there are multiple barcodes in CSV format
426 //split on commas and clean up barcodes
427 angular.forEach(args.barcode.split(/,/), function(line) {
428 //remove all whitespace and commas
429 line = line.replace(/[\s,]+/g,'');
431 //Or remove leading/trailing whitespace
432 //line = line.replace(/(^[\s,]+|[\s,]+$/g,'');
438 if(barcodes.length > 1){
439 //convert to newline seperated list and send to barcodesFromFile processor
440 $scope.barcodesFromFile = barcodes.join('\n');
441 //console.log('Barcodes: ',barcodes);
446 return itemSvc.fetch(args.barcode).then(function(res) {
448 if (!noGridRefresh) {
451 copyGrid.selectItems([res.index]);
452 $scope.args.barcode = '';
454 $scope.context.itemNotFound = true;
455 egCore.audio.play('warning.item_status.itemNotFound');
457 $scope.context.selectBarcode = true;
462 var add_barcode_to_list = function (b, noGridRefresh) {
463 // console.log('listCtrl: add_barcode_to_list',b);
464 return $scope.context.search({barcode:b}, noGridRefresh);
466 itemSvc.add_barcode_to_list = add_barcode_to_list;
468 $scope.context.toggleDisplay = function() {
469 var item = copyGrid.selectedItems()[0];
471 $location.path('/cat/item/' + item.id);
474 $scope.context.show_triggered_events = function() {
475 var item = copyGrid.selectedItems()[0];
477 window.open('/eg2/staff/circ/item/event-log/' + item.id, '_blank');
480 function gatherSelectedRecordIds () {
483 copyGrid.selectedItems(),
485 if (rid_list.indexOf(item['call_number.record.id']) == -1)
486 rid_list.push(item['call_number.record.id'])
492 function gatherSelectedVolumeIds (rid) {
495 copyGrid.selectedItems(),
497 if (rid && item['call_number.record.id'] != rid) return;
498 if (cn_id_list.indexOf(item['call_number.id']) == -1)
499 cn_id_list.push(item['call_number.id'])
505 function gatherSelectedHoldingsIds (rid) {
508 copyGrid.selectedItems(),
510 if (rid && item['call_number.record.id'] != rid) return;
511 cp_id_list.push(item.id)
517 function gatherSelectedHoldingsRecords() {
518 var record_id_list = [];
520 copyGrid.selectedItems(),
522 record_id_list.push(item['call_number.record.id']);
525 return record_id_list;
528 // Refresh the data shown in the item status grid,
529 // such as after the user has changed some data
531 // Takes an optional restrictToIds argument, which
532 // is a Set of item IDs that have been changed
533 $scope.refreshGridData = function(restrictToIds) {
537 angular.forEach(itemSvc.copies, function(item, index) {
538 if (!restrictToIds || restrictToIds.has(item['id'])) {
540 itemSvc.fetch(null, item['id'], null, true)
541 .then(function(res) {
542 itemSvc.copies[index] = res;
543 if (progress_bar) egProgressDialog.increment();
550 progress_bar = $timeout(egProgressDialog.open, 5000, true, {value: 0, max: fetch_list.length});
555 if (progress_bar) $timeout.cancel(progress_bar);
556 egProgressDialog.close();
557 ngToast.create(egCore.strings.ITEMS_SUCCESSFULLY_MODIFIED);
562 $scope.add_copies_to_bucket = function() {
563 var copy_list = gatherSelectedHoldingsIds();
564 itemSvc.add_copies_to_bucket(copy_list);
567 $scope.add_records_to_bucket = function() {
568 var record_list = gatherSelectedHoldingsRecords();
569 itemSvc.add_copies_to_bucket(record_list, 'biblio');
572 $scope.locateAcquisition = function() {
573 if (gatherSelectedHoldingsIds) {
574 var cp_list = gatherSelectedHoldingsIds();
576 if (cp_list.length > 0) {
577 $scope.openAcquisitionLineItem(cp_list);
583 $scope.update_inventory = function() {
584 var copy_list = gatherSelectedHoldingsIds();
585 itemSvc.updateInventory(copy_list, $scope.gridControls.allItems()).then(function(res) {
587 ngToast.create(egCore.strings.SUCCESS_UPDATE_INVENTORY);
589 ngToast.warning(egCore.strings.FAIL_UPDATE_INVENTORY);
594 $scope.need_one_selected = function() {
595 var items = $scope.gridControls.selectedItems();
596 if (items.length == 1) return false;
600 $scope.need_at_least_one_selected = function() {
601 var items = $scope.gridControls.selectedItems();
602 if (items.length == 0) return true; // Disable the control (i.e. true) if none selected
606 $scope.make_copies_bookable = function() {
607 itemSvc.make_copies_bookable(copyGrid.selectedItems());
610 $scope.book_copies_now = function() {
611 var item = copyGrid.selectedItems()[0];
613 itemSvc.book_copies_now(item.barcode);
616 $scope.manage_reservations = function() {
617 var item = copyGrid.selectedItems()[0];
619 itemSvc.manage_reservations(item.barcode);
622 $scope.create_carousel = function() {
623 var itemIds = copyGrid.selectedItems().map(function (item) {return item.id});
624 itemSvc.create_carousel_from_items(itemIds);
627 $scope.requestItems = function() {
628 var copy_list = gatherSelectedHoldingsIds();
629 var record_list = gatherSelectedRecordIds();
630 itemSvc.requestItems(copy_list,record_list);
633 $scope.replaceBarcodes = function() {
634 itemSvc.replaceBarcodes(copyGrid.selectedItems());
637 $scope.attach_to_peer_bib = function() {
638 itemSvc.attach_to_peer_bib(copyGrid.selectedItems());
641 $scope.selectedHoldingsCopyDelete = function () {
642 itemSvc.selectedHoldingsCopyDelete(copyGrid.selectedItems());
645 $scope.selectedHoldingsItemStatusTgrEvt= function() {
646 var item = copyGrid.selectedItems()[0];
648 window.open('/eg2/staff/circ/item/event-log/' + item.id, '_blank');
651 $scope.selectedHoldingsItemStatusHolds= function() {
652 var item = copyGrid.selectedItems()[0];
654 $location.path('/cat/item/' + item.id + '/holds');
657 $scope.cancel_transit = function () {
658 itemSvc.cancel_transit(copyGrid.selectedItems());
661 $scope.selectedHoldingsDamaged = function () {
662 itemSvc.selectedHoldingsDamaged(copyGrid.selectedItems());
665 $scope.selectedHoldingsDiscard = function () {
666 itemSvc.selectedHoldingsDiscard(copyGrid.selectedItems());
669 $scope.selectedHoldingsMissing = function () {
670 var dialog = egProgressDialog.open();
671 dialog.opened.then(function() {
672 itemSvc.selectedHoldingsMissing(copyGrid.selectedItems())
674 console.debug('Marking missing complete, refreshing grid');
676 }).catch(function() {
677 }).finally(function() {
678 egProgressDialog.close();
683 $scope.checkin = function () {
684 itemSvc.checkin(copyGrid.selectedItems());
687 $scope.renew = function () {
688 itemSvc.renew(copyGrid.selectedItems());
691 $scope.selectedHoldingsVolCopyAdd = function () {
692 itemSvc.spawnHoldingsAdd(copyGrid.selectedItems(),true,false);
694 $scope.selectedHoldingsCopyAdd = function () {
695 itemSvc.spawnHoldingsAdd(copyGrid.selectedItems(),false,true);
698 $scope.selectedHoldingsCopyAlertsAdd = function(items) {
700 angular.forEach(items, function(item) {
701 if (item.id) copy_ids.push(item.id);
703 egCirc.add_copy_alerts(copy_ids).then(function() {
704 // update grid items?
708 $scope.selectedHoldingsCopyAlertsEdit = function(items) {
710 angular.forEach(items, function(item) {
711 if (item.id) copy_ids.push(item.id);
713 egCirc.manage_copy_alerts(copy_ids).then(function() {
714 // update grid items?
718 $scope.gridCellHandlers = {};
719 $scope.gridCellHandlers.copyAlertsEdit = function(id) {
720 egCirc.manage_copy_alerts([id]).then(function() {
721 // update grid items?
725 $scope.showBibHolds = function () {
726 angular.forEach(gatherSelectedRecordIds(), function (r) {
727 var url = '/eg2/staff/catalog/record/' + r + '/holds';
728 $timeout(function() { $window.open(url, '_blank') });
732 $scope.selectedHoldingsVolCopyEdit = function () {
733 itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),false,false);
735 $scope.selectedHoldingsVolEdit = function () {
736 itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),false,true);
738 $scope.selectedHoldingsCopyEdit = function () {
739 itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),true,false);
742 $scope.changeItemOwningLib = function() {
743 itemSvc.changeItemOwningLib(copyGrid.selectedItems());
746 $scope.transferItems = function (){
747 itemSvc.transferItems(copyGrid.selectedItems());
750 $scope.print_labels = function() {
753 'open-ils.actor.anon_cache.set_value',
754 null, 'print-labels-these-copies', {
755 copies : gatherSelectedHoldingsIds()
757 ).then(function(key) {
759 var url = egCore.env.basePath + 'cat/printlabels/' + key;
760 $timeout(function() { $window.open(url, '_blank') });
762 alert('Could not create anonymous cache key!');
767 $scope.print_list = function() {
768 var print_data = { copies : copyGrid.allItems() };
770 if (print_data.copies.length == 0) return $q.when();
772 return egCore.print.print({
773 template : 'item_status',
778 $scope.show_in_catalog = function(){
779 itemSvc.show_in_catalog(copyGrid.selectedItems());
782 if (copyId.length > 0) {
784 angular.forEach(copyId, function (c) {
785 fetch_list.push(itemSvc.fetch(null,c));
788 return $q.all(fetch_list).then(function (res) { copyGrid.refresh(); });
791 $scope.statusIconColumn = {
793 template: function(item) {
795 if (modified_items.has(item['id'])) {
796 icon = '<span class="glyphicon glyphicon-floppy-saved"' +
797 'title="' + egCore.strings.ITEM_SUCCESSFULLY_MODIFIED + '" ' +
798 'aria-label="' + egCore.strings.ITEM_SUCCESSFULLY_MODIFIED + '">' +
805 if (typeof BroadcastChannel != 'undefined') {
806 var holdings_bChannel = new BroadcastChannel("eg.holdings.update");
807 holdings_bChannel.onmessage = function(e) {
808 if (e.data.copies.length) {
809 angular.forEach(e.data.copies, function(i) {
810 modified_items.add(i);
812 $scope.refreshGridData(modified_items);
813 } else { // if only call numbers were modified
814 egCore.pcrud.search('acp',
817 call_number : e.data.volumes
818 }, null, {atomic: true}
820 ).then(function(all_affected_items) {
821 all_affected_items.map(function(item) {
822 modified_items.add(item.id());
824 $scope.refreshGridData(modified_items);
829 $scope.$on('$destroy', function() {
830 holdings_bChannel.close();
837 * Detail view -- shows one copy
839 .controller('ViewCtrl',
840 ['$scope','$q','egGridDataProvider','$location','$routeParams','$timeout','$window','egCore','egItem','egBilling','egCirc',
841 function($scope , $q , egGridDataProvider , $location , $routeParams , $timeout , $window , egCore , itemSvc , egBilling , egCirc) {
842 var copyId = $routeParams.id;
843 $scope.args.copyId = copyId;
844 $scope.tab = $routeParams.tab || 'summary';
845 $scope.context.page = 'detail';
846 $scope.summaryRecord = null;
847 $scope.courseModulesOptIn = fetchCourseOptIn();
848 $scope.has_course_perms = fetchCoursePerms();
850 if ($scope.tab == 'edit') {
851 $scope.tab = 'summary';
855 // use the cached record info
857 $scope.copy_alert_count = itemSvc.copy.copy_alerts().filter(function(aca) {
858 return !aca.ack_time();
860 $scope.recordId = itemSvc.copy.call_number().record().id();
861 $scope.args.recordId = $scope.recordId;
862 $scope.args.cnId = itemSvc.copy.call_number().id();
863 $scope.args.cnOwningLib = itemSvc.copy.call_number().owning_lib();
864 $scope.args.cnLabel = itemSvc.copy.call_number().label();
865 $scope.args.cnLabelClass = itemSvc.copy.call_number().label_class();
866 $scope.args.cnPrefixId = itemSvc.copy.call_number().prefix().id();
867 $scope.args.cnSuffixId = itemSvc.copy.call_number().suffix().id();
868 $scope.args.copyBarcode = itemSvc.copy.barcode();
871 function loadCopy(barcode) {
872 $scope.context.itemNotFound = false;
874 // Avoid re-fetching the same copy while jumping tabs.
875 // In addition to being quicker, this helps to avoid flickering
876 // of the top panel which is always visible in the detail view.
878 // 'barcode' represents the loading of a new item - refetch it
879 // regardless of whether it matches the current item.
880 if (!barcode && itemSvc.copy && itemSvc.copy.id() == copyId) {
881 $scope.copy = itemSvc.copy;
882 if (itemSvc.latest_inventory && itemSvc.latest_inventory.copy() == copyId) {
883 $scope.latest_inventory = itemSvc.latest_inventory;
885 $scope.copy_alert_count = itemSvc.copy.copy_alerts().filter(function(aca) {
886 return !aca.ack_time();
888 $scope.recordId = itemSvc.copy.call_number().record().id();
889 $scope.args.recordId = $scope.recordId;
890 $scope.args.cnId = itemSvc.copy.call_number().id();
891 $scope.args.cnOwningLib = itemSvc.copy.call_number().owning_lib();
892 $scope.args.cnLabel = itemSvc.copy.call_number().label();
893 $scope.args.cnLabelClass = itemSvc.copy.call_number().label_class();
894 $scope.args.cnPrefixId = itemSvc.copy.call_number().prefix().id();
895 $scope.args.cnSuffixId = itemSvc.copy.call_number().suffix().id();
896 $scope.args.copyBarcode = itemSvc.copy.barcode();
903 var deferred = $q.defer();
904 itemSvc.fetch(barcode, copyId, true).then(function(res) {
905 $scope.context.selectBarcode = true;
909 $scope.context.itemNotFound = true;
910 egCore.audio.play('warning.item_status.itemNotFound');
911 deferred.reject(); // avoid propagation of data fetch calls
917 if (res.latest_inventory) itemSvc.latest_inventory = res.latest_inventory;
921 $scope.latest_inventory = res.latest_inventory;
922 $scope.copy_alert_count = copy.copy_alerts().filter(function(aca) {
923 return !aca.ack_time();
925 console.debug($scope.copy_alert_count);
926 $scope.recordId = copy.call_number().record().id();
927 $scope.args.recordId = $scope.recordId;
928 $scope.args.cnId = itemSvc.copy.call_number().id();
929 $scope.args.cnOwningLib = itemSvc.copy.call_number().owning_lib();
930 $scope.args.cnLabel = itemSvc.copy.call_number().label();
931 $scope.args.cnLabelClass = itemSvc.copy.call_number().label_class();
932 $scope.args.cnPrefixId = itemSvc.copy.call_number().prefix().id();
933 $scope.args.cnSuffixId = itemSvc.copy.call_number().suffix().id();
934 $scope.args.copyBarcode = copy.barcode();
935 $scope.args.barcode = '';
937 // locally flesh org units
938 copy.circ_lib(egCore.org.get(copy.circ_lib()));
939 copy.call_number().owning_lib(
940 egCore.org.get(copy.call_number().owning_lib()));
942 var r = copy.call_number().record();
943 if (r.owner()) r.owner(egCore.org.get(r.owner()));
945 // make boolean for auto-magic true/false display
947 ['ref','opac_visible','holdable','circulate'],
948 function(field) { copy[field](Boolean(copy[field]() == 't')) }
951 // finally, if this is a different copy, redirect.
952 // Note that we flesh first since the copy we just
953 // fetched will be used after the redirect.
954 if (copyId && copyId != copy.id()) {
955 // if a new barcode is scanned in the detail view,
956 // update the url to match the ID of the new copy
957 $location.path('/cat/item/' + copy.id() + '/' + $scope.tab);
958 deferred.reject(); // avoid propagation of data fetch calls
966 return deferred.promise;
969 // load the two most recent circulations in /circs tab
970 function loadCurrentCirc() {
972 delete $scope.circ_summary;
973 delete $scope.prev_circ_summary;
974 delete $scope.prev_circ_usr;
978 itemSvc.copy.call_number().id() == -1 ?
979 itemSvc.copy.circ_lib().id() :
980 itemSvc.copy.call_number().owning_lib().id();
982 // since a user can still view patron checkout history here, check perms
983 egCore.perm.hasPermAt('VIEW_COPY_CHECKOUT_HISTORY', true)
984 .then(function(orgIds){
985 if(orgIds.indexOf(copy_org) == -1){
986 console.warn('User is not allowed to view circ history!');
990 return fetchMaxCircHistory();
992 .then(function(maxHistCount){
994 if (!maxHistCount) $scope.isMaxCircHistoryZero = true;
996 egCore.pcrud.search('aacs',
997 {target_copy : copyId},
1003 'checkin_workstation',
1006 'recurring_fine_rule'
1010 order_by : {aacs : 'xact_start desc'},
1014 ).then(null, null, function(circ) {
1017 if (!circ) return $q.when();
1019 // load the chain for this circ
1022 'open-ils.circ.renewal_chain.retrieve_by_circ.summary',
1023 egCore.auth.token(), $scope.circ.id()
1024 ).then(function(summary) {
1025 $scope.circ_summary = summary;
1028 if (maxHistCount <= 1) return;
1030 // load the chain for the previous circ, plus the user
1033 'open-ils.circ.prev_renewal_chain.retrieve_by_circ.summary',
1034 egCore.auth.token(), $scope.circ.id()
1036 ).then(null, null, function(summary) {
1037 $scope.prev_circ_summary = summary.summary;
1039 if (summary.usr) { // aged circs have no 'usr'.
1040 egCore.pcrud.retrieve('au', summary.usr,
1041 {flesh : 1, flesh_fields : {au : ['card']}})
1043 .then(function(user) { $scope.prev_circ_usr = user });
1051 function fetchMaxCircHistory() {
1052 if (maxHistory) return $q.when(maxHistory);
1053 return egCore.org.settings(
1054 'circ.item_checkout_history.max')
1055 .then(function(set) {
1056 maxHistory = set['circ.item_checkout_history.max'] || 4;
1057 return Number(maxHistory);
1061 // Check for Course Modules Opt-In to enable Course Info tab
1062 function fetchCourseOptIn() {
1063 return egCore.org.settings(
1064 'circ.course_materials_opt_in'
1065 ).then(function(set) {
1066 $scope.courseModulesOptIn = set['circ.course_materials_opt_in'];
1068 return $scope.courseModulesOptIn;
1072 function fetchCoursePerms() {
1073 return egCore.perm.hasPermAt('MANAGE RESERVES', true).then(function(orgIds) {
1074 if(orgIds.indexOf(egCore.auth.user().ws_ou()) != -1){
1075 $scope.has_course_perms = true;
1077 return $scope.has_course_perms;
1082 $scope.addBilling = function(circ) {
1083 egBilling.showBillDialog({
1084 xact_id : circ.id(),
1089 $scope.retrieveAllPatrons = function() {
1090 var users = new Set();
1091 angular.forEach($scope.circ_list.map(function(circ) { return circ.usr(); }),function(usr) {
1092 // aged circs have no 'usr'.
1093 if (usr) users.add(usr);
1095 users.forEach(function(usr) {
1096 $timeout(function() {
1097 var url = $location.absUrl().replace(
1099 '/circ/patron/' + usr.id() + '/checkout');
1100 $window.open(url, '_blank')
1105 // load data for /circ_list tab
1106 function loadCircHistory() {
1107 $scope.circ_list = [];
1110 itemSvc.copy.call_number().id() == -1 ?
1111 itemSvc.copy.circ_lib().id() :
1112 itemSvc.copy.call_number().owning_lib().id();
1114 // there is an extra layer of permissibility over circ
1116 egCore.perm.hasPermAt('VIEW_COPY_CHECKOUT_HISTORY', true)
1117 .then(function(orgIds) {
1119 if (orgIds.indexOf(copy_org) == -1) {
1120 console.log('User is not allowed to view circ history');
1124 return fetchMaxCircHistory();
1126 }).then(function(maxHistCount) {
1128 if(!maxHistCount) $scope.isMaxCircHistoryZero = true;
1130 egCore.pcrud.search('aacs',
1131 {target_copy : copyId},
1137 'checkin_workstation',
1138 'recurring_fine_rule',
1143 order_by : {aacs : 'xact_start desc'},
1144 // fetch at least one to see if copy ever circulated
1145 limit : $scope.isMaxCircHistoryZero ? 1 : maxHistCount
1148 ).then(null, null, function(circ) {
1152 // flesh circ_lib locally
1153 circ.circ_lib(egCore.org.get(circ.circ_lib()));
1154 circ.checkin_lib(egCore.org.get(circ.checkin_lib()));
1155 $scope.circ_list.push(circ);
1161 function loadCircCounts() {
1163 delete $scope.circ_counts;
1164 $scope.total_circs = 0;
1165 $scope.total_circs_this_year = 0;
1166 $scope.total_circs_prev_year = 0;
1167 $scope.circ_popover_placement = 'top';
1168 if (!copyId) return;
1170 egCore.pcrud.search('circbyyr',
1171 {copy : copyId}, null, {atomic : true})
1173 .then(function(counts) {
1174 var this_year = new Date().getFullYear();
1175 var prev_year = this_year - 1;
1177 $scope.circ_counts = counts.reduce(function(circ_counts, circbyyr) {
1178 var count = Number(circbyyr.count());
1179 var year = Number(circbyyr.year());
1181 var index = circ_counts.findIndex(function(existing_count) {
1182 return existing_count.year === year;
1186 circ_counts.push({count: count, year: year});
1188 circ_counts[index].count += count;
1191 $scope.total_circs += count;
1192 if (this_year === year) {
1193 $scope.total_circs_this_year += count;
1195 if (prev_year === year) {
1196 $scope.total_circs_prev_year += count;
1202 if ($scope.circ_counts.length > 15) {
1203 $scope.circ_popover_placement = 'right';
1208 function loadHolds() {
1210 if (!copyId) return;
1212 egCore.pcrud.search('ahr',
1213 { current_copy : copyId,
1215 fulfillment_time : null,
1216 capture_time : {'<>' : null}
1220 ahr : ['requestor', 'usr'],
1224 ).then(null, null, function(hold) {
1226 hold.pickup_lib(egCore.org.get(hold.pickup_lib()));
1227 if (hold.current_shelf_lib()) {
1228 hold.current_shelf_lib(
1229 egCore.org.get(hold.current_shelf_lib()));
1231 hold.behind_desk(Boolean(hold.behind_desk() == 't'));
1235 function loadMostRecentTransit() {
1236 delete $scope.transit;
1237 delete $scope.hold_transit;
1238 if (!copyId) return;
1240 egCore.pcrud.search('atc',
1241 {target_copy : copyId},
1243 order_by : {atc : 'source_send_time DESC'},
1247 ).then(null, null, function(transit) {
1248 // use progress callback since we'll get up to one result
1249 $scope.transit = transit;
1250 transit.source(egCore.org.get(transit.source()));
1251 transit.dest(egCore.org.get(transit.dest()));
1255 function loadCourseInfo() {
1256 delete $scope.courses;
1257 delete $scope.instructors;
1258 delete $scope.course_ids;
1259 delete $scope.instructors_exist;
1260 if (!copyId) return;
1261 $scope.course_ids = [];
1262 $scope.courses = [];
1263 $scope.instructors = {};
1265 egCore.pcrud.search('acmcm', {
1271 }, order_by: {acmc : 'id desc'}
1272 }).then(null, null, function(material) {
1274 $scope.courses.push(material.course());
1277 'open-ils.circ.course_users.retrieve',
1278 material.course().id()
1279 ).then(null, null, function(instructors) {
1280 angular.forEach(instructors, function(instructor) {
1281 var patron_id = instructor.patron_id.toString();
1282 if (!$scope.instructors[patron_id]) {
1283 $scope.instructors[patron_id] = instructor;
1284 $scope.instructors_exist = true;
1285 $scope.instructors[patron_id]._linked_course = [];
1287 $scope.instructors[patron_id]._linked_course.push({
1288 role: instructor.usr_role,
1289 course: material.course().name()
1297 // we don't need all data on all tabs, so fetch what's needed when needed.
1298 function loadTabData() {
1299 switch($scope.tab) {
1315 loadMostRecentTransit();
1322 case 'triggered_events':
1323 var url = $location.absUrl().replace(/\/staff\/.*/, '/actor/user/event_log');
1324 url += '?copy_id=' + encodeURIComponent(copyId);
1325 $scope.triggered_events_url = url;
1332 'open-ils.actor.anon_cache.set_value',
1333 null, 'edit-these-copies', {
1334 record_id: $scope.recordId,
1339 ).then(function(key) {
1341 var url = egCore.env.basePath + 'cat/volcopy/' + key;
1342 $window.location.href = url;
1344 alert('Could not create anonymous cache key!');
1352 $scope.addCopyAlerts = function(copy_id) {
1353 egCirc.add_copy_alerts([copy_id]).then(function() {
1355 loadCopy($scope.copy.barcode()).then(loadTabData);
1358 $scope.manageCopyAlerts = function(copy_id) {
1359 egCirc.manage_copy_alerts([copy_id]).then(function() {
1361 loadCopy($scope.copy.barcode()).then(loadTabData);
1365 $scope.context.toggleDisplay = function() {
1366 $location.path('/cat/item/search');
1369 // handle the barcode scan box, which will replace our current copy
1370 $scope.context.search = function(args) {
1371 loadCopy(args.barcode).then(loadTabData);
1374 $scope.context.show_triggered_events = function() {
1375 window.open('/eg2/staff/circ/item/event-log/' + copyId, '_blank');
1378 loadCopy().then(loadTabData);