[Koha-patches] [PATCH] Serials planning: Update subscription edit to properly deal with date changes.

Ryan Higgins rch at liblime.com
Sun Jul 27 20:28:49 CEST 2008


Previously subscription-add.pl allowed modification of 'firstacquidate', which changed
the subscription definition, but did not affect prediction.  This patch adds two fuctions
to Serials.pm to get/set the current expected issue date (note that all date calculations
in prediction patterns are based on the current expected date, and there's only one serial
issue per subscription in the 'expected' status at any time).  Subscription editing thus
now allows you to edit the next expected date, but not the first acqui date (unless you
haven't received any issues yet), thus allowing for adjustments in the prediction pattern.
Irregularities still aren't handled properly for edits to subscriptions ( i.e., they still
_work_, but the javascript irregularity display won't calculate the correct dates).
---
 C4/Serials.pm                                      |   60 +++++++++++++++++++-
 .../prog/en/modules/serials/subscription-add.tmpl  |    4 +-
 serials/subscription-add.pl                        |   30 +++++++---
 3 files changed, 82 insertions(+), 12 deletions(-)

diff --git a/C4/Serials.pm b/C4/Serials.pm
index ade4103..b51f4b9 100644
--- a/C4/Serials.pm
+++ b/C4/Serials.pm
@@ -47,7 +47,7 @@ BEGIN {
     &GetLatestSerials   &ModSerialStatus    &GetNextDate       &GetSerials2
     &ReNewSubscription  &GetLateIssues      &GetLateOrMissingIssues
     &GetSerialInformation                   &AddItem2Serial
-    &PrepareSerialsData
+    &PrepareSerialsData &GetNextExpected    &ModNextExpected
     
     &UpdateClaimdateIssues
     &GetSuppliersWithLateIssues             &getsupplierbyserialid
@@ -722,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
@@ -1215,6 +1216,63 @@ 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) = @_;
+    warn $subscriptionid;
+    warn $date->output('iso');
+    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
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tmpl
index 87c9bd3..ff43f9c 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tmpl
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tmpl
@@ -1028,10 +1028,10 @@ addLoadEvent(irregularity_check);
 	<legend>Serials planning</legend>
     <ol>
         <li>
-           <label for="firstacquidate"> First issue publication date:</label>
+           <label for="firstacquidate"> <!-- TMPL_IF NAME="mod" -->Next<!-- TMPL_ELSE -->First<!-- /TMPL_IF --> issue publication date:</label>
             
                 <img src="<!-- TMPL_VAR Name="themelang" -->/lib/calendar/cal.gif" id="button2" style="cursor: pointer;" alt="Show Calendar" title="Show Calendar" />
-                <input type="text" name="firstacquidate" value="<!-- TMPL_VAR name="firstacquidate" -->" size="13" maxlength="10" id="acqui_date" style="border-width: 0px;" onchange='JavaScript:update_weeks(); return false;'  />
+                <input type="text" name="firstacquidate" <!-- TMPL_IF NAME="mod" -->value="<!-- TMPL_VAR name="firstacquidate" -->" <!-- TMPL_ELSE --> value="<!-- TMPL_VAR name="nextacquidate" -->"<!-- /TMPL_IF -->  id="acqui_date" size="13" maxlength="10" style="border-width: 0px;" onchange='JavaScript:update_weeks(); return false;'  />
                 <!-- both scripts for calendar must follow the input field --> 
                 <script type="text/javascript">
                     Calendar.setup({
diff --git a/serials/subscription-add.pl b/serials/subscription-add.pl
index cb64725..af95250 100755
--- a/serials/subscription-add.pl
+++ b/serials/subscription-add.pl
@@ -92,7 +92,9 @@ $template->param(branchloop => \@branchloop,
 my $subscriptionid;
 my $subs;
 my $firstissuedate;
-if ($op eq 'mod'||$op eq 'dup') {
+my $nextexpected;
+
+if ($op eq 'mod' || $op eq 'dup' || $op eq 'modsubscription') {
 
     $subscriptionid = $query->param('subscriptionid');
     $subs = &GetSubscription($subscriptionid);
@@ -101,8 +103,8 @@ if ($op eq 'mod'||$op eq 'dup') {
       warn "Attempt to modify subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
       print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
     } 
-$firstissuedate = $subs->{firstacquidate};  # in iso format.
-for (qw(startdate firstacquidate histstartdate enddate histenddate)) {
+    $firstissuedate = $subs->{firstacquidate};  # in iso format.
+    for (qw(startdate firstacquidate histstartdate enddate histenddate)) {
 	# TODO : Handle date formats properly.
          if ($subs->{$_} eq '0000-00-00') {
             $subs->{$_} = ''
@@ -113,8 +115,12 @@ for (qw(startdate firstacquidate histstartdate enddate histenddate)) {
     $subs->{'letter'}='' unless($subs->{'letter'});
     $irregularity   = $subs->{'irregularity'};
     $numberpattern  = $subs->{'numberpattern'};
+    $nextexpected = GetNextExpected($subscriptionid);
+    $nextexpected->{ isfirstissue => ($nextexpected->{planneddate}->output('iso') eq $firstissuedate)};
+    # firstacquidate is taken to be the upcoming issue's planned date if we're modifying the sub
+    $subs->{firstacquidate} = $nextexpected->{planneddate}->output()  if($op eq 'mod');
 
-
+  unless($op eq 'modsubscription') {
     if($subs->{numberlength} > 0){
         $sublength = $subs->{numberlength};
         $sub_on = $subscription_types[0];
@@ -148,10 +154,11 @@ for (qw(startdate firstacquidate histstartdate enddate histenddate)) {
                 "dow".$subs->{'dow'} => 1,
                 "numberpattern".$subs->{'numberpattern'} => 1,
                 );
+  }
 }
+
 my $weekarrayjs='';
 my $count = 0;
-# FIXME: 
 my ($year, $month, $day) = ($firstissuedate) ? split(/-/,$firstissuedate) : Today ;
 my $firstday   =  Day_of_Year($year,$month,$day);
 my ($wkno,$yr) = Week_of_Year($year,$month,$day); # week starting monday
@@ -163,7 +170,6 @@ for(my $i=$firstday;$i<($firstday+365);$i=$i+7){
         $wkno++;    
 }
 chop($weekarrayjs);
-# warn $weekarrayjs;
 
 if ($op eq 'addsubscription') {
     my $auser = $query->param('user');
@@ -290,14 +296,20 @@ if ($op eq 'addsubscription') {
     my $opacnote = $query->param('opacnote');
     my $librariannote = $query->param('librariannote');
     my $history_only = $query->param('history_only');
-	# FIXME:  If it's  a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
-	#
+	#  If it's  a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
+    #  Here firstacquidate is interpreted as nextacquidate.	
+
+    if ( $firstacquidate ne $nextexpected->{planneddate}->output('iso') ) {
+        ModNextExpected($subscriptionid,C4::Dates->new($firstacquidate,'iso'));
+        $firstissuedate = $nextexpected->{planneddate}->output('iso') if($nextexpected->{isfirstissue});
+    }
+    
     if ($history_only) {
         ModSubscriptionHistory ($subscriptionid,$histstartdate,$histenddate,$recievedlist,$missinglist,$opacnote,$librariannote);
     } else {
         &ModSubscription(
             $auser,           $branchcode,   $aqbooksellerid, $cost,
-            $aqbudgetid,      $startdate,    $periodicity,    $firstacquidate,
+            $aqbudgetid,      $startdate,    $periodicity,    $firstissuedate,
             $dow,             join(",", at irregularity), $numberpattern,  $numberlength,
             $weeklength,      $monthlength,  $add1,           $every1,
             $whenmorethan1,   $setto1,       $lastvalue1,     $innerloop1,
-- 
1.5.5.GIT




More information about the Koha-patches mailing list