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 use C4::Circulation::Issues;
33 use C4::Circulation::Renewals;
38 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
40 # set the version for version checking
44 @EXPORT = qw(&findborrower &Borenq &findoneborrower &NewBorrowerNumber
46 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
48 # your exported package globals go here,
49 # as well as any optionally exported functions
51 @EXPORT_OK = qw($Var1 %Hashit);
54 # non-exported package globals go here
55 use vars qw(@more $stuff);
57 # initalize package globals, first exported ones
62 # then the others (which are still accessible as $Some::Module::stuff)
66 # all file-scoped lexicals must be created before
67 # the functions below that use them.
69 # file-private lexicals go here
73 # here's a file-private function as a closure,
74 # callable as &$priv_func; it cannot be prototyped.
79 # make all your functions, whether exported or not;
84 C4::InterfaceCDK::helptext('');
85 C4::InterfaceCDK::clearscreen();
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);
98 $borcode = $env->{'newborrower'};
101 $env->{'newborrower'}= "";
103 #C4::Circulation::Main
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);
114 if (my $issdata = $iss_sth->fetchrow_hashref) {
115 $bornum=$issdata->{'borrowernumber'};
116 $sth = $dbh->prepare("Select * from borrowers
117 where borrowernumber = '$bornum'");
119 $borrower=$sth->fetchrow_hashref;
122 error_msg($env,"Item $book not found");
128 my ($issuesallowed,$owing);
130 $env->{'bornum'} = $bornum;
131 $env->{'bcard'} = $borrower->{'cardnumber'};
132 my $borrowers=join(' ',($borrower->{'title'},$borrower->{'firstname'},$borrower->{'surname'}));
134 ($issuesallowed,$odues,$owing) = &checktraps($env,$dbh,$bornum,$borrower);
135 # error_msg ($env,"bcard = $env->{'bcard'}");
137 #debug_msg ($env,"2 = $env->{'IssuesAllowed'}");
138 return ($bornum, $issuesallowed,$borrower,$reason,$owing);
142 sub findoneborrower {
143 # output(1,1,$borcode);
144 my ($env,$dbh,$borcode)=@_;
147 my $ucborcode = uc $borcode;
148 my $lcborcode = lc $borcode;
149 my $sth=$dbh->prepare("Select * from borrowers where cardnumber=\"$ucborcode\"");
151 if ($borrower=$sth->fetchrow_hashref) {
152 $bornum=$borrower->{'borrowernumber'};
156 # my $borquery = "Select * from borrowers
157 # where surname ~* '$borcode' order by surname";
159 my $borquery = "Select * from borrowers
160 where lower(surname) like \"$lcborcode%\" order by surname,firstname";
161 my $sthb =$dbh->prepare($borquery);
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'};
174 $bornum = $bornums[0];
175 my $query = "select * from borrowers where borrowernumber = '$bornum'";
176 $sth = $dbh->prepare($query);
178 $borrower =$sth->fetchrow_hashref;
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);
185 $borrower =$sth->fetchrow_hashref;
187 $bornum=$borrower->{'borrowernumber'};
188 #C4::InterfaceCDK::clearscreen();
190 error_msg($env,"Borrower not found");
194 return ($bornum,$borrower);
197 my ($env,$dbh,$bornum,$borrower) = @_;
198 my $issuesallowed = "1";
204 while ($traps_done ne "DONE") {
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);
225 $traps_done = "DONE";
228 return ($issuesallowed, $odues,$amount);
232 my ($env,$dbh,$bornum,$borrower,$amount,$odues,$traps_set,$waiting) = @_;
233 my $issuesallowed = 1;
236 while (@$traps_set[$x] ne "") {
237 $traps{@$traps_set[$x]} = 1;
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);
249 $traps{'CHARGES'} = 0;
252 while ($traps_set->[$x] ne "") {
253 if ($traps_set->[$x] ne "CHARGES") {
254 push @newtraps,$traps_set->[$x];
258 $traps_set = \@newtraps;
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);
269 while ($traps_set->[$x] ne "") {
270 if ($traps_set->[$x] ne "ODUES") {
271 push @newtraps,$traps_set->[$x];
275 $traps_set = \@newtraps;
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);
285 $borrower->{'borrowernotes'} = $notes;
291 while ($traps_set->[$x] ne "") {
292 if ($traps_set->[$x] ne "NOTES") {
293 push @newtraps,$traps_set->[$x];
297 $traps_set = \@newtraps;
300 my $notr = @$traps_set;
304 $traps_done = "DONE";
306 if ($traps{'GNA'} eq 1 ) {
308 $env->{'IssuesAllowed'} = 0;
310 if ($traps{'CHARGES'} eq 1) {
312 $env->{'IssuesAllowed'} = 0;
316 return ($issuesallowed,$traps_done,$amount,$odues);
317 } # end of process_traps
327 $env->{'sysarea'} = "Enquiries";
328 while ($reason eq "") {
329 $env->{'sysarea'} = "Enquiries";
330 ($bornum,$issuesallowed,$borrower,$reason) = &findborrower($env,$dbh);
332 my ($data,$reason)=&borrowerwindow($env,$borrower);
333 if ($reason eq 'Modify'){
334 modifyuser($env,$borrower);
336 } elsif ($reason eq 'New'){
346 my ($env,$borrower) = @_;
347 debug_msg($env,"Please use intranet");
352 my ($env,$borrower,$amount,$odues,$waiting) = @_;
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);
361 while (my $data=$sth->fetchrow_hashref){
362 my $itemdata = itemnodata($env,$dbh,$data->{'itemnumber'});
364 push @items,$itemdata;
368 reservesdisplay($env,$borrower,$amount,$odues,\@items);
372 sub NewBorrowerNumber {
374 my $sth=$dbh->prepare("Select max(borrowernumber) from borrowers");
376 my $data=$sth->fetchrow_hashref;
378 $data->{'max(borrowernumber)'}++;
379 return($data->{'max(borrowernumber)'});
386 my $query="select cardnumber,borrowernumber from borrowers where
387 guarantor='$bornum'";
388 my $sth=$dbh->prepare($query);
392 while (my $data=$sth->fetchrow_hashref){
400 END { } # module clean-up code here (global destructor)