webstaff: Make Items Bookable and Book Item Now for Holdings View
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / cat / catalog / app.js
index 3ab19cc..1f1ac58 100644 (file)
@@ -243,13 +243,12 @@ function($scope , $routeParams , $location , $window , $q , egCore) {
     
 
 }])
-
 .controller('CatalogCtrl',
        ['$scope','$routeParams','$location','$window','$q','egCore','egHolds','egCirc','egConfirmDialog','ngToast',
-        'egGridDataProvider','egHoldGridActions','$timeout','$uibModal','holdingsSvc','egUser','conjoinedSvc',
+        'egGridDataProvider','egHoldGridActions','egProgressDialog','$timeout','$uibModal','holdingsSvc','egUser','conjoinedSvc',
         '$cookies',
 function($scope , $routeParams , $location , $window , $q , egCore , egHolds , egCirc , egConfirmDialog , ngToast ,
-         egGridDataProvider , egHoldGridActions , $timeout , $uibModal , holdingsSvc , egUser , conjoinedSvc,
+         egGridDataProvider , egHoldGridActions , egProgressDialog , $timeout , $uibModal , holdingsSvc , egUser , conjoinedSvc,
          $cookies
 ) {
 
@@ -388,9 +387,11 @@ function($scope , $routeParams , $location , $window , $q , egCore , egHolds , e
         $scope.current_overlay_target     = null;
         $scope.current_voltransfer_target = null;
         $scope.current_conjoined_target   = null;
+        $scope.current_hold_transfer_dest = null;
         egCore.hatch.removeLocalItem('eg.cat.marked_volume_transfer_record');
         egCore.hatch.removeLocalItem('eg.cat.marked_conjoined_record');
         egCore.hatch.removeLocalItem('eg.cat.marked_overlay_record');
+        egCore.hatch.removeLocalItem('eg.circ.hold.title_transfer_target');
     }
 
     $scope.stop_unload = false;
@@ -634,6 +635,140 @@ function($scope , $routeParams , $location , $window , $q , egCore , egHolds , e
         });
     }
 
+    // TODO: refactor common code between cat/catalog/app.js and cat/item/app.js 
+
+    $scope.need_one_selected = function() {
+        var items = $scope.holdingsGridControls.selectedItems();
+        if (items.length == 1) return false;
+        return true;
+    };
+
+    $scope.make_copies_bookable = function() {
+
+        var copies_by_record = {};
+        var record_list = [];
+        angular.forEach(
+            $scope.holdingsGridControls.selectedItems(),
+            function (item) {
+                var record_id = item['call_number.record.id'];
+                if (typeof copies_by_record[ record_id ] == 'undefined') {
+                    copies_by_record[ record_id ] = [];
+                    record_list.push( record_id );
+                }
+                copies_by_record[ record_id ].push(item.id);
+            }
+        );
+
+        var promises = [];
+        var combined_results = [];
+        angular.forEach(record_list, function(record_id) {
+            promises.push(
+                egCore.net.request(
+                    'open-ils.booking',
+                    'open-ils.booking.resources.create_from_copies',
+                    egCore.auth.token(),
+                    copies_by_record[record_id]
+                ).then(function(results) {
+                    if (results && results['brsrc']) {
+                        combined_results = combined_results.concat(results['brsrc']);
+                    }
+                })
+            );
+        });
+
+        $q.all(promises).then(function() {
+            if (combined_results.length > 0) {
+                $uibModal.open({
+                    template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
+                    animation: true,
+                    size: 'md',
+                    controller:
+                           ['$scope','$location','egCore','$uibModalInstance',
+                    function($scope , $location , egCore , $uibModalInstance) {
+
+                        $scope.funcs = {
+                            ses : egCore.auth.token(),
+                            resultant_brsrc : combined_results.map(function(o) { return o[0]; })
+                        }
+
+                        var booking_path = '/eg/conify/global/booking/resource';
+
+                        $scope.booking_admin_url =
+                            $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
+                    }]
+                });
+            }
+        });
+    }
+
+    $scope.book_copies_now = function() {
+        var copies_by_record = {};
+        var record_list = [];
+        angular.forEach(
+            $scope.holdingsGridControls.selectedItems(),
+            function (item) {
+                var record_id = item['call_number.record.id'];
+                if (typeof copies_by_record[ record_id ] == 'undefined') {
+                    copies_by_record[ record_id ] = [];
+                    record_list.push( record_id );
+                }
+                copies_by_record[ record_id ].push(item.id);
+            }
+        );
+
+        var promises = [];
+        var combined_brt = [];
+        var combined_brsrc = [];
+        angular.forEach(record_list, function(record_id) {
+            promises.push(
+                egCore.net.request(
+                    'open-ils.booking',
+                    'open-ils.booking.resources.create_from_copies',
+                    egCore.auth.token(),
+                    copies_by_record[record_id]
+                ).then(function(results) {
+                    if (results && results['brt']) {
+                        combined_brt = combined_brt.concat(results['brt']);
+                    }
+                    if (results && results['brsrc']) {
+                        combined_brsrc = combined_brsrc.concat(results['brsrc']);
+                    }
+                })
+            );
+        });
+
+        $q.all(promises).then(function() {
+            if (combined_brt.length > 0 || combined_brsrc.length > 0) {
+                $uibModal.open({
+                    template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
+                    animation: true,
+                    size: 'md',
+                    controller:
+                           ['$scope','$location','egCore','$uibModalInstance',
+                    function($scope , $location , egCore , $uibModalInstance) {
+
+                        $scope.funcs = {
+                            ses : egCore.auth.token(),
+                            bresv_interface_opts : {
+                                booking_results : {
+                                     brt : combined_brt
+                                    ,brsrc : combined_brsrc
+                                }
+                            }
+                        }
+
+                        var booking_path = '/eg/booking/reservation';
+
+                        $scope.booking_admin_url =
+                            $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
+
+                    }]
+                });
+            }
+        });
+    }
+
+
     $scope.requestItems = function() {
         var copy_list = gatherSelectedHoldingsIds();
         if (copy_list.length == 0) return;
@@ -1353,6 +1488,7 @@ function($scope , $routeParams , $location , $window , $q , egCore , egHolds , e
     var hold_ids = []; // current list of holds
     function fetchHolds(offset, count) {
         var ids = hold_ids.slice(offset, offset + count);
+
         return egHolds.fetch_holds(ids).then(null, null,
             function(hold_data) { 
                 return hold_data;
@@ -1365,6 +1501,9 @@ function($scope , $routeParams , $location , $window , $q , egCore , egHolds , e
         var deferred = $q.defer();
         hold_ids = []; // no caching ATM
 
+        // open a determinate progress dialog, max value set below.
+        egProgressDialog.open({max : 1, value : 0});
+
         // fetch the IDs
         egCore.net.request(
             'open-ils.circ',
@@ -1376,8 +1515,22 @@ function($scope , $routeParams , $location , $window , $q , egCore , egHolds , e
                 angular.forEach(hold_data, function(list, type) {
                     hold_ids = hold_ids.concat(list);
                 });
-                fetchHolds(offset, count).then(
-                    deferred.resolve, null, deferred.notify);
+
+                // Set the max value of the progress bar to the lesser of
+                // the total number of holds to fetch or the page size
+                // of the grid.
+                egProgressDialog.update(
+                    {max : Math.min(hold_ids.length, count)});
+
+                var holds_fetched = 0;
+                fetchHolds(offset, count)
+                .then(deferred.resolve, null, 
+                    function(hold_data) {
+                        holds_fetched++;
+                        deferred.notify(hold_data);
+                        egProgressDialog.increment();
+                    }
+                )['finally'](egProgressDialog.close);
             }
         );
 
@@ -1423,7 +1576,10 @@ function($scope , $routeParams , $location , $window , $q , egCore , egHolds , e
         });
     }
 
+    $scope.current_hold_transfer_dest = egCore.hatch.getLocalItem ('eg.circ.hold.title_transfer_target');
+
     $scope.mark_hold_transfer_dest = function() {
+        $scope.current_hold_transfer_dest = $scope.record_id;
         egCore.hatch.setLocalItem(
             'eg.circ.hold.title_transfer_target', $scope.record_id);
         ngToast.create(egCore.strings.HOLD_TRANSFER_DEST_MARKED);
@@ -1504,6 +1660,7 @@ function($scope , $routeParams , $location , $window , $q , egCore , egHolds , e
                 $scope.detail_hold_record_id = $scope.record_id; 
                 // refresh the holds grid
                 provider.refresh();
+
                 break;
         }
     }