From 67bcdeed2826a77190ef3952b3a63b74be0f508e Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Fri, 27 Jan 2023 14:36:40 +0000 Subject: [PATCH] Bug 29145: (QA follow-up) Use objects instead of straight SQL queries Signed-off-by: Tomas Cohen Arazi --- 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 . + +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 . + +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 df9ad20bd9..48cf9aae1e 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; @@ -1073,23 +1074,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.39.5