Bug 16011: $VERSION - Remove the $VERSION init
[koha.git] / C4 / Contract.pm
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 strict;
22 #use warnings; FIXME - Bug 2505
23 use C4::Context;
24 use Koha::Database;
25
26 use vars qw(@ISA @EXPORT);
27
28 BEGIN {
29         # set the version for version checking
30     require Exporter;
31         @ISA    = qw(Exporter);
32         @EXPORT = qw(
33         &GetContracts
34         &GetContract
35         &AddContract
36         &ModContract
37         &DelContract
38         );
39 }
40
41 =head1 NAME
42
43 C4::Contract - Koha functions for dealing with bookseller contracts.
44
45 =head1 SYNOPSIS
46
47 use C4::Contract;
48
49 =head1 DESCRIPTION
50
51 The functions in this module deal with contracts. They allow to
52 add a new contract, to modify it or to get some informations around
53 a contract.
54
55 =cut
56
57
58 =head2 GetContracts
59
60 $contractlist = GetContracts({
61     booksellerid => $booksellerid,
62     activeonly => $activeonly
63 });
64
65 Looks up the contracts that belong to a bookseller
66
67 Returns a list of contracts
68
69 =over
70
71 =item C<$booksellerid> is the "id" field in the "aqbooksellers" table.
72
73 =item C<$activeonly> if exists get only contracts that are still active.
74
75 =back
76
77 =cut
78
79 sub GetContracts {
80     my ($filters) = @_;
81     if( $filters->{activeonly} ) {
82         $filters->{contractenddate} = {'>=' => \'now()'};
83         delete $filters->{activeonly};
84     }
85
86     my $rs = Koha::Database->new()->schema->resultset('Aqcontract');
87     $rs = $rs->search($filters);
88     $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
89     return [ $rs->all ];
90 }
91
92 =head2 GetContract
93
94 $contract = GetContract( { contractnumber => $contractnumber } );
95
96 Looks up the contract that has PRIMKEY (contractnumber) value $contractID
97
98 Returns a contract
99
100 =cut
101
102 sub GetContract {
103     my ($params) = @_;
104     my $contractnumber = $params->{contractnumber};
105
106     return unless $contractnumber;
107
108     my $contracts = GetContracts({
109         contractnumber => $contractnumber,
110     });
111     return $contracts->[0];
112 }
113
114 sub AddContract {
115     my ($contract) = @_;
116     return unless($contract->{booksellerid});
117
118     my $rs = Koha::Database->new()->schema->resultset('Aqcontract');
119     return $rs->create($contract)->id;
120 }
121
122 sub ModContract {
123     my ($contract) = @_;
124     my $result = Koha::Database->new()->schema->resultset('Aqcontract')->find($contract);
125     return unless($result);
126
127     $result = $result->update($contract);
128     return $result->in_storage;
129 }
130
131 sub DelContract {
132     my ($contract) = @_;
133     return unless($contract->{contractnumber});
134
135     my $result = Koha::Database->new()->schema->resultset('Aqcontract')->find($contract);
136     return unless($result);
137
138     eval { $result->delete };
139     return !( $result->in_storage );
140 }
141
142 1;
143
144 __END__
145
146 =head1 AUTHOR
147
148 Koha Development Team <http://koha-community.org/>
149
150 =cut