/* global __ */ function CheckRenewCheckinBoxes() { $('#RenewChecked').prop('disabled', !$('.renew:checked').length ); $('#CheckinChecked').prop('disabled', !$('.checkin:checked').length ); } 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() { var checkout_count = table.page.info().recordsTotal; $('.checkout_count').text(checkout_count); renewchecked.forEach( function(item) { $('.renew[value="'+item+'"]').prop('checked' , true ); }); checkinchecked.forEach( function(item) { $('.checkin[value="'+item+'"]').prop('checked' , true ); }); CheckRenewCheckinBoxes(); }); } 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", { "language": { "emptyTable": msg_loading, "processing": msg_loading, }, "autoWidth": false, "dom": '<"table_controls"B>rt', "columns": [ { "data": function( oObj ) { return oObj.sort_order; } }, { "data": function( oObj ) { if ( oObj.issued_today ) { return "" + __("Today's checkouts") + ""; } else { return "" + __("Previous checkouts") + ""; } } }, { "data": "date_due", "visible": false, }, { "orderData": 2, // Sort on hidden unformatted date due column "data": 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; } }, { "data": function ( oObj ) { let title = "" + (oObj.title ? oObj.title.escapeHtml() : '' ); var ymd = flatpickr.formatDate(new Date(), "Y-m-d"); $.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; }, "type": "anti-the" }, { "data": function ( oObj ) { return oObj.recordtype_description.escapeHtml(); } }, { "data": function ( oObj ) { return oObj.itemtype_description.escapeHtml(); } }, { "data": function ( oObj ) { return ( oObj.collection ? oObj.collection.escapeHtml() : '' ); } }, { "data": function ( oObj ) { return ( oObj.location ? oObj.location.escapeHtml() : '' ); } }, { "data": function ( oObj ) { return (oObj.homebranch ? oObj.homebranch.escapeHtml() : '' ); } }, { "data": "issuedate", "visible": false, }, { "orderData": 10, // Sort on hidden unformatted issuedate column "data": function( oObj ) { return $datetime(oObj.issuedate, { no_tz_adjust: true }); } }, { "data": function ( oObj ) { return (oObj.branchname ? oObj.branchname.escapeHtml() : '' ); } }, { "data": function ( oObj ) { return ( oObj.itemcallnumber ? oObj.itemcallnumber.escapeHtml() : '' ); } }, { "data": function ( oObj ) { return ( oObj.copynumber ? oObj.copynumber.escapeHtml() : '' ); } }, { "data": function ( oObj ) { if ( ! oObj.charge ) oObj.charge = 0; return '' + parseFloat(oObj.charge).format_price() + ''; }, "className": "nowrap" }, { "data": function ( oObj ) { if ( ! oObj.fine ) oObj.fine = 0; return '' + parseFloat(oObj.fine).format_price() + ''; }, "className": "nowrap" }, { "data": function ( oObj ) { if ( ! oObj.price ) oObj.price = 0; return '' + parseFloat(oObj.price).format_price() + ''; }, "className": "nowrap" }, { "orderable": false, "visible": AllowCirculate ? true : false, "data": function ( oObj ) { var content = ""; var msg = ""; var span_style = ""; var span_class = ""; if ( oObj.can_renew ) { // Do nothing } else if ( oObj.can_renew_error == "recalled" ) { msg += "" + "" + __("Recalled") + "" + ""; span_style = "display: none"; span_class = "renewals-allowed-recalled"; } else if ( oObj.can_renew_error == "booked" ) { msg += "" + "" + __("Booked") + "" + ""; span_style = "display: none"; span_class = "renewals-allowed-booked"; } else if ( oObj.can_renew_error == "on_reserve" ) { msg += "" +"" + __("On hold") + "" + ""; span_style = "display: none"; span_class = "renewals-allowed-on_reserve"; } else if ( oObj.can_renew_error == "too_many" ) { msg += "" + __("Not renewable") + ""; span_style = "display: none"; span_class = "renewals-allowed"; } else if ( oObj.can_renew_error == "too_unseen" ) { msg += "" + __("Must be renewed at the library") + ""; span_class = "renewals-allowed"; } else if ( oObj.can_renew_error == "restriction" ) { msg += "" + __("Not allowed: patron restricted") + ""; span_style = "display: none"; span_class = "renewals-allowed"; } else if ( oObj.can_renew_error == "overdue" ) { msg += "" + __("Not allowed: overdue") + ""; span_style = "display: none"; span_class = "renewals-allowed"; } else if ( oObj.can_renew_error == "too_soon" ) { msg += "" + __("No renewal before %s").format(oObj.can_renew_date) + ""; span_style = "display: none"; span_class = "renewals-allowed"; } else if ( oObj.can_renew_error == "auto_too_late" ) { msg += "" + __("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_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; } }, { "orderable": false, "visible": AllowCirculate ? true : false, "data": 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 __("Confirm (%s)".format(oObj.materials.escapeHtml())); } else { return ""; } } }, { "visible": ClaimReturnedLostValue ? true : false, "orderable": false, "data": 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; } }, { "visible": exports_enabled == 1 ? true : false, "orderable": false, "data": function ( oObj ) { var s = ""; s += ""; return s; } } ], "footerCallback": 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() ); }, "paging": false, "processing": true, "serverSide": 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"); } } }, "initComplete": function(oSettings, json) { CheckRenewCheckinBoxes(); // 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 = $('
    '); Object.keys(checkoutsByItype).sort().forEach(function (itype) { var li = $('
  • ') .append($('').html(itype || __("No itemtype"))) .append(': ' + checkoutsByItype[itype]); ul.append(li); }) $('
    ') .addClass('checkouts-by-itemtype') .append($('').html( __("Number of checkouts by item type") )) .append(ul) .insertBefore(oSettings.nTableWrapper) }, }, table_settings_issues_table); if ( $("#issues-table").length ) { $("#issues-table_processing").position({ of: $( "#issues-table" ), collision: "none" }); } } var loadIssuesTableDelayTimeoutId; var barcodefield = $("#barcode"); $('#issues-table-load-now-button').click(function(){ if ( loadIssuesTableDelayTimeoutId ) clearTimeout(loadIssuesTableDelayTimeoutId); LoadIssuesTable(); barcodefield.focus(); return false; }); $(document).ready(function() { var onHoldDueDateSet = false; var onHoldChecked = function() { var isChecked = false; $('input[data-on-reserve]').each(function() { if ($(this).is(':checked')) { isChecked=true; } }); return isChecked; }; var showHideOnHoldRenewal = function() { // Display the date input if (onHoldChecked()) { $('#newonholdduedate').show() } else { $('#newonholdduedate').hide(); } }; // Handle the select all/none links for checkouts table columns $("#CheckAllRenewals").on("click",function(){ $("#UncheckAllCheckins").click(); $(".renew:visible").prop("checked", true); CheckRenewCheckinBoxes(); showHideOnHoldRenewal(); return false; }); $("#UncheckAllRenewals").on("click",function(){ $(".renew:visible").prop("checked", false); CheckRenewCheckinBoxes(); showHideOnHoldRenewal(); return false; }); $("#CheckAllCheckins").on("click",function(){ $("#UncheckAllRenewals").click(); $(".checkin:visible").prop("checked", true); CheckRenewCheckinBoxes(); return false; }); $("#UncheckAllCheckins").on("click",function(){ $(".checkin:visible").prop("checked", false); CheckRenewCheckinBoxes(); return false; }); $("#newduedate").on("change", function() { if (!onHoldDueDateSet) { $('#newonholdduedate input').val($('#newduedate').val()); } }); $("#newonholdduedate").on("change", function() { onHoldDueDateSet = true; }); // 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); } CheckRenewCheckinBoxes(); }); $(document).on("change", '.checkin', function(){ if ( $(this).is(":checked") ) { $( "#renew_" + $(this).val() ).prop("checked", false); } CheckRenewCheckinBoxes(); }); // Display on hold due dates input when an on hold item is // selected $(document).on('change', '.renew', function(){ showHideOnHoldRenewal(); }); $("#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(); } }); // 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(); }); } }); // Handle renewals and returns $("#CheckinChecked").on("click",function(e){ e.preventDefault(); let refresh_table = true; function checkin(item_id){ params = { item_id, patron_id: borrowernumber, library_id: branchcode, exempt_fine: $("#exemptfine").is(':checked') }; const client = APIClient.circulation; return client.checkins.create(params).then( success => { id = "#checkin_" + item_id; content = ""; if ( success.returned ) { content = __("Checked in"); $(id).parent().parent().addClass('ok'); $('#date_due_' + success.itemnumber).html( __("Checked in") ); if ( success.patronnote != null ) { $('.patron_note_' + success.itemnumber).html( __("Patron note") + ": " + success.patronnote); } } else { content = __("Unable to check in"); $(id).parent().parent().addClass('warn'); refresh_table = false; } $(id).parent().empty().append(content); }, error => { console.warn("Something wrong happened: %s".format(error)); } ) } function checkin_all(item_ids, fn){ let i = 0; function next(){ if (i < item_ids.length) { return fn(item_ids[i++]).then(function(id) { return next(); }); } } $(item_ids).each((i, id) => { $("#checkin_"+id).parent().append(""); $("#checkin_"+id).hide(); }); return next(); } let item_ids = $(".checkin:checked:visible").map((i, c) => c.value); checkin_all(item_ids, checkin).then(() => { // Refocus on barcode field if it exists if ( $("#barcode").length ) { $("#barcode").focus(); } if ( refresh_table ) { RefreshIssuesTable(); } $('#RenewChecked, #CheckinChecked').prop('disabled' , true ); }); CheckRenewCheckinBoxes(); // Prevent form submit return false; }); $("#RenewChecked").on("click",function(e){ e.preventDefault(); let refresh_table = true; function renew(item_id){ var override_limit = $("#override_limit").is(':checked') ? 1 : 0; $(this).parent().parent().replaceWith(""); var params = { item_id, patron_id: borrowernumber, library_id: 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; } var dueDate = $("#newduedate").val(); if (dueDate && dueDate.length > 0) { params.date_due = dueDate } const client = APIClient.circulation; return client.checkouts.renew(params).then( success => { var id = "#renew_" + success.itemnumber; var content = ""; if ( success.renew_okay ) { content = __("Renewed, due:") + " " + success.date_due; $('#date_due_' + success.itemnumber).replaceWith( success.date_due ); } else { content = __("Renew failed:") + " "; if ( success.error == "no_checkout" ) { content += __("not checked out"); } else if ( success.error == "too_many" ) { content += __("too many renewals"); } else if ( success.error == "too_unseen" ) { content += __("too many consecutive renewals without being seen by the library"); } else if ( success.error == "on_reserve" ) { content += __("on hold"); } else if ( success.error == "restriction" ) { content += __("Not allowed: patron restricted"); } else if ( success.error == "overdue" ) { content += __("Not allowed: overdue"); } else if ( success.error == 'no_open_days' ) { content += __('Unable to find an open day'); } else if ( success.error ) { content += success.error; } else { content += __("reason unknown"); } refresh_table = false; } $(id).replaceWith( content ); }, error => { console.warn("Something wrong happened: %s".format(error)); } ); } function renew_all(item_ids, fn){ let i = 0; function next(){ if (i < item_ids.length) { return fn(item_ids[i++]).then(function(id) { return next(); }); } } $(item_ids).each((i, id) => { $("#renew_"+id).parent().append(""); $("#renew_"+id).hide(); }); return next(); } let item_ids = $(".renew:checked:visible").map((i, c) => c.value); renew_all(item_ids, renew).then(() => { // Refocus on barcode field if it exists if ( $("#barcode").length ) { $("#barcode").focus(); } if ( refresh_table ) { RefreshIssuesTable(); } $('#RenewChecked, #CheckinChecked').prop('disabled' , true ); }); // 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(); }); 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' }); }); // Don't load relatives' issues table unless it is clicked on var relativesIssuesTable; $("#relatives-issues-tab").click( function() { if ( ! relativesIssuesTable ) { relativesIssuesTable = KohaTable("relatives-issues-table", { "autoWidth": false, "dom": '<"table_controls"B>rt', "order": [], "columns": [ { "data": "date_due", "visible": false, }, { "orderData": 0, // Sort on hidden unformatted date due column "data": function( oObj ) { var today = new Date(); var due = new Date( oObj.date_due ); let date_due_formatted = $datetime(oObj.date_due, { as_due_date: true, no_tz_adjust: true }); if ( today > due ) { return "" + date_due_formatted + ""; } else { return date_due_formatted; } } }, { "data": 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 = ""; 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 = ""; 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") + ")"; } title += " " + "" + (oObj.barcode ? oObj.barcode.escapeHtml() : "") + "" + onsite_checkout; return title; }, "type": "anti-the" }, { "data": function ( oObj ) { return oObj.recordtype_description.escapeHtml(); } }, { "data": function ( oObj ) { return oObj.itemtype_description.escapeHtml(); } }, { "data": function ( oObj ) { return ( oObj.collection ? oObj.collection.escapeHtml() : '' ); } }, { "data": function ( oObj ) { return ( oObj.location ? oObj.location.escapeHtml() : '' ); } }, { "data": "issuedate", "visible": false, }, { "orderData": 7, // Sort on hidden unformatted issuedate column "data": function ( oObj ) { return $datetime(oObj.issuedate, { no_tz_adjust: true }); } }, { "data": function ( oObj ) { return ( oObj.branchname ? oObj.branchname.escapeHtml() : '' ); } }, { "data": function ( oObj ) { return ( oObj.itemcallnumber ? oObj.itemcallnumber.escapeHtml() : '' ); } }, { "data": function ( oObj ) { return ( oObj.copynumber ? oObj.copynumber.escapeHtml() : '' ); } }, { "data": function ( oObj ) { if ( ! oObj.charge ) oObj.charge = 0; return parseFloat(oObj.charge).toFixed(2); } }, { "data": function ( oObj ) { if ( ! oObj.fine ) oObj.fine = 0; return parseFloat(oObj.fine).toFixed(2); } }, { "data": function ( oObj ) { if ( ! oObj.price ) oObj.price = 0; return parseFloat(oObj.price).toFixed(2); } }, { "data": function( oObj ) { return "" + ( oObj.borrower.firstname ? oObj.borrower.firstname.escapeHtml() : "" ) + " " + ( oObj.borrower.surname ? oObj.borrower.surname.escapeHtml() : "" ) + " (" + ( oObj.borrower.cardnumber ? oObj.borrower.cardnumber.escapeHtml() : "" ) + ")" } }, ], "paging": false, "processing": true, "serverSide": false, "sAjaxSource": '/cgi-bin/koha/svc/checkouts', "fnServerData": function ( sSource, aoData, fnCallback ) { $.each(relatives_borrowernumbers, function( index, value ) { aoData.push( { "name": "borrowernumber", "value": value } ); }); $.getJSON( sSource, aoData, function (json) { fnCallback(json) } ); }, }, table_settings_relatives_issues_table); } }); if ( $("#relatives-issues-table").length ) { $("#relatives-issues-table_processing").position({ of: $( "#relatives-issues-table" ), collision: "none" }); } if ( AllowRenewalLimitOverride || AllowRenewalOnHoldOverride ) { $( '#override_limit' ).click( function () { if ( this.checked ) { if ( AllowRenewalLimitOverride ) { $( '.renewals-allowed' ).show(); $( '.renewals-disabled' ).hide(); } if ( AllowRenewalOnHoldOverride ) { $( '.renewals-allowed-on_reserve' ).show(); } } else { $( '.renewals-allowed' ).hide(); $( '.renewals-allowed-on_reserve' ).hide(); $( '.renewals-disabled' ).show(); } } ).prop('checked', false); } // Refresh after return claim $('body').on('refreshClaimModal', function () { refreshReturnClaimsTable(); issuesTable.api().ajax.reload(); }); // Don't load return claims table unless its tab is shown var returnClaimsTable; $("#return-claims-tab").on('shown.bs.tab', function() { refreshReturnClaimsTable(); }); function refreshReturnClaimsTable(){ const table = $('#return-claims-table'); if ($.fn.dataTable.isDataTable(table)) { table.DataTable().ajax.reload(); } else { loadReturnClaimsTable(); } } function loadReturnClaimsTable() { if ( ! returnClaimsTable ) { returnClaimsTable = $("#return-claims-table").dataTable({ "autoWidth": false, "dom": "rt", "order": [], "columnDefs": [ { "orderable": false, "searchable": false, "targets": ['NoSort'] }, { "type": "anti-the", "targets": ["anti-the"] }, ], "columns": [ { "data": "id", "visible": false, }, { "data": function (oObj) { if (oObj.resolution) { return "is_resolved"; } else { return "is_unresolved"; } }, "visible": false, }, { "data": function ( oObj ) { let title = '' + oObj.title + ( oObj.subtitle ? " " + oObj.subtitle : "" ) + ( oObj.enumchron || "" ) + ''; if ( oObj.author ) { title += ' by ' + oObj.author; } title += ' ' + (oObj.barcode ? oObj.barcode.escapeHtml() : "") + ''; return title; } }, { "className": "return-claim-notes-td", "data": function ( oObj ) { let notes = ''; if ( oObj.notes ) { notes += oObj.notes; } notes += ''; notes += ''; return notes; } }, { "data": "created_on", "visible": false, }, { "orderData": 4, "data": function ( oObj ) { if ( oObj.created_on ) { return $date(oObj.created_on, { no_tz_adjust: true });; } else { return ""; } } }, { "data": "updated_on", "visible": false, }, { "orderData": 6, "data": function ( oObj ) { if ( oObj.updated_on ) { return $date(oObj.updated_on, { no_tz_adjust: true }); } else { return ""; } } }, { "data": function ( oObj ) { if ( ! oObj.resolution ) return ""; let desc = '' + oObj.resolution_data.lib + ' ('; if (oObj.resolved_by_data) desc += '' + ( oObj.resolved_by_data.firstname || "" ) + " " + ( oObj.resolved_by_data.surname || "" ) + ''; desc += ', ' + oObj.resolved_on + ')'; return desc; } }, { "data": function ( oObj ) { let delete_html = oObj.resolved_on ? '
  • ' + __("Delete") + '
  • ' : ""; let resolve_html = ! oObj.resolution ? '
  • ' + __("Resolve") + '
  • ' : ""; return '
    ' + ' ' + ' ' + '
    '; } }, ], "paging": false, "processing": true, "serverSide": false, "sAjaxSource": '/cgi-bin/koha/svc/return_claims', "fnServerData": function ( sSource, aoData, fnCallback ) { aoData.push( { "name": "borrowernumber", "value": borrowernumber } ); $.getJSON( sSource, aoData, function (json) { let resolved = json.resolved; let unresolved = json.unresolved; if ( resolved > 0 ) { $('#return-claims-count-resolved').text(resolved) .removeClass('label-default') .addClass('label-success'); } else { $('#return-claims-count-resolved').text(resolved) .removeClass('label-success') .addClass('label-default'); } if ( unresolved > 0 ) { $('#return-claims-count-unresolved').text(unresolved) .removeClass('label-default') .addClass('label-warning'); } else { $('#return-claims-count-unresolved').text(unresolved) .removeClass('label-warning') .addClass('label-default'); } fnCallback(json) } ); }, "search": { "search": "is_unresolved" }, "footerCallback": function (row, data, start, end, display) { var api = this.api(); // Total over all pages var colData = api.column(1).data(); var is_unresolved = 0; var is_resolved = 0; colData.each(function( index, value ){ if( index == "is_unresolved" ){ is_unresolved++; } if (index == "is_resolved") { is_resolved++; } }); // Update footer $("#return-claims-controls").html( showClaimFilter( is_unresolved, is_resolved ) ) } }); } } function showClaimFilter( is_unresolved, is_resolved ){ var showAll, showUnresolved; var total = Number( is_unresolved ) + Number( is_resolved ); if( total > 0 ){ showAll = __nx("Show 1 claim", "Show all {count} claims", total, { count: total }); } else { showAll = ""; } if( is_unresolved > 0 ){ showUnresolved = __nx("Show 1 unresolved claim", "Show {count} unresolved claims", is_unresolved, { count: is_unresolved }) } else { showUnresolved = ""; } $("#show_all_claims").html( showAll ); $("#show_unresolved_claims").html( showUnresolved ); } $('body').on('click', '.return-claim-tools-editnotes', function() { let id = $(this).data('return-claim-id'); $('#return-claim-notes-static-' + id).parent().dblclick(); }); $('body').on('dblclick', '.return-claim-notes-td', function() { let elt = $(this).children('.return-claim-notes'); let id = elt.data('return-claim-id'); if ( $('#return-claim-notes-editor-textarea-' + id).length == 0 ) { let note = elt.text(); let editor = ' ' + ' ' + '
    ' + ' ' + __("Update") + '' + ' ' + __("Cancel") + '' + '
    '; elt.hide(); $(editor).insertAfter( elt ); } }); $('body').on('click', '.claim-returned-notes-editor-submit', function(){ let id = $(this).data('return-claim-id'); let notes = $('#return-claim-notes-editor-textarea-' + id).val(); let params = { notes: notes, updated_by: logged_in_user_borrowernumber }; $(this).parent().remove(); $.ajax({ url: '/api/v1/return_claims/' + id + '/notes', type: 'PUT', data: JSON.stringify(params), success: function( data ) { let notes = $('#return-claim-notes-static-' + id); notes.text(data.notes); notes.show(); }, contentType: "json" }); }); $('body').on('click', '.claim-returned-notes-editor-cancel', function(){ let id = $(this).data('return-claim-id'); $(this).parent().remove(); $('#return-claim-notes-static-' + id).show(); }); // Hanld return claim deletion $('body').on('click', '.return-claim-tools-delete', function() { let confirmed = confirm(__("Are you sure you want to delete this return claim?")); if ( confirmed ) { let id = $(this).data('return-claim-id'); $.ajax({ url: '/api/v1/return_claims/' + id, type: 'DELETE', success: function( data ) { refreshReturnClaimsTable(); issuesTable.api().ajax.reload(); } }); } }); $("#show_all_claims").on("click", function(e){ e.preventDefault(); $(".ctrl_link").removeClass("disabled"); $(this).addClass("disabled"); $("#return-claims-table").DataTable().search("").draw(); }); $("#show_unresolved_claims").on("click", function (e) { e.preventDefault(); $(".ctrl_link").removeClass("disabled"); $(this).addClass("disabled"); $("#return-claims-table").DataTable().search("is_unresolved").draw(); }); });