Bug 5549 : Get basic 24 Hr loan working
[koha_fer] / circ / overdue.pl
index 4257fdf..a7207b7 100755 (executable)
@@ -2,6 +2,7 @@
 
 
 # Copyright 2000-2002 Katipo Communications
+# Parts copyright 2010 BibLibre
 #
 # This file is part of Koha.
 #
@@ -14,9 +15,9 @@
 # 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., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# 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.
 
 use strict;
 use warnings;
@@ -26,7 +27,7 @@ use CGI qw(-oldstyle_urls);
 use C4::Auth;
 use C4::Branch;
 use C4::Debug;
-use C4::Dates qw/format_date/;
+use C4::Dates qw/format_date format_date_in_iso/;
 use Date::Calc qw/Today/;
 use Text::CSV_XS;
 
@@ -39,6 +40,8 @@ my $itemtypefilter  = $input->param('itemtype') || '';
 my $borflagsfilter  = $input->param('borflag') || '';
 my $branchfilter    = $input->param('branch') || '';
 my $op              = $input->param('op') || '';
+my $dateduefrom = format_date_in_iso($input->param( 'dateduefrom' )) || '';
+my $datedueto   = format_date_in_iso($input->param( 'datedueto' )) || '';
 my $isfiltered      = $op =~ /apply/i && $op =~ /filter/i;
 my $noreport        = C4::Context->preference('FilterBeforeOverdueReport') && ! $isfiltered && $op ne "csv";
 
@@ -100,7 +103,7 @@ for my $attrcode (grep { /^patron_attr_filter_/ } $input->param) {
 my $have_pattr_filter_data = keys(%cgi_attrcode_to_attrvalues) > 0;
 
 my @patron_attr_filter_loop;   # array of [ domid cgivalue ismany isclone ordinal code description repeatable authorised_value_category ]
-#my @patron_attr_order_loop;    # array of { label => $patron_attr_label, value => $patron_attr_order }
+my @patron_attr_order_loop;    # array of { label => $patron_attr_label, value => $patron_attr_order }
 
 my @sort_roots = qw(borrower title barcode date_due);
 push @sort_roots, map {$_ . " desc"} @sort_roots;
@@ -144,13 +147,13 @@ while (my $row = $sth->fetchrow_hashref) {
         };
     }
 } continue { ++$ordinal }
-#for (@patron_attr_order_loop) { $_->{selected} = 1 if $order eq $_->{value} }
+for (@patron_attr_order_loop) { $_->{selected} = 1 if $order eq $_->{value} }
 
 $template->param(ORDER_LOOP => \@order_loop);
 
 my %borrowernumber_to_attributes;    # hash of { borrowernumber => { attrcode => [ [val,display], [val,display], ... ] } }
                                      #   i.e. val differs from display when attr is an authorised value
