bug 2505: enable strict and warnings in 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 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         # set the version for version checking
36         $VERSION = 3.00;
37         require Exporter;
38         @ISA    = qw(Exporter);
39         @EXPORT = qw(
40                 &BorrowerExists &CanBookBeReserved &Availability
41         );
42 }
43
44 =head1 NAME
45
46 C4::ILS-DI::Utility - ILS-DI Utilities
47
48 =cut
49
50 =head2 BorrowerExists
51
52 Checks, for a given userid and password, if the borrower exists.
53
54         if ( BorrowerExists($userid, $password) ) {
55                 # Do stuff
56         }
57
58 =cut
59
60 sub BorrowerExists {
61         my ( $userid, $password ) = @_;
62         $password = md5_base64($password);
63         my $dbh = C4::Context->dbh;
64         my $sth =
65         $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         }
141         elsif ($item->{'onloan'}) {
142                 return ($biblionumber, 'not available', 'Checked out', $location);
143         }
144         elsif ($item->{'itemlost'}) {
145                 return ($biblionumber, 'not available', 'Item lost', $location);
146         }
147         elsif ($item->{'wthdrawn'}) {
148                 return ($biblionumber, 'not available', 'Item withdrawn', $location);
149         }
150         elsif ($item->{'damaged'}) {
151                 return ($biblionumber, 'not available', 'Item damaged', $location);
152         }
153         else {
154                 return ($biblionumber, 'available', undef, $location);
155         }
156         
157         die Data::Dumper::Dumper($item);
158 }
159
160 1;