Bug 11772: (follow-up) make warning about no active currency a link to currency setup...
[koha.git] / serials / showpredictionpattern.pl
blob690705de9710cff7ee93d2ce92dd3a8e17c7b37f
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;
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');
59 my %pattern = (
60 numberingmethod => $input->param('numberingmethod') // '',
61 numbering1 => $input->param('numbering1') // '',
62 numbering2 => $input->param('numbering2') // '',
63 numbering3 => $input->param('numbering3') // '',
64 add1 => $input->param('add1') // '',
65 add2 => $input->param('add2') // '',
66 add3 => $input->param('add3') // '',
67 whenmorethan1 => $input->param('whenmorethan1') // '',
68 whenmorethan2 => $input->param('whenmorethan2') // '',
69 whenmorethan3 => $input->param('whenmorethan3') // '',
70 setto1 => $input->param('setto1') // '',
71 setto2 => $input->param('setto2') // '',
72 setto3 => $input->param('setto3') // '',
73 every1 => $input->param('every1') // '',
74 every2 => $input->param('every2') // '',
75 every3 => $input->param('every3') // '',
78 if(!defined $firstacquidate || $firstacquidate eq ''){
79 my ($year, $month, $day) = Today();
80 $firstacquidate = sprintf "%04d-%02d-%02d", $year, $month, $day;
81 } else {
82 $firstacquidate = C4::Dates->new($firstacquidate)->output('iso');
85 if($enddate){
86 $enddate = C4::Dates->new($enddate)->output('iso');
89 if($nextacquidate) {
90 $nextacquidate = C4::Dates->new($nextacquidate)->output('iso');
91 } else {
92 $nextacquidate = $firstacquidate;
94 my $date = $nextacquidate;
96 my %subscription = (
97 locale => $input->param('locale') // '',
98 lastvalue1 => $input->param('lastvalue1') // '',
99 lastvalue2 => $input->param('lastvalue2') // '',
100 lastvalue3 => $input->param('lastvalue3') // '',
101 innerloop1 => $input->param('innerloop1') // '',
102 innerloop2 => $input->param('innerloop2') // '',
103 innerloop3 => $input->param('innerloop3') // '',
104 irregularity => '',
105 periodicity => $frequencyid,
106 countissuesperunit => 1,
107 firstacquidate => $firstacquidate,
110 my $issuenumber;
111 if(defined $subscriptionid) {
112 ($issuenumber) = C4::Serials::GetFictiveIssueNumber(\%subscription, $date);
113 } else {
114 $issuenumber = 1;
117 my @predictions_loop;
118 my ($calculated) = GetSeq(\%subscription, \%pattern);
119 push @predictions_loop, {
120 number => $calculated,
121 publicationdate => $date,
122 issuenumber => $issuenumber,
123 dow => Day_of_Week(split /-/, $date),
125 my @irreg = ();
126 if(defined $subscriptionid) {
127 @irreg = C4::Serials::GetSubscriptionIrregularities($subscriptionid);
128 while(@irreg && $issuenumber > $irreg[0]) {
129 shift @irreg;
131 if(@irreg && $issuenumber == $irreg[0]){
132 $predictions_loop[0]->{'not_published'} = 1;
133 shift @irreg;
137 my $i = 1;
138 while( $i < 1000 ) {
139 my %line;
141 if(defined $date){
142 $date = GetNextDate(\%subscription, $date);
144 if(defined $date){
145 $line{'publicationdate'} = $date;
146 $line{'dow'} = Day_of_Week(split /-/, $date);
149 # Check if we don't have exceed end date
150 if($sublength){
151 if($subtype eq "issues" && $i >= $sublength){
152 last;
153 } elsif($subtype eq "weeks" && $date && Delta_Days( split(/-/, $date), Add_Delta_Days( split(/-/, $firstacquidate), 7*$sublength - 1 ) ) < 0) {
154 last;
155 } elsif($subtype eq "months" && $date && (Delta_Days( split(/-/, $date), Add_Delta_YM( split(/-/, $firstacquidate), 0, $sublength) ) - 1) < 0 ) {
156 last;
159 if($enddate && $date && Delta_Days( split(/-/, $date), split(/-/, $enddate) ) <= 0 ) {
160 last;
163 ($calculated, $subscription{'lastvalue1'}, $subscription{'lastvalue2'}, $subscription{'lastvalue3'}, $subscription{'innerloop1'}, $subscription{'innerloop2'}, $subscription{'innerloop3'}) = GetNextSeq(\%subscription, \%pattern);
164 $issuenumber++;
165 $line{'number'} = $calculated;
166 $line{'issuenumber'} = $issuenumber;
167 if(@irreg && $issuenumber == $irreg[0]){
168 $line{'not_published'} = 1;
169 shift @irreg;
171 push @predictions_loop, \%line;
173 $i++;
176 $template->param(
177 predictions_loop => \@predictions_loop,
180 my $frequency = GetSubscriptionFrequency($frequencyid);
182 if ( $frequency->{unit} and not $custompattern ) {
183 $template->param( ask_for_irregularities => 1 );
184 if ( $frequency->{unit} eq 'day' and $frequency->{unitsperissue} == 1 ) {
185 $template->param( daily_options => 1 );
189 if ( ( $date && $enddate && $date ne $enddate )
190 or ( $subtype eq 'issues' && $i < $sublength ) )
192 $template->param( not_consistent_end_date => 1 );
195 output_html_with_http_headers $input, $cookie, $template->output;