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

(-)a/C4/Contract.pm (-166 lines)
Lines 1-166 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
=head2 AddContract
113
114
Missing POD for AddContract.
115
116
=cut
117
118
sub AddContract {
119
    my ($contract) = @_;
120
    return unless ( $contract->{booksellerid} );
121
122
    my $rs = Koha::Database->new()->schema->resultset('Aqcontract');
123
    return $rs->create($contract)->id;
124
}
125
126
=head2 ModContract
127
128
Missing POD for ModContract.
129
130
=cut
131
132
sub ModContract {
133
    my ($contract) = @_;
134
    my $result = Koha::Database->new()->schema->resultset('Aqcontract')->find($contract);
135
    return unless ($result);
136
137
    $result = $result->update($contract);
138
    return $result->in_storage;
139
}
140
141
=head2 DelContract
142
143
Missing POD for DelContract.
144
145
=cut
146
147
sub DelContract {
148
    my ($contract) = @_;
149
    return unless ( $contract->{contractnumber} );
150
151
    my $result = Koha::Database->new()->schema->resultset('Aqcontract')->find($contract);
152
    return unless ($result);
153
154
    eval { $result->delete };
155
    return !( $result->in_storage );
156
}
157
158
1;
159
160
__END__
161
162
=head1 AUTHOR
163
164
Koha Development Team <http://koha-community.org/>
165
166
=cut
(-)a/t/Contract.t (-15 lines)
Lines 1-15 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::NoWarnings;
10
use Test::More tests => 2;
11
12
BEGIN {
13
    use_ok('C4::Contract');
14
}
15
(-)a/t/db_dependent/Contract.t (-195 lines)
Lines 1-194 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::NoWarnings;
30
use Test::More tests => 44;
31
32
BEGIN {
33
    use_ok( 'C4::Contract', qw( GetContracts GetContract AddContract ModContract DelContract ) );
34
}
35
36
my $schema = Koha::Database->new->schema;
37
$schema->storage->txn_begin;
38
my $dbh = C4::Context->dbh;
39
40
$dbh->do(q|DELETE FROM aqbasket|);
41
$dbh->do(q|DELETE FROM aqcontract|);
42
$dbh->do(q|DELETE FROM aqbooksellers|);
43
44
my $bookseller1    = Koha::Acquisition::Bookseller->new( { name => 'My first bookseller' } )->store;
45
my $bookseller_id1 = $bookseller1->id;
46
isnt( $bookseller_id1, undef, 'AddBookseller does not return undef' );
47
my $bookseller2    = Koha::Acquisition::Bookseller->new( { name => 'My second bookseller' } )->store;
48
my $bookseller_id2 = $bookseller2->id;
49
isnt( $bookseller_id2, undef, 'AddBookseller does not return undef' );
50
my $contracts = GetContracts();
51
is( @$contracts, 0, 'GetContracts returns the correct number of contracts' );
52
my $contract = GetContract();
53
is( $contract, undef, 'GetContract without argument returns undef' );
54
55
my $my_contract1 = {
56
    contractstartdate   => '2014-06-01',
57
    contractenddate     => '2014-06-30',
58
    contractname        => 'My contract name',
59
    contractdescription => 'My contract description',
60
    booksellerid        => $bookseller_id1,
61
};
62
my $my_contract_id1 = AddContract();
63
is( $my_contract_id1, undef, 'AddContract without argument returns undef' );
64
$my_contract_id1 = AddContract($my_contract1);
65
isnt( $my_contract_id1, undef, 'AddContract does not return undef' );
66
67
$contracts = GetContracts();
68
is( @$contracts, 1, 'AddContract adds a contract' );
69
70
$contract = GetContract();
71
is( $contract, undef, 'GetContract without argument returns undef' );
72
$contract = GetContract( { contractnumber => $my_contract_id1 } );
73
is(
74
    $contract->{contractstartdate}, $my_contract1->{contractstartdate},
75
    'AddContract stores the contract start date correctly.'
76
);
77
is(
78
    $contract->{contractenddate}, $my_contract1->{contractenddate},
79
    'AddContract stores the contract end date correctly.'
80
);
81
is( $contract->{contractname}, $my_contract1->{contractname}, 'AddContract stores the contract name correctly.' );
82
is(
83
    $contract->{contractdescription}, $my_contract1->{contractdescription},
84
    'AddContract stores the contract description correctly.'
85
);
86
is( $contract->{booksellerid}, $my_contract1->{booksellerid}, 'AddContract stores the bookseller id correctly.' );
87
88
my $now             = dt_from_string;
89
my $three_more_days = $now + DateTime::Duration->new( days => 3 );
90
91
$my_contract1 = {
92
    contractstartdate   => $now->ymd,
93
    contractenddate     => $three_more_days->ymd,
94
    contractname        => 'My modified contract name',
95
    contractdescription => 'My modified contract description',
96
    booksellerid        => $bookseller_id2,
97
};
98
my $mod_status = ModContract($my_contract1);
99
is( $mod_status, undef, 'ModContract without the contract number returns 0E0' );
100
101
$my_contract1->{contractnumber} = $my_contract_id1;
102
$mod_status = ModContract($my_contract1);
103
is( $mod_status, 1, 'ModContract returns true' );
104
$contracts = GetContracts();
105
is( @$contracts, 1, 'ModContract does not modify the number of contracts' );
106
$contract = GetContract( { contractnumber => $my_contract_id1 } );
107
is(
108
    $contract->{contractstartdate}, $my_contract1->{contractstartdate},
109
    'ModContract updates the contract start date correctly.'
110
);
111
is(
112
    $contract->{contractenddate}, $my_contract1->{contractenddate},
113
    'ModContract updates the contract end date correctly.'
114
);
115
is( $contract->{contractname}, $my_contract1->{contractname}, 'ModContract updates the contract name correctly.' );
116
is(
117
    $contract->{contractdescription}, $my_contract1->{contractdescription},
118
    'ModContract updates the contract description correctly.'
119
);
120
is( $contract->{booksellerid}, $my_contract1->{booksellerid}, 'ModContract updates the bookseller id correctly.' );
121
122
my $my_contract2 = {
123
    contractstartdate   => '2013-08-05',
124
    contractenddate     => '2013-09-25',
125
    contractname        => 'My other contract name',
126
    contractdescription => 'My other description contract name',
127
    booksellerid        => $bookseller_id1,
128
};
129
my $my_contract_id2 = AddContract($my_contract2);
130
$contracts = GetContracts( { booksellerid => $bookseller_id1 } );
131
is( @$contracts, 1, 'GetContracts returns the correct number of contracts' );
132
$contracts = GetContracts( { activeonly => 1 } );
133
is( @$contracts, 1, 'GetContracts with active only returns only current contracts' );
134
$contracts = GetContracts( { booksellerid => $bookseller_id2 } );
135
is( @$contracts, 1, 'GetContracts returns the correct number of contracts' );
136
$contracts = GetContracts();
137
is( @$contracts, 2, 'GetContracts returns the correct number of contracts' );
138
139
is( $contracts->[0]->{contractnumber}, $my_contract_id1, 'GetContracts returns the contract number correctly' );
140
is(
141
    $contracts->[0]->{contractstartdate}, $my_contract1->{contractstartdate},
142
    'GetContracts returns the contract start date correctly.'
143
);
144
is(
145
    $contracts->[0]->{contractenddate}, $my_contract1->{contractenddate},
146
    'GetContracts returns the contract end date correctly.'
147
);
148
is(
149
    $contracts->[0]->{contractname}, $my_contract1->{contractname},
150
    'GetContracts returns the contract name correctly.'
151
);
152
is(
153
    $contracts->[0]->{contractdescription}, $my_contract1->{contractdescription},
154
    'GetContracts returns the contract description correctly.'
155
);
156
is(
157
    $contracts->[0]->{booksellerid}, $my_contract1->{booksellerid},
158
    'GetContracts returns the bookseller id correctly.'
159
);
160
161
is( $contracts->[1]->{contractnumber}, $my_contract_id2, 'GetContracts returns the contract number correctly' );
162
is(
163
    $contracts->[1]->{contractstartdate}, $my_contract2->{contractstartdate},
164
    'GetContracts returns the contract start date correctly.'
165
);
166
is(
167
    $contracts->[1]->{contractenddate}, $my_contract2->{contractenddate},
168
    'GetContracts returns the contract end date correctly.'
169
);
170
is(
171
    $contracts->[1]->{contractname}, $my_contract2->{contractname},
172
    'GetContracts returns the contract name correctly.'
173
);
174
is(
175
    $contracts->[1]->{contractdescription}, $my_contract2->{contractdescription},
176
    'GetContracts returns the contract description correctly.'
177
);
178
is(
179
    $contracts->[1]->{booksellerid}, $my_contract2->{booksellerid},
180
    'GetContracts returns the bookseller id correctly.'
181
);
182
183
my $del_status = DelContract();
184
is( $del_status, undef, 'DelContract without contract number returns undef' );
185
186
$del_status = DelContract( { contractnumber => $my_contract_id1 } );
187
is( $del_status, 1, 'DelContract returns true' );
188
$contracts = GetContracts();
189
is( @$contracts, 1, 'DelContract deletes a contract' );
190
191
$del_status = DelContract( { contractnumber => $my_contract_id2 } );
192
is( $del_status, 1, 'DelContract returns true' );
193
$contracts = GetContracts();
194
is( @$contracts, 0, 'DelContract deletes a contract' );
195
- 

Return to bug 39711