bug 2263 - transfer zipcode when selecting guarantor
[koha_fer] / C4 / Serials.pm
index 1fffe9e..4bb4716 100644 (file)
@@ -19,44 +19,25 @@ 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 C4::Debug;
 
 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 +57,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
@@ -244,7 +240,7 @@ sub GetSerialInformation {
     my ($serialid) = @_;
     my $dbh        = C4::Context->dbh;
     my $query      = qq|
-        SELECT serial.*, serial.notes as sernotes, serial.status as serstatus,subscription.*,subscription.subscriptionid as subsid|;
+        SELECT serial.*, serial.notes as sernotes, serial.status as serstatus,subscription.*,subscription.subscriptionid as subsid |;
        if (C4::Context->preference('IndependantBranches') && 
               C4::Context->userenv && 
               C4::Context->userenv->{'flags'} != 1 && C4::Context->userenv->{'branch'}){
@@ -258,15 +254,15 @@ sub GetSerialInformation {
     my $rq = $dbh->prepare($query);
     $rq->execute($serialid);
     my $data = $rq->fetchrow_hashref;
-
-    if ( C4::Context->preference("serialsadditems") ) {
+    # 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) {
 
                 #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, bibnum :".$data->{'biblionumber'};
                 my $itemprocessed =
                   PrepareItemrecordDisplay( $data->{'biblionumber'}, $itemnum );
                 $itemprocessed->{'itemnumber'}   = $itemnum;
@@ -307,12 +303,8 @@ Adds an itemnumber to Serial record
 sub AddItem2Serial {
     my ( $serialid, $itemnumber ) = @_;
     my $dbh   = C4::Context->dbh;
-    my $query = qq|
-        UPDATE serial SET itemnumber=IF(itemnumber IS NULL, $itemnumber, CONCAT(itemnumber,",",$itemnumber))
-        WHERE  serialid = ?
-    |;
-    my $rq = $dbh->prepare($query);
-    $rq->execute($serialid);
+    my $rq = $dbh->prepare("INSERT INTO `serialitems` SET serialid=? , itemnumber=?");
+    $rq->execute($serialid, $itemnumber);
     return $rq->rows;
 }
 
@@ -361,6 +353,7 @@ sub GetSubscription {
     my $query            = qq(
         SELECT  subscription.*,
                 subscriptionhistory.*,
+                subscriptionhistory.enddate as histenddate,
                 aqbudget.bookfundid,
                 aqbooksellers.name AS aqbooksellername,
                 biblio.title AS bibliotitle,
@@ -382,15 +375,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
@@ -437,11 +428,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({});
 }
 
 
@@ -527,6 +517,7 @@ sub GetSubscriptionsFromBiblionumber {
         SELECT subscription.*,
                branches.branchname,
                subscriptionhistory.*,
+               subscriptionhistory.enddate as histenddate, 
                aqbudget.bookfundid,
                aqbooksellers.name AS aqbooksellername,
                biblio.title AS bibliotitle
@@ -549,6 +540,7 @@ sub GetSubscriptionsFromBiblionumber {
     while ( my $subs = $sth->fetchrow_hashref ) {
         $subs->{startdate}     = format_date( $subs->{startdate} );
         $subs->{histstartdate} = format_date( $subs->{histstartdate} );
+        $subs->{histenddate} = format_date( $subs->{histenddate} );
         $subs->{opacnote}     =~ s/\n/\<br\/\>/g;
         $subs->{missinglist}  =~ s/\n/\<br\/\>/g;
         $subs->{recievedlist} =~ s/\n/\<br\/\>/g;
@@ -620,8 +612,7 @@ sub GetFullSubscriptionsFromBiblionumber {
           |;
     my $sth = $dbh->prepare($query);
     $sth->execute($biblionumber);
-    my $subs= $sth->fetchall_arrayref({});
-    return $subs;
+    return $sth->fetchall_arrayref({});
 }
 
 =head2 GetSubscriptions
@@ -651,7 +642,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);
     }
@@ -664,6 +655,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 );
         }
@@ -677,7 +669,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 . "%" );
             }
@@ -691,7 +683,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;
             }
@@ -704,13 +696,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  && 
@@ -803,7 +794,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;
@@ -1245,7 +1236,7 @@ sub ModSubscription {
         $whenmorethan3,   $setto3,       $lastvalue3,     $innerloop3,
         $numberingmethod, $status,       $biblionumber,   $callnumber,
         $notes,           $letter,       $hemisphere,     $manualhistory,
-        $internalnotes,
+        $internalnotes,   $serialsadditems,
         $subscriptionid
     ) = @_;
 #     warn $irregularity;
@@ -1256,7 +1247,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=?
+                        numberingmethod=?, status=?, biblionumber=?, callnumber=?, notes=?, letter=?, hemisphere=?,manualhistory=?,internalnotes=?,serialsadditems=?
                     WHERE subscriptionid = ?";
 #     warn "query :".$query;
     my $sth = $dbh->prepare($query);
@@ -1271,14 +1262,13 @@ sub ModSubscription {
         $whenmorethan3,   $setto3,       $lastvalue3,     $innerloop3,
         $numberingmethod, $status,       $biblionumber,   $callnumber,
         $notes,           $letter,       $hemisphere,     ($manualhistory?$manualhistory:0),
-        $internalnotes,
+        $internalnotes,   $serialsadditems,
         $subscriptionid
     );
     my $rows=$sth->rows;
     $sth->finish;
     
-    &logaction(C4::Context->userenv->{'number'},"SERIAL","MODIFY",$subscriptionid,"") 
-        if C4::Context->preference("SubscriptionLog");
+    logaction("SERIAL", "MODIFY", $subscriptionid, "") if C4::Context->preference("SubscriptionLog");
     return $rows;
 }
 
