Serials updates to link item record to serial table.
[koha_gimpoz] / C4 / Serials.pm
index 1fffe9e..e0f260c 100644 (file)
@@ -19,44 +19,24 @@ package C4::Serials;    #assumes C4/Serials.pm
 
 
 use strict;
-use C4::Date;
+use C4::Dates qw(format_date format_date_in_iso);
 use Date::Calc qw(:all);
 use POSIX qw(strftime);
 use C4::Suggestions;
 use C4::Koha;
 use C4::Biblio;
+use C4::Items;
 use C4::Search;
 use C4::Letters;
 use C4::Log; # logaction
 
-require Exporter;
-
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
 
-# set the version for version checking
-$VERSION = do { my @v = '$Revision$' =~ /\d+/g;
-    shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v );
-};
-
-=head1 NAME
-
-C4::Serials - Give functions for serializing.
-
-=head1 SYNOPSIS
-
-  use C4::Serials;
-
-=head1 DESCRIPTION
-
-Give all XYZ functions
-
-=head1 FUNCTIONS
-
-=cut
-
-@ISA    = qw(Exporter);
-@EXPORT = qw(
-    
+BEGIN {
+       $VERSION = 3.01;        # set version for version checking
+       require Exporter;
+       @ISA    = qw(Exporter);
+       @EXPORT = qw(
     &NewSubscription    &ModSubscription    &DelSubscription    &GetSubscriptions
     &GetSubscription    &CountSubscriptionFromBiblionumber      &GetSubscriptionsFromBiblionumber
     &GetFullSubscriptionsFromBiblionumber   &GetFullSubscription &ModSubscriptionHistory
@@ -76,10 +56,25 @@ Give all XYZ functions
     &check_routing &updateClaim &removeMissingIssue
     
     &old_newsubscription &old_modsubscription &old_getserials
-);
+       );
+}
 
 =head2 GetSuppliersWithLateIssues
 
+=head1 NAME
+
+C4::Serials - Give functions for serializing.
+
+=head1 SYNOPSIS
+
+  use C4::Serials;
+
+=head1 DESCRIPTION
+
+Give all XYZ functions
+
+=head1 FUNCTIONS
+
 =over 4
 
 %supplierlist = &GetSuppliersWithLateIssues
@@ -1359,13 +1354,13 @@ sub NewSubscription {
     my $subscriptionid = $dbh->{'mysql_insertid'};
     $query             = qq(
         INSERT INTO subscriptionhistory
-            (biblionumber, subscriptionid, histstartdate, enddate, missinglist, recievedlist, opacnote, librariannote)
+            (biblionumber, subscriptionid, histstartdate,  opacnote, librariannote)
         VALUES (?,?,?,?,?,?,?,?)
         );
     $sth = $dbh->prepare($query);
     $sth->execute( $biblionumber, $subscriptionid,
         format_date_in_iso($startdate),
-        0, "", "", "", "$notes" );
+        $notes,$internalnotes );
 
    # reread subscription to get a hash (for calculation of the 1st issue number)
     $query = qq(
@@ -1394,6 +1389,16 @@ sub NewSubscription {
     &logaction(C4::Context->userenv->{'number'},"SERIAL","ADD",$subscriptionid,"") 
         if C4::Context->preference("SubscriptionLog");
     
+#set serial flag on biblio if not already set.
+    my ($null, ($bib)) = GetBiblio($biblionumber);
+    if( ! $bib->{'serial'} ) {
+        my $record = GetMarcBiblio($biblionumber);
+        my ($tag,$subf) = GetMarcFromKohaField('biblio.serial',$bib->{'frameworkcode'});
+        if($tag) {
+            $record->field($tag)->update( $subf => 1 );
+        }
+        ModBiblio($record,$biblionumber,$bib->{'frameworkcode'});
+    }    
     return $subscriptionid;
 }
 
@@ -1434,12 +1439,12 @@ sub ReNewSubscription {
      );
 
     # renew subscription
-    my $query = qq|
+    $query = qq|
         UPDATE subscription
         SET    startdate=?,numberlength=?,weeklength=?,monthlength=?
         WHERE  subscriptionid=?
     |;
-    my $sth = $dbh->prepare($query);
+    $sth = $dbh->prepare($query);
     $sth->execute( format_date_in_iso($startdate),
         $numberlength, $weeklength, $monthlength, $subscriptionid );
         
@@ -1698,7 +1703,7 @@ sub ItemizeSerials {
                     $marcrecord->insert_fields_ordered($newField);
                 }
             }
