(bug #3364) add itemnotes in itemslost report
[koha.git] / C4 / Serials.pm
index 5bec265..26b08db 100644 (file)
@@ -51,12 +51,10 @@ BEGIN {
     
     &UpdateClaimdateIssues
     &GetSuppliersWithLateIssues             &getsupplierbyserialid
-    &GetDistributedTo   &SetDistributedTo
     &getroutinglist     &delroutingmember   &addroutingmember
     &reorder_members
     &check_routing &updateClaim &removeMissingIssue
     
-    &old_newsubscription &old_modsubscription &old_getserials
        );
 }
 
@@ -106,9 +104,6 @@ sub GetSuppliersWithLateIssues {
     while ( my ( $id, $name ) = $sth->fetchrow ) {
         $supplierlist{$id} = $name;
     }
-    if ( C4::Context->preference("RoutingSerials") ) {
-        $supplierlist{''} = "All Suppliers";
-    }
     return %supplierlist;
 }
 
@@ -552,7 +547,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' ) {
@@ -707,7 +702,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;
@@ -835,7 +830,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);
@@ -858,27 +853,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
@@ -1106,9 +1080,9 @@ sub ModSubscriptionHistory {
                     WHERE subscriptionid=?
                 ";
     my $sth = $dbh->prepare($query);
-    $recievedlist =~ s/^,//g;
-    $missinglist  =~ s/^,//g;
-    $opacnote     =~ s/^,//g;
+    $recievedlist =~ s/^; //;
+    $missinglist  =~ s/^; //;
+    $opacnote     =~ s/^; //;
     $sth->execute(
         $histstartdate, $enddate,       $recievedlist, $missinglist,
         $opacnote,      $librariannote, $subscriptionid
@@ -1164,21 +1138,22 @@ sub ModSerialStatus {
             my ( $missinglist, $recievedlist ) = $sth->fetchrow;
             if ( $status eq 2 ) {
 
-#             warn "receivedlist : $recievedlist serialseq :$serialseq, ".index("$recievedlist","$serialseq");
-                $recievedlist .= ",$serialseq"
+                $recievedlist .= "; $serialseq"
                   unless ( index( "$recievedlist", "$serialseq" ) >= 0 );
             }
 
 #         warn "missinglist : $missinglist serialseq :$serialseq, ".index("$missinglist","$serialseq");
-            $missinglist .= ",$serialseq"
+            $missinglist .= "$serialseq"
               if ( $status eq 4
                 and not index( "$missinglist", "$serialseq" ) >= 0 );
-            $missinglist .= ",not issued $serialseq"
+            $missinglist .= "not issued $serialseq"
               if ( $status eq 5
                 and index( "$missinglist", "$serialseq" ) >= 0 );
             $query =
 "UPDATE subscriptionhistory SET recievedlist=?, missinglist=? WHERE  subscriptionid=?";
             $sth = $dbh->prepare($query);
+            $recievedlist =~ s/^; //;
+            $missinglist  =~ s/^; //;
             $sth->execute( $recievedlist, $missinglist, $subscriptionid );
         }
     }
@@ -1303,7 +1278,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;
@@ -1313,7 +1288,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);
@@ -1329,8 +1304,9 @@ sub ModSubscription {
         $numberingmethod, $status,       $biblionumber,   $callnumber,
         $notes,           $letter,       $hemisphere,     ($manualhistory?$manualhistory:0),
         $internalnotes,   $serialsadditems,
-        $staffdisplaycount, $opacdisplaycount, $subscriptionid
-    ); warn "$staffdisplaycount, $opacdisplaycount";
+        $staffdisplaycount, $opacdisplaycount, $graceperiod, $location,
+        $subscriptionid
+    );
     my $rows=$sth->rows;
     $sth->finish;
     
@@ -1347,7 +1323,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.
 
@@ -1370,7 +1347,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;
 
