]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/patron/checkout.js
LP#1402797 Use .finally() instead of hash-y syntax~
[Evergreen.git] / Open-ILS / web / js / ui / default / staff / circ / patron / checkout.js
1 /**
2  * Checkout items to patrons
3  */
4
5 angular.module('egPatronApp').controller('PatronCheckoutCtrl',
6
7        ['$scope','$q','$modal','$routeParams','egCore','egUser','patronSvc',
8         'egGridDataProvider','$location','$timeout','egCirc',
9
10 function($scope , $q , $modal , $routeParams , egCore , egUser , patronSvc , 
11          egGridDataProvider , $location , $timeout , egCirc) {
12
13     $scope.initTab('checkout', $routeParams.id).finally(function(){
14         $scope.focusMe = true;
15     });
16     $scope.checkouts = patronSvc.checkouts;
17     $scope.checkoutArgs = {
18         noncat_type : 'barcode',
19         due_date : new Date()
20     };
21
22     $scope.gridDataProvider = egGridDataProvider.instance({
23         get : function(offset, count) {
24             return this.arrayNotifier($scope.checkouts, offset, count);
25         }
26     });
27
28     $scope.disable_checkout = function() {
29         return (
30             !patronSvc.current ||
31             patronSvc.current.active() == 'f' ||
32             patronSvc.current.deleted() == 't' ||
33             patronSvc.current.card().active() == 'f'
34         );
35     }
36
37     $scope.using_hatch = egCore.hatch.usingHatch();
38
39     // avoid multiple, in-flight attempts on the same barcode
40     var pending_barcodes = {};
41
42     var printOnComplete = true;
43     egCore.org.settings([
44         'circ.staff_client.do_not_auto_attempt_print'
45     ]).then(function(settings) { 
46         printOnComplete = !Boolean(
47             settings['circ.staff_client.do_not_auto_attempt_print']);
48     });
49
50     egCirc.get_noncat_types().then(function(list) {
51         $scope.nonCatTypes = list;
52     });
53
54     $scope.selectedNcType = function() {
55         if (!egCore.env.cnct) return null; // too soon
56         var type = egCore.env.cnct.map[$scope.checkoutArgs.noncat_type];
57         return type ? type.name() : null;
58     }
59
60     $scope.checkout = function(args) {
61         var params = angular.copy(args);
62         params.patron_id = patronSvc.current.id();
63
64         if (args.sticky_date) {
65             params.due_date = args.due_date.toISOString();
66         } else {
67             delete params.due_date;
68         }
69         delete params.sticky_date;
70
71         if (params.noncat_type == 'barcode') {
72             if (!args.copy_barcode) return;
73
74             args.copy_barcode = ''; // reset UI input
75             params.noncat_type = ''; // "barcode"
76
77             if (pending_barcodes[params.copy_barcode]) {
78                 console.log(
79                     "Skipping checkout of redundant barcode " 
80                     + params.copy_barcode
81                 );
82                 return;
83             }
84
85             pending_barcodes[params.copy_barcode] = true;
86             send_checkout(params);
87
88         } else {
89             egCirc.noncat_dialog(params).then(function() {
90                 send_checkout(params)
91             });
92         }
93
94         $scope.focusMe = true; // return focus to barcode input
95     }
96
97     function send_checkout(params) {
98
99         params.noncat_type = params.noncat ? params.noncat_type : '';
100
101         // populate the grid row before we send the request so that the
102         // order of actions is maintained and so the user gets an 
103         // immediate reaction to their barcode input action.
104         var row_item = {
105             index : $scope.checkouts.length,
106             copy_barcode : params.copy_barcode,
107             noncat_type : params.noncat_type
108         };
109
110         $scope.checkouts.unshift(row_item);
111         $scope.gridDataProvider.refresh();
112
113         var options = {check_barcode : $scope.strict_barcode};
114
115         egCirc.checkout(params, options).then(
116             function(co_resp) {
117                 // update stats locally so we don't have to fetch them w/
118                 // each checkout.
119                 patronSvc.patron_stats.checkouts.out++;
120                 patronSvc.patron_stats.checkouts.total_out++;
121
122                 // copy the response event into the original grid row item
123                 // note: angular.copy clobbers the destination
124                 row_item.evt = co_resp.evt;
125                 angular.forEach(co_resp.data, function(val, key) {
126                     row_item[key] = val;
127                 });
128                 munge_checkout_resp(co_resp, row_item);
129             },
130             function() {
131                 // Circ was rejected somewhere along the way.
132                 // Remove the copy from the grid since there was no action.
133                 // note: since checkouts are unshifted onto the array, the
134                 // index value does not (generally) match the array position.
135                 var pos = -1;
136                 angular.forEach($scope.checkouts, function(co, idx) {
137                     if (co.index == row_item.index) pos = idx;
138                 });
139                 $scope.checkouts.splice(pos, 1);
140                 $scope.gridDataProvider.refresh();
141             }
142
143         ).finally(function() {
144
145             // regardless of the outcome of the circ, remove the 
146             // barcode from the pending list.
147             if (params.copy_barcode)
148                 delete pending_barcodes[params.copy_barcode];
149
150             $scope.focusMe = true; // return focus to barcode input
151         });
152     }
153
154     // add some checkout-specific additions for display
155     function munge_checkout_resp(co_resp, row_item) {
156         var params = co_resp.params;
157         if (params.noncat) {
158             row_item.title = egCore.env.cnct.map[params.noncat_type].name();
159             row_item.noncat_count = params.noncat_count;
160             row_item.circ = new egCore.idl.circ();
161             row_item.circ.due_date(co_resp.evt.payload.noncat_circ.duedate());
162         }
163     }
164
165     $scope.print_receipt = function() {
166         var print_data = {circulations : []}
167
168         if ($scope.checkouts.length == 0) return $q.when();
169
170         angular.forEach($scope.checkouts, function(co) {
171             if (co.circ) {
172                 print_data.circulations.push({
173                     circ : egCore.idl.toHash(co.circ),
174                     copy : egCore.idl.toHash(co.acp),
175                     call_number : egCore.idl.toHash(co.acn),
176                     title : co.title,
177                     author : co.author
178                 })
179             };
180         });
181
182         return egCore.print.print({
183             context : 'default', 
184             template : 'checkout', 
185             scope : print_data,
186             show_dialog : $scope.show_print_dialog
187         });
188     }
189
190     // Redirect the user to the barcode entry page to load a new patron.
191     // If configured to do so, print the receipt first
192     $scope.done = function() {
193         if (printOnComplete) {
194
195             $scope.print_receipt().then(function() {
196                 $location.path('/circ/patron/bcsearch');
197             });
198
199         } else {
200             $location.path('/circ/patron/bcsearch');
201         }
202     }
203 }])
204