Result of a cherry-pick, tidying up a comment
[koha.git] / circ / overdue.pl
index 23d5c75..3797926 100755 (executable)
@@ -25,6 +25,7 @@ use CGI;
 use C4::Auth;
 use C4::Branch;
 use C4::Dates qw/format_date/;
+use Date::Calc qw/Today/;
 
 my $input = new CGI;
 my $type    = $input->param('type');
@@ -37,6 +38,7 @@ my   $borcatfilter = $input->param('borcat');
 my $itemtypefilter = $input->param('itemtype');
 my $borflagsfilter = $input->param('borflags') || " ";
 my   $branchfilter = $input->param('branch');
+my $op             = $input->param('op');
 
 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
     {
@@ -50,6 +52,19 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
 );
 my $dbh = C4::Context->dbh;
 
+
+# download the complete CSV
+if ($op eq 'csv') {
+warn "BRANCH : $branchfilter";
+    my $lib = $branchfilter ? "-library $branchfilter" :'';
+    my $csv = `../misc/cronjobs/overdue_notices.pl -csv -n $lib`;
+    print $input->header(-type => 'application/vnd.sun.xml.calc',
+                        -encoding    => 'utf-8',
+                        -attachment=>"overdues.csv",
+                        -filename=>"overdues.csv" );
+    print $csv;
+    exit;
+}
 my $req;
 $req = $dbh->prepare( "select categorycode, description from categories order by description");
 $req->execute;
@@ -80,32 +95,22 @@ my $onlymine=C4::Context->preference('IndependantBranches') &&
              C4::Context->userenv->{branch};
 my $branches = GetBranches($onlymine);
 my @branchloop;
-my @selectflags;
-push @selectflags, " ";#
-push @selectflags,"gonenoaddress";#
-push @selectflags,"debarred";#
-push @selectflags,"lost";#
-my $CGIflags=CGI::scrolling_list( -name     => 'borflags',
-            -id =>'borflags',
-            -values   => \@selectflags,
-            -size     => 1,
-            -multiple => 0 );
 
 foreach my $thisbranch ( sort keys %$branches ) {
      my %row = (
         value      => $thisbranch,
         branchname => $branches->{$thisbranch}->{'branchname'},
-        selected   => (C4::Context->userenv && $branches->{$thisbranch}->{'branchcode'} eq C4::Context->userenv->{'branch'})
+        selected   => ($branches->{$thisbranch}->{'branchcode'} eq $branchfilter)
     );
     push @branchloop, \%row;
 }
 $branchfilter=C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter);
 
-$template->param( branchloop => \@branchloop );
+$template->param( branchloop => \@branchloop,
+                  branchfilter => $branchfilter);
 $template->param(borcatloop=> \@borcatloop,
           itemtypeloop => \@itemtypeloop,
           branchloop=> \@branchloop,
-          CGIflags     => $CGIflags,
           borname => $bornamefilter,
           order => $order,
           showall => $showall);
@@ -119,49 +124,45 @@ my $data3;
 my $name;
 my $phone;
 my $email;
-my $biblionumber;
 my $title;
 my $author;
-my @datearr    = localtime( time() );
-my $todaysdate =
-    ( 1900 + $datearr[5] ) . '-'
-  . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
-  . sprintf( "%0.2d", $datearr[3] );
 
+my $todaysdate = sprintf("%-04.4d-%-02.2d-%02.2d", Today());
 
 $bornamefilter =~s/\*/\%/g;
 $bornamefilter =~s/\?/\_/g;
 
-my $strsth="select date_due,concat(surname,' ', firstname) as borrower, 
-  borrowers.phone, borrowers.email,issues.itemnumber, items.barcode, biblio.title, biblio.author,borrowers.borrowernumber 
-  from issues
+my $strsth="SELECT date_due,concat(surname,' ', firstname) as borrower, 
+  borrowers.phone, borrowers.email,issues.itemnumber, items.barcode, biblio.title, biblio.author,borrowers.borrowernumber,biblio.biblionumber,borrowers.branchcode 
+  FROM issues
 LEFT JOIN borrowers ON (issues.borrowernumber=borrowers.borrowernumber )
 LEFT JOIN items ON (issues.itemnumber=items.itemnumber)
 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
 LEFT JOIN biblio ON (biblio.biblionumber=items.biblionumber )
