From 4a13b1865f448db041279dd4e1d1091e0aa8b3ce Mon Sep 17 00:00:00 2001 From: Yohann Dufour Date: Mon, 30 Jun 2014 14:42:27 +0200 Subject: [PATCH] Bug 12487: SQLHelper replacement - C4::Contract With this patch, the subroutines GetContract, GetContracts, AddContract, ModContract and DelContract uses DBIx::Class instead of C4::SQLHelper Test plan: 1) Apply the patch 2) Execute the unit tests by launching: prove t/db_dependent/Contract.t 3) The command has to be a success : t/db_dependent/Contract.t .. ok All tests successful. Files=1, Tests=43, 2 wallclock secs ( 0.04 usr 0.01 sys + 1.49 cusr 0.05 csys = 1.59 CPU) Result: PASS 4) Log on with a superlibrarian permission 5) Go on the page acqui/supplier.pl (Acquisitions > Button "New vendor") 6) Record a vendor with a nonzero "name" 7) Go on the page admin/aqcontract.pl (click on the "Contracts" item in the menu) 8) Click on the button "New" > "Contract" and record a new one 9) Verify the displayed data are correct about the contract 10) "Edit" the contract with different values and verify the data are updated 11) Click on "Delete" in order to delete the contract, verify the displayed data are correct but cancel the operation 12) Click on "New" > "Basket" and verify there is the created contract in field "Contract", then record a basket by selectioning the created contract 13) Verify the contract name displayed is correct 14) Record an active budget and a fund linked to this budget 15) Go on the new basket (Home > Acquisitions > Search the created vendor) 16) Click on "Add to basket" then "From a new (empty) record" and verify the displayed contract name is correct, then cancel 17) Click on "Delete this basket" 18) Click on "Contracts" (in the left menu) in order to go on aqcontract.pl, then "Delete" the created contract 19) The contract is not displayed anymore in the vendor page --- C4/Contract.pm | 67 +++++++++++++++++++++++++---------------------- t/db_dependent/Contract.t | 20 +++++++++----- 2 files changed, 50 insertions(+), 37 deletions(-) diff --git a/C4/Contract.pm b/C4/Contract.pm index 77fefaa..57978a0 100644 --- a/C4/Contract.pm +++ b/C4/Contract.pm @@ -21,7 +21,7 @@ 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); @@ -78,27 +78,16 @@ 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 @@ -113,25 +102,41 @@ 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=?"; + my $contracts = GetContracts({ + contractnumber => $contractnumber, + }); + return $contracts->[0]; +} + +sub AddContract { + my ($contract) = @_; + return unless($contract->{booksellerid}); - my $sth = $dbh->prepare($query); - $sth->execute($contractno); - my $result = $sth->fetchrow_hashref; - return $result; + 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 GetContract { SearchInTable("aqcontract", shift); } + $result = $result->update($contract); + return $result->in_storage; +} -sub AddContract { InsertInTable("aqcontract", shift); } +sub DelContract { + my ($contract) = @_; + return unless($contract->{contractnumber}); -sub ModContract { UpdateInTable("aqcontract", shift); } + my $result = Koha::Database->new()->schema->resultset('Aqcontract')->find($contract); + return unless($result); -sub DelContract { DeleteInTable("aqcontract", shift); } + eval { $result->delete }; + return !( $result->in_storage ); +} 1; diff --git a/t/db_dependent/Contract.t b/t/db_dependent/Contract.t index 0f348a7..7439873 100644 --- a/t/db_dependent/Contract.t +++ b/t/db_dependent/Contract.t @@ -21,7 +21,7 @@ use Modern::Perl; use C4::Context; use C4::Bookseller; -use Test::More tests => 40; +use Test::More tests => 43; BEGIN { use_ok('C4::Contract'); @@ -42,6 +42,9 @@ my $bookseller_id2 = C4::Bookseller::AddBookseller( { name => 'My second booksel isnt( $bookseller_id2, undef, 'AddBookseller does not return undef' ); my $contracts = GetContracts(); is( @$contracts, 0, 'GetContracts returns the correct number of contracts' ); +my $contract = GetContract(); +is( $contract, undef, 'GetContract without argument returns undef' ); + my $my_contract1 = { contractstartdate => '2014-06-01', @@ -50,11 +53,17 @@ my $my_contract1 = { contractdescription => 'My contract description', booksellerid => $bookseller_id1, }; -my $my_contract_id1 = AddContract($my_contract1); +my $my_contract_id1 = AddContract(); +is( $my_contract_id1, undef, 'AddContract without argument returns undef' ); +$my_contract_id1 = AddContract($my_contract1); isnt( $my_contract_id1, undef, 'AddContract does not return undef' ); + $contracts = GetContracts(); is( @$contracts, 1, 'AddContract adds a contract' ); -my $contract = GetContract( { contractnumber => $my_contract_id1 } ); + +$contract = GetContract(); +is( $contract, undef, 'GetContract without argument returns undef' ); +$contract = GetContract( { contractnumber => $my_contract_id1 } ); is( $contract->{contractstartdate}, $my_contract1->{contractstartdate}, 'AddContract stores the contract start date correctly.' ); is( $contract->{contractenddate}, $my_contract1->{contractenddate}, 'AddContract stores the contract end date correctly.' ); is( $contract->{contractname}, $my_contract1->{contractname}, 'AddContract stores the contract name correctly.' ); @@ -70,7 +79,7 @@ $my_contract1 = { booksellerid => $bookseller_id2, }; my $mod_status = ModContract($my_contract1); -is( $mod_status, '0E0', 'ModContract without the contract number returns 0E0' ); +is( $mod_status, undef, 'ModContract without the contract number returns 0E0' ); $my_contract1->{contractnumber} = $my_contract_id1; $mod_status = ModContract($my_contract1); @@ -96,10 +105,9 @@ my $my_contract_id2 = AddContract($my_contract2); $contracts = GetContracts( { booksellerid => $bookseller_id1 } ); is( @$contracts, 1, 'GetContracts returns the correct number of contracts' ); $contracts = GetContracts({ - booksellerid => $bookseller_id1, activeonly => 1 }); -is( @$contracts, 0, 'GetContracts with active only returns only current contracts' ); +is( @$contracts, 1, 'GetContracts with active only returns only current contracts' ); $contracts = GetContracts( { booksellerid => $bookseller_id2 } ); is( @$contracts, 1, 'GetContracts returns the correct number of contracts' ); $contracts = GetContracts(); -- 1.9.1