synch'ing 2.0.0 branch and head
[koha.git] / C4 / Circulation / Main.pm
1 package C4::Circulation::Main;
2
3 # $Id$
4
5 #package to deal with circulation
6
7
8 # Copyright 2000-2002 Katipo Communications
9 #
10 # This file is part of Koha.
11 #
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
15 # version.
16 #
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License along with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA  02111-1307 USA
24
25 use strict;
26 require Exporter;
27 use DBI;
28 use C4::Context;
29 use C4::Circulation::Issues;
30         # FIXME - C4::Circulation::Main and C4::Circulation::Issues
31         # use each other, so functions get redefined.
32 use C4::Circulation::Returns;
33         # FIXME - C4::Circulation::Main and C4::Circulation::Returns
34         # use each other, so functions get redefined.
35 use C4::Circulation::Renewals;
36         # FIXME - C4::Circulation::Main and C4::Circulation::Renewals
37         # use each other, so functions get redefined.
38 use C4::Circulation::Borrower;
39         # FIXME - C4::Circulation::Main and C4::Circulation::Borrower
40         # use each other, so functions get redefined.
41 use C4::Reserves;
42 use C4::Search;
43 use C4::InterfaceCDK;
44 use C4::Security;
45
46 use vars qw($VERSION @ISA @EXPORT);
47
48 # set the version for version checking
49 $VERSION = 0.01;
50
51 @ISA = qw(Exporter);
52 @EXPORT = qw(&pastitems &checkoverdues &previousissue
53 &checkreserve &checkwaiting &scanbook &scanborrower &getbranch &getprinter);
54
55 =head1 NAME
56
57 C4::Circulation::Main - Koha circulation desk functions
58
59 =head1 SYNOPSIS
60
61   use C4::Circulation::Main;
62
63 =head1 DESCRIPTION
64
65 This module provides functions useful to the circulation desk,
66 primarily for checking reserves and overdue items.
67
68 =head1 FUNCTIONS
69
70 =over 2
71
72 =cut
73
74 # FIXME - This is only used in C4::Circmain and telnet/startint.pl,
75 # which look obsolete. Presumably this means this function is obsolete
76 # as well.
77 # Otherwise, it needs a POD.
78 sub getbranch {
79   my ($env) = @_;
80   my $dbh = C4::Context->dbh;
81   my $sth = $dbh->prepare("select * from branches order by branchcode");
82   $sth->execute;
83   if ($sth->rows>1) {
84       my @branches;
85       while (my $data = $sth->fetchrow_hashref) {
86         push @branches,$data;
87       }
88       brmenu ($env,\@branches);
89   } else {
90       my $data = $sth->fetchrow_hashref;
91       $env->{'branchcode'}=$data->{'branchcode'};
92   }
93   $sth = $dbh->prepare("select * from branches
94     where branchcode = ?");
95   $sth->execute($env->{'branchcode'});
96   my $data = $sth->fetchrow_hashref;
97   $env->{'brdata'} = $data;
98   $env->{'branchname'} = $data->{'branchname'};
99   $sth->finish;
100 }
101
102 # FIXME - This is only used in C4::Circmain and telnet/startint.pl,
103 # which look obsolete. Presumably this means this function is obsolete
104 # as well.
105 # Otherwise, it needs a POD.
106 sub getprinter {
107   my ($env) = @_;
108   my $dbh = C4::Context->dbh;
109   my $sth = $dbh->prepare("select * from printers order by printername");
110   $sth->execute;
111   if ($sth->rows>1) {
112       my @printers;
113       while (my $data = $sth->fetchrow_hashref) {
114         push @printers,$data;
115       }
116       prmenu ($env,\@printers);
117   } else {
118       my $data=$sth->fetchrow_hashref;
119       $env->{'queue'}=$data->{'printqueue'};
120       $env->{'printtype'}=$data->{'printtype'};
121   }
122   $sth->finish;
123 }
124
125 # FIXME - This is not the same as &C4::Circulation::pastitems, though
126 # the two appear to share some code.
127 # FIXME - This function is called in &C4::Circulation::Issues::Issue
128 # and in telnet/borrwraper.pl, both of which look obsolete. Presumably
129 # this means this function is obsolete as well.
130 # Otherwise, it needs a POD.
131 sub pastitems{
132   #Get list of all items borrower has currently on issue
133   my ($env,$bornum,$dbh)=@_;
134   my $sth=$dbh->prepare("select * from issues  where (borrowernumber=?)
135     and (returndate is null) order by date_due");
136   $sth->execute($bornum);
137   my $i=0;
138   my @items;
139   my @items2;
140   while (my $data1=$sth->fetchrow_hashref) {
141     my $data = itemnodata($env,$dbh,$data1->{'itemnumber'}); #C4::Search
142     my @date = split("-",$data1->{'date_due'});
143     my $odate = (@date[2]+0)."-".(@date[1]+0)."-".@date[0];
144     my $line = C4::Circulation::Issues::formatitem($env,$data,$odate,"");
145     $items[$i]=$line;
146     $i++;
147   }
148   $sth->finish();
149   return(\@items,\@items2);
150 }
151
152 =item checkoverdues
153
154   $num_items = &checkoverdues($env, $borrowernumber, $dbh);
155
156 Returns the number of overdue books a patron has.
157
158 C<$env> is ignored.
159
160 C<$borrowernumber> is the patron's borrower number.
161
162 C<$dbh> is a DBI handle to the Koha database.
163
164 =cut
165 #'
166 sub checkoverdues{
167   #checks whether a borrower has overdue items
168   # FIXME - Use C4::Context->dbh instead of getting $dbh as an argument
169   my ($env,$bornum,$dbh)=@_;
170   my $sth=$dbh->prepare("Select count(*) from issues where borrowernumber=? and
171         returndate is NULL and date_due < curdate()");
172   $sth->execute($bornum);
173   my $data = $sth->fetchrow_hashref;
174   $sth->finish;
175   return $data->{'count(*)'};
176 }
177
178 # FIXME - This is quite similar to &C4::Circulation::previousissue
179 # FIXME - Never used. Obsolete, presumably.
180 # Otherwise, it needs a POD.
181 sub previousissue {
182   my ($env,$itemnum,$dbh,$bornum)=@_;
183   my $sth=$dbh->prepare("Select
184      firstname,surname,issues.borrowernumber,cardnumber,returndate
185      from issues,borrowers where
186      issues.itemnumber='$itemnum' and
187      issues.borrowernumber=borrowers.borrowernumber
188      and issues.returndate is NULL");
189   $sth->execute;
190   my $borrower=$sth->fetchrow_hashref;
191   my $canissue = "Y";
192   $sth->finish;
193   my $newdate;
194   if ($borrower->{'borrowernumber'} ne ''){
195     if ($bornum eq $borrower->{'borrowernumber'}){
196       # no need to issue
197       my ($renewstatus) = C4::Circulation::Renewals::renewstatus($env,$dbh,$bornum,$itemnum);
198       my ($resbor,$resrec) = checkreserve($env,$dbh,$itemnum);
199       if ($renewstatus == "0") {
200         info_msg($env,"</S>Issued to this borrower - No renewals<!S>");
201         $canissue = "N";
202       } elsif ($resbor ne "") {
203         my $resp = C4::InterfaceCDK::msg_ny($env,"Book is issued to this borrower",
204           "and is reserved - Renew?");
205         if ($resp eq "Y") {
206           $newdate = C4::Circulation::Renewals::renewbook($env,$dbh,$bornum,$itemnum);
207           $canissue = "R";
208         } else {
209           $canissue = "N";
210         }
211       } else {
212         my $resp = C4::InterfaceCDK::msg_yn($env,"Book is issued to this borrower", "Renew?");
213         if ($resp eq "Y") {
214           $newdate = C4::Circulation::Renewals::renewbook($env,$dbh,$bornum,$itemnum);
215           $canissue = "R";
216         } else {
217           $canissue = "N";
218         }
219       }
220     } else {
221       my $text="Issued to $borrower->{'firstname'} $borrower->{'surname'} ($borrower->{'cardnumber'})";
222       my $resp = C4::InterfaceCDK::msg_yn($env,$text,"Mark as returned?");
223       if ( $resp eq "Y") {
224         &returnrecord($env,$dbh,$borrower->{'borrowernumber'},$itemnum);
225       } else {
226         $canissue = "N";
227       }
228     }
229   }
230   return($borrower->{'borrowernumber'},$canissue,$newdate);
231 }
232
233 =item checkreserve
234
235   ($borrowernumber, $reserve) = &checkreserve($env, $dbh, $itemnumber);
236
237 C<$env> is ignored.
238
239 C<$dbh> is a DBI handle to the Koha database.
240
241 C<$itemnumber> is the number of the item to find.
242
243 C<&checkreserve> returns two values:
244
245 C<$borrowernumber> is the borrower number of the patron for whom the
246 book is reserved, or the empty string. I can't tell when it returns a
247 number and when it returns a string, nor what it means.
248
249 C<$reserve> describes the reserved item. It is a reference-to-hash
250 whose keys are the fields of the reserves and items tables of the Koha
251 database.
252
253 =cut
254 #'
255 sub checkreserve{
256   # Check for reserves for biblio
257   # FIXME - Use C4::Context->dbh to get $dbh, instead of passing it
258   # on the argument list.
259   my ($env,$dbh,$itemnum)=@_;
260   my $resbor = "";
261   # Find this item in the reserves.
262   # Apparently reserves.found=='W' means "Waiting".
263   # FIXME - Is it necessary to get every field from both tables?
264   my $sth = $dbh->prepare("select * from reserves,items
265     where (items.itemnumber = ?)
266     and (reserves.cancellationdate is NULL)
267     and (items.biblionumber = reserves.biblionumber)
268     and ((reserves.found = 'W')
269     or (reserves.found is null))
270     order by priority");
271   $sth->execute($itemnum);
272   my $resrec;
273   if (my $data=$sth->fetchrow_hashref) {
274     $resrec=$data;
275     my $const = $data->{'constrainttype'};
276     if ($const eq "a") {                # FIXME - What does 'a' mean?
277       $resbor = $data->{'borrowernumber'};
278     } else {
279       my $found = 0;
280       my $csth = $dbh->prepare("select * from reserveconstraints,items
281          where (borrowernumber=?)
282          and reservedate=?
283          and reserveconstraints.biblionumber=?
284          and (items.itemnumber=? and
285          items.biblioitemnumber = reserveconstraints.biblioitemnumber)");
286       $csth->execute($data->{'borrowernumber'},$data->{'reservedate'},$data->{'biblionumber'},$itemnum);
287       if (my $cdata=$csth->fetchrow_hashref) {$found = 1;}
288       if ($const eq 'o') {              # FIXME - What does 'o' mean?
289         if ($found eq 1) {$resbor = $data->{'borrowernumber'};}
290       } else {
291         if ($found eq 0) {$resbor = $data->{'borrowernumber'};}
292       }
293       $csth->finish();
294     }
295   }
296   $sth->finish;
297   return ($resbor,$resrec);
298 }
299
300 # FIXME - This is only used in C4::Circulation::Borrower, which
301 # appears to be obsolete. Presumably this function is obsolete as
302 # well. Otherwise, it needs a POD.
303 sub checkwaiting{
304   # check for reserves waiting
305   my ($env,$dbh,$bornum)=@_;
306   my @itemswaiting;
307   my $sth = $dbh->prepare("select * from reserves
308     where (borrowernumber = ?)
309     and (reserves.found='W') and cancellationdate is NULL");
310   $sth->execute($bornum);
311   my $cnt=0;
312   if (my $data=$sth->fetchrow_hashref) {
313     @itemswaiting[$cnt] =$data;
314     $cnt ++
315   }
316   $sth->finish;
317   return ($cnt,\@itemswaiting);
318 }
319
320 # FIXME - This is identical to &C4::Circulation::scanbook
321 # FIXME - This function is only used in tkperl/tkcirc, if anywhere
322 # (it's hard to tell). Presumably it's obsolete.
323 # Otherwise, it needs a POD.
324 sub scanbook {
325   my ($env,$interface)=@_;
326   #scan barcode
327   my ($number,$reason)=dialog("Book Barcode:");
328   $number=uc $number;
329   return ($number,$reason);
330 }
331
332 # FIXME - This is very similar to &C4::Circulation::scanborrower
333 # FIXME - This is only used in C4::Circulation::Borrower, which
334 # appears to be obsolete. Presumably this function is obsolete as
335 # well.
336 # Otherwise, it needs a POD.
337 sub scanborrower {
338   my ($env,$interface)=@_;
339   #scan barcode
340   my ($number,$reason,$book)=C4::InterfaceCDK::borrower_dialog($env); #C4::InterfaceCDK
341   $number= $number;             # FIXME - WTF?
342   $book=uc $book;
343   return ($number,$reason,$book);
344 }
345
346 1;
347 __END__
348
349 =back
350
351 =head1 AUTHOR
352
353 Koha Developement team <info@koha.org>
354
355 =cut