@@ -1291,7 +1281,7 @@ $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)
+    $numberingmethod, $status, $notes, $serialsadditems)
 
 Create a new subscription with value given on input args.
 
@@ -1314,7 +1304,7 @@ sub NewSubscription {
         $lastvalue3,    $innerloop3,   $numberingmethod, $status,
         $notes,         $letter,       $firstacquidate,  $irregularity,
         $numberpattern, $callnumber,   $hemisphere,      $manualhistory,
-        $internalnotes
+        $internalnotes, $serialsadditems,
     ) = @_;
     my $dbh = C4::Context->dbh;
 
@@ -1327,8 +1317,8 @@ 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)
-        VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
+            numberpattern, callnumber, hemisphere,manualhistory,internalnotes,serialsadditems)
+        VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
         |;
     my $sth = $dbh->prepare($query);
     $sth->execute(
@@ -1352,20 +1342,20 @@ sub NewSubscription {
         format_date_in_iso($firstacquidate),                $irregularity,
         $numberpattern,                 $callnumber,
         $hemisphere,                    $manualhistory,
-        $internalnotes
+        $internalnotes,                 $serialsadditems,
     );
 
     #then create the 1st waited number
     my $subscriptionid = $dbh->{'mysql_insertid'};
     $query             = qq(
         INSERT INTO subscriptionhistory
-            (biblionumber, subscriptionid, histstartdate, enddate, missinglist, recievedlist, opacnote, librariannote)
-        VALUES (?,?,?,?,?,?,?,?)
+            (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(
@@ -1391,9 +1381,20 @@ sub NewSubscription {
         format_date_in_iso($startdate)
     );
     
-    &logaction(C4::Context->userenv->{'number'},"SERIAL","ADD",$subscriptionid,"") 
-        if C4::Context->preference("SubscriptionLog");
+    logaction("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) {
+            eval {
+            $record->field($tag)->update( $subf => 1 );
+            };
+        }
+        ModBiblio($record,$biblionumber,$bib->{'frameworkcode'});
+    }    
     return $subscriptionid;
 }
 
@@ -1424,27 +1425,28 @@ sub ReNewSubscription {
      my $sth = $dbh->prepare($query);
      $sth->execute( $subscription->{biblionumber} );
      my $biblio = $sth->fetchrow_hashref;
-     NewSuggestion(
-         $user,             $subscription->{bibliotitle},
-         $biblio->{author}, $biblio->{publishercode},
-         $biblio->{note},   '',
-         '',                '',
-         '',                '',
-         $subscription->{biblionumber}
-     );
+     if (C4::Context->preference("RenewSerialAddsSuggestion")){
+        NewSuggestion(
+            $user,             $subscription->{bibliotitle},
+            $biblio->{author}, $biblio->{publishercode},
+            $biblio->{note},   '',
+            '',                '',
+            '',                '',
+            $subscription->{biblionumber}
+        );
+    }
 
     # 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 );
         
-    &logaction(C4::Context->userenv->{'number'},"SERIAL","RENEW",$subscriptionid,"") 
-        if C4::Context->preference("SubscriptionLog");
+    logaction("SERIAL", "RENEW", $subscriptionid, "") if C4::Context->preference("SubscriptionLog");
 }
 
 =head2 NewIssue
@@ -1698,7 +1700,7 @@ sub ItemizeSerials {
                     $marcrecord->insert_fields_ordered($newField);
                 }
             }
-            AddItem( $marcrecord, $data->{'biblionumber'} );
+            AddItemFromMarc( $marcrecord, $data->{'biblionumber'} );
             return 1;
         }
         return ( 0, @errors );
@@ -1750,7 +1752,7 @@ sub HasSubscriptionExpired {
              return 0;
       }
     }
-    return 0;
+    return 0;  # Notice that you'll never get here.
 }
 
 =head2 SetDistributedto
@@ -1796,8 +1798,7 @@ sub DelSubscription {
         "DELETE FROM subscriptionhistory WHERE subscriptionid=$subscriptionid");
     $dbh->do("DELETE FROM serial WHERE subscriptionid=$subscriptionid");
     
-    &logaction(C4::Context->userenv->{'number'},"SERIAL","DELETE",$subscriptionid,"") 
-        if C4::Context->preference("SubscriptionLog");
+    logaction("SERIAL", "DELETE", $subscriptionid, "") if C4::Context->preference("SubscriptionLog");
 }
 
 =head2 DelIssue
@@ -2311,7 +2312,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 +2596,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 +2703,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 +2735,8 @@ sub itemdata {
     return ($data);
 }
 
-END { }    # module clean-up code here (global destructor)
-
 1;
+__END__
 
 =back