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