Added comments to ILS-DI code
[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 =head1 NAME
29
30 C4::ILS-DI::Utility - ILS-DI Utilities
31
32 =cut
33
34 =head2 BorrowerExists
35
36 Checks, for a given userid and password, if the borrower exists.
37
38         if ( BorrowerExists($userid, $password) ) {
39                 # Do stuff
40         }
41
42 =cut
43
44 sub BorrowerExists {
45         my ( $userid, $password ) = @_;
46         $password = md5_base64($password);
47         my $dbh = C4::Context->dbh;
48         my $sth =
49         $dbh->prepare("SELECT COUNT(*) FROM borrowers WHERE userid =? and password=? ");
50         $sth->execute( $userid, $password );
51         return $sth->fetchrow;
52 }
53
54 =head2 CanBookBeReserved
55
56 Checks if a book (at bibliographic level) can be reserved by a borrower.
57
58         if ( CanBookBeReserved($borrower, $biblionumber) ) {
59                 # Do stuff
60         }
61
62 =cut
63
64 sub CanBookBeReserved {
65         my ( $borrower, $biblionumber ) = @_;
66
67         my $MAXIMUM_NUMBER_OF_RESERVES = C4::Context->preference("maxreserves");
68         my $MAXOUTSTANDING             = C4::Context->preference("maxoutstanding");
69
70         my $out = 1;
71
72         if ( $borrower->{'amountoutstanding'} > $MAXOUTSTANDING ) {
73                 $out = undef;
74         }
75         if ( $borrower->{gonenoaddress} eq 1 ) {
76                 $out = undef;
77         }
78         if ( $borrower->{lost} eq 1 ) {
79                 $out = undef;
80         }
81         if ( $borrower->{debarred} eq 1 ) {
82                 $out = undef;
83         }
84         my @reserves = GetReservesFromBorrowernumber( $borrower->{'borrowernumber'} );
85         if ( scalar(@reserves) >= $MAXIMUM_NUMBER_OF_RESERVES ) {
86                 $out = undef;
87         }
88         foreach my $res (@reserves) {
89                 if ( $res->{'biblionumber'} == $biblionumber ) {
90                         $out = undef;
91                 }
92         }
93         my $issues = GetPendingIssues($borrower->{'borrowernumber'});
94         foreach my $issue (@$issues) {
95                 if ( $issue->{'biblionumber'} == $biblionumber ) {
96                         $out = undef;
97                 }
98         }
99         
100         return $out;
101 }
102
103 =head2 Availability
104
105 Returns, for an itemnumber, an array containing availability information.
106
107         my ($biblionumber, $status, $msg, $location) = Availability($id);
108
109 =cut
110
111 sub Availability {
112         my ( $itemnumber ) = @_;
113         my $item = GetItem($itemnumber, undef, undef);
114         
115         if ( not $item->{'itemnumber'} ) { 
116                 return (undef, 'unknown', 'Error: could not retrieve availability for this ID', undef);
117         }
118         
119         my $biblionumber = $item->{'biblioitemnumber'};
120         my $location = GetBranchName($item->{'holdingbranch'});
121         
122         if ($item->{'notforloan'}) {
123                 return ($biblionumber, 'not available', 'Not for loan', $location);
124         }
125         elsif ($item->{'onloan'}) {
126                 return ($biblionumber, 'not available', 'Checked out', $location);
127         }
128         elsif ($item->{'itemlost'}) {
129                 return ($biblionumber, 'not available', 'Item lost', $location);
130         }
131         elsif ($item->{'wthdrawn'}) {
132                 return ($biblionumber, 'not available', 'Item withdrawn', $location);
133         }
134         elsif ($item->{'damaged'}) {
135                 return ($biblionumber, 'not available', 'Item damaged', $location);
136         }
137         else {
138                 return ($biblionumber, 'available', undef, $location);
139         }
140         
141         die Data::Dumper::Dumper($item);
142 }
143
144 1;