Merged with arensb-context branch: use C4::Context->dbh instead of
[koha.git] / C4 / Circulation / Borrower.pm
1 package C4::Circulation::Borrower; #assumes C4/Circulation/Borrower
2
3 #package to deal with Issues
4 #written 3/11/99 by chris@katipo.co.nz
5
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 use strict;
25 require Exporter;
26 use DBI;
27 use C4::Context;
28 use C4::Accounts;
29 use C4::InterfaceCDK;
30 use C4::Interface::FlagsCDK;
31 use C4::Circulation::Main;
32         # FIXME - C4::Circulation::Main and C4::Circulation::Borrower
33         # use each other, so functions get redefined.
34 use C4::Circulation::Issues;
35         # FIXME - C4::Circulation::Issues and C4::Circulation::Borrower
36         # use each other, so functions get redefined.
37 use C4::Circulation::Renewals;
38 use C4::Scan;
39 use C4::Search;
40 use C4::Stats;
41 use C4::Format;
42 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
43   
44 # set the version for version checking
45 $VERSION = 0.01;
46     
47 @ISA = qw(Exporter);
48 @EXPORT = qw(&findborrower &Borenq &findoneborrower &NewBorrowerNumber
49 &findguarantees);
50 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
51                   
52 # your exported package globals go here,
53 # as well as any optionally exported functions
54
55 @EXPORT_OK   = qw($Var1 %Hashit);
56
57
58 # non-exported package globals go here
59 use vars qw(@more $stuff);
60         
61 # initalize package globals, first exported ones
62
63 my $Var1   = '';
64 my %Hashit = ();
65                     
66 # then the others (which are still accessible as $Some::Module::stuff)
67 my $stuff  = '';
68 my @more   = ();
69         
70 # all file-scoped lexicals must be created before
71 # the functions below that use them.
72                 
73 # file-private lexicals go here
74 my $priv_var    = '';
75 my %secret_hash = ();
76                             
77 # here's a file-private function as a closure,
78 # callable as &$priv_func;  it cannot be prototyped.
79 my $priv_func = sub {
80   # stuff goes here.
81 };
82                                                     
83 # make all your functions, whether exported or not;
84
85
86 sub findborrower  {
87   my ($env,$dbh) = @_;
88   C4::InterfaceCDK::helptext('');
89   C4::InterfaceCDK::clearscreen();
90   my $bornum = "";
91   my $sth = "";
92   my $borcode = "";
93   my $borrower;
94   my $reason = "";
95   my $book;
96   while (($bornum eq '') && ($reason eq "")) {
97     #get borrowerbarcode from scanner
98     my $title = C4::InterfaceCDK::titlepanel($env,$env->{'sysarea'},"Borrower Entry");
99     if ($env->{'newborrower'} eq "") {
100       ($borcode,$reason,$book)=&C4::Circulation::Main::scanborrower($env); 
101     } else { 
102       $borcode = $env->{'newborrower'};
103       $reason = "";
104       $book = "";
105       $env->{'newborrower'}= "";
106     }  
107     #C4::Circulation::Main
108     if ($reason eq "") {
109       if ($borcode ne '') {
110         ($bornum,$borrower) = findoneborrower($env,$dbh,$borcode);
111         $env->{'IssuesAllowed'} = 1;
112       } elsif ($book ne "") {
113         my $query = "select * from issues,items where (barcode = '$book') 
114           and (items.itemnumber = issues.itemnumber) 
115           and (issues.returndate is null)";
116         my $iss_sth=$dbh->prepare($query);
117         $iss_sth->execute;
118         if (my $issdata  = $iss_sth->fetchrow_hashref) {
119            $bornum=$issdata->{'borrowernumber'};
120            $sth = $dbh->prepare("Select * from borrowers 
121              where borrowernumber =  '$bornum'");
122            $sth->execute;
123            $borrower=$sth->fetchrow_hashref;
124            $sth->finish;  
125          } else {
126            error_msg($env,"Item $book not found");
127          } 
128          $iss_sth->finish;
129       }
130     } 
131   } 
132   my ($issuesallowed,$owing);
133   if ($reason eq "") {
134     $env->{'bornum'} = $bornum;
135     $env->{'bcard'} = $borrower->{'cardnumber'};
136     my $borrowers=join(' ',($borrower->{'title'},$borrower->{'firstname'},$borrower->{'surname'}));
137     my $odues;
138     ($issuesallowed,$odues,$owing) = &checktraps($env,$dbh,$bornum,$borrower);
139 #    error_msg ($env,"bcard =  $env->{'bcard'}");
140   }
141   #debug_msg ($env,"2 =  $env->{'IssuesAllowed'}");
142   return ($bornum, $issuesallowed,$borrower,$reason,$owing);
143 };
144
145
146 sub findoneborrower {
147   #  output(1,1,$borcode);
148   my ($env,$dbh,$borcode)=@_;
149   my $bornum;
150   my $borrower;
151   my $ucborcode = uc $borcode;
152   my $lcborcode = lc $borcode;
153   my $sth=$dbh->prepare("Select * from borrowers where cardnumber=\"$ucborcode\"");
154   $sth->execute;
155   if ($borrower=$sth->fetchrow_hashref) {
156     $bornum=$borrower->{'borrowernumber'};
157     $sth->finish;
158   } else {
159     $sth->finish;
160     # my $borquery = "Select * from borrowers
161     # where surname ~* '$borcode' order by surname";
162               
163     my $borquery = "Select * from borrowers 
164       where lower(surname) like \"$lcborcode%\" order by surname,firstname";
165     my $sthb =$dbh->prepare($borquery);
166     $sthb->execute;
167     my $cntbor = 0;
168     my @borrows;
169     my @bornums;
170     while ($borrower= $sthb->fetchrow_hashref) {
171       my $line = $borrower->{'cardnumber'}.' '.$borrower->{'categorycode'}.' '.$borrower->{'surname'}.
172         ', '.$borrower->{'othernames'};
173       $borrows[$cntbor] = fmtstr($env,$line,"L50");
174       $bornums[$cntbor] =$borrower->{'borrowernumber'};
175       $cntbor++;
176     }
177     if ($cntbor == 1)  {
178       $bornum = $bornums[0];       
179       my $query = "select * from borrowers where borrowernumber = '$bornum'";      
180       $sth = $dbh->prepare($query);
181       $sth->execute;
182       $borrower =$sth->fetchrow_hashref;
183       $sth->finish;                                              
184     } elsif ($cntbor > 0) {
185       my ($cardnum) = C4::InterfaceCDK::selborrower($env,$dbh,\@borrows,\@bornums);
186       my $query = "select * from borrowers where cardnumber = '$cardnum'";   
187       $sth = $dbh->prepare($query);                          
188       $sth->execute;                          
189       $borrower =$sth->fetchrow_hashref;
190       $sth->finish;
191       $bornum=$borrower->{'borrowernumber'};
192       #C4::InterfaceCDK::clearscreen();
193       if ($bornum eq '') {
194         error_msg($env,"Borrower not found");
195       }
196     }  
197   }
198   return ($bornum,$borrower); 
199 }
200 sub checktraps {
201   my ($env,$dbh,$bornum,$borrower) = @_;
202   my $issuesallowed = "1";
203   #my @traps_set;
204   #check amountowing
205   my $traps_done; 
206   my $odues;
207   my $amount;
208   while ($traps_done ne "DONE") {
209     my @traps_set;
210     $amount=C4::Accounts::checkaccount($env,$bornum,$dbh);    #from C4::Accounts
211     if ($amount > 0) { push (@traps_set,"CHARGES");}  
212     if ($borrower->{'gonenoaddress'} == 1){ push (@traps_set,"GNA");}
213     #check if member has a card reported as lost
214     if ($borrower->{'lost'} ==1){push (@traps_set,"LOST");}
215     #check the notes field if notes exist display them
216     if ($borrower->{'borrowernotes'} ne ''){ push (@traps_set,"NOTES");}
217     #check if borrower has overdue items
218     #call overdue checker
219     my $odues = &C4::Circulation::Main::checkoverdues($env,$bornum,$dbh);
220     if ($odues > 0) {push (@traps_set,"ODUES");}  
221     #check if borrower has any items waiting
222     my ($nowaiting,$itemswaiting) = &C4::Circulation::Main::checkwaiting($env,$dbh,$bornum);
223     if ($nowaiting > 0) { push (@traps_set,"WAITING"); } 
224     # FIXME - This should be $traps_set[0], right?
225     if (@traps_set[0] ne "" ) {
226       ($issuesallowed,$traps_done,$amount,$odues) = 
227          process_traps($env,$dbh,$bornum,$borrower,
228          $amount,$odues,\@traps_set,$itemswaiting);
229     } else {
230       $traps_done = "DONE";
231     }   
232   }
233   return ($issuesallowed, $odues,$amount);
234 }
235
236 sub process_traps {
237   my ($env,$dbh,$bornum,$borrower,$amount,$odues,$traps_set,$waiting) = @_;
238   my $issuesallowed = 1;
239   my $x = 0;
240   my %traps;
241   while (@$traps_set[$x] ne "") {
242     $traps{@$traps_set[$x]} = 1; 
243     $x++;
244   }
245   my $traps_done;
246   my $trapact;
247   my $issues;
248   while ($trapact ne "NONE") {
249     $trapact = &trapscreen($env,$bornum,$borrower,$amount,$traps_set);
250     if ($trapact eq "CHARGES") {
251       C4::Accounts::reconcileaccount($env,$dbh,$bornum,$amount,$borrower,$odues);
252       ($odues,$issues,$amount)=borrdata2($env,$bornum);          
253       if ($amount <= 0) {
254         $traps{'CHARGES'} = 0;
255         my @newtraps;
256         $x =0;
257         while ($traps_set->[$x] ne "") {
258           if ($traps_set->[$x] ne "CHARGES") {
259             push @newtraps,$traps_set->[$x];
260           }
261           $x++;
262         }
263         $traps_set = \@newtraps;
264       }
265     } elsif ($trapact eq "WAITING") {
266       reserveslist($env,$borrower,$amount,$odues,$waiting);
267     } elsif ($trapact eq "ODUES") {
268       C4::Circulation::Renewals::bulkrenew($env,$dbh,$bornum,$amount,$borrower,$odues);
269       ($odues,$issues,$amount)=borrdata2($env,$bornum);
270       if ($odues == 0) {
271         $traps{'ODUES'} = 0;
272         my @newtraps;
273         $x =0;
274         while ($traps_set->[$x] ne "") {
275           if ($traps_set->[$x] ne "ODUES") {
276             push @newtraps,$traps_set->[$x];
277           }
278           $x++;
279         }
280         $traps_set = \@newtraps;
281       }
282     } elsif  ($trapact eq "NOTES") {
283       my $notes = trapsnotes($env,$bornum,$borrower,$amount);
284       if ($notes ne $borrower->{'borrowernotes'}) { 
285         my $query = "update borrowers set borrowernotes = '$notes' 
286            where borrowernumber = $bornum";
287         my $sth = $dbh->prepare($query);
288         $sth->execute();
289         $sth->finish();
290         $borrower->{'borrowernotes'} = $notes;
291       }
292       if ($notes eq "") {
293         $traps{'NOTES'} = 0;
294         my @newtraps;
295         $x =0;
296         while ($traps_set->[$x] ne "") {
297           if ($traps_set->[$x] ne "NOTES") {
298             push @newtraps,$traps_set->[$x];
299           }
300           $x++;
301         }                 
302         $traps_set = \@newtraps;                                                     
303       }
304     }
305     my $notr = @$traps_set;
306     if ($notr == 0) {
307       $trapact = "NONE";
308     }
309     $traps_done = "DONE";
310   }
311   if ($traps{'GNA'} eq 1 ) {
312     $issuesallowed=0;
313     $env->{'IssuesAllowed'} = 0;
314   }
315   if ($traps{'CHARGES'} eq 1) {
316     if ($amount > 5) {
317       $env->{'IssuesAllowed'} = 0;
318       $issuesallowed=0;
319     }
320   }
321   return ($issuesallowed,$traps_done,$amount,$odues);
322 } # end of process_traps
323
324 sub Borenq {
325   my ($env)=@_;
326   my $dbh = C4::Context->dbh;
327   #get borrower guff
328   my $bornum;
329   my $issuesallowed;
330   my $borrower;
331   my $reason;
332   $env->{'sysarea'} = "Enquiries";
333   while ($reason eq "") {
334     $env->{'sysarea'} = "Enquiries";
335     ($bornum,$issuesallowed,$borrower,$reason) = &findborrower($env,$dbh);
336     if ($reason eq "") {
337       my ($data,$reason)=&borrowerwindow($env,$borrower);
338       if ($reason eq 'Modify'){
339         modifyuser($env,$borrower);
340         $reason = "";
341       } elsif ($reason eq 'New'){
342         $reason = "";
343        }
344     }
345   }
346   return $reason;
347 }  
348
349 sub modifyuser {
350   my ($env,$borrower) = @_;
351   debug_msg($env,"Please use intranet");
352   #return;
353 }
354
355 sub reserveslist {
356   my ($env,$borrower,$amount,$odues,$waiting) = @_;
357   my $dbh = C4::Context->dbh;
358   my @items;
359   my $x=0;
360   my $query="Select * from reserves where
361   borrowernumber='$borrower->{'borrowernumber'}' and found='W' and
362   cancellationdate is null order by timestamp";
363   my $sth=$dbh->prepare($query);
364   $sth->execute;
365   while (my $data=$sth->fetchrow_hashref){
366     my $itemdata = itemnodata($env,$dbh,$data->{'itemnumber'});
367     if ($itemdata){
368       push @items,$itemdata;
369     }
370   }
371   $sth->finish;
372   reservesdisplay($env,$borrower,$amount,$odues,\@items);
373 }
374   
375 sub NewBorrowerNumber {
376   my $dbh = C4::Context->dbh;
377   my $sth=$dbh->prepare("Select max(borrowernumber) from borrowers");
378   $sth->execute;
379   my $data=$sth->fetchrow_hashref;
380   $sth->finish;
381   $data->{'max(borrowernumber)'}++;
382   return($data->{'max(borrowernumber)'});
383 }
384
385 sub findguarantees{
386   my ($bornum)=@_;
387   my $dbh = C4::Context->dbh;
388   my $query="select cardnumber,borrowernumber from borrowers where 
389   guarantor='$bornum'";
390   my $sth=$dbh->prepare($query);
391   $sth->execute;
392   my @dat;
393   my $i=0;
394   while (my $data=$sth->fetchrow_hashref){
395     $dat[$i]=$data;
396     $i++;
397   }
398   $sth->finish;
399   return($i,\@dat);
400 }
401 END { }       # module clean-up code here (global destructor)