Changed a function call to a class method call, and a few more anal
[koha.git] / C4 / Circulation.pm
1 package C4::Circulation; #assumes C4/Circulation
2
3 #package to deal with circulation 
4
5
6 # Copyright 2000-2002 Katipo Communications
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along with
20 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
21 # Suite 330, Boston, MA  02111-1307 USA
22
23 use strict;
24 require Exporter;
25 use DBI;
26 use C4::Database;
27 use C4::Circulation::Issues;
28 use C4::Circulation::Returns;
29 use C4::Circulation::Renewals;
30 use C4::Circulation::Borrower;
31 use C4::Reserves;
32 #use C4::Interface;
33 use C4::Security;
34
35 use vars qw($VERSION @ISA @EXPORT);
36   
37 # set the version for version checking
38 $VERSION = 0.01;
39     
40 @ISA = qw(Exporter);
41 @EXPORT = qw(&Start_circ &scanborrower);
42
43 sub Start_circ{
44   my ($env)=@_;
45   #connect to database
46   #start interface
47   &startint($env,'Circulation');
48   my $donext = 'Circ';
49   my $reason;
50   my $data;
51   while ($donext ne 'Quit') {
52     if ($donext  eq "Circ") {
53       clearscreen();        
54       ($reason,$data) = menu($env,'console','Circulation', 
55         ('Issues','Returns','Borrower Enquiries','Reserves','Log In'));
56       #debug_msg($env,"data = $data");
57     } else {
58       $data = $donext;
59     }
60     if ($data eq 'Issues') {  
61       $donext=Issue($env); #C4::Circulation::Issues
62       #debug_msg("","do next $donext");
63     } elsif ($data eq 'Returns') {
64       $donext=Returns($env); #C4::Circulation::Returns
65     } elsif ($data eq 'Borrower Enquiries'){
66       $donext=Borenq($env); #C4::Circulation::Borrower
67     } elsif ($data eq 'Reserves'){
68       $donext=EnterReserves($env); #C4::Reserves
69     } elsif ($data eq 'Log In') {
70       &endint($env);
71       &Login($env);   #C4::Security
72       &startint($env,'Circulation');
73     } elsif ($data eq 'Quit') { 
74       $donext = $data;
75     }
76     #debug_msg($env,"donext -  $donext");
77   }
78   &endint($env)  
79 }
80
81 # Not exported.
82 # FIXME - This is not the same as &C4::Circulation::Main::pastitems,
83 # though the two appear to share some code.
84 sub pastitems{
85   #Get list of all items borrower has currently on issue
86   my ($env,$bornum,$dbh)=@_;
87   my $sth=$dbh->prepare("Select * from issues,items,biblio
88     where borrowernumber=$bornum and issues.itemnumber=items.itemnumber
89     and items.biblionumber=biblio.biblionumber
90     and returndate is null
91     order by date_due");
92   $sth->execute;
93   my $i=0;
94   my @items;
95   my @items2;
96   #$items[0]=" "x29;
97   #$items2[0]=" "x29;
98   $items[0]=" "x72;
99   $items2[0]=" "x72;
100   while (my $data=$sth->fetchrow_hashref) {
101      my $line = "$data->{'date_due'} $data->{'title'}";
102      # $items[$i]=fmtstr($env,$line,"L29");
103      $items[$i]=fmtstr($env,$line,"L72");
104      $i++;
105   }
106   return(\@items,\@items2);
107   $sth->finish;
108 }
109
110 sub checkoverdues{
111   #checks whether a borrower has overdue items
112   my ($env,$bornum,$dbh)=@_;
113   my $sth=$dbh->prepare("Select * from issues,items,biblio where
114   borrowernumber=$bornum and issues.itemnumber=items.itemnumber and
115   items.biblionumber=biblio.biblionumber");
116   $sth->execute;
117   my $row=1;
118   my $col=40;
119   while (my $data=$sth->fetchrow_hashref){
120     output($row,$col,$data->{'title'});
121     $row++;
122   }
123   $sth->finish;
124 }
125
126 # FIXME - This is quite similar to &C4::Circulation::Main::previousissue
127 sub previousissue {
128   my ($env,$itemnum,$dbh,$bornum)=@_;
129   my $sth=$dbh->prepare("Select firstname,surname,issues.borrowernumber,cardnumber,returndate
130   from issues,borrowers where 
131   issues.itemnumber='$itemnum' and
132   issues.borrowernumber=borrowers.borrowernumber and issues.returndate is
133 NULL");
134   $sth->execute;
135   my $borrower=$sth->fetchrow_hashref;
136   $sth->finish;
137   if ($borrower->{'borrowernumber'} ne ''){
138     if ($bornum eq $borrower->{'borrowernumber'}){
139       # no need to issue
140       my ($renewstatus) = &renewstatus($env,$dbh,$bornum,$itemnum);
141       my $resp = &msg_yn("Book is issued to this borrower", "Renew?");
142       if ($resp == "y") {
143         &renewbook($env,$dbh,$bornum,$itemnum);
144       }  
145       
146     } else {
147       my $text="Issued to $borrower->{'firstname'} $borrower->{'surname'} ($borrower->{'cardnumber'})";    
148       my $resp = &msg_yn($text,"Mark as returned?");
149       if ($resp == "y") {
150         &returnrecord($env,$dbh,$borrower->{'borrowernumber'},$itemnum);
151         # can issue
152       } else {
153         # can't issue
154       } 
155     }
156   } 
157   return($borrower->{'borrowernumber'});
158   $sth->finish;
159 }
160
161
162 sub checkreserve{
163   # Check for reserves for biblio 
164   # does not look at constraints yet
165   my ($env,$dbh,$itemnum)=@_;
166   my $resbor = "";
167   my $query = "select * from reserves,items 
168   where (items.itemnumber = '$itemnum')
169   and (items.biblionumber = reserves.biblionumber)
170   and (reserves.found is null) order by priority";
171   my $sth = $dbh->prepare($query);
172   $sth->execute();
173   if (my $data=$sth->fetchrow_hashref) {
174     $resbor = $data->{'borrowernumber'}; 
175   }
176   return ($resbor);
177   $sth->finish;
178 }
179
180 sub checkwaiting{
181   # check for reserves waiting
182   my ($env,$dbh,$bornum)=@_;
183   my @itemswaiting="";
184   my $query = "select * from reserves
185   where (borrowernumber = '$bornum')
186   and (reserves.found='W')";
187   my $sth = $dbh->prepare($query);
188   $sth->execute();
189   if (my $data=$sth->fetchrow_hashref) {
190     push @itemswaiting,$data->{'itemnumber'}; 
191   }
192   return (\@itemswaiting);
193   $sth->finish;
194 }
195
196 # FIXME - This is identical to &C4::Circulation/Main::scanbook
197 sub scanbook {
198   my ($env,$interface)=@_;
199   #scan barcode
200   my ($number,$reason)=dialog("Book Barcode:");
201   $number=uc $number;
202   return ($number,$reason);
203 }
204
205 # FIXME - This is very similar to &C4::Circulation::Main::scanborrower
206 sub scanborrower {
207   my ($env,$interface)=@_;
208   #scan barcode
209   my ($number,$reason,$book)=&borrower_dialog($env); #C4::Interface
210   $number= $number;
211   $book=uc $book;
212   return ($number,$reason,$book);
213 }
214
215
216 END { }       # module clean-up code here (global destructor)