2 * Checkout items to patrons
5 angular.module('egPatronApp').controller('PatronCheckoutCtrl',
7 ['$scope','$q','$routeParams','egCore','egUser','patronSvc',
8 'egGridDataProvider','$location','$timeout','egCirc','ngToast',
10 function($scope , $q , $routeParams , egCore , egUser , patronSvc ,
11 egGridDataProvider , $location , $timeout , egCirc , ngToast) {
13 $scope.initTab('checkout', $routeParams.id).finally(function(){
14 $scope.focusMe = true;
16 $scope.checkouts = patronSvc.checkouts;
17 $scope.checkoutArgs = {
18 noncat_type : 'barcode',
22 $scope.gridDataProvider = egGridDataProvider.instance({
23 get : function(offset, count) {
24 return this.arrayNotifier($scope.checkouts, offset, count);
28 $scope.disable_checkout = function() {
31 patronSvc.current.active() == 'f' ||
32 patronSvc.current.deleted() == 't' ||
33 patronSvc.current.card().active() == 'f' ||
34 patronSvc.fetchedWithInactiveCard()
38 function setting_value (user, setting) {
40 var list = user.settings().filter(function(s){
41 return s.name() == setting;
44 if (list.length) return list[0].value();
48 $scope.has_email_address = function() {
51 patronSvc.current.email() &&
52 patronSvc.current.email().match(/.*@.*/).length
56 $scope.may_email_receipt = function() {
58 $scope.has_email_address() &&
61 'circ.send_email_checkout_receipts'
66 $scope.using_hatch_printer = egCore.hatch.usePrinting();
68 egCore.hatch.getItem('circ.checkout.strict_barcode')
69 .then(function(sb){ $scope.strict_barcode = sb });
71 // avoid multiple, in-flight attempts on the same barcode
72 var pending_barcodes = {};
74 var printOnComplete = true;
76 'circ.staff_client.do_not_auto_attempt_print'
77 ]).then(function(settings) {
78 printOnComplete = !Boolean(
79 angular.isArray(settings['circ.staff_client.do_not_auto_attempt_print']) &&
80 (settings['circ.staff_client.do_not_auto_attempt_print'].indexOf('Checkout') > -1)
84 egCirc.get_noncat_types().then(function(list) {
85 $scope.nonCatTypes = list;
88 $scope.selectedNcType = function() {
89 if (!egCore.env.cnct) return null; // too soon
90 var type = egCore.env.cnct.map[$scope.checkoutArgs.noncat_type];
91 return type ? type.name() : null;
94 $scope.checkout = function(args) {
95 var params = angular.copy(args);
96 params.patron_id = patronSvc.current.id();
98 if (args.sticky_date) {
99 params.due_date = args.due_date.toISOString();
101 delete params.due_date;
103 delete params.sticky_date;
105 if (params.noncat_type == 'barcode') {
106 if (!args.copy_barcode) return;
108 args.copy_barcode = ''; // reset UI input
109 params.noncat_type = ''; // "barcode"
111 if (pending_barcodes[params.copy_barcode]) {
113 "Skipping checkout of redundant barcode "
114 + params.copy_barcode
119 pending_barcodes[params.copy_barcode] = true;
120 send_checkout(params);
123 egCirc.noncat_dialog(params).then(function() {
124 send_checkout(params)
128 $scope.focusMe = true; // return focus to barcode input
131 function send_checkout(params) {
133 params.noncat_type = params.noncat ? params.noncat_type : '';
135 // populate the grid row before we send the request so that the
136 // order of actions is maintained and so the user gets an
137 // immediate reaction to their barcode input action.
139 index : $scope.checkouts.length,
140 copy_barcode : params.copy_barcode,
141 noncat_type : params.noncat_type
144 $scope.checkouts.unshift(row_item);
145 $scope.gridDataProvider.refresh();
147 egCore.hatch.setItem('circ.checkout.strict_barcode', $scope.strict_barcode);
148 var options = {check_barcode : $scope.strict_barcode};
150 egCirc.checkout(params, options).then(
152 // update stats locally so we don't have to fetch them w/
154 patronSvc.patron_stats.checkouts.out++;
155 patronSvc.patron_stats.checkouts.total_out++;
157 // copy the response event into the original grid row item
158 // note: angular.copy clobbers the destination
159 row_item.evt = co_resp.evt;
160 angular.forEach(co_resp.data, function(val, key) {
163 munge_checkout_resp(co_resp, row_item);
166 // Circ was rejected somewhere along the way.
167 // Remove the copy from the grid since there was no action.
168 // note: since checkouts are unshifted onto the array, the
169 // index value does not (generally) match the array position.
171 angular.forEach($scope.checkouts, function(co, idx) {
172 if (co.index == row_item.index) pos = idx;
174 $scope.checkouts.splice(pos, 1);
175 $scope.gridDataProvider.refresh();
178 ).finally(function() {
180 // regardless of the outcome of the circ, remove the
181 // barcode from the pending list.
182 if (params.copy_barcode)
183 delete pending_barcodes[params.copy_barcode];
185 $scope.focusMe = true; // return focus to barcode input
189 // add some checkout-specific additions for display
190 function munge_checkout_resp(co_resp, row_item) {
191 var params = co_resp.params;
193 row_item.title = egCore.env.cnct.map[params.noncat_type].name();
194 row_item.noncat_count = params.noncat_count;
195 row_item.circ = new egCore.idl.circ();
196 row_item.circ.due_date(co_resp.evt.payload.noncat_circ.duedate());
200 $scope.print_receipt = function() {
201 var print_data = {circulations : []}
203 if ($scope.checkouts.length == 0) return $q.when();
205 angular.forEach($scope.checkouts, function(co) {
207 print_data.circulations.push({
208 circ : egCore.idl.toHash(co.circ),
209 copy : egCore.idl.toHash(co.acp),
210 call_number : egCore.idl.toHash(co.acn),
217 return egCore.print.print({
219 template : 'checkout',
221 show_dialog : $scope.show_print_dialog
225 $scope.email_receipt = function() {
226 if ($scope.has_email_address() && $scope.checkouts.length) {
227 return egCore.net.request(
229 'open-ils.circ.checkout.batch_notify.session.atomic',
231 patronSvc.current.id(),
232 $scope.checkouts.map(function (c) { return c.circ.id() })
234 ngToast.create(egCore.strings.EMAILED_CHECKOUT_RECEIPT);
241 $scope.print_or_email_receipt = function() {
242 if ($scope.may_email_receipt()) return $scope.email_receipt();
243 $scope.print_receipt();
246 // set of functions to issue a receipt (if desired), then
248 $scope.done_auto_receipt = function() {
249 if ($scope.may_email_receipt()) {
250 $scope.email_receipt().then(function() {
251 $scope.done_redirect();
254 if (printOnComplete) {
256 $scope.print_receipt().then(function() {
257 $scope.done_redirect();
261 $scope.done_redirect();
265 $scope.done_print_receipt = function() {
266 $scope.print_receipt().then( function () {
267 $scope.done_redirect();
270 $scope.done_email_receipt = function() {
271 $scope.email_receipt().then( function () {
272 $scope.done_redirect();
275 $scope.done_no_receipt = function() {
276 $scope.done_redirect();
279 // Redirect the user to the barcode entry page to load a new patron.
280 $scope.done_redirect = function() {
281 $location.path('/circ/patron/bcsearch');