LP #1705497 Replaces functionality in web client from legacy
[evergreen-equinox.git] / Open-ILS / web / js / ui / default / staff / cat / item / app.js
index aafea2e..26f21ba 100644 (file)
@@ -3,7 +3,7 @@
  */
 
 angular.module('egItemStatus', 
-    ['ngRoute', 'ui.bootstrap', 'egCoreMod', 'egUiMod', 'egGridMod'])
+    ['ngRoute', 'ui.bootstrap', 'egCoreMod', 'egUiMod', 'egGridMod', 'egUserMod'])
 
 .filter('boolText', function(){
     return function (v) {
@@ -13,8 +13,8 @@ angular.module('egItemStatus',
 
 .config(function($routeProvider, $locationProvider, $compileProvider) {
     $locationProvider.html5Mode(true);
-    $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/); // grid export
-
+    $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|mailto|blob):/); // grid export
+       
     var resolver = {delay : function(egStartup) {return egStartup.go()}};
 
     // search page shows the list view by default
@@ -47,79 +47,13 @@ angular.module('egItemStatus',
     $routeProvider.otherwise({redirectTo : '/cat/item/search'});
 })
 
-.factory('itemSvc', 
-       ['egCore',
-function(egCore) {
-
-    var service = {
-        copies : [], // copy barcode search results
-        index : 0 // search grid index
-    };
-
-    service.flesh = {   
-        flesh : 3, 
-        flesh_fields : {
-            acp : ['call_number','location','status','location'],
-            acn : ['record','prefix','suffix'],
-            bre : ['simple_record','creator','editor']
-        },
-        select : { 
-            // avoid fleshing MARC on the bre
-            // note: don't add simple_record.. not sure why
-            bre : ['id','tcn_value','creator','editor'],
-        } 
-    }
-
-    // resolved with the last received copy
-    service.fetch = function(barcode, id, noListDupes) {
-        var promise;
-
-        if (barcode) {
-            promise = egCore.pcrud.search('acp', 
-                {barcode : barcode, deleted : 'f'}, service.flesh);
-        } else {
-            promise = egCore.pcrud.retrieve('acp', id, service.flesh);
-        }
-
-        var lastRes;
-        return promise.then(
-            function() {return lastRes},
-            null, // error
-
-            // notify reads the stream of copies, one at a time.
-            function(copy) {
-
-                var flatCopy;
-                if (noListDupes) {
-                    // use the existing copy if possible
-                    flatCopy = service.copies.filter(
-                        function(c) {return c.id == copy.id()})[0];
-                }
-
-                if (!flatCopy) {
-                    flatCopy = egCore.idl.toHash(copy, true);
-                    flatCopy.index = service.index++;
-                    service.copies.unshift(flatCopy);
-                }
-
-                return lastRes = {
-                    copy : copy, 
-                    index : flatCopy.index
-                }
-            }
-        );
-    }
-
-    return service;
-}])
-
 /**
  * Search bar along the top of the page.
  * Parent scope for list and detail views
  */
 .controller('SearchCtrl', 
-       ['$scope','$location','egCore','egGridDataProvider','itemSvc',
-function($scope , $location , egCore , egGridDataProvider , itemSvc) {
+       ['$scope','$q','$window','$location','$timeout','egCore','egNet','egGridDataProvider','egItem',
+function($scope , $q , $window , $location , $timeout , egCore , egNet , egGridDataProvider , itemSvc) {
     $scope.args = {}; // search args
 
     // sub-scopes (search / detail-view) apply their version 
@@ -133,14 +67,223 @@ function($scope , $location , egCore , egGridDataProvider , itemSvc) {
         $scope.context.toggleDisplay();
         $event.preventDefault(); // avoid form submission
     }
+
+    // The functions that follow in this controller are never called
+    // when the List View is active, only the Detail View.
+    
+    // In this context, we're only ever dealing with 1 item, so
+    // we can simply refresh the page.  These various itemSvc
+    // functions used to live in the ListCtrl, but they're now
+    // shared between SearchCtrl (for Actions for the Detail View)
+    // and ListCtrl (Actions in the egGrid)
+    itemSvc.add_barcode_to_list = function(b) {
+        //console.log('SearchCtrl: add_barcode_to_list',b);
+        // timeout so audible can happen upon checkin
+        $timeout(function() { location.href = location.href; }, 1000);
+    }
+
+    $scope.add_copies_to_bucket = function() {
+        itemSvc.add_copies_to_bucket([$scope.args.copyId]);
+    }
+
+    $scope.make_copies_bookable = function() {
+        itemSvc.make_copies_bookable([{
+            id : $scope.args.copyId,
+            'call_number.record.id' : $scope.args.recordId
+        }]);
+    }
+
+    $scope.book_copies_now = function() {
+        itemSvc.book_copies_now([{
+            id : $scope.args.copyId,
+            'call_number.record.id' : $scope.args.recordId
+        }]);
+    }
+
+    $scope.findAcquisition = function() {
+        var acqData;
+        var promises = [];
+        $scope.openAcquisitionLineItem([$scope.args.copyId]);
+    }
+
+    $scope.openAcquisitionLineItem = function (cp_list) {
+        var hasResults = false;
+        var promises = [];
+
+        angular.forEach(cp_list, function (copyId) {
+            promises.push(
+                egNet.request(
+                    'open-ils.acq',
+                    'open-ils.acq.lineitem.retrieve.by_copy_id',
+                    egCore.auth.token(),
+                    copyId
+                ).then(function (acqData) {
+                    if (acqData) {
+                        if (acqData.a) {
+                            acqData = egCore.idl.toHash(acqData);
+                            var url = '/eg/acq/po/view/' + acqData.purchase_order + '/' + acqData.id;
+                            $timeout(function () { $window.open(url, '_blank') });
+                            hasResults = true;
+                        }
+                    }
+                })
+            )
+        });
+
+        $q.all(promises).then(function () {
+            !hasResults ? alert('There is no corresponding purchase order for this item.') : false;
+        });
+    }
+
+    $scope.requestItems = function() {
+        itemSvc.requestItems([$scope.args.copyId]);
+    }
+
+    $scope.update_inventory = function() {
+        itemSvc.updateInventory([$scope.args.copyId], null)
+        .then(function(res) {
+            $timeout(function() { location.href = location.href; }, 1000);
+        });
+    }
+
+    $scope.attach_to_peer_bib = function() {
+        itemSvc.attach_to_peer_bib([{
+            id : $scope.args.copyId,
+            barcode : $scope.args.copyBarcode
+        }]);
+    }
+
+    $scope.selectedHoldingsCopyDelete = function () {
+        itemSvc.selectedHoldingsCopyDelete([{
+            id : $scope.args.copyId,
+            barcode : $scope.args.copyBarcode
+        }]);
+    }
+
+    $scope.checkin = function () {
+        itemSvc.checkin([{
+            id : $scope.args.copyId,
+            barcode : $scope.args.copyBarcode
+        }]);
+    }
+
+    $scope.renew = function () {
+        itemSvc.renew([{
+            id : $scope.args.copyId,
+            barcode : $scope.args.copyBarcode
+        }]);
+    }
+
+    $scope.cancel_transit = function () {
+        itemSvc.cancel_transit([{
+            id : $scope.args.copyId,
+            barcode : $scope.args.copyBarcode
+        }]);
+    }
+
+    $scope.selectedHoldingsDamaged = function () {
+        itemSvc.selectedHoldingsDamaged([{
+            id : $scope.args.copyId,
+            barcode : $scope.args.copyBarcode,
+            refresh : true
+        }]);
+    }
+
+    $scope.selectedHoldingsMissing = function () {
+        itemSvc.selectedHoldingsMissing([{
+            id : $scope.args.copyId,
+            barcode : $scope.args.copyBarcode
+        }]);
+    }
+
+    $scope.selectedHoldingsVolCopyAdd = function () {
+        itemSvc.spawnHoldingsAdd([{
+            id : $scope.args.copyId,
+            'call_number.owning_lib' : $scope.args.cnOwningLib,
+            'call_number.record.id' : $scope.args.recordId,
+            barcode : $scope.args.copyBarcode
+        }],true,false);
+    }
+    $scope.selectedHoldingsCopyAdd = function () {
+        itemSvc.spawnHoldingsAdd([{
+            id : $scope.args.copyId,
+            'call_number.id' : $scope.args.cnId,
+            'call_number.owning_lib' : $scope.args.cnOwningLib,
+            'call_number.record.id' : $scope.args.recordId,
+            barcode : $scope.args.copyBarcode
+        }],false,true);
+    }
+
+    $scope.selectedHoldingsVolCopyEdit = function () {
+        itemSvc.spawnHoldingsEdit([{
+            id : $scope.args.copyId,
+            'call_number.id' : $scope.args.cnId,
+            'call_number.owning_lib' : $scope.args.cnOwningLib,
+            'call_number.record.id' : $scope.args.recordId,
+            barcode : $scope.args.copyBarcode
+        }],false,false);
+    }
+    $scope.selectedHoldingsVolEdit = function () {
+        itemSvc.spawnHoldingsEdit([{
+            id : $scope.args.copyId,
+            'call_number.id' : $scope.args.cnId,
+            'call_number.owning_lib' : $scope.args.cnOwningLib,
+            'call_number.record.id' : $scope.args.recordId,
+            barcode : $scope.args.copyBarcode
+        }],false,true);
+    }
+    $scope.selectedHoldingsCopyEdit = function () {
+        itemSvc.spawnHoldingsEdit([{
+            id : $scope.args.copyId,
+            'call_number.id' : $scope.args.cnId,
+            'call_number.owning_lib' : $scope.args.cnOwningLib,
+            'call_number.record.id' : $scope.args.recordId,
+            barcode : $scope.args.copyBarcode
+        }],true,false);
+    }
+
+    $scope.replaceBarcodes = function() {
+        itemSvc.replaceBarcodes([{
+            id : $scope.args.copyId,
+            barcode : $scope.args.copyBarcode
+        }]);
+    }
+
+    $scope.changeItemOwningLib = function() {
+        itemSvc.changeItemOwningLib([{
+            id : $scope.args.copyId,
+            'call_number.id' : $scope.args.cnId,
+            'call_number.owning_lib' : $scope.args.cnOwningLib,
+            'call_number.record.id' : $scope.args.recordId,
+            'call_number.label' : $scope.args.cnLabel,
+            'call_number.label_class' : $scope.args.cnLabelClass,
+            'call_number.prefix.id' : $scope.args.cnPrefixId,
+            'call_number.suffix.id' : $scope.args.cnSuffixId,
+            barcode : $scope.args.copyBarcode
+        }]);
+    }
+
+    $scope.transferItems = function (){
+        itemSvc.transferItems([{
+            id : $scope.args.copyId,
+            barcode : $scope.args.copyBarcode
+        }]);
+    }
+
 }])
 
 /**
  * List view - grid stuff
  */
 .controller('ListCtrl', 
-       ['$scope','$q','$routeParams','$location','$timeout','egCore','egGridDataProvider','itemSvc',
-function($scope , $q , $routeParams , $location , $timeout , egCore , egGridDataProvider , itemSvc) {
+       ['$scope','$q','$routeParams','$location','$timeout','$window','egCore',
+        'egGridDataProvider','egItem','egUser','$uibModal','egCirc','egConfirmDialog',
+        'egProgressDialog', 'ngToast',
+// function($scope , $q , $routeParams , $location , $timeout , $window , egCore , 
+//          egGridDataProvider , itemSvc , egUser , $uibModal , egCirc , egConfirmDialog,
+//          egProgressDialog, ngToast) {
+    function($scope , $q , $routeParams , $location , $timeout , $window , egCore , egGridDataProvider , itemSvc , egUser , $uibModal , egCirc , egConfirmDialog,
+                 egProgressDialog, ngToast) {
     var copyId = [];
     var cp_list = $routeParams.idList;
     if (cp_list) {
@@ -187,12 +330,25 @@ function($scope , $q , $routeParams , $location , $timeout , egCore , egGridData
                 barcodes.push(line);
             });
 
-            itemSvc.fetch(barcodes).then(
-                function() {
+            // Serialize copy retrieval since there may be many, many copies.
+            function fetch_next_copy() {
+                var barcode = barcodes.pop();
+                egProgressDialog.increment();
+
+                if (!barcode) { // All done here.
+                    egProgressDialog.close();
                     copyGrid.refresh();
                     copyGrid.selectItems([itemSvc.copies[0].index]);
+                    return;
                 }
-            );
+
+                itemSvc.fetch(barcode).then(fetch_next_copy);
+            }
+
+            if (barcodes.length) {
+                egProgressDialog.open({value: 0, max: barcodes.length});
+                fetch_next_copy();
+            }
         }
     });
 
@@ -206,11 +362,18 @@ function($scope , $q , $routeParams , $location , $timeout , egCore , egGridData
                 $scope.args.barcode = '';
             } else {
                 $scope.context.itemNotFound = true;
+                egCore.audio.play('warning.item_status.itemNotFound');
             }
             $scope.context.selectBarcode = true;
         })
     }
 
+    var add_barcode_to_list = function (b) {
+        //console.log('listCtrl: add_barcode_to_list',b);
+        $scope.context.search({barcode:b});
+    }
+    itemSvc.add_barcode_to_list = add_barcode_to_list;
+
     $scope.context.toggleDisplay = function() {
         var item = copyGrid.selectedItems()[0];
         if (item) 
@@ -223,6 +386,225 @@ function($scope , $q , $routeParams , $location , $timeout , egCore , egGridData
             $location.path('/cat/item/' + item.id + '/triggered_events');
     }
 
+    function gatherSelectedRecordIds () {
+        var rid_list = [];
+        angular.forEach(
+            copyGrid.selectedItems(),
+            function (item) {
+                if (rid_list.indexOf(item['call_number.record.id']) == -1)
+                    rid_list.push(item['call_number.record.id'])
+            }
+        );
+        return rid_list;
+    }
+
+    function gatherSelectedVolumeIds (rid) {
+        var cn_id_list = [];
+        angular.forEach(
+            copyGrid.selectedItems(),
+            function (item) {
+                if (rid && item['call_number.record.id'] != rid) return;
+                if (cn_id_list.indexOf(item['call_number.id']) == -1)
+                    cn_id_list.push(item['call_number.id'])
+            }
+        );
+        return cn_id_list;
+    }
+
+    function gatherSelectedHoldingsIds (rid) {
+        var cp_id_list = [];
+        angular.forEach(
+            copyGrid.selectedItems(),
+            function (item) {
+                if (rid && item['call_number.record.id'] != rid) return;
+                cp_id_list.push(item.id)
+            }
+        );
+        return cp_id_list;
+    }
+
+    $scope.add_copies_to_bucket = function() {
+        var copy_list = gatherSelectedHoldingsIds();
+        itemSvc.add_copies_to_bucket(copy_list);
+    }
+
+    $scope.locateAcquisition = function() {
+        if (gatherSelectedHoldingsIds) {
+            var cp_list = gatherSelectedHoldingsIds();
+            if (cp_list) {
+                if (cp_list.length > 0) {
+                    $scope.openAcquisitionLineItem(cp_list);
+                }
+            }
+        }
+    }
+
+    $scope.update_inventory = function() {
+        var copy_list = gatherSelectedHoldingsIds();
+        itemSvc.updateInventory(copy_list, $scope.gridControls.allItems()).then(function(res) {
+            if (res) {
+                $scope.gridControls.allItems(res);
+                ngToast.create(egCore.strings.SUCCESS_UPDATE_INVENTORY);
+            } else {
+                ngToast.warning(egCore.strings.FAIL_UPDATE_INVENTORY);
+            }
+        });
+    }
+
+    $scope.need_one_selected = function() {
+        var items = $scope.gridControls.selectedItems();
+        if (items.length == 1) return false;
+        return true;
+    };
+
+    $scope.make_copies_bookable = function() {
+        itemSvc.make_copies_bookable(copyGrid.selectedItems());
+    }
+
+    $scope.book_copies_now = function() {
+        itemSvc.book_copies_now(copyGrid.selectedItems());
+    }
+
+    $scope.requestItems = function() {
+        var copy_list = gatherSelectedHoldingsIds();
+        itemSvc.requestItems(copy_list);
+    }
+
+    $scope.replaceBarcodes = function() {
+        itemSvc.replaceBarcodes(copyGrid.selectedItems());
+    }
+
+    $scope.attach_to_peer_bib = function() {
+        itemSvc.attach_to_peer_bib(copyGrid.selectedItems());
+    }
+
+    $scope.selectedHoldingsCopyDelete = function () {
+        itemSvc.selectedHoldingsCopyDelete(copyGrid.selectedItems());
+    }
+
+    $scope.selectedHoldingsItemStatusTgrEvt= function() {
+        var item = copyGrid.selectedItems()[0];
+        if (item)
+            $location.path('/cat/item/' + item.id + '/triggered_events');
+    }
+
+    $scope.selectedHoldingsItemStatusHolds= function() {
+        var item = copyGrid.selectedItems()[0];
+        if (item)
+            $location.path('/cat/item/' + item.id + '/holds');
+    }
+
+    $scope.cancel_transit = function () {
+        itemSvc.cancel_transit(copyGrid.selectedItems());
+    }
+
+    $scope.selectedHoldingsDamaged = function () {
+        itemSvc.selectedHoldingsDamaged(copyGrid.selectedItems());
+    }
+
+    $scope.selectedHoldingsMissing = function () {
+        itemSvc.selectedHoldingsMissing(copyGrid.selectedItems());
+    }
+
+    $scope.checkin = function () {
+        itemSvc.checkin(copyGrid.selectedItems());
+    }
+
+    $scope.renew = function () {
+        itemSvc.renew(copyGrid.selectedItems());
+    }
+
+    $scope.selectedHoldingsVolCopyAdd = function () {
+        itemSvc.spawnHoldingsAdd(copyGrid.selectedItems(),true,false);
+    }
+    $scope.selectedHoldingsCopyAdd = function () {
+        itemSvc.spawnHoldingsAdd(copyGrid.selectedItems(),false,true);
+    }
+
+    $scope.selectedHoldingsCopyAlertsAdd = function(items) {
+        var copy_ids = [];
+        angular.forEach(items, function(item) {
+            if (item.id) copy_ids.push(item.id);
+        });
+        egCirc.add_copy_alerts(copy_ids).then(function() {
+            // update grid items?
+        });
+    }
+
+    $scope.selectedHoldingsCopyAlertsEdit = function(items) {
+        var copy_ids = [];
+        angular.forEach(items, function(item) {
+            if (item.id) copy_ids.push(item.id);
+        });
+        egCirc.manage_copy_alerts(copy_ids).then(function() {
+            // update grid items?
+        });
+    }
+
+    $scope.gridCellHandlers = {};
+    $scope.gridCellHandlers.copyAlertsEdit = function(id) {
+        egCirc.manage_copy_alerts([id]).then(function() {
+            // update grid items?
+        });
+    };
+
+    $scope.showBibHolds = function () {
+        angular.forEach(gatherSelectedRecordIds(), function (r) {
+            var url = egCore.env.basePath + 'cat/catalog/record/' + r + '/holds';
+            $timeout(function() { $window.open(url, '_blank') });
+        });
+    }
+
+    $scope.selectedHoldingsVolCopyEdit = function () {
+        itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),false,false);
+    }
+    $scope.selectedHoldingsVolEdit = function () {
+        itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),false,true);
+    }
+    $scope.selectedHoldingsCopyEdit = function () {
+        itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),true,false);
+    }
+
+    $scope.changeItemOwningLib = function() {
+        itemSvc.changeItemOwningLib(copyGrid.selectedItems());
+    }
+
+    $scope.transferItems = function (){
+        itemSvc.transferItems(copyGrid.selectedItems());
+    }
+
+    $scope.print_labels = function() {
+        egCore.net.request(
+            'open-ils.actor',
+            'open-ils.actor.anon_cache.set_value',
+            null, 'print-labels-these-copies', {
+                copies : gatherSelectedHoldingsIds()
+            }
+        ).then(function(key) {
+            if (key) {
+                var url = egCore.env.basePath + 'cat/printlabels/' + key;
+                $timeout(function() { $window.open(url, '_blank') });
+            } else {
+                alert('Could not create anonymous cache key!');
+            }
+        });
+    }
+
+    $scope.print_list = function() {
+        var print_data = { copies : copyGrid.allItems() };
+
+        if (print_data.copies.length == 0) return $q.when();
+
+        return egCore.print.print({
+            template : 'item_status',
+            scope : print_data
+        });
+    }
+
+    $scope.show_in_catalog = function(){
+        itemSvc.show_in_catalog(copyGrid.selectedItems());
+    }
+
     if (copyId.length > 0) {
         itemSvc.fetch(null,copyId).then(
             function() {
@@ -237,11 +619,13 @@ function($scope , $q , $routeParams , $location , $timeout , egCore , egGridData
  * Detail view -- shows one copy
  */
 .controller('ViewCtrl', 
-       ['$scope','$q','$location','$routeParams','$timeout','$window','egCore','itemSvc','egBilling',
-function($scope , $q , $location , $routeParams , $timeout , $window , egCore , itemSvc , egBilling) {
+       ['$scope','$q','$location','$routeParams','$timeout','$window','egCore','egItem','egBilling','egCirc',
+function($scope , $q , $location , $routeParams , $timeout , $window , egCore , itemSvc , egBilling , egCirc) {
     var copyId = $routeParams.id;
+    $scope.args.copyId = copyId;
     $scope.tab = $routeParams.tab || 'summary';
     $scope.context.page = 'detail';
+    $scope.summaryRecord = null;
 
     $scope.edit = false;
     if ($scope.tab == 'edit') {
@@ -251,8 +635,20 @@ function($scope , $q , $location , $routeParams , $timeout , $window , egCore ,
 
 
     // use the cached record info
-    if (itemSvc.copy)
-        $scope.summaryRecord = itemSvc.copy.call_number().record();
+    if (itemSvc.copy) {
+        $scope.copy_alert_count = itemSvc.copy.copy_alerts().filter(function(aca) {
+            return !aca.ack_time();
+        }).length;
+        $scope.recordId = itemSvc.copy.call_number().record().id();
+        $scope.args.recordId = $scope.recordId;
+        $scope.args.cnId = itemSvc.copy.call_number().id();
+        $scope.args.cnOwningLib = itemSvc.copy.call_number().owning_lib();
+        $scope.args.cnLabel = itemSvc.copy.call_number().label();
+        $scope.args.cnLabelClass = itemSvc.copy.call_number().label_class();
+        $scope.args.cnPrefixId = itemSvc.copy.call_number().prefix().id();
+        $scope.args.cnSuffixId = itemSvc.copy.call_number().suffix().id();
+        $scope.args.copyBarcode = itemSvc.copy.barcode();
+    }
 
     function loadCopy(barcode) {
         $scope.context.itemNotFound = false;
@@ -265,7 +661,21 @@ function($scope , $q , $location , $routeParams , $timeout , $window , egCore ,
         // regardless of whether it matches the current item.
         if (!barcode && itemSvc.copy && itemSvc.copy.id() == copyId) {
             $scope.copy = itemSvc.copy;
+            if (itemSvc.latest_inventory && itemSvc.latest_inventory.copy() == copyId) {
+                $scope.latest_inventory = itemSvc.latest_inventory;
+            }
+            $scope.copy_alert_count = itemSvc.copy.copy_alerts().filter(function(aca) {
+                return !aca.ack_time();
+            }).length;
             $scope.recordId = itemSvc.copy.call_number().record().id();
+            $scope.args.recordId = $scope.recordId;
+            $scope.args.cnId = itemSvc.copy.call_number().id();
+            $scope.args.cnOwningLib = itemSvc.copy.call_number().owning_lib();
+            $scope.args.cnLabel = itemSvc.copy.call_number().label();
+            $scope.args.cnLabelClass = itemSvc.copy.call_number().label_class();
+            $scope.args.cnPrefixId = itemSvc.copy.call_number().prefix().id();
+            $scope.args.cnSuffixId = itemSvc.copy.call_number().suffix().id();
+            $scope.args.copyBarcode = itemSvc.copy.barcode();
             return $q.when();
         }
 
@@ -279,17 +689,31 @@ function($scope , $q , $location , $routeParams , $timeout , $window , egCore ,
             if (!res) {
                 copyId = null;
                 $scope.context.itemNotFound = true;
+                egCore.audio.play('warning.item_status.itemNotFound');
                 deferred.reject(); // avoid propagation of data fetch calls
                 return;
             }
 
             var copy = res.copy;
             itemSvc.copy = copy;
+            if (res.latest_inventory) itemSvc.latest_inventory = res.latest_inventory;
 
 
             $scope.copy = copy;
+            $scope.latest_inventory = res.latest_inventory;
+            $scope.copy_alert_count = copy.copy_alerts().filter(function(aca) {
+                return !aca.ack_time();
+            }).length;
+console.debug($scope.copy_alert_count);
             $scope.recordId = copy.call_number().record().id();
-            $scope.summaryRecord = itemSvc.copy.call_number().record();
+            $scope.args.recordId = $scope.recordId;
+            $scope.args.cnId = itemSvc.copy.call_number().id();
+            $scope.args.cnOwningLib = itemSvc.copy.call_number().owning_lib();
+            $scope.args.cnLabel = itemSvc.copy.call_number().label();
+            $scope.args.cnLabelClass = itemSvc.copy.call_number().label_class();
+            $scope.args.cnPrefixId = itemSvc.copy.call_number().prefix().id();
+            $scope.args.cnSuffixId = itemSvc.copy.call_number().suffix().id();
+            $scope.args.copyBarcode = copy.barcode();
             $scope.args.barcode = '';
 
             // locally flesh org units
@@ -302,7 +726,7 @@ function($scope , $q , $location , $routeParams , $timeout , $window , egCore ,
 
             // make boolean for auto-magic true/false display
             angular.forEach(
-                ['ref','opac_visible','holdable','floating','circulate'],
+                ['ref','opac_visible','holdable','circulate'],
                 function(field) { copy[field](Boolean(copy[field]() == 't')) }
             );
 
@@ -324,62 +748,85 @@ function($scope , $q , $location , $routeParams , $timeout , $window , egCore ,
         return deferred.promise;
     }
 
-    // if loadPrev load the two most recent circulations
-    function loadCurrentCirc(loadPrev) {
+    // load the two most recent circulations in /circs tab
+    function loadCurrentCirc() {
         delete $scope.circ;
         delete $scope.circ_summary;
         delete $scope.prev_circ_summary;
+        delete $scope.prev_circ_usr;
         if (!copyId) return;
         
-        egCore.pcrud.search('circ', 
-            {target_copy : copyId},
-            {   flesh : 2,
-                flesh_fields : {
-                    circ : [
-                        'usr',
-                        'workstation',                                         
-                        'checkin_workstation',                                 
-                        'duration_rule',                                       
-                        'max_fine_rule',                                       
-                        'recurring_fine_rule'   
-                    ],
-                    au : ['card']
-                },
-                order_by : {circ : 'xact_start desc'}, 
-                limit :  1
+        var copy_org =
+            itemSvc.copy.call_number().id() == -1 ?
+            itemSvc.copy.circ_lib().id() :
+            itemSvc.copy.call_number().owning_lib().id();
+
+        // since a user can still view patron checkout history here, check perms
+        egCore.perm.hasPermAt('VIEW_COPY_CHECKOUT_HISTORY', true)
+        .then(function(orgIds){
+            if(orgIds.indexOf(copy_org) == -1){
+                console.warn('User is not allowed to view circ history!');
+                $q.when(0);
             }
 
-        ).then(null, null, function(circ) {
-            $scope.circ = circ;
+            return fetchMaxCircHistory();
+        })
+        .then(function(maxHistCount){
 
-            // load the chain for this circ
-            egCore.net.request(
-                'open-ils.circ',
-                'open-ils.circ.renewal_chain.retrieve_by_circ.summary',
-                egCore.auth.token(), $scope.circ.id()
-            ).then(function(summary) {
-                $scope.circ_summary = summary.summary;
-            });
+            if (!maxHistCount) $scope.isMaxCircHistoryZero = true;
+
+            egCore.pcrud.search('aacs',
+                {target_copy : copyId},
+                {   flesh : 2,
+                    flesh_fields : {
+                        aacs : [
+                            'usr',
+                            'workstation',
+                            'checkin_workstation',
+                            'duration_rule',
+                            'max_fine_rule',
+                            'recurring_fine_rule'
+                        ],
+                        au : ['card']
+                    },
+                    order_by : {aacs : 'xact_start desc'},
+                    limit :  1
+                }
 
-            if (!loadPrev) return;
+            ).then(null, null, function(circ) {
+                $scope.circ = circ;
 
-            // load the chain for the previous circ, plus the user
-            egCore.net.request(
-                'open-ils.circ',
-                'open-ils.circ.prev_renewal_chain.retrieve_by_circ.summary',
-                egCore.auth.token(), $scope.circ.id()
+                if (!circ) return $q.when();
+
+                // load the chain for this circ
+                egCore.net.request(
+                    'open-ils.circ',
+                    'open-ils.circ.renewal_chain.retrieve_by_circ.summary',
+                    egCore.auth.token(), $scope.circ.id()
+                ).then(function(summary) {
+                    $scope.circ_summary = summary;
+                });
+
+                if (maxHistCount <= 1) return;
+
+                // load the chain for the previous circ, plus the user
+                egCore.net.request(
+                    'open-ils.circ',
+                    'open-ils.circ.prev_renewal_chain.retrieve_by_circ.summary',
+                    egCore.auth.token(), $scope.circ.id()
 
-            ).then(null, null, function(summary) {
-                $scope.prev_circ_summary = summary.summary;
+                ).then(null, null, function(summary) {
+                    $scope.prev_circ_summary = summary.summary;
 
-                egCore.pcrud.retrieve('au', summary.usr,
-                    {flesh : 1, flesh_fields : {au : ['card']}})
+                    if (summary.usr) { // aged circs have no 'usr'.
+                        egCore.pcrud.retrieve('au', summary.usr,
+                            {flesh : 1, flesh_fields : {au : ['card']}})
 
-                .then(function(user) {
-                    $scope.prev_circ_usr = user;
+                        .then(function(user) { $scope.prev_circ_usr = user });
+                    }
                 });
             });
-        });
+        })
     }
 
     var maxHistory;
@@ -389,7 +836,7 @@ function($scope , $q , $location , $routeParams , $timeout , $window , egCore ,
             'circ.item_checkout_history.max')
         .then(function(set) {
             maxHistory = set['circ.item_checkout_history.max'] || 4;
-            return maxHistory;
+            return Number(maxHistory);
         });
     }
 
@@ -403,7 +850,8 @@ function($scope , $q , $location , $routeParams , $timeout , $window , egCore ,
     $scope.retrieveAllPatrons = function() {
         var users = new Set();
         angular.forEach($scope.circ_list.map(function(circ) { return circ.usr(); }),function(usr) {
-            users.add(usr);
+            // aged circs have no 'usr'.
+            if (usr) users.add(usr);
         });
         users.forEach(function(usr) {
             $timeout(function() {
@@ -415,13 +863,14 @@ function($scope , $q , $location , $routeParams , $timeout , $window , egCore ,
         });
     }
 
+    // load data for /circ_list tab
     function loadCircHistory() {
         $scope.circ_list = [];
 
         var copy_org = 
             itemSvc.copy.call_number().id() == -1 ?
             itemSvc.copy.circ_lib().id() :
-            itemSvc.copy.call_number().owning_lib().id()
+            itemSvc.copy.call_number().owning_lib().id();
 
         // there is an extra layer of permissibility over circ
         // history views
@@ -435,26 +884,31 @@ function($scope , $q , $location , $routeParams , $timeout , $window , egCore ,
 
             return fetchMaxCircHistory();
 
-        }).then(function(count) {
+        }).then(function(maxHistCount) {
 
-            egCore.pcrud.search('circ', 
+            if(!maxHistCount) $scope.isMaxCircHistoryZero = true;
+
+            egCore.pcrud.search('aacs',
                 {target_copy : copyId},
                 {   flesh : 2,
                     flesh_fields : {
-                        circ : [
+                        aacs : [
                             'usr',
-                            'workstation',                                         
-                            'checkin_workstation',                                 
-                            'recurring_fine_rule'   
+                            'workstation',
+                            'checkin_workstation',
+                            'recurring_fine_rule'
                         ],
                         au : ['card']
                     },
-                    order_by : {circ : 'xact_start desc'}, 
-                    limit :  count
+                    order_by : {aacs : 'xact_start desc'},
+                    // fetch at least one to see if copy ever circulated
+                    limit : $scope.isMaxCircHistoryZero ? 1 : maxHistCount
                 }
 
             ).then(null, null, function(circ) {
 
+                $scope.circ = circ;
+
                 // flesh circ_lib locally
                 circ.circ_lib(egCore.org.get(circ.circ_lib()));
                 circ.checkin_lib(egCore.org.get(circ.checkin_lib()));
@@ -486,15 +940,29 @@ function($scope , $q , $location , $routeParams , $timeout , $window , egCore ,
                 return c.year() == new Date().getFullYear();
             });
 
-            $scope.total_circs_this_year = 
-                this_year.length ? this_year[0].count() : 0;
+            $scope.total_circs_this_year = (function() {
+                total = 0;
+                if (this_year.length == 2) {
+                    total = (Number(this_year[0].count()) + Number(this_year[1].count()));
+                } else if (this_year.length == 1) {
+                    total = Number(this_year[0].count());
+                }
+                return total;
+            })();
 
             var prev_year = counts.filter(function(c) {
                 return c.year() == new Date().getFullYear() - 1;
             });
 
-            $scope.total_circs_prev_year = 
-                prev_year.length ? prev_year[0].count() : 0;
+            $scope.total_circs_prev_year = (function() {
+                total = 0;
+                if (prev_year.length == 2) {
+                    total = (Number(prev_year[0].count()) + Number(prev_year[1].count()));
+                } else if (prev_year.length == 1) {
+                    total = Number(prev_year[0].count());
+                }
+                return total;
+            })();
 
         });
     }
@@ -526,16 +994,20 @@ function($scope , $q , $location , $routeParams , $timeout , $window , egCore ,
         });
     }
 
-    function loadTransits() {
+    function loadMostRecentTransit() {
         delete $scope.transit;
         delete $scope.hold_transit;
         if (!copyId) return;
 
         egCore.pcrud.search('atc', 
             {target_copy : copyId},
-            {order_by : {atc : 'source_send_time DESC'}}
+            {
+                order_by : {atc : 'source_send_time DESC'},
+                limit : 1
+            }
 
         ).then(null, null, function(transit) {
+            // use progress callback since we'll get up to one result
             $scope.transit = transit;
             transit.source(egCore.org.get(transit.source()));
             transit.dest(egCore.org.get(transit.dest()));
@@ -552,7 +1024,7 @@ function($scope , $q , $location , $routeParams , $timeout , $window , egCore ,
                 break;
 
             case 'circs':
-                loadCurrentCirc(true);
+                loadCurrentCirc();
                 break;
 
             case 'circ_list':
@@ -561,7 +1033,7 @@ function($scope , $q , $location , $routeParams , $timeout , $window , egCore ,
 
             case 'holds':
                 loadHolds()
-                loadTransits();
+                loadMostRecentTransit();
                 break;
 
             case 'triggered_events':
@@ -594,6 +1066,19 @@ function($scope , $q , $location , $routeParams , $timeout , $window , egCore ,
         return;
     }
 
+    $scope.addCopyAlerts = function(copy_id) {
+        egCirc.add_copy_alerts([copy_id]).then(function() {
+            // force a refresh
+            loadCopy($scope.copy.barcode()).then(loadTabData);
+        });
+    }
+    $scope.manageCopyAlerts = function(copy_id) {
+        egCirc.manage_copy_alerts([copy_id]).then(function() {
+            // force a refresh
+            loadCopy($scope.copy.barcode()).then(loadTabData);
+        });
+    }
+
     $scope.context.toggleDisplay = function() {
         $location.path('/cat/item/search');
     }