@@ -1383,8 +1361,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(
@@ -1409,7 +1388,8 @@ sub NewSubscription {
         $numberpattern,                 $callnumber,
         $hemisphere,                    $manualhistory,
         $internalnotes,                 $serialsadditems,
-               $staffdisplaycount,                             $opacdisplaycount
+               $staffdisplaycount,                             $opacdisplaycount,
+        $graceperiod,                   $location,
     );
 
     #then create the 1st waited number
@@ -1558,10 +1538,10 @@ sub NewIssue {
       ### TODO Add a feature that improves recognition and description.
       ### As such count (serialseq) i.e. : N18,2(N19),N20
       ### Would use substr and index But be careful to previous presence of ()
-        $recievedlist .= ",$serialseq" unless (index($recievedlist,$serialseq)>0);
+        $recievedlist .= "$serialseq" unless (index($recievedlist,$serialseq)>0);
     }
     if ( $status eq 4 ) {
-        $missinglist .= ",$serialseq" unless (index($missinglist,$serialseq)>0);
+        $missinglist .= "$serialseq" unless (index($missinglist,$serialseq)>0);
     }
     $query = qq|
         UPDATE subscriptionhistory
@@ -1569,6 +1549,8 @@ sub NewIssue {
         WHERE  subscriptionid=?
     |;
     $sth = $dbh->prepare($query);
+    $recievedlist =~ s/^; //;
+    $missinglist  =~ s/^; //;
     $sth->execute( $recievedlist, $missinglist, $subscriptionid );
     return $serialid;
 }
@@ -1778,12 +1760,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
 
@@ -1805,8 +1789,8 @@ sub HasSubscriptionExpired {
       my ($res) = $sth->fetchrow  ;
          return 0 unless $res;
       my @res=split (/-/,$res);
-# warn "date expiration :$expirationdate";
       my @endofsubscriptiondate=split(/-/,$expirationdate);
+      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));
@@ -1823,29 +1807,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
@@ -2366,9 +2327,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; }
@@ -2396,232 +2357,6 @@ sub abouttoexpire {
    } else {return 0}
 }
 