-            AddItem( $marcrecord, $data->{'biblionumber'} );
+            AddItemFromMarc( $marcrecord, $data->{'biblionumber'} );
             return 1;
         }
         return ( 0, @errors );
@@ -2311,7 +2316,7 @@ sub abouttoexpire {
       if ( $per == 10 ) { $x = 365; }
       if ( $per == 11 ) { $x = 730; }
       my @datebeforeend=Add_Delta_Days(  $endofsubscriptiondate[0],$endofsubscriptiondate[1],$endofsubscriptiondate[2],
-                    - (3 * $x)) if (@endofsubscriptiondate);
+                    - (3 * $x)) if (@endofsubscriptiondate && $endofsubscriptiondate[0]*$endofsubscriptiondate[1]*$endofsubscriptiondate[2]);
               # warn "DATE BEFORE END: $datebeforeend";
       return 1 if ( @res && 
                     (@datebeforeend && 
@@ -2595,65 +2600,78 @@ sub GetNextDate(@) {
       return 0;
     }  
     if ( $subscription->{periodicity} == 1 ) {
-        my $dayofweek = Day_of_Week( $year,$month, $day );
-        for ( my $i = 0 ; $i < @irreg ; $i++ ) {
-            $dayofweek = 0 if ( $dayofweek == 7 ); 
-            if ( in_array( ($dayofweek + 1), @irreg ) ) {
-                ($year,$month,$day) = Add_Delta_Days($year,$month, $day , 1 );
-                $dayofweek++;
-            }
-        }
-        @resultdate = Add_Delta_Days($year,$month, $day , 1 );
+        my $dayofweek = eval{Day_of_Week( $year,$month, $day )};
+        if ($@){warn "year month day : $year $month $day $subscription->{subscriptionid} : $@";}
+        else {    
+          for ( my $i = 0 ; $i < @irreg ; $i++ ) {
+              $dayofweek = 0 if ( $dayofweek == 7 ); 
+              if ( in_array( ($dayofweek + 1), @irreg ) ) {
+                  ($year,$month,$day) = Add_Delta_Days($year,$month, $day , 1 );
+                  $dayofweek++;
+              }
+          }
+          @resultdate = Add_Delta_Days($year,$month, $day , 1 );
+        }    
     }
     if ( $subscription->{periodicity} == 2 ) {
-        my ($wkno,$year) = Week_of_Year( $year,$month, $day );
-        for ( my $i = 0 ; $i < @irreg ; $i++ ) {
-            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);
-            }
-        }
-        @resultdate = Add_Delta_Days( $year,$month, $day, 7);
+        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++ ) {
+              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);
+              }
+          }
+          @resultdate = Add_Delta_Days( $year,$month, $day, 7);
+        }        
     }
-    if ( $subscription->{periodicity} == 3 ) {
-        my ($wkno,$year) = Week_of_Year( $year,$month, $day );
-        for ( my $i = 0 ; $i < @irreg ; $i++ ) {
-            if ( $irreg[$i] == (($wkno!=50)?($wkno +2) % 52 :52)) {
-            ### BUGFIX was previously +1 ^
-                ($year,$month,$day) = Add_Delta_Days($year,$month, $day , 14 );
-                $wkno=(($wkno!=50)?($wkno +2) % 52 :52);
-            }
-        }
-        @resultdate = Add_Delta_Days($year,$month, $day , 14 );
+    if ( $subscription->{periodicity} == 3 ) {        
+        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++ ) {
+              if ( $irreg[$i] == (($wkno!=50)?($wkno +2) % 52 :52)) {
+              ### BUGFIX was previously +1 ^
+                  ($year,$month,$day) = Add_Delta_Days($year,$month, $day , 14 );
+                  $wkno=(($wkno!=50)?($wkno +2) % 52 :52);
+              }
+          }
+          @resultdate = Add_Delta_Days($year,$month, $day , 14 );
+        }        
     }
     if ( $subscription->{periodicity} == 4 ) {
-        my ($wkno,$year) = Week_of_Year( $year,$month, $day );
-        for ( my $i = 0 ; $i < @irreg ; $i++ ) {
-            if ( $irreg[$i] == (($wkno!=49)?($wkno +3) % 52 :52)) {
-                ($year,$month,$day) = Add_Delta_Days($year,$month, $day , 21 );
-                $wkno=(($wkno!=49)?($wkno +3) % 52 :52);
-            }
-        }
-        @resultdate = Add_Delta_Days($year,$month, $day , 21 );
+        my ($wkno,$year) = eval {Week_of_Year( $year,$month, $day )};
+        if ($@){warn "annĂ©e mois jour : $year $month $day $subscription->{subscriptionid} : $@";}
+        else {    
+          for ( my $i = 0 ; $i < @irreg ; $i++ ) {
+              if ( $irreg[$i] == (($wkno!=49)?($wkno +3) % 52 :52)) {
+                  ($year,$month,$day) = Add_Delta_Days($year,$month, $day , 21 );
+                  $wkno=(($wkno!=49)?($wkno +3) % 52 :52);
+              }
+          }
+          @resultdate = Add_Delta_Days($year,$month, $day , 21 );
+        }        
     }
     my $tmpmonth=$month;
