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