Bug 10445: (follow-up) fix error
[koha.git] / circ / bookcount.pl
index 3a94e0d..75bf319 100755 (executable)
 # 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;
+#use warnings; FIXME - Bug 2505
 use CGI;
 use C4::Debug;
 use C4::Context;
@@ -45,12 +45,11 @@ my $data  = GetBiblioItemData($bi);
 my $homebranch    = $branches->{ $idata->{'homebranch'}    }->{'branchname'};
 my $holdingbranch = $branches->{ $idata->{'holdingbranch'} }->{'branchname'};
 
-my ( $lastmove, $message ) = lastmove($itm);
+my $lastmove = lastmove($itm);
 
 my $lastdate;
 my $count;
 if ( not $lastmove ) {
-#    $lastdate = $message;
     $count = issuessince( $itm, 0 );
 } else {
     $lastdate = $lastmove->{'datearrived'};
@@ -79,8 +78,6 @@ foreach (@$branchloop) {
     $_->{seentime}   = $timechunk;
 }
 
-### $lastdate
-
 $template->param(
     biblionumber            => $biblionumber,
     title                   => $data->{'title'},
@@ -89,7 +86,7 @@ $template->param(
     biblioitemnumber        => $bi,
     homebranch              => $homebranch,
     holdingbranch           => $holdingbranch,
-    lastdate                => $lastdate ?  format_date($lastdate) : $message,
+    lastdate                => $lastdate ?  format_date($lastdate) : 0,
     count                   => $count,
     branchloop              => $branchloop,
 );
@@ -112,14 +109,14 @@ sub lastmove {
     );
     $sth->execute($itemnumber);
     my ($date) = $sth->fetchrow_array;
-    return ( 0, "Item has no branch transfers record" ) if not $date;
+    return 0 unless $date;
     $sth = $dbh->prepare(
 "SELECT * FROM branchtransfers WHERE branchtransfers.itemnumber=? and branchtransfers.datearrived=?"
     );
     $sth->execute( $itemnumber, $date );
     my ($data) = $sth->fetchrow_hashref;
-    return ( 0, "Item has no branch transfers record" ) if not $data;
-    return ( $data, "" );
+    return 0 unless $data;
+    return $data;
 }
 
 sub issuessince {
@@ -162,9 +159,12 @@ sub lastseenat {
     $sth->execute( $itm, $brc, $itm, $brc );
     my ($date1) = $sth->fetchrow_array;
     $sth = $dbh->prepare(
-"SELECT max(datearrived) FROM branchtransfers WHERE itemnumber=? AND tobranch = ?"
+    "SELECT MAX(transfer) FROM (SELECT max(datearrived) AS transfer FROM branchtransfers WHERE itemnumber=? AND tobranch = ?
+     UNION ALL
+     SELECT max(datesent) AS transfer FROM branchtransfers WHERE itemnumber=? AND frombranch = ?
+    ) tmp"
     );
-    $sth->execute( $itm, $brc );
+    $sth->execute( $itm, $brc, $itm, $brc );
     my ($date2) = $sth->fetchrow_array;
 
     my $date = ( $date1 lt $date2 ) ? $date2 : $date1 ;
@@ -176,7 +176,6 @@ sub lastseenat {
 sub slashdate {
     my ($date) = @_;
     $date or return;
-    # warn "slashdate($date)...";
     return (
         format_date($date),
         substr($date,11,5)