Bug 26922: Regression tests
[koha.git] / serials / showpredictionpattern.pl
blobd6bcfc79187044ec2befebe2a47251571d953ac2
1 #!/usr/bin/perl
3 # Copyright 2011-2013 Biblibre SARL
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 =head1 NAME
22 showpredictionpattern.pl
24 =head1 DESCRIPTION
26 This script calculate numbering of serials based on numbering pattern, and
27 publication date, based on frequency and first publication date.
29 =cut
31 use Modern::Perl;
33 use CGI qw ( -utf8 );
34 use Date::Calc qw(Today Day_of_Year Week_of_Year Day_of_Week Days_in_Year Delta_Days Add_Delta_Days Add_Delta_YM);
35 use C4::Auth;
36 use C4::Output;
37 use C4::Serials;
38 use C4::Serials::Frequency;
39 use Koha::DateUtils;
41 my $input = CGI->new;
42 my ($template, $loggedinuser, $cookie, $flags) = get_template_and_user( {
43 template_name => 'serials/showpredictionpattern.tt',
44 query => $input,
45 type => 'intranet',
46 flagsrequired => { 'serials' => '*' },
47 } );
49 my $subscriptionid = $input->param('subscriptionid');
50 my $frequencyid = $input->param('frequency');
51 my $firstacquidate = $input->param('firstacquidate');
52 my $nextacquidate = $input->param('nextacquidate');
53 my $enddate = $input->param('to');
54 my $subtype = $input->param('subtype');
55 my $sublength = $input->param('sublength');
56 my $custompattern = $input->param('custompattern');
59 my $frequency;
60 if ( $frequencyid eq 'mana' ) {
61 $frequency = {
62 'id' => undef,
63 'displayorder' => undef,
64 'description' => scalar $input->param('sfdescription') // '',
65 'unitsperissue' => scalar $input->param('unitsperissue') // '',
66 'issuesperunit' => scalar $input->param('issuesperunit') // '',
67 'unit' => scalar $input->param('unit') // ''
70 else {
71 $frequency = GetSubscriptionFrequency($frequencyid);
74 my %pattern = (
75 numberingmethod => scalar $input->param('numberingmethod') // '',
76 numbering1 => scalar $input->param('numbering1') // '',
77 numbering2 => scalar $input->param('numbering2') // '',
78 numbering3 => scalar $input->param('numbering3') // '',
79 add1 => scalar $input->param('add1') // '',
80 add2 => scalar $input->param('add2') // '',
81 add3 => scalar $input->param('add3') // '',
82 whenmorethan1 => scalar $input->param('whenmorethan1') // '',
83 whenmorethan2 => scalar $input->param('whenmorethan2') // '',
84 whenmorethan3 => scalar $input->param('whenmorethan3') // '',
85 setto1 => scalar $input->param('setto1') // '',
86 setto2 => scalar $input->param('setto2') // '',
87 setto3 => scalar $input->param('setto3') // '',
88 every1 => scalar $input->param('every1') // '',
89 every2 => scalar $input->param('every2') // '',
90 every3 => scalar $input->param('every3') // '',
93 $firstacquidate = eval { output_pref( { str => $firstacquidate, dateonly => 1, dateformat => 'iso' } ); }
94 or output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' } );
96 $enddate = eval { output_pref( { str => $enddate, dateonly => 1, dateformat => 'iso' } ); };
98 if($nextacquidate) {
99 $nextacquidate = eval { output_pref( { str => $nextacquidate, dateonly => 1, dateformat => 'iso' } ); };
100 } else {
101 $nextacquidate = $firstacquidate;
103 my $date = $nextacquidate;
105 my %subscription = (
106 locale => scalar $input->param('locale') // '',
107 lastvalue1 => scalar $input->param('lastvalue1') // '',
108 lastvalue2 => scalar $input->param('lastvalue2') // '',
109 lastvalue3 => scalar $input->param('lastvalue3') // '',
110 innerloop1 => scalar $input->param('innerloop1') // '',
111 innerloop2 => scalar $input->param('innerloop2') // '',
112 innerloop3 => scalar $input->param('innerloop3') // '',
113 irregularity => '',
114 countissuesperunit => 1,
115 firstacquidate => $firstacquidate,
118 my $issuenumber;
119 if(defined $subscriptionid) {
120 ($issuenumber) = C4::Serials::GetFictiveIssueNumber(\%subscription, $date, $frequency);
121 } else {
122 $issuenumber = 1;
125 my @predictions_loop;
126 my ($calculated) = GetSeq(\%subscription, \%pattern);
127 push @predictions_loop, {
128 number => $calculated,
129 publicationdate => $date,
130 issuenumber => $issuenumber,
131 dow => Day_of_Week(split /-/, $date),
133 my @irreg = ();
134 if(defined $subscriptionid) {
135 @irreg = C4::Serials::GetSubscriptionIrregularities($subscriptionid);
136 while(@irreg && $issuenumber > $irreg[0]) {
137 shift @irreg;
139 if(@irreg && $issuenumber == $irreg[0]){
140 $predictions_loop[0]->{'not_published'} = 1;
141 shift @irreg;
145 my $i = 1;
146 while( $i < 1000 ) {
147 my %line;
149 if(defined $date){
150 $date = GetNextDate(\%subscription, $date, $frequency);
152 if(defined $date){
153 $line{'publicationdate'} = $date;
154 $line{'dow'} = Day_of_Week(split /-/, $date);
157 # Check if we don't have exceed end date
158 if($sublength){
159 if($subtype eq "issues" && $i >= $sublength){
160 last;
161 } elsif($subtype eq "weeks" && $date && Delta_Days( split(/-/, $date), Add_Delta_Days( split(/-/, $firstacquidate), 7*$sublength - 1 ) ) < 0) {
162 last;
163 } elsif($subtype eq "months" && $date && (Delta_Days( split(/-/, $date), Add_Delta_YM( split(/-/, $firstacquidate), 0, $sublength) ) - 1) < 0 ) {
164 last;
167 if($enddate && $date && Delta_Days( split(/-/, $date), split(/-/, $enddate) ) <= 0 ) {
168 last;
171 ($calculated, $subscription{'lastvalue1'}, $subscription{'lastvalue2'}, $subscription{'lastvalue3'}, $subscription{'innerloop1'}, $subscription{'innerloop2'}, $subscription{'innerloop3'}) = GetNextSeq(\%subscription, \%pattern, $frequency);
172 $issuenumber++;
173 $line{'number'} = $calculated;
174 $line{'issuenumber'} = $issuenumber;
175 if(@irreg && $issuenumber == $irreg[0]){
176 $line{'not_published'} = 1;
177 shift @irreg;
179 push @predictions_loop, \%line;
181 $i++;
184 $template->param(
185 predictions_loop => \@predictions_loop,
188 if ( $frequency->{unit} and not $custompattern ) {
189 $template->param( ask_for_irregularities => 1 );
190 if ( $frequency->{unit} eq 'day' and $frequency->{unitsperissue} == 1 ) {
191 $template->param( daily_options => 1 );
195 if ( ( $date && $enddate && $date ne $enddate )
196 or ( $subtype eq 'issues' && $i < $sublength ) )
198 $template->param( not_consistent_end_date => 1 );
201 output_html_with_http_headers $input, $cookie, $template->output;