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