Bug 22043: (QA follow-up) Add parameter to control behavior
[koha.git] / C4 / Contract.pm
index 77fefaa..0de49ad 100644 (file)
@@ -4,30 +4,28 @@ package C4::Contract;
 #
 # 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 Modern::Perl;
 use strict;
 #use warnings; FIXME - Bug 2505
 use C4::Context;
-use C4::SQLHelper qw(:all);
+use Koha::Database;
 
-use vars qw($VERSION @ISA @EXPORT);
+use vars qw(@ISA @EXPORT);
 
 BEGIN {
-       # set the version for version checking
-    $VERSION = 3.07.00.049;
     require Exporter;
        @ISA    = qw(Exporter);
        @EXPORT = qw(
@@ -78,32 +76,21 @@ Returns a list of contracts
 =cut
 
 sub GetContracts {
-    my ($params) = @_;
-    my $booksellerid = $params->{booksellerid};
-    my $activeonly = $params->{activeonly};
-
-    my $dbh = C4::Context->dbh;
-    my $query = "SELECT * FROM aqcontract";
-    my $result_set;
-    if($booksellerid) {
-        $query .= " WHERE booksellerid=?";
-
-        if($activeonly) {
-            $query .= " AND contractenddate >= CURDATE( )";
-        }
-
-        $result_set = $dbh->selectall_arrayref( $query, { Slice => {} }, $booksellerid );
-    }
-    else {
-        $result_set = $dbh->selectall_arrayref( $query, { Slice => {} } );
+    my ($filters) = @_;
+    if( $filters->{activeonly} ) {
+        $filters->{contractenddate} = {'>=' => \'now()'};
+        delete $filters->{activeonly};
     }
 
-    return $result_set;
+    my $rs = Koha::Database->new()->schema->resultset('Aqcontract');
+    $rs = $rs->search($filters);
+    $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
+    return [ $rs->all ];
 }
 
 =head2 GetContract
 
-$contract = GetContract( { contractnumber => $contractnumber } );
+$contract = GetContract( { contractnumber => $contractnumber } );
 
 Looks up the contract that has PRIMKEY (contractnumber) value $contractID
 
@@ -113,25 +100,43 @@ Returns a contract
 
 sub GetContract {
     my ($params) = @_;
-    my $contractno = $params->{contractnumber};
+    my $contractnumber = $params->{contractnumber};
 
-    my $dbh = C4::Context->dbh;
-    my $query = "SELECT * FROM aqcontract WHERE contractnumber=?";
+    return unless $contractnumber;
 
-    my $sth = $dbh->prepare($query);
-    $sth->execute($contractno);
-    my $result = $sth->fetchrow_hashref;
-    return $result;
+    my $contracts = GetContracts({
+        contractnumber => $contractnumber,
+    });
+    return $contracts->[0];
 }
 
+sub AddContract {
+    my ($contract) = @_;
+    return unless($contract->{booksellerid});
 
-#sub GetContract { SearchInTable("aqcontract", shift); }
+    my $rs = Koha::Database->new()->schema->resultset('Aqcontract');
+    return $rs->create($contract)->id;
+}
+
+sub ModContract {
+    my ($contract) = @_;
+    my $result = Koha::Database->new()->schema->resultset('Aqcontract')->find($contract);
+    return unless($result);
 
-sub AddContract { InsertInTable("aqcontract", shift); }
+    $result = $result->update($contract);
+    return $result->in_storage;
+}
 
-sub ModContract { UpdateInTable("aqcontract", shift); }
+sub DelContract {
+    my ($contract) = @_;
+    return unless($contract->{contractnumber});
 
-sub DelContract { DeleteInTable("aqcontract", shift); }
+    my $result = Koha::Database->new()->schema->resultset('Aqcontract')->find($contract);
+    return unless($result);
+
+    eval { $result->delete };
+    return !( $result->in_storage );
+}
 
 1;