From 5f741230087e91c94063e1cf35308d07ebcdc18f Mon Sep 17 00:00:00 2001
From: John Doe <you@example.com>
Date: Fri, 27 Jan 2023 14:36:40 +0000
Subject: [PATCH] Bug 29145: (QA follow-up) Use objects instead of straight SQL
 queries

---
 Koha/OverdueRule.pm               | 40 +++++++++++++++++++++++++
 Koha/OverdueRules.pm              | 50 +++++++++++++++++++++++++++++++
 Koha/Patron.pm                    | 38 ++++++++++++-----------
 Koha/Schema/Result/Overduerule.pm |  7 ++++-
 4 files changed, 116 insertions(+), 19 deletions(-)
 create mode 100644 Koha/OverdueRule.pm
 create mode 100644 Koha/OverdueRules.pm

diff --git a/Koha/OverdueRule.pm b/Koha/OverdueRule.pm
new file mode 100644
index 0000000000..16152a5098
--- /dev/null
+++ b/Koha/OverdueRule.pm
@@ -0,0 +1,40 @@
+package Koha::OverdueRule;
+
+# Copyright ByWater Solutions 2023
+#
+# 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 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.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use base qw(Koha::Object);
+
+=head1 NAME
+
+Koha::OverdueRule - Koha Overdue rule object class
+
+=head1 API
+
+=head2 Class Methods
+
+=head3 _type
+
+=cut
+
+sub _type {
+    return 'Overduerule';
+}
+
+1;
diff --git a/Koha/OverdueRules.pm b/Koha/OverdueRules.pm
new file mode 100644
index 0000000000..a958ee031c
--- /dev/null
+++ b/Koha/OverdueRules.pm
@@ -0,0 +1,50 @@
+package Koha::OverdueRules;
+
+# Copyright ByWater Solutions 2023
+#
+# 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 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.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use Koha::OverdueRule;
+
+use base qw(Koha::Objects);
+
+=head1 NAME
+
+Koha::OverdueRules - Koha::OverdueRule Object set class
+
+=head1 API
+
+=head2 Class Methods
+
+=head3 _type
+
+=cut
+
+sub _type {
+    return 'Overduerule';
+}
+
+=head3 object_class
+
+=cut
+
+sub object_class {
+    return 'Koha::OverdueRule';
+}
+
+1;
diff --git a/Koha/Patron.pm b/Koha/Patron.pm
index 9fcf0d654e..1a6c1c78da 100644
--- a/Koha/Patron.pm
+++ b/Koha/Patron.pm
@@ -25,23 +25,24 @@ use JSON qw( to_json );
 use Unicode::Normalize qw( NFKD );
 use Try::Tiny;
 
-use C4::Context;
 use C4::Auth qw( checkpw_hash );
+use C4::Context;
+use C4::Letters qw( GetPreparedLetter EnqueueLetter SendQueuedMessages );
 use C4::Log qw( logaction );
 use Koha::Account;
 use Koha::ArticleRequests;
-use C4::Letters qw( GetPreparedLetter EnqueueLetter SendQueuedMessages );
 use Koha::AuthUtils;
 use Koha::Checkouts;
 use Koha::CirculationRules;
 use Koha::Club::Enrollments;
+use Koha::CurbsidePickups;
 use Koha::Database;
 use Koha::DateUtils qw( dt_from_string );
 use Koha::Encryption;
 use Koha::Exceptions::Password;
 use Koha::Holds;
-use Koha::CurbsidePickups;
 use Koha::Old::Checkouts;
+use Koha::OverdueRules;
 use Koha::Patron::Attributes;
 use Koha::Patron::Categories;
 use Koha::Patron::Debarments;
@@ -1044,23 +1045,24 @@ sub _get_overdue_debarred_delay {
     my ($branchcode, $categorycode) = @_;
     my $dbh = C4::Context->dbh();
 
-    my $query = "SELECT * FROM overduerules WHERE delay1 IS NOT NULL AND branchcode = ? AND categorycode = ?";
-
-    my $rqoverduerules = $dbh->prepare($query);
-    $rqoverduerules->execute($branchcode, $categorycode);
-
     # We get default rules if there is no rule for this branch
-    if($rqoverduerules->rows == 0) {
-        $query = "SELECT * FROM overduerules WHERE delay1 IS NOT NULL AND branchcode = '' AND categorycode = ?";
-
-        $rqoverduerules = $dbh->prepare($query);
-        $rqoverduerules->execute($categorycode);
-    }
+    my $rule = Koha::OverdueRules->find(
+        {
+            branchcode   => $branchcode,
+            categorycode => $categorycode
+        }
+      )
+      || Koha::OverdueRules->find(
+        {
+            branchcode   => q{},
+            categorycode => $categorycode
+        }
+      );
 
-    while ( my $overdue_rules = $rqoverduerules->fetchrow_hashref ) {
-        return $overdue_rules->{"delay1"} if($overdue_rules->{"debarred1"});
-        return $overdue_rules->{"delay2"} if($overdue_rules->{"debarred2"});
-        return $overdue_rules->{"delay3"} if($overdue_rules->{"debarred3"});
+    if ( $rule ) {
+        return $rule->delay1 if $rule->debarred1;
+        return $rule->delay2 if $rule->debarred2;
+        return $rule->delay3 if $rule->debarred3;
     }
 }
 
diff --git a/Koha/Schema/Result/Overduerule.pm b/Koha/Schema/Result/Overduerule.pm
index e5c112c2f4..c52005ca50 100644
--- a/Koha/Schema/Result/Overduerule.pm
+++ b/Koha/Schema/Result/Overduerule.pm
@@ -198,6 +198,11 @@ __PACKAGE__->has_many(
 # Created by DBIx::Class::Schema::Loader v0.07049 @ 2021-01-21 13:39:29
 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:pORigxtC5qztZWHI29mZ/g
 
+sub koha_object_class {
+    'Koha::OverdueRule';
+}
+sub koha_objects_class {
+    'Koha::OverdueRules';
+}
 
-# You can replace this text with custom content, and it will be preserved on regeneration
 1;
-- 
2.30.2