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