-if (0 && @patron_attr_filter_loop) {
+if (@patron_attr_filter_loop) {
     # MAYBE FIXME: currently, *all* borrower_attributes are loaded into %borrowernumber_to_attributes
     #              then filtered and honed down to match the patron attribute filters. If this is
     #              too resource intensive, MySQL can be used to do the filtering, i.e. rewire the
@@ -200,14 +203,19 @@ if (0 && @patron_attr_filter_loop) {
 
 
 $template->param(
+    patron_attr_header_loop => [ map { { header => $_->{description} } } grep { ! $_->{isclone} } @patron_attr_filter_loop ],
     branchloop   => GetBranchesLoop($branchfilter, $onlymine),
     branchfilter => $branchfilter,
     borcatloop=> \@borcatloop,
     itemtypeloop => \@itemtypeloop,
-    patron_attr_filter_loop => \@patron_attr_filter_loop,
+    patron_attr_filter_loop => \@patron_attr_filter_loop,
     borname => $bornamefilter,
     order => $order,
-    showall => $showall);
+    showall => $showall,
+    DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
+    dateduefrom => $input->param( 'dateduefrom' ) || '',
+    datedueto   => $input->param( 'datedueto' ) || '',
+);
 
 if ($noreport) {
     # la de dah ... page comes up presto-quicko
@@ -227,10 +235,20 @@ if ($noreport) {
     $bornamefilter =~s/\?/\_/g;
 
     my $strsth="SELECT date_due,
-        concat(surname,' ', firstname) as borrower, 
+        borrowers.title as borrowertitle,
+        borrowers.surname,
+        borrowers.firstname,
+        borrowers.streetnumber,
+        borrowers.streettype, 
+        borrowers.address,
+        borrowers.address2,
+        borrowers.city,
+        borrowers.zipcode,
+        borrowers.country,
         borrowers.phone,
         borrowers.email,
         issues.itemnumber,
+        issues.issuedate,
         items.barcode,
         biblio.title,
         biblio.author,
@@ -249,19 +267,36 @@ if ($noreport) {
     $strsth.=" AND date_due               < '" . $todaysdate     . "' " unless ($showall);
     $strsth.=" AND (borrowers.firstname like '".$bornamefilter."%' or borrowers.surname like '".$bornamefilter."%' or borrowers.cardnumber like '".$bornamefilter."%')" if($bornamefilter) ;
     $strsth.=" AND borrowers.categorycode = '" . $borcatfilter   . "' " if $borcatfilter;
-    $strsth.=" AND biblioitems.itemtype   = '" . $itemtypefilter . "' " if $itemtypefilter;
-    $strsth.=" AND borrowers.flags        = '" . $borflagsfilter . "' " if $borflagsfilter;
+    if( $itemtypefilter ){
+        if( C4::Context->preference('item-level_itypes') ){
+            $strsth.=" AND items.itype   = '" . $itemtypefilter . "' ";
+        } else {
+            $strsth.=" AND biblioitems.itemtype   = '" . $itemtypefilter . "' ";
+        }
+    }
+    if ( $borflagsfilter eq 'gonenoaddress' ) {
+        $strsth .= " AND borrowers.gonenoaddress <> 0";
+    }
+    elsif ( $borflagsfilter eq 'debarred' ) {
+        $strsth .= " AND borrowers.debarred >=  CURDATE()" ;
+    }
+    elsif ( $borflagsfilter eq 'lost') {
+        $strsth .= " AND borrowers.lost <> 0";
+    }
     $strsth.=" AND borrowers.branchcode   = '" . $branchfilter   . "' " if $branchfilter;
+    $strsth.=" AND date_due < '" . $datedueto . "' "  if $datedueto;
+    $strsth.=" AND date_due > '" . $dateduefrom . "' " if $dateduefrom;
     # 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;
     $strsth =~ s/WHERE 1=1/WHERE 0=1/ if $have_pattr_filter_data  && !$bnlist;  # no match if no borrowers matched patron attrs
     $strsth.=" ORDER BY " . (
-        ($order eq "borrower" or $order eq "borrower desc") ? "$order, date_due"                 : 
-        ($order eq "title"    or $order eq    "title desc") ? "$order, date_due, borrower"       :
-        ($order eq "barcode"  or $order eq  "barcode desc") ? "items.$order, date_due, borrower" :
-                                ($order eq "date_due desc") ? "date_due DESC, borrower"          :
-                                                            "date_due, borrower"  # default sort order
+        ($order eq "borrower")                              ? "surname, firstname, date_due"               : 
+        ($order eq "borrower desc")                         ? "surname desc, firstname desc, date_due"     : 
+        ($order eq "title"    or $order eq    "title desc") ? "$order, date_due, surname, firstname"       :
+        ($order eq "barcode"  or $order eq  "barcode desc") ? "items.$order, date_due, surname, firstname" :
+                                ($order eq "date_due desc") ? "date_due DESC, surname, firstname"          :
+                                                            "date_due, surname, firstname"  # default sort order
     );
     $template->param(sql=>$strsth);
     my $sth=$dbh->prepare($strsth);
@@ -288,7 +323,17 @@ if ($noreport) {
             borrowernumber         => $data->{borrowernumber},
             barcode                => $data->{barcode},
             itemnum                => $data->{itemnumber},
-            name                   => $data->{borrower},
+            issuedate              => format_date($data->{issuedate}),
+            borrowertitle          => $data->{borrowertitle},
+            surname                => $data->{surname},
+            firstname              => $data->{firstname},
+            streetnumber           => $data->{streetnumber},                   
+            streettype             => $data->{streettype},                     
+            address                => $data->{address},                        
+            address2               => $data->{address2},                       
+            city                   => $data->{city},                   
+            zipcode                => $data->{zipcode},                        
+            country                => $data->{country},
             phone                  => $data->{phone},
             email                  => $data->{email},
             biblionumber           => $data->{biblionumber},
@@ -325,7 +370,7 @@ if ($noreport) {
     }
 
     if ($op eq 'csv') {
-        binmode(STDOUT, ":utf8");
+        binmode(STDOUT, ":encoding(UTF-8)");
         my $csv = build_csv(\@overduedata);
         print $input->header(-type => 'application/vnd.sun.xml.calc',
                              -encoding    => 'utf-8',
@@ -366,7 +411,8 @@ sub build_csv {
     my @lines = ();
 
     # build header ...
-    my @keys = grep { $_ ne 'patron_attr_value_loop' } sort keys %{ $overdues->[0] };
+    my @keys = qw /duedate title author borrowertitle name phone barcode email address address2 zipcode city country
+                branchcode itemcallnumber biblionumber borrowernumber itemnum issuedate replacementprice streetnumber streettype/;
     my $csv = Text::CSV_XS->new();
     $csv->combine(@keys);
     push @lines, $csv->string();