Bug 3316 - Improve last 3 issues displayed on OPAC detail page
[koha.git] / C4 / Serials.pm
index 90db643..2de4ab3 100644 (file)
@@ -51,7 +51,6 @@ BEGIN {
     
     &UpdateClaimdateIssues
     &GetSuppliersWithLateIssues             &getsupplierbyserialid
-    &GetDistributedTo   &SetDistributedTo
     &getroutinglist     &delroutingmember   &addroutingmember
     &reorder_members
     &check_routing &updateClaim &removeMissingIssue
@@ -549,7 +548,7 @@ sub GetSubscriptionsFromBiblionumber {
         $subs->{ "status" . $subs->{'status'} } = 1;
         $subs->{'cannotedit'}=(C4::Context->preference('IndependantBranches') && 
                 C4::Context->userenv && 
-                C4::Context->userenv->{flags} !=1  && 
+                C4::Context->userenv->{flags} % 2 !=1  && 
                 C4::Context->userenv->{branch} && $subs->{branchcode} &&
                 (C4::Context->userenv->{branch} ne $subs->{branchcode}));
         if ( $subs->{enddate} eq '0000-00-00' ) {
@@ -704,7 +703,7 @@ sub GetSubscriptions {
         $line->{toggle} = 1 if $odd == 1;
         $line->{'cannotedit'}=(C4::Context->preference('IndependantBranches') && 
                 C4::Context->userenv && 
-                C4::Context->userenv->{flags} !=1  && 
+                C4::Context->userenv->{flags} % 2 !=1  && 
                 C4::Context->userenv->{branch} && $line->{branchcode} &&
                 (C4::Context->userenv->{branch} ne $line->{branchcode}));
         push @results, $line;
@@ -832,7 +831,7 @@ sub GetLatestSerials {
                         FROM     serial
                         WHERE    subscriptionid = ?
                         AND      (status =2 or status=4)
-                        ORDER BY planneddate DESC LIMIT 0,$limit
+                        ORDER BY publisheddate DESC LIMIT 0,$limit
                 ";
     my $sth = $dbh->prepare($strsth);
     $sth->execute($subscriptionid);
@@ -855,27 +854,6 @@ sub GetLatestSerials {
     return \@serials;
 }
 
-=head2 GetDistributedTo
-
-=over 4
-
-$distributedto=GetDistributedTo($subscriptionid)
-This function select the old previous value of distributedto in the database.
-
-=back
-
-=cut
-
-sub GetDistributedTo {
-    my $dbh = C4::Context->dbh;
-    my $distributedto;
-    my $subscriptionid = @_;
-    my $query = "SELECT distributedto FROM subscription WHERE subscriptionid=?";
-    my $sth   = $dbh->prepare($query);
-    $sth->execute($subscriptionid);
-    return ($distributedto) = $sth->fetchrow;
-}
-
 =head2 GetNextSeq
 
 =over 4
@@ -1301,7 +1279,7 @@ sub ModSubscription {
         $numberingmethod, $status,       $biblionumber,   $callnumber,
         $notes,           $letter,       $hemisphere,     $manualhistory,
         $internalnotes,   $serialsadditems,$subscriptionid,
-        $staffdisplaycount,$opacdisplaycount
+        $staffdisplaycount,$opacdisplaycount, $graceperiod, $location
     ) = @_;
 #     warn $irregularity;
     my $dbh   = C4::Context->dbh;
@@ -1311,7 +1289,7 @@ sub ModSubscription {
                         add1=?,every1=?,whenmorethan1=?,setto1=?,lastvalue1=?,innerloop1=?,
                         add2=?,every2=?,whenmorethan2=?,setto2=?,lastvalue2=?,innerloop2=?,
                         add3=?,every3=?,whenmorethan3=?,setto3=?,lastvalue3=?,innerloop3=?,
-                        numberingmethod=?, status=?, biblionumber=?, callnumber=?, notes=?, letter=?, hemisphere=?,manualhistory=?,internalnotes=?,serialsadditems=?,staffdisplaycount = ?,opacdisplaycount = ?
+                        numberingmethod=?, status=?, biblionumber=?, callnumber=?, notes=?, letter=?, hemisphere=?,manualhistory=?,internalnotes=?,serialsadditems=?,staffdisplaycount = ?,opacdisplaycount = ?, graceperiod = ?, location = ?
                     WHERE subscriptionid = ?";
      #warn "query :".$query;
     my $sth = $dbh->prepare($query);
@@ -1327,7 +1305,8 @@ sub ModSubscription {
         $numberingmethod, $status,       $biblionumber,   $callnumber,
         $notes,           $letter,       $hemisphere,     ($manualhistory?$manualhistory:0),
         $internalnotes,   $serialsadditems,
-        $staffdisplaycount, $opacdisplaycount, $subscriptionid
+        $staffdisplaycount, $opacdisplaycount, $graceperiod, $location,
+        $subscriptionid
     );
     my $rows=$sth->rows;
     $sth->finish;
@@ -1345,7 +1324,8 @@ $subscriptionid = &NewSubscription($auser,branchcode,$aqbooksellerid,$cost,$aqbu
     $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
     $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
     $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
-    $numberingmethod, $status, $notes, $serialsadditems)
+    $numberingmethod, $status, $notes, $serialsadditems,
+    $staffdisplaycount, $opacdisplaycount, $graceperiod, $location);
 
 Create a new subscription with value given on input args.
 
@@ -1368,7 +1348,8 @@ sub NewSubscription {
         $lastvalue3,    $innerloop3,   $numberingmethod, $status,
         $notes,         $letter,       $firstacquidate,  $irregularity,
         $numberpattern, $callnumber,   $hemisphere,      $manualhistory,
-        $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount
+        $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount,
+        $graceperiod, $location
     ) = @_;
     my $dbh = C4::Context->dbh;
 
@@ -1381,8 +1362,9 @@ sub NewSubscription {
             add2,every2,whenmorethan2,setto2,lastvalue2,innerloop2,
             add3,every3,whenmorethan3,setto3,lastvalue3,innerloop3,
             numberingmethod, status, notes, letter,firstacquidate,irregularity,
-            numberpattern, callnumber, hemisphere,manualhistory,internalnotes,serialsadditems,staffdisplaycount,opacdisplaycount)
-        VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
+            numberpattern, callnumber, hemisphere,manualhistory,internalnotes,serialsadditems,
+            staffdisplaycount,opacdisplaycount,graceperiod,location)
+        VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
         |;
     my $sth = $dbh->prepare($query);
     $sth->execute(
@@ -1407,7 +1389,8 @@ sub NewSubscription {
         $numberpattern,                 $callnumber,
         $hemisphere,                    $manualhistory,
         $internalnotes,                 $serialsadditems,
-               $staffdisplaycount,                             $opacdisplaycount
+               $staffdisplaycount,                             $opacdisplaycount,
+        $graceperiod,                   $location,
     );
 
     #then create the 1st waited number
@@ -1778,12 +1761,14 @@ sub ItemizeSerials {
 
 =over 4
 
-1 or 0 = HasSubscriptionExpired($subscriptionid)
+$has_expired = HasSubscriptionExpired($subscriptionid)
 
 the subscription has expired when the next issue to arrive is out of subscription limit.
 
 return :
-1 if true, 0 if false.
+0 if the subscription has not expired
+1 if the subscription has expired
+2 if has subscription does not have a valid expiration date set
 
 =back
 
@@ -1806,9 +1791,7 @@ sub HasSubscriptionExpired {
          return 0 unless $res;
       my @res=split (/-/,$res);
       my @endofsubscriptiondate=split(/-/,$expirationdate);
-      map{
-        return 2 unless $_ ;
-      } (@res,@endofsubscriptiondate);     
+      return 2 if (scalar(@res)!=3 || scalar(@endofsubscriptiondate)!=3||not check_date(@res) || not check_date(@endofsubscriptiondate));
       return 1 if ( (@endofsubscriptiondate && Delta_Days($res[0],$res[1],$res[2],
                   $endofsubscriptiondate[0],$endofsubscriptiondate[1],$endofsubscriptiondate[2]) <= 0)
                   || (!$res));
@@ -1825,29 +1808,6 @@ sub HasSubscriptionExpired {
     return 0;  # Notice that you'll never get here.
 }
 
-=head2 SetDistributedto
-
-=over 4
-
-SetDistributedto($distributedto,$subscriptionid);
-This function update the value of distributedto for a subscription given on input arg.
-
-=back
-
-=cut
-
-sub SetDistributedto {
-    my ( $distributedto, $subscriptionid ) = @_;
-    my $dbh   = C4::Context->dbh;
-    my $query = qq|
-        UPDATE subscription
-        SET    distributedto=?
-        WHERE  subscriptionid=?
-    |;
-    my $sth = $dbh->prepare($query);
-    $sth->execute( $distributedto, $subscriptionid );
-}
-
 =head2 DelSubscription
 
 =over 4
@@ -2368,9 +2328,9 @@ sub abouttoexpire {
       $sth->execute($subscriptionid);
       my ($res) = $sth->fetchrow ;
 #        warn "date expiration : ".$expirationdate." date courante ".$res;
-      my @res=split /-/,$res;
+      my @res=split (/-/,$res);
       @res=Date::Calc::Today if ($res[0]*$res[1]==0);
-      my @endofsubscriptiondate=split/-/,$expirationdate;
+      my @endofsubscriptiondate=split(/-/,$expirationdate);
       my $x;
       if ( $per == 1 ) {$x=7;}
       if ( $per == 2 ) {$x=7; }