-where isnull(returndate) ";
+WHERE 1=1 "; # placeholder, since it is possible that none of the additional
+             # conditions will be selected by user
 $strsth.= " && date_due<'".$todaysdate."' " unless ($showall);
 $strsth.=" && (borrowers.firstname like '".$bornamefilter."%' or borrowers.surname like '".$bornamefilter."%' or borrowers.cardnumber like '".$bornamefilter."%')" if($bornamefilter) ;
 $strsth.=" && borrowers.categorycode = '".$borcatfilter."' " if($borcatfilter) ;
 $strsth.=" && biblioitems.itemtype = '".$itemtypefilter."' " if($itemtypefilter) ;
 $strsth.=" && borrowers.flags = '".$borflagsfilter."' " if ($borflagsfilter ne " ") ;
-$strsth.=" && issues.branchcode = '".$branchfilter."' " if($branchfilter) ;
+$strsth.=" && borrowers.branchcode = '".$branchfilter."' " if($branchfilter) ;
 if ($order eq "borrower"){
-  $strsth.=" order by borrower,date_due " ;
+  $strsth.=" ORDER BY borrower,date_due " ;
 } elsif ($order eq "title"){
-  $strsth.=" order by title,date_due,borrower ";
+  $strsth.=" ORDER BY title,date_due,borrower ";
 } elsif ($order eq "barcode"){
-  $strsth.=" order by items.barcode,date_due,borrower ";
-}elsif ($order eq "borrower desc"){
-  $strsth.=" order by borrower desc,date_due " ;
-} elsif ($order eq "title desc"){
-  $strsth.=" order by title desc,date_due,borrower ";
-} elsif ($order eq "barcode desc"){
-  $strsth.=" order by items.barcode desc,date_due,borrower ";
-} elsif ($order eq "date_due desc"){
-  $strsth.=" order by date_due desc,borrower ";
+  $strsth.=" ORDER BY items.barcode,date_due,borrower ";
+}elsif ($order eq "borrower DESC"){
+  $strsth.=" ORDER BY borrower desc,date_due " ;
+} elsif ($order eq "title DESC"){
+  $strsth.=" ORDER BY title desc,date_due,borrower ";
+} elsif ($order eq "barcode DESC"){
+  $strsth.=" ORDER BY items.barcode desc,date_due,borrower ";
+} elsif ($order eq "date_due DESC"){
+  $strsth.=" ORDER BY date_due DESC,borrower ";
 } else {
-  $strsth.=" order by date_due,borrower ";
+  $strsth.=" ORDER BY date_due,borrower ";
 }
 my $sth=$dbh->prepare($strsth);
 #warn "overdue.pl : query string ".$strsth;
@@ -179,21 +180,22 @@ while (my $data=$sth->fetchrow_hashref) {
 
   $title=$data->{'title'};
   $author=$data->{'author'};
-  push (@overduedata, { duedate      => $duedate,
-      bornum       => $data->{borrowernumber},
-      barcode      => $data->{barcode},
-      itemnum      => $itemnum,
-      name         => $name,
-      phone        => $phone,
-      email        => $email,
-      biblionumber => $biblionumber,
-      title        => $title,
-      author       => $author });
-
+  push (@overduedata, {
+                        duedate        => $duedate,
+                        borrowernumber => $data->{borrowernumber},
+                        barcode        => $data->{barcode},
+                        itemnum        => $itemnum,
+                        name           => $name,
+                        phone          => $phone,
+                        email          => $email,
+                        biblionumber   => $data->{'biblionumber'},
+                        title          => $title,
+                        author         => $author,
+                        branchcode     => $data->{'branchcode'} });
 }
 
 $template->param(
-    todaysdate  => $todaysdate,
+    todaysdate  => format_date($todaysdate),
     overdueloop => \@overduedata
 );