]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/renew/app.js
LP#1350042 Browser client templates/scripts (phase 1)
[Evergreen.git] / Open-ILS / web / js / ui / default / staff / circ / renew / app.js
1 /**
2  * Renewal
3  */
4
5 angular.module('egRenewApp', 
6     ['ngRoute', 'ui.bootstrap', 'egCoreMod', 'egUiMod', 'egGridMod'])
7
8 .config(function($routeProvider, $locationProvider, $compileProvider) {
9     $locationProvider.html5Mode(true);
10     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/); // grid export
11     var resolver = {delay : function(egStartup) {return egStartup.go()}};
12
13     $routeProvider.when('/circ/renew/renew', {
14         templateUrl: './circ/renew/t_renew',
15         controller: 'RenewCtrl',
16         resolve : resolver
17     });
18
19     $routeProvider.when('/circ/renew/renew', {
20         templateUrl: './circ/renew/t_renew',
21         controller: 'RenewCtrl',
22         resolve : resolver
23     });
24     
25     $routeProvider.otherwise({redirectTo : '/circ/renew/renew'});
26 })
27
28
29
30
31 .controller('RenewCtrl',
32        ['$scope','$window','$location','egCore','egGridDataProvider','egCirc',
33 function($scope , $window , $location , egCore , egGridDataProvider , egCirc) {
34
35     $scope.focusBarcode = true;
36     $scope.renewals = [];
37
38     var today = new Date();
39     $scope.renewalArgs = {due_date : today};
40
41     $scope.gridDataProvider = egGridDataProvider.instance({
42         get : function(offset, count) {
43             return this.arrayNotifier($scope.renewals, offset, count);
44         }
45     });
46
47     // avoid multiple, in-flight attempts on the same barcode
48     var pending_barcodes = {};
49
50     $scope.renew = function(args) {
51         var params = angular.copy(args);
52
53         if (args.sticky_date) {
54             params.due_date = args.due_date.toISOString();
55         } else {
56             delete params.due_date;
57         }
58         delete params.sticky_date;
59          if (!args.copy_barcode) return;
60
61         args.copy_barcode = ''; // reset UI input
62
63         if (pending_barcodes[params.copy_barcode]) {
64             console.log(
65                 "Skipping renewals of redundant barcode " 
66                 + params.copy_barcode
67             );
68             return;
69         }
70
71         pending_barcodes[params.copy_barcode] = true;
72         send_renewal(params);
73
74         $scope.focusBarcode = true; // return focus to barcode input
75     }
76
77     function send_renewal(params) {
78
79         params.noncat_type = params.noncat ? params.noncat_type : '';
80
81         // populate the grid row before we send the request so that the
82         // order of actions is maintained and so the user gets an 
83         // immediate reaction to their barcode input action.
84         var row_item = {
85             index : $scope.renewals.length,
86             copy_barcode : params.copy_barcode,
87             noncat_type : params.noncat_type
88         };
89
90         $scope.renewals.unshift(row_item);
91         $scope.gridDataProvider.refresh();
92
93         var options = {check_barcode : $scope.strict_barcode};
94
95         egCirc.renew(params, options).then(
96             function(final_resp) {
97
98                 row_item.evt = final_resp.evt;
99                 angular.forEach(final_resp.data, function(val, key) {
100                     row_item[key] = val;
101                 });
102
103                 if (row_item.mbts) {
104                     var amt = Number(row_item.mbts.balance_owed());
105                     if (amt != 0) {
106                         $scope.billable_barcode = row_item.copy_barcode;
107                         $scope.billable_amount = amt;
108                         $scope.fine_total = 
109                             ($scope.fine_total * 100 + amt * 100) / 100;
110                     }
111                 }
112
113                 if ($scope.trim_list && checkinSvc.checkins.length > 20)
114                     checkinSvc.checkins = checkinSvc.checkins.splice(0, 20);
115
116             },
117             function() {
118                 // Circ was rejected somewhere along the way.
119                 // Remove the copy from the grid since there was no action.
120                 // note: since renewals are unshifted onto the array, the
121                 // index value does not (generally) match the array position.
122                 var pos = -1;
123                 angular.forEach($scope.renewals, function(co, idx) {
124                     if (co.index == row_item.index) pos = idx;
125                 });
126                 $scope.renewals.splice(pos, 1);
127                 $scope.gridDataProvider.refresh();
128             }
129
130         )['finally'](function() {
131
132             // regardless of the outcome of the circ, remove the 
133             // barcode from the pending list.
134             if (params.copy_barcode)
135                 delete pending_barcodes[params.copy_barcode];
136         });
137     }
138
139     $scope.fetchLastCircPatron = function(items) {
140         var renewal = items[0];
141         if (!renewal || !renewal.acp) return;
142
143         egCirc.last_copy_circ(renewal.acp.id())
144         .then(function(circ) {
145
146             if (circ) {
147                 // jump to the patron UI (separate app)
148                 $window.location.href = $location
149                     .path('/circ/patron/' + circ.usr() + '/checkout')
150                     .absUrl();
151                 return;
152             }
153
154             $scope.alert = {item_never_circed : renewal.acp.barcode()};
155         });
156     }
157
158     $scope.showMarkDamaged = function(items) {
159         var copy_ids = [];
160         angular.forEach(items, function(item) {
161             if (item.acp) copy_ids.push(item.acp.id());
162         });
163
164         if (copy_ids.length) {
165             egCirc.mark_damaged(copy_ids).then(function() {
166                 // update grid items?
167             });
168         }
169     }
170
171     $scope.showLastFewCircs = function(items) {
172         if (items.length && (copy = items[0].acp)) {
173             var url = $location.path(
174                 '/cat/item/' + copy.id() + '/circ_list').absUrl();
175             $window.open(url, '_blank').focus();
176         }
177     }
178
179     $scope.abortTransit = function(items) {
180         var transit_ids = [];
181         angular.forEach(items, function(item) {
182             if (item.transit) transit_ids.push(item.transit.id());
183         });
184
185         egCirc.abort_transits(transit_ids).then(function() {
186             // update grid items?
187         });
188     }
189
190     $scope.print_receipt = function() {
191         var print_data = {circulations : []}
192
193         if ($scope.renewals.length == 0) return $q.when();
194
195         angular.forEach($scope.renewals, function(renewal) {
196             if (renewal.circ) {
197                 print_data.circulations.push({
198                     circ : egCore.idl.toHash(renewal.circ),
199                     copy : egCore.idl.toHash(renewal.acp),
200                     title : egCore.idl.toHash(renewal.title),
201                     author : egCore.idl.toHash(renewal.author)
202                 });
203             }
204         });
205
206         return egCore.print.print({
207             context : 'default', 
208             template : 'renew', 
209             scope : print_data,
210         });
211     }
212 }])
213