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