Removed unnecessary "use" line.
[koha.git] / C4 / Circulation / Main.pm
1 package C4::Circulation::Main; #assumes C4/Circulation/Main
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::Context;
27 use C4::Circulation::Issues;
28         # FIXME - C4::Circulation::Main and C4::Circulation::Issues
29         # use each other, so functions get redefined.
30 use C4::Circulation::Returns;
31         # FIXME - C4::Circulation::Main and C4::Circulation::Returns
32         # use each other, so functions get redefined.
33 use C4::Circulation::Renewals;
34         # FIXME - C4::Circulation::Main and C4::Circulation::Renewals
35         # use each other, so functions get redefined.
36 use C4::Circulation::Borrower;
37         # FIXME - C4::Circulation::Main and C4::Circulation::Borrower
38         # use each other, so functions get redefined.
39 use C4::Reserves;
40 use C4::Search;
41 use C4::InterfaceCDK;
42 use C4::Security;
43
44 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
45   
46 # set the version for version checking
47 $VERSION = 0.01;
48     
49 @ISA = qw(Exporter);
50 @EXPORT = qw(&pastitems &checkoverdues &previousissue 
51 &checkreserve &checkwaiting &scanbook &scanborrower &getbranch &getprinter);
52 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
53                   
54 # your exported package globals go here,
55 # as well as any optionally exported functions
56
57 @EXPORT_OK   = qw($Var1 %Hashit);
58
59
60 # non-exported package globals go here
61 use vars qw(@more $stuff);
62         
63 # initalize package globals, first exported ones
64
65 my $Var1   = '';
66 my %Hashit = ();
67                     
68 # then the others (which are still accessible as $Some::Module::stuff)
69 my $stuff  = '';
70 my @more   = ();
71         
72 # all file-scoped lexicals must be created before
73 # the functions below that use them.
74                 
75 # file-private lexicals go here
76 my $priv_var    = '';
77 my %secret_hash = ();
78                             
79 # here's a file-private function as a closure,
80 # callable as &$priv_func;  it cannot be prototyped.
81 my $priv_func = sub {
82   # stuff goes here.
83 };
84                                                     
85 # make all your functions, whether exported or not;
86
87 sub getbranch {
88   my ($env) = @_;
89   my $dbh = C4::Context->dbh;
90   my $query = "select * from branches order by branchcode";
91   my $sth = $dbh->prepare($query);
92   $sth->execute;
93   if ($sth->rows>1) {
94       my @branches;
95       while (my $data = $sth->fetchrow_hashref) {
96         push @branches,$data;
97       }
98       brmenu ($env,\@branches);
99   } else {
100       my $data = $sth->fetchrow_hashref;
101       $env->{'branchcode'}=$data->{'branchcode'};
102   }
103   my $query = "select * from branches  
104     where branchcode = '$env->{'branchcode'}'";
105   $sth = $dbh->prepare($query);
106   $sth->execute;
107   my $data = $sth->fetchrow_hashref;
108   $env->{'brdata'} = $data;
109   $env->{'branchname'} = $data->{'branchname'};
110   $sth->finish;
111 }
112
113 sub getprinter {
114   my ($env) = @_;
115   my $dbh = C4::Context->dbh;
116   my $query = "select * from printers order by printername";
117   my $sth = $dbh->prepare($query);
118   $sth->execute;
119   if ($sth->rows>1) {
120       my @printers;
121       while (my $data = $sth->fetchrow_hashref) {
122         push @printers,$data;
123       }
124       prmenu ($env,\@printers);
125   } else {
126       my $data=$sth->fetchrow_hashref;
127       $env->{'queue'}=$data->{'printqueue'};
128       $env->{'printtype'}=$data->{'printtype'};
129   }
130   $sth->finish;
131 }
132                       
133 # FIXME - This is not the same as &C4::Circulation::pastitems, though
134 # the two appear to share some code.
135 sub pastitems{
136   #Get list of all items borrower has currently on issue
137   my ($env,$bornum,$dbh)=@_;
138   my $query1 = "select * from issues  where (borrowernumber=$bornum)
139     and (returndate is null) order by date_due";
140   my $sth=$dbh->prepare($query1);
141   $sth->execute;
142   my $i=0;
143   my @items;
144   my @items2;
145   while (my $data1=$sth->fetchrow_hashref) {
146     my $data = itemnodata($env,$dbh,$data1->{'itemnumber'}); #C4::Search
147     my @date = split("-",$data1->{'date_due'});
148     my $odate = (@date[2]+0)."-".(@date[1]+0)."-".@date[0];
149     my $line = C4::Circulation::Issues::formatitem($env,$data,$odate,"");
150     $items[$i]=$line;
151     $i++;
152   }
153   $sth->finish();
154   return(\@items,\@items2);
155 }
156
157 sub checkoverdues{
158   #checks whether a borrower has overdue items
159   my ($env,$bornum,$dbh)=@_;
160   my @datearr = localtime;
161   my $today = ($datearr[5] + 1900)."-".($datearr[4]+1)."-".$datearr[3];
162   my $query = "Select count(*) from issues where borrowernumber=$bornum and
163         returndate is NULL and date_due < '$today'";
164   my $sth=$dbh->prepare($query);
165   $sth->execute;
166   my $data = $sth->fetchrow_hashref;
167   $sth->finish;
168   return $data->{'count(*)'};
169 }
170
171 # FIXME - This is quite similar to &C4::Circulation::previousissue
172 sub previousissue {
173   my ($env,$itemnum,$dbh,$bornum)=@_;
174   my $sth=$dbh->prepare("Select 
175      firstname,surname,issues.borrowernumber,cardnumber,returndate
176      from issues,borrowers where 
177      issues.itemnumber='$itemnum' and
178      issues.borrowernumber=borrowers.borrowernumber 
179      and issues.returndate is NULL");
180   $sth->execute;
181   my $borrower=$sth->fetchrow_hashref;
182   my $canissue = "Y";
183   $sth->finish;
184   my $newdate;
185   if ($borrower->{'borrowernumber'} ne ''){
186     if ($bornum eq $borrower->{'borrowernumber'}){
187       # no need to issue
188       my ($renewstatus) = C4::Circulation::Renewals::renewstatus($env,$dbh,$bornum,$itemnum);
189       my ($resbor,$resrec) = checkreserve($env,$dbh,$itemnum);
190       if ($renewstatus == "0") {
191         info_msg($env,"</S>Issued to this borrower - No renewals<!S>");
192         $canissue = "N";
193       } elsif ($resbor ne "") {
194         my $resp = C4::InterfaceCDK::msg_ny($env,"Book is issued to this borrower",
195           "and is reserved - Renew?");
196         if ($resp eq "Y") {
197           $newdate = C4::Circulation::Renewals::renewbook($env,$dbh,$bornum,$itemnum);
198           $canissue = "R";
199         } else {
200           $canissue = "N";
201         }
202       } else {
203         my $resp = C4::InterfaceCDK::msg_yn($env,"Book is issued to this borrower", "Renew?");
204         if ($resp eq "Y") {
205           $newdate = C4::Circulation::Renewals::renewbook($env,$dbh,$bornum,$itemnum);
206           $canissue = "R";
207         } else {
208           $canissue = "N";
209         }
210       }    
211     } else {
212       my $text="Issued to $borrower->{'firstname'} $borrower->{'surname'} ($borrower->{'cardnumber'})";    
213       my $resp = C4::InterfaceCDK::msg_yn($env,$text,"Mark as returned?");
214       if ( $resp eq "Y") {
215         &returnrecord($env,$dbh,$borrower->{'borrowernumber'},$itemnum);
216       } else {
217         $canissue = "N";
218       }
219     }
220   } 
221   return($borrower->{'borrowernumber'},$canissue,$newdate);
222 }
223
224
225 sub checkreserve{
226   # Check for reserves for biblio 
227   my ($env,$dbh,$itemnum)=@_;
228   my $resbor = "";
229   my $query = "select * from reserves,items 
230     where (items.itemnumber = '$itemnum')
231     and (reserves.cancellationdate is NULL)
232     and (items.biblionumber = reserves.biblionumber)
233     and ((reserves.found = 'W')
234     or (reserves.found is null)) 
235     order by priority";
236   my $sth = $dbh->prepare($query);
237   $sth->execute();
238   my $resrec;
239   if (my $data=$sth->fetchrow_hashref) {
240     $resrec=$data;
241     my $const = $data->{'constrainttype'};
242     if ($const eq "a") {
243       $resbor = $data->{'borrowernumber'}; 
244     } else {
245       my $found = 0;
246       my $cquery = "select * from reserveconstraints,items 
247          where (borrowernumber='$data->{'borrowernumber'}') 
248          and reservedate='$data->{'reservedate'}'
249          and reserveconstraints.biblionumber='$data->{'biblionumber'}'
250          and (items.itemnumber=$itemnum and 
251          items.biblioitemnumber = reserveconstraints.biblioitemnumber)";
252       my $csth = $dbh->prepare($cquery);
253       $csth->execute;
254       if (my $cdata=$csth->fetchrow_hashref) {$found = 1;}
255       if ($const eq 'o') {
256         if ($found eq 1) {$resbor = $data->{'borrowernumber'};}
257       } else {
258         if ($found eq 0) {$resbor = $data->{'borrowernumber'};} 
259       }
260       $csth->finish();
261     }     
262   }
263   $sth->finish;
264   return ($resbor,$resrec);
265 }
266
267 sub checkwaiting{
268   # check for reserves waiting
269   my ($env,$dbh,$bornum)=@_;
270   my @itemswaiting;
271   my $query = "select * from reserves
272     where (borrowernumber = '$bornum')
273     and (reserves.found='W') and cancellationdate is NULL";
274   my $sth = $dbh->prepare($query);
275   $sth->execute();
276   my $cnt=0;
277   if (my $data=$sth->fetchrow_hashref) {
278     @itemswaiting[$cnt] =$data;
279     $cnt ++
280   }
281   $sth->finish;
282   return ($cnt,\@itemswaiting);
283 }
284
285 # FIXME - This is identical to &C4::Circulation::scanbook
286 sub scanbook {
287   my ($env,$interface)=@_;
288   #scan barcode
289   my ($number,$reason)=dialog("Book Barcode:");
290   $number=uc $number;
291   return ($number,$reason);
292 }
293
294 # FIXME - This is very similar to &C4::Circulation::scanborrower
295 sub scanborrower {
296   my ($env,$interface)=@_;
297   #scan barcode
298   my ($number,$reason,$book)=C4::InterfaceCDK::borrower_dialog($env); #C4::InterfaceCDK
299   $number= $number;
300   $book=uc $book;
301   return ($number,$reason,$book);
302 }
303
304
305 END { }       # module clean-up code here (global destructor)