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