Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.git] / serials / showpredictionpattern.pl
1 #!/usr/bin/perl
2
3 # Copyright 2011-2013 Biblibre SARL
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 =head1 NAME
21
22 showpredictionpattern.pl
23
24 =head1 DESCRIPTION
25
26 This script calculate numbering of serials based on numbering pattern, and
27 publication date, based on frequency and first publication date.
28
29 =cut
30
31 use Modern::Perl;
32
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;
40
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 } );
49
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');
58
59
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') // ''
69     };
70 }
71 else {
72     $frequency = GetSubscriptionFrequency($frequencyid);
73 }
74
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') // '',
92 );
93
94 $firstacquidate = eval { output_pref( { str => $firstacquidate, dateonly => 1, dateformat => 'iso' } ); }
95     or output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' } );
96
97 $enddate = eval { output_pref( { str => $enddate, dateonly => 1, dateformat => 'iso' } ); };
98
99 if($nextacquidate) {
100     $nextacquidate =  eval { output_pref( { str => $nextacquidate, dateonly => 1, dateformat => 'iso' } ); };
101 } else {
102     $nextacquidate = $firstacquidate;
103 }
104 my $date = $nextacquidate;
105
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,
117 );
118
119 my $issuenumber;
120 if(defined $subscriptionid) {
121     ($issuenumber) = C4::Serials::GetFictiveIssueNumber(\%subscription, $date, $frequency);
122 } else {
123     $issuenumber = 1;
124 }
125
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),
133 };
134 my @irreg = ();
135 if(defined $subscriptionid) {
136     @irreg = C4::Serials::GetSubscriptionIrregularities($subscriptionid);
137     while(@irreg && $issuenumber > $irreg[0]) {
138         shift @irreg;
139     }
140     if(@irreg && $issuenumber == $irreg[0]){
141         $predictions_loop[0]->{'not_published'} = 1;
142         shift @irreg;
143     }
144 }
145
146 my $i = 1;
147 while( $i < 1000 ) {
148     my %line;
149
150     if(defined $date){
151         $date = GetNextDate(\%subscription, $date, $frequency);
152     }
153     if(defined $date){
154         $line{'publicationdate'} = $date;
155         $line{'dow'} = Day_of_Week(split /-/, $date);
156     }
157
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;
166         }
167     }
168     if($enddate && $date && Delta_Days( split(/-/, $date), split(/-/, $enddate) ) <= 0 ) {
169         last;
170     }
171
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;
179     }
180     push @predictions_loop, \%line;
181
182     $i++;
183 }
184
185 $template->param(
186     predictions_loop => \@predictions_loop,
187 );
188
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 );
193     }
194 }
195
196 if (   ( $date && $enddate && $date ne $enddate )
197     or ( $subtype eq 'issues' && $i < $sublength ) )
198 {
199     $template->param( not_consistent_end_date => 1 );
200 }
201
202 output_html_with_http_headers $input, $cookie, $template->output;