2 * Checkout items to patrons
5 angular.module('egPatronApp').controller('PatronCheckoutCtrl',
7 ['$scope','$q','$modal','$routeParams','egCore','egUser','patronSvc',
8 'egGridDataProvider','$location','$timeout','egCirc',
10 function($scope , $q , $modal , $routeParams , egCore , egUser , patronSvc ,
11 egGridDataProvider , $location , $timeout , egCirc) {
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'
37 $scope.using_hatch = egCore.hatch.usingHatch();
39 // avoid multiple, in-flight attempts on the same barcode
40 var pending_barcodes = {};
42 var printOnComplete = true;
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']);
50 egCirc.get_noncat_types().then(function(list) {
51 $scope.nonCatTypes = list;
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;
60 $scope.checkout = function(args) {
61 var params = angular.copy(args);
62 params.patron_id = patronSvc.current.id();
64 if (args.sticky_date) {
65 params.due_date = args.due_date.toISOString();
67 delete params.due_date;
69 delete params.sticky_date;
71 if (params.noncat_type == 'barcode') {
72 if (!args.copy_barcode) return;
74 args.copy_barcode = ''; // reset UI input
75 params.noncat_type = ''; // "barcode"
77 if (pending_barcodes[params.copy_barcode]) {
79 "Skipping checkout of redundant barcode "
85 pending_barcodes[params.copy_barcode] = true;
86 send_checkout(params);
89 egCirc.noncat_dialog(params).then(function() {
94 $scope.focusMe = true; // return focus to barcode input
97 function send_checkout(params) {
99 params.noncat_type = params.noncat ? params.noncat_type : '';
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.
105 index : $scope.checkouts.length,
106 copy_barcode : params.copy_barcode,
107 noncat_type : params.noncat_type
110 $scope.checkouts.unshift(row_item);
111 $scope.gridDataProvider.refresh();
113 var options = {check_barcode : $scope.strict_barcode};
115 egCirc.checkout(params, options).then(
117 // update stats locally so we don't have to fetch them w/
119 patronSvc.patron_stats.checkouts.out++;
120 patronSvc.patron_stats.checkouts.total_out++;
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) {
128 munge_checkout_resp(co_resp, row_item);
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.
136 angular.forEach($scope.checkouts, function(co, idx) {
137 if (co.index == row_item.index) pos = idx;
139 $scope.checkouts.splice(pos, 1);
140 $scope.gridDataProvider.refresh();
143 ).finally(function() {
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];
150 $scope.focusMe = true; // return focus to barcode input
154 // add some checkout-specific additions for display
155 function munge_checkout_resp(co_resp, row_item) {
156 var params = co_resp.params;
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());
165 $scope.print_receipt = function() {
166 var print_data = {circulations : []}
168 if ($scope.checkouts.length == 0) return $q.when();
170 angular.forEach($scope.checkouts, function(co) {
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),
182 return egCore.print.print({
184 template : 'checkout',
186 show_dialog : $scope.show_print_dialog
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) {
195 $scope.print_receipt().then(function() {
196 $location.path('/circ/patron/bcsearch');
200 $location.path('/circ/patron/bcsearch');