From 061cf5f837a7073dc282e68f01fab90f78f8c2f0 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 23 Jan 2024 16:04:32 +0100 Subject: [PATCH] Bug 35506: Move the checkouts table load delay logic out of document ready --- koha-tmpl/intranet-tmpl/prog/js/checkouts.js | 1198 +++++++++--------- 1 file changed, 600 insertions(+), 598 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/js/checkouts.js b/koha-tmpl/intranet-tmpl/prog/js/checkouts.js index bebe9866795..1614d978d41 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/checkouts.js +++ b/koha-tmpl/intranet-tmpl/prog/js/checkouts.js @@ -1,478 +1,187 @@ /* global __ */ -$(document).ready(function() { - var loadIssuesTableDelayTimeoutId; - - var barcodefield = $("#barcode"); - var onHoldDueDateSet = false; - - var onHoldChecked = function() { - var isChecked = false; - $('input[data-on-reserve]').each(function() { - if ($(this).is(':checked')) { - isChecked=true; + function RefreshIssuesTable() { + var table = $('#issues-table').DataTable(); + var renewchecked = $('input[name=renew]:checked').map(function(){ + return this.value; + }).get(); + var checkinchecked = $('input[name=checkin]:checked').map(function(){ + return this.value; + }).get(); + table.ajax.reload( function() { + $('#RenewChecked, #CheckinChecked').prop('disabled' , true ); + if ( renewchecked.length ) { + $('#RenewChecked').prop('disabled' , false ); + renewchecked.forEach( function(checked) { + $('.renew[value="'+checked+'"]').prop('checked' , true ); + }); + } + if ( checkinchecked.length ) { + $('#CheckinChecked').prop('disabled' , false ); + checkinchecked.forEach( function(checked) { + $('.checkin[value="'+checked+'"]').prop('checked' , true ); + }); } + var checkout_count = table.page.info().recordsTotal; + $('.checkout_count').text(checkout_count); }); - return isChecked; - }; + } - var showHideOnHoldRenewal = function() { - // Display the date input - if (onHoldChecked()) { - $('#newonholdduedate').show() - } else { - $('#newonholdduedate').hide(); - } - }; + function LoadIssuesTable() { + $('#issues-table-loading-message').hide(); + $('#issues-table').show(); + $('#issues-table-actions').show(); + var msg_loading = __('Loading... you may continue scanning.'); + issuesTable = KohaTable("issues-table", { + "oLanguage": { + "sEmptyTable" : msg_loading, + "sProcessing": msg_loading, + }, + "bAutoWidth": false, + "dom": '<"table_controls"B>rt', + "aoColumns": [ + { + "mDataProp": function( oObj ) { + return oObj.sort_order; + } + }, + { + "mDataProp": function( oObj ) { + if ( oObj.issued_today ) { + return "" + __("Today's checkouts") + ""; + } else { + return "" + __("Previous checkouts") + ""; + } + } + }, + { + "mDataProp": "date_due", + "bVisible": false, + }, + { + "iDataSort": 2, // Sort on hidden unformatted date due column + "mDataProp": function( oObj ) { + let date_due_formatted = $datetime(oObj.date_due, { as_due_date: true, no_tz_adjust: true }); + var due = oObj.date_due_overdue + ? "" + date_due_formatted + "" + : date_due_formatted; - // Handle the select all/none links for checkouts table columns - $("#CheckAllRenewals").on("click",function(){ - $("#UncheckAllCheckins").click(); - $(".renew:visible").prop("checked", true); - $('#RenewChecked').prop('disabled', !$('.renew:checked').length ); - showHideOnHoldRenewal(); - return false; - }); - $("#UncheckAllRenewals").on("click",function(){ - $(".renew:visible").prop("checked", false); - $('#RenewChecked').prop('disabled', !$('.renew:checked').length ); - showHideOnHoldRenewal(); - return false; - }); + due = "" + due + ""; - $("#CheckAllCheckins").on("click",function(){ - $("#UncheckAllRenewals").click(); - $(".checkin:visible").prop("checked", true); - $('#CheckinChecked').prop('disabled', !$('.checkin:checked').length ); - return false; - }); - $("#UncheckAllCheckins").on("click",function(){ - $(".checkin:visible").prop("checked", false); - $('#CheckinChecked').prop('disabled', !$('.checkin:checked').length ); - return false; - }); + if ( oObj.lost && oObj.claims_returned ) { + due += "" + oObj.lost.escapeHtml() + ""; + } else if ( oObj.lost ) { + due += "" + oObj.lost.escapeHtml() + ""; + } - $("#newduedate").on("change", function() { - if (!onHoldDueDateSet) { - $('#newonholdduedate input').val($('#newduedate').val()); - } - }); + if ( oObj.damaged ) { + due += "" + oObj.damaged.escapeHtml() + ""; + } - $("#newonholdduedate").on("change", function() { - onHoldDueDateSet = true; - }); + var patron_note = " "; + due +="
" + patron_note; - // Don't allow both return and renew checkboxes to be checked - $(document).on("change", '.renew', function(){ - if ( $(this).is(":checked") ) { - $( "#checkin_" + $(this).val() ).prop("checked", false); - } - $('#CheckinChecked').prop('disabled', !$('.checkin:checked').length ); - $('#RenewChecked').prop('disabled', !$('.renew:checked').length ); - }); - $(document).on("change", '.checkin', function(){ - if ( $(this).is(":checked") ) { - $( "#renew_" + $(this).val() ).prop("checked", false); - } - $('#CheckinChecked').prop('disabled', !$('.checkin:checked').length ); - $('#RenewChecked').prop('disabled', !$('.renew:checked').length ); - }); + return due; + } + }, + { + "mDataProp": function ( oObj ) { + let title = "" + + (oObj.title ? oObj.title.escapeHtml() : '' ); - // Display on hold due dates input when an on hold item is - // selected - $(document).on('change', '.renew', function(){ - showHideOnHoldRenewal(); - }); + $.each(oObj.subtitle, function( index, value ) { + title += " " + value.escapeHtml(); + }); - $("#output_format > option:first-child").attr("selected", "selected"); - $("select[name='csv_profile_id']").hide(); - $(document).on("change", '#issues-table-output-format', function(){ - if ( $(this).val() == 'csv' ) { - $("select[name='csv_profile_id']").show(); - } else { - $("select[name='csv_profile_id']").hide(); - } - }); + title += " " + oObj.part_number + " " + oObj.part_name; - // Clicking the table cell checks the checkbox inside it - $(document).on("click", 'td', function(e){ - if(e.target.tagName.toLowerCase() == 'td'){ - $(this).find("input:checkbox:visible").each( function() { - $(this).click(); - }); - } - }); + if ( oObj.enumchron ) { + title += " (" + oObj.enumchron.escapeHtml() + ")"; + } - // Handle renewals and returns - $("#CheckinChecked").on("click",function(){ + title += ""; - let refresh_table = true; - $(".checkin:checked:visible").each(function() { - itemnumber = $(this).val(); + if ( oObj.author ) { + title += " " + __("by _AUTHOR_").replace( "_AUTHOR_", " " + oObj.author.escapeHtml() ); + } - $(this).replaceWith(""); + if ( oObj.itemnotes ) { + var span_class = "text-muted"; + if ( flatpickr.formatDate( new Date(oObj.issuedate), "Y-m-d" ) == ymd ){ + span_class = "circ-hlt"; + } + title += " - " + oObj.itemnotes.escapeHtml() + ""; + } - params = { - itemnumber: itemnumber, - borrowernumber: borrowernumber, - branchcode: branchcode, - exempt_fine: $("#exemptfine").is(':checked') - }; + if ( oObj.itemnotes_nonpublic ) { + var span_class = "text-danger"; + if ( flatpickr.formatDate( new Date(oObj.issuedate), "Y-m-d" ) == ymd ){ + span_class = "circ-hlt"; + } + title += " - " + oObj.itemnotes_nonpublic.escapeHtml() + ""; + } - $.post({ - url: "/cgi-bin/koha/svc/checkin", - data: params, - success: function( data ) { - id = "#checkin_" + data.itemnumber; + var onsite_checkout = ''; + if ( oObj.onsite_checkout == 1 ) { + onsite_checkout += " (" + __("On-site checkout") + ")"; + } - content = ""; - if ( data.returned ) { - content = __("Checked in"); - $(id).parent().parent().addClass('ok'); - $('#date_due_' + data.itemnumber).html( __("Checked in") ); - if ( data.patronnote != null ) { - $('.patron_note_' + data.itemnumber).html( __("Patron note") + ": " + data.patronnote); + if ( oObj.recalled == 1 ) { + title += " - " + __("This item has been recalled and the due date updated") + "."; } - } else { - content = __("Unable to check in"); - $(id).parent().parent().addClass('warn'); - refresh_table = false; - } - $(id).replaceWith( content ); - }, - dataType: "json", - async: false, - }); - }); - // Refocus on barcode field if it exists - if ( $("#barcode").length ) { - $("#barcode").focus(); - } + title += " " + + "" + + (oObj.barcode ? oObj.barcode.escapeHtml() : "") + + "" + + onsite_checkout - if ( refresh_table ) { - RefreshIssuesTable(); - } - $('#RenewChecked, #CheckinChecked').prop('disabled' , true ); - // Prevent form submit - return false; - }); - $("#RenewChecked").on("click",function(){ - let refresh_table = true; - $(".confirm:checked:visible").each(function() { - itemnumber = $(this).val(); - id = "#checkin_" + itemnumber; - materials = $(this).data('materials'); - - $(this).replaceWith("" + __("Confirm") + " (" + materials + "): "); - $(id).parent().parent().addClass('warn'); - }); - - $(".renew:checked:visible").each(function() { - var override_limit = $("#override_limit").is(':checked') ? 1 : 0; - - var isOnReserve = $(this).data().hasOwnProperty('onReserve'); - - var itemnumber = $(this).val(); - - $(this).parent().parent().replaceWith(""); - - var params = { - itemnumber: itemnumber, - borrowernumber: borrowernumber, - branchcode: branchcode, - override_limit: override_limit - }; - - if (UnseenRenewals) { - var ren = $("#renew_as_unseen_checkbox"); - var renew_unseen = ren.length > 0 && ren.is(':checked') ? 1 : 0; - params.seen = renew_unseen === 1 ? 0 : 1; - } - - // Determine which due date we need to use - var dueDate = isOnReserve ? - $("#newonholdduedate input").val() : - $("#newduedate").val(); - - if (dueDate && dueDate.length > 0) { - params.date_due = dueDate - } - - $.post({ - url: "/cgi-bin/koha/svc/renew", - data: params, - success: function( data ) { - var id = "#renew_" + data.itemnumber; - - var content = ""; - if ( data.renew_okay ) { - content = __("Renewed, due:") + " " + data.date_due; - $('#date_due_' + data.itemnumber).replaceWith( data.date_due ); - } else { - content = __("Renew failed:") + " "; - if ( data.error == "no_checkout" ) { - content += __("not checked out"); - } else if ( data.error == "too_many" ) { - content += __("too many renewals"); - } else if ( data.error == "too_unseen" ) { - content += __("too many consecutive renewals without being seen by the library"); - } else if ( data.error == "on_reserve" ) { - content += __("on hold"); - } else if ( data.error == "restriction" ) { - content += __("Not allowed: patron restricted"); - } else if ( data.error == "overdue" ) { - content += __("Not allowed: overdue"); - } else if ( data.error == 'no_open_days' ) { - content += __('Unable to find an open day'); - } else if ( data.error ) { - content += data.error; - } else { - content += __("reason unknown"); - } - refresh_table = false; + return title; + }, + "sType": "anti-the" + }, + { + "mDataProp": function ( oObj ) { + return oObj.recordtype_description.escapeHtml(); } - - $(id).replaceWith( content ); - }, - dataType: "json", - async: false, - }); - }); - - // Refocus on barcode field if it exists - if ( $("#barcode").length ) { - $("#barcode").focus(); - } - - if ( refresh_table ) { - RefreshIssuesTable(); - } - // Prevent form submit - return false; - }); - - $("#RenewAll").on("click",function(){ - $("#CheckAllRenewals").click(); - $("#UncheckAllCheckins").click(); - showHideOnHoldRenewal(); - $("#RenewChecked").click(); - $('#RenewChecked').prop('disabled' , true ); - // Prevent form submit - return false; - }); - - var ymd = flatpickr.formatDate(new Date(), "Y-m-d"); - - $('#issues-table-load-immediately').change(function(){ - if ( this.checked && typeof issuesTable === 'undefined') { - $('#issues-table-load-now-button').click(); - } - barcodefield.focus(); - }); - $('#issues-table-load-now-button').click(function(){ - if ( loadIssuesTableDelayTimeoutId ) clearTimeout(loadIssuesTableDelayTimeoutId); - LoadIssuesTable(); - barcodefield.focus(); - return false; - }); - - if ( Cookies.get("issues-table-load-immediately-" + script) == "true" ) { - if ( LoadCheckoutsTableDelay ) { - loadIssuesTableDelayTimeoutId = setTimeout( function(){ LoadIssuesTable() }, LoadCheckoutsTableDelay * 1000); - } else { - LoadIssuesTable(); - } - $('#issues-table-load-immediately').prop('checked', true); - } else { - $('#issues-table-load-delay').hide(); - } - $('#issues-table-load-immediately').on( "change", function(){ - Cookies.set("issues-table-load-immediately-" + script, $(this).is(':checked'), { expires: 365, sameSite: 'Lax' }); - }); - - function RefreshIssuesTable() { - var table = $('#issues-table').DataTable(); - var renewchecked = $('input[name=renew]:checked').map(function(){ - return this.value; - }).get(); - var checkinchecked = $('input[name=checkin]:checked').map(function(){ - return this.value; - }).get(); - table.ajax.reload( function() { - $('#RenewChecked, #CheckinChecked').prop('disabled' , true ); - if ( renewchecked.length ) { - $('#RenewChecked').prop('disabled' , false ); - renewchecked.forEach( function(checked) { - $('.renew[value="'+checked+'"]').prop('checked' , true ); - }); - } - if ( checkinchecked.length ) { - $('#CheckinChecked').prop('disabled' , false ); - checkinchecked.forEach( function(checked) { - $('.checkin[value="'+checked+'"]').prop('checked' , true ); - }); - } - var checkout_count = table.page.info().recordsTotal; - $('.checkout_count').text(checkout_count); - }); - } - - function LoadIssuesTable() { - $('#issues-table-loading-message').hide(); - $('#issues-table').show(); - $('#issues-table-actions').show(); - var msg_loading = __('Loading... you may continue scanning.'); - issuesTable = KohaTable("issues-table", { - "oLanguage": { - "sEmptyTable" : msg_loading, - "sProcessing": msg_loading, - }, - "bAutoWidth": false, - "dom": '<"table_controls"B>rt', - "aoColumns": [ + }, { - "mDataProp": function( oObj ) { - return oObj.sort_order; + "mDataProp": function ( oObj ) { + return oObj.itemtype_description.escapeHtml(); } }, { - "mDataProp": function( oObj ) { - if ( oObj.issued_today ) { - return "" + __("Today's checkouts") + ""; - } else { - return "" + __("Previous checkouts") + ""; - } + "mDataProp": function ( oObj ) { + return ( oObj.collection ? oObj.collection.escapeHtml() : '' ); } }, { - "mDataProp": "date_due", + "mDataProp": function ( oObj ) { + return ( oObj.location ? oObj.location.escapeHtml() : '' ); + } + }, + { + "mDataProp": function ( oObj ) { + return (oObj.homebranch ? oObj.homebranch.escapeHtml() : '' ); + } + }, + { + "mDataProp": "issuedate", "bVisible": false, }, { - "iDataSort": 2, // Sort on hidden unformatted date due column + "iDataSort": 10, // Sort on hidden unformatted issuedate column "mDataProp": function( oObj ) { - let date_due_formatted = $datetime(oObj.date_due, { as_due_date: true, no_tz_adjust: true }); - var due = oObj.date_due_overdue - ? "" + date_due_formatted + "" - : date_due_formatted; - - due = "" + due + ""; - - if ( oObj.lost && oObj.claims_returned ) { - due += "" + oObj.lost.escapeHtml() + ""; - } else if ( oObj.lost ) { - due += "" + oObj.lost.escapeHtml() + ""; - } - - if ( oObj.damaged ) { - due += "" + oObj.damaged.escapeHtml() + ""; - } - - var patron_note = " "; - due +="
" + patron_note; - - return due; - } - }, - { - "mDataProp": function ( oObj ) { - let title = "" - + (oObj.title ? oObj.title.escapeHtml() : '' ); - - $.each(oObj.subtitle, function( index, value ) { - title += " " + value.escapeHtml(); - }); - - title += " " + oObj.part_number + " " + oObj.part_name; - - if ( oObj.enumchron ) { - title += " (" + oObj.enumchron.escapeHtml() + ")"; - } - - title += ""; - - if ( oObj.author ) { - title += " " + __("by _AUTHOR_").replace( "_AUTHOR_", " " + oObj.author.escapeHtml() ); - } - - if ( oObj.itemnotes ) { - var span_class = "text-muted"; - if ( flatpickr.formatDate( new Date(oObj.issuedate), "Y-m-d" ) == ymd ){ - span_class = "circ-hlt"; - } - title += " - " + oObj.itemnotes.escapeHtml() + ""; - } - - if ( oObj.itemnotes_nonpublic ) { - var span_class = "text-danger"; - if ( flatpickr.formatDate( new Date(oObj.issuedate), "Y-m-d" ) == ymd ){ - span_class = "circ-hlt"; - } - title += " - " + oObj.itemnotes_nonpublic.escapeHtml() + ""; - } - - var onsite_checkout = ''; - if ( oObj.onsite_checkout == 1 ) { - onsite_checkout += " (" + __("On-site checkout") + ")"; - } - - if ( oObj.recalled == 1 ) { - title += " - " + __("This item has been recalled and the due date updated") + "."; - } - - title += " " - + "" - + (oObj.barcode ? oObj.barcode.escapeHtml() : "") - + "" - + onsite_checkout - - return title; - }, - "sType": "anti-the" - }, - { - "mDataProp": function ( oObj ) { - return oObj.recordtype_description.escapeHtml(); - } - }, - { - "mDataProp": function ( oObj ) { - return oObj.itemtype_description.escapeHtml(); - } - }, - { - "mDataProp": function ( oObj ) { - return ( oObj.collection ? oObj.collection.escapeHtml() : '' ); - } - }, - { - "mDataProp": function ( oObj ) { - return ( oObj.location ? oObj.location.escapeHtml() : '' ); - } - }, - { - "mDataProp": function ( oObj ) { - return (oObj.homebranch ? oObj.homebranch.escapeHtml() : '' ); - } - }, - { - "mDataProp": "issuedate", - "bVisible": false, - }, - { - "iDataSort": 10, // Sort on hidden unformatted issuedate column - "mDataProp": function( oObj ) { - return $datetime(oObj.issuedate, { no_tz_adjust: true }); + return $datetime(oObj.issuedate, { no_tz_adjust: true }); } }, { @@ -574,180 +283,473 @@ $(document).ready(function() { + __("Can no longer be auto-renewed - number of checkout days exceeded") + ""; - span_style = "display: none"; - span_class = "renewals-allowed"; - } else if ( oObj.can_renew_error == "auto_too_much_oweing" ) { - msg += "" - + __("Automatic renewal failed, patron has unpaid fines") - + ""; + span_style = "display: none"; + span_class = "renewals-allowed"; + } else if ( oObj.can_renew_error == "auto_too_much_oweing" ) { + msg += "" + + __("Automatic renewal failed, patron has unpaid fines") + + ""; + + span_style = "display: none"; + span_class = "renewals-allowed"; + } else if ( oObj.can_renew_error == "auto_account_expired" ) { + msg += "" + + __("Automatic renewal failed, account expired") + + ""; + + span_style = "display: none"; + span_class = "renewals-allowed"; + } else if ( oObj.can_renew_error == "onsite_checkout" ) { + // Don't display something if it's an onsite checkout + } else if ( oObj.can_renew_error == "item_denied_renewal" ) { + content += "" + + __("Renewal denied by syspref") + + ""; + + span_style = "display: none"; + span_class = "renewals-allowed"; + } else { + msg += "" + + oObj.can_renew_error + + ""; + + span_style = "display: none"; + span_class = "renewals-allowed"; + } + + var can_force_renew = ( oObj.onsite_checkout == 0 ) && + ( oObj.can_renew_error != "on_reserve" || (oObj.can_renew_error == "on_reserve" && AllowRenewalOnHoldOverride)) + ? true : false; + var can_renew = ( oObj.renewals_remaining > 0 && ( !oObj.can_renew_error || oObj.can_renew_error == "too_unseen" )); + content += ""; + if ( can_renew || can_force_renew ) { + content += "" + oObj.renewals_count + ""; + content += "" + + "" + + ""; + } + content += msg; + if ( can_renew || can_force_renew ) { + content += "("; + content += __("%s of %s renewals remaining").format(oObj.renewals_remaining, oObj.renewals_allowed); + if (UnseenRenewals && oObj.unseen_allowed) { + content += __(" and %s of %s unseen renewals remaining").format(oObj.unseen_remaining, oObj.unseen_allowed); + } + content += ")"; + } + if(oObj.auto_renew){ + content += "("; + content += __("Scheduled for automatic renewal"); + content += ")"; + } + + return content; + } + }, + { + "bSortable": false, + "bVisible": AllowCirculate ? true : false, + "mDataProp": function ( oObj ) { + if ( oObj.can_renew_error == "recalled" ) { + return "" + __("Recalled") + ""; + } else if ( oObj.can_renew_error == "on_reserve" ) { + return "" + __("On hold") + ""; + } else if ( oObj.materials ) { + return ""; + } else { + return ""; + } + } + }, + { + "bVisible": ClaimReturnedLostValue ? true : false, + "bSortable": false, + "mDataProp": function ( oObj ) { + let content = ""; + + if ( oObj.return_claim_id ) { + content = '' + oObj.return_claim_created_on_formatted + ''; + } else if ( ClaimReturnedLostValue ) { + content = ' ' + __("Claim returned") + ''; + } else { + content = ' ' + __("Claim returned") + ''; + } + return content; + } + }, + { + "bVisible": exports_enabled == 1 ? true : false, + "bSortable": false, + "mDataProp": function ( oObj ) { + var s = ""; + + s += ""; + return s; + } + } + ], + "fnFooterCallback": function ( nRow, aaData, iStart, iEnd, aiDisplay ) { + var total_charge = 0; + var total_fine = 0; + var total_price = 0; + for ( var i=0; i < aaData.length; i++ ) { + total_charge += aaData[i]['charge'] * 1; + total_fine += aaData[i]['fine'] * 1; + total_price += aaData[i]['price'] * 1; + } + $("#totaldue").html(total_charge.format_price() ); + $("#totalfine").html(total_fine.format_price() ); + $("#totalprice").html(total_price.format_price() ); + }, + "bPaginate": false, + "bProcessing": true, + "bServerSide": false, + "sAjaxSource": '/cgi-bin/koha/svc/checkouts', + "fnServerData": function ( sSource, aoData, fnCallback ) { + aoData.push( { "name": "borrowernumber", "value": borrowernumber } ); + + $.getJSON( sSource, aoData, function (json) { + fnCallback(json) + } ); + }, + "rowGroup":{ + "dataSrc": "issued_today", + "startRender": function ( rows, group ) { + if ( group ) { + return __("Today's checkouts"); + } else { + return __("Previous checkouts"); + } + } + }, + "fnInitComplete": function(oSettings, json) { + // Build a summary of checkouts grouped by itemtype + var checkoutsByItype = json.aaData.reduce(function (obj, row) { + obj[row.type_for_stat] = (obj[row.type_for_stat] || 0) + 1; + return obj; + }, {}); + var ul = $('