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

(-)a/C4/Contract.pm (-148 lines)
Lines 1-148 Link Here
1
package C4::Contract;
2
3
# Copyright 2009-2010 BibLibre SARL
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
use C4::Context;
22
use Koha::Database;
23
24
use vars qw(@ISA @EXPORT);
25
26
BEGIN {
27
    require Exporter;
28
    @ISA    = qw(Exporter);
29
    @EXPORT = qw(
30
        GetContracts
31
        GetContract
32
        AddContract
33
        ModContract
34
        DelContract
35
    );
36
}
37
38
=head1 NAME
39
40
C4::Contract - Koha functions for dealing with bookseller contracts.
41
42
=head1 SYNOPSIS
43
44
use C4::Contract;
45
46
=head1 DESCRIPTION
47
48
The functions in this module deal with contracts. They allow to
49
add a new contract, to modify it or to get some informations around
50
a contract.
51
52
=cut
53
54
=head2 GetContracts
55
56
$contractlist = GetContracts({
57
    booksellerid => $booksellerid,
58
    activeonly => $activeonly
59
});
60
61
Looks up the contracts that belong to a bookseller
62
63
Returns a list of contracts
64
65
=over
66
67
=item C<$booksellerid> is the "id" field in the "aqbooksellers" table.
68
69
=item C<$activeonly> if exists get only contracts that are still active.
70
71
=back
72
73
=cut
74
75
sub GetContracts {
76
    my ($filters) = @_;
77
    if ( $filters->{activeonly} ) {
78
        $filters->{contractenddate} = { '>=' => \'now()' };
79
        delete $filters->{activeonly};
80
    }
81
82
    my $rs = Koha::Database->new()->schema->resultset('Aqcontract');
83
    $rs = $rs->search($filters);
84
    $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
85
    return [ $rs->all ];
86
}
87
88
=head2 GetContract
89
90
$contract = GetContract( { contractnumber => $contractnumber } );
91
92
Looks up the contract that has PRIMKEY (contractnumber) value $contractID
93
94
Returns a contract
95
96
=cut
97
98
sub GetContract {
99
    my ($params) = @_;
100
    my $contractnumber = $params->{contractnumber};
101
102
    return unless $contractnumber;
103
104
    my $contracts = GetContracts(
105
        {
106
            contractnumber => $contractnumber,
107
        }
108
    );
109
    return $contracts->[0];
110
}
111
112
sub AddContract {
113
    my ($contract) = @_;
114
    return unless ( $contract->{booksellerid} );
115
116
    my $rs = Koha::Database->new()->schema->resultset('Aqcontract');
117
    return $rs->create($contract)->id;
118
}
119
120
sub ModContract {
121
    my ($contract) = @_;
122
    my $result = Koha::Database->new()->schema->resultset('Aqcontract')->find($contract);
123
    return unless ($result);
124
125
    $result = $result->update($contract);
126
    return $result->in_storage;
127
}
128
129
sub DelContract {
130
    my ($contract) = @_;
131
    return unless ( $contract->{contractnumber} );
132
133
    my $result = Koha::Database->new()->schema->resultset('Aqcontract')->find($contract);
134
    return unless ($result);
135
136
    eval { $result->delete };
137
    return !( $result->in_storage );
138
}
139
140
1;
141
142
__END__
143
144
=head1 AUTHOR
145
146
Koha Development Team <http://koha-community.org/>
147
148
=cut
(-)a/t/Contract.t (-14 lines)
Lines 1-14 Link Here
1
#!/usr/bin/perl
2
#
3
# This Koha test module is a stub!
4
# Add more tests here!!!
5
6
use strict;
7
use warnings;
8
9
use Test::More tests => 1;
10
11
BEGIN {
12
    use_ok('C4::Contract');
13
}
14
(-)a/t/db_dependent/Contract.t (-194 lines)
Lines 1-193 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Copyright 2014  Biblibre SARL
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use C4::Context;
23
use Koha::Database;
24
use Koha::DateUtils qw( dt_from_string );
25
use Koha::Acquisition::Booksellers;
26
27
use DateTime::Duration;
28
29
use Test::More tests => 43;
30
31
BEGIN {
32
    use_ok( 'C4::Contract', qw( GetContracts GetContract AddContract ModContract DelContract ) );
33
}
34
35
my $schema = Koha::Database->new->schema;
36
$schema->storage->txn_begin;
37
my $dbh = C4::Context->dbh;
38
39
$dbh->do(q|DELETE FROM aqbasket|);
40
$dbh->do(q|DELETE FROM aqcontract|);
41
$dbh->do(q|DELETE FROM aqbooksellers|);
42
43
my $bookseller1    = Koha::Acquisition::Bookseller->new( { name => 'My first bookseller' } )->store;
44
my $bookseller_id1 = $bookseller1->id;
45
isnt( $bookseller_id1, undef, 'AddBookseller does not return undef' );
46
my $bookseller2    = Koha::Acquisition::Bookseller->new( { name => 'My second bookseller' } )->store;
47
my $bookseller_id2 = $bookseller2->id;
48
isnt( $bookseller_id2, undef, 'AddBookseller does not return undef' );
49
my $contracts = GetContracts();
50
is( @$contracts, 0, 'GetContracts returns the correct number of contracts' );
51
my $contract = GetContract();
52
is( $contract, undef, 'GetContract without argument returns undef' );
53
54
my $my_contract1 = {
55
    contractstartdate   => '2014-06-01',
56
    contractenddate     => '2014-06-30',
57
    contractname        => 'My contract name',
58
    contractdescription => 'My contract description',
59
    booksellerid        => $bookseller_id1,
60
};
61
my $my_contract_id1 = AddContract();
62
is( $my_contract_id1, undef, 'AddContract without argument returns undef' );
63
$my_contract_id1 = AddContract($my_contract1);
64
isnt( $my_contract_id1, undef, 'AddContract does not return undef' );
65
66
$contracts = GetContracts();
67
is( @$contracts, 1, 'AddContract adds a contract' );
68
69
$contract = GetContract();
70
is( $contract, undef, 'GetContract without argument returns undef' );
71
$contract = GetContract( { contractnumber => $my_contract_id1 } );
72
is(
73
    $contract->{contractstartdate}, $my_contract1->{contractstartdate},
74
    'AddContract stores the contract start date correctly.'
75
);
76
is(
77
    $contract->{contractenddate}, $my_contract1->{contractenddate},
78
    'AddContract stores the contract end date correctly.'
79
);
80
is( $contract->{contractname}, $my_contract1->{contractname}, 'AddContract stores the contract name correctly.' );
81
is(
82
    $contract->{contractdescription}, $my_contract1->{contractdescription},
83
    'AddContract stores the contract description correctly.'
84
);
85
is( $contract->{booksellerid}, $my_contract1->{booksellerid}, 'AddContract stores the bookseller id correctly.' );
86
87
my $now             = dt_from_string;
88
my $three_more_days = $now + DateTime::Duration->new( days => 3 );
89
90
$my_contract1 = {
91
    contractstartdate   => $now->ymd,
92
    contractenddate     => $three_more_days->ymd,
93
    contractname        => 'My modified contract name',
94
    contractdescription => 'My modified contract description',
95
    booksellerid        => $bookseller_id2,
96
};
97
my $mod_status = ModContract($my_contract1);
98
is( $mod_status, undef, 'ModContract without the contract number returns 0E0' );
99
100
$my_contract1->{contractnumber} = $my_contract_id1;
101
$mod_status = ModContract($my_contract1);
102
is( $mod_status, 1, 'ModContract returns true' );
103
$contracts = GetContracts();
104
is( @$contracts, 1, 'ModContract does not modify the number of contracts' );
105
$contract = GetContract( { contractnumber => $my_contract_id1 } );
106
is(
107
    $contract->{contractstartdate}, $my_contract1->{contractstartdate},
108
    'ModContract updates the contract start date correctly.'
109
);
110
is(
111
    $contract->{contractenddate}, $my_contract1->{contractenddate},
112
    'ModContract updates the contract end date correctly.'
113
);
114
is( $contract->{contractname}, $my_contract1->{contractname}, 'ModContract updates the contract name correctly.' );
115
is(
116
    $contract->{contractdescription}, $my_contract1->{contractdescription},
117
    'ModContract updates the contract description correctly.'
118
);
119
is( $contract->{booksellerid}, $my_contract1->{booksellerid}, 'ModContract updates the bookseller id correctly.' );
120
121
my $my_contract2 = {
122
    contractstartdate   => '2013-08-05',
123
    contractenddate     => '2013-09-25',
124
    contractname        => 'My other contract name',
125
    contractdescription => 'My other description contract name',
126
    booksellerid        => $bookseller_id1,
127
};
128
my $my_contract_id2 = AddContract($my_contract2);
129
$contracts = GetContracts( { booksellerid => $bookseller_id1 } );
130
is( @$contracts, 1, 'GetContracts returns the correct number of contracts' );
131
$contracts = GetContracts( { activeonly => 1 } );
132
is( @$contracts, 1, 'GetContracts with active only returns only current contracts' );
133
$contracts = GetContracts( { booksellerid => $bookseller_id2 } );
134
is( @$contracts, 1, 'GetContracts returns the correct number of contracts' );
135
$contracts = GetContracts();
136
is( @$contracts, 2, 'GetContracts returns the correct number of contracts' );
137
138
is( $contracts->[0]->{contractnumber}, $my_contract_id1, 'GetContracts returns the contract number correctly' );
139
is(
140
    $contracts->[0]->{contractstartdate}, $my_contract1->{contractstartdate},
141
    'GetContracts returns the contract start date correctly.'
142
);
143
is(
144
    $contracts->[0]->{contractenddate}, $my_contract1->{contractenddate},
145
    'GetContracts returns the contract end date correctly.'
146
);
147
is(
148
    $contracts->[0]->{contractname}, $my_contract1->{contractname},
149
    'GetContracts returns the contract name correctly.'
150
);
151
is(
152
    $contracts->[0]->{contractdescription}, $my_contract1->{contractdescription},
153
    'GetContracts returns the contract description correctly.'
154
);
155
is(
156
    $contracts->[0]->{booksellerid}, $my_contract1->{booksellerid},
157
    'GetContracts returns the bookseller id correctly.'
158
);
159
160
is( $contracts->[1]->{contractnumber}, $my_contract_id2, 'GetContracts returns the contract number correctly' );
161
is(
162
    $contracts->[1]->{contractstartdate}, $my_contract2->{contractstartdate},
163
    'GetContracts returns the contract start date correctly.'
164
);
165
is(
166
    $contracts->[1]->{contractenddate}, $my_contract2->{contractenddate},
167
    'GetContracts returns the contract end date correctly.'
168
);
169
is(
170
    $contracts->[1]->{contractname}, $my_contract2->{contractname},
171
    'GetContracts returns the contract name correctly.'
172
);
173
is(
174
    $contracts->[1]->{contractdescription}, $my_contract2->{contractdescription},
175
    'GetContracts returns the contract description correctly.'
176
);
177
is(
178
    $contracts->[1]->{booksellerid}, $my_contract2->{booksellerid},
179
    'GetContracts returns the bookseller id correctly.'
180
);
181
182
my $del_status = DelContract();
183
is( $del_status, undef, 'DelContract without contract number returns undef' );
184
185
$del_status = DelContract( { contractnumber => $my_contract_id1 } );
186
is( $del_status, 1, 'DelContract returns true' );
187
$contracts = GetContracts();
188
is( @$contracts, 1, 'DelContract deletes a contract' );
189
190
$del_status = DelContract( { contractnumber => $my_contract_id2 } );
191
is( $del_status, 1, 'DelContract returns true' );
192
$contracts = GetContracts();
193
is( @$contracts, 0, 'DelContract deletes a contract' );
194
- 

Return to bug 39711