]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/checkin/app.js
LP#1402797 webby: checkin fine tally patron bills link
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / circ / checkin / app.js
1 angular.module('egCheckinApp', ['ngRoute', 'ui.bootstrap', 
2     'egCoreMod', 'egUiMod', 'egGridMod', 'egUserMod'])
3
4 .config(function($routeProvider, $locationProvider, $compileProvider) {
5     $locationProvider.html5Mode(true);
6     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/); // grid export
7
8     var resolver = {delay : 
9         ['egStartup', function(egStartup) {return egStartup.go()}]}
10
11     $routeProvider.when('/circ/checkin/checkin', {
12         templateUrl: './circ/checkin/t_checkin',
13         controller: 'CheckinCtrl',
14         resolve : resolver
15     });
16
17     $routeProvider.when('/circ/checkin/capture', {
18         templateUrl: './circ/checkin/t_checkin',
19         controller: 'CheckinCtrl',
20         resolve : resolver
21     });
22
23     $routeProvider.otherwise({redirectTo : '/circ/checkin/checkin'});
24 })
25
26 .factory('checkinSvc', [function() {
27     var service = {};
28     service.checkins = [];
29     return service;
30 }])
31
32
33 /**
34  * Manages checkin
35  */
36 .controller('CheckinCtrl',
37        ['$scope','$q','$window','$location','egCore','checkinSvc','egGridDataProvider','egCirc',
38 function($scope , $q , $window , $location , egCore , checkinSvc , egGridDataProvider , egCirc)  {
39
40     $scope.focusMe = true;
41     $scope.checkins = checkinSvc.checkins;
42     var today = new Date();
43     $scope.checkinArgs = {backdate : today}
44     $scope.using_hatch = egCore.hatch.usingHatch();
45     $scope.modifiers = {};
46     $scope.fine_total = 0;
47     $scope.is_capture = $location.path().match(/capture$/);
48     var suppress_popups = false;
49     $scope.grid_persist_key = $scope.is_capture ? 
50         'circ.checkin.capture' : 'circ.checkin.checkin';
51
52     egCore.org.settings([
53         'ui.circ.suppress_checkin_popups' // add other settings as needed
54     ]).then(function(set) {
55         suppress_popups = set['ui.circ.suppress_checkin_popups'];
56     });
57
58     // checkin & hold capture modifiers
59     var modifiers = [
60         'void_overdues', 
61         'clear_expired',
62         'hold_as_transit',
63         'manual_float',
64         'no_precat_alert',
65         'retarget_holds',
66         'retarget_holds_all'
67     ];
68
69     if ($scope.is_capture) {
70         // in hold capture mode, some values are forced, regardless
71         // of stored preferences.
72         $scope.modifiers.noop = false;
73         $scope.modifiers.auto_print_holds_transits = true;
74     } else {
75         modifiers.push('noop'); // AKA suppress holds and transits
76         modifiers.push('auto_print_holds_transits');
77     }
78
79     // set modifiers from stored preferences
80     angular.forEach(modifiers, function(mod) {
81         egCore.hatch.getItem('eg.circ.checkin.' + mod)
82         .then(function(val) { if (val) $scope.modifiers[mod] = true });
83     });
84
85     // set / unset a checkin modifier
86     // when set, store the preference
87     $scope.toggle_mod = function(mod) {
88         if ($scope.modifiers[mod]) {
89             $scope.modifiers[mod] = false;
90             egCore.hatch.removeItem('eg.circ.checkin.' + mod);
91         } else {
92             $scope.modifiers[mod] = true;
93             egCore.hatch.setItem('eg.circ.checkin.' + mod, true);
94         }
95     }
96
97
98     // ensure the backdate is not in the future
99     // note: input type=date max=foo not yet supported anywhere
100     $scope.$watch('checkinArgs.backdate', function(newval) {
101         if (newval && newval > today) 
102             $scope.checkinArgs.backdate = today;
103     });
104
105     $scope.is_backdate = function() {
106         return $scope.checkinArgs.backdate < today;
107     }
108
109     var checkinGrid = $scope.gridControls = {};
110
111     $scope.gridDataProvider = egGridDataProvider.instance({
112         get : function(offset, count) {
113             return this.arrayNotifier($scope.checkins, offset, count);
114         }
115     });
116
117     // turns the various inputs (form args, modifiers, etc.) into
118     // checkin params and options.
119     function compile_checkin_args(args) {
120         var params = angular.copy(args);
121
122         if (params.backdate) {
123             params.backdate = 
124                 params.backdate.toISOString().replace(/T.*/,'');
125
126             // a backdate of 'today' is not really a backdate
127             if (params.backdate == $scope.max_backdate)
128                 delete params.backdate;
129         }
130
131         angular.forEach(['noop','void_overdues',
132                 'clear_expired','hold_as_transit','manual_float'],
133             function(opt) {
134                 if ($scope.modifiers[opt]) params[opt] = true;
135             }
136         );
137
138         if ($scope.modifiers.retarget_holds) {
139             if ($scope.modifiers.retarget_holds_all) {
140                 params.retarget_mode = 'retarget.all';
141             } else {
142                 params.retarget_mode = 'retarget';
143             }
144         }
145
146         var options = {
147             check_barcode : $scope.strict_barcode,
148             no_precat_alert : $scope.modifiers.no_precat_alert,
149             auto_print_holds_transits : 
150                 $scope.modifiers.auto_print_holds_transits,
151             suppress_popups : suppress_popups
152         };
153
154         return {params : params, options: options};
155     }
156
157     $scope.checkin = function(args) {
158
159         var compiled = compile_checkin_args(args);
160         args.copy_barcode = ''; // reset UI for next scan
161         $scope.focusMe = true;
162         delete $scope.alert;
163         delete $scope.billable_amount;
164         delete $scope.billable_barcode;
165         delete $scope.billable_user_id;
166
167         var params = compiled.params;
168         var options = compiled.options;
169
170         if (!params.copy_barcode) return;
171         delete $scope.alert;
172
173         var row_item = {
174             index : checkinSvc.checkins.length,
175             copy_barcode : params.copy_barcode
176         };
177
178         // track the item in the grid before sending the request
179         checkinSvc.checkins.unshift(row_item);
180         checkinGrid.refresh();
181
182         egCirc.checkin(params, options).then(
183         function(final_resp) {
184
185             row_item.evt = final_resp.evt;
186             angular.forEach(final_resp.data, function(val, key) {
187                 row_item[key] = val;
188             });
189
190             if (row_item.mbts) {
191                 var amt = Number(row_item.mbts.balance_owed());
192                 if (amt != 0) {
193                     $scope.billable_barcode = row_item.copy_barcode;
194                     $scope.billable_amount = amt;
195                     $scope.billable_user_id = row_item.circ.usr();
196                     $scope.fine_total = 
197                         ($scope.fine_total * 100 + amt * 100) / 100;
198                 }
199             }
200
201             if (final_resp.evt.textcode == 'NO_CHANGE') {
202                 $scope.alert = 
203                     {already_checked_in : final_resp.evt.copy_barcode};
204             }
205
206             if ($scope.trim_list && checkinSvc.checkins.length > 20)
207                 checkinSvc.checkins = checkinSvc.checkins.splice(0, 20);
208         },
209         function() {
210             // Checkin was rejected somewhere along the way.
211             // Remove the copy from the grid since there was no action.
212             // note: since checkins are unshifted onto the array, the
213             // index value does not (generally) match the array position.
214             var pos = -1;
215             angular.forEach(checkinSvc.checkins, function(ci, idx) {
216                 if (ci.index == row_item.index) pos = idx;
217             });
218             checkinSvc.checkin.splice(pos, 1);
219
220         })['finally'](function() {
221
222             // when all is said and done, refresh the grid and refocus
223             checkinGrid.refresh();
224             $scope.focusMe = true;
225         });
226     }
227
228     $scope.print_receipt = function() {
229         var print_data = {checkins : []}
230
231         if (checkinSvc.checkins.length == 0) return $q.when();
232
233         angular.forEach(checkinSvc.checkins, function(checkin) {
234
235             var checkin = {
236                 copy : egCore.idl.toHash(checkin.acp) || {},
237                 call_number : egCore.idl.toHash(checkin.acn) || {},
238                 copy_barcode : checkin.copy_barcode,
239                 title : checkin.title,
240                 author : checkin.author
241             }
242
243             print_data.checkins.push(checkin);
244         });
245
246         return egCore.print.print({
247             template : 'checkin', 
248             scope : print_data,
249             show_dialog : $scope.show_print_dialog
250         });
251     }
252
253
254     // --- context menu actions
255     //
256     $scope.fetchLastCircPatron = function(items) {
257         var checkin = items[0];
258         if (!checkin || !checkin.acp) return;
259
260         egCirc.last_copy_circ(checkin.acp.id())
261         .then(function(circ) {
262
263             if (circ) {
264                 // jump to the patron UI (separate app)
265                 $window.location.href = $location
266                     .path('/circ/patron/' + circ.usr() + '/checkout')
267                     .absUrl();
268                 return;
269             }
270
271             $scope.alert = {item_never_circed : checkin.acp.barcode()};
272         });
273     }
274
275     $scope.showBackdateDialog = function(items) {
276         var circ_ids = [];
277
278         angular.forEach(items, function(item) {
279             if (item.circ) circ_ids.push(item.circ.id());
280         });
281
282         if (circ_ids.length) {
283             egCirc.backdate_dialog(circ_ids).then(function(result) {
284                 angular.forEach(items, function(item) {
285                     item.circ.checkin_time(result.backdate);
286                 })
287             });
288             // TODO: support grid row styling
289             checkinGrid.refresh();
290         }
291     }
292
293     $scope.showMarkDamaged = function(items) {
294         var copy_ids = [];
295         angular.forEach(items, function(item) {
296             if (item.acp) copy_ids.push(item.acp.id());
297         });
298
299         if (copy_ids.length) {
300             egCirc.mark_damaged(copy_ids).then(function() {
301                 // update grid items?
302             });
303         }
304     }
305
306     $scope.abortTransit = function(items) {
307         var transit_ids = [];
308         angular.forEach(items, function(item) {
309             if (item.transit) transit_ids.push(item.transit.id());
310         });
311
312         egCirc.abort_transits(transit_ids).then(function() {
313             // update grid items?
314         });
315     }
316
317 }])
318