-=head2 old_newsubscription
-
-=over 4
-
-($subscriptionid) = &old_newsubscription($auser,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
-                        $startdate,$periodicity,$firstacquidate,$dow,$irregularity,$numberpattern,$numberlength,$weeklength,$monthlength,
-                        $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,
-                        $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,
-                        $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,
-                        $numberingmethod, $status, $callnumber, $notes, $hemisphere)
-
-this function is similar to the NewSubscription subroutine but has a few different
-values passed in 
-$firstacquidate - date of first serial issue to arrive
-$irregularity - the issues not expected separated by a '|'
-- eg. monthly issue but not expecting issue for june and july would have $irregularity of '6|7'
-$numberpattern - the number for an array of labels to reconstruct the javascript correctly in the 
-   subscription-add.tmpl file
-$callnumber - display the callnumber of the serial
-$hemisphere - either 2 = southern hemisphere or 1 = northern hemisphere - used for quarterly serials
-
-return :
-the $subscriptionid number of the new subscription
-
-=back
-
-=cut
-
-sub old_newsubscription {
-    my (
-        $auser,         $aqbooksellerid,  $cost,          $aqbudgetid,
-        $biblionumber,  $startdate,       $periodicity,   $firstacquidate,
-        $dow,           $irregularity,    $numberpattern, $numberlength,
-        $weeklength,    $monthlength,     $add1,          $every1,
-        $whenmorethan1, $setto1,          $lastvalue1,    $add2,
-        $every2,        $whenmorethan2,   $setto2,        $lastvalue2,
-        $add3,          $every3,          $whenmorethan3, $setto3,
-        $lastvalue3,    $numberingmethod, $status,        $callnumber,
-        $notes,         $hemisphere
-    ) = @_;
-    my $dbh = C4::Context->dbh;
-
-    #save subscription
-    my $sth = $dbh->prepare(
-"insert into subscription (librarian,aqbooksellerid,cost,aqbudgetid,biblionumber,
-                                                        startdate,periodicity,firstacquidate,dow,irregularity,numberpattern,numberlength,weeklength,monthlength,
-                                                                add1,every1,whenmorethan1,setto1,lastvalue1,
-                                                                add2,every2,whenmorethan2,setto2,lastvalue2,
-                                                                add3,every3,whenmorethan3,setto3,lastvalue3,
-                                                                numberingmethod, status, callnumber, notes, hemisphere) values
-                                                          (?,?,?,?,?,?,?,?,?,?,?,
-                                                                                           ?,?,?,?,?,?,?,?,?,?,?,
-                                                                                           ?,?,?,?,?,?,?,?,?,?,?,?)"
-    );
-    $sth->execute(
-        $auser,         $aqbooksellerid,
-        $cost,          $aqbudgetid,
-        $biblionumber,  format_date_in_iso($startdate),
-        $periodicity,   format_date_in_iso($firstacquidate),
-        $dow,           $irregularity,
-        $numberpattern, $numberlength,
-        $weeklength,    $monthlength,
-        $add1,          $every1,
-        $whenmorethan1, $setto1,
-        $lastvalue1,    $add2,
-        $every2,        $whenmorethan2,
-        $setto2,        $lastvalue2,
-        $add3,          $every3,
-        $whenmorethan3, $setto3,
-        $lastvalue3,    $numberingmethod,
-        $status,        $callnumber,
-        $notes,         $hemisphere
-    );
-
-    #then create the 1st waited number
-    my $subscriptionid = $dbh->{'mysql_insertid'};
-    my $enddate        = GetExpirationDate($subscriptionid);
-
-    $sth =
-      $dbh->prepare(
-"insert into subscriptionhistory (biblionumber, subscriptionid, histstartdate, enddate, missinglist, recievedlist, opacnote, librariannote) values (?,?,?,?,?,?,?,?)"
-      );
-    $sth->execute(
-        $biblionumber, $subscriptionid,
-        format_date_in_iso($startdate),
-        format_date_in_iso($enddate),
-        "", "", "", $notes
-    );
-
-   # reread subscription to get a hash (for calculation of the 1st issue number)
-    $sth =
-      $dbh->prepare("select * from subscription where subscriptionid = ? ");
-    $sth->execute($subscriptionid);
-    my $val = $sth->fetchrow_hashref;
-
-    # calculate issue number
-    my $serialseq = GetSeq($val);
-    $sth =
-      $dbh->prepare(
-"insert into serial (serialseq,subscriptionid,biblionumber,status, planneddate) values (?,?,?,?,?)"
-      );
-    $sth->execute( $serialseq, $subscriptionid, $val->{'biblionumber'},
-        1, format_date_in_iso($startdate) );
-    return $subscriptionid;
-}
-
-=head2 old_modsubscription
-
-=over 4
-
-($subscriptionid) = &old_modsubscription($auser,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
-                        $startdate,$periodicity,$firstacquidate,$dow,$irregularity,$numberpattern,$numberlength,$weeklength,$monthlength,
-                        $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,
-                        $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,
-                        $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,
-                        $numberingmethod, $status, $callnumber, $notes, $hemisphere, $subscriptionid)
-
-this function is similar to the ModSubscription subroutine but has a few different
-values passed in 
-$firstacquidate - date of first serial issue to arrive
-$irregularity - the issues not expected separated by a '|'
-- eg. monthly issue but not expecting issue for june and july would have $irregularity of '6|7'
-$numberpattern - the number for an array of labels to reconstruct the javascript correctly in the 
-   subscription-add.tmpl file
-$callnumber - display the callnumber of the serial
-$hemisphere - either 2 = southern hemisphere or 1 = northern hemisphere - used for quarterly serials
-
-=back
-
-=cut
-
-sub old_modsubscription {
-    my (
-        $auser,        $aqbooksellerid, $cost,           $aqbudgetid,
-        $startdate,    $periodicity,    $firstacquidate, $dow,
-        $irregularity, $numberpattern,  $numberlength,   $weeklength,
-        $monthlength,  $add1,           $every1,         $whenmorethan1,
-        $setto1,       $lastvalue1,     $innerloop1,     $add2,
-        $every2,       $whenmorethan2,  $setto2,         $lastvalue2,
-        $innerloop2,   $add3,           $every3,         $whenmorethan3,
-        $setto3,       $lastvalue3,     $innerloop3,     $numberingmethod,
-        $status,       $biblionumber,   $callnumber,     $notes,
-        $hemisphere,   $subscriptionid
-    ) = @_;
-    my $dbh = C4::Context->dbh;
-    my $sth = $dbh->prepare(
-"update subscription set librarian=?, aqbooksellerid=?,cost=?,aqbudgetid=?,startdate=?,
-                                                   periodicity=?,firstacquidate=?,dow=?,irregularity=?,numberpattern=?,numberlength=?,weeklength=?,monthlength=?,
-                                                  add1=?,every1=?,whenmorethan1=?,setto1=?,lastvalue1=?,innerloop1=?,
-                                                  add2=?,every2=?,whenmorethan2=?,setto2=?,lastvalue2=?,innerloop2=?,
-                                                  add3=?,every3=?,whenmorethan3=?,setto3=?,lastvalue3=?,innerloop3=?,
-                                                  numberingmethod=?, status=?, biblionumber=?, callnumber=?, notes=?, hemisphere=? where subscriptionid = ?"
-    );
-    $sth->execute(
-        $auser,        $aqbooksellerid, $cost,           $aqbudgetid,
-        $startdate,    $periodicity,    $firstacquidate, $dow,
-        $irregularity, $numberpattern,  $numberlength,   $weeklength,
-        $monthlength,  $add1,           $every1,         $whenmorethan1,
-        $setto1,       $lastvalue1,     $innerloop1,     $add2,
-        $every2,       $whenmorethan2,  $setto2,         $lastvalue2,
-        $innerloop2,   $add3,           $every3,         $whenmorethan3,
-        $setto3,       $lastvalue3,     $innerloop3,     $numberingmethod,
-        $status,       $biblionumber,   $callnumber,     $notes,
-        $hemisphere,   $subscriptionid
-    );
-    $sth->finish;
-
-    $sth =
-      $dbh->prepare("select * from subscription where subscriptionid = ? ");
-    $sth->execute($subscriptionid);
-    my $val = $sth->fetchrow_hashref;
-
-    # calculate issue number
-    my $serialseq = Get_Seq($val);
-    $sth =
-      $dbh->prepare("UPDATE serial SET serialseq = ? WHERE subscriptionid = ?");
-    $sth->execute( $serialseq, $subscriptionid );
-
-    my $enddate = subscriptionexpirationdate($subscriptionid);
-    $sth = $dbh->prepare("update subscriptionhistory set enddate=?");
-    $sth->execute( format_date_in_iso($enddate) );
-}
-
-=head2 old_getserials
-
-=over 4
-
-($totalissues,@serials) = &old_getserials($subscriptionid)
-
-this function get a hashref of serials and the total count of them
-
-return :
-$totalissues - number of serial lines
-the serials into a table. Each line of this table containts a ref to a hash which it containts
-serialid, serialseq, status,planneddate,notes,routingnotes  from tables : serial where status is not 2, 4, or 5
-
-=back
-
-=cut
-
-sub old_getserials {
-    my ($subscriptionid) = @_;
-    my $dbh = C4::Context->dbh;
-
-    # status = 2 is "arrived"
-    my $sth =
-      $dbh->prepare(
-"select serialid,serialseq, status, planneddate,notes,routingnotes from serial where subscriptionid = ? and status <>2 and status <>4 and status <>5"
-      );
-    $sth->execute($subscriptionid);
-    my @serials;
-    my $num = 1;
-    while ( my $line = $sth->fetchrow_hashref ) {
-        $line->{ "status" . $line->{status} } =
-          1;    # fills a "statusX" value, used for template status select list
-        $line->{"planneddate"} = format_date( $line->{"planneddate"} );
-        $line->{"num"}         = $num;
-        $num++;
-        push @serials, $line;
-    }
-    $sth = $dbh->prepare("select count(*) from serial where subscriptionid=?");
-    $sth->execute($subscriptionid);
-    my ($totalissues) = $sth->fetchrow;
-    return ( $totalissues, @serials );
-}
-
 =head2 GetNextDate
 
 ($resultdate) = &GetNextDate($planneddate,$subscription)