Bug 12748: Code tidy
[srvgit] / tools / overduerules.pl
index a824bd4..1478c3a 100755 (executable)
@@ -4,22 +4,22 @@
 #
 # 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 CGI qw ( -utf8 );
 use C4::Context;
 use C4::Output;
 use C4::Auth;
@@ -28,6 +28,7 @@ use C4::Branch;
 use C4::Letters;
 use C4::Members;
 use C4::Overdues;
+use Koha::Libraries;
 
 our $input = new CGI;
 my $dbh = C4::Context->dbh;
@@ -59,27 +60,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::Branch::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 +99,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){
@@ -182,7 +194,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);