Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.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 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
55 =head2 GetContracts
56
57 $contractlist = GetContracts({
58     booksellerid => $booksellerid,
59     activeonly => $activeonly
60 });
61
62 Looks up the contracts that belong to a bookseller
63
64 Returns a list of contracts
65
66 =over
67
68 =item C<$booksellerid> is the "id" field in the "aqbooksellers" table.
69
70 =item C<$activeonly> if exists get only contracts that are still active.
71
72 =back
73
74 =cut
75
76 sub GetContracts {
77     my ($filters) = @_;
78     if( $filters->{activeonly} ) {
79         $filters->{contractenddate} = {'>=' => \'now()'};
80         delete $filters->{activeonly};
81     }
82
83     my $rs = Koha::Database->new()->schema->resultset('Aqcontract');
84     $rs = $rs->search($filters);
85     $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
86     return [ $rs->all ];
87 }
88
89 =head2 GetContract
90
91 $contract = GetContract( { contractnumber => $contractnumber } );
92
93 Looks up the contract that has PRIMKEY (contractnumber) value $contractID
94
95 Returns a contract
96
97 =cut
98
99 sub GetContract {
100     my ($params) = @_;
101     my $contractnumber = $params->{contractnumber};
102
103     return unless $contractnumber;
104
105     my $contracts = GetContracts({
106         contractnumber => $contractnumber,
107     });
108     return $contracts->[0];
109 }
110
111 sub AddContract {
112     my ($contract) = @_;
113     return unless($contract->{booksellerid});
114
115     my $rs = Koha::Database->new()->schema->resultset('Aqcontract');
116     return $rs->create($contract)->id;
117 }
118
119 sub ModContract {
120     my ($contract) = @_;
121     my $result = Koha::Database->new()->schema->resultset('Aqcontract')->find($contract);
122     return unless($result);
123
124     $result = $result->update($contract);
125     return $result->in_storage;
126 }
127
128 sub DelContract {
129     my ($contract) = @_;
130     return unless($contract->{contractnumber});
131
132     my $result = Koha::Database->new()->schema->resultset('Aqcontract')->find($contract);
133     return unless($result);
134
135     eval { $result->delete };
136     return !( $result->in_storage );
137 }
138
139 1;
140
141 __END__
142
143 =head1 AUTHOR
144
145 Koha Development Team <http://koha-community.org/>
146
147 =cut