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