Fix overdue list if you do not specify a branchcode.
[koha_fer] / circ / overdue.pl
index 81d3aa3..3797926 100755 (executable)
@@ -1,6 +1,5 @@
 #!/usr/bin/perl
 
-# $Id$
 
 # Copyright 2000-2002 Katipo Communications
 #
@@ -24,12 +23,22 @@ use C4::Context;
 use C4::Output;
 use CGI;
 use C4::Auth;
-use C4::Date;
+use C4::Branch;
+use C4::Dates qw/format_date/;
+use Date::Calc qw/Today/;
 
 my $input = new CGI;
-my $type  = $input->param('type');
+my $type    = $input->param('type');
+my $theme   = $input->param('theme');    # only used if allowthemeoverride is set
+my $order   = $input->param('order');
+my $showall = $input->param('showall');
 
-my $theme = $input->param('theme');    # only used if allowthemeoverride is set
+my  $bornamefilter = $input->param('borname');
+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(
     {
@@ -41,6 +50,71 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
         debug           => 1,
     }
 );
+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;
+my @borcatloop;
+while (my ($catcode, $description) =$req->fetchrow) {
+  my $selected = 1 if $catcode eq $borcatfilter;
+  my %row =(value => $catcode,
+        selected => $selected,
+        catname => $description,
+      );
+  push @borcatloop, \%row;
+}
+
+$req = $dbh->prepare( "select itemtype, description from itemtypes order by description");
+$req->execute;
+my @itemtypeloop;
+while (my ($itemtype, $description) =$req->fetchrow) {
+  my $selected = 1 if $itemtype eq $itemtypefilter;
+  my %row =(value => $itemtype,
+        selected => $selected,
+        itemtypename => $description,
+      );
+  push @itemtypeloop, \%row;
+}
+my $onlymine=C4::Context->preference('IndependantBranches') && 
+             C4::Context->userenv && 
+             C4::Context->userenv->{flags}!=1 && 
+             C4::Context->userenv->{branch};
+my $branches = GetBranches($onlymine);
+my @branchloop;
+
+foreach my $thisbranch ( sort keys %$branches ) {
+     my %row = (
+        value      => $thisbranch,
+        branchname => $branches->{$thisbranch}->{'branchname'},
+        selected   => ($branches->{$thisbranch}->{'branchcode'} eq $branchfilter)
+    );
+    push @branchloop, \%row;
+}
+$branchfilter=C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter);
+
+$template->param( branchloop => \@branchloop,
+                  branchfilter => $branchfilter);
+$template->param(borcatloop=> \@borcatloop,
+          itemtypeloop => \@itemtypeloop,
+          branchloop=> \@branchloop,
+          borname => $bornamefilter,
+          order => $order,
+          showall => $showall);
+
 my $duedate;
 my $borrowernumber;
 my $itemnum;
@@ -50,74 +124,78 @@ 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 $dbh = C4::Context->dbh;
+my $todaysdate = sprintf("%-04.4d-%-02.2d-%02.2d", Today());
+
+$bornamefilter =~s/\*/\%/g;
+$bornamefilter =~s/\?/\_/g;
 
-my $sth =
-  $dbh->prepare(
-    "select date_due,borrowernumber,itemnumber
-     from issues
-     where isnull(returndate) && date_due<? order by date_due,borrowernumber"
-  );
-$sth->execute($todaysdate);
+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 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.=" && borrowers.branchcode = '".$branchfilter."' " if($branchfilter) ;
+if ($order eq "borrower"){
+  $strsth.=" ORDER BY borrower,date_due " ;
+} elsif ($order eq "title"){
+  $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 ";
+} else {
+  $strsth.=" ORDER BY date_due,borrower ";
+}
+my $sth=$dbh->prepare($strsth);
+#warn "overdue.pl : query string ".$strsth;
+$sth->execute();
 
 my @overduedata;
-while ( my $data = $sth->fetchrow_hashref ) {
-    $duedate        = format_date($data->{'date_due'});
-    $borrowernumber = $data->{'borrowernumber'};
-    $itemnum        = $data->{'itemnumber'};
-
-    my $sth1 =
-      $dbh->prepare(
-"select concat(firstname,' ',surname),phone,email from borrowers where borrowernumber=?"
-      );
-    $sth1->execute($borrowernumber);
-    $data1 = $sth1->fetchrow_hashref;
-    $name  = $data1->{'concat(firstname,\' \',surname)'};
-    $phone = $data1->{'phone'};
-    $email = $data1->{'email'};
-    $sth1->finish;
-
-    my $sth2 =
-      $dbh->prepare("select biblionumber from items where itemnumber=?");
-    $sth2->execute($itemnum);
-    $data2        = $sth2->fetchrow_hashref;
-    $biblionumber = $data2->{'biblionumber'};
-    $sth2->finish;
-
-    my $sth3 =
-      $dbh->prepare("select title,author from biblio where biblionumber=?");
-    $sth3->execute($biblionumber);
-    $data3  = $sth3->fetchrow_hashref;
-    $title  = $data3->{'title'};
-    $author = $data3->{'author'};
-    $sth3->finish;
-    push(
-        @overduedata,
-        {
-            duedate        => $duedate,
-            borrowernumber => $borrowernumber,
-            itemnum        => $itemnum,
-            name           => $name,
-            phone          => $phone,
-            email          => $email,
-            biblionumber   => $biblionumber,
-            title          => $title,
-            author         => $author
-        }
-    );
+while (my $data=$sth->fetchrow_hashref) {
+  $duedate=$data->{'date_due'};
+  $duedate = format_date($duedate);
+  $itemnum=$data->{'itemnumber'};
+
+  $name=$data->{'borrower'};
+  $phone=$data->{'phone'};
+  $email=$data->{'email'};
+
+  $title=$data->{'title'};
+  $author=$data->{'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
 );