Replaced expressions of the form "$x = $x <op> $y" with "$x <op>= $y".
[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 $query = "select * from branches order by branchcode";
82   my $sth = $dbh->prepare($query);
83   $sth->execute;
84   if ($sth->rows>1) {
85       my @branches;
86       while (my $data = $sth->fetchrow_hashref) {
87         push @branches,$data;
88       }
89       brmenu ($env,\@branches);
90   } else {
91       my $data = $sth->fetchrow_hashref;
92       $env->{'branchcode'}=$data->{'branchcode'};
93   }
94   my $query = "select * from branches
95     where branchcode = '$env->{'branchcode'}'";
96   $sth = $dbh->prepare($query);
97   $sth->execute;
98   my $data = $sth->fetchrow_hashref;
99   $env->{'brdata'} = $data;
100   $env->{'branchname'} = $data->{'branchname'};
101   $sth->finish;
102 }
103
104 # FIXME - This is only used in C4::Circmain and telnet/startint.pl,
105 # which look obsolete. Presumably this means this function is obsolete
106 # as well.
107 # Otherwise, it needs a POD.
108 sub getprinter {
109   my ($env) = @_;
110   my $dbh = C4::Context->dbh;
111   my $query = "select * from printers order by printername";
112   my $sth = $dbh->prepare($query);
113   $sth->execute;
114   if ($sth->rows>1) {
115       my @printers;
116       while (my $data = $sth->fetchrow_hashref) {
117         push @printers,$data;
118       }
119       prmenu ($env,\@printers);
120   } else {
121       my $data=$sth->fetchrow_hashref;
122       $env->{'queue'}=$data->{'printqueue'};
123       $env->{'printtype'}=$data->{'printtype'};
124   }
125   $sth->finish;
126 }
127
128 # FIXME - This is not the same as &C4::Circulation::pastitems, though
129 # the two appear to share some code.
130 # FIXME - This function is called in &C4::Circulation::Issues::Issue
131 # and in telnet/borrwraper.pl, both of which look obsolete. Presumably
132 # this means this function is obsolete as well.
133 # Otherwise, it needs a POD.
134 sub pastitems{
135   #Get list of all items borrower has currently on issue
136   my ($env,$bornum,$dbh)=@_;
137   my $query1 = "select * from issues  where (borrowernumber=$bornum)
138     and (returndate is null) order by date_due";
139   my $sth=$dbh->prepare($query1);
140   $sth->execute;
141   my $i=0;
142   my @items;
143   my @items2;
144   while (my $data1=$sth->fetchrow_hashref) {
145     my $data = itemnodata($env,$dbh,$data1->{'itemnumber'}); #C4::Search
146     my @date = split("-",$data1->{'date_due'});
147     my $odate = (@date[2]+0)."-".(@date[1]+0)."-".@date[0];
148     my $line = C4::Circulation::Issues::formatitem($env,$data,$odate,"");
149     $items[$i]=$line;
150     $i++;
151   }
152   $sth->finish();
153   return(\@items,\@items2);
154 }
155
156 =item checkoverdues
157
158   $num_items = &checkoverdues($env, $borrowernumber, $dbh);
159
160 Returns the number of overdue books a patron has.
161
162 C<$env> is ignored.
163
164 C<$borrowernumber> is the patron's borrower number.
165
166 C<$dbh> is a DBI handle to the Koha database.
167
168 =cut
169 #'
170 sub checkoverdues{
171   #checks whether a borrower has overdue items
172   # FIXME - Use C4::Context->dbh instead of getting $dbh as an argument
173   my ($env,$bornum,$dbh)=@_;
174   # FIXME - This is what POSIX::strftime is for.
175   my @datearr = localtime;
176   my $today = ($datearr[5] + 1900)."-".($datearr[4]+1)."-".$datearr[3];
177   # FIXME - MySQL can figure out what today is, so there's no need
178   # to calculate that separately. Just use
179   #     ... date_due < curdate()
180   my $query = "Select count(*) from issues where borrowernumber=$bornum and
181         returndate is NULL and date_due < '$today'";
182   my $sth=$dbh->prepare($query);
183   $sth->execute;
184   my $data = $sth->fetchrow_hashref;
185   $sth->finish;
186   return $data->{'count(*)'};
187 }
188
189 # FIXME - This is quite similar to &C4::Circulation::previousissue
190 # FIXME - Never used. Obsolete, presumably.
191 # Otherwise, it needs a POD.
192 sub previousissue {
193   my ($env,$itemnum,$dbh,$bornum)=@_;
194   my $sth=$dbh->prepare("Select
195      firstname,surname,issues.borrowernumber,cardnumber,returndate
196      from issues,borrowers where
197      issues.itemnumber='$itemnum' and
198      issues.borrowernumber=borrowers.borrowernumber
199      and issues.returndate is NULL");
200   $sth->execute;
201   my $borrower=$sth->fetchrow_hashref;
202   my $canissue = "Y";
203   $sth->finish;
204   my $newdate;
205   if ($borrower->{'borrowernumber'} ne ''){
206     if ($bornum eq $borrower->{'borrowernumber'}){
207       # no need to issue
208       my ($renewstatus) = C4::Circulation::Renewals::renewstatus($env,$dbh,$bornum,$itemnum);
209       my ($resbor,$resrec) = checkreserve($env,$dbh,$itemnum);
210       if ($renewstatus == "0") {
211         info_msg($env,"</S>Issued to this borrower - No renewals<!S>");
212         $canissue = "N";
213       } elsif ($resbor ne "") {
214         my $resp = C4::InterfaceCDK::msg_ny($env,"Book is issued to this borrower",
215           "and is reserved - Renew?");
216         if ($resp eq "Y") {
217           $newdate = C4::Circulation::Renewals::renewbook($env,$dbh,$bornum,$itemnum);
218           $canissue = "R";
219         } else {
220           $canissue = "N";
221         }
222       } else {
223         my $resp = C4::InterfaceCDK::msg_yn($env,"Book is issued to this borrower", "Renew?");
224         if ($resp eq "Y") {
225           $newdate = C4::Circulation::Renewals::renewbook($env,$dbh,$bornum,$itemnum);
226           $canissue = "R";
227         } else {
228           $canissue = "N";
229         }
230       }
231     } else {
232       my $text="Issued to $borrower->{'firstname'} $borrower->{'surname'} ($borrower->{'cardnumber'})";
233       my $resp = C4::InterfaceCDK::msg_yn($env,$text,"Mark as returned?");
234       if ( $resp eq "Y") {
235         &returnrecord($env,$dbh,$borrower->{'borrowernumber'},$itemnum);
236       } else {
237         $canissue = "N";
238       }
239     }
240   }
241   return($borrower->{'borrowernumber'},$canissue,$newdate);
242 }
243
244 =item checkreserve
245
246   ($borrowernumber, $reserve) = &checkreserve($env, $dbh, $itemnumber);
247
248 C<$env> is ignored.
249
250 C<$dbh> is a DBI handle to the Koha database.
251
252 C<$itemnumber> is the number of the item to find.
253
254 C<&checkreserve> returns two values:
255
256 C<$borrowernumber> is the borrower number of the patron for whom the
257 book is reserved, or the empty string. I can't tell when it returns a
258 number and when it returns a string, nor what it means.
259
260 C<$reserve> describes the reserved item. It is a reference-to-hash
261 whose keys are the fields of the reserves and items tables of the Koha
262 database.
263
264 =cut
265 #'
266 sub checkreserve{
267   # Check for reserves for biblio
268   # FIXME - Use C4::Context->dbh to get $dbh, instead of passing it
269   # on the argument list.
270   my ($env,$dbh,$itemnum)=@_;
271   my $resbor = "";
272   # Find this item in the reserves.
273   # Apparently reserves.found=='W' means "Waiting".
274   # FIXME - Is it necessary to get every field from both tables?
275   my $query = "select * from reserves,items
276     where (items.itemnumber = '$itemnum')
277     and (reserves.cancellationdate is NULL)
278     and (items.biblionumber = reserves.biblionumber)
279     and ((reserves.found = 'W')
280     or (reserves.found is null))
281     order by priority";
282   my $sth = $dbh->prepare($query);
283   $sth->execute();
284   my $resrec;
285   if (my $data=$sth->fetchrow_hashref) {
286     $resrec=$data;
287     my $const = $data->{'constrainttype'};
288     if ($const eq "a") {                # FIXME - What does 'a' mean?
289       $resbor = $data->{'borrowernumber'};
290     } else {
291       my $found = 0;
292       my $cquery = "select * from reserveconstraints,items
293          where (borrowernumber='$data->{'borrowernumber'}')
294          and reservedate='$data->{'reservedate'}'
295          and reserveconstraints.biblionumber='$data->{'biblionumber'}'
296          and (items.itemnumber=$itemnum and
297          items.biblioitemnumber = reserveconstraints.biblioitemnumber)";
298       my $csth = $dbh->prepare($cquery);
299       $csth->execute;
300       if (my $cdata=$csth->fetchrow_hashref) {$found = 1;}
301       if ($const eq 'o') {              # FIXME - What does 'o' mean?
302         if ($found eq 1) {$resbor = $data->{'borrowernumber'};}
303       } else {
304         if ($found eq 0) {$resbor = $data->{'borrowernumber'};}
305       }
306       $csth->finish();
307     }
308   }
309   $sth->finish;
310   return ($resbor,$resrec);
311 }
312
313 # FIXME - This is only used in C4::Circulation::Borrower, which
314 # appears to be obsolete. Presumably this function is obsolete as
315 # well. Otherwise, it needs a POD.
316 sub checkwaiting{
317   # check for reserves waiting
318   my ($env,$dbh,$bornum)=@_;
319   my @itemswaiting;
320   my $query = "select * from reserves
321     where (borrowernumber = '$bornum')
322     and (reserves.found='W') and cancellationdate is NULL";
323   my $sth = $dbh->prepare($query);
324   $sth->execute();
325   my $cnt=0;
326   if (my $data=$sth->fetchrow_hashref) {
327     @itemswaiting[$cnt] =$data;
328     $cnt ++
329   }
330   $sth->finish;
331   return ($cnt,\@itemswaiting);
332 }
333
334 # FIXME - This is identical to &C4::Circulation::scanbook
335 # FIXME - This function is only used in tkperl/tkcirc, if anywhere
336 # (it's hard to tell). Presumably it's obsolete.
337 # Otherwise, it needs a POD.
338 sub scanbook {
339   my ($env,$interface)=@_;
340   #scan barcode
341   my ($number,$reason)=dialog("Book Barcode:");
342   $number=uc $number;
343   return ($number,$reason);
344 }
345
346 # FIXME - This is very similar to &C4::Circulation::scanborrower
347 # FIXME - This is only used in C4::Circulation::Borrower, which
348 # appears to be obsolete. Presumably this function is obsolete as
349 # well.
350 # Otherwise, it needs a POD.
351 sub scanborrower {
352   my ($env,$interface)=@_;
353   #scan barcode
354   my ($number,$reason,$book)=C4::InterfaceCDK::borrower_dialog($env); #C4::InterfaceCDK
355   $number= $number;             # FIXME - WTF?
356   $book=uc $book;
357   return ($number,$reason,$book);
358 }
359
360 1;
361 __END__
362
363 =back
364
365 =head1 AUTHOR
366
367 Koha Developement team <info@koha.org>
368
369 =cut