LP #1705497 Replaces functionality in web client from legacy
[evergreen-equinox.git] / Open-ILS / web / js / ui / default / staff / cat / item / app.js
index 8b8f9ef..26f21ba 100644 (file)
@@ -52,8 +52,8 @@ angular.module('egItemStatus',
  * Parent scope for list and detail views
  */
 .controller('SearchCtrl', 
-       ['$scope','$location','$timeout','egCore','egGridDataProvider','egItem',
-function($scope , $location , $timeout , 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 
@@ -100,10 +100,52 @@ function($scope , $location , $timeout , egCore , egGridDataProvider , itemSvc)
         }]);
     }
 
+    $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,
@@ -234,8 +276,14 @@ function($scope , $location , $timeout , egCore , egGridDataProvider , itemSvc)
  * List view - grid stuff
  */
 .controller('ListCtrl', 
-       ['$scope','$q','$routeParams','$location','$timeout','$window','egCore','egGridDataProvider','egItem','egUser','$uibModal','egCirc','egConfirmDialog',
-function($scope , $q , $routeParams , $location , $timeout , $window , egCore , egGridDataProvider , itemSvc , egUser , $uibModal , egCirc , egConfirmDialog) {
+       ['$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) {
@@ -282,18 +330,24 @@ function($scope , $q , $routeParams , $location , $timeout , $window , egCore ,
                 barcodes.push(line);
             });
 
-            if (barcodes.length > 0) {
-                var promises = [];
-                angular.forEach(barcodes, function (b) {
-                    promises.push(itemSvc.fetch(b));
-                });
+            // Serialize copy retrieval since there may be many, many copies.
+            function fetch_next_copy() {
+                var barcode = barcodes.pop();
+                egProgressDialog.increment();
 
-                $q.all(promises).then(
-                    function() {
-                        copyGrid.refresh();
-                        copyGrid.selectItems([itemSvc.copies[0].index]);
-                    }
-                );
+                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();
             }
         }
     });
@@ -374,6 +428,29 @@ function($scope , $q , $routeParams , $location , $timeout , $window , egCore ,
         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;
@@ -444,6 +521,33 @@ function($scope , $q , $routeParams , $location , $timeout , $window , egCore ,
         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';
@@ -497,6 +601,10 @@ function($scope , $q , $routeParams , $location , $timeout , $window , egCore ,
         });
     }
 
+    $scope.show_in_catalog = function(){
+        itemSvc.show_in_catalog(copyGrid.selectedItems());
+    }
+
     if (copyId.length > 0) {
         itemSvc.fetch(null,copyId).then(
             function() {
@@ -511,8 +619,8 @@ function($scope , $q , $routeParams , $location , $timeout , $window , egCore ,
  * Detail view -- shows one copy
  */
 .controller('ViewCtrl', 
-       ['$scope','$q','$location','$routeParams','$timeout','$window','egCore','egItem','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';
@@ -528,6 +636,9 @@ function($scope , $q , $location , $routeParams , $timeout , $window , egCore ,
 
     // use the cached record info
     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();
@@ -550,6 +661,12 @@ 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();
@@ -579,9 +696,15 @@ function($scope , $q , $location , $routeParams , $timeout , $window , egCore ,
 
             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.args.recordId = $scope.recordId;
             $scope.args.cnId = itemSvc.copy.call_number().id();
@@ -817,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;
+            })();
 
         });
     }
@@ -857,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()));
@@ -892,7 +1033,7 @@ function($scope , $q , $location , $routeParams , $timeout , $window , egCore ,
 
             case 'holds':
                 loadHolds()
-                loadTransits();
+                loadMostRecentTransit();
                 break;
 
             case 'triggered_events':
@@ -925,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');
     }