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