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 $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'");
91 $borrower=$sth->fetchrow_hashref;
94 error_msg($env,"Item $book not found");
100 my ($issuesallowed,$owing);
102 $env->{'bornum'} = $bornum;
103 $env->{'bcard'} = $borrower->{'cardnumber'};
104 my $borrowers=join(' ',($borrower->{'title'},$borrower->{'firstname'},$borrower->{'surname'}));
106 ($issuesallowed,$odues,$owing) = &checktraps($env,$dbh,$bornum,$borrower);
107 # error_msg ($env,"bcard = $env->{'bcard'}");
109 #debug_msg ($env,"2 = $env->{'IssuesAllowed'}");
110 return ($bornum, $issuesallowed,$borrower,$reason,$owing);
114 sub findoneborrower {
115 # output(1,1,$borcode);
116 my ($env,$dbh,$borcode)=@_;
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'};
128 # my $borquery = "Select * from borrowers
129 # where surname ~* '$borcode' order by surname";
131 my $sthb =$dbh->prepare("Select * from borrowers where lower(surname) like ? order by surname,firstname");
132 $sthb->execute("$lcborcode%");
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'};
144 $bornum = $bornums[0];
145 my $query = "select * from borrowers where borrowernumber = '$bornum'";
146 $sth = $dbh->prepare($query);
148 $borrower =$sth->fetchrow_hashref;
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;
156 $bornum=$borrower->{'borrowernumber'};
157 #C4::InterfaceCDK::clearscreen();
159 error_msg($env,"Borrower not found");
163 return ($bornum,$borrower);
166 my ($env,$dbh,$bornum,$borrower) = @_;
167 my $issuesallowed = "1";
173 while ($traps_done ne "DONE") {
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);
195 $traps_done = "DONE";
198 return ($issuesallowed, $odues,$amount);
202 my ($env,$dbh,$bornum,$borrower,$amount,$odues,$traps_set,$waiting) = @_;
203 my $issuesallowed = 1;
206 while (@$traps_set[$x] ne "") {
207 $traps{@$traps_set[$x]} = 1;
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);
219 $traps{'CHARGES'} = 0;
222 while ($traps_set->[$x] ne "") {
223 if ($traps_set->[$x] ne "CHARGES") {
224 push @newtraps,$traps_set->[$x];
228 $traps_set = \@newtraps;
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);
239 while ($traps_set->[$x] ne "") {
240 if ($traps_set->[$x] ne "ODUES") {
241 push @newtraps,$traps_set->[$x];
245 $traps_set = \@newtraps;
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);
253 $borrower->{'borrowernotes'} = $notes;
259 while ($traps_set->[$x] ne "") {
260 if ($traps_set->[$x] ne "NOTES") {
261 push @newtraps,$traps_set->[$x];
265 $traps_set = \@newtraps;
268 my $notr = @$traps_set;
272 $traps_done = "DONE";
274 if ($traps{'GNA'} eq 1 ) {
276 $env->{'IssuesAllowed'} = 0;
278 if ($traps{'CHARGES'} eq 1) {
280 $env->{'IssuesAllowed'} = 0;
284 return ($issuesallowed,$traps_done,$amount,$odues);
285 } # end of process_traps
289 my $dbh = C4::Context->dbh;
295 $env->{'sysarea'} = "Enquiries";
296 while ($reason eq "") {
297 $env->{'sysarea'} = "Enquiries";
298 ($bornum,$issuesallowed,$borrower,$reason) = &findborrower($env,$dbh);
300 my ($data,$reason)=&borrowerwindow($env,$borrower);
301 if ($reason eq 'Modify'){
302 modifyuser($env,$borrower);
304 } elsif ($reason eq 'New'){
313 my ($env,$borrower) = @_;
314 debug_msg($env,"Please use intranet");
319 my ($env,$borrower,$amount,$odues,$waiting) = @_;
320 my $dbh = C4::Context->dbh;
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'});
330 push @items,$itemdata;
334 reservesdisplay($env,$borrower,$amount,$odues,\@items);
337 =item NewBorrowerNumber
339 $num = &NewBorrowerNumber();
341 Allocates a new, unused borrower number, and returns it.
345 # FIXME - This is identical to C4::Search::NewBorrowerNumber.
346 # Pick one (preferably this one) and stick with it.
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.
358 sub NewBorrowerNumber {
359 my $dbh = C4::Context->dbh;
360 my $sth=$dbh->prepare("Select max(borrowernumber) from borrowers");
362 my $data=$sth->fetchrow_hashref;
364 $data->{'max(borrowernumber)'}++;
365 return($data->{'max(borrowernumber)'});
370 my $dbh = C4::Context->dbh;
371 my $sth=$dbh->prepare("select cardnumber,borrowernumber from borrowers where
373 $sth->execute($bornum);
376 while (my $data=$sth->fetchrow_hashref){