Bug 23251: Correct formatting of IMD data ending in apostrophe
[koha.git] / serials / showpredictionpattern.pl
blob4a3f7b2a64116e8ef15b3774b6ff38e73835d703
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('to');
55 my $subtype = $input->param('subtype');
56 my $sublength = $input->param('sublength');
57 my $custompattern = $input->param('custompattern');
60 my $frequency;
61 if ( $frequencyid eq 'mana' ) {
62 $frequency = {
63 'id' => undef,
64 'displayorder' => undef,
65 'description' => scalar $input->param('sfdescription') // '',
66 'unitsperissue' => scalar $input->param('unitsperissue') // '',
67 'issuesperunit' => scalar $input->param('issuesperunit') // '',
68 'unit' => scalar $input->param('unit') // ''
71 else {
72 $frequency = GetSubscriptionFrequency($frequencyid);
75 my %pattern = (
76 numberingmethod => scalar $input->param('numberingmethod') // '',
77 numbering1 => scalar $input->param('numbering1') // '',
78 numbering2 => scalar $input->param('numbering2') // '',
79 numbering3 => scalar $input->param('numbering3') // '',
80 add1 => scalar $input->param('add1') // '',
81 add2 => scalar $input->param('add2') // '',
82 add3 => scalar $input->param('add3') // '',
83 whenmorethan1 => scalar $input->param('whenmorethan1') // '',
84 whenmorethan2 => scalar $input->param('whenmorethan2') // '',
85 whenmorethan3 => scalar $input->param('whenmorethan3') // '',
86 setto1 => scalar $input->param('setto1') // '',
87 setto2 => scalar $input->param('setto2') // '',
88 setto3 => scalar $input->param('setto3') // '',
89 every1 => scalar $input->param('every1') // '',
90 every2 => scalar $input->param('every2') // '',
91 every3 => scalar $input->param('every3') // '',
94 $firstacquidate = eval { output_pref( { str => $firstacquidate, dateonly => 1, dateformat => 'iso' } ); }
95 or output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' } );
97 $enddate = eval { output_pref( { str => $enddate, dateonly => 1, dateformat => 'iso' } ); };
99 if($nextacquidate) {
100 $nextacquidate = eval { output_pref( { str => $nextacquidate, dateonly => 1, dateformat => 'iso' } ); };
101 } else {
102 $nextacquidate = $firstacquidate;
104 my $date = $nextacquidate;
106 my %subscription = (
107 locale => scalar $input->param('locale') // '',
108 lastvalue1 => scalar $input->param('lastvalue1') // '',
109 lastvalue2 => scalar $input->param('lastvalue2') // '',
110 lastvalue3 => scalar $input->param('lastvalue3') // '',
111 innerloop1 => scalar $input->param('innerloop1') // '',
112 innerloop2 => scalar $input->param('innerloop2') // '',
113 innerloop3 => scalar $input->param('innerloop3') // '',
114 irregularity => '',
115 countissuesperunit => 1,
116 firstacquidate => $firstacquidate,
119 my $issuenumber;
120 if(defined $subscriptionid) {
121 ($issuenumber) = C4::Serials::GetFictiveIssueNumber(\%subscription, $date, $frequency);
122 } else {
123 $issuenumber = 1;
126 my @predictions_loop;
127 my ($calculated) = GetSeq(\%subscription, \%pattern);
128 push @predictions_loop, {
129 number => $calculated,
130 publicationdate => $date,
131 issuenumber => $issuenumber,
132 dow => Day_of_Week(split /-/, $date),
134 my @irreg = ();
135 if(defined $subscriptionid) {
136 @irreg = C4::Serials::GetSubscriptionIrregularities($subscriptionid);
137 while(@irreg && $issuenumber > $irreg[0]) {
138 shift @irreg;
140 if(@irreg && $issuenumber == $irreg[0]){
141 $predictions_loop[0]->{'not_published'} = 1;
142 shift @irreg;
146 my $i = 1;
147 while( $i < 1000 ) {
148 my %line;
150 if(defined $date){
151 $date = GetNextDate(\%subscription, $date, $frequency);
153 if(defined $date){
154 $line{'publicationdate'} = $date;
155 $line{'dow'} = Day_of_Week(split /-/, $date);
158 # Check if we don't have exceed end date
159 if($sublength){
160 if($subtype eq "issues" && $i >= $sublength){
161 last;
162 } elsif($subtype eq "weeks" && $date && Delta_Days( split(/-/, $date), Add_Delta_Days( split(/-/, $firstacquidate), 7*$sublength - 1 ) ) < 0) {
163 last;
164 } elsif($subtype eq "months" && $date && (Delta_Days( split(/-/, $date), Add_Delta_YM( split(/-/, $firstacquidate), 0, $sublength) ) - 1) < 0 ) {
165 last;
168 if($enddate && $date && Delta_Days( split(/-/, $date), split(/-/, $enddate) ) <= 0 ) {
169 last;
172 ($calculated, $subscription{'lastvalue1'}, $subscription{'lastvalue2'}, $subscription{'lastvalue3'}, $subscription{'innerloop1'}, $subscription{'innerloop2'}, $subscription{'innerloop3'}) = GetNextSeq(\%subscription, \%pattern, $frequency);
173 $issuenumber++;
174 $line{'number'} = $calculated;
175 $line{'issuenumber'} = $issuenumber;
176 if(@irreg && $issuenumber == $irreg[0]){
177 $line{'not_published'} = 1;
178 shift @irreg;
180 push @predictions_loop, \%line;
182 $i++;
185 $template->param(
186 predictions_loop => \@predictions_loop,
189 if ( $frequency->{unit} and not $custompattern ) {
190 $template->param( ask_for_irregularities => 1 );
191 if ( $frequency->{unit} eq 'day' and $frequency->{unitsperissue} == 1 ) {
192 $template->param( daily_options => 1 );
196 if ( ( $date && $enddate && $date ne $enddate )
197 or ( $subtype eq 'issues' && $i < $sublength ) )
199 $template->param( not_consistent_end_date => 1 );
202 output_html_with_http_headers $input, $cookie, $template->output;