Bug 18789: (QA follow-up) Fix viewlog when accessed via circulation
[koha.git] / tools / overduerules.pl
index cc90d0b..616f607 100755 (executable)
@@ -4,38 +4,37 @@
 #
 # 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>.
 
-use strict;
-use warnings;
-use CGI;
+use Modern::Perl;
+use CGI qw ( -utf8 );
 use C4::Context;
 use C4::Output;
 use C4::Auth;
 use C4::Koha;
-use C4::Branch;
 use C4::Letters;
 use C4::Members;
+use C4::Overdues;
+use Koha::Libraries;
+
+use Koha::Patron::Categories;
 
 our $input = new CGI;
 my $dbh = C4::Context->dbh;
 
-my @categories = @{$dbh->selectall_arrayref(
-    'SELECT description, categorycode FROM categories WHERE overduenoticerequired > 0',
-    { Slice => {} }
-)};
-my @category_codes  = map { $_->{categorycode} } @categories;
+my @patron_categories = Koha::Patron::Categories->search( { overduenoticerequired => { '>' => 0 } } );
+my @category_codes  = map { $_->categorycode } @patron_categories;
 our @rule_params     = qw(delay letter debarred);
 
 # blank_row($category_code) - return true if the entire row is blank.
@@ -44,11 +43,11 @@ sub blank_row {
     for my $rp (@rule_params) {
         for my $n (1 .. 3) {
             my $key   = "${rp}${n}-$category_code";
-            
+
             if (utf8::is_utf8($key)) {
               utf8::encode($key);
             }
-            
+
             my $value = $input->param($key);
             if ($value) {
                 return 0;
@@ -58,32 +57,55 @@ sub blank_row {
     return 1;
 }
 
-my $type=$input->param('type');
+my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
+    {
+        template_name   => "tools/overduerules.tt",
+        query           => $input,
+        type            => "intranet",
+        authnotrequired => 0,
+        flagsrequired   => { tools => 'edit_notice_status_triggers' },
+        debug           => 1,
+    }
+);
+
+my $type = $input->param('type');
+
 my $branch = $input->param('branch');
+$branch =
+    defined $branch                                                    ? $branch
+  : C4::Context->preference('DefaultToLoggedInLibraryOverdueTriggers') ? C4::Context::mybranch()
+  : Koha::Libraries->search->count() == 1                              ? undef
+  :                                                                      undef;
 $branch ||= q{};
+$branch = q{} if $branch eq 'NO_LIBRARY_SET';
+
 my $op = $input->param('op');
 $op ||= q{};
 
-my ($template, $loggedinuser, $cookie)
-    = get_template_and_user({template_name => "tools/overduerules.tmpl",
-                            query => $input,
-                            type => "intranet",
-                            authnotrequired => 0,
-                            flagsrequired => { tools => 'edit_notice_status_triggers'},
-                            debug => 1,
-                            });
 my $err=0;
 
 # save the values entered into tables
 my %temphash;
 my $input_saved = 0;
 if ($op eq 'save') {
-    my @names=$input->param();
+    my @names=$input->multi_param();
     my $sth_search = $dbh->prepare("SELECT count(*) AS total FROM overduerules WHERE branchcode=? AND categorycode=?");
 
     my $sth_insert = $dbh->prepare("INSERT INTO overduerules (branchcode,categorycode, delay1,letter1,debarred1, delay2,letter2,debarred2, delay3,letter3,debarred3) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
     my $sth_update=$dbh->prepare("UPDATE overduerules SET delay1=?, letter1=?, debarred1=?, delay2=?, letter2=?, debarred2=?, delay3=?, letter3=?, debarred3=? WHERE branchcode=? AND categorycode=?");
     my $sth_delete=$dbh->prepare("DELETE FROM overduerules WHERE branchcode=? AND categorycode=?");
+    my $sth_insert_mtt = $dbh->prepare("
+        INSERT INTO overduerules_transport_types(
+            overduerules_id, letternumber, message_transport_type
+        ) VALUES (
+            (SELECT overduerules_id FROM overduerules WHERE branchcode = ? AND categorycode = ?), ?, ?
+        )
+    ");
+    my $sth_delete_mtt = $dbh->prepare("
+        DELETE FROM overduerules_transport_types
+        WHERE overduerules_id = (SELECT overduerules_id FROM overduerules WHERE branchcode = ? AND categorycode = ?)
+    ");
+
     foreach my $key (@names){
             # ISSUES
             if ($key =~ /(delay|letter|debarred)([1-3])-(.*)/) {
@@ -105,8 +127,8 @@ if ($op eq 'save') {
 
     foreach my $bor (keys %temphash){
         # get category name if we need it for an error message
-        my $bor_category = GetBorrowercategory($bor);
-        my $bor_category_name = defined($bor_category) ? $bor_category->{description} : $bor;
+        my $bor_category = Koha::Patron::Categories->find($bor);
+        my $bor_category_name = $bor_category ? $bor_category->description : $bor;
 
         # Do some Checking here : delay1 < delay2 <delay3 all of them being numbers
         # Raise error if not true
@@ -166,6 +188,15 @@ if ($op eq 'save') {
                             ($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0)
                             );
                     }
+
+                    $sth_delete_mtt->execute( $branch, $bor );
+                    for my $letternumber ( 1..3 ) {
+                        my @mtt = $input->multi_param( "mtt${letternumber}-$bor" );
+                        next unless @mtt;
+                        for my $mtt ( @mtt ) {
+                            $sth_insert_mtt->execute( $branch, $bor, $letternumber, $mtt);
+                        }
+                    }
                 }
         }
     }
@@ -177,79 +208,108 @@ if ($op eq 'save') {
         $input_saved = 1;
     }
 }
-my $branchloop = GetBranchesLoop($branch);
 
-my $letters = GetLetters("circulation");
-
-my $countletters = keys %{$letters};
+my $letters = C4::Letters::GetLettersAvailableForALibrary(
+    {
+        branchcode => $branch,
+        module => "circulation",
+    }
+);
 
 my @line_loop;
 
-for my $data (@categories) {
-    my %row = (
-        overduename => $data->{'categorycode'},
-        line        => $data->{'description'}
-    );
+my $message_transport_types = C4::Letters::GetMessageTransportTypes();
+my ( @first, @second, @third );
+for my $patron_category (@patron_categories) {
     if (%temphash and not $input_saved){
         # if we managed to save the form submission, don't
         # reuse %temphash, but take the values from the
         # database - this makes it easier to identify
         # bugs where the form submission was not correctly saved
-        for (my $i=1;$i<=3;$i++){
-            $row{"delay$i"}=$temphash{$data->{'categorycode'}}->{"delay$i"};
-            $row{"debarred$i"}=$temphash{$data->{'categorycode'}}->{"debarred$i"};
-            if ($countletters){
-                my @letterloop;
-                foreach my $thisletter (sort { $letters->{$a} cmp $letters->{$b} } keys %$letters) {
-                    my $selected;
-                    if ( $temphash{$data->{categorycode}}->{"letter$i"} &&
-                        $thisletter eq $temphash{$data->{'categorycode'}}->{"letter$i"}) {
-                        $selected = 1;
-                    }
-                    my %letterrow =(value => $thisletter,
-                                    selected => $selected,
-                                    lettername => $letters->{$thisletter},
-                                    );
-                    push @letterloop, \%letterrow;
+        for my $i ( 1..3 ){
+            my %row = (
+                overduename => $patron_category->categorycode,
+                line        => $patron_category->description,
+            );
+            $row{delay}=$temphash{$patron_category->categorycode}->{"delay$i"};
+            $row{debarred}=$temphash{$patron_category->categorycode}->{"debarred$i"};
+            $row{selected_lettercode} = $temphash{ $patron_category->categorycode }->{"letter$i"};
+            my @selected_mtts = @{ GetOverdueMessageTransportTypes( $branch, $patron_category->categorycode, $i) };
+            my @mtts;
+            for my $mtt ( @$message_transport_types ) {
+                push @mtts, {
+                    value => $mtt,
+                    selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 ,
                 }
-                $row{"letterloop$i"}=\@letterloop;
+            }
+            $row{message_transport_types} = \@mtts;
+            if ( $i == 1 ) {
+                push @first, \%row;
+            } elsif ( $i == 2 ) {
+                push @second, \%row;
             } else {
-                $row{"noletter"}=1;
-                $row{"letter$i"}=$temphash{$data->{'categorycode'}}->{"letter$i"};
+                push @third, \%row;
             }
         }
     } else {
     #getting values from table
         my $sth2=$dbh->prepare("SELECT * from overduerules WHERE branchcode=? AND categorycode=?");
-        $sth2->execute($branch,$data->{'categorycode'});
+        $sth2->execute($branch,$patron_category->categorycode);
         my $dat=$sth2->fetchrow_hashref;
-        for (my $i=1;$i<=3;$i++){
-            if ($countletters){
-                my @letterloop;
-                foreach my $thisletter (sort { $letters->{$a} cmp $letters->{$b} } keys %$letters) {
-                    my $selected;
-                    if ($dat->{"letter$i"} && $thisletter eq $dat->{"letter$i"}) {
-                        $selected = 1;
-                    }
-                    my %letterrow =(value => $thisletter,
-                                    selected => $selected,
-                                    lettername => $letters->{$thisletter},
-                                    );
-                    push @letterloop, \%letterrow;
+        for my $i ( 1..3 ){
+            my %row = (
+                overduename => $patron_category->categorycode,
+                line        => $patron_category->description,
+            );
+
+            $row{selected_lettercode} = $dat->{"letter$i"};
+
+            if ($dat->{"delay$i"}){$row{delay}=$dat->{"delay$i"};}
+            if ($dat->{"debarred$i"}){$row{debarred}=$dat->{"debarred$i"};}
+            my @selected_mtts = @{ GetOverdueMessageTransportTypes( $branch, $patron_category->categorycode, $i) };
+            my @mtts;
+            for my $mtt ( @$message_transport_types ) {
+                push @mtts, {
+                    value => $mtt,
+                    selected => ( grep {/$mtt/} @selected_mtts ) ? 1 : 0 ,
                 }
-                $row{"letterloop$i"}=\@letterloop;
+            }
+            $row{message_transport_types} = \@mtts;
+            if ( $i == 1 ) {
+                push @first, \%row;
+            } elsif ( $i == 2 ) {
+                push @second, \%row;
             } else {
-                $row{"noletter"}=1;
-                if ($dat->{"letter$i"}){$row{"letter$i"}=$dat->{"letter$i"};}
+                push @third, \%row;
             }
-            if ($dat->{"delay$i"}){$row{"delay$i"}=$dat->{"delay$i"};}
-            if ($dat->{"debarred$i"}){$row{"debarred$i"}=$dat->{"debarred$i"};}
+
         }
     }
-    push @line_loop,\%row;
 }
 
-$template->param(table=> \@line_loop,
-                branchloop => $branchloop,
-                branch => $branch);
+my @tabs = (
+    {
+        id => 'first',
+        number => 1,
+        values => \@first,
+    },
+    {
+        id => 'second',
+        number => 2,
+        values => \@second,
+    },
+    {
+        id => 'third',
+        number => 3,
+        values => \@third,
+    },
+);
+
+$template->param(
+    table => ( @first or @second or @third ? 1 : 0 ),
+    branch => $branch,
+    tabs => \@tabs,
+    message_transport_types => $message_transport_types,
+    letters => $letters,
+);
 output_html_with_http_headers $input, $cookie, $template->output;