Bug 32214: Prevent empty search option block in catalog search header
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / checkouts.js
1 /* global __ */
2
3 $(document).ready(function() {
4     var barcodefield = $("#barcode");
5
6     var onHoldDueDateSet = false;
7
8     var onHoldChecked = function() {
9         var isChecked = false;
10         $('input[data-on-reserve]').each(function() {
11             if ($(this).is(':checked')) {
12                 isChecked=true;
13             }
14         });
15         return isChecked;
16     };
17
18     var showHideOnHoldRenewal = function() {
19         // Display the date input
20         if (onHoldChecked()) {
21             $('#newonholdduedate').show()
22         } else {
23             $('#newonholdduedate').hide();
24         }
25     };
26
27     // Handle the select all/none links for checkouts table columns
28     $("#CheckAllRenewals").on("click",function(){
29         $("#UncheckAllCheckins").click();
30         $(".renew:visible").prop("checked", true);
31         showHideOnHoldRenewal();
32         return false;
33     });
34     $("#UncheckAllRenewals").on("click",function(){
35         $(".renew:visible").prop("checked", false);
36         showHideOnHoldRenewal();
37         return false;
38     });
39
40     $("#CheckAllCheckins").on("click",function(){
41         $("#UncheckAllRenewals").click();
42         $(".checkin:visible").prop("checked", true);
43         return false;
44     });
45     $("#UncheckAllCheckins").on("click",function(){
46         $(".checkin:visible").prop("checked", false);
47         return false;
48     });
49
50     $("#newduedate").on("change", function() {
51         if (!onHoldDueDateSet) {
52             $('#newonholdduedate input').val($('#newduedate').val());
53         }
54     });
55
56     $("#newonholdduedate").on("change", function() {
57         onHoldDueDateSet = true;
58     });
59
60     // Don't allow both return and renew checkboxes to be checked
61     $(document).on("change", '.renew', function(){
62         if ( $(this).is(":checked") ) {
63             $( "#checkin_" + $(this).val() ).prop("checked", false);
64         }
65     });
66     $(document).on("change", '.checkin', function(){
67         if ( $(this).is(":checked") ) {
68             $( "#renew_" + $(this).val() ).prop("checked", false);
69         }
70     });
71
72     // Display on hold due dates input when an on hold item is
73     // selected
74     $(document).on('change', '.renew', function(){
75         showHideOnHoldRenewal();
76     });
77
78     $("#output_format > option:first-child").attr("selected", "selected");
79     $("select[name='csv_profile_id']").hide();
80     $(document).on("change", '#issues-table-output-format', function(){
81         if ( $(this).val() == 'csv' ) {
82             $("select[name='csv_profile_id']").show();
83         } else {
84             $("select[name='csv_profile_id']").hide();
85         }
86     });
87
88     // Clicking the table cell checks the checkbox inside it
89     $(document).on("click", 'td', function(e){
90         if(e.target.tagName.toLowerCase() == 'td'){
91           $(this).find("input:checkbox:visible").each( function() {
92             $(this).click();
93           });
94         }
95     });
96
97     // Handle renewals and returns
98     $("#RenewCheckinChecked").on("click",function(){
99         $(".checkin:checked:visible").each(function() {
100             itemnumber = $(this).val();
101
102             $(this).replaceWith("<img id='checkin_" + itemnumber + "' src='" + interface + "/" + theme + "/img/spinner-small.gif' />");
103
104             params = {
105                 itemnumber:     itemnumber,
106                 borrowernumber: borrowernumber,
107                 branchcode:     branchcode,
108                 exempt_fine:    $("#exemptfine").is(':checked')
109             };
110
111             $.post({
112                 url: "/cgi-bin/koha/svc/checkin",
113                 data: params,
114                 success: function( data ) {
115                     id = "#checkin_" + data.itemnumber;
116
117                     content = "";
118                     if ( data.returned ) {
119                         content = __("Checked in");
120                         $(id).parent().parent().addClass('ok');
121                         $('#date_due_' + data.itemnumber).html( __("Checked in") );
122                         if ( data.patronnote != null ) {
123                             $('.patron_note_' + data.itemnumber).html( __("Patron note") + ": " + data.patronnote);
124                         }
125                     } else {
126                         content = __("Unable to check in");
127                         $(id).parent().parent().addClass('warn');
128                     }
129
130                     $(id).replaceWith( content );
131                 },
132                 dataType: "json",
133                 async: false,
134             });
135         });
136
137         $(".confirm:checked:visible").each(function() {
138             itemnumber = $(this).val();
139             id = "#checkin_" + itemnumber;
140             materials = $(this).data('materials');
141
142             $(this).replaceWith("<span class='confirm' id='checkin_" + itemnumber + "'>" + __("Confirm") + " (<span>" + materials + "</span>): <input type='checkbox' class='checkin' name='checkin' value='" + itemnumber +"'></input></span>");
143             $(id).parent().parent().addClass('warn');
144         });
145
146         $(".renew:checked:visible").each(function() {
147             var override_limit = $("#override_limit").is(':checked') ? 1 : 0;
148
149             var isOnReserve = $(this).data().hasOwnProperty('onReserve');
150
151             var itemnumber = $(this).val();
152
153             $(this).parent().parent().replaceWith("<img id='renew_" + itemnumber + "' src='" + interface + "/" + theme + "/img/spinner-small.gif' />");
154
155             var params = {
156                 itemnumber:      itemnumber,
157                 borrowernumber:  borrowernumber,
158                 branchcode:      branchcode,
159                 override_limit:  override_limit
160             };
161
162             if (UnseenRenewals) {
163                 var ren = $("#renew_as_unseen_checkbox");
164                 var renew_unseen = ren.length > 0 && ren.is(':checked') ? 1 : 0;
165                 params.seen = renew_unseen === 1 ? 0 : 1;
166             }
167
168             // Determine which due date we need to use
169             var dueDate = isOnReserve ?
170                 $("#newonholdduedate input").val() :
171                 $("#newduedate").val();
172
173             if (dueDate && dueDate.length > 0) {
174                 params.date_due = dueDate
175             }
176
177             $.post({
178                 url: "/cgi-bin/koha/svc/renew",
179                 data: params,
180                 success: function( data ) {
181                     var id = "#renew_" + data.itemnumber;
182
183                     var content = "";
184                     if ( data.renew_okay ) {
185                         content = __("Renewed, due:") + " " + data.date_due;
186                         $('#date_due_' + data.itemnumber).replaceWith( data.date_due );
187                     } else {
188                         content = __("Renew failed:") + " ";
189                         if ( data.error == "no_checkout" ) {
190                             content += __("not checked out");
191                         } else if ( data.error == "too_many" ) {
192                             content += __("too many renewals");
193                         } else if ( data.error == "too_unseen" ) {
194                             content += __("too many consecutive renewals without being seen by the library");
195                         } else if ( data.error == "on_reserve" ) {
196                             content += __("on hold");
197                         } else if ( data.error == "restriction" ) {
198                             content += __("Not allowed: patron restricted");
199                         } else if ( data.error == "overdue" ) {
200                             content += __("Not allowed: overdue");
201                         } else if ( data.error ) {
202                             content += data.error;
203                         } else {
204                             content += __("reason unknown");
205                         }
206                     }
207
208                     $(id).replaceWith( content );
209             },
210             dataType: "json",
211             async: false,
212             });
213         });
214
215         // Refocus on barcode field if it exists
216         if ( $("#barcode").length ) {
217             $("#barcode").focus();
218         }
219
220         // Prevent form submit
221         return false;
222     });
223
224     $("#RenewAll").on("click",function(){
225         $("#CheckAllRenewals").click();
226         $("#UncheckAllCheckins").click();
227         showHideOnHoldRenewal();
228         $("#RenewCheckinChecked").click();
229
230         // Prevent form submit
231         return false;
232     });
233
234     var ymd = flatpickr.formatDate(new Date(), "Y-m-d");
235
236     $('#issues-table').hide();
237     $('#issues-table-actions').hide();
238     $('#issues-table-load-immediately').change(function(){
239         if ( this.checked && typeof issuesTable === 'undefined') {
240             $('#issues-table-load-now-button').click();
241         }
242         barcodefield.focus();
243     });
244     $('#issues-table-load-now-button').click(function(){
245         LoadIssuesTable();
246         barcodefield.focus();
247         return false;
248     });
249
250     $('#RenewCheckinChecked').on('click', function(){
251         RefreshIssuesTable();
252     });
253
254     if ( Cookies.get("issues-table-load-immediately-" + script) == "true" ) {
255         LoadIssuesTable();
256         $('#issues-table-load-immediately').prop('checked', true);
257     }
258     $('#issues-table-load-immediately').on( "change", function(){
259         Cookies.set("issues-table-load-immediately-" + script, $(this).is(':checked'), { expires: 365, sameSite: 'Lax'  });
260     });
261
262     function RefreshIssuesTable() {
263         var table = $('#issues-table').DataTable();
264         table.ajax.reload();
265     }
266
267     function LoadIssuesTable() {
268         $('#issues-table-loading-message').hide();
269         $('#issues-table').show();
270         $('#issues-table-actions').show();
271
272         var msg_loading = __('Loading... you may continue scanning.');
273         issuesTable = KohaTable("issues-table", {
274             "oLanguage": {
275                 "sEmptyTable" : msg_loading,
276                 "sProcessing": msg_loading,
277             },
278             "bAutoWidth": false,
279             "dom": '<"table_controls"B>rt',
280             "aoColumns": [
281                 {
282                     "mDataProp": function( oObj ) {
283                         return oObj.sort_order;
284                     }
285                 },
286                 {
287                     "mDataProp": function( oObj ) {
288                         if ( oObj.issued_today ) {
289                             return "<strong>" + __("Today's checkouts") + "</strong>";
290                         } else {
291                             return "<strong>" + __("Previous checkouts") + "</strong>";
292                         }
293                     }
294                 },
295                 {
296                     "mDataProp": "date_due",
297                     "bVisible": false,
298                 },
299                 {
300                     "iDataSort": 2, // Sort on hidden unformatted date due column
301                     "mDataProp": function( oObj ) {
302                         var due = oObj.date_due_formatted;
303                         if ( oObj.date_due_overdue ) {
304                             due = "<span class='overdue'>" + due + "</span>";
305                         }
306
307                         due = "<span id='date_due_" + oObj.itemnumber + "' class='date_due'>" + due + "</span>";
308
309                         if ( oObj.lost && oObj.claims_returned ) {
310                             due += "<span class='lost claims_returned'>" + oObj.lost.escapeHtml() + "</span>";
311                         } else if ( oObj.lost ) {
312                             due += "<span class='lost'>" + oObj.lost.escapeHtml() + "</span>";
313                         }
314
315                         if ( oObj.damaged ) {
316                             due += "<span class='dmg'>" + oObj.damaged.escapeHtml() + "</span>";
317                         }
318
319                         var patron_note = " <span class='patron_note_" + oObj.itemnumber + "'></span>";
320                         due +="<br>" + patron_note;
321
322                         return due;
323                     }
324                 },
325                 {
326                     "mDataProp": function ( oObj ) {
327                         let title = "<span id='title_" + oObj.itemnumber + "' class='strong'><a href='/cgi-bin/koha/catalogue/detail.pl?biblionumber="
328                               + oObj.biblionumber
329                               + "'>"
330                               + (oObj.title ? oObj.title.escapeHtml() : '' );
331
332                         $.each(oObj.subtitle, function( index, value ) {
333                                   title += " " + value.escapeHtml();
334                         });
335
336                         title += " " + oObj.part_number + " " + oObj.part_name;
337
338                         if ( oObj.enumchron ) {
339                             title += " (" + oObj.enumchron.escapeHtml() + ")";
340                         }
341
342                         title += "</a></span>";
343
344                         if ( oObj.author ) {
345                             title += " " + __("by _AUTHOR_").replace( "_AUTHOR_",  " " + oObj.author.escapeHtml() );
346                         }
347
348                         if ( oObj.itemnotes ) {
349                             var span_class = "text-muted";
350                             if ( flatpickr.formatDate( new Date(oObj.issuedate), "Y-m-d" ) == ymd ){
351                                 span_class = "circ-hlt";
352                             }
353                             title += " - <span class='" + span_class + " item-note-public'>" + oObj.itemnotes.escapeHtml() + "</span>";
354                         }
355
356                         if ( oObj.itemnotes_nonpublic ) {
357                             var span_class = "text-danger";
358                             if ( flatpickr.formatDate( new Date(oObj.issuedate), "Y-m-d" ) == ymd ){
359                                 span_class = "circ-hlt";
360                             }
361                             title += " - <span class='" + span_class + " item-note-nonpublic'>" + oObj.itemnotes_nonpublic.escapeHtml() + "</span>";
362                         }
363
364                         var onsite_checkout = '';
365                         if ( oObj.onsite_checkout == 1 ) {
366                             onsite_checkout += " <span class='onsite_checkout'>(" + __("On-site checkout") + ")</span>";
367                         }
368
369                         if ( oObj.recalled == 1 ) {
370                              title += " - <span class='circ-hlt item-recalled'>" +  __("This item has been recalled and the due date updated") + ".</span>";
371                         }
372
373                         title += " "
374                               + "<a href='/cgi-bin/koha/catalogue/moredetail.pl?biblionumber="
375                               + oObj.biblionumber
376                               + "&itemnumber="
377                               + oObj.itemnumber
378                               + "#"
379                               + oObj.itemnumber
380                               + "'>"
381                               + (oObj.barcode ? oObj.barcode.escapeHtml() : "")
382                               + "</a>"
383                               + onsite_checkout
384
385                         return title;
386                     },
387                     "sType": "anti-the"
388                 },
389                 {
390                     "mDataProp": function ( oObj ) {
391                         return oObj.recordtype_description.escapeHtml();
392                     }
393                 },
394                 {
395                     "mDataProp": function ( oObj ) {
396                         return oObj.itemtype_description.escapeHtml();
397                     }
398                 },
399                 {
400                     "mDataProp": function ( oObj ) {
401                         return ( oObj.collection ? oObj.collection.escapeHtml() : '' );
402                     }
403                 },
404                 {
405                     "mDataProp": function ( oObj ) {
406                         return ( oObj.location ? oObj.location.escapeHtml() : '' );
407                     }
408                 },
409                 {
410                     "mDataProp": function ( oObj ) {
411                         return (oObj.homebranch ? oObj.homebranch.escapeHtml() : '' );
412                     }
413                 },
414                 {
415                     "mDataProp": "issuedate",
416                     "bVisible": false,
417                 },
418                 {
419                     "iDataSort": 10, // Sort on hidden unformatted issuedate column
420                     "mDataProp": "issuedate_formatted",
421                 },
422                 {
423                     "mDataProp": function ( oObj ) {
424                         return (oObj.branchname ? oObj.branchname.escapeHtml() : '' );
425                     }
426                 },
427                 {
428                     "mDataProp": function ( oObj ) {
429                         return ( oObj.itemcallnumber ? oObj.itemcallnumber.escapeHtml() : '' );
430                     }
431                 },
432                 {
433                     "mDataProp": function ( oObj ) {
434                         return ( oObj.copynumber ? oObj.copynumber.escapeHtml() : '' );
435                     }
436                 },
437                 {
438                     "mDataProp": function ( oObj ) {
439                         if ( ! oObj.charge ) oObj.charge = 0;
440                         return '<span style="text-align: right; display: block;">' + parseFloat(oObj.charge).format_price() + '<span>';
441                     },
442                     "sClass": "nowrap"
443                 },
444                 {
445                     "mDataProp": function ( oObj ) {
446                         if ( ! oObj.fine ) oObj.fine = 0;
447                         return '<span style="text-align: right; display: block;">' + parseFloat(oObj.fine).format_price()   + '<span>';
448                     },
449                     "sClass": "nowrap"
450                 },
451                 {
452                     "mDataProp": function ( oObj ) {
453                         if ( ! oObj.price ) oObj.price = 0;
454                         return '<span style="text-align: right; display: block;">' + parseFloat(oObj.price).format_price()  + '<span>';
455                     },
456                     "sClass": "nowrap"
457                 },
458                 {
459                     "bSortable": false,
460                     "bVisible": AllowCirculate ? true : false,
461                     "mDataProp": function ( oObj ) {
462                         var content = "";
463                         var msg = "";
464                         var span_style = "";
465                         var span_class = "";
466
467                         if ( oObj.can_renew ) {
468                             // Do nothing
469                         } else if ( oObj.can_renew_error == "recalled" ) {
470                             msg += "<span>"
471                                     + "<a href='/cgi-bin/koha/recalls/request.pl?biblionumber=" + oObj.biblionumber + "'>" + __("Recalled") + "</a>"
472                                     + "</span>";
473
474                             span_style = "display: none";
475                             span_class = "renewals-allowed-recalled";
476                         } else if ( oObj.can_renew_error == "on_reserve" ) {
477                             msg += "<span>"
478                                     +"<a href='/cgi-bin/koha/reserve/request.pl?biblionumber=" + oObj.biblionumber + "'>" + __("On hold") + "</a>"
479                                     + "</span>";
480
481                             span_style = "display: none";
482                             span_class = "renewals-allowed-on_reserve";
483                         } else if ( oObj.can_renew_error == "too_many" ) {
484                             msg += "<span class='renewals-disabled'>"
485                                     + __("Not renewable")
486                                     + "</span>";
487
488                             span_style = "display: none";
489                             span_class = "renewals-allowed";
490                         } else if ( oObj.can_renew_error == "too_unseen" ) {
491                             msg += "<span>"
492                                     + __("Must be renewed at the library")
493                                     + "</span>";
494                             span_class = "renewals-allowed";
495                         } else if ( oObj.can_renew_error == "restriction" ) {
496                             msg += "<span class='renewals-disabled'>"
497                                     + __("Not allowed: patron restricted")
498                                     + "</span>";
499
500                             span_style = "display: none";
501                             span_class = "renewals-allowed";
502                         } else if ( oObj.can_renew_error == "overdue" ) {
503                             msg += "<span class='renewals-disabled'>"
504                                     + __("Not allowed: overdue")
505                                     + "</span>";
506
507                             span_style = "display: none";
508                             span_class = "renewals-allowed";
509                         } else if ( oObj.can_renew_error == "too_soon" ) {
510                             msg += "<span class='renewals-disabled'>"
511                                     + __("No renewal before %s").format(oObj.can_renew_date)
512                                     + "</span>";
513
514                             span_style = "display: none";
515                             span_class = "renewals-allowed";
516                         } else if ( oObj.can_renew_error == "auto_too_soon" ) {
517                             msg += "<span class='renewals-disabled'>"
518                                     + __("Scheduled for automatic renewal")
519                                     + "</span>";
520
521                             span_style = "display: none";
522                             span_class = "renewals-allowed";
523                         } else if ( oObj.can_renew_error == "auto_too_late" ) {
524                             msg += "<span class='renewals-disabled'>"
525                                     + __("Can no longer be auto-renewed - number of checkout days exceeded")
526                                     + "</span>";
527
528                             span_style = "display: none";
529                             span_class = "renewals-allowed";
530                         } else if ( oObj.can_renew_error == "auto_too_much_oweing" ) {
531                             msg += "<span class='renewals-disabled'>"
532                                     + __("Automatic renewal failed, patron has unpaid fines")
533                                     + "</span>";
534
535                             span_style = "display: none";
536                             span_class = "renewals-allowed";
537                         } else if ( oObj.can_renew_error == "auto_account_expired" ) {
538                             msg += "<span class='renewals-disabled'>"
539                                     + __("Automatic renewal failed, account expired")
540                                     + "</span>";
541
542                             span_style = "display: none";
543                             span_class = "renewals-allowed";
544                         } else if ( oObj.can_renew_error == "auto_renew" ) {
545                             msg += "<span class='renewals-disabled'>"
546                                     + __("Scheduled for automatic renewal")
547                                     + "</span>";
548
549                             span_style = "display: none";
550                             span_class = "renewals-allowed";
551                         } else if ( oObj.can_renew_error == "onsite_checkout" ) {
552                             // Don't display something if it's an onsite checkout
553                         } else if ( oObj.can_renew_error == "item_denied_renewal" ) {
554                             content += "<span class='renewals-disabled'>"
555                                     + __("Renewal denied by syspref")
556                                     + "</span>";
557
558                             span_style = "display: none";
559                             span_class = "renewals-allowed";
560                         } else {
561                             msg += "<span class='renewals-disabled'>"
562                                     + oObj.can_renew_error
563                                     + "</span>";
564
565                             span_style = "display: none";
566                             span_class = "renewals-allowed";
567                         }
568
569                         var can_force_renew = ( oObj.onsite_checkout == 0 ) &&
570                             ( oObj.can_renew_error != "on_reserve" || (oObj.can_renew_error == "on_reserve" && AllowRenewalOnHoldOverride))
571                             ? true : false;
572                         var can_renew = ( oObj.renewals_remaining > 0 && ( !oObj.can_renew_error || oObj.can_renew_error == "too_unseen" ));
573                         content += "<span>";
574                         if ( can_renew || can_force_renew ) {
575                             content += "<span style='padding: 0 1em;'>" + oObj.renewals_count + "</span>";
576                             content += "<span class='" + span_class + "' style='" + span_style + "'>"
577                                     +  "<input type='checkbox' ";
578                             if ( oObj.date_due_overdue && can_renew ) {
579                                 content += "checked='checked' ";
580                             }
581                             if (oObj.can_renew_error == "on_reserve") {
582                                 content += "data-on-reserve ";
583                             }
584                             content += "class='renew' id='renew_" + oObj.itemnumber + "' name='renew' value='" + oObj.itemnumber +"'/>"
585                                     +  "</span>";
586                         }
587                         content += msg;
588                         if ( can_renew || can_force_renew ) {
589                             content += "<span class='renewals-info'>(";
590                             content += __("%s of %s renewals remaining").format(oObj.renewals_remaining, oObj.renewals_allowed);
591                             if (UnseenRenewals && oObj.unseen_allowed) {
592                                 content += __(" and %s of %s unseen renewals remaining").format(oObj.unseen_remaining, oObj.unseen_allowed);
593                             }
594                             content += ")</span>";
595                         }
596
597                         return content;
598                     }
599                 },
600                 {
601                     "bSortable": false,
602                     "bVisible": AllowCirculate ? true : false,
603                     "mDataProp": function ( oObj ) {
604                         if ( oObj.can_renew_error == "recalled" ) {
605                             return "<a href='/cgi-bin/koha/recalls/request.pl?biblionumber=" + oObj.biblionumber + "'>" + __("Recalled") + "</a>";
606                         } else if ( oObj.can_renew_error == "on_reserve" ) {
607                             return "<a href='/cgi-bin/koha/reserve/request.pl?biblionumber=" + oObj.biblionumber + "'>" + __("On hold") + "</a>";
608                         } else if ( oObj.materials ) {
609                             return "<input type='checkbox' class='confirm' id='confirm_" + oObj.itemnumber + "' name='confirm' value='" + oObj.itemnumber + "' data-materials='" + oObj.materials.escapeHtml() + "'></input>";
610                         } else {
611                             return "<input type='checkbox' class='checkin' id='checkin_" + oObj.itemnumber + "' name='checkin' value='" + oObj.itemnumber +"'></input>";
612                         }
613                     }
614                 },
615                 {
616                     "bVisible": ClaimReturnedLostValue ? true : false,
617                     "bSortable": false,
618                     "mDataProp": function ( oObj ) {
619                         let content = "";
620
621                         if ( oObj.return_claim_id ) {
622                           content = '<span class="badge">' + oObj.return_claim_created_on_formatted + '</span>';
623                         } else if ( ClaimReturnedLostValue ) {
624                           content = '<a class="btn btn-default btn-xs claim-returned-btn" data-itemnumber="' + oObj.itemnumber + '"><i class="fa fa-exclamation-circle"></i> ' + __("Claim returned") + '</a>';
625                         } else {
626                           content = '<a class="btn btn-default btn-xs" disabled="disabled" title="ClaimReturnedLostValue is not set, this feature is disabled"><i class="fa fa-exclamation-circle"></i> ' + __("Claim returned") + '</a>';
627                         }
628                         return content;
629                     }
630                 },
631                 {
632                     "bVisible": exports_enabled == 1 ? true : false,
633                     "bSortable": false,
634                     "mDataProp": function ( oObj ) {
635                         var s = "<input type='checkbox' name='itemnumbers' value='" + oObj.itemnumber + "' style='visibility:hidden;' />";
636
637                         s += "<input type='checkbox' class='export' id='export_" + oObj.biblionumber + "' name='biblionumbers' value='" + oObj.biblionumber + "' />";
638                         return s;
639                     }
640                 }
641             ],
642             "fnFooterCallback": function ( nRow, aaData, iStart, iEnd, aiDisplay ) {
643                 var total_charge = 0;
644                 var total_fine  = 0;
645                 var total_price = 0;
646                 for ( var i=0; i < aaData.length; i++ ) {
647                     total_charge += aaData[i]['charge'] * 1;
648                     total_fine += aaData[i]['fine'] * 1;
649                     total_price  += aaData[i]['price'] * 1;
650                 }
651                 $("#totaldue").html(total_charge.format_price() );
652                 $("#totalfine").html(total_fine.format_price() );
653                 $("#totalprice").html(total_price.format_price() );
654             },
655             "bPaginate": false,
656             "bProcessing": true,
657             "bServerSide": false,
658             "sAjaxSource": '/cgi-bin/koha/svc/checkouts',
659             "fnServerData": function ( sSource, aoData, fnCallback ) {
660                 aoData.push( { "name": "borrowernumber", "value": borrowernumber } );
661
662                 $.getJSON( sSource, aoData, function (json) {
663                     fnCallback(json)
664                 } );
665             },
666             "rowGroup":{
667                 "dataSrc": "issued_today",
668                 "startRender": function ( rows, group ) {
669                     if ( group ) {
670                         return __("Today's checkouts");
671                     } else {
672                         return __("Previous checkouts");
673                     }
674                 }
675             },
676             "fnInitComplete": function(oSettings, json) {
677                 // Build a summary of checkouts grouped by itemtype
678                 var checkoutsByItype = json.aaData.reduce(function (obj, row) {
679                     obj[row.type_for_stat] = (obj[row.type_for_stat] || 0) + 1;
680                     return obj;
681                 }, {});
682                 var ul = $('<ul>');
683                 Object.keys(checkoutsByItype).sort().forEach(function (itype) {
684                     var li = $('<li>')
685                         .append($('<strong>').html(itype || __("No itemtype")))
686                         .append(': ' + checkoutsByItype[itype]);
687                     ul.append(li);
688                 })
689                 $('<details>')
690                     .addClass('checkouts-by-itemtype')
691                     .append($('<summary>').html( __("Number of checkouts by item type") ))
692                     .append(ul)
693                     .insertBefore(oSettings.nTableWrapper)
694             },
695         }, table_settings_issues_table);
696
697         if ( $("#issues-table").length ) {
698             $("#issues-table_processing").position({
699                 of: $( "#issues-table" ),
700                 collision: "none"
701             });
702         }
703     }
704
705     // Don't load relatives' issues table unless it is clicked on
706     var relativesIssuesTable;
707     $("#relatives-issues-tab").click( function() {
708         if ( ! relativesIssuesTable ) {
709             relativesIssuesTable = KohaTable("relatives-issues-table", {
710                 "bAutoWidth": false,
711                 "dom": '<"table_controls"B>rt',
712                 "aaSorting": [],
713                 "aoColumns": [
714                     {
715                         "mDataProp": "date_due",
716                         "bVisible": false,
717                     },
718                     {
719                         "iDataSort": 0, // Sort on hidden unformatted date due column
720                         "mDataProp": function( oObj ) {
721                             var today = new Date();
722                             var due = new Date( oObj.date_due );
723                             if ( today > due ) {
724                                 return "<span class='overdue'>" + oObj.date_due_formatted + "</span>";
725                             } else {
726                                 return oObj.date_due_formatted;
727                             }
728                         }
729                     },
730                     {
731                         "mDataProp": function ( oObj ) {
732                             let title = "<span class='strong'><a href='/cgi-bin/koha/catalogue/detail.pl?biblionumber="
733                                   + oObj.biblionumber
734                                   + "'>"
735                                   + (oObj.title ? oObj.title.escapeHtml() : '' );
736
737                             $.each(oObj.subtitle, function( index, value ) {
738                                       title += " " + value.escapeHtml();
739                             });
740
741                             title += " " + oObj.part_number + " " + oObj.part_name;
742
743                             if ( oObj.enumchron ) {
744                                 title += " (" + oObj.enumchron.escapeHtml() + ")";
745                             }
746
747                             title += "</a></span>";
748
749                             if ( oObj.author ) {
750                                 title += " " + __("by _AUTHOR_").replace( "_AUTHOR_", " " + oObj.author.escapeHtml() );
751                             }
752
753                             if ( oObj.itemnotes ) {
754                                 var span_class = "";
755                                 if ( flatpickr.formatDate( new Date(oObj.issuedate), "Y-m-d" ) == ymd ){
756                                     span_class = "circ-hlt";
757                                 }
758                                 title += " - <span class='" + span_class + "'>" + oObj.itemnotes.escapeHtml() + "</span>"
759                             }
760
761                             if ( oObj.itemnotes_nonpublic ) {
762                                 var span_class = "";
763                                 if ( flatpickr.formatDate( new Date(oObj.issuedate), "Y-m-d" ) == ymd ){
764                                     span_class = "circ-hlt";
765                                 }
766                                 title += " - <span class='" + span_class + "'>" + oObj.itemnotes_nonpublic.escapeHtml() + "</span>"
767                             }
768
769                             var onsite_checkout = '';
770                             if ( oObj.onsite_checkout == 1 ) {
771                                 onsite_checkout += " <span class='onsite_checkout'>(" + __("On-site checkout") + ")</span>";
772                             }
773
774                             title += " "
775                                   + "<a href='/cgi-bin/koha/catalogue/moredetail.pl?biblionumber="
776                                   + oObj.biblionumber
777                                   + "&itemnumber="
778                                   + oObj.itemnumber
779                                   + "#"
780                                   + oObj.itemnumber
781                                   + "'>"
782                                   + (oObj.barcode ? oObj.barcode.escapeHtml() : "")
783                                   + "</a>"
784                                   + onsite_checkout;
785
786                             return title;
787                         },
788                         "sType": "anti-the"
789                     },
790                     {
791                         "mDataProp": function ( oObj ) {
792                             return oObj.recordtype_description.escapeHtml();
793                         }
794                     },
795                     {
796                         "mDataProp": function ( oObj ) {
797                             return oObj.itemtype_description.escapeHtml();
798                         }
799                     },
800                     {
801                         "mDataProp": function ( oObj ) {
802                             return ( oObj.collection ? oObj.collection.escapeHtml() : '' );
803                         }
804                     },
805                     {
806                         "mDataProp": function ( oObj ) {
807                             return ( oObj.location ? oObj.location.escapeHtml() : '' );
808                         }
809                     },
810                     {
811                         "mDataProp": "issuedate",
812                         "bVisible": false,
813                     },
814                     {
815                         "iDataSort": 7, // Sort on hidden unformatted issuedate column
816                         "mDataProp": "issuedate_formatted",
817                     },
818                     {
819                         "mDataProp": function ( oObj ) {
820                             return ( oObj.branchname ? oObj.branchname.escapeHtml() : '' );
821                         }
822                     },
823                     {
824                         "mDataProp": function ( oObj ) {
825                             return ( oObj.itemcallnumber ? oObj.itemcallnumber.escapeHtml() : '' );
826                         }
827                     },
828                     {
829                         "mDataProp": function ( oObj ) {
830                             return ( oObj.copynumber ? oObj.copynumber.escapeHtml() : '' );
831                         }
832                     },
833                     {
834                         "mDataProp": function ( oObj ) {
835                             if ( ! oObj.charge ) oObj.charge = 0;
836                             return parseFloat(oObj.charge).toFixed(2);
837                         }
838                     },
839                     {
840                         "mDataProp": function ( oObj ) {
841                             if ( ! oObj.fine ) oObj.fine = 0;
842                             return parseFloat(oObj.fine).toFixed(2);
843                         }
844                     },
845                     {
846                         "mDataProp": function ( oObj ) {
847                             if ( ! oObj.price ) oObj.price = 0;
848                             return parseFloat(oObj.price).toFixed(2);
849                         }
850                     },
851                     {
852                         "mDataProp": function( oObj ) {
853                             return "<a href='/cgi-bin/koha/members/moremember.pl?borrowernumber=" + oObj.borrowernumber + "'>"
854                                 + ( oObj.borrower.firstname ? oObj.borrower.firstname.escapeHtml() : "" )
855                                 + " " +
856                                 ( oObj.borrower.surname ? oObj.borrower.surname.escapeHtml() : "" )
857                                 + " (" + ( oObj.borrower.cardnumber ? oObj.borrower.cardnumber.escapeHtml() : "" ) + ")</a>"
858                         }
859                     },
860                 ],
861                 "bPaginate": false,
862                 "bProcessing": true,
863                 "bServerSide": false,
864                 "sAjaxSource": '/cgi-bin/koha/svc/checkouts',
865                 "fnServerData": function ( sSource, aoData, fnCallback ) {
866                     $.each(relatives_borrowernumbers, function( index, value ) {
867                         aoData.push( { "name": "borrowernumber", "value": value } );
868                     });
869
870                     $.getJSON( sSource, aoData, function (json) {
871                         fnCallback(json)
872                     } );
873                 },
874             }, table_settings_relatives_issues_table);
875         }
876     });
877
878     if ( $("#relatives-issues-table").length ) {
879         $("#relatives-issues-table_processing").position({
880             of: $( "#relatives-issues-table" ),
881             collision: "none"
882         });
883     }
884
885     if ( AllowRenewalLimitOverride || AllowRenewalOnHoldOverride ) {
886         $( '#override_limit' ).click( function () {
887             if ( this.checked ) {
888                 if ( AllowRenewalLimitOverride ) {
889                     $( '.renewals-allowed' ).show();
890                     $( '.renewals-disabled' ).hide();
891                 }
892                 if ( AllowRenewalOnHoldOverride ) {
893                     $( '.renewals-allowed-on_reserve' ).show();
894                 }
895             } else {
896                 $( '.renewals-allowed' ).hide();
897                 $( '.renewals-allowed-on_reserve' ).hide();
898                 $( '.renewals-disabled' ).show();
899             }
900         } ).prop('checked', false);
901     }
902
903     // Refresh after return claim
904     $('body').on('refreshClaimModal', function () {
905         refreshReturnClaimsTable();
906         issuesTable.api().ajax.reload();
907     });
908
909     // Don't load return claims table unless it is clicked on
910     var returnClaimsTable;
911     $("#return-claims-tab").click( function() {
912         refreshReturnClaimsTable();
913     });
914
915     function refreshReturnClaimsTable(){
916         loadReturnClaimsTable();
917         $("#return-claims-table").DataTable().ajax.reload();
918     }
919     function loadReturnClaimsTable() {
920         if ( ! returnClaimsTable ) {
921             returnClaimsTable = $("#return-claims-table").dataTable({
922                 "bAutoWidth": false,
923                 "sDom": "rt",
924                 "aaSorting": [],
925                 "aoColumnDefs": [
926                     { "bSortable": false, "bSearchable": false, 'aTargets': ['NoSort'] },
927                     { "sType": "anti-the", "aTargets": ["anti-the"] },
928                 ],
929                 "aoColumns": [
930                     {
931                         "mDataProp": "id",
932                         "bVisible": false,
933                     },
934                     {
935                         "mDataProp": function (oObj) {
936                             if (oObj.resolution) {
937                                 return "is_resolved";
938                             } else {
939                                 return "is_unresolved";
940                             }
941                         },
942                         "bVisible": false,
943                     },
944                     {
945                         "mDataProp": function ( oObj ) {
946                               let title = '<a class="return-claim-title strong" href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=' + oObj.biblionumber + '">'
947                                   + oObj.title
948                                   + ( oObj.subtitle ? " " + oObj.subtitle : "" )
949                                   + ( oObj.enumchron || "" )
950                               + '</a>';
951                               if ( oObj.author ) {
952                                 title += ' by ' + oObj.author;
953                               }
954                               title += ' <a href="/cgi-bin/koha/catalogue/moredetail.pl?biblionumber='
955                                     + oObj.biblionumber
956                                     + '&itemnumber='
957                                     + oObj.itemnumber
958                                     + '">'
959                                     + (oObj.barcode ? oObj.barcode.escapeHtml() : "")
960                                     + '</a>';
961
962                               return title;
963                         }
964                     },
965                     {
966                         "sClass": "return-claim-notes-td",
967                         "mDataProp": function ( oObj ) {
968                             let notes =  '<span id="return-claim-notes-static-' + oObj.id + '" class="return-claim-notes" data-return-claim-id="' + oObj.id + '">';
969                             if ( oObj.notes ) {
970                                 notes += oObj.notes;
971                             }
972                             notes += '</span>';
973                             notes += '<i style="float:right" class="fa fa-pencil-square-o" title="' + __("Double click to edit") + '"></i>';
974                             return notes;
975                         }
976                     },
977                     {
978                         "mDataProp": function ( oObj ) {
979                             let created_on = new Date( oObj.created_on );
980                             return created_on.toLocaleDateString();
981                         }
982                     },
983                     {
984                         "mDataProp": function ( oObj ) {
985                             if ( oObj.updated_on ) {
986                                 let updated_on = new Date( oObj.updated_on );
987                                 return updated_on.toLocaleDateString();
988                             } else {
989                                 return "";
990                             }
991                         }
992                     },
993                     {
994                         "mDataProp": function ( oObj ) {
995                             if ( ! oObj.resolution ) return "";
996
997                             let desc = '<strong>' + oObj.resolution_data.lib + '</strong> <i>(';
998                             if (oObj.resolved_by_data) desc += '<a href="/cgi-bin/koha/circ/circulation.pl?borrowernumber=' + oObj.resolved_by_data.borrowernumber + '">' + ( oObj.resolved_by_data.firstname || "" ) + " " + ( oObj.resolved_by_data.surname || "" ) + '</a>';
999                             desc += ', ' + oObj.resolved_on + ')</i>';
1000                             return desc;
1001                         }
1002                     },
1003                     {
1004                         "mDataProp": function ( oObj ) {
1005                             let delete_html = oObj.resolved_on
1006                                 ? '<li><a href="#" class="return-claim-tools-delete" data-return-claim-id="' + oObj.id + '"><i class="fa fa-trash"></i> ' + __("Delete") + '</a></li>'
1007                                 : "";
1008                             let resolve_html = ! oObj.resolution
1009                                 ? '<li><a href="#" class="return-claim-tools-resolve" data-return-claim-id="' + oObj.id + '" data-current-lost-status="' + escape_str(oObj.itemlost) + '"><i class="fa fa-check-square"></i> ' + __("Resolve") + '</a></li>'
1010                                 : "";
1011
1012                             return  '<div class="btn-group">'
1013                                   + ' <button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">'
1014                                   + __("Actions") + ' <span class="caret"></span>'
1015                                   + ' </button>'
1016                                   + ' <ul class="dropdown-menu">'
1017                                   + '  <li><a href="#" class="return-claim-tools-editnotes" data-return-claim-id="' + oObj.id + '"><i class="fa fa-edit"></i> ' + __("Edit notes") + '</a></li>'
1018                                   + resolve_html
1019                                   + delete_html
1020                                   + ' </ul>'
1021                                   + ' </div>';
1022                         }
1023                     },
1024                 ],
1025                 "bPaginate": false,
1026                 "bProcessing": true,
1027                 "bServerSide": false,
1028                 "sAjaxSource": '/cgi-bin/koha/svc/return_claims',
1029                 "fnServerData": function ( sSource, aoData, fnCallback ) {
1030                     aoData.push( { "name": "borrowernumber", "value": borrowernumber } );
1031
1032                     $.getJSON( sSource, aoData, function (json) {
1033                         let resolved = json.resolved;
1034                         let unresolved = json.unresolved;
1035
1036                         if ( resolved > 0 ) {
1037                             $('#return-claims-count-resolved').text(resolved)
1038                                                               .removeClass('label-default')
1039                                                               .addClass('label-success');
1040                         } else {
1041                             $('#return-claims-count-resolved').text(resolved)
1042                                                               .removeClass('label-success')
1043                                                               .addClass('label-default');
1044                         }
1045                         if ( unresolved > 0 ) {
1046                             $('#return-claims-count-unresolved').text(unresolved)
1047                                                                 .removeClass('label-default')
1048                                                                 .addClass('label-warning');
1049                         } else {
1050                             $('#return-claims-count-unresolved').text(unresolved)
1051                                                                 .removeClass('label-warning')
1052                                                                 .addClass('label-default');
1053                         }
1054
1055                         fnCallback(json)
1056                     } );
1057                 },
1058                 "search": { "search": "is_unresolved" },
1059                 "footerCallback": function (row, data, start, end, display) {
1060                     var api = this.api();
1061                     // Total over all pages
1062                     var colData = api.column(1).data();
1063                     var is_unresolved = 0;
1064                     var is_resolved = 0;
1065                     colData.each(function( index, value ){
1066                         if( index == "is_unresolved" ){ is_unresolved++; }
1067                         if (index == "is_resolved") { is_resolved++; }
1068                     });
1069                     // Update footer
1070                     $("#return-claims-controls").html( showClaimFilter( is_unresolved, is_resolved ) )
1071                 }
1072             });
1073         }
1074     }
1075
1076     function showClaimFilter( is_unresolved, is_resolved ){
1077         var showAll, showUnresolved;
1078         var total = Number( is_unresolved ) + Number( is_resolved );
1079         if( total > 0 ){
1080             showAll = __nx("Show 1 claim", "Show all {count} claims", total, { count: total });
1081         } else {
1082             showAll = "";
1083         }
1084         if( is_unresolved > 0 ){
1085             showUnresolved = __nx("Show 1 unresolved claim", "Show {count} unresolved claims", is_unresolved, { count: is_unresolved })
1086         } else {
1087             showUnresolved = "";
1088         }
1089         $("#show_all_claims").html( showAll );
1090         $("#show_unresolved_claims").html( showUnresolved );
1091     }
1092
1093     $('body').on('click', '.return-claim-tools-editnotes', function() {
1094         let id = $(this).data('return-claim-id');
1095         $('#return-claim-notes-static-' + id).parent().dblclick();
1096     });
1097     $('body').on('dblclick', '.return-claim-notes-td', function() {
1098         let elt = $(this).children('.return-claim-notes');
1099         let id = elt.data('return-claim-id');
1100         if ( $('#return-claim-notes-editor-textarea-' + id).length == 0 ) {
1101             let note = elt.text();
1102             let editor =
1103                 '  <span id="return-claim-notes-editor-' + id + '">'
1104                 + ' <textarea id="return-claim-notes-editor-textarea-' + id + '">' + note + '</textarea>'
1105                 + ' <br/>'
1106                 + ' <a class="btn btn-default btn-xs claim-returned-notes-editor-submit" data-return-claim-id="' + id + '"><i class="fa fa-save"></i> ' + __("Update") + '</a>'
1107                 + ' <a class="claim-returned-notes-editor-cancel" data-return-claim-id="' + id + '" href="#">' + __("Cancel") + '</a>'
1108                 + '</span>';
1109             elt.hide();
1110             $(editor).insertAfter( elt );
1111         }
1112     });
1113
1114     $('body').on('click', '.claim-returned-notes-editor-submit', function(){
1115         let id = $(this).data('return-claim-id');
1116         let notes = $('#return-claim-notes-editor-textarea-' + id).val();
1117
1118         let params = {
1119             notes: notes,
1120             updated_by: logged_in_user_borrowernumber
1121         };
1122
1123         $(this).parent().remove();
1124
1125         $.ajax({
1126             url: '/api/v1/return_claims/' + id + '/notes',
1127             type: 'PUT',
1128             data: JSON.stringify(params),
1129             success: function( data ) {
1130                 let notes = $('#return-claim-notes-static-' + id);
1131                 notes.text(data.notes);
1132                 notes.show();
1133             },
1134             contentType: "json"
1135         });
1136     });
1137
1138     $('body').on('click', '.claim-returned-notes-editor-cancel', function(){
1139         let id = $(this).data('return-claim-id');
1140         $(this).parent().remove();
1141         $('#return-claim-notes-static-' + id).show();
1142     });
1143
1144     // Hanld return claim deletion
1145     $('body').on('click', '.return-claim-tools-delete', function() {
1146         let confirmed = confirm(__("Are you sure you want to delete this return claim?"));
1147         if ( confirmed ) {
1148             let id = $(this).data('return-claim-id');
1149
1150             $.ajax({
1151                 url: '/api/v1/return_claims/' + id,
1152                 type: 'DELETE',
1153                 success: function( data ) {
1154                     refreshReturnClaimsTable();
1155                     issuesTable.api().ajax.reload();
1156                 }
1157             });
1158         }
1159     });
1160
1161     $("#show_all_claims").on("click", function(e){
1162         e.preventDefault();
1163         $(".ctrl_link").removeClass("disabled");
1164         $(this).addClass("disabled");
1165         $("#return-claims-table").DataTable().search("").draw();
1166     });
1167
1168     $("#show_unresolved_claims").on("click", function (e) {
1169         e.preventDefault();
1170         $(".ctrl_link").removeClass("disabled");
1171         $(this).addClass("disabled");
1172         $("#return-claims-table").DataTable().search("is_unresolved").draw();
1173     });
1174
1175  });