Bug 6755 follow up
[koha.git] / C4 / ILSDI / Utility.pm
blob8ee1224e640a6648e3ad2f9f4243b28eb5718f93
1 package C4::ILSDI::Utility;
3 # Copyright 2009 SARL Biblibre
5 # This file is part of Koha.
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.
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.
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.
20 use strict;
21 use warnings;
23 use C4::Members;
24 use C4::Items;
25 use C4::Circulation;
26 use C4::Biblio;
27 use C4::Reserves qw(GetReservesFromBorrowernumber);
28 use C4::Context;
29 use C4::Branch qw/GetBranchName/;
30 use Digest::MD5 qw(md5_base64);
32 use vars qw($VERSION @ISA @EXPORT);
34 BEGIN {
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
45 =head1 NAME
47 C4::ILS-DI::Utility - ILS-DI Utilities
49 =cut
51 =head2 BorrowerExists
53 Checks, for a given userid and password, if the borrower exists.
55 if ( BorrowerExists($userid, $password) ) {
56 # Do stuff
59 =cut
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;
70 =head2 CanBookBeReserved
72 Checks if a book (at bibliographic level) can be reserved by a borrower.
74 if ( CanBookBeReserved($borrower, $biblionumber) ) {
75 # Do stuff
78 =cut
80 sub CanBookBeReserved {
81 my ( $borrower, $biblionumber ) = @_;
83 my $MAXIMUM_NUMBER_OF_RESERVES = C4::Context->preference("maxreserves");
84 my $MAXOUTSTANDING = C4::Context->preference("maxoutstanding");
86 my $out = 1;
88 if ( $borrower->{'amountoutstanding'} > $MAXOUTSTANDING ) {
89 $out = undef;
91 if ( $borrower->{gonenoaddress} eq 1 ) {
92 $out = undef;
94 if ( $borrower->{lost} eq 1 ) {
95 $out = undef;
97 if ( $borrower->{debarred} eq 1 ) {
98 $out = undef;
100 my @reserves = GetReservesFromBorrowernumber( $borrower->{'borrowernumber'} );
101 if ( $MAXIMUM_NUMBER_OF_RESERVES && scalar(@reserves) >= $MAXIMUM_NUMBER_OF_RESERVES ) {
102 $out = undef;
104 foreach my $res (@reserves) {
105 if ( $res->{'biblionumber'} == $biblionumber ) {
106 $out = undef;
109 my $issues = GetPendingIssues( $borrower->{'borrowernumber'} );
110 foreach my $issue (@$issues) {
111 if ( $issue->{'biblionumber'} == $biblionumber ) {
112 $out = undef;
116 return $out;
119 =head2 Availability
121 Returns, for an itemnumber, an array containing availability information.
123 my ($biblionumber, $status, $msg, $location) = Availability($id);
125 =cut
127 sub Availability {
128 my ($itemnumber) = @_;
129 my $item = GetItem( $itemnumber, undef, undef );
131 if ( not $item->{'itemnumber'} ) {
132 return ( undef, 'unknown', 'Error: could not retrieve availability for this ID', undef );
135 my $biblionumber = $item->{'biblioitemnumber'};
136 my $location = GetBranchName( $item->{'holdingbranch'} );
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 );
152 die Data::Dumper::Dumper($item);