From 4f1c9cba6c9e93d07c6e66e5cb0753e7aa88eac2 Mon Sep 17 00:00:00 2001
From: Alex Buckley <alexbuckley@catalyst.net.nz>
Date: Mon, 27 Nov 2017 15:31:35 +1300
Subject: [PATCH] Bug 7046 - Implemented dropdown sub length element

To make this work I moved the _get_sub_length function from
subscription-add.pl to C4/Serials.pm so that the subscription-renew.pl
script could also call it to store the sublength for the appropriate
field of the subscriptions database table.

Test plan:
1. Create a subscription and notice that there is a dropdown box for sub
length containing the values: issues, weeks, months

2. Renew the subscription and notice that there are 3 input text boxes:
'number of num', 'number of weeks' and 'number of months'

3. Input a 'Number of weeks' value of 2

4. Query the subscription database table and notice that the value of 2
has been stored in the weeklength field for the subscription record you
just renewed

5. Apply the patch

6. Renew the subscription and notice that there is now a sublength dropdown box
containing issues, weeks and months

7. Set the month value to 3

8. Query the database and notice that 3 was stored in the monthlength
field for the subscription record

9. Create a new subscription and select the sub length values of issues
and 3

10. Query the database and notice that the numberlength field for the
subscription you just created is set to 3 showing that the sublength
dropbox is still working for creating a new subscription

Sponsored-By: Catalyst IT
---
 C4/Serials.pm                                      | 19 +++++++++++-
 .../prog/en/modules/serials/subscription-renew.tt  | 35 ++++++++++++++++++++--
 serials/subscription-add.pl                        | 14 ++-------
 serials/subscription-renew.pl                      |  9 ++++--
 4 files changed, 59 insertions(+), 18 deletions(-)

diff --git a/C4/Serials.pm b/C4/Serials.pm
index 45bae8c..0035f6d 100644
--- a/C4/Serials.pm
+++ b/C4/Serials.pm
@@ -1495,6 +1495,24 @@ sub NewSubscription {
     return $subscriptionid;
 }
 
+=head2 _get_sub_length
+my ($numberlength, $weeklength, $monthlength) = _get_sub_length( $subtype, $sublength );
+
+this function calculates the subscription length.
+
+=cut
+
+sub _get_sub_length {
+    my ($type, $subtype, $length) = @_;
+    return
+    (
+          $subtype eq 'issues' ? $length : 0,
+          $subtype eq 'weeks'   ? $length : 0,
+          $subtype eq 'months'  ? $length : 0,
+    );
+}
+
+
 =head2 ReNewSubscription
 
 ReNewSubscription($subscriptionid,$user,$startdate,$numberlength,$weeklength,$monthlength,$note)
@@ -1529,7 +1547,6 @@ sub ReNewSubscription {
             }
         );
     }
-
     # renew subscription
     $query = qq|
         UPDATE subscription
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-renew.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-renew.tt
index e7eb83d..96db6af 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-renew.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-renew.tt
@@ -32,9 +32,38 @@
             </li>
 		<li><fieldset>
 		<legend>Subscription length:</legend>
-		<ol><li><label for="numberlength">Number of num:</label><input type="text" id="numberlength" name="numberlength" value="[% numberlength %]" /></li>
-		<li><label for="weeklength">Number of weeks: </label><input type="text" id="weeklength" name="weeklength" value="[% weeklength %]" /></li>
-		<li><label for="monthlength">Number of months: </label><input type="text" id="monthlength" name="monthlength" value="[% monthlength %]" /></li></ol></fieldset></li>
+		<select name="subtype" id="subtype">
+        [% FOREACH st IN subtypes %]
+            [% SWITCH st %]
+                [% CASE 'numberlength'%]
+                    [% IF st == subtype %]
+                        <option value="issues" selected="selected">
+                    [% ELSE %]
+                        <option value="issues">
+                    [% END %]
+                    issues
+                [% CASE 'weeklength' %]
+                    [% IF st == subtype %]
+                        <option value="weeks" selected="selected">
+                    [% ELSE %]
+                        <option value="weeks">
+                    [% END %]
+                    weeks
+                [% CASE 'monthlength' %]
+                    [% IF st == subtype %]
+                        <option value="months" selected="selected">
+                    [% ELSE %]
+                        <option value="months">
+                    [% END %]
+                    months
+                [% CASE %][% st %]
+            [% END %]
+            </option>
+        [% END %]
+    </select>
+    <input type="text" name="sublength" id="sublength" value="[% sublength %]" size="3" />(enter amount in numerals)
+    <input type="hidden" name="issuelengthcount">
+    </li>
 		<li><label for="note">Note for the librarian that will manage your renewal request: </label>
 		<textarea name="note" id="note" rows="5" cols="50"></textarea></li></ol></fieldset>
 		<fieldset class="action"><input type="submit" value="Submit" class="button" /></fieldset>
