Bug 14667: (QA followup) make the UI show 'Label creator' consistently
[koha.git] / serials / showpredictionpattern.pl
blob6945a36e83ee548ce42bb838d982d89289e2143d
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;
40 my $input = new CGI;
41 my ($template, $loggedinuser, $cookie, $flags) = get_template_and_user( {
42 template_name => 'serials/showpredictionpattern.tt',
43 query => $input,
44 type => 'intranet',
45 authnotrequired => 0,
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('enddate');
54 my $subtype = $input->param('subtype');
55 my $sublength = $input->param('sublength');
56 my $custompattern = $input->param('custompattern');
58 my $frequency = GetSubscriptionFrequency($frequencyid);
60 my %pattern = (
61 numberingmethod => $input->param('numberingmethod') // '',
62 numbering1 => $input->param('numbering1') // '',
63 numbering2 => $input->param('numbering2') // '',
64 numbering3 => $input->param('numbering3') // '',
65 add1 => $input->param('add1') // '',
66 add2 => $input->param('add2') // '',
67 add3 => $input->param('add3') // '',
68 whenmorethan1 => $input->param('whenmorethan1') // '',
69 whenmorethan2 => $input->param('whenmorethan2') // '',
70 whenmorethan3 => $input->param('whenmorethan3') // '',
71 setto1 => $input->param('setto1') // '',
72 setto2 => $input->param('setto2') // '',
73 setto3 => $input->param('setto3') // '',
74 every1 => $input->param('every1') // '',
75 every2 => $input->param('every2') // '',
76 every3 => $input->param('every3') // '',
79 if(!defined $firstacquidate || $firstacquidate eq ''){
80 my ($year, $month, $day) = Today();
81 $firstacquidate = sprintf "%04d-%02d-%02d", $year, $month, $day;
82 } else {
83 $firstacquidate = C4::Dates->new($firstacquidate)->output('iso');
86 if($enddate){
87 $enddate = C4::Dates->new($enddate)->output('iso');
90 if($nextacquidate) {
91 $nextacquidate = C4::Dates->new($nextacquidate)->output('iso');
92 } else {
93 $nextacquidate = $firstacquidate;
95 my $date = $nextacquidate;
97 my %subscription = (
98 locale => $input->param('locale') // '',
99 lastvalue1 => $input->param('lastvalue1') // '',
100 lastvalue2 => $input->param('lastvalue2') // '',
101 lastvalue3 => $input->param('lastvalue3') // '',
102 innerloop1 => $input->param('innerloop1') // '',
103 innerloop2 => $input->param('innerloop2') // '',
104 innerloop3 => $input->param('innerloop3') // '',
105 irregularity => '',
106 periodicity => $frequencyid,
107 countissuesperunit => 1,
108 firstacquidate => $firstacquidate,
111 my $issuenumber;
112 if(defined $subscriptionid) {
113 ($issuenumber) = C4::Serials::GetFictiveIssueNumber(\%subscription, $date);
114 } else {
115 $issuenumber = 1;
118 my @predictions_loop;
119 my ($calculated) = GetSeq(\%subscription, \%pattern);
120 push @predictions_loop, {
121 number => $calculated,
122 publicationdate => $date,
123 issuenumber => $issuenumber,
124 dow => Day_of_Week(split /-/, $date),
126 my @irreg = ();
127 if(defined $subscriptionid) {
128 @irreg = C4::Serials::GetSubscriptionIrregularities($subscriptionid);
129 while(@irreg && $issuenumber > $irreg[0]) {
130 shift @irreg;
132 if(@irreg && $issuenumber == $irreg[0]){
133 $predictions_loop[0]->{'not_published'} = 1;
134 shift @irreg;
138 my $i = 1;
139 while( $i < 1000 ) {
140 my %line;
142 if(defined $date){
143 $date = GetNextDate(\%subscription, $date);
145 if(defined $date){
146 $line{'publicationdate'} = $date;
147 $line{'dow'} = Day_of_Week(split /-/, $date);
150 # Check if we don't have exceed end date
151 if($sublength){
152 if($subtype eq "issues" && $i >= $sublength){
153 last;
154 } elsif($subtype eq "weeks" && $date && Delta_Days( split(/-/, $date), Add_Delta_Days( split(/-/, $firstacquidate), 7*$sublength - 1 ) ) < 0) {
155 last;
156 } elsif($subtype eq "months" && $date && (Delta_Days( split(/-/, $date), Add_Delta_YM( split(/-/, $firstacquidate), 0, $sublength) ) - 1) < 0 ) {
157 last;
160 if($enddate && $date && Delta_Days( split(/-/, $date), split(/-/, $enddate) ) <= 0 ) {
161 last;
164 ($calculated, $subscription{'lastvalue1'}, $subscription{'lastvalue2'}, $subscription{'lastvalue3'}, $subscription{'innerloop1'}, $subscription{'innerloop2'}, $subscription{'innerloop3'}) = GetNextSeq(\%subscription, \%pattern);
165 $issuenumber++;
166 $line{'number'} = $calculated;
167 $line{'issuenumber'} = $issuenumber;
168 if(@irreg && $issuenumber == $irreg[0]){
169 $line{'not_published'} = 1;
170 shift @irreg;
172 push @predictions_loop, \%line;
174 $i++;
177 $template->param(
178 predictions_loop => \@predictions_loop,
181 if ( $frequency->{unit} and not $custompattern ) {
182 $template->param( ask_for_irregularities => 1 );
183 if ( $frequency->{unit} eq 'day' and $frequency->{unitsperissue} == 1 ) {
184 $template->param( daily_options => 1 );
188 if ( ( $date && $enddate && $date ne $enddate )
189 or ( $subtype eq 'issues' && $i < $sublength ) )
191 $template->param( not_consistent_end_date => 1 );
194 output_html_with_http_headers $input, $cookie, $template->output;