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