Cleaned things up a bit.
[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::Circulation::Issues;
27 use C4::Circulation::Returns;
28 use C4::Circulation::Renewals;
29 use C4::Circulation::Borrower;
30 use C4::Reserves;
31 #use C4::Interface;
32 use C4::Security;
33
34 use vars qw($VERSION @ISA @EXPORT);
35   
36 # set the version for version checking
37 $VERSION = 0.01;
38     
39 @ISA = qw(Exporter);
40 @EXPORT = qw(&Start_circ &scanborrower);
41
42 sub Start_circ{
43   my ($env)=@_;
44   #connect to database
45   #start interface
46   &startint($env,'Circulation');
47   my $donext = 'Circ';
48   my $reason;
49   my $data;
50   while ($donext ne 'Quit') {
51     if ($donext  eq "Circ") {
52       clearscreen();        
53       ($reason,$data) = menu($env,'console','Circulation', 
54         ('Issues','Returns','Borrower Enquiries','Reserves','Log In'));
55       #debug_msg($env,"data = $data");
56     } else {
57       $data = $donext;
58     }
59     if ($data eq 'Issues') {  
60       $donext=Issue($env); #C4::Circulation::Issues
61       #debug_msg("","do next $donext");
62     } elsif ($data eq 'Returns') {
63       $donext=Returns($env); #C4::Circulation::Returns
64     } elsif ($data eq 'Borrower Enquiries'){
65       $donext=Borenq($env); #C4::Circulation::Borrower
66     } elsif ($data eq 'Reserves'){
67       $donext=EnterReserves($env); #C4::Reserves
68     } elsif ($data eq 'Log In') {
69       &endint($env);
70       &Login($env);   #C4::Security
71       &startint($env,'Circulation');
72     } elsif ($data eq 'Quit') { 
73       $donext = $data;
74     }
75     #debug_msg($env,"donext -  $donext");
76   }
77   &endint($env)  
78 }
79
80 # Not exported.
81 # FIXME - This is not the same as &C4::Circulation::Main::pastitems,
82 # though the two appear to share some code.
83 sub pastitems{
84   #Get list of all items borrower has currently on issue
85   my ($env,$bornum,$dbh)=@_;
86   my $sth=$dbh->prepare("Select * from issues,items,biblio
87     where borrowernumber=$bornum and issues.itemnumber=items.itemnumber
88     and items.biblionumber=biblio.biblionumber
89     and returndate is null
90     order by date_due");
91   $sth->execute;
92   my $i=0;
93   my @items;
94   my @items2;
95   #$items[0]=" "x29;
96   #$items2[0]=" "x29;
97   $items[0]=" "x72;
98   $items2[0]=" "x72;
99   while (my $data=$sth->fetchrow_hashref) {
100      my $line = "$data->{'date_due'} $data->{'title'}";
101      # $items[$i]=fmtstr($env,$line,"L29");
102      $items[$i]=fmtstr($env,$line,"L72");
103      $i++;
104   }
105   return(\@items,\@items2);
106   $sth->finish;
107 }
108
109 sub checkoverdues{
110   #checks whether a borrower has overdue items
111   my ($env,$bornum,$dbh)=@_;
112   my $sth=$dbh->prepare("Select * from issues,items,biblio where
113   borrowernumber=$bornum and issues.itemnumber=items.itemnumber and
114   items.biblionumber=biblio.biblionumber");
115   $sth->execute;
116   my $row=1;
117   my $col=40;
118   while (my $data=$sth->fetchrow_hashref){
119     output($row,$col,$data->{'title'});
120     $row++;
121   }
122   $sth->finish;
123 }
124
125 # FIXME - This is quite similar to &C4::Circulation::Main::previousissue
126 sub previousissue {
127   my ($env,$itemnum,$dbh,$bornum)=@_;
128   my $sth=$dbh->prepare("Select firstname,surname,issues.borrowernumber,cardnumber,returndate
129   from issues,borrowers where 
130   issues.itemnumber='$itemnum' and
131   issues.borrowernumber=borrowers.borrowernumber and issues.returndate is
132 NULL");
133   $sth->execute;
134   my $borrower=$sth->fetchrow_hashref;
135   $sth->finish;
136   if ($borrower->{'borrowernumber'} ne ''){
137     if ($bornum eq $borrower->{'borrowernumber'}){
138       # no need to issue
139       my ($renewstatus) = &renewstatus($env,$dbh,$bornum,$itemnum);
140       my $resp = &msg_yn("Book is issued to this borrower", "Renew?");
141       if ($resp == "y") {
142         &renewbook($env,$dbh,$bornum,$itemnum);
143       }  
144       
145     } else {
146       my $text="Issued to $borrower->{'firstname'} $borrower->{'surname'} ($borrower->{'cardnumber'})";    
147       my $resp = &msg_yn($text,"Mark as returned?");
148       if ($resp == "y") {
149         &returnrecord($env,$dbh,$borrower->{'borrowernumber'},$itemnum);
150         # can issue
151       } else {
152         # can't issue
153       } 
154     }
155   } 
156   return($borrower->{'borrowernumber'});
157   $sth->finish;
158 }
159
160
161 sub checkreserve{
162   # Check for reserves for biblio 
163   # does not look at constraints yet
164   my ($env,$dbh,$itemnum)=@_;
165   my $resbor = "";
166   my $query = "select * from reserves,items 
167   where (items.itemnumber = '$itemnum')
168   and (items.biblionumber = reserves.biblionumber)
169   and (reserves.found is null) order by priority";
170   my $sth = $dbh->prepare($query);
171   $sth->execute();
172   if (my $data=$sth->fetchrow_hashref) {
173     $resbor = $data->{'borrowernumber'}; 
174   }
175   return ($resbor);
176   $sth->finish;
177 }
178
179 sub checkwaiting{
180   # check for reserves waiting
181   my ($env,$dbh,$bornum)=@_;
182   my @itemswaiting="";
183   my $query = "select * from reserves
184   where (borrowernumber = '$bornum')
185   and (reserves.found='W')";
186   my $sth = $dbh->prepare($query);
187   $sth->execute();
188   if (my $data=$sth->fetchrow_hashref) {
189     push @itemswaiting,$data->{'itemnumber'}; 
190   }
191   return (\@itemswaiting);
192   $sth->finish;
193 }
194
195 # FIXME - This is identical to &C4::Circulation/Main::scanbook
196 sub scanbook {
197   my ($env,$interface)=@_;
198   #scan barcode
199   my ($number,$reason)=dialog("Book Barcode:");
200   $number=uc $number;
201   return ($number,$reason);
202 }
203
204 # FIXME - This is very similar to &C4::Circulation::Main::scanborrower
205 sub scanborrower {
206   my ($env,$interface)=@_;
207   #scan barcode
208   my ($number,$reason,$book)=&borrower_dialog($env); #C4::Interface
209   $number= $number;
210   $book=uc $book;
211   return ($number,$reason,$book);
212 }
213
214
215 END { }       # module clean-up code here (global destructor)