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