1 package C4::Circulation::Borrower;
5 #package to deal with Issues
6 #written 3/11/99 by chris@katipo.co.nz
9 # Copyright 2000-2002 Katipo Communications
11 # This file is part of Koha.
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
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.
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
26 # FIXME - This module is never used. Obsolete?
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;
46 use vars qw($VERSION @ISA @EXPORT);
48 # set the version for version checking
52 @EXPORT = qw(&findborrower &Borenq &findoneborrower &NewBorrowerNumber
57 C4::InterfaceCDK::helptext(''); # FIXME - This looks useless
58 C4::InterfaceCDK::clearscreen();
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);
71 $borcode = $env->{'newborrower'};
74 $env->{'newborrower'}= "";
76 #C4::Circulation::Main
79 ($bornum,$borrower) = findoneborrower($env,$dbh,$borcode);
80 $env->{'IssuesAllowed'} = 1;
81 } elsif ($book ne "") {
82 my $query = "select * from issues,items where (barcode = '$book')
83 and (items.itemnumber = issues.itemnumber)
84 and (issues.returndate is null)";
85 my $iss_sth=$dbh->prepare($query);
87 if (my $issdata = $iss_sth->fetchrow_hashref) {
88 $bornum=$issdata->{'borrowernumber'};
89 $sth = $dbh->prepare("Select * from borrowers
90 where borrowernumber = '$bornum'");
92 $borrower=$sth->fetchrow_hashref;
95 error_msg($env,"Item $book not found");
101 my ($issuesallowed,$owing);
103 $env->{'bornum'} = $bornum;
104 $env->{'bcard'} = $borrower->{'cardnumber'};
105 my $borrowers=join(' ',($borrower->{'title'},$borrower->{'firstname'},$borrower->{'surname'}));
107 ($issuesallowed,$odues,$owing) = &checktraps($env,$dbh,$bornum,$borrower);
108 # error_msg ($env,"bcard = $env->{'bcard'}");
110 #debug_msg ($env,"2 = $env->{'IssuesAllowed'}");
111 return ($bornum, $issuesallowed,$borrower,$reason,$owing);
115 sub findoneborrower {
116 # output(1,1,$borcode);
117 my ($env,$dbh,$borcode)=@_;
120 my $ucborcode = uc $borcode;
121 my $lcborcode = lc $borcode;
122 my $sth=$dbh->prepare("Select * from borrowers where cardnumber=\"$ucborcode\"");
124 if ($borrower=$sth->fetchrow_hashref) {
125 $bornum=$borrower->{'borrowernumber'};
129 # my $borquery = "Select * from borrowers
130 # where surname ~* '$borcode' order by surname";
132 my $borquery = "Select * from borrowers
133 where lower(surname) like \"$lcborcode%\" order by surname,firstname";
134 my $sthb =$dbh->prepare($borquery);
139 while ($borrower= $sthb->fetchrow_hashref) {
140 my $line = $borrower->{'cardnumber'}.' '.$borrower->{'categorycode'}.' '.$borrower->{'surname'}.
141 ', '.$borrower->{'othernames'};
142 $borrows[$cntbor] = fmtstr($env,$line,"L50");
143 $bornums[$cntbor] =$borrower->{'borrowernumber'};
147 $bornum = $bornums[0];
148 my $query = "select * from borrowers where borrowernumber = '$bornum'";
149 $sth = $dbh->prepare($query);
151 $borrower =$sth->fetchrow_hashref;
153 } elsif ($cntbor > 0) {
154 my ($cardnum) = C4::InterfaceCDK::selborrower($env,$dbh,\@borrows,\@bornums);
155 my $query = "select * from borrowers where cardnumber = '$cardnum'";
156 $sth = $dbh->prepare($query);
158 $borrower =$sth->fetchrow_hashref;
160 $bornum=$borrower->{'borrowernumber'};
161 #C4::InterfaceCDK::clearscreen();
163 error_msg($env,"Borrower not found");
167 return ($bornum,$borrower);
170 my ($env,$dbh,$bornum,$borrower) = @_;
171 my $issuesallowed = "1";
177 while ($traps_done ne "DONE") {
179 $amount=C4::Accounts::checkaccount($env,$bornum,$dbh); #from C4::Accounts
180 if ($amount > 0) { push (@traps_set,"CHARGES");}
181 if ($borrower->{'gonenoaddress'} == 1){ push (@traps_set,"GNA");}
182 #check if member has a card reported as lost
183 if ($borrower->{'lost'} ==1){push (@traps_set,"LOST");}
184 #check the notes field if notes exist display them
185 if ($borrower->{'borrowernotes'} ne ''){ push (@traps_set,"NOTES");}
186 #check if borrower has overdue items
187 #call overdue checker
188 my $odues = &C4::Circulation::Main::checkoverdues($env,$bornum,$dbh);
189 if ($odues > 0) {push (@traps_set,"ODUES");}
190 #check if borrower has any items waiting
191 my ($nowaiting,$itemswaiting) = &C4::Circulation::Main::checkwaiting($env,$dbh,$bornum);
192 if ($nowaiting > 0) { push (@traps_set,"WAITING"); }
193 # FIXME - This should be $traps_set[0], right?
194 if (@traps_set[0] ne "" ) {
195 ($issuesallowed,$traps_done,$amount,$odues) =
196 process_traps($env,$dbh,$bornum,$borrower,
197 $amount,$odues,\@traps_set,$itemswaiting);
199 $traps_done = "DONE";
202 return ($issuesallowed, $odues,$amount);
206 my ($env,$dbh,$bornum,$borrower,$amount,$odues,$traps_set,$waiting) = @_;
207 my $issuesallowed = 1;
210 while (@$traps_set[$x] ne "") {
211 $traps{@$traps_set[$x]} = 1;
217 while ($trapact ne "NONE") {
218 $trapact = &trapscreen($env,$bornum,$borrower,$amount,$traps_set);
219 if ($trapact eq "CHARGES") {
220 C4::Accounts::reconcileaccount($env,$dbh,$bornum,$amount,$borrower,$odues);
221 ($odues,$issues,$amount)=borrdata2($env,$bornum);
223 $traps{'CHARGES'} = 0;
226 while ($traps_set->[$x] ne "") {
227 if ($traps_set->[$x] ne "CHARGES") {
228 push @newtraps,$traps_set->[$x];
232 $traps_set = \@newtraps;
234 } elsif ($trapact eq "WAITING") {
235 reserveslist($env,$borrower,$amount,$odues,$waiting);
236 } elsif ($trapact eq "ODUES") {
237 C4::Circulation::Renewals::bulkrenew($env,$dbh,$bornum,$amount,$borrower,$odues);
238 ($odues,$issues,$amount)=borrdata2($env,$bornum);
243 while ($traps_set->[$x] ne "") {
244 if ($traps_set->[$x] ne "ODUES") {
245 push @newtraps,$traps_set->[$x];
249 $traps_set = \@newtraps;
251 } elsif ($trapact eq "NOTES") {
252 my $notes = trapsnotes($env,$bornum,$borrower,$amount);
253 if ($notes ne $borrower->{'borrowernotes'}) {
254 my $query = "update borrowers set borrowernotes = '$notes'
255 where borrowernumber = $bornum";
256 my $sth = $dbh->prepare($query);
259 $borrower->{'borrowernotes'} = $notes;
265 while ($traps_set->[$x] ne "") {
266 if ($traps_set->[$x] ne "NOTES") {
267 push @newtraps,$traps_set->[$x];
271 $traps_set = \@newtraps;
274 my $notr = @$traps_set;
278 $traps_done = "DONE";
280 if ($traps{'GNA'} eq 1 ) {
282 $env->{'IssuesAllowed'} = 0;
284 if ($traps{'CHARGES'} eq 1) {
286 $env->{'IssuesAllowed'} = 0;
290 return ($issuesallowed,$traps_done,$amount,$odues);
291 } # end of process_traps
295 my $dbh = C4::Context->dbh;
301 $env->{'sysarea'} = "Enquiries";
302 while ($reason eq "") {
303 $env->{'sysarea'} = "Enquiries";
304 ($bornum,$issuesallowed,$borrower,$reason) = &findborrower($env,$dbh);
306 my ($data,$reason)=&borrowerwindow($env,$borrower);
307 if ($reason eq 'Modify'){
308 modifyuser($env,$borrower);
310 } elsif ($reason eq 'New'){
319 my ($env,$borrower) = @_;
320 debug_msg($env,"Please use intranet");
325 my ($env,$borrower,$amount,$odues,$waiting) = @_;
326 my $dbh = C4::Context->dbh;
329 my $query="Select * from reserves where
330 borrowernumber='$borrower->{'borrowernumber'}' and found='W' and
331 cancellationdate is null order by timestamp";
332 my $sth=$dbh->prepare($query);
334 while (my $data=$sth->fetchrow_hashref){
335 my $itemdata = itemnodata($env,$dbh,$data->{'itemnumber'});
337 push @items,$itemdata;
341 reservesdisplay($env,$borrower,$amount,$odues,\@items);
344 =item NewBorrowerNumber
346 $num = &NewBorrowerNumber();
348 Allocates a new, unused borrower number, and returns it.
352 # FIXME - This is identical to C4::Search::NewBorrowerNumber.
353 # Pick one (preferably this one) and stick with it.
355 # FIXME - Race condition: this function just says what the next unused
356 # number is, but doesn't allocate it. Hence, two clients adding
357 # patrons at the same time could get the same new borrower number and
358 # clobber each other.
359 # A better approach might be to change the database to make
360 # borrowers.borrowernumber a unique key and auto_increment. Then, to
361 # allocate a new borrower number, use "insert" to create a new record
362 # (leaving the database daemon with the job of serializing requests),
363 # and use the newly-created record.
365 sub NewBorrowerNumber {
366 my $dbh = C4::Context->dbh;
367 my $sth=$dbh->prepare("Select max(borrowernumber) from borrowers");
369 my $data=$sth->fetchrow_hashref;
371 $data->{'max(borrowernumber)'}++;
372 return($data->{'max(borrowernumber)'});
377 my $dbh = C4::Context->dbh;
378 my $query="select cardnumber,borrowernumber from borrowers where
379 guarantor='$bornum'";
380 my $sth=$dbh->prepare($query);
384 while (my $data=$sth->fetchrow_hashref){