Bug 28007: (follow-up) Address missing instances
[koha.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 = CGI->new;
42 my ($template, $loggedinuser, $cookie, $flags) = get_template_and_user( {
43     template_name   => 'serials/showpredictionpattern.tt',
44     query           => $input,
45     type            => 'intranet',
46     flagsrequired   => { 'serials' => '*' },
47 } );
48
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('to');
54 my $subtype = $input->param('subtype');
55 my $sublength = $input->param('sublength');
56 my $custompattern = $input->param('custompattern');
57
58
59 my $frequency;
60 if ( $frequencyid eq 'mana' ) {
61     $frequency = {
62         'id'            => undef,
63         'displayorder'  => undef,
64         'description'   => scalar $input->param('sfdescription') // '',
65         'unitsperissue' => scalar $input->param('unitsperissue') // '',
66         'issuesperunit' => scalar $input->param('issuesperunit') // '',
67         'unit'          => scalar $input->param('unit') // ''
68     };
69 }
70 else {
71     $frequency = GetSubscriptionFrequency($frequencyid);
72 }
73
74 my %pattern = (
75     numberingmethod => scalar $input->param('numberingmethod') // '',
76     numbering1      => scalar $input->param('numbering1') // '',
77     numbering2      => scalar $input->param('numbering2') // '',
78     numbering3      => scalar $input->param('numbering3') // '',
79     add1            => scalar $input->param('add1') // '',
80     add2            => scalar $input->param('add2') // '',
81     add3            => scalar $input->param('add3') // '',
82     whenmorethan1   => scalar $input->param('whenmorethan1') // '',
83     whenmorethan2   => scalar $input->param('whenmorethan2') // '',
84     whenmorethan3   => scalar $input->param('whenmorethan3') // '',
85     setto1          => scalar $input->param('setto1') // '',
86     setto2          => scalar $input->param('setto2') // '',
87     setto3          => scalar $input->param('setto3') // '',
88     every1          => scalar $input->param('every1') // '',
89     every2          => scalar $input->param('every2') // '',
90     every3          => scalar $input->param('every3') // '',
91 );
92
93 $firstacquidate = eval { output_pref( { str => $firstacquidate, dateonly => 1, dateformat => 'iso' } ); }
94     or output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' } );
95
96 $enddate = eval { output_pref( { str => $enddate, dateonly => 1, dateformat => 'iso' } ); };
97
98 if($nextacquidate) {
99     $nextacquidate =  eval { output_pref( { str => $nextacquidate, dateonly => 1, dateformat => 'iso' } ); };
100 } else {
101     $nextacquidate = $firstacquidate;
102 }
103 my $date = $nextacquidate;
104
105 my %subscription = (
106     locale      => scalar $input->param('locale') // '',
107     lastvalue1      => scalar $input->param('lastvalue1') // '',
108     lastvalue2      => scalar $input->param('lastvalue2') // '',
109     lastvalue3      => scalar $input->param('lastvalue3') // '',
110     innerloop1      => scalar $input->param('innerloop1') // '',
111     innerloop2      => scalar $input->param('innerloop2') // '',
112     innerloop3      => scalar $input->param('innerloop3') // '',
113     irregularity    => '',
114     countissuesperunit  => 1,
115     firstacquidate  => $firstacquidate,
116 );
117
118 my $issuenumber;
119 if(defined $subscriptionid) {
120     ($issuenumber) = C4::Serials::GetFictiveIssueNumber(\%subscription, $date, $frequency);
121 } else {
122     $issuenumber = 1;
123 }
124
125 my @predictions_loop;
126 my ($calculated) = GetSeq(\%subscription, \%pattern);
127 push @predictions_loop, {
128     number => $calculated,
129     publicationdate => $date,
130     issuenumber => $issuenumber,
131     dow => Day_of_Week(split /-/, $date),
132 };
133 my @irreg = ();
134 if(defined $subscriptionid) {
135     @irreg = C4::Serials::GetSubscriptionIrregularities($subscriptionid);
136     while(@irreg && $issuenumber > $irreg[0]) {
137         shift @irreg;
138     }
139     if(@irreg && $issuenumber == $irreg[0]){
140         $predictions_loop[0]->{'not_published'} = 1;
141         shift @irreg;
142     }
143 }
144
145 my $i = 1;
146 while( $i < 1000 ) {
147     my %line;
148
149     if(defined $date){
150         $date = GetNextDate(\%subscription, $date, $frequency);
151     }
152     if(defined $date){
153         $line{'publicationdate'} = $date;
154         $line{'dow'} = Day_of_Week(split /-/, $date);
155     }
156
157     # Check if we don't have exceed end date
158     if($sublength){
159         if($subtype eq "issues" && $i >= $sublength){
160             last;
161         } elsif($subtype eq "weeks" && $date && Delta_Days( split(/-/, $date), Add_Delta_Days( split(/-/, $firstacquidate), 7*$sublength - 1 ) ) < 0) {
162             last;
163         } elsif($subtype eq "months" && $date && (Delta_Days( split(/-/, $date), Add_Delta_YM( split(/-/, $firstacquidate), 0, $sublength) ) - 1) < 0 ) {
164             last;
165         }
166     }
167     if($enddate && $date && Delta_Days( split(/-/, $date), split(/-/, $enddate) ) <= 0 ) {
168         last;
169     }
170
171     ($calculated, $subscription{'lastvalue1'}, $subscription{'lastvalue2'}, $subscription{'lastvalue3'}, $subscription{'innerloop1'}, $subscription{'innerloop2'}, $subscription{'innerloop3'}) = GetNextSeq(\%subscription, \%pattern, $frequency);
172     $issuenumber++;
173     $line{'number'} = $calculated;
174     $line{'issuenumber'} = $issuenumber;
175     if(@irreg && $issuenumber == $irreg[0]){
176         $line{'not_published'} = 1;
177         shift @irreg;
178     }
179     push @predictions_loop, \%line;
180
181     $i++;
182 }
183
184 $template->param(
185     predictions_loop => \@predictions_loop,
186 );
187
188 if ( $frequency->{unit} and not $custompattern ) {
189     $template->param( ask_for_irregularities => 1 );
190     if ( $frequency->{unit} eq 'day' and $frequency->{unitsperissue} == 1 ) {
191         $template->param( daily_options => 1 );
192     }
193 }
194
195 if (   ( $date && $enddate && $date ne $enddate )
196     or ( $subtype eq 'issues' && $i < $sublength ) )
197 {
198     $template->param( not_consistent_end_date => 1 );
199 }
200
201 output_html_with_http_headers $input, $cookie, $template->output;