Bug 14668: Show serial enumeration in INTRANET circulation.tt and OPAC patron's relat...
[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": "issuedate_formatted" },
280                 { "mDataProp": "branchname" },
281                 { "mDataProp": "itemcallnumber" },
282                 {
283                     "mDataProp": function ( oObj ) {
284                         if ( ! oObj.charge ) oObj.charge = 0;
285                         return '<span style="text-align: right; display: block;">' + parseFloat(oObj.charge).toFixed(2) + '<span>';
286                     }
287                 },
288                 {
289                     "mDataProp": function ( oObj ) {
290                         if ( ! oObj.fine ) oObj.fine = 0;
291                         return '<span style="text-align: right; display: block;">' + parseFloat(oObj.fine).toFixed(2)  + '<span>';
292                     }
293                 },
294                 {
295                     "mDataProp": function ( oObj ) {
296                         if ( ! oObj.price ) oObj.price = 0;
297                         return '<span style="text-align: right; display: block;">' + parseFloat(oObj.price).toFixed(2) + '<span>';
298                     }
299                 },
300                 {
301                     "bSortable": false,
302                     "bVisible": AllowCirculate ? true : false,
303                     "mDataProp": function ( oObj ) {
304                         var content = "";
305                         var span_style = "";
306                         var span_class = "";
307
308                         content += "<span>";
309                         content += "<span style='padding: 0 1em;'>" + oObj.renewals_count + "</span>";
310
311                         if ( oObj.can_renew ) {
312                             // Do nothing
313                         } else if ( oObj.can_renew_error == "on_reserve" ) {
314                             content += "<span class='renewals-disabled-no-override'>"
315                                     + "<a href='/cgi-bin/koha/reserve/request.pl?biblionumber=" + oObj.biblionumber + "'>" + ON_HOLD + "</a>"
316                                     + "</span>";
317
318                             span_style = "display: none";
319                             span_class = "renewals-allowed";
320                         } else if ( oObj.can_renew_error == "too_many" ) {
321                             content += "<span class='renewals-disabled'>"
322                                     + NOT_RENEWABLE
323                                     + "</span>";
324
325                             span_style = "display: none";
326                             span_class = "renewals-allowed";
327                         } else if ( oObj.can_renew_error == "restriction" ) {
328                             content += "<span class='renewals-disabled'>"
329                                     + NOT_RENEWABLE_RESTRICTION
330                                     + "</span>";
331
332                             span_style = "display: none";
333                             span_class = "renewals-allowed";
334                         } else if ( oObj.can_renew_error == "overdue" ) {
335                             content += "<span class='renewals-disabled'>"
336                                     + NOT_RENEWABLE_OVERDUE
337                                     + "</span>";
338
339                             span_style = "display: none";
340                             span_class = "renewals-allowed";
341                         } else if ( oObj.can_renew_error == "too_soon" ) {
342                             content += "<span class='renewals-disabled'>"
343                                     + NOT_RENEWABLE_TOO_SOON.format( oObj.can_renew_date )
344                                     + "</span>";
345
346                             span_style = "display: none";
347                             span_class = "renewals-allowed";
348                         } else if ( oObj.can_renew_error == "auto_too_soon" ) {
349                             content += "<span class='renewals-disabled'>"
350                                     + NOT_RENEWABLE_AUTO_TOO_SOON
351                                     + "</span>";
352
353                             span_style = "display: none";
354                             span_class = "renewals-allowed";
355                         } else if ( oObj.can_renew_error == "auto_renew" ) {
356                             content += "<span class='renewals-disabled'>"
357                                     + NOT_RENEWABLE_AUTO_RENEW
358                                     + "</span>";
359
360                             span_style = "display: none";
361                             span_class = "renewals-allowed";
362                         } else if ( oObj.can_renew_error == "onsite_checkout" ) {
363                             // Don't display something if it's an onsite checkout
364                         } else {
365                             content += "<span class='renewals-disabled'>"
366                                     + oObj.can_renew_error
367                                     + "</span>";
368
369                             span_style = "display: none";
370                             span_class = "renewals-allowed";
371                         }
372
373                         var can_force_renew = ( oObj.onsite_checkout == 0 ) && ( oObj.can_renew_error != "on_reserve" );
374                         var can_renew = ( oObj.renewals_remaining > 0  && !oObj.can_renew_error );
375                         if ( can_renew || can_force_renew ) {
376                             content += "<span class='" + span_class + "' style='" + span_style + "'>"
377                                     +  "<input type='checkbox' ";
378                             if ( oObj.date_due_overdue && can_renew ) {
379                                 content += "checked='checked' ";
380                             }
381                             content += "class='renew' id='renew_" + oObj.itemnumber + "' name='renew' value='" + oObj.itemnumber +"'/>"
382                                     +  "</span>";
383
384                             content += "<span class='renewals'>("
385                                     + RENEWALS_REMAINING.format( oObj.renewals_remaining, oObj.renewals_allowed )
386                                     + ")</span>";
387                         }
388
389                         content += "</span>";
390
391                         return content;
392                     }
393                 },
394                 {
395                     "bSortable": false,
396                     "bVisible": AllowCirculate ? true : false,
397                     "mDataProp": function ( oObj ) {
398                         if ( oObj.can_renew_error == "on_reserve" ) {
399                             return "<a href='/cgi-bin/koha/reserve/request.pl?biblionumber=" + oObj.biblionumber + "'>" + ON_HOLD + "</a>";
400                         } else {
401                             return "<input type='checkbox' class='checkin' id='checkin_" + oObj.itemnumber + "' name='checkin' value='" + oObj.itemnumber +"'></input>";
402                         }
403                     }
404                 },
405                 {
406                     "bVisible": exports_enabled ? true : false,
407                     "bSortable": false,
408                     "mDataProp": function ( oObj ) {
409                         var s = "<input type='checkbox' name='itemnumbers' value='" + oObj.itemnumber + "' style='visibility:hidden;' />";
410
411                         s += "<input type='checkbox' class='export' id='export_" + oObj.biblionumber + "' name='biblionumbers' value='" + oObj.biblionumber + "' />";
412                         return s;
413                     }
414                 }
415             ],
416             "fnFooterCallback": function ( nRow, aaData, iStart, iEnd, aiDisplay ) {
417                 var total_charge = 0;
418                 var total_fine  = 0;
419                 var total_price = 0;
420                 for ( var i=0; i < aaData.length; i++ ) {
421                     total_charge += aaData[i]['charge'] * 1;
422                     total_fine += aaData[i]['fine'] * 1;
423                     total_price  += aaData[i]['price'] * 1;
424                 }
425                 $("#totaldue").html(total_charge.toFixed(2));
426                 $("#totalfine").html(total_fine.toFixed(2));
427                 $("#totalprice").html(total_price.toFixed(2));
428             },
429             "bPaginate": false,
430             "bProcessing": true,
431             "bServerSide": false,
432             "sAjaxSource": '/cgi-bin/koha/svc/checkouts',
433             "fnServerData": function ( sSource, aoData, fnCallback ) {
434                 aoData.push( { "name": "borrowernumber", "value": borrowernumber } );
435
436                 $.getJSON( sSource, aoData, function (json) {
437                     fnCallback(json)
438                 } );
439             },
440             "fnInitComplete": function(oSettings) {
441                 // Disable rowGrouping plugin after first use
442                 // so any sorting on the table doesn't use it
443                 var oSettings = issuesTable.fnSettings();
444
445                 for (f = 0; f < oSettings.aoDrawCallback.length; f++) {
446                     if (oSettings.aoDrawCallback[f].sName == 'fnRowGrouping') {
447                         oSettings.aoDrawCallback.splice(f, 1);
448                         break;
449                     }
450                 }
451
452                 oSettings.aaSortingFixed = null;
453             },
454         }, columns_settings).rowGrouping(
455             {
456                 iGroupingColumnIndex: 1,
457                 iGroupingOrderByColumnIndex: 0,
458                 sGroupingColumnSortDirection: "asc"
459             }
460         );
461
462         if ( $("#issues-table").length ) {
463             $("#issues-table_processing").position({
464                 of: $( "#issues-table" ),
465                 collision: "none"
466             });
467         }
468     }
469
470     // Don't load relatives' issues table unless it is clicked on
471     var relativesIssuesTable;
472     $("#relatives-issues-tab").click( function() {
473         if ( ! relativesIssuesTable ) {
474             relativesIssuesTable = $("#relatives-issues-table").dataTable({
475                 "bAutoWidth": false,
476                 "sDom": "rt",
477                 "aaSorting": [],
478                 "aoColumns": [
479                     {
480                         "mDataProp": "date_due",
481                         "bVisible": false,
482                     },
483                     {
484                         "iDataSort": 1, // Sort on hidden unformatted date due column
485                         "mDataProp": function( oObj ) {
486                             var today = new Date();
487                             var due = new Date( oObj.date_due );
488                             if ( today > due ) {
489                                 return "<span class='overdue'>" + oObj.date_due_formatted + "</span>";
490                             } else {
491                                 return oObj.date_due_formatted;
492                             }
493                         }
494                     },
495                     {
496                         "mDataProp": function ( oObj ) {
497                             title = "<span class='strong'><a href='/cgi-bin/koha/catalogue/detail.pl?biblionumber="
498                                   + oObj.biblionumber
499                                   + "'>"
500                                   + oObj.title;
501
502                             $.each(oObj.subtitle, function( index, value ) {
503                                       title += " " + value.subfield;
504                             });
505
506                             if ( oObj.enumchron ) {
507                                 title += ' ' + oObj.enumchron;
508                             }
509
510                             title += "</a></span>";
511
512                             if ( oObj.author ) {
513                                 title += " " + BY.replace( "_AUTHOR_", " " + oObj.author );
514                             }
515
516                             if ( oObj.itemnotes ) {
517                                 var span_class = "";
518                                 if ( $.datepicker.formatDate('yy-mm-dd', new Date(oObj.issuedate) ) == ymd ) {
519                                     span_class = "circ-hlt";
520                                 }
521                                 title += " - <span class='" + span_class + "'>" + oObj.itemnotes + "</span>"
522                             }
523
524                             if ( oObj.itemnotes_nonpublic ) {
525                                 var span_class = "";
526                                 if ( $.datepicker.formatDate('yy-mm-dd', new Date(oObj.issuedate) ) == ymd ) {
527                                     span_class = "circ-hlt";
528                                 }
529                                 title += " - <span class='" + span_class + "'>" + oObj.itemnotes_nonpublic + "</span>"
530                             }
531
532                             var onsite_checkout = '';
533                             if ( oObj.onsite_checkout == 1 ) {
534                                 onsite_checkout += " <span class='onsite_checkout'>(" + INHOUSE_USE + ")</span>";
535                             }
536
537                             title += " "
538                                   + "<a href='/cgi-bin/koha/catalogue/moredetail.pl?biblionumber="
539                                   + oObj.biblionumber
540                                   + "&itemnumber="
541                                   + oObj.itemnumber
542                                   + "#"
543                                   + oObj.itemnumber
544                                   + "'>"
545                                   + oObj.barcode
546                                   + "</a>"
547                                   + onsite_checkout;
548
549                             return title;
550                         },
551                         "sType": "anti-the"
552                     },
553                     { "mDataProp": "itemtype" },
554                     { "mDataProp": "location" },
555                     { "mDataProp": "issuedate_formatted" },
556                     { "mDataProp": "branchname" },
557                     { "mDataProp": "itemcallnumber" },
558                     {
559                         "mDataProp": function ( oObj ) {
560                             if ( ! oObj.charge ) oObj.charge = 0;
561                             return parseFloat(oObj.charge).toFixed(2);
562                         }
563                     },
564                     {
565                         "mDataProp": function ( oObj ) {
566                             if ( ! oObj.fine ) oObj.fine = 0;
567                             return parseFloat(oObj.fine).toFixed(2);
568                         }
569                     },
570                     {
571                         "mDataProp": function ( oObj ) {
572                             if ( ! oObj.price ) oObj.price = 0;
573                             return parseFloat(oObj.price).toFixed(2);
574                         }
575                     },
576                     {
577                         "mDataProp": function( oObj ) {
578                             return "<a href='/cgi-bin/koha/members/moremember.pl?borrowernumber=" + oObj.borrowernumber + "'>"
579                                  + oObj.borrower.firstname + " " + oObj.borrower.surname + " (" + oObj.borrower.cardnumber + ")</a>"
580                         }
581                     },
582                 ],
583                 "bPaginate": false,
584                 "bProcessing": true,
585                 "bServerSide": false,
586                 "sAjaxSource": '/cgi-bin/koha/svc/checkouts',
587                 "fnServerData": function ( sSource, aoData, fnCallback ) {
588                     $.each(relatives_borrowernumbers, function( index, value ) {
589                         aoData.push( { "name": "borrowernumber", "value": value } );
590                     });
591
592                     $.getJSON( sSource, aoData, function (json) {
593                         fnCallback(json)
594                     } );
595                 },
596             });
597         }
598     });
599
600     if ( $("#relatives-issues-table").length ) {
601         $("#relatives-issues-table_processing").position({
602             of: $( "#relatives-issues-table" ),
603             collision: "none"
604         });
605     }
606
607     if ( AllowRenewalLimitOverride ) {
608         $( '#override_limit' ).click( function () {
609             if ( this.checked ) {
610                 $( '.renewals-allowed' ).show(); $( '.renewals-disabled' ).hide();
611             } else {
612                 $( '.renewals-allowed' ).hide(); $( '.renewals-disabled' ).show();
613             }
614         } ).prop('checked', false);
615     }
616  });