1 package C4::Accounts2; #assumes C4/Accounts2
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
27 use C4::Circulation::Circ2;
28 use vars qw($VERSION @ISA @EXPORT);
30 # set the version for version checking
31 $VERSION = 0.01; # FIXME - Should probably be different from
32 # the version for C4::Accounts
36 C4::Accounts - Functions for dealing with Koha accounts
44 The functions in this module deal with the monetary aspect of Koha,
45 including looking up and modifying the amount of money owed by a
55 @EXPORT = qw(&recordpayment &fixaccounts &makepayment &manualinvoice
65 &recordpayment($env, $borrowernumber, $payment);
67 Record payment by a patron. C<$borrowernumber> is the patron's
68 borrower number. C<$payment> is a floating-point number, giving the
69 amount that was paid. C<$env> is a reference-to-hash;
70 C<$env-E<gt>{branchcode}> is the code of the branch where payment was
73 Amounts owed are paid off oldest first. That is, if the patron has a
74 $1 fine from Feb. 1, another $1 fine from Mar. 1, and makes a payment
75 of $1.50, then the oldest fine will be paid off in full, and $0.50
76 will be credited to the next one.
81 #here we update both the accountoffsets and the account lines
82 my ($env,$bornumber,$data)=@_;
83 my $dbh = C4::Context->dbh;
87 my $branch=$env->{'branchcode'};
88 my $amountleft = $data;
90 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
91 # get lines with outstanding amounts to offset
92 my $query = "select * from accountlines
93 where (borrowernumber = '$bornumber') and (amountoutstanding<>0)
95 my $sth = $dbh->prepare($query);
98 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
99 if ($accdata->{'amountoutstanding'} < $amountleft) {
101 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
104 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
107 my $thisacct = $accdata->{accountno};
108 $updquery = "update accountlines set amountoutstanding= '$newamtos'
109 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
110 my $usth = $dbh->prepare($updquery);
113 $updquery = "insert into accountoffsets
114 (borrowernumber, accountno, offsetaccount, offsetamount)
115 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
116 $usth = $dbh->prepare($updquery);
121 $updquery = "insert into accountlines
122 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
123 values ($bornumber,$nextaccntno,now(),0-$data,'Payment,thanks',
124 'Pay',0-$amountleft)";
125 my $usth = $dbh->prepare($updquery);
128 UpdateStats($env,$branch,'payment',$data,'','','',$bornumber);
134 &makepayment($borrowernumber, $acctnumber, $amount, $branchcode);
136 Records the fact that a patron has paid off the entire amount he or
139 C<$borrowernumber> is the patron's borrower number. C<$acctnumber> is
140 the account that was credited. C<$amount> is the amount paid (this is
141 only used to record the payment. It is assumed to be equal to the
142 amount owed). C<$branchcode> is the code of the branch where payment
147 # FIXME - I'm not at all sure about the above, because I don't
148 # understand what the acct* tables in the Koha database are for.
150 #here we update both the accountoffsets and the account lines
151 #updated to check, if they are paying off a lost item, we return the item
152 # from their card, and put a note on the item record
153 my ($bornumber,$accountno,$amount,$user)=@_;
155 my $dbh = C4::Context->dbh;
157 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
159 my $sel="Select * from accountlines where borrowernumber=$bornumber and
160 accountno=$accountno";
161 my $sth=$dbh->prepare($sel);
163 my $data=$sth->fetchrow_hashref;
168 SET amountoutstanding = 0
169 WHERE borrowernumber = $bornumber
170 AND accountno = $accountno
175 INSERT INTO accountoffsets
176 (borrowernumber, accountno, offsetaccount,
178 VALUES ($bornumber, $accountno, $nextaccntno, $newamtos)
182 my $payment=0-$amount;
184 INSERT INTO accountlines
185 (borrowernumber, accountno, date, amount,
186 description, accounttype, amountoutstanding)
187 VALUES ($bornumber, $nextaccntno, now(), $payment,
188 'Payment,thanks - $user', 'Pay', 0)
191 # FIXME - The second argument to &UpdateStats is supposed to be the
193 UpdateStats($env,$user,'payment',$amount,'','','',$bornumber);
195 #check to see what accounttype
196 if ($data->{'accounttype'} eq 'Rep' || $data->{'accounttype'} eq 'L'){
197 returnlost($bornumber,$data->{'itemnumber'});
203 $nextacct = &getnextacctno($env, $borrowernumber, $dbh);
205 Returns the next unused account number for the patron with the given
208 C<$dbh> is a DBI::db handle to the Koha database.
214 # FIXME - Okay, so what does the above actually _mean_?
216 my ($env,$bornumber,$dbh)=@_;
218 my $query = "select * from accountlines
219 where (borrowernumber = '$bornumber')
220 order by accountno desc";
221 my $sth = $dbh->prepare($query);
223 if (my $accdata=$sth->fetchrow_hashref){
224 $nextaccntno = $accdata->{'accountno'} + 1;
227 return($nextaccntno);
232 &fixaccounts($borrowernumber, $accountnumber, $amount);
236 # FIXME - I don't understand what this function does.
238 my ($borrowernumber,$accountno,$amount)=@_;
239 my $dbh = C4::Context->dbh;
240 my $query="Select * from accountlines where borrowernumber=$borrowernumber
241 and accountno=$accountno";
242 my $sth=$dbh->prepare($query);
244 my $data=$sth->fetchrow_hashref;
245 # FIXME - Error-checking
246 my $diff=$amount-$data->{'amount'};
247 my $outstanding=$data->{'amountoutstanding'}+$diff;
252 SET amount = '$amount',
253 amountoutstanding = '$outstanding'
254 WHERE borrowernumber = $borrowernumber
255 AND accountno = $accountno
259 # FIXME - Never used, but not exported, either.
261 my ($borrnum,$itemnum)=@_;
262 my $dbh = C4::Context->dbh;
263 my $borrower=borrdata('',$borrnum); #from C4::Search;
264 my $upiss="Update issues set returndate=now() where
265 borrowernumber='$borrnum' and itemnumber='$itemnum' and returndate is null";
266 my $sth=$dbh->prepare($upiss);
269 my @datearr = localtime(time);
270 my $date = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
271 my $bor="$borrower->{'firstname'} $borrower->{'surname'} $borrower->{'cardnumber'}";
272 # FIXME - Use $dbh->do();
273 my $upitem="Update items set paidfor='Paid for by $bor $date' where itemnumber='$itemnum'";
274 $sth=$dbh->prepare($upitem);
281 &manualinvoice($borrowernumber, $itemnumber, $description, $type,
284 C<$borrowernumber> is the patron's borrower number.
285 C<$description> is a description of the transaction.
286 C<$type> may be one of C<CS>, C<CB>, C<CW>, C<CF>, C<CL>, C<N>, C<L>,
288 C<$itemnumber> is the item involved, if pertinent; otherwise, it
289 should be the empty string.
293 # FIXME - Okay, so what does this function do, really?
295 my ($bornum,$itemnum,$desc,$type,$amount,$user)=@_;
296 my $dbh = C4::Context->dbh;
300 my $accountno=getnextacctno('',$bornum,$dbh);
301 my $amountleft=$amount;
303 if ($type eq 'CS' || $type eq 'CB' || $type eq 'CW'
304 || $type eq 'CF' || $type eq 'CL'){
305 my $amount2=$amount*-1; # FIXME - $amount2 = -$amount
306 $amountleft=fixcredit(\%env,$bornum,$amount2,$itemnum,$type,$user);
311 if ($type eq 'L' && $desc eq ''){
315 $amountleft=refund('',$bornum,$amount);
318 my $sth=$dbh->prepare("Select * from items where barcode='$itemnum'");
320 my $data=$sth->fetchrow_hashref;
323 $desc=$dbh->quote($desc);
325 INSERT INTO accountlines
326 (borrowernumber, accountno, date, amount,
327 description, accounttype, amountoutstanding,
329 VALUES ($bornum, $accountno, now(), '$amount',
330 $desc, '$type', '$amountleft',
331 '$data->{'itemnumber'}')
334 $desc=$dbh->quote($desc);
336 INSERT INTO accountlines
337 (borrowernumber, accountno, date, amount,
338 description, accounttype, amountoutstanding)
339 VALUES ($bornum, $accountno, now(), '$amount',
340 $desc, '$type', '$amountleft')
346 # $amountleft = &fixcredit($env, $bornumber, $data, $barcode, $type, $user);
348 # This function is only used internally.
349 # FIXME - Figure out what this function does, and write it down.
351 #here we update both the accountoffsets and the account lines
352 my ($env,$bornumber,$data,$barcode,$type,$user)=@_;
353 my $dbh = C4::Context->dbh;
357 my $amountleft = $data;
359 my $item=getiteminformation($env,'',$barcode);
360 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
361 my $query="Select * from accountlines where (borrowernumber='$bornumber'
362 and itemnumber='$item->{'itemnumber'}' and amountoutstanding > 0)";
364 $query.=" and (accounttype = 'L' or accounttype = 'Rep')";
365 } elsif ($type eq 'CF'){
366 $query.=" and (accounttype = 'F' or accounttype = 'FU' or
367 accounttype='Res' or accounttype='Rent')";
368 } elsif ($type eq 'CB'){
369 $query.=" and accounttype='A'";
372 my $sth=$dbh->prepare($query);
374 $accdata=$sth->fetchrow_hashref;
376 if ($accdata->{'amountoutstanding'} < $amountleft) {
378 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
381 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
384 my $thisacct = $accdata->{accountno};
385 my $updquery = "update accountlines set amountoutstanding= '$newamtos'
386 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
387 my $usth = $dbh->prepare($updquery);
390 $updquery = "insert into accountoffsets
391 (borrowernumber, accountno, offsetaccount, offsetamount)
392 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
393 $usth = $dbh->prepare($updquery);
398 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
399 # get lines with outstanding amounts to offset
400 my $query = "select * from accountlines
401 where (borrowernumber = '$bornumber') and (amountoutstanding >0)
403 my $sth = $dbh->prepare($query);
406 # offset transactions
407 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
408 if ($accdata->{'amountoutstanding'} < $amountleft) {
410 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
413 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
416 my $thisacct = $accdata->{accountno};
417 $updquery = "update accountlines set amountoutstanding= '$newamtos'
418 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
419 my $usth = $dbh->prepare($updquery);
422 $updquery = "insert into accountoffsets
423 (borrowernumber, accountno, offsetaccount, offsetamount)
424 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
425 $usth = $dbh->prepare($updquery);
430 $env->{'branch'}=$user;
431 $type="Credit ".$type;
432 UpdateStats($env,$user,$type,$data,$user,'','',$bornumber);
438 # FIXME - Figure out what this function does, and write it down.
440 #here we update both the accountoffsets and the account lines
441 my ($env,$bornumber,$data)=@_;
442 my $dbh = C4::Context->dbh;
446 # my $branch=$env->{'branchcode'};
447 my $amountleft = $data *-1;
450 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
451 # get lines with outstanding amounts to offset
452 my $query = "select * from accountlines
453 where (borrowernumber = '$bornumber') and (amountoutstanding<0)
455 my $sth = $dbh->prepare($query);
459 # offset transactions
460 while (($accdata=$sth->fetchrow_hashref) and ($amountleft<0)){
461 if ($accdata->{'amountoutstanding'} > $amountleft) {
463 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
466 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
470 my $thisacct = $accdata->{accountno};
471 $updquery = "update accountlines set amountoutstanding= '$newamtos'
472 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
473 my $usth = $dbh->prepare($updquery);
476 $updquery = "insert into accountoffsets
477 (borrowernumber, accountno, offsetaccount, offsetamount)
478 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
479 $usth = $dbh->prepare($updquery);
487 END { } # module clean-up code here (global destructor)