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/
155 // Avoid updating checkout counts when a checkout turns
156 // into a renewal via auto_renew.
157 if (!co_resp.auto_renew && !params.noncat) {
158 patronSvc.patron_stats.checkouts.out++;
159 patronSvc.patron_stats.checkouts.total_out++;
162 // copy the response event into the original grid row item
163 // note: angular.copy clobbers the destination
164 row_item.evt = co_resp.evt;
165 angular.forEach(co_resp.data, function(val, key) {
168 munge_checkout_resp(co_resp, row_item);
171 // Circ was rejected somewhere along the way.
172 // Remove the copy from the grid since there was no action.
173 // note: since checkouts are unshifted onto the array, the
174 // index value does not (generally) match the array position.
176 angular.forEach($scope.checkouts, function(co, idx) {
177 if (co.index == row_item.index) pos = idx;
179 $scope.checkouts.splice(pos, 1);
180 $scope.gridDataProvider.refresh();
183 ).finally(function() {
185 // regardless of the outcome of the circ, remove the
186 // barcode from the pending list.
187 if (params.copy_barcode)
188 delete pending_barcodes[params.copy_barcode];
190 $scope.focusMe = true; // return focus to barcode input
194 // add some checkout-specific additions for display
195 function munge_checkout_resp(co_resp, row_item) {
196 var params = co_resp.params;
198 row_item.title = egCore.env.cnct.map[params.noncat_type].name();
199 row_item.noncat_count = params.noncat_count;
200 row_item.circ = new egCore.idl.circ();
201 row_item.circ.due_date(co_resp.evt[0].payload.noncat_circ.duedate());
202 // Non-cat circs don't return the full list of circs.
203 // Refresh the list of non-cat circs from the server.
204 patronSvc.getUserNonCats(patronSvc.current.id());
208 $scope.print_receipt = function() {
209 var print_data = {circulations : []}
211 if ($scope.checkouts.length == 0) return $q.when();
213 angular.forEach($scope.checkouts, function(co) {
215 print_data.circulations.push({
216 circ : egCore.idl.toHash(co.circ),
217 copy : egCore.idl.toHash(co.acp),
218 call_number : egCore.idl.toHash(co.acn),
225 return egCore.print.print({
227 template : 'checkout',
229 show_dialog : $scope.show_print_dialog
233 $scope.email_receipt = function() {
234 if ($scope.has_email_address() && $scope.checkouts.length) {
235 return egCore.net.request(
237 'open-ils.circ.checkout.batch_notify.session.atomic',
239 patronSvc.current.id(),
240 $scope.checkouts.map(function (c) { return c.circ.id() })
242 ngToast.create(egCore.strings.EMAILED_CHECKOUT_RECEIPT);
249 $scope.print_or_email_receipt = function() {
250 if ($scope.may_email_receipt()) return $scope.email_receipt();
251 $scope.print_receipt();
254 // set of functions to issue a receipt (if desired), then
256 $scope.done_auto_receipt = function() {
257 if ($scope.may_email_receipt()) {
258 $scope.email_receipt().then(function() {
259 $scope.done_redirect();
262 if (printOnComplete) {
264 $scope.print_receipt().then(function() {
265 $scope.done_redirect();
269 $scope.done_redirect();
273 $scope.done_print_receipt = function() {
274 $scope.print_receipt().then( function () {
275 $scope.done_redirect();
278 $scope.done_email_receipt = function() {
279 $scope.email_receipt().then( function () {
280 $scope.done_redirect();
283 $scope.done_no_receipt = function() {
284 $scope.done_redirect();
287 // Redirect the user to the barcode entry page to load a new patron.
288 $scope.done_redirect = function() {
289 $location.path('/circ/patron/bcsearch');