Line 0
Link Here
|
0 |
- |
1 |
$(document).ready(function() { |
|
|
2 |
|
3 |
let checkouts_count = 0; |
4 |
|
5 |
function addResult(type, code, data) { |
6 |
let result = ''; |
7 |
if (type == 'danger') { |
8 |
result += '<div class="alert alert-danger">'; |
9 |
} else if (type == 'warning') { |
10 |
result += '<div class="alert alert-warning">'; |
11 |
} else if (type == 'info') { |
12 |
result += '<div class="alert alert-info">'; |
13 |
} else { |
14 |
result += '<div class="alert alert-success">'; |
15 |
} |
16 |
|
17 |
if (code == 'NOT_FOUND') { |
18 |
result += _("Item '%s' not found").format(data); |
19 |
} else if (code == 'RENEW_ISSUE') { |
20 |
result += _("Item will be renewed").format(data); |
21 |
} else if (code == 'OTHER_CHARGES') { |
22 |
result += _("Your account currently has outstanding charges of '%s'").format(data); |
23 |
} else if (code == 'DEBT') { |
24 |
result += _("Your account is currently in debt by '%s'").format(data); |
25 |
} else if (code == 'ISSUED_TO_ANOTHER') { |
26 |
result += _("This item appears to be checked out to another patron, please return it to the desk"); |
27 |
} else if (code == 'RESERVED') { |
28 |
result += _("This item appears to be reserved for another patron, please return it to the desk"); |
29 |
} else if (code == 'NOT_FOR_LOAN') { |
30 |
result += _("This item is not normally for loan, please select another or ask at the desk"); |
31 |
} else if (code == 'WTHDRAWN') { |
32 |
result += _("This item is marked withdrawn, please select another or ask at the desk"); |
33 |
} else if (code == 'EMPTY') { |
34 |
result += _("Please enter the barcode for the item you wish to checkout"); |
35 |
} else { |
36 |
result += _("Message code '%s' with data '%s'").format(code, data); |
37 |
} |
38 |
|
39 |
result += '</div>'; |
40 |
$('#availabilityResult').append(result); |
41 |
}; |
42 |
|
43 |
// On modal show, clear any prior results and set focus |
44 |
$('#checkoutModal').on('shown.bs.modal', function(e) { |
45 |
$('#checkoutResults').replaceWith('<div id="checkoutResults"></div>'); |
46 |
$('#availabilityResult').replaceWith('<div id="availabilityResult"></div>'); |
47 |
$('#checkoutsTable').hide(); |
48 |
$('#checkout_barcode').val("").focus(); |
49 |
}); |
50 |
|
51 |
// On modal submit |
52 |
$('#checkoutModal').on('click', '#checkoutSubmit', function(e) { |
53 |
|
54 |
// Get item from barcode |
55 |
let external_id = $('#checkout_barcode').val(); |
56 |
if ( external_id === '' ) { |
57 |
addResult('warning', 'EMPTY'); |
58 |
return; |
59 |
} |
60 |
|
61 |
let item_id; |
62 |
let items = $.ajax({ |
63 |
url: '/api/v1/public/items?external_id=' + external_id, |
64 |
dataType: 'json', |
65 |
type: 'GET' |
66 |
}); |
67 |
|
68 |
$('#availabilityResult').replaceWith('<div id="availabilityResult"></div>'); |
69 |
|
70 |
// Get availability of the item |
71 |
let availability = items.then( |
72 |
function(data, textStatus, jqXHR) { |
73 |
if (data.length == 1) { |
74 |
item_id = data[0].item_id; |
75 |
return $.ajax({ |
76 |
url: '/api/v1/public/checkouts/availability?item_id=' + item_id + '&patron_id=' + logged_in_user_id, |
77 |
type: 'GET', |
78 |
contentType: "json" |
79 |
}); |
80 |
} else { |
81 |
addResult('danger', 'NOT_FOUND', external_id); |
82 |
} |
83 |
}, |
84 |
function(data, textStatus, jqXHR) { |
85 |
addResult('danger', 'NOT_FOUND', external_id); |
86 |
console.log('Items request failed with: ' + textStatus); |
87 |
console.log(data); |
88 |
} |
89 |
); |
90 |
|
91 |
let checkout = availability.then( |
92 |
function(data, textStatus, jqXHR) { |
93 |
let result; |
94 |
// blocked |
95 |
if (Object.keys(data.blockers).length >= 1) { |
96 |
for (const key in data.blockers) { |
97 |
if (data.blockers.hasOwnProperty(key)) { |
98 |
addResult('danger', `${key}`, `${data.blockers[key]}`); |
99 |
console.log(`${key}: ${data.blockers[key]}`); |
100 |
} |
101 |
} |
102 |
} |
103 |
// requires confirmation |
104 |
else if (Object.keys(data.confirms).length >= 1) { |
105 |
for (const key in data.confirms) { |
106 |
if (data.confirms.hasOwnProperty(key)) { |
107 |
addResult('warning', `${key}`, `${data.confirms[key]}`); |
108 |
} |
109 |
} |
110 |
$('#checkout_barcode').prop("readonly", true); |
111 |
$('#checkoutSubmit').replaceWith('<input type="hidden" id="item_id" value="' + item_id + '"><input type="hidden" id="confirm_token" value="' + data.confirmation_token + '"><button type="submit" id="checkoutConfirm" class="btn btn-warning">Confirm</button>'); |
112 |
console.log("Request succeeded confirmation is required"); |
113 |
} |
114 |
// straight checkout |
115 |
else { |
116 |
let params = { |
117 |
"checkout_id": undefined, |
118 |
"patron_id": logged_in_user_id, |
119 |
"item_id": item_id, |
120 |
"due_date": undefined, |
121 |
"library_id": undefined, |
122 |
"issuer_id": undefined, |
123 |
"checkin_date": undefined, |
124 |
"last_renewed_date": undefined, |
125 |
"renewals_count": undefined, |
126 |
"unseen_renewals": undefined, |
127 |
"auto_renew": undefined, |
128 |
"auto_renew_error": undefined, |
129 |
"timestamp": undefined, |
130 |
"checkout_date": undefined, |
131 |
"onsite_checkout": false, |
132 |
"note": undefined, |
133 |
"note_date": undefined, |
134 |
"note_seen": undefined, |
135 |
}; |
136 |
result = $.ajax({ |
137 |
url: '/api/v1/public/checkouts', |
138 |
type: 'POST', |
139 |
data: JSON.stringify(params), |
140 |
contentType: "json" |
141 |
}); |
142 |
} |
143 |
|
144 |
// warnings to display |
145 |
if (Object.keys(data.warnings).length >= 1) { |
146 |
for (const key in data.warnings) { |
147 |
if (data.warnings.hasOwnProperty(key)) { |
148 |
addResult('info', `${key}`, `${data.warnings[key]}`); |
149 |
} |
150 |
} |
151 |
} |
152 |
|
153 |
// return a rejected promise if we've reached here |
154 |
return result ? result : $.Deferred().reject('Checkout halted'); |
155 |
}, |
156 |
function(data, textStatus, jqXHR) { |
157 |
console.log('Items request failed with: ' + textStatus); |
158 |
console.log(data); |
159 |
} |
160 |
); |
161 |
|
162 |
checkout.then( |
163 |
function(data, textStatus, jqXHR) { |
164 |
$('#checkout_barcode').val("").prop("readonly", false).focus(); |
165 |
$('#checkoutResults').replaceWith('<div id="checkoutResults" class="alert alert-success">' + _("Item '%s' was checked out").format(external_id) + '</div>'); |
166 |
$('#checkoutsTable').show(); |
167 |
$('#checkoutsTable > tbody').append('<tr><td>' + external_id +'</td><td>'+ $date(data.due_date) +'</td></tr>'); |
168 |
checkouts_count++; |
169 |
$('#checkoutsCount').html(checkouts_count); |
170 |
// data retrieved from url2 as provided by the first request |
171 |
}, |
172 |
function(data, textStatus, jqXHR) { |
173 |
console.log("checkout.then failed"); |
174 |
} |
175 |
); |
176 |
|
177 |
}); |
178 |
|
179 |
$('#checkoutModal').on('click', '#checkoutConfirm', function(e) { |
180 |
let external_id = $('#checkout_barcode').val(); |
181 |
let item_id = $('#item_id').val(); |
182 |
let token = $('#confirm_token').val(); |
183 |
let params = { |
184 |
"checkout_id": undefined, |
185 |
"patron_id": logged_in_user_id, |
186 |
"item_id": item_id, |
187 |
"due_date": undefined, |
188 |
"library_id": undefined, |
189 |
"issuer_id": undefined, |
190 |
"checkin_date": undefined, |
191 |
"last_renewed_date": undefined, |
192 |
"renewals_count": undefined, |
193 |
"unseen_renewals": undefined, |
194 |
"auto_renew": undefined, |
195 |
"auto_renew_error": undefined, |
196 |
"timestamp": undefined, |
197 |
"checkout_date": undefined, |
198 |
"onsite_checkout": false, |
199 |
"note": undefined, |
200 |
"note_date": undefined, |
201 |
"note_seen": undefined, |
202 |
}; |
203 |
let checkout = $.ajax({ |
204 |
url: '/api/v1/public/checkouts?confirmation=' + token, |
205 |
type: 'POST', |
206 |
data: JSON.stringify(params), |
207 |
contentType: "json" |
208 |
}); |
209 |
|
210 |
checkout.then( |
211 |
function(data, textStatus, jqXHR) { |
212 |
$('#item_id').remove; |
213 |
$('#confirm_token').remove; |
214 |
$('#checkoutResults').replaceWith('<div id="checkoutResults" class="alert alert-success">' + _("Item '%s' was checked out").format(external_id) + '</div>'); |
215 |
$('#checkout_barcode').val("").prop("readonly", false).focus(); |
216 |
$('#checkoutsTable').show(); |
217 |
$('#checkoutsTable > tbody').append('<tr><td>' + external_id +'</td><td>'+ $date(data.due_date) +'</td></tr>'); |
218 |
$('#checkoutConfirm').replaceWith('<button type="submit" id="checkoutSubmit" class="btn btn-primary">Submit</button>'); |
219 |
checkouts_count++; |
220 |
$('#checkoutsCount').html(checkouts_count); |
221 |
// data retrieved from url2 as provided by the first request |
222 |
}, |
223 |
function(data, textStatus, jqXHR) { |
224 |
console.log("checkout.then failed"); |
225 |
} |
226 |
); |
227 |
}); |
228 |
}); |