Bug 12598: Add use Koha::Patron::debarments statement
[koha.git] / tools / overduerules.pl
index 178a569..616f607 100755 (executable)
@@ -4,39 +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 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.
@@ -59,27 +57,38 @@ 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.tt",
-                            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 (?,?,?,?,?,?,?,?,?,?,?)");
@@ -87,14 +96,14 @@ if ($op eq 'save') {
     my $sth_delete=$dbh->prepare("DELETE FROM overduerules WHERE branchcode=? AND categorycode=?");
     my $sth_insert_mtt = $dbh->prepare("
         INSERT INTO overduerules_transport_types(
-            branchcode, categorycode, letternumber, message_transport_type
+            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 branchcode = ? AND categorycode = ?
+        WHERE overduerules_id = (SELECT overduerules_id FROM overduerules WHERE branchcode = ? AND categorycode = ?)
     ");
 
     foreach my $key (@names){
@@ -118,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
@@ -182,7 +191,7 @@ if ($op eq 'save') {
 
                     $sth_delete_mtt->execute( $branch, $bor );
                     for my $letternumber ( 1..3 ) {
-                        my @mtt = $input->param( "mtt${letternumber}-$bor" );
+                        my @mtt = $input->multi_param( "mtt${letternumber}-$bor" );
                         next unless @mtt;
                         for my $mtt ( @mtt ) {
                             $sth_insert_mtt->execute( $branch, $bor, $letternumber, $mtt);
@@ -199,7 +208,6 @@ if ($op eq 'save') {
         $input_saved = 1;
     }
 }
-my $branchloop = GetBranchesLoop($branch);
 
 my $letters = C4::Letters::GetLettersAvailableForALibrary(
     {
@@ -212,7 +220,7 @@ my @line_loop;
 
 my $message_transport_types = C4::Letters::GetMessageTransportTypes();
 my ( @first, @second, @third );
-for my $data (@categories) {
+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
@@ -220,13 +228,13 @@ for my $data (@categories) {
         # bugs where the form submission was not correctly saved
         for my $i ( 1..3 ){
             my %row = (
-                overduename => $data->{'categorycode'},
-                line        => $data->{'description'}
+                overduename => $patron_category->categorycode,
+                line        => $patron_category->description,
             );
-            $row{delay}=$temphash{$data->{'categorycode'}}->{"delay$i"};
-            $row{debarred}=$temphash{$data->{'categorycode'}}->{"debarred$i"};
-            $row{selected_lettercode} = $temphash{ $data->{categorycode} }->{"letter$i"};
-            my @selected_mtts = @{ GetOverdueMessageTransportTypes( $branch, $data->{'categorycode'}, $i) };
+            $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, {
@@ -246,19 +254,19 @@ for my $data (@categories) {
     } 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..3 ){
             my %row = (
-                overduename => $data->{'categorycode'},
-                line        => $data->{'description'}
+                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, $data->{'categorycode'}, $i) };
+            my @selected_mtts = @{ GetOverdueMessageTransportTypes( $branch, $patron_category->categorycode, $i) };
             my @mtts;
             for my $mtt ( @$message_transport_types ) {
                 push @mtts, {
@@ -299,7 +307,6 @@ my @tabs = (
 
 $template->param(
     table => ( @first or @second or @third ? 1 : 0 ),
-    branchloop => $branchloop,
     branch => $branch,
     tabs => \@tabs,
     message_transport_types => $message_transport_types,