From 44d831dabe4a998317f09b95ffb60611601c4cc9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Joonas=20Kylm=C3=A4l=C3=A4?= <j.kylmala@gmail.com>
Date: Wed, 4 Nov 2015 15:15:04 +0000
Subject: [PATCH] Bug 15129: Add Koha::Object for issuing rules and let
 smart-rules.pl use it

Test plan:
1. Make sure that in koha/admin/smart-rules.pl you can still create
   new rules and that the new rules have all their values remained
   after saving the rule (so put to every field something).

Sponsored-by: Vaara-kirjastot
---
 Koha/IssuingRule.pm  | 42 ++++++++++++++++++++++++++++++++++++++++++
 Koha/IssuingRules.pm | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 admin/smart-rules.pl |  6 ++----
 3 files changed, 94 insertions(+), 4 deletions(-)
 create mode 100644 Koha/IssuingRule.pm
 create mode 100644 Koha/IssuingRules.pm

diff --git a/Koha/IssuingRule.pm b/Koha/IssuingRule.pm
new file mode 100644
index 0000000..4b3d012
--- /dev/null
+++ b/Koha/IssuingRule.pm
@@ -0,0 +1,42 @@
+package Koha::IssuingRule;
+
+# Copyright Vaara-kirjastot 2015
+#
+# 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, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
+use Koha::Database;
+use base qw(Koha::Object);
+
+=head1 NAME
+
+Koha::Hold - Koha Hold object class
+
+=head1 API
+
+=head2 Class Methods
+
+=cut
+
+=head3 type
+
+=cut
+
+sub type {
+    return 'Issuingrule';
+}
+
+1;
\ No newline at end of file
diff --git a/Koha/IssuingRules.pm b/Koha/IssuingRules.pm
new file mode 100644
index 0000000..5c1b0bb
--- /dev/null
+++ b/Koha/IssuingRules.pm
@@ -0,0 +1,50 @@
+package Koha::IssuingRules;
+
+# Copyright Vaara-kirjastot 2015
+#
+# 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, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
+use Koha::Database;
+use base qw(Koha::Objects);
+
+=head1 NAME
+
+Koha::IssuingRules - Koha IssuingRules object set class
+
+=head1 API
+
+=head2 Class Methods
+
+=cut
+
+=head3 type
+
+=cut
+
+sub type {
+    return 'Issuingrule';
+}
+
+=head3 object_class
+
+=cut
+
+sub object_class {
+    return 'Koha::IssuingRule';
+}
+
+1;
\ No newline at end of file
diff --git a/admin/smart-rules.pl b/admin/smart-rules.pl
index d43c686..5d000ad 100755
--- a/admin/smart-rules.pl
+++ b/admin/smart-rules.pl
@@ -28,6 +28,7 @@ use C4::Debug;
 use C4::Branch; # GetBranches
 use Koha::DateUtils;
 use Koha::Database;
+use Koha::IssuingRule;
 
 my $input = CGI->new;
 my $dbh = C4::Context->dbh;
@@ -136,9 +137,6 @@ elsif ($op eq 'add') {
     my $overduefinescap = $input->param('overduefinescap') || undef;
     $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty";
 
-    my $schema = Koha::Database->new()->schema();
-    my $rs = $schema->resultset('Issuingrule');
-
     my $params = {
         branchcode             => $br,
         categorycode           => $bor,
@@ -166,7 +164,7 @@ elsif ($op eq 'add') {
         overduefinescap        => $overduefinescap,
     };
 
-    $rs->update_or_create($params);
+    Koha::IssuingRule->new()->set($params)->store();
 
 }
 elsif ($op eq "set-branch-defaults") {
-- 
1.9.1