Bug 17344 - Can't set guarantor in quick add brief form
[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_too_late" ) {
357                             content += "<span class='renewals-disabled'>"
358                                     + NOT_RENEWABLE_AUTO_TOO_LATE
359                                     + "</span>";
360
361                             span_style = "display: none";
362                             span_class = "renewals-allowed";
363                         } else if ( oObj.can_renew_error == "auto_renew" ) {
364                             content += "<span class='renewals-disabled'>"
365                                     + NOT_RENEWABLE_AUTO_RENEW
366                                     + "</span>";
367
368                             span_style = "display: none";
369                             span_class = "renewals-allowed";
370                         } else if ( oObj.can_renew_error == "onsite_checkout" ) {
371                             // Don't display something if it's an onsite checkout
372                         } else {
373                             content += "<span class='renewals-disabled'>"
374                                     + oObj.can_renew_error
375                                     + "</span>";
376
377                             span_style = "display: none";
378                             span_class = "renewals-allowed";
379                         }
380
381                         var can_force_renew = ( oObj.onsite_checkout == 0 ) && ( oObj.can_renew_error != "on_reserve" );
382                         var can_renew = ( oObj.renewals_remaining > 0  && !oObj.can_renew_error );
383                         if ( can_renew || can_force_renew ) {
384                             content += "<span class='" + span_class + "' style='" + span_style + "'>"
385                                     +  "<input type='checkbox' ";
386                             if ( oObj.date_due_overdue && can_renew ) {
387                                 content += "checked='checked' ";
388                             }
389                             content += "class='renew' id='renew_" + oObj.itemnumber + "' name='renew' value='" + oObj.itemnumber +"'/>"
390                                     +  "</span>";
391
392                             content += "<span class='renewals'>("
393                                     + RENEWALS_REMAINING.format( oObj.renewals_remaining, oObj.renewals_allowed )
394                                     + ")</span>";
395                         }
396
397                         content += "</span>";
398
399                         return content;
400                     }
401                 },
402                 {
403                     "bSortable": false,
404                     "bVisible": AllowCirculate ? true : false,
405                     "mDataProp": function ( oObj ) {
406                         if ( oObj.can_renew_error == "on_reserve" ) {
407                             return "<a href='/cgi-bin/koha/reserve/request.pl?biblionumber=" + oObj.biblionumber + "'>" + ON_HOLD + "</a>";
408                         } else {
409                             return "<input type='checkbox' class='checkin' id='checkin_" + oObj.itemnumber + "' name='checkin' value='" + oObj.itemnumber +"'></input>";
410                         }
411                     }
412                 },
413                 {
414                     "bVisible": exports_enabled ? true : false,
415                     "bSortable": false,
416                     "mDataProp": function ( oObj ) {
417                         var s = "<input type='checkbox' name='itemnumbers' value='" + oObj.itemnumber + "' style='visibility:hidden;' />";
418
419                         s += "<input type='checkbox' class='export' id='export_" + oObj.biblionumber + "' name='biblionumbers' value='" + oObj.biblionumber + "' />";
420                         return s;
421                     }
422                 }
423             ],
424             "fnFooterCallback": function ( nRow, aaData, iStart, iEnd, aiDisplay ) {
425                 var total_charge = 0;
426                 var total_fine  = 0;
427                 var total_price = 0;
428                 for ( var i=0; i < aaData.length; i++ ) {
429                     total_charge += aaData[i]['charge'] * 1;
430                     total_fine += aaData[i]['fine'] * 1;
431                     total_price  += aaData[i]['price'] * 1;
432                 }
433                 $("#totaldue").html(total_charge.toFixed(2));
434                 $("#totalfine").html(total_fine.toFixed(2));
435                 $("#totalprice").html(total_price.toFixed(2));
436             },
437             "bPaginate": false,
438             "bProcessing": true,
439             "bServerSide": false,
440             "sAjaxSource": '/cgi-bin/koha/svc/checkouts',
441             "fnServerData": function ( sSource, aoData, fnCallback ) {
442                 aoData.push( { "name": "borrowernumber", "value": borrowernumber } );
443
444                 $.getJSON( sSource, aoData, function (json) {
445                     fnCallback(json)
446                 } );
447             },
448             "fnInitComplete": function(oSettings) {
449                 // Disable rowGrouping plugin after first use
450                 // so any sorting on the table doesn't use it
451                 var oSettings = issuesTable.fnSettings();
452
453                 for (f = 0; f < oSettings.aoDrawCallback.length; f++) {
454                     if (oSettings.aoDrawCallback[f].sName == 'fnRowGrouping') {
455                         oSettings.aoDrawCallback.splice(f, 1);
456                         break;
457                     }
458                 }
459
460                 oSettings.aaSortingFixed = null;
461             },
462         }, columns_settings).rowGrouping(
463             {
464                 iGroupingColumnIndex: 1,
465                 iGroupingOrderByColumnIndex: 0,
466                 sGroupingColumnSortDirection: "asc"
467             }
468         );
469
470         if ( $("#issues-table").length ) {
471             $("#issues-table_processing").position({
472                 of: $( "#issues-table" ),
473                 collision: "none"
474             });
475         }
476     }
477
478     // Don't load relatives' issues table unless it is clicked on
479     var relativesIssuesTable;
480     $("#relatives-issues-tab").click( function() {
481         if ( ! relativesIssuesTable ) {
482             relativesIssuesTable = $("#relatives-issues-table").dataTable({
483                 "bAutoWidth": false,
484                 "sDom": "rt",
485                 "aaSorting": [],
486                 "aoColumns": [
487                     {
488                         "mDataProp": "date_due",
489                         "bVisible": false,
490                     },
491                     {
492                         "iDataSort": 1, // Sort on hidden unformatted date due column
493                         "mDataProp": function( oObj ) {
494                             var today = new Date();
495                             var due = new Date( oObj.date_due );
496                             if ( today > due ) {
497                                 return "<span class='overdue'>" + oObj.date_due_formatted + "</span>";
498                             } else {
499                                 return oObj.date_due_formatted;
500                             }
501                         }
502                     },
503                     {
504                         "mDataProp": function ( oObj ) {
505                             title = "<span class='strong'><a href='/cgi-bin/koha/catalogue/detail.pl?biblionumber="
506                                   + oObj.biblionumber
507                                   + "'>"
508                                   + oObj.title;
509
510                             $.each(oObj.subtitle, function( index, value ) {
511                                       title += " " + value.subfield;
512                             });
513
514                             if ( oObj.enumchron ) {
515                                 title += " (" + oObj.enumchron + ")";
516                             }
517
518                             title += "</a></span>";
519
520                             if ( oObj.author ) {
521                                 title += " " + BY.replace( "_AUTHOR_", " " + oObj.author );
522                             }
523
524                             if ( oObj.itemnotes ) {
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 + "</span>"
530                             }
531
532                             if ( oObj.itemnotes_nonpublic ) {
533                                 var span_class = "";
534                                 if ( $.datepicker.formatDate('yy-mm-dd', new Date(oObj.issuedate) ) == ymd ) {
535                                     span_class = "circ-hlt";
536                                 }
537                                 title += " - <span class='" + span_class + "'>" + oObj.itemnotes_nonpublic + "</span>"
538                             }
539
540                             var onsite_checkout = '';
541                             if ( oObj.onsite_checkout == 1 ) {
542                                 onsite_checkout += " <span class='onsite_checkout'>(" + INHOUSE_USE + ")</span>";
543                             }
544
545                             title += " "
546                                   + "<a href='/cgi-bin/koha/catalogue/moredetail.pl?biblionumber="
547                                   + oObj.biblionumber
548                                   + "&itemnumber="
549                                   + oObj.itemnumber
550                                   + "#"
551                                   + oObj.itemnumber
552                                   + "'>"
553                                   + oObj.barcode
554                                   + "</a>"
555                                   + onsite_checkout;
556
557                             return title;
558                         },
559                         "sType": "anti-the"
560                     },
561                     { "mDataProp": "itemtype" },
562                     { "mDataProp": "location" },
563                     { "mDataProp": "issuedate_formatted" },
564                     { "mDataProp": "branchname" },
565                     { "mDataProp": "itemcallnumber" },
566                     {
567                         "mDataProp": function ( oObj ) {
568                             if ( ! oObj.charge ) oObj.charge = 0;
569                             return parseFloat(oObj.charge).toFixed(2);
570                         }
571                     },
572                     {
573                         "mDataProp": function ( oObj ) {
574                             if ( ! oObj.fine ) oObj.fine = 0;
575                             return parseFloat(oObj.fine).toFixed(2);
576                         }
577                     },
578                     {
579                         "mDataProp": function ( oObj ) {
580                             if ( ! oObj.price ) oObj.price = 0;
581                             return parseFloat(oObj.price).toFixed(2);
582                         }
583                     },
584                     {
585                         "mDataProp": function( oObj ) {
586                             return "<a href='/cgi-bin/koha/members/moremember.pl?borrowernumber=" + oObj.borrowernumber + "'>"
587                                  + oObj.borrower.firstname + " " + oObj.borrower.surname + " (" + oObj.borrower.cardnumber + ")</a>"
588                         }
589                     },
590                 ],
591                 "bPaginate": false,
592                 "bProcessing": true,
593                 "bServerSide": false,
594                 "sAjaxSource": '/cgi-bin/koha/svc/checkouts',
595                 "fnServerData": function ( sSource, aoData, fnCallback ) {
596                     $.each(relatives_borrowernumbers, function( index, value ) {
597                         aoData.push( { "name": "borrowernumber", "value": value } );
598                     });
599
600                     $.getJSON( sSource, aoData, function (json) {
601                         fnCallback(json)
602                     } );
603                 },
604             });
605         }
606     });
607
608     if ( $("#relatives-issues-table").length ) {
609         $("#relatives-issues-table_processing").position({
610             of: $( "#relatives-issues-table" ),
611             collision: "none"
612         });
613     }
614
615     if ( AllowRenewalLimitOverride ) {
616         $( '#override_limit' ).click( function () {
617             if ( this.checked ) {
618                 $( '.renewals-allowed' ).show(); $( '.renewals-disabled' ).hide();
619             } else {
620                 $( '.renewals-allowed' ).hide(); $( '.renewals-disabled' ).show();
621             }
622         } ).prop('checked', false);
623     }
624  });