View | Details | Raw Unified | Return to bug 12487
Collapse All | Expand All

(-)a/C4/Contract.pm (-31 / +36 lines)
Lines 21-27 use Modern::Perl; Link Here
21
use strict;
21
use strict;
22
#use warnings; FIXME - Bug 2505
22
#use warnings; FIXME - Bug 2505
23
use C4::Context;
23
use C4::Context;
24
use C4::SQLHelper qw(:all);
24
use Koha::Database;
25
25
26
use vars qw($VERSION @ISA @EXPORT);
26
use vars qw($VERSION @ISA @EXPORT);
27
27
Lines 78-104 Returns a list of contracts Link Here
78
=cut
78
=cut
79
79
80
sub GetContracts {
80
sub GetContracts {
81
    my ($params) = @_;
81
    my ($filters) = @_;
82
    my $booksellerid = $params->{booksellerid};
82
    if( $filters->{activeonly} ) {
83
    my $activeonly = $params->{activeonly};
83
        $filters->{contractenddate} = {'>=' => \'now()'};
84
84
        delete $filters->{activeonly};
85
    my $dbh = C4::Context->dbh;
86
    my $query = "SELECT * FROM aqcontract";
87
    my $result_set;
88
    if($booksellerid) {
89
        $query .= " WHERE booksellerid=?";
90
91
        if($activeonly) {
92
            $query .= " AND contractenddate >= CURDATE( )";
93
        }
94
95
        $result_set = $dbh->selectall_arrayref( $query, { Slice => {} }, $booksellerid );
96
    }
97
    else {
98
        $result_set = $dbh->selectall_arrayref( $query, { Slice => {} } );
99
    }
85
    }
100
86
101
    return $result_set;
87
    my $rs = Koha::Database->new()->schema->resultset('Aqcontract');
88
    $rs = $rs->search($filters);
89
    $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
90
    return [ $rs->all ];
102
}
91
}
103
92
104
=head2 GetContract
93
=head2 GetContract
Lines 113-137 Returns a contract Link Here
113
102
114
sub GetContract {
103
sub GetContract {
115
    my ($params) = @_;
104
    my ($params) = @_;
116
    my $contractno = $params->{contractnumber};
105
    my $contractnumber = $params->{contractnumber};
117
106
118
    my $dbh = C4::Context->dbh;
107
    my $contracts = GetContracts({
119
    my $query = "SELECT * FROM aqcontract WHERE contractnumber=?";
108
        contractnumber => $contractnumber,
109
    });
110
    return $contracts->[0];
111
}
112
113
sub AddContract {
114
    my ($contract) = @_;
115
    return unless($contract->{booksellerid});
120
116
121
    my $sth = $dbh->prepare($query);
117
    my $rs = Koha::Database->new()->schema->resultset('Aqcontract');
122
    $sth->execute($contractno);
118
    return $rs->create($contract)->id;
123
    my $result = $sth->fetchrow_hashref;
124
    return $result;
125
}
119
}
126
120
121
sub ModContract {
122
    my ($contract) = @_;
123
    my $result = Koha::Database->new()->schema->resultset('Aqcontract')->find($contract);
124
    return unless($result);
127
125
128
#sub GetContract { SearchInTable("aqcontract", shift); }
126
    $result = $result->update($contract);
127
    return $result->in_storage;
128
}
129
129
130
sub AddContract { InsertInTable("aqcontract", shift); }
130
sub DelContract {
131
    my ($contract) = @_;
132
    return unless($contract->{contractnumber});
131
133
132
sub ModContract { UpdateInTable("aqcontract", shift); }
134
    my $result = Koha::Database->new()->schema->resultset('Aqcontract')->find($contract);
135
    return unless($result);
133
136
134
sub DelContract { DeleteInTable("aqcontract", shift); }
137
    eval { $result->delete };
138
    return !( $result->in_storage );
139
}
135
140
136
1;
141
1;
137
142
(-)a/t/db_dependent/Contract.t (-7 / +14 lines)
Lines 21-27 use Modern::Perl; Link Here
21
use C4::Context;
21
use C4::Context;
22
use C4::Bookseller;
22
use C4::Bookseller;
23
23
24
use Test::More tests => 40;
24
use Test::More tests => 43;
25
25
26
BEGIN {
26
BEGIN {
27
    use_ok('C4::Contract');
27
    use_ok('C4::Contract');
Lines 42-47 my $bookseller_id2 = C4::Bookseller::AddBookseller( { name => 'My second booksel Link Here
42
isnt( $bookseller_id2, undef, 'AddBookseller does not return undef' );
42
isnt( $bookseller_id2, undef, 'AddBookseller does not return undef' );
43
my $contracts = GetContracts();
43
my $contracts = GetContracts();
44
is( @$contracts, 0, 'GetContracts returns the correct number of contracts' );
44
is( @$contracts, 0, 'GetContracts returns the correct number of contracts' );
45
my $contract = GetContract();
46
is( $contract, undef, 'GetContract without argument returns undef' );
47
45
48
46
my $my_contract1 = {
49
my $my_contract1 = {
47
    contractstartdate   => '2014-06-01',
50
    contractstartdate   => '2014-06-01',
Lines 50-60 my $my_contract1 = { Link Here
50
    contractdescription => 'My contract description',
53
    contractdescription => 'My contract description',
51
    booksellerid        => $bookseller_id1,
54
    booksellerid        => $bookseller_id1,
52
};
55
};
53
my $my_contract_id1 = AddContract($my_contract1);
56
my $my_contract_id1 = AddContract();
57
is( $my_contract_id1, undef, 'AddContract without argument returns undef' );
58
$my_contract_id1 = AddContract($my_contract1);
54
isnt( $my_contract_id1, undef, 'AddContract does not return undef' );
59
isnt( $my_contract_id1, undef, 'AddContract does not return undef' );
60
55
$contracts = GetContracts();
61
$contracts = GetContracts();
56
is( @$contracts, 1, 'AddContract adds a contract' );
62
is( @$contracts, 1, 'AddContract adds a contract' );
57
my $contract = GetContract( { contractnumber => $my_contract_id1 } );
63
64
$contract = GetContract();
65
is( $contract, undef, 'GetContract without argument returns undef' );
66
$contract = GetContract( { contractnumber => $my_contract_id1 } );
58
is( $contract->{contractstartdate}, $my_contract1->{contractstartdate}, 'AddContract stores the contract start date correctly.' );
67
is( $contract->{contractstartdate}, $my_contract1->{contractstartdate}, 'AddContract stores the contract start date correctly.' );
59
is( $contract->{contractenddate}, $my_contract1->{contractenddate}, 'AddContract stores the contract end date correctly.' );
68
is( $contract->{contractenddate}, $my_contract1->{contractenddate}, 'AddContract stores the contract end date correctly.' );
60
is( $contract->{contractname}, $my_contract1->{contractname}, 'AddContract stores the contract name correctly.' );
69
is( $contract->{contractname}, $my_contract1->{contractname}, 'AddContract stores the contract name correctly.' );
Lines 70-76 $my_contract1 = { Link Here
70
    booksellerid        => $bookseller_id2,
79
    booksellerid        => $bookseller_id2,
71
};
80
};
72
my $mod_status = ModContract($my_contract1);
81
my $mod_status = ModContract($my_contract1);
73
is( $mod_status, '0E0', 'ModContract without the contract number returns 0E0' );
82
is( $mod_status, undef, 'ModContract without the contract number returns 0E0' );
74
83
75
$my_contract1->{contractnumber} = $my_contract_id1;
84
$my_contract1->{contractnumber} = $my_contract_id1;
76
$mod_status = ModContract($my_contract1);
85
$mod_status = ModContract($my_contract1);
Lines 96-105 my $my_contract_id2 = AddContract($my_contract2); Link Here
96
$contracts = GetContracts( { booksellerid => $bookseller_id1 } );
105
$contracts = GetContracts( { booksellerid => $bookseller_id1 } );
97
is( @$contracts, 1, 'GetContracts returns the correct number of contracts' );
106
is( @$contracts, 1, 'GetContracts returns the correct number of contracts' );
98
$contracts = GetContracts({
107
$contracts = GetContracts({
99
    booksellerid => $bookseller_id1,
100
    activeonly => 1
108
    activeonly => 1
101
});
109
});
102
is( @$contracts, 0, 'GetContracts with active only returns only current contracts' );
110
is( @$contracts, 1, 'GetContracts with active only returns only current contracts' );
103
$contracts = GetContracts( { booksellerid => $bookseller_id2 } );
111
$contracts = GetContracts( { booksellerid => $bookseller_id2 } );
104
is( @$contracts, 1, 'GetContracts returns the correct number of contracts' );
112
is( @$contracts, 1, 'GetContracts returns the correct number of contracts' );
105
$contracts = GetContracts();
113
$contracts = GetContracts();
106
- 

Return to bug 12487