Bug 12467 - Lost items marked as not on loan even if they are!
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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($VERSION @ISA @EXPORT);
27
28 BEGIN {
29         # set the version for version checking
30     $VERSION = 3.07.00.049;
31     require Exporter;
32         @ISA    = qw(Exporter);
33         @EXPORT = qw(
34         &GetContracts
35         &GetContract
36         &AddContract
37         &ModContract
38         &DelContract
39         );
40 }
41
42 =head1 NAME
43
44 C4::Contract - Koha functions for dealing with bookseller contracts.
45
46 =head1 SYNOPSIS
47
48 use C4::Contract;
49
50 =head1 DESCRIPTION
51
52 The functions in this module deal with contracts. They allow to
53 add a new contract, to modify it or to get some informations around
54 a contract.
55
56 =cut
57
58
59 =head2 GetContracts
60
61 $contractlist = GetContracts({
62     booksellerid => $booksellerid,
63     activeonly => $activeonly
64 });
65
66 Looks up the contracts that belong to a bookseller
67
68 Returns a list of contracts
69
70 =over
71
72 =item C<$booksellerid> is the "id" field in the "aqbooksellers" table.
73
74 =item C<$activeonly> if exists get only contracts that are still active.
75
76 =back
77
78 =cut
79
80 sub GetContracts {
81     my ($filters) = @_;
82     if( $filters->{activeonly} ) {
83         $filters->{contractenddate} = {'>=' => \'now()'};
84         delete $filters->{activeonly};
85     }
86
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 ];
91 }
92
93 =head2 GetContract
94
95 $contract = GetContract( { contractnumber => $contractnumber } );
96
97 Looks up the contract that has PRIMKEY (contractnumber) value $contractID
98
99 Returns a contract
100
101 =cut
102
103 sub GetContract {
104     my ($params) = @_;
105     my $contractnumber = $params->{contractnumber};
106
107     return unless $contractnumber;
108
109     my $contracts = GetContracts({
110         contractnumber => $contractnumber,
111     });
112     return $contracts->[0];
113 }
114
115 sub AddContract {
116     my ($contract) = @_;
117     return unless($contract->{booksellerid});
118
119     my $rs = Koha::Database->new()->schema->resultset('Aqcontract');
120     return $rs->create($contract)->id;
121 }
122
123 sub ModContract {
124     my ($contract) = @_;
125     my $result = Koha::Database->new()->schema->resultset('Aqcontract')->find($contract);
126     return unless($result);
127
128     $result = $result->update($contract);
129     return $result->in_storage;
130 }
131
132 sub DelContract {
133     my ($contract) = @_;
134     return unless($contract->{contractnumber});
135
136     my $result = Koha::Database->new()->schema->resultset('Aqcontract')->find($contract);
137     return unless($result);
138
139     eval { $result->delete };
140     return !( $result->in_storage );
141 }
142
143 1;
144
145 __END__
146
147 =head1 AUTHOR
148
149 Koha Development Team <http://koha-community.org/>
150
151 =cut