kohabug 2463 & 2464 Removing unlinked fields from label-edit-layout.pl
[koha_fer] / C4 / Serials.pm
index 487f1c0..768aa7c 100644 (file)
@@ -29,6 +29,7 @@ use C4::Items;
 use C4::Search;
 use C4::Letters;
 use C4::Log; # logaction
+use C4::Debug;
 
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
 
@@ -46,7 +47,7 @@ BEGIN {
     &GetLatestSerials   &ModSerialStatus    &GetNextDate       &GetSerials2
     &ReNewSubscription  &GetLateIssues      &GetLateOrMissingIssues
     &GetSerialInformation                   &AddItem2Serial
-    &PrepareSerialsData
+    &PrepareSerialsData &GetNextExpected    &ModNextExpected
     
     &UpdateClaimdateIssues
     &GetSuppliersWithLateIssues             &getsupplierbyserialid
@@ -255,17 +256,18 @@ sub GetSerialInformation {
     my $data = $rq->fetchrow_hashref;
     # create item information if we have serialsadditems for this subscription
     if ( $data->{'serialsadditems'} ) {
-        if ( $data->{'itemnumber'} ) {
-            my @itemnumbers = split /,/, $data->{'itemnumber'};
-            foreach my $itemnum (@itemnumbers) {
-
+        my $queryitem=$dbh->prepare("SELECT itemnumber from serialitems where serialid=?");
+        $queryitem->execute($serialid);
+        my $itemnumbers=$queryitem->fetchall_arrayref([0]);
+        if (scalar(@$itemnumbers)>0){
+            foreach my $itemnum (@$itemnumbers) {
                 #It is ASSUMED that GetMarcItem ALWAYS WORK...
                 #Maybe GetMarcItem should return values on failure
-#                 warn "itemnumber :$itemnum, bibnum :".$data->{'biblionumber'};
+                $debug and warn "itemnumber :$itemnum->[0], bibnum :".$data->{'biblionumber'};
                 my $itemprocessed =
-                  PrepareItemrecordDisplay( $data->{'biblionumber'}, $itemnum );
-                $itemprocessed->{'itemnumber'}   = $itemnum;
-                $itemprocessed->{'itemid'}       = $itemnum;
+                  PrepareItemrecordDisplay( $data->{'biblionumber'}, $itemnum->[0] , $data );
+                $itemprocessed->{'itemnumber'}   = $itemnum->[0];
+                $itemprocessed->{'itemid'}       = $itemnum->[0];
                 $itemprocessed->{'serialid'}     = $serialid;
                 $itemprocessed->{'biblionumber'} = $data->{'biblionumber'};
                 push @{ $data->{'items'} }, $itemprocessed;
@@ -273,7 +275,7 @@ sub GetSerialInformation {
         }
         else {
             my $itemprocessed =
-              PrepareItemrecordDisplay( $data->{'biblionumber'} );
+              PrepareItemrecordDisplay( $data->{'biblionumber'}, '', $data );
             $itemprocessed->{'itemid'}       = "N$serialid";
             $itemprocessed->{'serialid'}     = $serialid;
             $itemprocessed->{'biblionumber'} = $data->{'biblionumber'};
@@ -374,15 +376,13 @@ sub GetSubscription {
 #     if (C4::Context->preference('IndependantBranches') && 
 #         C4::Context->userenv && 
 #         C4::Context->userenv->{'flags'} != 1){
-# #       warn "flags: ".C4::Context->userenv->{'flags'};
+# #       $debug and warn "flags: ".C4::Context->userenv->{'flags'};
 #       $query.=" AND subscription.branchcode IN ('".C4::Context->userenv->{'branch'}."',\"\")";
 #     }
-#        warn "query : $query";
+       $debug and warn "query : $query\nsubsid :$subscriptionid";
     my $sth = $dbh->prepare($query);
-#       warn "subsid :$subscriptionid";
     $sth->execute($subscriptionid);
-    my $subs = $sth->fetchrow_hashref;
-    return $subs;
+    return $sth->fetchrow_hashref;
 }
 
 =head2 GetFullSubscription
@@ -429,11 +429,10 @@ sub GetFullSubscription {
           IF(serial.publisheddate="00-00-0000",serial.planneddate,serial.publisheddate) DESC,
           serial.subscriptionid
           |;
-#     warn $query;   
+       $debug and warn "GetFullSubscription query: $query";   
     my $sth = $dbh->prepare($query);
     $sth->execute($subscriptionid);
-    my $subs = $sth->fetchall_arrayref({});
-    return $subs;
+    return $sth->fetchall_arrayref({});
 }
 
 
@@ -614,8 +613,7 @@ sub GetFullSubscriptionsFromBiblionumber {
           |;
     my $sth = $dbh->prepare($query);
     $sth->execute($biblionumber);
-    my $subs= $sth->fetchall_arrayref({});
-    return $subs;
+    return $sth->fetchall_arrayref({});
 }
 
 =head2 GetSubscriptions
@@ -645,7 +643,7 @@ sub GetSubscriptions {
             WHERE biblio.biblionumber=?
         );
         $query.=" ORDER BY title";
-#         warn "query :$query";
+        $debug and warn "GetSubscriptions query: $query";
         $sth = $dbh->prepare($query);
         $sth->execute($biblionumber);
     }
@@ -658,6 +656,7 @@ sub GetSubscriptions {
                 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
                 WHERE (biblioitems.issn = ? or|. join('and ',map{"biblio.title LIKE \"%$_%\""}split (" ",$title))." )";
             $query.=" ORDER BY title";
+               $debug and warn "GetSubscriptions query: $query";
             $sth = $dbh->prepare($query);
             $sth->execute( $ISSN );
         }
@@ -671,7 +670,7 @@ sub GetSubscriptions {
                     WHERE biblioitems.issn LIKE ?
                 );
                 $query.=" ORDER BY title";
-#         warn "query :$query";
+                       $debug and warn "GetSubscriptions query: $query";
                 $sth = $dbh->prepare($query);
                 $sth->execute( "%" . $ISSN . "%" );
             }
@@ -685,7 +684,7 @@ sub GetSubscriptions {
                     ).($title?" and ":""). join('and ',map{"biblio.title LIKE \"%$_%\""} split (" ",$title) );
                 
                 $query.=" ORDER BY title";
-#                 warn $query;       
+                       $debug and warn "GetSubscriptions query: $query";
                 $sth = $dbh->prepare($query);
                 $sth->execute;
             }
@@ -698,13 +697,12 @@ sub GetSubscriptions {
         if ( $previoustitle eq $line->{title} ) {
             $line->{title}  = "";
             $line->{issn}   = "";
-            $line->{toggle} = 1 if $odd == 1;
         }
         else {
             $previoustitle = $line->{title};
             $odd           = -$odd;
-            $line->{toggle} = 1 if $odd == 1;
         }
+        $line->{toggle} = 1 if $odd == 1;
         $line->{'cannotedit'}=(C4::Context->preference('IndependantBranches') && 
                 C4::Context->userenv && 
                 C4::Context->userenv->{flags} !=1  && 
@@ -724,6 +722,7 @@ this function get every serial not arrived for a given subscription
 as well as the number of issues registered in the database (all types)
 this number is used to see if a subscription can be deleted (=it must have only 1 issue)
 
+FIXME: We should return \@serials.
 =back
 
 =cut
@@ -797,7 +796,7 @@ sub GetSerials2 {
                  WHERE    subscriptionid=$subscription AND status IN ($status)
                  ORDER BY publisheddate,serialid DESC
                     |;
-#     warn $query;
+       $debug and warn "GetSerials2 query: $query";
     my $sth=$dbh->prepare($query);
     $sth->execute;
     my @serials;
@@ -1217,6 +1216,61 @@ sub ModSerialStatus {
     }
 }
 
+=head2 GetNextExpected
+
+=over 4
+
+$nextexpected = GetNextExpected($subscriptionid)
+
+Get the planneddate for the current expected issue of the subscription.
+
+returns a hashref:
+
+$nextexepected = {
+    serialid => int
+    planneddate => C4::Dates object
+    }
+
+=back
+
+=cut
+
+sub GetNextExpected($) {
+    my ($subscriptionid) = @_;
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare('SELECT serialid, planneddate FROM serial WHERE subscriptionid=? AND status=?');
+    # Each subscription has only one 'expected' issue, with serial.status==1.
+    $sth->execute( $subscriptionid, 1 );
+    my ( $nextissue ) = $sth->fetchrow_hashref;
+    $nextissue->{planneddate} = C4::Dates->new($nextissue->{planneddate},'iso');
+    return $nextissue;
+}
+=head2 ModNextExpected
+
+=over 4
+
+ModNextExpected($subscriptionid,$date)
+
+Update the planneddate for the current expected issue of the subscription.
+This will modify all future prediction results.  
+
+C<$date> is a C4::Dates object.
+
+=back
+
+=cut
+
+sub ModNextExpected($$) {
+    my ($subscriptionid,$date) = @_;
+    my $dbh = C4::Context->dbh;
+    #FIXME: Would expect to only set planneddate, but we set both on new issue creation, so updating it here
+    my $sth = $dbh->prepare('UPDATE serial SET planneddate=?,publisheddate=? WHERE subscriptionid=? AND status=?');
+    # Each subscription has only one 'expected' issue, with serial.status==1.
+    $sth->execute( $date->output('iso'),$date->output('iso'), $subscriptionid, 1);
+    return 0;
+
+}
+
 =head2 ModSubscription
 
 =over 4
@@ -1380,8 +1434,8 @@ sub NewSubscription {
     $sth = $dbh->prepare($query);
     $sth->execute(
         "$serialseq", $subscriptionid, $biblionumber, 1,
-        format_date_in_iso($startdate),
-        format_date_in_iso($startdate)
+        format_date_in_iso($firstacquidate),
+        format_date_in_iso($firstacquidate)
     );
     
     logaction("SERIAL", "ADD", $subscriptionid, "") if C4::Context->preference("SubscriptionLog");
@@ -1749,13 +1803,13 @@ sub HasSubscriptionExpired {
     } else {
       if ($subscription->{'numberlength'}){
         my $countreceived=countissuesfrom($subscriptionid,$subscription->{'startdate'});
-       return 1 if ($countreceived >$subscription->{'numberlentgh'});
+       return 1 if ($countreceived >$subscription->{'numberlength'});
              return 0;
       } else {
              return 0;
       }
     }
-    return 0;
+    return 0;  # Notice that you'll never get here.
 }
 
 =head2 SetDistributedto
@@ -2595,10 +2649,13 @@ sub GetNextDate(@) {
     my @resultdate;
 
     #       warn "DOW $dayofweek";
-    if ( $subscription->{periodicity} % 16 == 0 ) {
+    if ( $subscription->{periodicity} % 16 == 0 ) {  # 'without regularity' || 'irregular'
       return 0;
     }  
-    if ( $subscription->{periodicity} == 1 ) {
+    #   daily : n / week
+    #   Since we're interpreting irregularity here as which days of the week to skip an issue,
+    #   renaming this pattern from 1/day to " n / week ".
+    if ( $subscription->{periodicity} == 1 ) {  
         my $dayofweek = eval{Day_of_Week( $year,$month, $day )};
         if ($@){warn "year month day : $year $month $day $subscription->{subscriptionid} : $@";}
         else {    
@@ -2612,11 +2669,13 @@ sub GetNextDate(@) {
           @resultdate = Add_Delta_Days($year,$month, $day , 1 );
         }    
     }
+    #   1  week
     if ( $subscription->{periodicity} == 2 ) {
         my ($wkno,$year) = eval {Week_of_Year( $year,$month, $day )};
         if ($@){warn "year month day : $year $month $day $subscription->{subscriptionid} : $@";}
         else {    
           for ( my $i = 0 ; $i < @irreg ; $i++ ) {
+          #FIXME: if two consecutive irreg, do we only skip one?
               if ( $irreg[$i] == (($wkno!=51)?($wkno +1) % 52 :52)) {
                   ($year,$month,$day) = Add_Delta_Days($year,$month, $day , 7 );
                   $wkno=(($wkno!=51)?($wkno +1) % 52 :52);
@@ -2625,6 +2684,7 @@ sub GetNextDate(@) {
           @resultdate = Add_Delta_Days( $year,$month, $day, 7);
         }        
     }
+    #   1 / 2 weeks
     if ( $subscription->{periodicity} == 3 ) {        
         my ($wkno,$year) = eval {Week_of_Year( $year,$month, $day )};
         if ($@){warn "year month day : $year $month $day $subscription->{subscriptionid} : $@";}
@@ -2639,6 +2699,7 @@ sub GetNextDate(@) {
           @resultdate = Add_Delta_Days($year,$month, $day , 14 );
         }        
     }
+    #   1 / 3 weeks
     if ( $subscription->{periodicity} == 4 ) {
         my ($wkno,$year) = eval {Week_of_Year( $year,$month, $day )};
         if ($@){warn "annĂ©e mois jour : $year $month $day $subscription->{subscriptionid} : $@";}