Merge remote branch 'kc/new/bug_5030' into kcmaster
[koha.git] / C4 / Serials.pm
index 7b42465..32e46be 100644 (file)
@@ -91,15 +91,12 @@ the array is in name order
 
 sub GetSuppliersWithLateIssues {
     my $dbh   = C4::Context->dbh;
-    my $query = qq|
-        SELECT DISTINCT id, name
-        FROM            subscription 
-        LEFT JOIN       serial ON serial.subscriptionid=subscription.subscriptionid
-        LEFT JOIN       aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
-        WHERE           subscription.subscriptionid = serial.subscriptionid
-        AND             (planneddate < now() OR serial.STATUS = 3 OR serial.STATUS = 4)
-        ORDER BY name
-    |;
+    my $query = q|
+    SELECT DISTINCT aqbooksellerid as id, aqbooksellers.name as name
+    FROM            subscription
+    LEFT JOIN       serial ON serial.subscriptionid=subscription.subscriptionid
+    LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id
+    WHERE id > 0 AND (planneddate < now() OR serial.STATUS = 3 OR serial.STATUS = 4) ORDER BY name|;
     return $dbh->selectall_arrayref($query, { Slice => {} });
 }
 
@@ -290,10 +287,10 @@ sub UpdateClaimdateIssues {
     my $dbh = C4::Context->dbh;
     $date = strftime( "%Y-%m-%d", localtime ) unless ($date);
     my $query = "
-        UPDATE serial SET claimdate=$date,status=7
-        WHERE  serialid in (" . join( ",", @$serialids ) . ")";
+        UPDATE serial SET claimdate = ?, status = 7
+        WHERE  serialid in (" . join( ",", map { '?' } @$serialids ) . ")";
     my $rq = $dbh->prepare($query);
-    $rq->execute;
+    $rq->execute($date, @$serialids);
     return $rq->rows;
 }
 
@@ -408,14 +405,17 @@ sub PrepareSerialsData {
     my $first;
     my $previousnote = "";
 
-    foreach my $subs (@$lines) {
-        $subs->{'publisheddate'} = (
-            $subs->{'publisheddate'}
-            ? format_date( $subs->{'publisheddate'} )
-            : "XXX"
-        );
+    foreach my $subs (@{$lines}) {
+        for my $datefield ( qw(publisheddate planneddate) ) {
+            # handle both undef and undef returned as 0000-00-00
+            if (!defined $subs->{$datefield} or $subs->{$datefield}=~m/^00/) {
+                $subs->{$datefield} = 'XXX';
+            }
+            else {
+                $subs->{$datefield} = format_date( $subs->{$datefield}  );
+            }
+        }
         $subs->{'branchname'} = GetBranchName( $subs->{'branchcode'} );
-        $subs->{'planneddate'}                  = format_date( $subs->{'planneddate'} );
         $subs->{ "status" . $subs->{'status'} } = 1;
         $subs->{"checked"}                      = $subs->{'status'} =~ /1|3|4|7/;
 
@@ -1594,6 +1594,9 @@ sub HasSubscriptionExpired {
     my $subscription     = GetSubscription($subscriptionid);
     if ( ( $subscription->{periodicity} % 16 ) > 0 ) {
         my $expirationdate = $subscription->{enddate};
+        if (!defined $expirationdate) {
+            $expirationdate = q{};
+        }
         my $query          = qq|
             SELECT max(planneddate)
             FROM   serial
@@ -1708,7 +1711,7 @@ sub DelIssue {
 
 =head2 GetLateOrMissingIssues
 
-@issuelist = &GetLateMissingIssues($supplierid,$serialid)
+@issuelist = GetLateMissingIssues($supplierid,$serialid)
 
 this function selects missing issues on database - where serial.status = 4 or serial.status=3 or planneddate<now
 
@@ -1766,10 +1769,11 @@ sub GetLateOrMissingIssues {
     $sth->execute;
     my @issuelist;
     while ( my $line = $sth->fetchrow_hashref ) {
-        if ($line->{planneddate}) {
+
+        if ($line->{planneddate} && $line->{planneddate} !~/^0+\-/) {
             $line->{planneddate} = format_date( $line->{planneddate} );
         }
-        if ($line->{claimdate}) {
+        if ($line->{claimdate} && $line->{claimdate} !~/^0+\-/) {
             $line->{claimdate}   = format_date( $line->{claimdate} );
         }
         $line->{"status".$line->{status}}   = 1;
@@ -2007,7 +2011,7 @@ sub getroutinglist {
     my $sth              = $dbh->prepare(
         "SELECT routingid, borrowernumber, ranking, biblionumber 
             FROM subscription 
-            LEFT JOIN subscriptionroutinglist ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid
+            JOIN subscriptionroutinglist ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid
             WHERE subscription.subscriptionid = ? ORDER BY ranking ASC
                               "
     );
@@ -2078,15 +2082,15 @@ returns a count of items from serial matching the subscriptionid
 sub HasItems {
     my ($subscriptionid) = @_;
     my $dbh              = C4::Context->dbh;
-    my $query = qq|
+    my $query = q|
             SELECT COUNT(serialitems.itemnumber)
             FROM   serial 
                        LEFT JOIN serialitems USING(serialid)
-            WHERE  subscriptionid=? AND serialitems.serialid NOT NULL
+            WHERE  subscriptionid=? AND serialitems.serialid IS NOT NULL
         |;
     my $sth=$dbh->prepare($query);
     $sth->execute($subscriptionid);
-    my ($countitems)=$sth->fetchrow;
+    my ($countitems)=$sth->fetchrow_array();
     return $countitems;  
 }
 
@@ -2332,6 +2336,6 @@ __END__
 
 =head1 AUTHOR
 
-Koha Developement team <info@koha.org>
+Koha Development Team <http://koha-community.org/>
 
 =cut