]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/checkin/app.js
LP#1791340 Webstaff: Don't backdate when we're not
[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?|mailto|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', '$timeout','egCore','checkinSvc','egGridDataProvider','egCirc', 'egItem',
38 function($scope , $q , $window , $location , $timeout , egCore , checkinSvc , egGridDataProvider , egCirc, itemSvc)  {
39
40     $scope.focusMe = true;
41     $scope.checkins = checkinSvc.checkins;
42     var today = new Date();
43     $scope.checkinArgs = {backdate : today}
44     $scope.using_hatch_printer = egCore.hatch.usePrinting();
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     // TODO: add this to the setting batch lookup below
53     egCore.hatch.getItem('circ.checkin.strict_barcode')
54         .then(function(sb){ $scope.strict_barcode = sb });
55
56     egCore.org.settings([
57         'ui.circ.suppress_checkin_popups' // add other settings as needed
58     ]).then(function(set) {
59         suppress_popups = set['ui.circ.suppress_checkin_popups'];
60     });
61
62     $scope.sort_money = function (a,b) {
63         var ma = parseFloat(a);
64         var mb = parseFloat(b);
65         if (ma < mb) return -1;
66         if (ma > mb) return 1;
67         return 0
68     }
69
70     // checkin & hold capture modifiers
71     var modifiers = [
72         'void_overdues', 
73         'clear_expired',
74         'hold_as_transit',
75         'manual_float',
76         'no_precat_alert',
77         'retarget_holds',
78         'retarget_holds_all'
79     ];
80
81     if ($scope.is_capture) {
82         // in hold capture mode, some values are forced, regardless
83         // of stored preferences.
84         $scope.modifiers.noop = false;
85         $scope.modifiers.auto_print_holds_transits = true;
86     } else {
87         modifiers.push('noop'); // AKA suppress holds and transits
88         modifiers.push('auto_print_holds_transits');
89         modifiers.push('do_inventory_update');
90     }
91
92     // set modifiers from stored preferences
93     var snames = modifiers.map(function(m) {return 'eg.circ.checkin.' + m;});
94     egCore.hatch.getItemBatch(snames).then(function(settings) {
95         angular.forEach(settings, function(val, key) {
96             if (val === true) {
97                 var parts = key.split('.')
98                 var mod = parts.pop();
99                 $scope.modifiers[mod] = true;
100             }
101         })
102     });
103
104     // set / unset a checkin modifier
105     // when set, store the preference
106     $scope.toggle_mod = function(mod) {
107         if ($scope.modifiers[mod]) {
108             $scope.modifiers[mod] = false;
109             egCore.hatch.removeItem('eg.circ.checkin.' + mod);
110         } else {
111             $scope.modifiers[mod] = true;
112             egCore.hatch.setItem('eg.circ.checkin.' + mod, true);
113         }
114     }
115
116
117     // ensure the backdate is not in the future
118     // note: input type=date max=foo not yet supported anywhere
119     $scope.$watch('checkinArgs.backdate', function(newval) {
120         if (newval && newval > today) 
121             $scope.checkinArgs.backdate = today;
122     });
123
124     $scope.is_backdate = function() {
125         return $scope.checkinArgs.backdate < today;
126     }
127
128     var checkinGrid = $scope.gridControls = {};
129
130     $scope.gridDataProvider = egGridDataProvider.instance({
131         get : function(offset, count) {
132             return this.arrayNotifier($scope.checkins, offset, count);
133         }
134     });
135
136     // turns the various inputs (form args, modifiers, etc.) into
137     // checkin params and options.
138     function compile_checkin_args(args) {
139         var params = angular.copy(args);
140
141         // a backdate of 'today' is not really a backdate
142         if (!$scope.is_backdate())
143             delete params.backdate;
144
145         if (params.backdate) {
146             params.backdate = 
147                 params.backdate.toISOString().replace(/T.*/,'');
148         }
149
150         angular.forEach(['noop','void_overdues',
151                 'clear_expired','hold_as_transit','manual_float'],
152             function(opt) {
153                 if ($scope.modifiers[opt]) params[opt] = true;
154             }
155         );
156
157         if ($scope.modifiers.retarget_holds) {
158             if ($scope.modifiers.retarget_holds_all) {
159                 params.retarget_mode = 'retarget.all';
160             } else {
161                 params.retarget_mode = 'retarget';
162             }
163         }
164         if ($scope.modifiers.do_inventory_update) params.do_inventory_update = true;
165
166         egCore.hatch.setItem('circ.checkin.strict_barcode', $scope.strict_barcode);
167         egCore.hatch.setItem('circ.checkin.do_inventory_update', $scope.modifiers.do_inventory_update);
168         var options = {
169             check_barcode : $scope.strict_barcode,
170             no_precat_alert : $scope.modifiers.no_precat_alert,
171             auto_print_holds_transits : 
172                 $scope.modifiers.auto_print_holds_transits,
173             suppress_popups : suppress_popups,
174             do_inventory_update : $scope.modifiers.do_inventory_update
175         };
176
177         return {params : params, options: options};
178     }
179
180     $scope.checkin = function(args) {
181
182         var compiled = compile_checkin_args(args);
183         args.copy_barcode = ''; // reset UI for next scan
184         $scope.focusMe = true;
185         delete $scope.alert;
186         delete $scope.billable_amount;
187         delete $scope.billable_barcode;
188         delete $scope.billable_user_id;
189
190         var params = compiled.params;
191         var options = compiled.options;
192
193         if (!params.copy_barcode) return;
194         delete $scope.alert;
195
196         var row_item = {
197             index : checkinSvc.checkins.length,
198             input_barcode : params.copy_barcode
199         };
200
201         // track the item in the grid before sending the request
202         checkinSvc.checkins.unshift(row_item);
203         egCirc.checkin(params, options).then(
204         function(final_resp) {
205             
206             row_item.evt = final_resp.evt;
207             angular.forEach(final_resp.data, function(val, key) {
208                 row_item[key] = val;
209             });
210             
211             row_item['copy_barcode'] = row_item.acp.barcode();
212
213             if (row_item.acp.latest_inventory() && row_item.acp.latest_inventory().inventory_date() == "now")
214                 row_item.acp.latest_inventory().inventory_date(Date.now());
215
216             if (row_item.mbts) {
217                 var amt = Number(row_item.mbts.balance_owed());
218                 if (amt != 0) {
219                     $scope.billable_barcode = row_item.copy_barcode;
220                     $scope.billable_amount = amt;
221                     $scope.billable_user_id = row_item.circ.usr();
222                     $scope.fine_total = 
223                         ($scope.fine_total * 100 + amt * 100) / 100;
224                 }
225             }
226
227             if (final_resp.evt.textcode == 'NO_CHANGE') {
228                 $scope.alert = 
229                     {already_checked_in : final_resp.evt.copy_barcode};
230             }
231
232             if ($scope.trim_list && checkinSvc.checkins.length > 20)
233                 //cut array short at 20 items
234                 checkinSvc.checkins.length = 20;
235         },
236         function() {
237             // Checkin was rejected somewhere along the way.
238             // Remove the copy from the grid since there was no action.
239             // note: since checkins are unshifted onto the array, the
240             // index value does not (generally) match the array position.
241             var pos = -1;
242             angular.forEach(checkinSvc.checkins, function(ci, idx) {
243                 if (ci.index == row_item.index) pos = idx;
244             });
245             checkinSvc.checkins.splice(pos, 1);
246
247         })['finally'](function() {
248
249             // when all is said and done, refresh the grid and refocus
250             checkinGrid.refresh();
251             $scope.focusMe = true;
252         });
253     }
254
255     $scope.print_receipt = function() {
256         var print_data = {checkins : []}
257
258         if (checkinSvc.checkins.length == 0) return $q.when();
259
260         angular.forEach(checkinSvc.checkins, function(checkin) {
261
262             var checkin = {
263                 copy : egCore.idl.toHash(checkin.acp) || {},
264                 call_number : egCore.idl.toHash(checkin.acn) || {},
265                 copy_barcode : checkin.copy_barcode,
266                 title : checkin.title,
267                 author : checkin.author
268             }
269
270             print_data.checkins.push(checkin);
271         });
272
273         return egCore.print.print({
274             template : 'checkin', 
275             scope : print_data,
276             show_dialog : $scope.show_print_dialog
277         });
278     }
279
280
281     // --- context menu actions
282     //
283     $scope.fetchLastCircPatron = function(items) {
284         var checkin = items[0];
285         if (!checkin || !checkin.acp) return;
286
287         egCirc.last_copy_circ(checkin.acp.id())
288         .then(function(circ) {
289
290             if (circ) {
291                 // jump to the patron UI (separate app)
292                 $window.location.href = $location
293                     .path('/circ/patron/' + circ.usr() + '/checkout')
294                     .absUrl();
295                 return;
296             }
297
298             $scope.alert = {item_never_circed : checkin.acp.barcode()};
299         });
300     }
301
302     $scope.showBackdateDialog = function(items) {
303         var circ_ids = [];
304
305         angular.forEach(items, function(item) {
306             if (item.circ) circ_ids.push(item.circ.id());
307         });
308
309         if (circ_ids.length) {
310             egCirc.backdate_dialog(circ_ids).then(function(result) {
311                 angular.forEach(items, function(item) {
312                     item.circ.checkin_time(result.backdate);
313                 })
314             });
315             // TODO: support grid row styling
316             checkinGrid.refresh();
317         }
318     }
319
320     $scope.showMarkDamaged = function(items) {
321         var copy_ids = [];
322         angular.forEach(items, function(item) {
323             if (item.acp) {
324                 egCirc.mark_damaged({
325                     id: item.acp.id(),
326                     barcode: item.acp.barcode()
327                 })
328
329             }
330         });
331
332     }
333
334     $scope.abortTransit = function(items) {
335         var transit_ids = [];
336         angular.forEach(items, function(item) {
337             if (item.transit) transit_ids.push(item.transit.id());
338         });
339
340         egCirc.abort_transits(transit_ids).then(function() {
341             // update grid items?
342         });
343     }
344
345     $scope.add_copies_to_bucket = function(items){
346         var itemsIds = [];
347         angular.forEach(items, function(cp){
348             itemsIds.push(cp.acp.id());
349         });
350
351         itemSvc.add_copies_to_bucket(itemsIds);
352     }
353
354     $scope.showBibHolds = function(items){
355         var recordIds = [];
356         angular.forEach(items, function(i){
357             recordIds.push(i.acn.record());
358         });
359         angular.forEach(recordIds, function (r) {
360             var url = egCore.env.basePath + 'cat/catalog/record/' + r + '/holds';
361             $timeout(function() { $window.open(url, '_blank') });
362         });
363     }
364
365     $scope.showLastCircs = function(items){
366         var itemIds = [];
367         angular.forEach(items, function(cp){
368             itemIds.push(cp.acp.id());
369         });
370         angular.forEach(itemIds, function (id) {
371             var url = egCore.env.basePath + 'cat/item/' + id + '/circs';
372             $timeout(function() { $window.open(url, '_blank') });
373         });
374     }
375
376     $scope.selectedHoldingsVolCopyEdit = function (items) {
377         var itemObjs = [];
378         angular.forEach(items, function(i){
379             var h = egCore.idl.toHash(i);
380             itemObjs.push({
381                 'call_number.record.id': h.record.doc_id,
382                 'id' : h.acp.id
383             });
384         });
385         itemSvc.spawnHoldingsEdit(itemObjs,false,false);
386     }
387
388     $scope.show_mark_missing_pieces = function(items){
389         angular.forEach(items, function(i){
390             i.acp.call_number(i.acn);
391             i.acp.call_number().record(i.record);
392             itemSvc.mark_missing_pieces(i.acp,$scope);
393         });
394     }
395
396     $scope.printSpineLabels = function(items){
397         var copy_ids = [];
398         angular.forEach(items, function(item) {
399             if (item.acp) copy_ids.push(item.acp.id());
400         });
401         itemSvc.print_spine_labels(copy_ids);
402     }
403
404     $scope.addCopyAlerts = function(items) {
405         var copy_ids = [];
406         angular.forEach(items, function(item) {
407             if (item.acp) copy_ids.push(item.acp.id());
408         });
409         egCirc.add_copy_alerts(copy_ids).then(function() {
410             // update grid items?
411         });
412     }
413
414     $scope.manageCopyAlerts = function(items) {
415         var copy_ids = [];
416         angular.forEach(items, function(item) {
417             if (item.acp) copy_ids.push(item.acp.id());
418         });
419         egCirc.manage_copy_alerts(copy_ids).then(function() {
420             // update grid items?
421         });
422     }
423
424 }])
425