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