Bug 27920: Add ability to update patron expiration dates when importing patrons
[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( Add_Delta_Days Add_Delta_YM Day_of_Week Delta_Days );
35 use C4::Auth qw( get_template_and_user );
36 use C4::Output qw( output_html_with_http_headers );
37 use C4::Serials qw( GetSubscription GetFictiveIssueNumber GetSeq GetSubscriptionIrregularities GetNextDate GetNextSeq );
38 use C4::Serials::Frequency;
39 use Koha::DateUtils qw( dt_from_string );
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 = $firstacquidate ? dt_from_string($firstacquidate)->ymd : dt_from_string->ymd;
94
95 $enddate = dt_from_string($enddate)->ymd;
96
97 $nextacquidate = $nextacquidate ? dt_from_string($nextacquidate)->ymd : $firstacquidate;
98 my $date = $nextacquidate;
99
100 my %subscription = (
101     locale      => scalar $input->param('locale') // '',
102     lastvalue1      => scalar $input->param('lastvalue1') // '',
103     lastvalue2      => scalar $input->param('lastvalue2') // '',
104     lastvalue3      => scalar $input->param('lastvalue3') // '',
105     innerloop1      => scalar $input->param('innerloop1') // '',
106     innerloop2      => scalar $input->param('innerloop2') // '',
107     innerloop3      => scalar $input->param('innerloop3') // '',
108     irregularity    => '',
109     countissuesperunit  => 1,
110     firstacquidate  => $firstacquidate,
111 );
112
113 my $issuenumber;
114 if(defined $subscriptionid) {
115     ($issuenumber) = C4::Serials::GetFictiveIssueNumber(\%subscription, $date, $frequency);
116 } else {
117     $issuenumber = 1;
118 }
119
120 my @predictions_loop;
121 my ($calculated) = GetSeq(\%subscription, \%pattern);
122 push @predictions_loop, {
123     number => $calculated,
124     publicationdate => $date,
125     issuenumber => $issuenumber,
126     dow => Day_of_Week(split /-/, $date),
127 };
128 my @irreg = ();
129 if(defined $subscriptionid) {
130     @irreg = C4::Serials::GetSubscriptionIrregularities($subscriptionid);
131     while(@irreg && $issuenumber > $irreg[0]) {
132         shift @irreg;
133     }
134     if(@irreg && $issuenumber == $irreg[0]){
135         $predictions_loop[0]->{'not_published'} = 1;
136         shift @irreg;
137     }
138 }
139
140 my $i = 1;
141 while( $i < 1000 ) {
142     my %line;
143
144     if(defined $date){
145         $date = GetNextDate(\%subscription, $date, $frequency);
146     }
147     if(defined $date){
148         $line{'publicationdate'} = $date;
149         $line{'dow'} = Day_of_Week(split /-/, $date);
150     }
151
152     # Check if we don't have exceed end date
153     if($sublength){
154         if($subtype eq "issues" && $i >= $sublength){
155             last;
156         } elsif($subtype eq "weeks" && $date && Delta_Days( split(/-/, $date), Add_Delta_Days( split(/-/, $firstacquidate), 7*$sublength - 1 ) ) < 0) {
157             last;
158         } elsif($subtype eq "months" && $date && (Delta_Days( split(/-/, $date), Add_Delta_YM( split(/-/, $firstacquidate), 0, $sublength) ) - 1) < 0 ) {
159             last;
160         }
161     }
162     if($enddate && $date && Delta_Days( split(/-/, $date), split(/-/, $enddate) ) <= 0 ) {
163         last;
164     }
165
166     ($calculated, $subscription{'lastvalue1'}, $subscription{'lastvalue2'}, $subscription{'lastvalue3'}, $subscription{'innerloop1'}, $subscription{'innerloop2'}, $subscription{'innerloop3'}) = GetNextSeq(\%subscription, \%pattern, $frequency);
167     $issuenumber++;
168     $line{'number'} = $calculated;
169     $line{'issuenumber'} = $issuenumber;
170     if(@irreg && $issuenumber == $irreg[0]){
171         $line{'not_published'} = 1;
172         shift @irreg;
173     }
174     push @predictions_loop, \%line;
175
176     $i++;
177 }
178
179 $template->param(
180     predictions_loop => \@predictions_loop,
181 );
182
183 if ( $frequency->{unit} and not $custompattern ) {
184     $template->param( ask_for_irregularities => 1 );
185     if ( $frequency->{unit} eq 'day' and $frequency->{unitsperissue} == 1 ) {
186         $template->param( daily_options => 1 );
187     }
188 }
189
190 if (   ( $date && $enddate && $date ne $enddate )
191     or ( $subtype eq 'issues' && $i < $sublength ) )
192 {
193     $template->param( not_consistent_end_date => 1 );
194 }
195
196 output_html_with_http_headers $input, $cookie, $template->output;