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