1 package C4::Circulation::Borrower; #assumes C4/Circulation/Borrower
3 #package to deal with Issues
4 #written 3/11/99 by chris@katipo.co.nz
7 # Copyright 2000-2002 Katipo Communications
9 # This file is part of Koha.
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
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.
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
30 use C4::Interface::FlagsCDK;
31 use C4::Circulation::Main;
32 # FIXME - C4::Circulation::Main and C4::Circulation::Borrower
33 # use each other, so functions get redefined.
34 use C4::Circulation::Issues;
35 # FIXME - C4::Circulation::Issues and C4::Circulation::Borrower
36 # use each other, so functions get redefined.
37 use C4::Circulation::Renewals;
42 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
44 # set the version for version checking
48 @EXPORT = qw(&findborrower &Borenq &findoneborrower &NewBorrowerNumber
50 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
52 # your exported package globals go here,
53 # as well as any optionally exported functions
55 @EXPORT_OK = qw($Var1 %Hashit);
58 # non-exported package globals go here
59 use vars qw(@more $stuff);
61 # initalize package globals, first exported ones
66 # then the others (which are still accessible as $Some::Module::stuff)
70 # all file-scoped lexicals must be created before
71 # the functions below that use them.
73 # file-private lexicals go here
77 # here's a file-private function as a closure,
78 # callable as &$priv_func; it cannot be prototyped.
83 # make all your functions, whether exported or not;
88 C4::InterfaceCDK::helptext('');
89 C4::InterfaceCDK::clearscreen();
96 while (($bornum eq '') && ($reason eq "")) {
97 #get borrowerbarcode from scanner
98 my $title = C4::InterfaceCDK::titlepanel($env,$env->{'sysarea'},"Borrower Entry");
99 if ($env->{'newborrower'} eq "") {
100 ($borcode,$reason,$book)=&C4::Circulation::Main::scanborrower($env);
102 $borcode = $env->{'newborrower'};
105 $env->{'newborrower'}= "";
107 #C4::Circulation::Main
109 if ($borcode ne '') {
110 ($bornum,$borrower) = findoneborrower($env,$dbh,$borcode);
111 $env->{'IssuesAllowed'} = 1;
112 } elsif ($book ne "") {
113 my $query = "select * from issues,items where (barcode = '$book')
114 and (items.itemnumber = issues.itemnumber)
115 and (issues.returndate is null)";
116 my $iss_sth=$dbh->prepare($query);
118 if (my $issdata = $iss_sth->fetchrow_hashref) {
119 $bornum=$issdata->{'borrowernumber'};
120 $sth = $dbh->prepare("Select * from borrowers
121 where borrowernumber = '$bornum'");
123 $borrower=$sth->fetchrow_hashref;
126 error_msg($env,"Item $book not found");
132 my ($issuesallowed,$owing);
134 $env->{'bornum'} = $bornum;
135 $env->{'bcard'} = $borrower->{'cardnumber'};
136 my $borrowers=join(' ',($borrower->{'title'},$borrower->{'firstname'},$borrower->{'surname'}));
138 ($issuesallowed,$odues,$owing) = &checktraps($env,$dbh,$bornum,$borrower);
139 # error_msg ($env,"bcard = $env->{'bcard'}");
141 #debug_msg ($env,"2 = $env->{'IssuesAllowed'}");
142 return ($bornum, $issuesallowed,$borrower,$reason,$owing);
146 sub findoneborrower {
147 # output(1,1,$borcode);
148 my ($env,$dbh,$borcode)=@_;
151 my $ucborcode = uc $borcode;
152 my $lcborcode = lc $borcode;
153 my $sth=$dbh->prepare("Select * from borrowers where cardnumber=\"$ucborcode\"");
155 if ($borrower=$sth->fetchrow_hashref) {
156 $bornum=$borrower->{'borrowernumber'};
160 # my $borquery = "Select * from borrowers
161 # where surname ~* '$borcode' order by surname";
163 my $borquery = "Select * from borrowers
164 where lower(surname) like \"$lcborcode%\" order by surname,firstname";
165 my $sthb =$dbh->prepare($borquery);
170 while ($borrower= $sthb->fetchrow_hashref) {
171 my $line = $borrower->{'cardnumber'}.' '.$borrower->{'categorycode'}.' '.$borrower->{'surname'}.
172 ', '.$borrower->{'othernames'};
173 $borrows[$cntbor] = fmtstr($env,$line,"L50");
174 $bornums[$cntbor] =$borrower->{'borrowernumber'};
178 $bornum = $bornums[0];
179 my $query = "select * from borrowers where borrowernumber = '$bornum'";
180 $sth = $dbh->prepare($query);
182 $borrower =$sth->fetchrow_hashref;
184 } elsif ($cntbor > 0) {
185 my ($cardnum) = C4::InterfaceCDK::selborrower($env,$dbh,\@borrows,\@bornums);
186 my $query = "select * from borrowers where cardnumber = '$cardnum'";
187 $sth = $dbh->prepare($query);
189 $borrower =$sth->fetchrow_hashref;
191 $bornum=$borrower->{'borrowernumber'};
192 #C4::InterfaceCDK::clearscreen();
194 error_msg($env,"Borrower not found");
198 return ($bornum,$borrower);
201 my ($env,$dbh,$bornum,$borrower) = @_;
202 my $issuesallowed = "1";
208 while ($traps_done ne "DONE") {
210 $amount=C4::Accounts::checkaccount($env,$bornum,$dbh); #from C4::Accounts
211 if ($amount > 0) { push (@traps_set,"CHARGES");}
212 if ($borrower->{'gonenoaddress'} == 1){ push (@traps_set,"GNA");}
213 #check if member has a card reported as lost
214 if ($borrower->{'lost'} ==1){push (@traps_set,"LOST");}
215 #check the notes field if notes exist display them
216 if ($borrower->{'borrowernotes'} ne ''){ push (@traps_set,"NOTES");}
217 #check if borrower has overdue items
218 #call overdue checker
219 my $odues = &C4::Circulation::Main::checkoverdues($env,$bornum,$dbh);
220 if ($odues > 0) {push (@traps_set,"ODUES");}
221 #check if borrower has any items waiting
222 my ($nowaiting,$itemswaiting) = &C4::Circulation::Main::checkwaiting($env,$dbh,$bornum);
223 if ($nowaiting > 0) { push (@traps_set,"WAITING"); }
224 # FIXME - This should be $traps_set[0], right?
225 if (@traps_set[0] ne "" ) {
226 ($issuesallowed,$traps_done,$amount,$odues) =
227 process_traps($env,$dbh,$bornum,$borrower,
228 $amount,$odues,\@traps_set,$itemswaiting);
230 $traps_done = "DONE";
233 return ($issuesallowed, $odues,$amount);
237 my ($env,$dbh,$bornum,$borrower,$amount,$odues,$traps_set,$waiting) = @_;
238 my $issuesallowed = 1;
241 while (@$traps_set[$x] ne "") {
242 $traps{@$traps_set[$x]} = 1;
248 while ($trapact ne "NONE") {
249 $trapact = &trapscreen($env,$bornum,$borrower,$amount,$traps_set);
250 if ($trapact eq "CHARGES") {
251 C4::Accounts::reconcileaccount($env,$dbh,$bornum,$amount,$borrower,$odues);
252 ($odues,$issues,$amount)=borrdata2($env,$bornum);
254 $traps{'CHARGES'} = 0;
257 while ($traps_set->[$x] ne "") {
258 if ($traps_set->[$x] ne "CHARGES") {
259 push @newtraps,$traps_set->[$x];
263 $traps_set = \@newtraps;
265 } elsif ($trapact eq "WAITING") {
266 reserveslist($env,$borrower,$amount,$odues,$waiting);
267 } elsif ($trapact eq "ODUES") {
268 C4::Circulation::Renewals::bulkrenew($env,$dbh,$bornum,$amount,$borrower,$odues);
269 ($odues,$issues,$amount)=borrdata2($env,$bornum);
274 while ($traps_set->[$x] ne "") {
275 if ($traps_set->[$x] ne "ODUES") {
276 push @newtraps,$traps_set->[$x];
280 $traps_set = \@newtraps;
282 } elsif ($trapact eq "NOTES") {
283 my $notes = trapsnotes($env,$bornum,$borrower,$amount);
284 if ($notes ne $borrower->{'borrowernotes'}) {
285 my $query = "update borrowers set borrowernotes = '$notes'
286 where borrowernumber = $bornum";
287 my $sth = $dbh->prepare($query);
290 $borrower->{'borrowernotes'} = $notes;
296 while ($traps_set->[$x] ne "") {
297 if ($traps_set->[$x] ne "NOTES") {
298 push @newtraps,$traps_set->[$x];
302 $traps_set = \@newtraps;
305 my $notr = @$traps_set;
309 $traps_done = "DONE";
311 if ($traps{'GNA'} eq 1 ) {
313 $env->{'IssuesAllowed'} = 0;
315 if ($traps{'CHARGES'} eq 1) {
317 $env->{'IssuesAllowed'} = 0;
321 return ($issuesallowed,$traps_done,$amount,$odues);
322 } # end of process_traps
326 my $dbh = C4::Context->dbh;
332 $env->{'sysarea'} = "Enquiries";
333 while ($reason eq "") {
334 $env->{'sysarea'} = "Enquiries";
335 ($bornum,$issuesallowed,$borrower,$reason) = &findborrower($env,$dbh);
337 my ($data,$reason)=&borrowerwindow($env,$borrower);
338 if ($reason eq 'Modify'){
339 modifyuser($env,$borrower);
341 } elsif ($reason eq 'New'){
350 my ($env,$borrower) = @_;
351 debug_msg($env,"Please use intranet");
356 my ($env,$borrower,$amount,$odues,$waiting) = @_;
357 my $dbh = C4::Context->dbh;
360 my $query="Select * from reserves where
361 borrowernumber='$borrower->{'borrowernumber'}' and found='W' and
362 cancellationdate is null order by timestamp";
363 my $sth=$dbh->prepare($query);
365 while (my $data=$sth->fetchrow_hashref){
366 my $itemdata = itemnodata($env,$dbh,$data->{'itemnumber'});
368 push @items,$itemdata;
372 reservesdisplay($env,$borrower,$amount,$odues,\@items);
375 sub NewBorrowerNumber {
376 my $dbh = C4::Context->dbh;
377 my $sth=$dbh->prepare("Select max(borrowernumber) from borrowers");
379 my $data=$sth->fetchrow_hashref;
381 $data->{'max(borrowernumber)'}++;
382 return($data->{'max(borrowernumber)'});
387 my $dbh = C4::Context->dbh;
388 my $query="select cardnumber,borrowernumber from borrowers where
389 guarantor='$bornum'";
390 my $sth=$dbh->prepare($query);
394 while (my $data=$sth->fetchrow_hashref){
401 END { } # module clean-up code here (global destructor)