Bug 11703 [QA Followup]
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / js / checkouts.js
1 $(document).ready(function() {
2     // Handle the select all/none links for checkouts table columns
3     $("#CheckAllRenewals").on("click",function(){
4         $("#UncheckAllCheckins").click();
5         $(".renew:visible").attr("checked", "checked" );
6         return false;
7     });
8     $("#UncheckAllRenewals").on("click",function(){
9         $(".renew:visible").removeAttr("checked");
10         return false;
11     });
12
13     $("#CheckAllCheckins").on("click",function(){
14         $("#UncheckAllRenewals").click();
15         $(".checkin:visible").attr("checked", "checked" );
16         return false;
17     });
18     $("#UncheckAllCheckins").on("click",function(){
19         $(".checkin:visible").removeAttr("checked");
20         return false;
21     });
22
23     // Don't allow both return and renew checkboxes to be checked
24     $(document).on("change", '.renew', function(){
25         if ( $(this).is(":checked") ) {
26             $( "#checkin_" + $(this).val() ).removeAttr("checked");
27         }
28     });
29     $(document).on("change", '.checkin', function(){
30         if ( $(this).is(":checked") ) {
31             $( "#renew_" + $(this).val() ).removeAttr("checked");
32         }
33     });
34
35     // Clicking the table cell checks the checkbox inside it
36     $(document).on("click", 'td', function(e){
37         if(e.target.tagName.toLowerCase() == 'td'){
38           $(this).find("input:checkbox:visible").each( function() {
39             $(this).click();
40           });
41         }
42     });
43
44     // Handle renewals and returns
45     $("#RenewCheckinChecked").on("click",function(){
46         $(".checkin:checked:visible").each(function() {
47             itemnumber = $(this).val();
48
49             $(this).replaceWith("<img id='checkin_" + itemnumber + "' src='" + interface + "/" + theme + "/img/loading-small.gif' />");
50
51             params = {
52                 itemnumber:     itemnumber,
53                 borrowernumber: borrowernumber,
54                 branchcode:     branchcode,
55                 exempt_fine:    $("#exemptfine").is(':checked')
56             };
57
58             $.post( "/cgi-bin/koha/svc/checkin.pl", params, function( data ) {
59                 id = "#checkin_" + data.itemnumber;
60
61                 content = "";
62                 if ( data.returned ) {
63                     content = CIRCULATION_RETURNED;
64                 } else {
65                     content = CIRCULATION_NOT_RETURNED;
66                 }
67
68                 $(id).replaceWith( content );
69             }, "json")
70         });
71
72         $(".renew:checked:visible").each(function() {
73             var override_limit = $("#override_limit").is(':checked') ? 1 : 0;
74
75             var itemnumber = $(this).val();
76
77             $(this).parent().parent().replaceWith("<img id='renew_" + itemnumber + "' src='" + interface + "/" + theme + "/img/loading-small.gif' />");
78
79             var params = {
80                 itemnumber:     itemnumber,
81                 borrowernumber: borrowernumber,
82                 branchcode:     branchcode,
83                 override_limit: override_limit,
84                 date_due:       $("#newduedate").val()
85             };
86
87             $.post( "/cgi-bin/koha/svc/renew.pl", params, function( data ) {
88                 var id = "#renew_" + data.itemnumber;
89
90                 var content = "";
91                 if ( data.renew_okay ) {
92                     content = CIRCULATION_RENEWED_DUE + " " + data.date_due;
93                 } else {
94                     content = CIRCULATION_RENEW_FAILED + " ";
95                     if ( data.error == "no_checkout" ) {
96                         content += NOT_CHECKED_OUT;
97                     } else if ( data.error == "too_many" ) {
98                         content += TOO_MANY_RENEWALS;
99                     } else if ( data.error == "on_reserve" ) {
100                         content += ON_RESERVE;
101                     } else if ( data.error ) {
102                         content += data.error;
103                     } else {
104                         content += REASON_UNKNOWN;
105                     }
106                 }
107
108                 $(id).replaceWith( content );
109             }, "json")
110         });
111
112         // Prevent form submit
113         return false;
114     });
115
116     $("#RenewAll").on("click",function(){
117         $("#CheckAllRenewals").click();
118         $("#UncheckAllCheckins").click();
119         $("#RenewCheckinChecked").click();
120
121         // Prevent form submit
122         return false;
123     });
124
125     var ymd = $.datepicker.formatDate('yy-mm-dd', new Date());
126
127     var issuesTable;
128     var drawn = 0;
129     issuesTable = $("#issues-table").dataTable({
130         "bAutoWidth": false,
131         "sDom": "<'row-fluid'<'span6'><'span6'>r>t<'row-fluid'>t",
132         "aoColumns": [
133             {
134                 "mDataProp": function( oObj ) {
135                     if ( oObj.issued_today ) {
136                         return "<strong>" + TODAYS_CHECKOUTS + "</strong>";
137                     } else {
138                         return "<strong>" + PREVIOUS_CHECKOUTS + "</strong>";
139                     }
140                 }
141             },
142             {
143                 "mDataProp": "date_due",
144                 "bVisible": false,
145             },
146             {
147                 "iDataSort": 1, // Sort on hidden unformatted date due column
148                 "mDataProp": function( oObj ) {
149                     var today = new Date();
150                     var due = new Date( oObj.date_due );
151                     if ( today > due ) {
152                         return "<span class='overdue'>" + oObj.date_due_formatted + "</span>";
153                     } else {
154                         return oObj.date_due_formatted;
155                     }
156                 }
157             },
158             {
159                 "mDataProp": function ( oObj ) {
160                     title = "<span class='strong'><a href='/cgi-bin/koha/catalogue/detail.pl?biblionumber="
161                           + oObj.biblionumber
162                           + "'>"
163                           + oObj.title;
164
165                     $.each(oObj.subtitle, function( index, value ) {
166                               title += " " + value.subfield;
167                     });
168
169                     title += "</a></span>";
170
171                     if ( oObj.author ) {
172                         title += " " + BY.replace( "_AUTHOR_",  " " + oObj.author );
173                     }
174
175                     if ( oObj.itemnotes ) {
176                         var span_class = "";
177                         if ( $.datepicker.formatDate('yy-mm-dd', new Date(oObj.issuedate) ) == ymd ) {
178                             span_class = "circ-hlt";
179                         }
180                         title += " - <span class='" + span_class + "'>" + oObj.itemnotes + "</span>"
181                     }
182
183                     title += " "
184                           + "<a href='/cgi-bin/koha/catalogue/moredetail.pl?biblionumber="
185                           + oObj.biblionumber
186                           + "&itemnumber="
187                           + oObj.itemnumber
188                           + "#"
189                           + oObj.itemnumber
190                           + "'>"
191                           + oObj.barcode
192                           + "</a>";
193
194                     return title;
195                 }
196             },
197             { "mDataProp": "itemtype" },
198             { "mDataProp": "issuedate_formatted" },
199             { "mDataProp": "branchname" },
200             { "mDataProp": "itemcallnumber" },
201             {
202                 "mDataProp": function ( oObj ) {
203                     if ( ! oObj.charge ) oObj.charge = 0;
204                     return parseFloat(oObj.charge).toFixed(2);
205                 }
206             },
207             {
208                 "mDataProp": function ( oObj ) {
209                     if ( ! oObj.price ) oObj.price = 0;
210                     return parseFloat(oObj.price).toFixed(2);
211                 }
212             },
213             {
214                 "bSortable": false,
215                 "mDataProp": function ( oObj ) {
216                     var content = "";
217                     var span_style = "";
218                     var span_class = "";
219
220                     content += "<span>";
221                     content += "<span style='padding: 0 1em;'>" + oObj.renewals_count + "</span>";
222
223                     if ( oObj.can_renew ) {
224                         // Do nothing
225                     } else if ( oObj.can_renew_error == "on_reserve" ) {
226                         content += "<span class='renewals-disabled'>"
227                                 + "<a href='/cgi-bin/koha/reserve/request.pl?biblionumber=" + oObj.biblionumber + "'>" + ON_HOLD + "</a>"
228                                 + "</span>";
229
230                         span_style = "display: none";
231                         span_class = "renewals-allowed";
232                     } else if ( oObj.can_renew_error == "too_many" ) {
233                         content += "<span class='renewals-disabled'>"
234                                 + NOT_RENEWABLE
235                                 + "</span>";
236
237                         span_style = "display: none";
238                         span_class = "renewals-allowed";
239                     } else if ( oObj.can_renew_error == "too_soon" ) {
240                         content += "<span class='renewals-disabled'>"
241                                 + NOT_RENEWABLE_TOO_SOON
242                                 + "</span>";
243
244                         span_style = "display: none";
245                         span_class = "renewals-allowed";
246                     } else {
247                         content += "<span class='renewals-disabled'>"
248                                 + oObj.can_renew_error
249                                 + "</span>";
250
251                         span_style = "display: none";
252                         span_class = "renewals-allowed";
253                     }
254
255                     content += "<span class='" + span_class + "' style='" + span_style + "'>"
256                             +  "<input type='checkbox' class='renew' id='renew_" + oObj.itemnumber + "' name='renew' value='" + oObj.itemnumber +"'/>"
257                             +  "</span>";
258
259                     if ( oObj.renewals_remaining ) {
260                         content += "<span class='renewals'>("
261                                 + RENEWALS_REMAINING.replace( "_RENEWALS_REMAINING_", oObj.renewals_remaining ).replace( "_RENEWALS_ALLOWED_", oObj.renewals_allowed )
262                                 + ")</span>";
263                     }
264
265                     content += "</span>";
266
267
268                     return content;
269                 }
270             },
271             {
272                 "bSortable": false,
273                 "mDataProp": function ( oObj ) {
274                     if ( oObj.can_renew_error == "on_reserve" ) {
275                         return "<a href='/cgi-bin/koha/reserve/request.pl?biblionumber=" + oObj.biblionumber + "'>" + ON_HOLD + "</a>";
276                     } else {
277                         return "<input type='checkbox' class='checkin' id='checkin_" + oObj.itemnumber + "' name='checkin' value='" + oObj.itemnumber +"'></input>";
278                     }
279                 }
280             },
281             {
282                 "bVisible": exports_enabled ? true : false,
283                 "bSortable": false,
284                 "mDataProp": function ( oObj ) {
285                     return "<input type='checkbox' class='export' id='export_" + oObj.biblionumber + "' name='biblionumbers' value='" + oObj.biblionumber + "' />";
286                 }
287             }
288         ],
289         "fnFooterCallback": function ( nRow, aaData, iStart, iEnd, aiDisplay ) {
290             var total_charge = 0;
291             var total_price = 0;
292             for ( var i=0; i < aaData.length; i++ ) {
293                 total_charge += aaData[i]['charge'] * 1;
294                 total_price  += aaData[i]['price'] * 1;
295             }
296             var nCells = nRow.getElementsByTagName('td');
297             nCells[1].innerHTML = total_charge.toFixed(2);
298             nCells[2].innerHTML = total_price.toFixed(2);
299         },
300         "bPaginate": false,
301         "bProcessing": true,
302         "bServerSide": false,
303         "sAjaxSource": '/cgi-bin/koha/svc/checkouts.pl',
304         "fnServerData": function ( sSource, aoData, fnCallback ) {
305             aoData.push( { "name": "borrowernumber", "value": borrowernumber } );
306
307             $.getJSON( sSource, aoData, function (json) {
308                 fnCallback(json)
309             } );
310         },
311         "fnInitComplete": function(oSettings) {
312             // Disable rowGrouping plugin after first use
313             // so any sorting on the table doesn't use it
314             var oSettings = issuesTable.fnSettings();
315
316             for (f = 0; f < oSettings.aoDrawCallback.length; f++) {
317                 if (oSettings.aoDrawCallback[f].sName == 'fnRowGrouping') {
318                     oSettings.aoDrawCallback.splice(f, 1);
319                     break;
320                 }
321             }
322
323             oSettings.aaSortingFixed = null;
324         },
325     }).rowGrouping(
326         {
327             iGroupingOrderByColumnIndex: 0,
328             sGroupingColumnSortDirection: "desc"
329         }
330     );
331
332     if ( $("#issues-table").length ) {
333         $("#issues-table_processing").position({
334             of: $( "#issues-table" ),
335             collision: "none"
336         });
337     }
338
339     // Don't load relatives' issues table unless it is clicked on
340     var relativesIssuesTable;
341     $("#relatives-issues-tab").click( function() {
342         if ( ! relativesIssuesTable ) {
343             relativesIssuesTable = $("#relatives-issues-table").dataTable({
344                 "bAutoWidth": false,
345                 "sDom": "<'row-fluid'<'span6'><'span6'>r>t<'row-fluid'>t",
346                 "aaSorting": [],
347                 "aoColumns": [
348                     {
349                         "mDataProp": "date_due",
350                         "bVisible": false,
351                     },
352                     {
353                         "iDataSort": 1, // Sort on hidden unformatted date due column
354                         "mDataProp": function( oObj ) {
355                             var today = new Date();
356                             var due = new Date( oObj.date_due );
357                             if ( today > due ) {
358                                 return "<span class='overdue'>" + oObj.date_due_formatted + "</span>";
359                             } else {
360                                 return oObj.date_due_formatted;
361                             }
362                         }
363                     },
364                     {
365                         "mDataProp": function ( oObj ) {
366                             title = "<span class='strong'><a href='/cgi-bin/koha/catalogue/detail.pl?biblionumber="
367                                   + oObj.biblionumber
368                                   + "'>"
369                                   + oObj.title;
370
371                             $.each(oObj.subtitle, function( index, value ) {
372                                       title += " " + value.subfield;
373                             });
374
375                             title += "</a></span>";
376
377                             if ( oObj.author ) {
378                                 title += " " + BY + " " + oObj.author;
379                             }
380
381                             if ( oObj.itemnotes ) {
382                                 var span_class = "";
383                                 if ( $.datepicker.formatDate('yy-mm-dd', new Date(oObj.issuedate) ) == ymd ) {
384                                     span_class = "circ-hlt";
385                                 }
386                                 title += " - <span class='" + span_class + "'>" + oObj.itemnotes + "</span>"
387                             }
388
389                             title += " "
390                                   + "<a href='/cgi-bin/koha/catalogue/moredetail.pl?biblionumber="
391                                   + oObj.biblionumber
392                                   + "&itemnumber="
393                                   + oObj.itemnumber
394                                   + "#"
395                                   + oObj.itemnumber
396                                   + "'>"
397                                   + oObj.barcode
398                                   + "</a>";
399
400                             return title;
401                         }
402                     },
403                     { "mDataProp": "itemtype" },
404                     { "mDataProp": "issuedate_formatted" },
405                     { "mDataProp": "branchname" },
406                     { "mDataProp": "itemcallnumber" },
407                     {
408                         "mDataProp": function ( oObj ) {
409                             if ( ! oObj.charge ) oObj.charge = 0;
410                             return parseFloat(oObj.charge).toFixed(2);
411                         }
412                     },
413                     {
414                         "mDataProp": function ( oObj ) {
415                             if ( ! oObj.price ) oObj.price = 0;
416                             return parseFloat(oObj.price).toFixed(2);
417                         }
418                     },
419                     {
420                         "mDataProp": function( oObj ) {
421                             return "<a href='/cgi-bin/koha/members/moremember.pl?borrowernumber=" + oObj.borrowernumber + "'>"
422                                  + oObj.borrower.firstname + " " + oObj.borrower.surname + " (" + oObj.borrower.cardnumber + ")</a>"
423                         }
424                     },
425                 ],
426                 "bPaginate": false,
427                 "bProcessing": true,
428                 "bServerSide": false,
429                 "sAjaxSource": '/cgi-bin/koha/svc/checkouts.pl',
430                 "fnServerData": function ( sSource, aoData, fnCallback ) {
431                     $.each(relatives_borrowernumbers, function( index, value ) {
432                         aoData.push( { "name": "borrowernumber", "value": value } );
433                     });
434
435                     $.getJSON( sSource, aoData, function (json) {
436                         fnCallback(json)
437                     } );
438                 },
439             });
440         }
441     });
442
443     if ( $("#relatives-issues-table").length ) {
444         $("#relatives-issues-table_processing").position({
445             of: $( "#relatives-issues-table" ),
446             collision: "none"
447         });
448     }
449
450     if ( AllowRenewalLimitOverride ) {
451         $( '#override_limit' ).click( function () {
452             if ( this.checked ) {
453                 $( '.renewals-allowed' ).show(); $( '.renewals-disabled' ).hide();
454             } else {
455                 $( '.renewals-allowed' ).hide(); $( '.renewals-disabled' ).show();
456             }
457         } ).attr( 'checked', false );
458     }
459  });