diff --git a/serials/subscription-add.pl b/serials/subscription-add.pl
index 16d397b..50bc702 100755
--- a/serials/subscription-add.pl
+++ b/serials/subscription-add.pl
@@ -248,16 +248,6 @@ sub get_letter_loop {
     ];
 }
 
-sub _get_sub_length {
-    my ($type, $length) = @_;
-    return
-        (
-            $type eq 'issues' ? $length : 0,
-            $type eq 'weeks'   ? $length : 0,
-            $type eq 'months'  ? $length : 0,
-        );
-}
-
 sub _guess_enddate {
     my ($startdate_iso, $frequencyid, $numberlength, $weeklength, $monthlength) = @_;
     my ($year, $month, $day);
@@ -301,7 +291,7 @@ sub redirect_add_subscription {
     my $subtype = $query->param('subtype');
     my $sublength = $query->param('sublength');
     my ( $numberlength, $weeklength, $monthlength )
-        = _get_sub_length( $subtype, $sublength );
+        = C4::Serials->_get_sub_length( $subtype, $sublength );
     my $add1              = $query->param('add1');
     my $lastvalue1        = $query->param('lastvalue1');
     my $innerloop1        = $query->param('innerloop1');
@@ -379,7 +369,7 @@ sub redirect_mod_subscription {
     my $subtype = $query->param('subtype');
     my $sublength = $query->param('sublength');
     my ($numberlength, $weeklength, $monthlength)
-        = _get_sub_length( $subtype, $sublength );
+        = C4::Serials->_get_sub_length( $subtype, $sublength );
     my $numberpattern = $query->param('numbering_pattern');
     my $locale = $query->param('locale');
     my $lastvalue1 = $query->param('lastvalue1');
diff --git a/serials/subscription-renew.pl b/serials/subscription-renew.pl
index bbbc8e5..24efe00 100755
--- a/serials/subscription-renew.pl
+++ b/serials/subscription-renew.pl
@@ -62,6 +62,10 @@ my $dbh   = C4::Context->dbh;
 my $mode           = $query->param('mode') || q{};
 my $op             = $query->param('op') || 'display';
 my $subscriptionid = $query->param('subscriptionid');
+my $sublength = $query->param('sublength');
+my $subtype = $query->param('subtype');
+my ($numberlength, $weeklength, $monthlength) = C4::Serials->_get_sub_length( $subtype, $sublength );
+
 my $done = 0;    # for after form has been submitted
 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
     {
@@ -77,8 +81,8 @@ if ( $op eq "renew" ) {
     my $startdate = output_pref( { str => scalar $query->param('startdate'), dateonly => 1, dateformat => 'iso' } );
     ReNewSubscription(
         $subscriptionid, $loggedinuser,
-        $startdate, $query->param('numberlength'),
-        $query->param('weeklength'), $query->param('monthlength'),
+        $startdate, $numberlength,
+        $weeklength, $monthlength,
         $query->param('note')
     );
 }
@@ -93,6 +97,7 @@ my $newstartdate = output_pref( { str => $subscription->{enddate}, dateonly => 1
     or output_pref( { dt => dt_from_string, dateonly => 1 } );
 
 $template->param(
+    subtypes       => [ qw( numberlength weeklength monthlength ) ],
     startdate      => $newstartdate,
     numberlength   => $subscription->{numberlength},
     weeklength     => $subscription->{weeklength},
-- 
2.1.4