Bug 16154: Fix some other occurrences
[koha.git] / circ / overdue.pl
index 1e1bd6f..8e06013 100755 (executable)
@@ -6,28 +6,26 @@
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
-use strict;
-use warnings;
+use Modern::Perl;
 use C4::Context;
 use C4::Output;
-use CGI qw(-oldstyle_urls);
+use CGI qw(-oldstyle_urls -utf8);
 use C4::Auth;
 use C4::Branch;
 use C4::Debug;
-use C4::Dates qw/format_date format_date_in_iso/;
 use Text::CSV_XS;
 use Koha::DateUtils;
 use DateTime;
@@ -43,22 +41,21 @@ my $branchfilter    = $input->param('branch') || '';
 my $homebranchfilter    = $input->param('homebranch') || '';
 my $holdingbranchfilter = $input->param('holdingbranch') || '';
 my $op              = $input->param('op') || '';
-my $dateduefrom = format_date_in_iso($input->param( 'dateduefrom' )) || '';
-my $datedueto   = format_date_in_iso($input->param( 'datedueto' )) || '';
-# FIXME This is a kludge to include times
-if ($datedueto) {
-    $datedueto .= ' 23:59';
+
+my ($dateduefrom, $datedueto);
+if ( $dateduefrom = $input->param('dateduefrom') ) {
+    $dateduefrom = dt_from_string( $dateduefrom );
 }
-if ($dateduefrom) {
-    $dateduefrom .= ' 00:00';
+if ( $datedueto = $input->param('datedueto') ) {
+    $datedueto = dt_from_string( $datedueto )->set_hour(23)->set_minute(59);
 }
-# kludge end
+
 my $isfiltered      = $op =~ /apply/i && $op =~ /filter/i;
 my $noreport        = C4::Context->preference('FilterBeforeOverdueReport') && ! $isfiltered && $op ne "csv";
 
 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
     {
-        template_name   => "circ/overdue.tmpl",
+        template_name   => "circ/overdue.tt",
         query           => $input,
         type            => "intranet",
         authnotrequired => 0,
@@ -91,10 +88,11 @@ while (my ($itemtype, $description) =$req->fetchrow) {
         itemtypename => $description,
     };
 }
-my $onlymine=C4::Context->preference('IndependantBranches') && 
-             C4::Context->userenv &&
-             C4::Context->userenv->{flags} % 2 !=1 &&
-             C4::Context->userenv->{branch};
+my $onlymine =
+     C4::Context->preference('IndependentBranches')
+  && C4::Context->userenv
+  && !C4::Context->IsSuperLibrarian()
+  && C4::Context->userenv->{branch};
 
 $branchfilter = C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter);
 
@@ -104,8 +102,8 @@ $branchfilter = C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter
 #  %borrowernumber_to_attributes   is populated by those borrowernumbers matching the patron attribute filters
 
 my %cgi_attrcode_to_attrvalues;     # ( patron_attribute_code => [ zero or more attribute filter values from the CGI ] )
-for my $attrcode (grep { /^patron_attr_filter_/ } $input->param) {
-    if (my @attrvalues = grep { length($_) > 0 } $input->param($attrcode)) {
+for my $attrcode (grep { /^patron_attr_filter_/ } $input->multi_param) {
+    if (my @attrvalues = grep { length($_) > 0 } $input->multi_param($attrcode)) {
         $attrcode =~ s/^patron_attr_filter_//;
         $cgi_attrcode_to_attrvalues{$attrcode} = \@attrvalues;
         print STDERR ">>>param($attrcode)[@{[scalar @attrvalues]}] = '@attrvalues'\n" if $debug;
@@ -227,8 +225,8 @@ $template->param(
     borname => $bornamefilter,
     order => $order,
     showall => $showall,
-    dateduefrom => $input->param( 'dateduefrom' ) || '',
-    datedueto   => $input->param( 'datedueto' ) || '',
+    dateduefrom => $dateduefrom,
+    datedueto   => $datedueto,
 );
 
 if ($noreport) {
@@ -263,6 +261,7 @@ if ($noreport) {
         borrowers.country,
         borrowers.phone,
         borrowers.email,
+        borrowers.cardnumber,
         issues.itemnumber,
         issues.issuedate,
         items.barcode,
@@ -305,8 +304,8 @@ if ($noreport) {
     $strsth.=" AND borrowers.branchcode   = '" . $branchfilter   . "' " if $branchfilter;
     $strsth.=" AND items.homebranch       = '" . $homebranchfilter . "' " if $homebranchfilter;
     $strsth.=" AND items.holdingbranch    = '" . $holdingbranchfilter . "' " if $holdingbranchfilter;
-    $strsth.=" AND date_due < '" . $datedueto . "' "  if $datedueto;
-    $strsth.=" AND date_due > '" . $dateduefrom . "' " if $dateduefrom;
+    $strsth.=" AND date_due >= ?" if $dateduefrom;
+    $strsth.=" AND date_due <= ?" if $datedueto;
     # restrict patrons (borrowers) to those matching the patron attribute filter(s), if any
     my $bnlist = $have_pattr_filter_data ? join(',',keys %borrowernumber_to_attributes) : '';
     $strsth =~ s/WHERE 1=1/WHERE 1=1 AND borrowers.borrowernumber IN ($bnlist)/ if $bnlist;
@@ -321,8 +320,10 @@ if ($noreport) {
     );
     $template->param(sql=>$strsth);
     my $sth=$dbh->prepare($strsth);
-    #warn "overdue.pl : query string ".$strsth;
-    $sth->execute();
+    $sth->execute(
+        ($dateduefrom ? output_pref({ dt => $dateduefrom, dateformat => 'iso' }) : ()),
+        ($datedueto ? output_pref({ dt => $datedueto, dateformat => 'iso' }) : ()),
+    );
 
     my @overduedata;
     while (my $data = $sth->fetchrow_hashref) {
@@ -344,8 +345,9 @@ if ($noreport) {
             duedate                => output_pref($dt),
             borrowernumber         => $data->{borrowernumber},
             barcode                => $data->{barcode},
+            cardnumber             => $data->{cardnumber},
             itemnum                => $data->{itemnumber},
-            issuedate              => format_date($data->{issuedate}),
+            issuedate              => output_pref({ dt => dt_from_string( $data->{issuedate} ), dateonly => 1 }),
             borrowertitle          => $data->{borrowertitle},
             surname                => $data->{surname},
             firstname              => $data->{firstname},