From 63f345bb0e9cbe7660f5714f5d1208b1be9d20cc Mon Sep 17 00:00:00 2001
From: Kyle M Hall <kyle@bywatersolutions.com>
Date: Wed, 24 Oct 2012 12:46:51 -0400
Subject: [PATCH] Bug 8956 - Split serials enumeration data into separate fields - Part 2 - Store x, y and z in DB when generating new serials

---
 C4/Serials.pm                 |   55 ++++++++++++++++++++++------------------
 serials/serials-collection.pl |    2 +-
 2 files changed, 31 insertions(+), 26 deletions(-)

diff --git a/C4/Serials.pm b/C4/Serials.pm
index 0b31a2b..8b5c697 100644
--- a/C4/Serials.pm
+++ b/C4/Serials.pm
@@ -926,7 +926,6 @@ sub GetNextSeq {
     $newlastvalue1 = $val->{lastvalue1};
     $newlastvalue2 = $val->{lastvalue2};
     $newlastvalue3 = $val->{lastvalue3};
-    $newlastvalue1 = $val->{lastvalue1};
 
     # check if we have to increase the new value.
     $newinnerloop1 = $val->{innerloop1} + 1;
@@ -935,8 +934,6 @@ sub GetNextSeq {
     $newlastvalue1 = $val->{setto1} if ( $newlastvalue1 > $val->{whenmorethan1} );    # reset counter if needed.
     $calculated =~ s/\{X\}/$newlastvalue1/g;
 
-    $newlastvalue2 = $val->{lastvalue2};
-
     # check if we have to increase the new value.
     $newinnerloop2 = $val->{innerloop2} + 1;
     $newinnerloop2 = 0 if ( $newinnerloop2 >= $val->{every2} );
@@ -977,29 +974,36 @@ the sequence in integer format
 =cut
 
 sub GetSeq {
-    my ($val) = @_;
-    my $pattern = $val->{numberpattern};
     my @seasons          = ( 'nothing', 'Winter', 'Spring', 'Summer', 'Autumn' );
     my @southern_seasons = ( '',        'Summer', 'Autumn', 'Winter', 'Spring' );
+
+    my ($val) = @_;
+    my $pattern = $val->{numberpattern};
     my $calculated       = $val->{numberingmethod};
     my $x                = $val->{'lastvalue1'};
-    $calculated =~ s/\{X\}/$x/g;
-    my $newlastvalue2 = $val->{'lastvalue2'};
+    my $y                = $val->{'lastvalue2'};
+    my $z                = $val->{'lastvalue3'};
 
-    if ( $pattern == 6 ) {
+    if ( $pattern == 6 ) { ## Seasons
         if ( $val->{hemisphere} == 2 ) {
-            my $newlastvalue2seq = $southern_seasons[$newlastvalue2];
-            $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
+            $y = $southern_seasons[$y];
         } else {
-            my $newlastvalue2seq = $seasons[$newlastvalue2];
-            $calculated =~ s/\{Y\}/$newlastvalue2seq/g;
+            $y = $seasons[$y];
         }
-    } else {
-        $calculated =~ s/\{Y\}/$newlastvalue2/g;
     }
-    my $z = $val->{'lastvalue3'};
+
+    $calculated =~ s/\{X\}/$x/g;
+    $calculated =~ s/\{Y\}/$y/g;
     $calculated =~ s/\{Z\}/$z/g;
-    return $calculated;
+
+    my $x_label = $val->{'serialseq_x_label'};
+    my $y_label = $val->{'serialseq_y_label'};
+    my $z_label = $val->{'serialseq_z_label'};
+    $calculated =~ s/\{XL\}/$x_label/g;
+    $calculated =~ s/\{YL\}/$y_label/g;
+    $calculated =~ s/\{ZL\}/$z_label/g;
+
+    return ( $calculated, $x, $y, $z );
 }
 
 =head2 GetExpirationDate
@@ -1168,7 +1172,7 @@ sub ModSerialStatus {
 
         # next date (calculated from actual date & frequency parameters)
         my $nextpublisheddate = GetNextDate( $publisheddate, $val );
-        NewIssue( $newserialseq, $subscriptionid, $val->{'biblionumber'}, 1, $nextpublisheddate, $nextpublisheddate );
+        NewIssue( $newserialseq, $subscriptionid, $val->{'biblionumber'}, 1, $nextpublisheddate, $nextpublisheddate, undef, $newlastvalue1, $newlastvalue2, $newlastvalue3 );
         $query = "UPDATE subscription SET lastvalue1=?, lastvalue2=?, lastvalue3=?, innerloop1=?, innerloop2=?, innerloop3=?
                     WHERE  subscriptionid = ?";
         $sth = $dbh->prepare($query);
@@ -1379,14 +1383,14 @@ sub NewSubscription {
     my $val = $sth->fetchrow_hashref;
 
     # calculate issue number
-    my $serialseq = GetSeq($val);
+    my ( $serialseq, $x, $y, $z ) = GetSeq($val);
     $query = qq|
         INSERT INTO serial
-            (serialseq,subscriptionid,biblionumber,status, planneddate, publisheddate)
-        VALUES (?,?,?,?,?,?)
+            (serialseq,serialseq_x,serialseq_y,serialseq_z,subscriptionid,biblionumber,status, planneddate, publisheddate)
+        VALUES (?,?,?,?,?,?,?,?,?)
     |;
     $sth = $dbh->prepare($query);
-    $sth->execute( "$serialseq", $subscriptionid, $biblionumber, 1, $firstacquidate, $firstacquidate );
+    $sth->execute( $serialseq, $x, $y, $z, $subscriptionid, $biblionumber, 1, $firstacquidate, $firstacquidate );
 
     logaction( "SERIAL", "ADD", $subscriptionid, "" ) if C4::Context->preference("SubscriptionLog");
 
@@ -1478,17 +1482,18 @@ returns the serial id
 =cut
 
 sub NewIssue {
-    my ( $serialseq, $subscriptionid, $biblionumber, $status, $planneddate, $publisheddate, $notes ) = @_;
+    my ( $serialseq, $subscriptionid, $biblionumber, $status, $planneddate, $publisheddate, $notes, $x, $y, $z ) = @_;
+    warn "NewIssue( $serialseq, $subscriptionid, $biblionumber, $status, $planneddate, $publisheddate, $notes, $x, $y, $z )";
     ### FIXME biblionumber CAN be provided by subscriptionid. So Do we STILL NEED IT ?
 
     my $dbh   = C4::Context->dbh;
     my $query = qq|
         INSERT INTO serial
-            (serialseq,subscriptionid,biblionumber,status,publisheddate,planneddate,notes)
-        VALUES (?,?,?,?,?,?,?)
+            (serialseq,subscriptionid,biblionumber,status,publisheddate,planneddate,notes,serialseq_x,serialseq_y,serialseq_z)
+        VALUES (?,?,?,?,?,?,?,?,?,?)
     |;
     my $sth = $dbh->prepare($query);
-    $sth->execute( $serialseq, $subscriptionid, $biblionumber, $status, $publisheddate, $planneddate, $notes );
+    $sth->execute( $serialseq, $subscriptionid, $biblionumber, $status, $publisheddate, $planneddate, $notes, $x, $y, $z );
     my $serialid = $dbh->{'mysql_insertid'};
     $query = qq|
         SELECT missinglist,recievedlist
diff --git a/serials/serials-collection.pl b/serials/serials-collection.pl
index ad57e07..d02721b 100755
--- a/serials/serials-collection.pl
+++ b/serials/serials-collection.pl
@@ -79,7 +79,7 @@ if($op eq 'gennext' && @subscriptionid){
 	     my $nextpublisheddate = GetNextDate( $expected->{planneddate}->output('iso'), $subscription );
 	     ## Creating the new issue
 	     NewIssue( $newserialseq, $subscriptionid, $subscription->{'biblionumber'},
-	             1, $nextpublisheddate, $nextpublisheddate );
+               1, $nextpublisheddate, $nextpublisheddate, undef, $newlastvalue1, $newlastvalue2, $newlastvalue3 );
 
 	     ## Updating the subscription seq status
 	     my $squery = "UPDATE subscription SET lastvalue1=?, lastvalue2=?, lastvalue3=?, innerloop1=?, innerloop2=?, innerloop3=?
-- 
1.7.2.5