Bug 17785: Fix OAI-PMH's XSLT-generated URLs under Plack
[koha.git] / serials / showpredictionpattern.pl
blob6986b5efad5c9ac6c20dc717e63c70a2bc70d613
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 = new CGI;
42 my ($template, $loggedinuser, $cookie, $flags) = get_template_and_user( {
43 template_name => 'serials/showpredictionpattern.tt',
44 query => $input,
45 type => 'intranet',
46 authnotrequired => 0,
47 flagsrequired => { 'serials' => '*' },
48 } );
50 my $subscriptionid = $input->param('subscriptionid');
51 my $frequencyid = $input->param('frequency');
52 my $firstacquidate = $input->param('firstacquidate');
53 my $nextacquidate = $input->param('nextacquidate');
54 my $enddate = $input->param('enddate');
55 my $subtype = $input->param('subtype');
56 my $sublength = $input->param('sublength');
57 my $custompattern = $input->param('custompattern');
59 my $frequency = GetSubscriptionFrequency($frequencyid);
61 my %pattern = (
62 numberingmethod => scalar $input->param('numberingmethod') // '',
63 numbering1 => scalar $input->param('numbering1') // '',
64 numbering2 => scalar $input->param('numbering2') // '',
65 numbering3 => scalar $input->param('numbering3') // '',
66 add1 => scalar $input->param('add1') // '',
67 add2 => scalar $input->param('add2') // '',
68 add3 => scalar $input->param('add3') // '',
69 whenmorethan1 => scalar $input->param('whenmorethan1') // '',
70 whenmorethan2 => scalar $input->param('whenmorethan2') // '',
71 whenmorethan3 => scalar $input->param('whenmorethan3') // '',
72 setto1 => scalar $input->param('setto1') // '',
73 setto2 => scalar $input->param('setto2') // '',
74 setto3 => scalar $input->param('setto3') // '',
75 every1 => scalar $input->param('every1') // '',
76 every2 => scalar $input->param('every2') // '',
77 every3 => scalar $input->param('every3') // '',
80 $firstacquidate = eval { output_pref( { str => $firstacquidate, dateonly => 1, dateformat => 'iso' } ); }
81 or output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' } );
83 $enddate = eval { output_pref( { str => $enddate, dateonly => 1, dateformat => 'iso' } ); };
85 if($nextacquidate) {
86 $nextacquidate = eval { output_pref( { str => $nextacquidate, dateonly => 1, dateformat => 'iso' } ); };
87 } else {
88 $nextacquidate = $firstacquidate;
90 my $date = $nextacquidate;
92 my %subscription = (
93 locale => scalar $input->param('locale') // '',
94 lastvalue1 => scalar $input->param('lastvalue1') // '',
95 lastvalue2 => scalar $input->param('lastvalue2') // '',
96 lastvalue3 => scalar $input->param('lastvalue3') // '',
97 innerloop1 => scalar $input->param('innerloop1') // '',
98 innerloop2 => scalar $input->param('innerloop2') // '',
99 innerloop3 => scalar $input->param('innerloop3') // '',
100 irregularity => '',
101 periodicity => $frequencyid,
102 countissuesperunit => 1,
103 firstacquidate => $firstacquidate,
106 my $issuenumber;
107 if(defined $subscriptionid) {
108 ($issuenumber) = C4::Serials::GetFictiveIssueNumber(\%subscription, $date);
109 } else {
110 $issuenumber = 1;
113 my @predictions_loop;
114 my ($calculated) = GetSeq(\%subscription, \%pattern);
115 push @predictions_loop, {
116 number => $calculated,
117 publicationdate => $date,
118 issuenumber => $issuenumber,
119 dow => Day_of_Week(split /-/, $date),
121 my @irreg = ();
122 if(defined $subscriptionid) {
123 @irreg = C4::Serials::GetSubscriptionIrregularities($subscriptionid);
124 while(@irreg && $issuenumber > $irreg[0]) {
125 shift @irreg;
127 if(@irreg && $issuenumber == $irreg[0]){
128 $predictions_loop[0]->{'not_published'} = 1;
129 shift @irreg;
133 my $i = 1;
134 while( $i < 1000 ) {
135 my %line;
137 if(defined $date){
138 $date = GetNextDate(\%subscription, $date);
140 if(defined $date){
141 $line{'publicationdate'} = $date;
142 $line{'dow'} = Day_of_Week(split /-/, $date);
145 # Check if we don't have exceed end date
146 if($sublength){
147 if($subtype eq "issues" && $i >= $sublength){
148 last;
149 } elsif($subtype eq "weeks" && $date && Delta_Days( split(/-/, $date), Add_Delta_Days( split(/-/, $firstacquidate), 7*$sublength - 1 ) ) < 0) {
150 last;
151 } elsif($subtype eq "months" && $date && (Delta_Days( split(/-/, $date), Add_Delta_YM( split(/-/, $firstacquidate), 0, $sublength) ) - 1) < 0 ) {
152 last;
155 if($enddate && $date && Delta_Days( split(/-/, $date), split(/-/, $enddate) ) <= 0 ) {
156 last;
159 ($calculated, $subscription{'lastvalue1'}, $subscription{'lastvalue2'}, $subscription{'lastvalue3'}, $subscription{'innerloop1'}, $subscription{'innerloop2'}, $subscription{'innerloop3'}) = GetNextSeq(\%subscription, \%pattern);
160 $issuenumber++;
161 $line{'number'} = $calculated;
162 $line{'issuenumber'} = $issuenumber;
163 if(@irreg && $issuenumber == $irreg[0]){
164 $line{'not_published'} = 1;
165 shift @irreg;
167 push @predictions_loop, \%line;
169 $i++;
172 $template->param(
173 predictions_loop => \@predictions_loop,
176 if ( $frequency->{unit} and not $custompattern ) {
177 $template->param( ask_for_irregularities => 1 );
178 if ( $frequency->{unit} eq 'day' and $frequency->{unitsperissue} == 1 ) {
179 $template->param( daily_options => 1 );
183 if ( ( $date && $enddate && $date ne $enddate )
184 or ( $subtype eq 'issues' && $i < $sublength ) )
186 $template->param( not_consistent_end_date => 1 );
189 output_html_with_http_headers $input, $cookie, $template->output;