+    if ($year && $month && $day){
     if ( $subscription->{periodicity} == 5 ) {
-        for ( my $i = 0 ; $i < @irreg ; $i++ ) {
-            if ( $irreg[$i] == (($tmpmonth!=11)?($tmpmonth +1) % 12 :12)) {
-                ($year,$month,$day) = Add_Delta_YMD($year,$month, $day ,0,1,0 );
-                $tmpmonth=(($tmpmonth!=11)?($tmpmonth +1) % 12 :12);
-            }
-        }
-        @resultdate = Add_Delta_YMD($year,$month, $day ,0,1,0 );
+          for ( my $i = 0 ; $i < @irreg ; $i++ ) {
+              if ( $irreg[$i] == (($tmpmonth!=11)?($tmpmonth +1) % 12 :12)) {
+                  ($year,$month,$day) = Add_Delta_YMD($year,$month, $day ,0,1,0 );
+                  $tmpmonth=(($tmpmonth!=11)?($tmpmonth +1) % 12 :12);
+              }
+          }        
+          @resultdate = Add_Delta_YMD($year,$month, $day ,0,1,0 );
     }
     if ( $subscription->{periodicity} == 6 ) {
-        for ( my $i = 0 ; $i < @irreg ; $i++ ) {
-            if ( $irreg[$i] == (($tmpmonth!=10)?($tmpmonth +2) % 12 :12)) {
-                ($year,$month,$day) = Add_Delta_YMD($year,$month, $day ,0,2,0 );
-                $tmpmonth=(($tmpmonth!=10)?($tmpmonth + 2) % 12 :12);
-            }
-        }
-        @resultdate = Add_Delta_YMD($year,$month, $day, 0, 2,0 );
+          for ( my $i = 0 ; $i < @irreg ; $i++ ) {
+              if ( $irreg[$i] == (($tmpmonth!=10)?($tmpmonth +2) % 12 :12)) {
+                  ($year,$month,$day) = Add_Delta_YMD($year,$month, $day ,0,2,0 );
+                  $tmpmonth=(($tmpmonth!=10)?($tmpmonth + 2) % 12 :12);
+              }
+          }
+          @resultdate = Add_Delta_YMD($year,$month, $day, 0, 2,0 );
     }
     if ( $subscription->{periodicity} == 7 ) {
         for ( my $i = 0 ; $i < @irreg ; $i++ ) {
@@ -2689,7 +2707,9 @@ sub GetNextDate(@) {
     if ( $subscription->{periodicity} == 11 ) {
         @resultdate = Add_Delta_YM($year,$month, $day, 2, 0 );
     }
+    }  
     my $resultdate=sprintf("%04d-%02d-%02d",$resultdate[0],$resultdate[1],$resultdate[2]);
+      
 #     warn "dateNEXTSEQ : ".$resultdate;
     return "$resultdate";
 }
@@ -2719,9 +2739,8 @@ sub itemdata {
     return ($data);
 }
 
-END { }    # module clean-up code here (global destructor)
-
 1;
+__END__
 
 =back