use Koha::Subscriptions;
use Koha::Subscription::Histories;
use Koha::SharedContent;
+use Scalar::Util qw( looks_like_number );
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
&GetNextSeq &GetSeq &NewIssue &GetSerials
&GetLatestSerials &ModSerialStatus &GetNextDate &GetSerials2
- &ReNewSubscription &GetLateOrMissingIssues
+ &GetSubscriptionLength &ReNewSubscription &GetLateOrMissingIssues
&GetSerialInformation &AddItem2Serial
&PrepareSerialsData &GetNextExpected &ModNextExpected
&GetPreviousSerialid
return $subscriptionid;
}
+=head2 GetSubscriptionLength
+
+my ($numberlength, $weeklength, $monthlength) = GetSubscriptionLength( $subtype, $sublength );
+
+This function calculates the subscription length.
+
+=cut
+
+sub GetSubscriptionLength {
+ my ($subtype, $length) = @_;
+
+ return unless looks_like_number($length);
+
+ return
+ (
+ $subtype eq 'issues' ? $length : 0,
+ $subtype eq 'weeks' ? $length : 0,
+ $subtype eq 'months' ? $length : 0,
+ );
+}
+
+
=head2 ReNewSubscription
ReNewSubscription($params);
<input type="text" size="10" id="startdate" name="startdate" value="[% startdate | html %]" class="datepicker"/>
<div class="hint">[% INCLUDE 'date-format.inc' %]</div>
</li>
- <li><fieldset>
- <legend>Subscription length:</legend>
- <ol><li><label for="numberlength">Number of num:</label><input type="text" id="numberlength" name="numberlength" value="[% subscription.numberlength | html %]" /></li>
- <li><label for="weeklength">Number of weeks: </label><input type="text" id="weeklength" name="weeklength" value="[% subscription.weeklength | html %]" /></li>
- <li><label for="monthlength">Number of months: </label><input type="text" id="monthlength" name="monthlength" value="[% subscription.monthlength | html %]" /></li></ol></fieldset></li>
+ <li>
+ <label>Subscription length:</label>
+ <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 | html %]
+ [% END %]
+ </option>
+ [% END %]
+ </select>
+ <input type="text" name="sublength" id="sublength" size="3" />(enter amount in numerals)
+ <input type="hidden" name="issuelengthcount">
+ </li>
-<li><label for="branchcode">Library:</label>
- <select name="branchcode" id="branchcode" style="width: 20em;">
- [% UNLESS ( Independentbranches ) %]
- <option value="">None</option>
- [% END %]
- [% IF CAN_user_serials_superserials %]
- [% FOREACH library IN libraries %]
- <option value="[% library.branchcode | html %]"> [% library.branchname | html %] </option>
+ <li><label for="branchcode">Library:</label>
+ <select name="branchcode" id="branchcode" style="width: 20em;">
+ [% UNLESS ( Independentbranches ) %]
+ <option value="">None</option>
+ [% END %]
+ [% IF CAN_user_serials_superserials %]
+ [% FOREACH library IN libraries %]
+ <option value="[% library.branchcode | html %]"> [% library.branchname | html %] </option>
+ [% END %]
[% END %]
- [% END %]
-</select> (select a library)
-</li>
+ </select> (select a library)
+ </li>
- <li><label for="note">Note for the librarian that will manage your renewal request: </label>
+ <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>
</form>
];
}
-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);
my $subtype = $query->param('subtype');
my $sublength = $query->param('sublength');
my ( $numberlength, $weeklength, $monthlength )
- = _get_sub_length( $subtype, $sublength );
+ = GetSubscriptionLength( $subtype, $sublength );
my $add1 = $query->param('add1');
my $lastvalue1 = $query->param('lastvalue1');
my $innerloop1 = $query->param('innerloop1');
my $subtype = $query->param('subtype');
my $sublength = $query->param('sublength');
- my ($numberlength, $weeklength, $monthlength)
- = _get_sub_length( $subtype, $sublength );
+ my ($numberlength, $weeklength, $monthlength) = GetSubscriptionLength( $subtype, $sublength );
+ my $numberpattern = $query->param('numbering_pattern');
my $locale = $query->param('locale');
my $lastvalue1 = $query->param('lastvalue1');
my $innerloop1 = $query->param('innerloop1');
my $op = $query->param('op') || 'display';
my @subscriptionids = $query->multi_param('subscriptionid');
my $branchcode = $query->param('branchcode');
+my $sublength = $query->param('sublength');
+my $subtype = $query->param('subtype');
+my ($numberlength, $weeklength, $monthlength);
+
my $done = 0; # for after form has been submitted
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
{
my $subscription = GetSubscription( $subscriptionid );
output_and_exit( $query, $cookie, $template, 'unknown_subscription') unless $subscription;
my $startdate = output_pref( { str => scalar $query->param('startdate'), dateonly => 1, dateformat => 'iso' } );
+ ($numberlength, $weeklength, $monthlength) = GetSubscriptionLength( $subtype, $sublength );
ReNewSubscription(
{
subscriptionid => $subscriptionid,
user => $loggedinuser,
startdate => $startdate,
- numberlength => scalar $query->param('numberlength'),
- weeklength => scalar $query->param('weeklength'),
- monthlength => scalar $query->param('monthlength'),
+ numberlength => $numberlength,
+ weeklength => $weeklength,
+ monthlength => $monthlength,
note => scalar $query->param('note'),
branchcode => $branchcode
}
$template->param(
op => $op,
libraries => $libraries,
+ subtypes => [ qw( numberlength weeklength monthlength ) ],
);
output_html_with_http_headers $query, $cookie, $template->output;
# This script includes tests for ReNewSubscription
# Copyright 2015 BibLibre, Paul Poulain
+# Copyright 2018 Catalyst IT, Alex Buckley
#
# This file is part of Koha.
#
use Modern::Perl;
-use Test::More tests => 1;
+use Test::More tests => 7;
use Test::MockModule;
use t::lib::TestBuilder;
use t::lib::Mocks;
monthlength => 12
}
);
+
+# Calculate the subscription length for the renewal for issues, days and months
+my ($numberlength, $weeklength, $monthlength) = GetSubscriptionLength('issues', 7);
+is ( $numberlength, 7, "Subscription length is 7 issues");
+
+($numberlength, $weeklength, $monthlength) = GetSubscriptionLength('weeks', 7);
+is ( $weeklength, 7, "Subscription length is 7 weeks");
+
+($numberlength, $weeklength, $monthlength) = GetSubscriptionLength('months', 7);
+is ( $monthlength, 7, "Subscription length is 7 months");
+
+# Check subscription length when no value is inputted into the numeric sublength field
+($numberlength, $weeklength, $monthlength) = GetSubscriptionLength('months', '');
+is ($monthlength, undef, "Subscription length is undef months, invalid month data was not stored");
+
+# Check subscription length when a letter is inputted into the numeric sublength field
+($numberlength, $weeklength, $monthlength) = GetSubscriptionLength('issues', 'w');
+is ($monthlength, undef, "Subscription length is undef issues, invalid issue data was not stored");
+
+# Check subscription length when a special character is inputted into numberic sublength field
+($numberlength, $weeklength, $monthlength) = GetSubscriptionLength('weeks', '!');
+is ($weeklength, undef, "Subscription length is undef weeks, invalid weeks data was not stored");
+
+# Renew the subscription and check that enddate has not been set
+
my $history = Koha::Subscription::Histories->find($subscription->{subscriptionid});
is ( $history->histenddate(), undef, 'subscription history not empty after renewal');