Bug 10860 [QA Followup] - Fix misc issues
[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-immediately').change(function(){
132         if ( this.checked && typeof issuesTable === 'undefined') {
133             $('#issues-table-load-now-button').click();
134         }
135     });
136     $('#issues-table-load-now-button').click(function(){
137         LoadIssuesTable();
138         return false;
139     });
140
141     if ( $.cookie("issues-table-load-immediately-" + script) == "true" ) {
142         LoadIssuesTable();
143         $('#issues-table-load-immediately').prop('checked', true);
144     }
145     $('#issues-table-load-immediately').on( "change", function(){
146         $.cookie("issues-table-load-immediately-" + script, $(this).is(':checked'));
147     });
148
149     function LoadIssuesTable() {
150         $('#issues-table-loading-message').hide();
151         $('#issues-table').show();
152         $('#issues-table-actions').show();
153
154         issuesTable = $("#issues-table").dataTable({
155             "oLanguage": {
156                 "sEmptyTable" : MSG_DT_LOADING_RECORDS,
157             },
158             "bAutoWidth": false,
159             "sDom": "<'row-fluid'<'span6'><'span6'>r>t<'row-fluid'>t",
160             "aoColumns": [
161                 {
162                     "mDataProp": function( oObj ) {
163                         if ( oObj.issued_today ) {
164                             return "1" + oObj.timestamp;
165                         } else {
166                             return "0" + oObj.date_due;
167                         }
168                     }
169                 },
170                 {
171                     "mDataProp": function( oObj ) {
172                         if ( oObj.issued_today ) {
173                             return "<strong>" + TODAYS_CHECKOUTS + "</strong>";
174                         } else {
175                             return "<strong>" + PREVIOUS_CHECKOUTS + "</strong>";
176                         }
177                     }
178                 },
179                 {
180                     "mDataProp": "date_due",
181                     "bVisible": false,
182                 },
183                 {
184                     "iDataSort": 1, // Sort on hidden unformatted date due column
185                     "mDataProp": function( oObj ) {
186                         if ( oObj.date_due_overdue ) {
187                             return "<span class='overdue'>" + oObj.date_due_formatted + "</span>";
188                         } else {
189                             return oObj.date_due_formatted;
190                         }
191                     }
192                 },
193                 {
194                     "mDataProp": function ( oObj ) {
195                         title = "<span class='strong'><a href='/cgi-bin/koha/catalogue/detail.pl?biblionumber="
196                               + oObj.biblionumber
197                               + "'>"
198                               + oObj.title;
199
200                         $.each(oObj.subtitle, function( index, value ) {
201                                   title += " " + value.subfield;
202                         });
203
204                         title += "</a></span>";
205
206                         if ( oObj.author ) {
207                             title += " " + BY.replace( "_AUTHOR_",  " " + oObj.author );
208                         }
209
210                         if ( oObj.itemnotes ) {
211                             var span_class = "";
212                             if ( $.datepicker.formatDate('yy-mm-dd', new Date(oObj.issuedate) ) == ymd ) {
213                                 span_class = "circ-hlt";
214                             }
215                             title += " - <span class='" + span_class + "'>" + oObj.itemnotes + "</span>"
216                         }
217
218                         var onsite_checkout = '';
219                         if ( oObj.onsite_checkout == 1 ) {
220                             onsite_checkout += " <span class='onsite_checkout'>(" + INHOUSE_USE + ")</span>";
221                         }
222
223                         title += " "
224                               + "<a href='/cgi-bin/koha/catalogue/moredetail.pl?biblionumber="
225                               + oObj.biblionumber
226                               + "&itemnumber="
227                               + oObj.itemnumber
228                               + "#"
229                               + oObj.itemnumber
230                               + "'>"
231                               + oObj.barcode
232                               + "</a>"
233                               + onsite_checkout;
234
235                         return title;
236                     }
237                 },
238                 { "mDataProp": "itemtype" },
239                 { "mDataProp": "issuedate_formatted" },
240                 { "mDataProp": "branchname" },
241                 { "mDataProp": "itemcallnumber" },
242                 {
243                     "mDataProp": function ( oObj ) {
244                         if ( ! oObj.charge ) oObj.charge = 0;
245                         return parseFloat(oObj.charge).toFixed(2);
246                     }
247                 },
248                 {
249                     "mDataProp": function ( oObj ) {
250                         if ( ! oObj.price ) oObj.price = 0;
251                         return parseFloat(oObj.price).toFixed(2);
252                     }
253                 },
254                 {
255                     "bSortable": false,
256                     "mDataProp": function ( oObj ) {
257                         var content = "";
258                         var span_style = "";
259                         var span_class = "";
260
261                         content += "<span>";
262                         content += "<span style='padding: 0 1em;'>" + oObj.renewals_count + "</span>";
263
264                         if ( oObj.can_renew ) {
265                             // Do nothing
266                         } else if ( oObj.can_renew_error == "on_reserve" ) {
267                             content += "<span class='renewals-disabled'>"
268                                     + "<a href='/cgi-bin/koha/reserve/request.pl?biblionumber=" + oObj.biblionumber + "'>" + ON_HOLD + "</a>"
269                                     + "</span>";
270
271                             span_style = "display: none";
272                             span_class = "renewals-allowed";
273                         } else if ( oObj.can_renew_error == "too_many" ) {
274                             content += "<span class='renewals-disabled'>"
275                                     + NOT_RENEWABLE
276                                     + "</span>";
277
278                             span_style = "display: none";
279                             span_class = "renewals-allowed";
280                         } else if ( oObj.can_renew_error == "too_soon" ) {
281                             content += "<span class='renewals-disabled'>"
282                                     + NOT_RENEWABLE_TOO_SOON.format( oObj.can_renew_date )
283                                     + "</span>";
284
285                             span_style = "display: none";
286                             span_class = "renewals-allowed";
287                         } else if ( oObj.can_renew_error == "auto_too_soon" ) {
288                             content += "<span class='renewals-disabled'>"
289                                     + NOT_RENEWABLE_AUTO_TOO_SOON
290                                     + "</span>";
291
292                             span_style = "display: none";
293                             span_class = "renewals-allowed";
294                         } else if ( oObj.can_renew_error == "auto_renew" ) {
295                             content += "<span class='renewals-disabled'>"
296                                     + NOT_RENEWABLE_AUTO_RENEW
297                                     + "</span>";
298
299                             span_style = "display: none";
300                             span_class = "renewals-allowed";
301                         } else {
302                             content += "<span class='renewals-disabled'>"
303                                     + oObj.can_renew_error
304                                     + "</span>";
305
306                             span_style = "display: none";
307                             span_class = "renewals-allowed";
308                         }
309
310                         if ( oObj.renewals_remaining && oObj.onsite_checkout == 0 ) {
311                             content += "<span class='" + span_class + "' style='" + span_style + "'>"
312                                     +  "<input type='checkbox' class='renew' id='renew_" + oObj.itemnumber + "' name='renew' value='" + oObj.itemnumber +"'/>"
313                                     +  "</span>";
314
315                             content += "<span class='renewals'>("
316                                     + RENEWALS_REMAINING.format( oObj.renewals_remaining, oObj.renewals_allowed )
317                                     + ")</span>";
318                         }
319
320                         content += "</span>";
321
322                         return content;
323                     }
324                 },
325                 {
326                     "bSortable": false,
327                     "mDataProp": function ( oObj ) {
328                         if ( oObj.can_renew_error == "on_reserve" ) {
329                             return "<a href='/cgi-bin/koha/reserve/request.pl?biblionumber=" + oObj.biblionumber + "'>" + ON_HOLD + "</a>";
330                         } else {
331                             return "<input type='checkbox' class='checkin' id='checkin_" + oObj.itemnumber + "' name='checkin' value='" + oObj.itemnumber +"'></input>";
332                         }
333                     }
334                 },
335                 {
336                     "bVisible": exports_enabled ? true : false,
337                     "bSortable": false,
338                     "mDataProp": function ( oObj ) {
339                         return "<input type='checkbox' class='export' id='export_" + oObj.biblionumber + "' name='biblionumbers' value='" + oObj.biblionumber + "' />";
340                     }
341                 }
342             ],
343             "fnFooterCallback": function ( nRow, aaData, iStart, iEnd, aiDisplay ) {
344                 var total_charge = 0;
345                 var total_price = 0;
346                 for ( var i=0; i < aaData.length; i++ ) {
347                     total_charge += aaData[i]['charge'] * 1;
348                     total_price  += aaData[i]['price'] * 1;
349                 }
350                 var nCells = nRow.getElementsByTagName('td');
351                 nCells[1].innerHTML = total_charge.toFixed(2);
352                 nCells[2].innerHTML = total_price.toFixed(2);
353             },
354             "bPaginate": false,
355             "bProcessing": true,
356             "bServerSide": false,
357             "sAjaxSource": '/cgi-bin/koha/svc/checkouts',
358             "fnServerData": function ( sSource, aoData, fnCallback ) {
359                 aoData.push( { "name": "borrowernumber", "value": borrowernumber } );
360
361                 $.getJSON( sSource, aoData, function (json) {
362                     fnCallback(json)
363                 } );
364             },
365             "fnInitComplete": function(oSettings) {
366                 // Disable rowGrouping plugin after first use
367                 // so any sorting on the table doesn't use it
368                 var oSettings = issuesTable.fnSettings();
369
370                 for (f = 0; f < oSettings.aoDrawCallback.length; f++) {
371                     if (oSettings.aoDrawCallback[f].sName == 'fnRowGrouping') {
372                         oSettings.aoDrawCallback.splice(f, 1);
373                         break;
374                     }
375                 }
376
377                 oSettings.aaSortingFixed = null;
378             },
379         }).rowGrouping(
380             {
381                 iGroupingColumnIndex: 1,
382                 iGroupingOrderByColumnIndex: 0,
383                 sGroupingColumnSortDirection: "desc"
384             }
385         );
386
387         if ( $("#issues-table").length ) {
388             $("#issues-table_processing").position({
389                 of: $( "#issues-table" ),
390                 collision: "none"
391             });
392         }
393     }
394
395     // Don't load relatives' issues table unless it is clicked on
396     var relativesIssuesTable;
397     $("#relatives-issues-tab").click( function() {
398         if ( ! relativesIssuesTable ) {
399             relativesIssuesTable = $("#relatives-issues-table").dataTable({
400                 "bAutoWidth": false,
401                 "sDom": "<'row-fluid'<'span6'><'span6'>r>t<'row-fluid'>t",
402                 "aaSorting": [],
403                 "aoColumns": [
404                     {
405                         "mDataProp": "date_due",
406                         "bVisible": false,
407                     },
408                     {
409                         "iDataSort": 1, // Sort on hidden unformatted date due column
410                         "mDataProp": function( oObj ) {
411                             var today = new Date();
412                             var due = new Date( oObj.date_due );
413                             if ( today > due ) {
414                                 return "<span class='overdue'>" + oObj.date_due_formatted + "</span>";
415                             } else {
416                                 return oObj.date_due_formatted;
417                             }
418                         }
419                     },
420                     {
421                         "mDataProp": function ( oObj ) {
422                             title = "<span class='strong'><a href='/cgi-bin/koha/catalogue/detail.pl?biblionumber="
423                                   + oObj.biblionumber
424                                   + "'>"
425                                   + oObj.title;
426
427                             $.each(oObj.subtitle, function( index, value ) {
428                                       title += " " + value.subfield;
429                             });
430
431                             title += "</a></span>";
432
433                             if ( oObj.author ) {
434                                 title += " " + BY + " " + oObj.author;
435                             }
436
437                             if ( oObj.itemnotes ) {
438                                 var span_class = "";
439                                 if ( $.datepicker.formatDate('yy-mm-dd', new Date(oObj.issuedate) ) == ymd ) {
440                                     span_class = "circ-hlt";
441                                 }
442                                 title += " - <span class='" + span_class + "'>" + oObj.itemnotes + "</span>"
443                             }
444
445                             var onsite_checkout = '';
446                             if ( oObj.onsite_checkout == 1 ) {
447                                 onsite_checkout += " <span class='onsite_checkout'>(" + INHOUSE_USE + ")</span>";
448                             }
449
450                             title += " "
451                                   + "<a href='/cgi-bin/koha/catalogue/moredetail.pl?biblionumber="
452                                   + oObj.biblionumber
453                                   + "&itemnumber="
454                                   + oObj.itemnumber
455                                   + "#"
456                                   + oObj.itemnumber
457                                   + "'>"
458                                   + oObj.barcode
459                                   + "</a>"
460                                   + onsite_checkout;
461
462                             return title;
463                         }
464                     },
465                     { "mDataProp": "itemtype" },
466                     { "mDataProp": "issuedate_formatted" },
467                     { "mDataProp": "branchname" },
468                     { "mDataProp": "itemcallnumber" },
469                     {
470                         "mDataProp": function ( oObj ) {
471                             if ( ! oObj.charge ) oObj.charge = 0;
472                             return parseFloat(oObj.charge).toFixed(2);
473                         }
474                     },
475                     {
476                         "mDataProp": function ( oObj ) {
477                             if ( ! oObj.price ) oObj.price = 0;
478                             return parseFloat(oObj.price).toFixed(2);
479                         }
480                     },
481                     {
482                         "mDataProp": function( oObj ) {
483                             return "<a href='/cgi-bin/koha/members/moremember.pl?borrowernumber=" + oObj.borrowernumber + "'>"
484                                  + oObj.borrower.firstname + " " + oObj.borrower.surname + " (" + oObj.borrower.cardnumber + ")</a>"
485                         }
486                     },
487                 ],
488                 "bPaginate": false,
489                 "bProcessing": true,
490                 "bServerSide": false,
491                 "sAjaxSource": '/cgi-bin/koha/svc/checkouts',
492                 "fnServerData": function ( sSource, aoData, fnCallback ) {
493                     $.each(relatives_borrowernumbers, function( index, value ) {
494                         aoData.push( { "name": "borrowernumber", "value": value } );
495                     });
496
497                     $.getJSON( sSource, aoData, function (json) {
498                         fnCallback(json)
499                     } );
500                 },
501             });
502         }
503     });
504
505     if ( $("#relatives-issues-table").length ) {
506         $("#relatives-issues-table_processing").position({
507             of: $( "#relatives-issues-table" ),
508             collision: "none"
509         });
510     }
511
512     if ( AllowRenewalLimitOverride ) {
513         $( '#override_limit' ).click( function () {
514             if ( this.checked ) {
515                 $( '.renewals-allowed' ).show(); $( '.renewals-disabled' ).hide();
516             } else {
517                 $( '.renewals-allowed' ).hide(); $( '.renewals-disabled' ).show();
518             }
519         } ).attr( 'checked', false );
520     }
521  });