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