Merge branch 'bug_9506' into 3.12-master
[koha.git] / serials / showpredictionpattern.pl
1 #!/usr/bin/perl
2
3 # Copyright 2011 BibLibre SARL
4 # This file is part of Koha.
5 #
6 # Koha is free software; you can redistribute it and/or modify it under the
7 # terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
9 # version.
10 #
11 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Koha; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 =head1 NAME
20
21 showpredictionpattern.pl
22
23 =head1 DESCRIPTION
24
25 This script calculate numbering of serials based on numbering pattern, and
26 publication date, based on frequency and first publication date.
27
28 =cut
29
30 use Modern::Perl;
31
32 use CGI;
33 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);
34 use C4::Auth;
35 use C4::Output;
36 use C4::Serials;
37 use C4::Serials::Frequency;
38
39 my $input = new CGI;
40 my ($template, $loggedinuser, $cookie, $flags) = get_template_and_user( {
41     template_name   => 'serials/showpredictionpattern.tt',
42     query           => $input,
43     type            => 'intranet',
44     authnotrequired => 0,
45     flagsrequired   => { 'serials' => '*' },
46 } );
47
48 my $subscriptionid = $input->param('subscriptionid');
49 my $frequencyid = $input->param('frequency');
50 my $firstacquidate = $input->param('firstacquidate');
51 my $nextacquidate = $input->param('nextacquidate');
52 my $enddate = $input->param('enddate');
53 my $subtype = $input->param('subtype');
54 my $sublength = $input->param('sublength');
55 my $custompattern = $input->param('custompattern');
56
57
58 my %pattern = (
59     numberingmethod => $input->param('numberingmethod') // '',
60     numbering1      => $input->param('numbering1') // '',
61     numbering2      => $input->param('numbering2') // '',
62     numbering3      => $input->param('numbering3') // '',
63     add1            => $input->param('add1') // '',
64     add2            => $input->param('add2') // '',
65     add3            => $input->param('add3') // '',
66     whenmorethan1   => $input->param('whenmorethan1') // '',
67     whenmorethan2   => $input->param('whenmorethan2') // '',
68     whenmorethan3   => $input->param('whenmorethan3') // '',
69     setto1          => $input->param('setto1') // '',
70     setto2          => $input->param('setto2') // '',
71     setto3          => $input->param('setto3') // '',
72     every1          => $input->param('every1') // '',
73     every2          => $input->param('every2') // '',
74     every3          => $input->param('every3') // '',
75 );
76
77 if(!defined $firstacquidate || $firstacquidate eq ''){
78     my ($year, $month, $day) = Today();
79     $firstacquidate = sprintf "%04d-%02d-%02d", $year, $month, $day;
80 } else {
81     $firstacquidate = C4::Dates->new($firstacquidate)->output('iso');
82 }
83
84 if($enddate){
85     $enddate = C4::Dates->new($enddate)->output('iso');
86 }
87
88 if($nextacquidate) {
89     $nextacquidate = C4::Dates->new($nextacquidate)->output('iso');
90 } else {
91     $nextacquidate = $firstacquidate;
92 }
93 my $date = $nextacquidate;
94
95 my %subscription = (
96     locale      => $input->param('locale') // '',
97     lastvalue1      => $input->param('lastvalue1') // '',
98     lastvalue2      => $input->param('lastvalue2') // '',
99     lastvalue3      => $input->param('lastvalue3') // '',
100     innerloop1      => $input->param('innerloop1') // '',
101     innerloop2      => $input->param('innerloop2') // '',
102     innerloop3      => $input->param('innerloop3') // '',
103     irregularity    => '',
104     periodicity     => $frequencyid,
105     countissuesperunit  => 1,
106     firstacquidate  => $firstacquidate,
107 );
108
109 my $issuenumber;
110 if(defined $subscriptionid) {
111     ($issuenumber) = C4::Serials::GetFictiveIssueNumber(\%subscription, $date);
112 } else {
113     $issuenumber = 1;
114 }
115
116 my @predictions_loop;
117 my ($calculated) = GetSeq(\%subscription, \%pattern);
118 push @predictions_loop, {
119     number => $calculated,
120     publicationdate => $date,
121     issuenumber => $issuenumber,
122     dow => Day_of_Week(split /-/, $date),
123 };
124 my @irreg = ();
125 if(defined $subscriptionid) {
126     @irreg = C4::Serials::GetSubscriptionIrregularities($subscriptionid);
127     while(@irreg && $issuenumber > $irreg[0]) {
128         shift @irreg;
129     }
130     if(@irreg && $issuenumber == $irreg[0]){
131         $predictions_loop[0]->{'not_published'} = 1;
132         shift @irreg;
133     }
134 }
135
136 my $i = 1;
137 while( $i < 1000 ) {
138     my %line;
139
140     if(defined $date){
141         $date = GetNextDate(\%subscription, $date);
142     }
143     if(defined $date){
144         $line{'publicationdate'} = $date;
145         $line{'dow'} = Day_of_Week(split /-/, $date);
146     }
147
148     # Check if we don't have exceed end date
149     if($sublength){
150         if($subtype eq "issues" && $i >= $sublength){
151             last;
152         } elsif($subtype eq "weeks" && $date && Delta_Days( split(/-/, $date), Add_Delta_Days( split(/-/, $firstacquidate), 7*$sublength - 1 ) ) < 0) {
153             last;
154         } elsif($subtype eq "months" && $date && (Delta_Days( split(/-/, $date), Add_Delta_YM( split(/-/, $firstacquidate), 0, $sublength) ) - 1) < 0 ) {
155             last;
156         }
157     }
158     if($enddate && $date && Delta_Days( split(/-/, $date), split(/-/, $enddate) ) <= 0 ) {
159         last;
160     }
161
162     ($calculated, $subscription{'lastvalue1'}, $subscription{'lastvalue2'}, $subscription{'lastvalue3'}, $subscription{'innerloop1'}, $subscription{'innerloop2'}, $subscription{'innerloop3'}) = GetNextSeq(\%subscription, \%pattern);
163     $issuenumber++;
164     $line{'number'} = $calculated;
165     $line{'issuenumber'} = $issuenumber;
166     if(@irreg && $issuenumber == $irreg[0]){
167         $line{'not_published'} = 1;
168         shift @irreg;
169     }
170     push @predictions_loop, \%line;
171
172     $i++;
173 }
174
175 $template->param(
176     predictions_loop => \@predictions_loop,
177 );
178
179 my $frequency = GetSubscriptionFrequency($frequencyid);
180
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 );
185     }
186 }
187
188 if (   ( $date && $enddate && $date ne $enddate )
189     or ( $subtype eq 'issues' && $i < $sublength ) )
190 {
191     $template->param( not_consistent_end_date => 1 );
192 }
193
194 output_html_with_http_headers $input, $cookie, $template->output;