Bug 15975 - Add Owning Library Column to Checkouts
[koha.git] / koha-tmpl / intranet-tmpl / prog / 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").prop("checked", true);
10         return false;
11     });
12     $("#UncheckAllRenewals").on("click",function(){
13         $(".renew:visible").prop("checked", false);
14         return false;
15     });
16
17     $("#CheckAllCheckins").on("click",function(){
18         $("#UncheckAllRenewals").click();
19         $(".checkin:visible").prop("checked", true);
20         return false;
21     });
22     $("#UncheckAllCheckins").on("click",function(){
23         $(".checkin:visible").prop("checked", false);
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() ).prop("checked", false);
31         }
32     });
33     $(document).on("change", '.checkin', function(){
34         if ( $(this).is(":checked") ) {
35             $( "#renew_" + $(this).val() ).prop("checked", false);
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 == "restriction" ) {
110                         content += NOT_RENEWABLE_RESTRICTION;
111                     } else if ( data.error == "overdue" ) {
112                         content += NOT_RENEWABLE_OVERDUE;
113                     } else if ( data.error ) {
114                         content += data.error;
115                     } else {
116                         content += REASON_UNKNOWN;
117                     }
118                 }
119
120                 $(id).replaceWith( content );
121             }, "json")
122         });
123
124         // Refocus on barcode field if it exists
125         if ( $("#barcode").length ) {
126             $("#barcode").focus();
127         }
128
129         // Prevent form submit
130         return false;
131     });
132
133     $("#RenewAll").on("click",function(){
134         $("#CheckAllRenewals").click();
135         $("#UncheckAllCheckins").click();
136         $("#RenewCheckinChecked").click();
137
138         // Prevent form submit
139         return false;
140     });
141
142     var ymd = $.datepicker.formatDate('yy-mm-dd', new Date());
143
144     $('#issues-table').hide();
145     $('#issues-table-actions').hide();
146     $('#issues-table-load-immediately').change(function(){
147         if ( this.checked && typeof issuesTable === 'undefined') {
148             $('#issues-table-load-now-button').click();
149         }
150         barcodefield.focus();
151     });
152     $('#issues-table-load-now-button').click(function(){
153         LoadIssuesTable();
154         barcodefield.focus();
155         return false;
156     });
157
158     if ( $.cookie("issues-table-load-immediately-" + script) == "true" ) {
159         LoadIssuesTable();
160         $('#issues-table-load-immediately').prop('checked', true);
161     }
162     $('#issues-table-load-immediately').on( "change", function(){
163         $.cookie("issues-table-load-immediately-" + script, $(this).is(':checked'), { expires: 365 });
164     });
165
166     function LoadIssuesTable() {
167         $('#issues-table-loading-message').hide();
168         $('#issues-table').show();
169         $('#issues-table-actions').show();
170
171         issuesTable = KohaTable("#issues-table", {
172             "oLanguage": {
173                 "sEmptyTable" : MSG_DT_LOADING_RECORDS,
174                 "sProcessing": MSG_DT_LOADING_RECORDS,
175             },
176             "bAutoWidth": false,
177             "dom": 'B<"clearfix">rt',
178             "aoColumns": [
179                 {
180                     "mDataProp": function( oObj ) {
181                         return oObj.sort_order;
182                     }
183                 },
184                 {
185                     "mDataProp": function( oObj ) {
186                         if ( oObj.issued_today ) {
187                             return "<strong>" + TODAYS_CHECKOUTS + "</strong>";
188                         } else {
189                             return "<strong>" + PREVIOUS_CHECKOUTS + "</strong>";
190                         }
191                     }
192                 },
193                 {
194                     "mDataProp": "date_due",
195                     "bVisible": false,
196                 },
197                 {
198                     "iDataSort": 2, // Sort on hidden unformatted date due column
199                     "mDataProp": function( oObj ) {
200                         var due = oObj.date_due_formatted;
201
202                         if ( oObj.date_due_overdue ) {
203                             due = "<span class='overdue'>" + due + "</span>";
204                         }
205
206                         if ( oObj.lost ) {
207                             due += "<span class='lost'>" + oObj.lost + "</span>";
208                         }
209
210                         if ( oObj.damaged ) {
211                             due += "<span class='dmg'>" + oObj.damaged + "</span>";
212                         }
213
214                         due = "<span id='date_due_" + oObj.itemnumber + "' class='date_due'>" + due + "</span>";
215
216                         return due;
217                     }
218                 },
219                 {
220                     "mDataProp": function ( oObj ) {
221                         title = "<span class='strong'><a href='/cgi-bin/koha/catalogue/detail.pl?biblionumber="
222                               + oObj.biblionumber
223                               + "'>"
224                               + oObj.title;
225
226                         $.each(oObj.subtitle, function( index, value ) {
227                                   title += " " + value.subfield;
228                         });
229
230                         if ( oObj.enumchron ) {
231                             title += " (" + oObj.enumchron + ")";
232                         }
233
234                         title += "</a></span>";
235
236                         if ( oObj.author ) {
237                             title += " " + BY.replace( "_AUTHOR_",  " " + oObj.author );
238                         }
239
240                         if ( oObj.itemnotes ) {
241                             var span_class = "";
242                             if ( $.datepicker.formatDate('yy-mm-dd', new Date(oObj.issuedate) ) == ymd ) {
243                                 span_class = "circ-hlt";
244                             }
245                             title += " - <span class='" + span_class + "'>" + oObj.itemnotes + "</span>"
246                         }
247
248                         if ( oObj.itemnotes_nonpublic ) {
249                             var span_class = "";
250                             if ( $.datepicker.formatDate('yy-mm-dd', new Date(oObj.issuedate) ) == ymd ) {
251                                 span_class = "circ-hlt";
252                             }
253                             title += " - <span class='" + span_class + "'>" + oObj.itemnotes_nonpublic + "</span>"
254                         }
255
256                         var onsite_checkout = '';
257                         if ( oObj.onsite_checkout == 1 ) {
258                             onsite_checkout += " <span class='onsite_checkout'>(" + INHOUSE_USE + ")</span>";
259                         }
260
261                         title += " "
262                               + "<a href='/cgi-bin/koha/catalogue/moredetail.pl?biblionumber="
263                               + oObj.biblionumber
264                               + "&itemnumber="
265                               + oObj.itemnumber
266                               + "#"
267                               + oObj.itemnumber
268                               + "'>"
269                               + oObj.barcode
270                               + "</a>"
271                               + onsite_checkout;
272
273                         return title;
274                     },
275                     "sType": "anti-the"
276                 },
277                 { "mDataProp": "itemtype_description" },
278                 { "mDataProp": "location" },
279                 { "mDataProp": "homebranch" },
280                 { "mDataProp": "issuedate_formatted" },
281                 { "mDataProp": "branchname" },
282                 { "mDataProp": "itemcallnumber" },
283                 {
284                     "mDataProp": function ( oObj ) {
285                         if ( ! oObj.charge ) oObj.charge = 0;
286                         return '<span style="text-align: right; display: block;">' + parseFloat(oObj.charge).toFixed(2) + '<span>';
287                     }
288                 },
289                 {
290                     "mDataProp": function ( oObj ) {
291                         if ( ! oObj.fine ) oObj.fine = 0;
292                         return '<span style="text-align: right; display: block;">' + parseFloat(oObj.fine).toFixed(2)  + '<span>';
293                     }
294                 },
295                 {
296                     "mDataProp": function ( oObj ) {
297                         if ( ! oObj.price ) oObj.price = 0;
298                         return '<span style="text-align: right; display: block;">' + parseFloat(oObj.price).toFixed(2) + '<span>';
299                     }
300                 },
301                 {
302                     "bSortable": false,
303                     "bVisible": AllowCirculate ? true : false,
304                     "mDataProp": function ( oObj ) {
305                         var content = "";
306                         var span_style = "";
307                         var span_class = "";
308
309                         content += "<span>";
310                         content += "<span style='padding: 0 1em;'>" + oObj.renewals_count + "</span>";
311
312                         if ( oObj.can_renew ) {
313                             // Do nothing
314                         } else if ( oObj.can_renew_error == "on_reserve" ) {
315                             content += "<span class='renewals-disabled-no-override'>"
316                                     + "<a href='/cgi-bin/koha/reserve/request.pl?biblionumber=" + oObj.biblionumber + "'>" + ON_HOLD + "</a>"
317                                     + "</span>";
318
319                             span_style = "display: none";
320                             span_class = "renewals-allowed";
321                         } else if ( oObj.can_renew_error == "too_many" ) {
322                             content += "<span class='renewals-disabled'>"
323                                     + NOT_RENEWABLE
324                                     + "</span>";
325
326                             span_style = "display: none";
327                             span_class = "renewals-allowed";
328                         } else if ( oObj.can_renew_error == "restriction" ) {
329                             content += "<span class='renewals-disabled'>"
330                                     + NOT_RENEWABLE_RESTRICTION
331                                     + "</span>";
332
333                             span_style = "display: none";
334                             span_class = "renewals-allowed";
335                         } else if ( oObj.can_renew_error == "overdue" ) {
336                             content += "<span class='renewals-disabled'>"
337                                     + NOT_RENEWABLE_OVERDUE
338                                     + "</span>";
339
340                             span_style = "display: none";
341                             span_class = "renewals-allowed";
342                         } else if ( oObj.can_renew_error == "too_soon" ) {
343                             content += "<span class='renewals-disabled'>"
344                                     + NOT_RENEWABLE_TOO_SOON.format( oObj.can_renew_date )
345                                     + "</span>";
346
347                             span_style = "display: none";
348                             span_class = "renewals-allowed";
349                         } else if ( oObj.can_renew_error == "auto_too_soon" ) {
350                             content += "<span class='renewals-disabled'>"
351                                     + NOT_RENEWABLE_AUTO_TOO_SOON
352                                     + "</span>";
353
354                             span_style = "display: none";
355                             span_class = "renewals-allowed";
356                         } else if ( oObj.can_renew_error == "auto_renew" ) {
357                             content += "<span class='renewals-disabled'>"
358                                     + NOT_RENEWABLE_AUTO_RENEW
359                                     + "</span>";
360
361                             span_style = "display: none";
362                             span_class = "renewals-allowed";
363                         } else if ( oObj.can_renew_error == "onsite_checkout" ) {
364                             // Don't display something if it's an onsite checkout
365                         } else {
366                             content += "<span class='renewals-disabled'>"
367                                     + oObj.can_renew_error
368                                     + "</span>";
369
370                             span_style = "display: none";
371                             span_class = "renewals-allowed";
372                         }
373
374                         var can_force_renew = ( oObj.onsite_checkout == 0 ) && ( oObj.can_renew_error != "on_reserve" );
375                         var can_renew = ( oObj.renewals_remaining > 0  && !oObj.can_renew_error );
376                         if ( can_renew || can_force_renew ) {
377                             content += "<span class='" + span_class + "' style='" + span_style + "'>"
378                                     +  "<input type='checkbox' ";
379                             if ( oObj.date_due_overdue && can_renew ) {
380                                 content += "checked='checked' ";
381                             }
382                             content += "class='renew' id='renew_" + oObj.itemnumber + "' name='renew' value='" + oObj.itemnumber +"'/>"
383                                     +  "</span>";
384
385                             content += "<span class='renewals'>("
386                                     + RENEWALS_REMAINING.format( oObj.renewals_remaining, oObj.renewals_allowed )
387                                     + ")</span>";
388                         }
389
390                         content += "</span>";
391
392                         return content;
393                     }
394                 },
395                 {
396                     "bSortable": false,
397                     "bVisible": AllowCirculate ? true : false,
398                     "mDataProp": function ( oObj ) {
399                         if ( oObj.can_renew_error == "on_reserve" ) {
400                             return "<a href='/cgi-bin/koha/reserve/request.pl?biblionumber=" + oObj.biblionumber + "'>" + ON_HOLD + "</a>";
401                         } else {
402                             return "<input type='checkbox' class='checkin' id='checkin_" + oObj.itemnumber + "' name='checkin' value='" + oObj.itemnumber +"'></input>";
403                         }
404                     }
405                 },
406                 {
407                     "bVisible": exports_enabled ? true : false,
408                     "bSortable": false,
409                     "mDataProp": function ( oObj ) {
410                         var s = "<input type='checkbox' name='itemnumbers' value='" + oObj.itemnumber + "' style='visibility:hidden;' />";
411
412                         s += "<input type='checkbox' class='export' id='export_" + oObj.biblionumber + "' name='biblionumbers' value='" + oObj.biblionumber + "' />";
413                         return s;
414                     }
415                 }
416             ],
417             "fnFooterCallback": function ( nRow, aaData, iStart, iEnd, aiDisplay ) {
418                 var total_charge = 0;
419                 var total_fine  = 0;
420                 var total_price = 0;
421                 for ( var i=0; i < aaData.length; i++ ) {
422                     total_charge += aaData[i]['charge'] * 1;
423                     total_fine += aaData[i]['fine'] * 1;
424                     total_price  += aaData[i]['price'] * 1;
425                 }
426                 $("#totaldue").html(total_charge.toFixed(2));
427                 $("#totalfine").html(total_fine.toFixed(2));
428                 $("#totalprice").html(total_price.toFixed(2));
429             },
430             "bPaginate": false,
431             "bProcessing": true,
432             "bServerSide": false,
433             "sAjaxSource": '/cgi-bin/koha/svc/checkouts',
434             "fnServerData": function ( sSource, aoData, fnCallback ) {
435                 aoData.push( { "name": "borrowernumber", "value": borrowernumber } );
436
437                 $.getJSON( sSource, aoData, function (json) {
438                     fnCallback(json)
439                 } );
440             },
441             "fnInitComplete": function(oSettings) {
442                 // Disable rowGrouping plugin after first use
443                 // so any sorting on the table doesn't use it
444                 var oSettings = issuesTable.fnSettings();
445
446                 for (f = 0; f < oSettings.aoDrawCallback.length; f++) {
447                     if (oSettings.aoDrawCallback[f].sName == 'fnRowGrouping') {
448                         oSettings.aoDrawCallback.splice(f, 1);
449                         break;
450                     }
451                 }
452
453                 oSettings.aaSortingFixed = null;
454             },
455         }, columns_settings).rowGrouping(
456             {
457                 iGroupingColumnIndex: 1,
458                 iGroupingOrderByColumnIndex: 0,
459                 sGroupingColumnSortDirection: "asc"
460             }
461         );
462
463         if ( $("#issues-table").length ) {
464             $("#issues-table_processing").position({
465                 of: $( "#issues-table" ),
466                 collision: "none"
467             });
468         }
469     }
470
471     // Don't load relatives' issues table unless it is clicked on
472     var relativesIssuesTable;
473     $("#relatives-issues-tab").click( function() {
474         if ( ! relativesIssuesTable ) {
475             relativesIssuesTable = $("#relatives-issues-table").dataTable({
476                 "bAutoWidth": false,
477                 "sDom": "rt",
478                 "aaSorting": [],
479                 "aoColumns": [
480                     {
481                         "mDataProp": "date_due",
482                         "bVisible": false,
483                     },
484                     {
485                         "iDataSort": 1, // Sort on hidden unformatted date due column
486                         "mDataProp": function( oObj ) {
487                             var today = new Date();
488                             var due = new Date( oObj.date_due );
489                             if ( today > due ) {
490                                 return "<span class='overdue'>" + oObj.date_due_formatted + "</span>";
491                             } else {
492                                 return oObj.date_due_formatted;
493                             }
494                         }
495                     },
496                     {
497                         "mDataProp": function ( oObj ) {
498                             title = "<span class='strong'><a href='/cgi-bin/koha/catalogue/detail.pl?biblionumber="
499                                   + oObj.biblionumber
500                                   + "'>"
501                                   + oObj.title;
502
503                             $.each(oObj.subtitle, function( index, value ) {
504                                       title += " " + value.subfield;
505                             });
506
507                             if ( oObj.enumchron ) {
508                                 title += " (" + oObj.enumchron + ")";
509                             }
510
511                             title += "</a></span>";
512
513                             if ( oObj.author ) {
514                                 title += " " + BY.replace( "_AUTHOR_", " " + oObj.author );
515                             }
516
517                             if ( oObj.itemnotes ) {
518                                 var span_class = "";
519                                 if ( $.datepicker.formatDate('yy-mm-dd', new Date(oObj.issuedate) ) == ymd ) {
520                                     span_class = "circ-hlt";
521                                 }
522                                 title += " - <span class='" + span_class + "'>" + oObj.itemnotes + "</span>"
523                             }
524
525                             if ( oObj.itemnotes_nonpublic ) {
526                                 var span_class = "";
527                                 if ( $.datepicker.formatDate('yy-mm-dd', new Date(oObj.issuedate) ) == ymd ) {
528                                     span_class = "circ-hlt";
529                                 }
530                                 title += " - <span class='" + span_class + "'>" + oObj.itemnotes_nonpublic + "</span>"
531                             }
532
533                             var onsite_checkout = '';
534                             if ( oObj.onsite_checkout == 1 ) {
535                                 onsite_checkout += " <span class='onsite_checkout'>(" + INHOUSE_USE + ")</span>";
536                             }
537
538                             title += " "
539                                   + "<a href='/cgi-bin/koha/catalogue/moredetail.pl?biblionumber="
540                                   + oObj.biblionumber
541                                   + "&itemnumber="
542                                   + oObj.itemnumber
543                                   + "#"
544                                   + oObj.itemnumber
545                                   + "'>"
546                                   + oObj.barcode
547                                   + "</a>"
548                                   + onsite_checkout;
549
550                             return title;
551                         },
552                         "sType": "anti-the"
553                     },
554                     { "mDataProp": "itemtype" },
555                     { "mDataProp": "location" },
556                     { "mDataProp": "issuedate_formatted" },
557                     { "mDataProp": "branchname" },
558                     { "mDataProp": "itemcallnumber" },
559                     {
560                         "mDataProp": function ( oObj ) {
561                             if ( ! oObj.charge ) oObj.charge = 0;
562                             return parseFloat(oObj.charge).toFixed(2);
563                         }
564                     },
565                     {
566                         "mDataProp": function ( oObj ) {
567                             if ( ! oObj.fine ) oObj.fine = 0;
568                             return parseFloat(oObj.fine).toFixed(2);
569                         }
570                     },
571                     {
572                         "mDataProp": function ( oObj ) {
573                             if ( ! oObj.price ) oObj.price = 0;
574                             return parseFloat(oObj.price).toFixed(2);
575                         }
576                     },
577                     {
578                         "mDataProp": function( oObj ) {
579                             return "<a href='/cgi-bin/koha/members/moremember.pl?borrowernumber=" + oObj.borrowernumber + "'>"
580                                  + oObj.borrower.firstname + " " + oObj.borrower.surname + " (" + oObj.borrower.cardnumber + ")</a>"
581                         }
582                     },
583                 ],
584                 "bPaginate": false,
585                 "bProcessing": true,
586                 "bServerSide": false,
587                 "sAjaxSource": '/cgi-bin/koha/svc/checkouts',
588                 "fnServerData": function ( sSource, aoData, fnCallback ) {
589                     $.each(relatives_borrowernumbers, function( index, value ) {
590                         aoData.push( { "name": "borrowernumber", "value": value } );
591                     });
592
593                     $.getJSON( sSource, aoData, function (json) {
594                         fnCallback(json)
595                     } );
596                 },
597             });
598         }
599     });
600
601     if ( $("#relatives-issues-table").length ) {
602         $("#relatives-issues-table_processing").position({
603             of: $( "#relatives-issues-table" ),
604             collision: "none"
605         });
606     }
607
608     if ( AllowRenewalLimitOverride ) {
609         $( '#override_limit' ).click( function () {
610             if ( this.checked ) {
611                 $( '.renewals-allowed' ).show(); $( '.renewals-disabled' ).hide();
612             } else {
613                 $( '.renewals-allowed' ).hide(); $( '.renewals-disabled' ).show();
614             }
615         } ).prop('checked', false);
616     }
617  });