Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.git] / serials / showpredictionpattern.pl
index e56695c..4a3f7b2 100755 (executable)
@@ -1,20 +1,21 @@
 #!/usr/bin/perl
 
-# Copyright 2011 BibLibre SARL
+# Copyright 2011-2013 Biblibre SARL
+#
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 =head1 NAME
 
@@ -29,12 +30,13 @@ publication date, based on frequency and first publication date.
 
 use Modern::Perl;
 
-use CGI;
+use CGI qw ( -utf8 );
 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);
 use C4::Auth;
 use C4::Output;
 use C4::Serials;
 use C4::Serials::Frequency;
+use Koha::DateUtils;
 
 my $input = new CGI;
 my ($template, $loggedinuser, $cookie, $flags) = get_template_and_user( {
@@ -49,72 +51,80 @@ my $subscriptionid = $input->param('subscriptionid');
 my $frequencyid = $input->param('frequency');
 my $firstacquidate = $input->param('firstacquidate');
 my $nextacquidate = $input->param('nextacquidate');
-my $enddate = $input->param('enddate');
+my $enddate = $input->param('to');
 my $subtype = $input->param('subtype');
 my $sublength = $input->param('sublength');
 my $custompattern = $input->param('custompattern');
 
 
-my %val = (
-    locale          => $input->param('locale') // '',
-    numberingmethod => $input->param('numberingmethod') // '',
-    numbering1      => $input->param('numbering1') // '',
-    numbering2      => $input->param('numbering2') // '',
-    numbering3      => $input->param('numbering3') // '',
-    lastvalue1      => $input->param('lastvalue1') // '',
-    lastvalue2      => $input->param('lastvalue2') // '',
-    lastvalue3      => $input->param('lastvalue3') // '',
-    add1            => $input->param('add1') // '',
-    add2            => $input->param('add2') // '',
-    add3            => $input->param('add3') // '',
-    whenmorethan1   => $input->param('whenmorethan1') // '',
-    whenmorethan2   => $input->param('whenmorethan2') // '',
-    whenmorethan3   => $input->param('whenmorethan3') // '',
-    setto1          => $input->param('setto1') // '',
-    setto2          => $input->param('setto2') // '',
-    setto3          => $input->param('setto3') // '',
-    every1          => $input->param('every1') // '',
-    every2          => $input->param('every2') // '',
-    every3          => $input->param('every3') // '',
-    innerloop1      => $input->param('innerloop1') // '',
-    innerloop2      => $input->param('innerloop2') // '',
-    innerloop3      => $input->param('innerloop3') // '',
+my $frequency;
+if ( $frequencyid eq 'mana' ) {
+    $frequency = {
+        'id'            => undef,
+        'displayorder'  => undef,
+        'description'   => scalar $input->param('sfdescription') // '',
+        'unitsperissue' => scalar $input->param('unitsperissue') // '',
+        'issuesperunit' => scalar $input->param('issuesperunit') // '',
+        'unit'          => scalar $input->param('unit') // ''
+    };
+}
+else {
+    $frequency = GetSubscriptionFrequency($frequencyid);
+}
+
+my %pattern = (
+    numberingmethod => scalar $input->param('numberingmethod') // '',
+    numbering1      => scalar $input->param('numbering1') // '',
+    numbering2      => scalar $input->param('numbering2') // '',
+    numbering3      => scalar $input->param('numbering3') // '',
+    add1            => scalar $input->param('add1') // '',
+    add2            => scalar $input->param('add2') // '',
+    add3            => scalar $input->param('add3') // '',
+    whenmorethan1   => scalar $input->param('whenmorethan1') // '',
+    whenmorethan2   => scalar $input->param('whenmorethan2') // '',
+    whenmorethan3   => scalar $input->param('whenmorethan3') // '',
+    setto1          => scalar $input->param('setto1') // '',
+    setto2          => scalar $input->param('setto2') // '',
+    setto3          => scalar $input->param('setto3') // '',
+    every1          => scalar $input->param('every1') // '',
+    every2          => scalar $input->param('every2') // '',
+    every3          => scalar $input->param('every3') // '',
 );
 
-if(!defined $firstacquidate || $firstacquidate eq ''){
-    my ($year, $month, $day) = Today();
-    $firstacquidate = sprintf "%04d-%02d-%02d", $year, $month, $day;
-} else {
-    $firstacquidate = C4::Dates->new($firstacquidate)->output('iso');
-}
+$firstacquidate = eval { output_pref( { str => $firstacquidate, dateonly => 1, dateformat => 'iso' } ); }
+    or output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' } );
 
-if($enddate){
-    $enddate = C4::Dates->new($enddate)->output('iso');
-}
+$enddate = eval { output_pref( { str => $enddate, dateonly => 1, dateformat => 'iso' } ); };
 
 if($nextacquidate) {
-    $nextacquidate = C4::Dates->new($nextacquidate)->output('iso');
+    $nextacquidate =  eval { output_pref( { str => $nextacquidate, dateonly => 1, dateformat => 'iso' } ); };
 } else {
     $nextacquidate = $firstacquidate;
 }
 my $date = $nextacquidate;
 
 my %subscription = (
+    locale      => scalar $input->param('locale') // '',
+    lastvalue1      => scalar $input->param('lastvalue1') // '',
+    lastvalue2      => scalar $input->param('lastvalue2') // '',
+    lastvalue3      => scalar $input->param('lastvalue3') // '',
+    innerloop1      => scalar $input->param('innerloop1') // '',
+    innerloop2      => scalar $input->param('innerloop2') // '',
+    innerloop3      => scalar $input->param('innerloop3') // '',
     irregularity    => '',
-    periodicity     => $frequencyid,
     countissuesperunit  => 1,
     firstacquidate  => $firstacquidate,
 );
 
 my $issuenumber;
 if(defined $subscriptionid) {
-    ($issuenumber) = C4::Serials::GetFictiveIssueNumber(\%subscription, $date);
+    ($issuenumber) = C4::Serials::GetFictiveIssueNumber(\%subscription, $date, $frequency);
 } else {
     $issuenumber = 1;
 }
 
 my @predictions_loop;
-my ($calculated) = GetSeq(\%val);
+my ($calculated) = GetSeq(\%subscription, \%pattern);
 push @predictions_loop, {
     number => $calculated,
     publicationdate => $date,
@@ -138,7 +148,7 @@ while( $i < 1000 ) {
     my %line;
 
     if(defined $date){
-        $date = GetNextDate(\%subscription, $date);
+        $date = GetNextDate(\%subscription, $date, $frequency);
     }
     if(defined $date){
         $line{'publicationdate'} = $date;
@@ -159,7 +169,7 @@ while( $i < 1000 ) {
         last;
     }
 
-    ($calculated, $val{'lastvalue1'}, $val{'lastvalue2'}, $val{'lastvalue3'}, $val{'innerloop1'}, $val{'innerloop2'}, $val{'innerloop3'}) = GetNextSeq(\%val);
+    ($calculated, $subscription{'lastvalue1'}, $subscription{'lastvalue2'}, $subscription{'lastvalue3'}, $subscription{'innerloop1'}, $subscription{'innerloop2'}, $subscription{'innerloop3'}) = GetNextSeq(\%subscription, \%pattern, $frequency);
     $issuenumber++;
     $line{'number'} = $calculated;
     $line{'issuenumber'} = $issuenumber;
@@ -176,8 +186,6 @@ $template->param(
     predictions_loop => \@predictions_loop,
 );
 
-my $frequency = GetSubscriptionFrequency($frequencyid);
-
 if ( $frequency->{unit} and not $custompattern ) {
     $template->param( ask_for_irregularities => 1 );
     if ( $frequency->{unit} eq 'day' and $frequency->{unitsperissue} == 1 ) {