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 //check for renew so that is doesn't update incorrectly
155 if(co_resp.evt[0].payload.parent_circ == null){
156 patronSvc.patron_stats.checkouts.out++;
157 patronSvc.patron_stats.checkouts.total_out++;
160 // copy the response event into the original grid row item
161 // note: angular.copy clobbers the destination
162 row_item.evt = co_resp.evt;
163 angular.forEach(co_resp.data, function(val, key) {
166 munge_checkout_resp(co_resp, row_item);
169 // Circ was rejected somewhere along the way.
170 // Remove the copy from the grid since there was no action.
171 // note: since checkouts are unshifted onto the array, the
172 // index value does not (generally) match the array position.
174 angular.forEach($scope.checkouts, function(co, idx) {
175 if (co.index == row_item.index) pos = idx;
177 $scope.checkouts.splice(pos, 1);
178 $scope.gridDataProvider.refresh();
181 ).finally(function() {
183 // regardless of the outcome of the circ, remove the
184 // barcode from the pending list.
185 if (params.copy_barcode)
186 delete pending_barcodes[params.copy_barcode];
188 $scope.focusMe = true; // return focus to barcode input
192 // add some checkout-specific additions for display
193 function munge_checkout_resp(co_resp, row_item) {
194 var params = co_resp.params;
196 row_item.title = egCore.env.cnct.map[params.noncat_type].name();
197 row_item.noncat_count = params.noncat_count;
198 row_item.circ = new egCore.idl.circ();
199 row_item.circ.due_date(co_resp.evt.payload.noncat_circ.duedate());
203 $scope.print_receipt = function() {
204 var print_data = {circulations : []}
206 if ($scope.checkouts.length == 0) return $q.when();
208 angular.forEach($scope.checkouts, function(co) {
210 print_data.circulations.push({
211 circ : egCore.idl.toHash(co.circ),
212 copy : egCore.idl.toHash(co.acp),
213 call_number : egCore.idl.toHash(co.acn),
220 return egCore.print.print({
222 template : 'checkout',
224 show_dialog : $scope.show_print_dialog
228 $scope.email_receipt = function() {
229 if ($scope.has_email_address() && $scope.checkouts.length) {
230 return egCore.net.request(
232 'open-ils.circ.checkout.batch_notify.session.atomic',
234 patronSvc.current.id(),
235 $scope.checkouts.map(function (c) { return c.circ.id() })
237 ngToast.create(egCore.strings.EMAILED_CHECKOUT_RECEIPT);
244 $scope.print_or_email_receipt = function() {
245 if ($scope.may_email_receipt()) return $scope.email_receipt();
246 $scope.print_receipt();
249 // set of functions to issue a receipt (if desired), then
251 $scope.done_auto_receipt = function() {
252 if ($scope.may_email_receipt()) {
253 $scope.email_receipt().then(function() {
254 $scope.done_redirect();
257 if (printOnComplete) {
259 $scope.print_receipt().then(function() {
260 $scope.done_redirect();
264 $scope.done_redirect();
268 $scope.done_print_receipt = function() {
269 $scope.print_receipt().then( function () {
270 $scope.done_redirect();
273 $scope.done_email_receipt = function() {
274 $scope.email_receipt().then( function () {
275 $scope.done_redirect();
278 $scope.done_no_receipt = function() {
279 $scope.done_redirect();
282 // Redirect the user to the barcode entry page to load a new patron.
283 $scope.done_redirect = function() {
284 $location.path('/circ/patron/bcsearch');