Bug 12550 - Add ability to delay loading of patron's checkouts in circulation and...
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / js / checkouts.js
1 $(document).ready(function() {
2     $.ajaxSetup ({ cache: false });
3
4     // Handle the select all/none links for checkouts table columns
5     $("#CheckAllRenewals").on("click",function(){
6         $("#UncheckAllCheckins").click();
7         $(".renew:visible").attr("checked", "checked" );
8         return false;
9     });
10     $("#UncheckAllRenewals").on("click",function(){
11         $(".renew:visible").removeAttr("checked");
12         return false;
13     });
14
15     $("#CheckAllCheckins").on("click",function(){
16         $("#UncheckAllRenewals").click();
17         $(".checkin:visible").attr("checked", "checked" );
18         return false;
19     });
20     $("#UncheckAllCheckins").on("click",function(){
21         $(".checkin:visible").removeAttr("checked");
22         return false;
23     });
24
25     // Don't allow both return and renew checkboxes to be checked
26     $(document).on("change", '.renew', function(){
27         if ( $(this).is(":checked") ) {
28             $( "#checkin_" + $(this).val() ).removeAttr("checked");
29         }
30     });
31     $(document).on("change", '.checkin', function(){
32         if ( $(this).is(":checked") ) {
33             $( "#renew_" + $(this).val() ).removeAttr("checked");
34         }
35     });
36
37     // Clicking the table cell checks the checkbox inside it
38     $(document).on("click", 'td', function(e){
39         if(e.target.tagName.toLowerCase() == 'td'){
40           $(this).find("input:checkbox:visible").each( function() {
41             $(this).click();
42           });
43         }
44     });
45
46     // Handle renewals and returns
47     $("#RenewCheckinChecked").on("click",function(){
48         $(".checkin:checked:visible").each(function() {
49             itemnumber = $(this).val();
50
51             $(this).replaceWith("<img id='checkin_" + itemnumber + "' src='" + interface + "/" + theme + "/img/loading-small.gif' />");
52
53             params = {
54                 itemnumber:     itemnumber,
55                 borrowernumber: borrowernumber,
56                 branchcode:     branchcode,
57                 exempt_fine:    $("#exemptfine").is(':checked')
58             };
59
60             $.post( "/cgi-bin/koha/svc/checkin", params, function( data ) {
61                 id = "#checkin_" + data.itemnumber;
62
63                 content = "";
64                 if ( data.returned ) {
65                     content = CIRCULATION_RETURNED;
66                 } else {
67                     content = CIRCULATION_NOT_RETURNED;
68                 }
69
70                 $(id).replaceWith( content );
71             }, "json")
72         });
73
74         $(".renew:checked:visible").each(function() {
75             var override_limit = $("#override_limit").is(':checked') ? 1 : 0;
76
77             var itemnumber = $(this).val();
78
79             $(this).parent().parent().replaceWith("<img id='renew_" + itemnumber + "' src='" + interface + "/" + theme + "/img/loading-small.gif' />");
80
81             var params = {
82                 itemnumber:     itemnumber,
83                 borrowernumber: borrowernumber,
84                 branchcode:     branchcode,
85                 override_limit: override_limit,
86                 date_due:       $("#newduedate").val()
87             };
88
89             $.post( "/cgi-bin/koha/svc/renew", params, function( data ) {
90                 var id = "#renew_" + data.itemnumber;
91
92                 var content = "";
93                 if ( data.renew_okay ) {
94                     content = CIRCULATION_RENEWED_DUE + " " + data.date_due;
95                 } else {
96                     content = CIRCULATION_RENEW_FAILED + " ";
97                     if ( data.error == "no_checkout" ) {
98                         content += NOT_CHECKED_OUT;
99                     } else if ( data.error == "too_many" ) {
100                         content += TOO_MANY_RENEWALS;
101                     } else if ( data.error == "on_reserve" ) {
102                         content += ON_RESERVE;
103                     } else if ( data.error ) {
104                         content += data.error;
105                     } else {
106                         content += REASON_UNKNOWN;
107                     }
108                 }
109
110                 $(id).replaceWith( content );
111             }, "json")
112         });
113
114         // Prevent form submit
115         return false;
116     });
117
118     $("#RenewAll").on("click",function(){
119         $("#CheckAllRenewals").click();
120         $("#UncheckAllCheckins").click();
121         $("#RenewCheckinChecked").click();
122
123         // Prevent form submit
124         return false;
125     });
126
127     var ymd = $.datepicker.formatDate('yy-mm-dd', new Date());
128
129     $('#issues-table').hide();
130     $('#issues-table-actions').hide();
131     $('#issues-table-load-now-button').click(function(){
132         LoadIssuesTable();
133         return false;
134     });
135
136     if ( $.cookie("issues-table-load-immediately-" + script) == "true" ) {
137         LoadIssuesTable();
138         $('#issues-table-load-immediately').prop('checked', true);
139     }
140     $('#issues-table-load-immediately').on( "change", function(){
141         $.cookie("issues-table-load-immediately-" + script, $(this).is(':checked'));
142     });
143
144     function LoadIssuesTable() {
145         $('#issues-table-loading-message').hide();
146         $('#issues-table').show();
147         $('#issues-table-actions').show();
148
149         issuesTable = $("#issues-table").dataTable({
150             "oLanguage": {
151                 "sEmptyTable" : MSG_DT_LOADING_RECORDS,
152             },
153             "bAutoWidth": false,
154             "sDom": "<'row-fluid'<'span6'><'span6'>r>t<'row-fluid'>t",
155             "aoColumns": [
156                 {
157                     "mDataProp": function( oObj ) {
158                         if ( oObj.issued_today ) {
159                             return "0";
160                         } else {
161                             return "100";
162                         }
163                     }
164                 },
165                 {
166                     "mDataProp": function( oObj ) {
167                         if ( oObj.issued_today ) {
168                             return "<strong>" + TODAYS_CHECKOUTS + "</strong>";
169                         } else {
170                             return "<strong>" + PREVIOUS_CHECKOUTS + "</strong>";
171                         }
172                     }
173                 },
174                 {
175                     "mDataProp": "date_due",
176                     "bVisible": false,
177                 },
178                 {
179                     "iDataSort": 1, // Sort on hidden unformatted date due column
180                     "mDataProp": function( oObj ) {
181                         if ( oObj.date_due_overdue ) {
182                             return "<span class='overdue'>" + oObj.date_due_formatted + "</span>";
183                         } else {
184                             return oObj.date_due_formatted;
185                         }
186                     }
187                 },
188                 {
189                     "mDataProp": function ( oObj ) {
190                         title = "<span class='strong'><a href='/cgi-bin/koha/catalogue/detail.pl?biblionumber="
191                               + oObj.biblionumber
192                               + "'>"
193                               + oObj.title;
194
195                         $.each(oObj.subtitle, function( index, value ) {
196                                   title += " " + value.subfield;
197                         });
198
199                         title += "</a></span>";
200
201                         if ( oObj.author ) {
202                             title += " " + BY.replace( "_AUTHOR_",  " " + oObj.author );
203                         }
204
205                         if ( oObj.itemnotes ) {
206                             var span_class = "";
207                             if ( $.datepicker.formatDate('yy-mm-dd', new Date(oObj.issuedate) ) == ymd ) {
208                                 span_class = "circ-hlt";
209                             }
210                             title += " - <span class='" + span_class + "'>" + oObj.itemnotes + "</span>"
211                         }
212
213                         title += " "
214                               + "<a href='/cgi-bin/koha/catalogue/moredetail.pl?biblionumber="
215                               + oObj.biblionumber
216                               + "&itemnumber="
217                               + oObj.itemnumber
218                               + "#"
219                               + oObj.itemnumber
220                               + "'>"
221                               + oObj.barcode
222                               + "</a>";
223
224                         return title;
225                     }
226                 },
227                 { "mDataProp": "itemtype" },
228                 { "mDataProp": "issuedate_formatted" },
229                 { "mDataProp": "branchname" },
230                 { "mDataProp": "itemcallnumber" },
231                 {
232                     "mDataProp": function ( oObj ) {
233                         if ( ! oObj.charge ) oObj.charge = 0;
234                         return parseFloat(oObj.charge).toFixed(2);
235                     }
236                 },
237                 {
238                     "mDataProp": function ( oObj ) {
239                         if ( ! oObj.price ) oObj.price = 0;
240                         return parseFloat(oObj.price).toFixed(2);
241                     }
242                 },
243                 {
244                     "bSortable": false,
245                     "mDataProp": function ( oObj ) {
246                         var content = "";
247                         var span_style = "";
248                         var span_class = "";
249
250                         content += "<span>";
251                         content += "<span style='padding: 0 1em;'>" + oObj.renewals_count + "</span>";
252
253                         if ( oObj.can_renew ) {
254                             // Do nothing
255                         } else if ( oObj.can_renew_error == "on_reserve" ) {
256                             content += "<span class='renewals-disabled'>"
257                                     + "<a href='/cgi-bin/koha/reserve/request.pl?biblionumber=" + oObj.biblionumber + "'>" + ON_HOLD + "</a>"
258                                     + "</span>";
259
260                             span_style = "display: none";
261                             span_class = "renewals-allowed";
262                         } else if ( oObj.can_renew_error == "too_many" ) {
263                             content += "<span class='renewals-disabled'>"
264                                     + NOT_RENEWABLE
265                                     + "</span>";
266
267                             span_style = "display: none";
268                             span_class = "renewals-allowed";
269                         } else if ( oObj.can_renew_error == "too_soon" ) {
270                             content += "<span class='renewals-disabled'>"
271                                     + NOT_RENEWABLE_TOO_SOON.format( oObj.can_renew_date )
272                                     + "</span>";
273
274                             span_style = "display: none";
275                             span_class = "renewals-allowed";
276                         } else if ( oObj.can_renew_error == "auto_too_soon" ) {
277                             content += "<span class='renewals-disabled'>"
278                                     + NOT_RENEWABLE_AUTO_TOO_SOON
279                                     + "</span>";
280
281                             span_style = "display: none";
282                             span_class = "renewals-allowed";
283                         } else if ( oObj.can_renew_error == "auto_renew" ) {
284                             content += "<span class='renewals-disabled'>"
285                                     + NOT_RENEWABLE_AUTO_RENEW
286                                     + "</span>";
287
288                             span_style = "display: none";
289                             span_class = "renewals-allowed";
290                         } else {
291                             content += "<span class='renewals-disabled'>"
292                                     + oObj.can_renew_error
293                                     + "</span>";
294
295                             span_style = "display: none";
296                             span_class = "renewals-allowed";
297                         }
298
299                         content += "<span class='" + span_class + "' style='" + span_style + "'>"
300                                 +  "<input type='checkbox' class='renew' id='renew_" + oObj.itemnumber + "' name='renew' value='" + oObj.itemnumber +"'/>"
301                                 +  "</span>";
302
303                         if ( oObj.renewals_remaining ) {
304                             content += "<span class='renewals'>("
305                                     + RENEWALS_REMAINING.format( oObj.renewals_remaining, oObj.renewals_allowed )
306                                     + ")</span>";
307                         }
308
309                         content += "</span>";
310
311
312                         return content;
313                     }
314                 },
315                 {
316                     "bSortable": false,
317                     "mDataProp": function ( oObj ) {
318                         if ( oObj.can_renew_error == "on_reserve" ) {
319                             return "<a href='/cgi-bin/koha/reserve/request.pl?biblionumber=" + oObj.biblionumber + "'>" + ON_HOLD + "</a>";
320                         } else {
321                             return "<input type='checkbox' class='checkin' id='checkin_" + oObj.itemnumber + "' name='checkin' value='" + oObj.itemnumber +"'></input>";
322                         }
323                     }
324                 },
325                 {
326                     "bVisible": exports_enabled ? true : false,
327                     "bSortable": false,
328                     "mDataProp": function ( oObj ) {
329                         return "<input type='checkbox' class='export' id='export_" + oObj.biblionumber + "' name='biblionumbers' value='" + oObj.biblionumber + "' />";
330                     }
331                 }
332             ],
333             "fnFooterCallback": function ( nRow, aaData, iStart, iEnd, aiDisplay ) {
334                 var total_charge = 0;
335                 var total_price = 0;
336                 for ( var i=0; i < aaData.length; i++ ) {
337                     total_charge += aaData[i]['charge'] * 1;
338                     total_price  += aaData[i]['price'] * 1;
339                 }
340                 var nCells = nRow.getElementsByTagName('td');
341                 nCells[1].innerHTML = total_charge.toFixed(2);
342                 nCells[2].innerHTML = total_price.toFixed(2);
343             },
344             "bPaginate": false,
345             "bProcessing": true,
346             "bServerSide": false,
347             "sAjaxSource": '/cgi-bin/koha/svc/checkouts',
348             "fnServerData": function ( sSource, aoData, fnCallback ) {
349                 aoData.push( { "name": "borrowernumber", "value": borrowernumber } );
350
351                 $.getJSON( sSource, aoData, function (json) {
352                     fnCallback(json)
353                 } );
354             },
355             "fnInitComplete": function(oSettings) {
356                 // Disable rowGrouping plugin after first use
357                 // so any sorting on the table doesn't use it
358                 var oSettings = issuesTable.fnSettings();
359
360                 for (f = 0; f < oSettings.aoDrawCallback.length; f++) {
361                     if (oSettings.aoDrawCallback[f].sName == 'fnRowGrouping') {
362                         oSettings.aoDrawCallback.splice(f, 1);
363                         break;
364                     }
365                 }
366
367                 oSettings.aaSortingFixed = null;
368             },
369         }).rowGrouping(
370             {
371                 iGroupingColumnIndex: 1,
372                 iGroupingOrderByColumnIndex: 0,
373                 sGroupingColumnSortDirection: "asc"
374             }
375         );
376
377         if ( $("#issues-table").length ) {
378             $("#issues-table_processing").position({
379                 of: $( "#issues-table" ),
380                 collision: "none"
381             });
382         }
383     }
384
385     // Don't load relatives' issues table unless it is clicked on
386     var relativesIssuesTable;
387     $("#relatives-issues-tab").click( function() {
388         if ( ! relativesIssuesTable ) {
389             relativesIssuesTable = $("#relatives-issues-table").dataTable({
390                 "bAutoWidth": false,
391                 "sDom": "<'row-fluid'<'span6'><'span6'>r>t<'row-fluid'>t",
392                 "aaSorting": [],
393                 "aoColumns": [
394                     {
395                         "mDataProp": "date_due",
396                         "bVisible": false,
397                     },
398                     {
399                         "iDataSort": 1, // Sort on hidden unformatted date due column
400                         "mDataProp": function( oObj ) {
401                             var today = new Date();
402                             var due = new Date( oObj.date_due );
403                             if ( today > due ) {
404                                 return "<span class='overdue'>" + oObj.date_due_formatted + "</span>";
405                             } else {
406                                 return oObj.date_due_formatted;
407                             }
408                         }
409                     },
410                     {
411                         "mDataProp": function ( oObj ) {
412                             title = "<span class='strong'><a href='/cgi-bin/koha/catalogue/detail.pl?biblionumber="
413                                   + oObj.biblionumber
414                                   + "'>"
415                                   + oObj.title;
416
417                             $.each(oObj.subtitle, function( index, value ) {
418                                       title += " " + value.subfield;
419                             });
420
421                             title += "</a></span>";
422
423                             if ( oObj.author ) {
424                                 title += " " + BY + " " + oObj.author;
425                             }
426
427                             if ( oObj.itemnotes ) {
428                                 var span_class = "";
429                                 if ( $.datepicker.formatDate('yy-mm-dd', new Date(oObj.issuedate) ) == ymd ) {
430                                     span_class = "circ-hlt";
431                                 }
432                                 title += " - <span class='" + span_class + "'>" + oObj.itemnotes + "</span>"
433                             }
434
435                             title += " "
436                                   + "<a href='/cgi-bin/koha/catalogue/moredetail.pl?biblionumber="
437                                   + oObj.biblionumber
438                                   + "&itemnumber="
439                                   + oObj.itemnumber
440                                   + "#"
441                                   + oObj.itemnumber
442                                   + "'>"
443                                   + oObj.barcode
444                                   + "</a>";
445
446                             return title;
447                         }
448                     },
449                     { "mDataProp": "itemtype" },
450                     { "mDataProp": "issuedate_formatted" },
451                     { "mDataProp": "branchname" },
452                     { "mDataProp": "itemcallnumber" },
453                     {
454                         "mDataProp": function ( oObj ) {
455                             if ( ! oObj.charge ) oObj.charge = 0;
456                             return parseFloat(oObj.charge).toFixed(2);
457                         }
458                     },
459                     {
460                         "mDataProp": function ( oObj ) {
461                             if ( ! oObj.price ) oObj.price = 0;
462                             return parseFloat(oObj.price).toFixed(2);
463                         }
464                     },
465                     {
466                         "mDataProp": function( oObj ) {
467                             return "<a href='/cgi-bin/koha/members/moremember.pl?borrowernumber=" + oObj.borrowernumber + "'>"
468                                  + oObj.borrower.firstname + " " + oObj.borrower.surname + " (" + oObj.borrower.cardnumber + ")</a>"
469                         }
470                     },
471                 ],
472                 "bPaginate": false,
473                 "bProcessing": true,
474                 "bServerSide": false,
475                 "sAjaxSource": '/cgi-bin/koha/svc/checkouts',
476                 "fnServerData": function ( sSource, aoData, fnCallback ) {
477                     $.each(relatives_borrowernumbers, function( index, value ) {
478                         aoData.push( { "name": "borrowernumber", "value": value } );
479                     });
480
481                     $.getJSON( sSource, aoData, function (json) {
482                         fnCallback(json)
483                     } );
484                 },
485             });
486         }
487     });
488
489     if ( $("#relatives-issues-table").length ) {
490         $("#relatives-issues-table_processing").position({
491             of: $( "#relatives-issues-table" ),
492             collision: "none"
493         });
494     }
495
496     if ( AllowRenewalLimitOverride ) {
497         $( '#override_limit' ).click( function () {
498             if ( this.checked ) {
499                 $( '.renewals-allowed' ).show(); $( '.renewals-disabled' ).hide();
500             } else {
501                 $( '.renewals-allowed' ).hide(); $( '.renewals-disabled' ).show();
502             }
503         } ).attr( 'checked', false );
504     }
505  });