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 = egCore.hatch.usingHatch();
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 settings['circ.staff_client.do_not_auto_attempt_print']);
82 egCirc.get_noncat_types().then(function(list) {
83 $scope.nonCatTypes = list;
86 $scope.selectedNcType = function() {
87 if (!egCore.env.cnct) return null; // too soon
88 var type = egCore.env.cnct.map[$scope.checkoutArgs.noncat_type];
89 return type ? type.name() : null;
92 $scope.checkout = function(args) {
93 var params = angular.copy(args);
94 params.patron_id = patronSvc.current.id();
96 if (args.sticky_date) {
97 params.due_date = args.due_date.toISOString();
99 delete params.due_date;
101 delete params.sticky_date;
103 if (params.noncat_type == 'barcode') {
104 if (!args.copy_barcode) return;
106 args.copy_barcode = ''; // reset UI input
107 params.noncat_type = ''; // "barcode"
109 if (pending_barcodes[params.copy_barcode]) {
111 "Skipping checkout of redundant barcode "
112 + params.copy_barcode
117 pending_barcodes[params.copy_barcode] = true;
118 send_checkout(params);
121 egCirc.noncat_dialog(params).then(function() {
122 send_checkout(params)
126 $scope.focusMe = true; // return focus to barcode input
129 function send_checkout(params) {
131 params.noncat_type = params.noncat ? params.noncat_type : '';
133 // populate the grid row before we send the request so that the
134 // order of actions is maintained and so the user gets an
135 // immediate reaction to their barcode input action.
137 index : $scope.checkouts.length,
138 copy_barcode : params.copy_barcode,
139 noncat_type : params.noncat_type
142 $scope.checkouts.unshift(row_item);
143 $scope.gridDataProvider.refresh();
145 egCore.hatch.setItem('circ.checkout.strict_barcode', $scope.strict_barcode);
146 var options = {check_barcode : $scope.strict_barcode};
148 egCirc.checkout(params, options).then(
150 // update stats locally so we don't have to fetch them w/
152 patronSvc.patron_stats.checkouts.out++;
153 patronSvc.patron_stats.checkouts.total_out++;
155 // copy the response event into the original grid row item
156 // note: angular.copy clobbers the destination
157 row_item.evt = co_resp.evt;
158 angular.forEach(co_resp.data, function(val, key) {
161 munge_checkout_resp(co_resp, row_item);
164 // Circ was rejected somewhere along the way.
165 // Remove the copy from the grid since there was no action.
166 // note: since checkouts are unshifted onto the array, the
167 // index value does not (generally) match the array position.
169 angular.forEach($scope.checkouts, function(co, idx) {
170 if (co.index == row_item.index) pos = idx;
172 $scope.checkouts.splice(pos, 1);
173 $scope.gridDataProvider.refresh();
176 ).finally(function() {
178 // regardless of the outcome of the circ, remove the
179 // barcode from the pending list.
180 if (params.copy_barcode)
181 delete pending_barcodes[params.copy_barcode];
183 $scope.focusMe = true; // return focus to barcode input
187 // add some checkout-specific additions for display
188 function munge_checkout_resp(co_resp, row_item) {
189 var params = co_resp.params;
191 row_item.title = egCore.env.cnct.map[params.noncat_type].name();
192 row_item.noncat_count = params.noncat_count;
193 row_item.circ = new egCore.idl.circ();
194 row_item.circ.due_date(co_resp.evt.payload.noncat_circ.duedate());
198 $scope.print_receipt = function() {
199 var print_data = {circulations : []}
201 if ($scope.checkouts.length == 0) return $q.when();
203 angular.forEach($scope.checkouts, function(co) {
205 print_data.circulations.push({
206 circ : egCore.idl.toHash(co.circ),
207 copy : egCore.idl.toHash(co.acp),
208 call_number : egCore.idl.toHash(co.acn),
215 return egCore.print.print({
217 template : 'checkout',
219 show_dialog : $scope.show_print_dialog
223 $scope.email_receipt = function() {
224 if ($scope.has_email_address() && $scope.checkouts.length) {
225 return egCore.net.request(
227 'open-ils.circ.checkout.batch_notify.session.atomic',
229 patronSvc.current.id(),
230 $scope.checkouts.map(function (c) { return c.circ.id() })
232 ngToast.create(egCore.strings.EMAILED_CHECKOUT_RECEIPT);
239 $scope.print_or_email_receipt = function() {
240 if ($scope.may_email_receipt()) return $scope.email_receipt();
241 $scope.print_receipt();
244 // set of functions to issue a receipt (if desired), then
246 $scope.done_auto_receipt = function() {
247 if ($scope.may_email_receipt()) {
248 $scope.email_receipt().then(function() {
249 $scope.done_redirect();
252 if (printOnComplete) {
254 $scope.print_receipt().then(function() {
255 $scope.done_redirect();
259 $scope.done_redirect();
263 $scope.done_print_receipt = function() {
264 $scope.print_receipt().then( function () {
265 $scope.done_redirect();
268 $scope.done_email_receipt = function() {
269 $scope.email_receipt().then( function () {
270 $scope.done_redirect();
273 $scope.done_no_receipt = function() {
274 $scope.done_redirect();
277 // Redirect the user to the barcode entry page to load a new patron.
278 $scope.done_redirect = function() {
279 $location.path('/circ/patron/bcsearch');