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