I forgot to export functions from Utility.pm
[koha.git] / C4 / ILSDI / Utility.pm
1 package C4::ILSDI::Utility;
2
3 # Copyright 2009 SARL Biblibre
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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use C4::Members;
21 use C4::Items;
22 use C4::Circulation;
23 use C4::Biblio;
24 use C4::Reserves;
25 use C4::Context;
26 use Digest::MD5 qw(md5_base64);
27
28 use vars qw($VERSION @ISA @EXPORT);
29
30 BEGIN {
31         # set the version for version checking
32         $VERSION = 3.00;
33         require Exporter;
34         @ISA    = qw(Exporter);
35         @EXPORT = qw(
36                 &BorrowerExists &CanBookBeReserved &Availability
37         );
38 }
39
40 =head1 NAME
41
42 C4::ILS-DI::Utility - ILS-DI Utilities
43
44 =cut
45
46 =head2 BorrowerExists
47
48 Checks, for a given userid and password, if the borrower exists.
49
50         if ( BorrowerExists($userid, $password) ) {
51                 # Do stuff
52         }
53
54 =cut
55
56 sub BorrowerExists {
57         my ( $userid, $password ) = @_;
58         $password = md5_base64($password);
59         my $dbh = C4::Context->dbh;
60         my $sth =
61         $dbh->prepare("SELECT COUNT(*) FROM borrowers WHERE userid =? and password=? ");
62         $sth->execute( $userid, $password );
63         return $sth->fetchrow;
64 }
65
66 =head2 CanBookBeReserved
67
68 Checks if a book (at bibliographic level) can be reserved by a borrower.
69
70         if ( CanBookBeReserved($borrower, $biblionumber) ) {
71                 # Do stuff
72         }
73
74 =cut
75
76 sub CanBookBeReserved {
77         my ( $borrower, $biblionumber ) = @_;
78
79         my $MAXIMUM_NUMBER_OF_RESERVES = C4::Context->preference("maxreserves");
80         my $MAXOUTSTANDING             = C4::Context->preference("maxoutstanding");
81
82         my $out = 1;
83
84         if ( $borrower->{'amountoutstanding'} > $MAXOUTSTANDING ) {
85                 $out = undef;
86         }
87         if ( $borrower->{gonenoaddress} eq 1 ) {
88                 $out = undef;
89         }
90         if ( $borrower->{lost} eq 1 ) {
91                 $out = undef;
92         }
93         if ( $borrower->{debarred} eq 1 ) {
94                 $out = undef;
95         }
96         my @reserves = GetReservesFromBorrowernumber( $borrower->{'borrowernumber'} );
97         if ( scalar(@reserves) >= $MAXIMUM_NUMBER_OF_RESERVES ) {
98                 $out = undef;
99         }
100         foreach my $res (@reserves) {
101                 if ( $res->{'biblionumber'} == $biblionumber ) {
102                         $out = undef;
103                 }
104         }
105         my $issues = GetPendingIssues($borrower->{'borrowernumber'});
106         foreach my $issue (@$issues) {
107                 if ( $issue->{'biblionumber'} == $biblionumber ) {
108                         $out = undef;
109                 }
110         }
111         
112         return $out;
113 }
114
115 =head2 Availability
116
117 Returns, for an itemnumber, an array containing availability information.
118
119         my ($biblionumber, $status, $msg, $location) = Availability($id);
120
121 =cut
122
123 sub Availability {
124         my ( $itemnumber ) = @_;
125         my $item = GetItem($itemnumber, undef, undef);
126         
127         if ( not $item->{'itemnumber'} ) { 
128                 return (undef, 'unknown', 'Error: could not retrieve availability for this ID', undef);
129         }
130         
131         my $biblionumber = $item->{'biblioitemnumber'};
132         my $location = GetBranchName($item->{'holdingbranch'});
133         
134         if ($item->{'notforloan'}) {
135                 return ($biblionumber, 'not available', 'Not for loan', $location);
136         }
137         elsif ($item->{'onloan'}) {
138                 return ($biblionumber, 'not available', 'Checked out', $location);
139         }
140         elsif ($item->{'itemlost'}) {
141                 return ($biblionumber, 'not available', 'Item lost', $location);
142         }
143         elsif ($item->{'wthdrawn'}) {
144                 return ($biblionumber, 'not available', 'Item withdrawn', $location);
145         }
146         elsif ($item->{'damaged'}) {
147                 return ($biblionumber, 'not available', 'Item damaged', $location);
148         }
149         else {
150                 return ($biblionumber, 'available', undef, $location);
151         }
152         
153         die Data::Dumper::Dumper($item);
154 }
155
156 1;