Bug 10963: Simplified creation - BKS framework
[koha.git] / C4 / Accounts.pm
blob9ca87e3f667b83a5d1e386f5d5b984800647be07
1 package C4::Accounts;
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use C4::Context;
24 use C4::Stats;
25 use C4::Members;
26 use C4::Circulation qw(ReturnLostItem);
27 use C4::Log qw(logaction);
29 use Data::Dumper qw(Dumper);
31 use vars qw($VERSION @ISA @EXPORT);
33 BEGIN {
34 # set the version for version checking
35 $VERSION = 3.07.00.049;
36 require Exporter;
37 @ISA = qw(Exporter);
38 @EXPORT = qw(
39 &recordpayment
40 &makepayment
41 &manualinvoice
42 &getnextacctno
43 &getcharges
44 &ModNote
45 &getcredits
46 &getrefunds
47 &chargelostitem
48 &ReversePayment
49 &makepartialpayment
50 &recordpayment_selectaccts
51 &WriteOffFee
55 =head1 NAME
57 C4::Accounts - Functions for dealing with Koha accounts
59 =head1 SYNOPSIS
61 use C4::Accounts;
63 =head1 DESCRIPTION
65 The functions in this module deal with the monetary aspect of Koha,
66 including looking up and modifying the amount of money owed by a
67 patron.
69 =head1 FUNCTIONS
71 =head2 recordpayment
73 &recordpayment($borrowernumber, $payment, $sip_paytype, $note);
75 Record payment by a patron. C<$borrowernumber> is the patron's
76 borrower number. C<$payment> is a floating-point number, giving the
77 amount that was paid. C<$sip_paytype> is an optional flag to indicate this
78 payment was made over a SIP2 interface, rather than the staff client. The
79 value passed is the SIP2 payment type value (message 37, characters 21-22)
81 Amounts owed are paid off oldest first. That is, if the patron has a
82 $1 fine from Feb. 1, another $1 fine from Mar. 1, and makes a payment
83 of $1.50, then the oldest fine will be paid off in full, and $0.50
84 will be credited to the next one.
86 =cut
89 sub recordpayment {
91 #here we update the account lines
92 my ( $borrowernumber, $data, $sip_paytype, $payment_note ) = @_;
93 my $dbh = C4::Context->dbh;
94 my $newamtos = 0;
95 my $accdata = "";
96 my $branch = C4::Context->userenv->{'branch'};
97 my $amountleft = $data;
98 my $manager_id = 0;
99 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
101 $payment_note //= "";
103 # begin transaction
104 my $nextaccntno = getnextacctno($borrowernumber);
106 # get lines with outstanding amounts to offset
107 my $sth = $dbh->prepare(
108 "SELECT * FROM accountlines
109 WHERE (borrowernumber = ?) AND (amountoutstanding<>0)
110 ORDER BY date"
112 $sth->execute($borrowernumber);
114 # offset transactions
115 my @ids;
116 while ( ( $accdata = $sth->fetchrow_hashref ) and ( $amountleft > 0 ) ) {
117 if ( $accdata->{'amountoutstanding'} < $amountleft ) {
118 $newamtos = 0;
119 $amountleft -= $accdata->{'amountoutstanding'};
121 else {
122 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
123 $amountleft = 0;
125 my $thisacct = $accdata->{accountlines_id};
126 my $usth = $dbh->prepare(
127 "UPDATE accountlines SET amountoutstanding= ?
128 WHERE (accountlines_id = ?)"
130 $usth->execute( $newamtos, $thisacct );
132 if ( C4::Context->preference("FinesLog") ) {
133 $accdata->{'amountoutstanding_new'} = $newamtos;
134 logaction("FINES", 'MODIFY', $borrowernumber, Dumper({
135 action => 'fee_payment',
136 borrowernumber => $accdata->{'borrowernumber'},
137 old_amountoutstanding => $accdata->{'amountoutstanding'},
138 new_amountoutstanding => $newamtos,
139 amount_paid => $accdata->{'amountoutstanding'} - $newamtos,
140 accountlines_id => $accdata->{'accountlines_id'},
141 accountno => $accdata->{'accountno'},
142 manager_id => $manager_id,
143 note => $payment_note,
144 }));
145 push( @ids, $accdata->{'accountlines_id'} );
149 # create new line
150 my $usth = $dbh->prepare(
151 "INSERT INTO accountlines
152 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding,manager_id, note)
153 VALUES (?,?,now(),?,'',?,?,?,?)"
156 my $paytype = "Pay";
157 $paytype .= $sip_paytype if defined $sip_paytype;
158 $usth->execute( $borrowernumber, $nextaccntno, 0 - $data, $paytype, 0 - $amountleft, $manager_id, $payment_note );
159 $usth->finish;
161 UpdateStats({
162 branch => $branch,
163 type =>'payment',
164 amount => $data,
165 borrowernumber => $borrowernumber,
166 accountno => $nextaccntno }
169 if ( C4::Context->preference("FinesLog") ) {
170 $accdata->{'amountoutstanding_new'} = $newamtos;
171 logaction("FINES", 'CREATE',$borrowernumber,Dumper({
172 action => 'create_payment',
173 borrowernumber => $borrowernumber,
174 accountno => $nextaccntno,
175 amount => $data * -1,
176 amountoutstanding => $amountleft * -1,
177 accounttype => 'Pay',
178 accountlines_paid => \@ids,
179 manager_id => $manager_id,
180 }));
185 =head2 makepayment
187 &makepayment($accountlines_id, $borrowernumber, $acctnumber, $amount, $branchcode);
189 Records the fact that a patron has paid off the entire amount he or
190 she owes.
192 C<$borrowernumber> is the patron's borrower number. C<$acctnumber> is
193 the account that was credited. C<$amount> is the amount paid (this is
194 only used to record the payment. It is assumed to be equal to the
195 amount owed). C<$branchcode> is the code of the branch where payment
196 was made.
198 =cut
201 # FIXME - I'm not at all sure about the above, because I don't
202 # understand what the acct* tables in the Koha database are for.
203 sub makepayment {
205 #here we update both the accountoffsets and the account lines
206 #updated to check, if they are paying off a lost item, we return the item
207 # from their card, and put a note on the item record
208 my ( $accountlines_id, $borrowernumber, $accountno, $amount, $user, $branch, $payment_note ) = @_;
209 my $dbh = C4::Context->dbh;
210 my $manager_id = 0;
211 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
213 # begin transaction
214 my $nextaccntno = getnextacctno($borrowernumber);
215 my $newamtos = 0;
216 my $sth = $dbh->prepare("SELECT * FROM accountlines WHERE accountlines_id=?");
217 $sth->execute( $accountlines_id );
218 my $data = $sth->fetchrow_hashref;
220 my $payment;
221 if ( $data->{'accounttype'} eq "Pay" ){
222 my $udp =
223 $dbh->prepare(
224 "UPDATE accountlines
225 SET amountoutstanding = 0
226 WHERE accountlines_id = ?
229 $udp->execute($accountlines_id);
230 }else{
231 my $udp =
232 $dbh->prepare(
233 "UPDATE accountlines
234 SET amountoutstanding = 0
235 WHERE accountlines_id = ?
238 $udp->execute($accountlines_id);
240 # create new line
241 my $payment = 0 - $amount;
242 $payment_note //= "";
244 my $ins =
245 $dbh->prepare(
246 "INSERT
247 INTO accountlines (borrowernumber, accountno, date, amount, itemnumber, description, accounttype, amountoutstanding, manager_id, note)
248 VALUES ( ?, ?, now(), ?, ?, '', 'Pay', 0, ?, ?)"
250 $ins->execute($borrowernumber, $nextaccntno, $payment, $data->{'itemnumber'}, $manager_id, $payment_note);
253 if ( C4::Context->preference("FinesLog") ) {
254 logaction("FINES", 'MODIFY', $borrowernumber, Dumper({
255 action => 'fee_payment',
256 borrowernumber => $borrowernumber,
257 old_amountoutstanding => $data->{'amountoutstanding'},
258 new_amountoutstanding => 0,
259 amount_paid => $data->{'amountoutstanding'},
260 accountlines_id => $data->{'accountlines_id'},
261 accountno => $data->{'accountno'},
262 manager_id => $manager_id,
263 }));
266 logaction("FINES", 'CREATE',$borrowernumber,Dumper({
267 action => 'create_payment',
268 borrowernumber => $borrowernumber,
269 accountno => $nextaccntno,
270 amount => $payment,
271 amountoutstanding => 0,,
272 accounttype => 'Pay',
273 accountlines_paid => [$data->{'accountlines_id'}],
274 manager_id => $manager_id,
275 }));
278 UpdateStats({
279 branch => $branch,
280 type => 'payment',
281 amount => $amount,
282 borrowernumber => $borrowernumber,
283 accountno => $accountno
286 #check to see what accounttype
287 if ( $data->{'accounttype'} eq 'Rep' || $data->{'accounttype'} eq 'L' ) {
288 C4::Circulation::ReturnLostItem( $borrowernumber, $data->{'itemnumber'} );
290 my $sthr = $dbh->prepare("SELECT max(accountlines_id) AS lastinsertid FROM accountlines");
291 $sthr->execute();
292 my $datalastinsertid = $sthr->fetchrow_hashref;
293 return $datalastinsertid->{'lastinsertid'};
296 =head2 getnextacctno
298 $nextacct = &getnextacctno($borrowernumber);
300 Returns the next unused account number for the patron with the given
301 borrower number.
303 =cut
306 # FIXME - Okay, so what does the above actually _mean_?
307 sub getnextacctno {
308 my ($borrowernumber) = shift or return;
309 my $sth = C4::Context->dbh->prepare(
310 "SELECT accountno+1 FROM accountlines
311 WHERE (borrowernumber = ?)
312 ORDER BY accountno DESC
313 LIMIT 1"
315 $sth->execute($borrowernumber);
316 return ($sth->fetchrow || 1);
319 =head2 fixaccounts (removed)
321 &fixaccounts($accountlines_id, $borrowernumber, $accountnumber, $amount);
324 # FIXME - I don't understand what this function does.
325 sub fixaccounts {
326 my ( $accountlines_id, $borrowernumber, $accountno, $amount ) = @_;
327 my $dbh = C4::Context->dbh;
328 my $sth = $dbh->prepare(
329 "SELECT * FROM accountlines WHERE accountlines_id=?"
331 $sth->execute( $accountlines_id );
332 my $data = $sth->fetchrow_hashref;
334 # FIXME - Error-checking
335 my $diff = $amount - $data->{'amount'};
336 my $outstanding = $data->{'amountoutstanding'} + $diff;
337 $sth->finish;
339 $dbh->do(<<EOT);
340 UPDATE accountlines
341 SET amount = '$amount',
342 amountoutstanding = '$outstanding'
343 WHERE accountlines_id = $accountlines_id
345 # FIXME: exceedingly bad form. Use prepare with placholders ("?") in query and execute args.
348 =cut
350 sub chargelostitem{
351 # lost ==1 Lost, lost==2 longoverdue, lost==3 lost and paid for
352 # FIXME: itemlost should be set to 3 after payment is made, should be a warning to the interface that
353 # a charge has been added
354 # FIXME : if no replacement price, borrower just doesn't get charged?
355 my $dbh = C4::Context->dbh();
356 my ($borrowernumber, $itemnumber, $amount, $description) = @_;
358 # first make sure the borrower hasn't already been charged for this item
359 my $sth1=$dbh->prepare("SELECT * from accountlines
360 WHERE borrowernumber=? AND itemnumber=? and accounttype='L'");
361 $sth1->execute($borrowernumber,$itemnumber);
362 my $existing_charge_hashref=$sth1->fetchrow_hashref();
364 # OK, they haven't
365 unless ($existing_charge_hashref) {
366 my $manager_id = 0;
367 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
368 # This item is on issue ... add replacement cost to the borrower's record and mark it returned
369 # Note that we add this to the account even if there's no replacement price, allowing some other
370 # process (or person) to update it, since we don't handle any defaults for replacement prices.
371 my $accountno = getnextacctno($borrowernumber);
372 my $sth2=$dbh->prepare("INSERT INTO accountlines
373 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber,manager_id)
374 VALUES (?,?,now(),?,?,'L',?,?,?)");
375 $sth2->execute($borrowernumber,$accountno,$amount,
376 $description,$amount,$itemnumber,$manager_id);
378 if ( C4::Context->preference("FinesLog") ) {
379 logaction("FINES", 'CREATE', $borrowernumber, Dumper({
380 action => 'create_fee',
381 borrowernumber => $borrowernumber,
382 accountno => $accountno,
383 amount => $amount,
384 amountoutstanding => $amount,
385 description => $description,
386 accounttype => 'L',
387 itemnumber => $itemnumber,
388 manager_id => $manager_id,
389 }));
395 =head2 manualinvoice
397 &manualinvoice($borrowernumber, $itemnumber, $description, $type,
398 $amount, $note);
400 C<$borrowernumber> is the patron's borrower number.
401 C<$description> is a description of the transaction.
402 C<$type> may be one of C<CS>, C<CB>, C<CW>, C<CF>, C<CL>, C<N>, C<L>,
403 or C<REF>.
404 C<$itemnumber> is the item involved, if pertinent; otherwise, it
405 should be the empty string.
407 =cut
410 # FIXME: In Koha 3.0 , the only account adjustment 'types' passed to this function
411 # are :
412 # 'C' = CREDIT
413 # 'FOR' = FORGIVEN (Formerly 'F', but 'F' is taken to mean 'FINE' elsewhere)
414 # 'N' = New Card fee
415 # 'F' = Fine
416 # 'A' = Account Management fee
417 # 'M' = Sundry
418 # 'L' = Lost Item
421 sub manualinvoice {
422 my ( $borrowernumber, $itemnum, $desc, $type, $amount, $note ) = @_;
423 my $manager_id = 0;
424 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
425 my $dbh = C4::Context->dbh;
426 my $notifyid = 0;
427 my $insert;
428 my $accountno = getnextacctno($borrowernumber);
429 my $amountleft = $amount;
431 if ( ( $type eq 'L' )
432 or ( $type eq 'F' )
433 or ( $type eq 'A' )
434 or ( $type eq 'N' )
435 or ( $type eq 'M' ) )
437 $notifyid = 1;
440 if ( $itemnum ) {
441 $desc .= ' ' . $itemnum;
442 my $sth = $dbh->prepare(
443 'INSERT INTO accountlines
444 (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding, itemnumber,notify_id, note, manager_id)
445 VALUES (?, ?, now(), ?,?, ?,?,?,?,?,?)');
446 $sth->execute($borrowernumber, $accountno, $amount, $desc, $type, $amountleft, $itemnum,$notifyid, $note, $manager_id) || return $sth->errstr;
447 } else {
448 my $sth=$dbh->prepare("INSERT INTO accountlines
449 (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding,notify_id, note, manager_id)
450 VALUES (?, ?, now(), ?, ?, ?, ?,?,?,?)"
452 $sth->execute( $borrowernumber, $accountno, $amount, $desc, $type,
453 $amountleft, $notifyid, $note, $manager_id );
456 if ( C4::Context->preference("FinesLog") ) {
457 logaction("FINES", 'CREATE',$borrowernumber,Dumper({
458 action => 'create_fee',
459 borrowernumber => $borrowernumber,
460 accountno => $accountno,
461 amount => $amount,
462 description => $desc,
463 accounttype => $type,
464 amountoutstanding => $amountleft,
465 notify_id => $notifyid,
466 note => $note,
467 itemnumber => $itemnum,
468 manager_id => $manager_id,
469 }));
472 return 0;
475 sub getcharges {
476 my ( $borrowerno, $timestamp, $accountno ) = @_;
477 my $dbh = C4::Context->dbh;
478 my $timestamp2 = $timestamp - 1;
479 my $query = "";
480 my $sth = $dbh->prepare(
481 "SELECT * FROM accountlines WHERE borrowernumber=? AND accountno = ?"
483 $sth->execute( $borrowerno, $accountno );
485 my @results;
486 while ( my $data = $sth->fetchrow_hashref ) {
487 push @results,$data;
489 return (@results);
492 sub ModNote {
493 my ( $accountlines_id, $note ) = @_;
494 my $dbh = C4::Context->dbh;
495 my $sth = $dbh->prepare('UPDATE accountlines SET note = ? WHERE accountlines_id = ?');
496 $sth->execute( $note, $accountlines_id );
499 sub getcredits {
500 my ( $date, $date2 ) = @_;
501 my $dbh = C4::Context->dbh;
502 my $sth = $dbh->prepare(
503 "SELECT * FROM accountlines,borrowers
504 WHERE amount < 0 AND accounttype not like 'Pay%' AND accountlines.borrowernumber = borrowers.borrowernumber
505 AND timestamp >=TIMESTAMP(?) AND timestamp < TIMESTAMP(?)"
508 $sth->execute( $date, $date2 );
509 my @results;
510 while ( my $data = $sth->fetchrow_hashref ) {
511 $data->{'date'} = $data->{'timestamp'};
512 push @results,$data;
514 return (@results);
518 sub getrefunds {
519 my ( $date, $date2 ) = @_;
520 my $dbh = C4::Context->dbh;
522 my $sth = $dbh->prepare(
523 "SELECT *,timestamp AS datetime
524 FROM accountlines,borrowers
525 WHERE (accounttype = 'REF'
526 AND accountlines.borrowernumber = borrowers.borrowernumber
527 AND date >=? AND date <?)"
530 $sth->execute( $date, $date2 );
532 my @results;
533 while ( my $data = $sth->fetchrow_hashref ) {
534 push @results,$data;
537 return (@results);
540 sub ReversePayment {
541 my ( $accountlines_id ) = @_;
542 my $dbh = C4::Context->dbh;
544 my $sth = $dbh->prepare('SELECT * FROM accountlines WHERE accountlines_id = ?');
545 $sth->execute( $accountlines_id );
546 my $row = $sth->fetchrow_hashref();
547 my $amount_outstanding = $row->{'amountoutstanding'};
549 if ( $amount_outstanding <= 0 ) {
550 $sth = $dbh->prepare('UPDATE accountlines SET amountoutstanding = amount * -1, description = CONCAT( description, " Reversed -" ) WHERE accountlines_id = ?');
551 $sth->execute( $accountlines_id );
552 } else {
553 $sth = $dbh->prepare('UPDATE accountlines SET amountoutstanding = 0, description = CONCAT( description, " Reversed -" ) WHERE accountlines_id = ?');
554 $sth->execute( $accountlines_id );
557 if ( C4::Context->preference("FinesLog") ) {
558 my $manager_id = 0;
559 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
561 if ( $amount_outstanding <= 0 ) {
562 $row->{'amountoutstanding'} *= -1;
563 } else {
564 $row->{'amountoutstanding'} = '0';
566 $row->{'description'} .= ' Reversed -';
567 logaction("FINES", 'MODIFY', $row->{'borrowernumber'}, Dumper({
568 action => 'reverse_fee_payment',
569 borrowernumber => $row->{'borrowernumber'},
570 old_amountoutstanding => $row->{'amountoutstanding'},
571 new_amountoutstanding => 0 - $amount_outstanding,,
572 accountlines_id => $row->{'accountlines_id'},
573 accountno => $row->{'accountno'},
574 manager_id => $manager_id,
575 }));
581 =head2 recordpayment_selectaccts
583 recordpayment_selectaccts($borrowernumber, $payment,$accts);
585 Record payment by a patron. C<$borrowernumber> is the patron's
586 borrower number. C<$payment> is a floating-point number, giving the
587 amount that was paid. C<$accts> is an array ref to a list of
588 accountnos which the payment can be recorded against
590 Amounts owed are paid off oldest first. That is, if the patron has a
591 $1 fine from Feb. 1, another $1 fine from Mar. 1, and makes a payment
592 of $1.50, then the oldest fine will be paid off in full, and $0.50
593 will be credited to the next one.
595 =cut
597 sub recordpayment_selectaccts {
598 my ( $borrowernumber, $amount, $accts, $note ) = @_;
600 my $dbh = C4::Context->dbh;
601 my $newamtos = 0;
602 my $accdata = q{};
603 my $branch = C4::Context->userenv->{branch};
604 my $amountleft = $amount;
605 my $manager_id = 0;
606 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
607 my $sql = 'SELECT * FROM accountlines WHERE (borrowernumber = ?) ' .
608 'AND (amountoutstanding<>0) ';
609 if (@{$accts} ) {
610 $sql .= ' AND accountno IN ( ' . join ',', @{$accts};
611 $sql .= ' ) ';
613 $sql .= ' ORDER BY date';
614 # begin transaction
615 my $nextaccntno = getnextacctno($borrowernumber);
617 # get lines with outstanding amounts to offset
618 my $rows = $dbh->selectall_arrayref($sql, { Slice => {} }, $borrowernumber);
620 # offset transactions
621 my $sth = $dbh->prepare('UPDATE accountlines SET amountoutstanding= ? ' .
622 'WHERE accountlines_id=?');
624 my @ids;
625 for my $accdata ( @{$rows} ) {
626 if ($amountleft == 0) {
627 last;
629 if ( $accdata->{amountoutstanding} < $amountleft ) {
630 $newamtos = 0;
631 $amountleft -= $accdata->{amountoutstanding};
633 else {
634 $newamtos = $accdata->{amountoutstanding} - $amountleft;
635 $amountleft = 0;
637 my $thisacct = $accdata->{accountlines_id};
638 $sth->execute( $newamtos, $thisacct );
640 if ( C4::Context->preference("FinesLog") ) {
641 logaction("FINES", 'MODIFY', $borrowernumber, Dumper({
642 action => 'fee_payment',
643 borrowernumber => $borrowernumber,
644 old_amountoutstanding => $accdata->{'amountoutstanding'},
645 new_amountoutstanding => $newamtos,
646 amount_paid => $accdata->{'amountoutstanding'} - $newamtos,
647 accountlines_id => $accdata->{'accountlines_id'},
648 accountno => $accdata->{'accountno'},
649 manager_id => $manager_id,
650 }));
651 push( @ids, $accdata->{'accountlines_id'} );
656 # create new line
657 $sql = 'INSERT INTO accountlines ' .
658 '(borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding,manager_id,note) ' .
659 q|VALUES (?,?,now(),?,'','Pay',?,?,?)|;
660 $dbh->do($sql,{},$borrowernumber, $nextaccntno, 0 - $amount, 0 - $amountleft, $manager_id, $note );
661 UpdateStats({
662 branch => $branch,
663 type => 'payment',
664 amount => $amount,
665 borrowernumber => $borrowernumber,
666 accountno => $nextaccntno}
669 if ( C4::Context->preference("FinesLog") ) {
670 logaction("FINES", 'CREATE',$borrowernumber,Dumper({
671 action => 'create_payment',
672 borrowernumber => $borrowernumber,
673 accountno => $nextaccntno,
674 amount => 0 - $amount,
675 amountoutstanding => 0 - $amountleft,
676 accounttype => 'Pay',
677 accountlines_paid => \@ids,
678 manager_id => $manager_id,
679 }));
682 return;
685 # makepayment needs to be fixed to handle partials till then this separate subroutine
686 # fills in
687 sub makepartialpayment {
688 my ( $accountlines_id, $borrowernumber, $accountno, $amount, $user, $branch, $payment_note ) = @_;
689 my $manager_id = 0;
690 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
691 if (!$amount || $amount < 0) {
692 return;
694 $payment_note //= "";
695 my $dbh = C4::Context->dbh;
697 my $nextaccntno = getnextacctno($borrowernumber);
698 my $newamtos = 0;
700 my $data = $dbh->selectrow_hashref(
701 'SELECT * FROM accountlines WHERE accountlines_id=?',undef,$accountlines_id);
702 my $new_outstanding = $data->{amountoutstanding} - $amount;
704 my $update = 'UPDATE accountlines SET amountoutstanding = ? WHERE accountlines_id = ? ';
705 $dbh->do( $update, undef, $new_outstanding, $accountlines_id);
707 if ( C4::Context->preference("FinesLog") ) {
708 logaction("FINES", 'MODIFY', $borrowernumber, Dumper({
709 action => 'fee_payment',
710 borrowernumber => $borrowernumber,
711 old_amountoutstanding => $data->{'amountoutstanding'},
712 new_amountoutstanding => $new_outstanding,
713 amount_paid => $data->{'amountoutstanding'} - $new_outstanding,
714 accountlines_id => $data->{'accountlines_id'},
715 accountno => $data->{'accountno'},
716 manager_id => $manager_id,
717 }));
720 # create new line
721 my $insert = 'INSERT INTO accountlines (borrowernumber, accountno, date, amount, '
722 . 'description, accounttype, amountoutstanding, itemnumber, manager_id, note) '
723 . ' VALUES (?, ?, now(), ?, ?, ?, 0, ?, ?, ?)';
725 $dbh->do( $insert, undef, $borrowernumber, $nextaccntno, $amount,
726 '', 'Pay', $data->{'itemnumber'}, $manager_id, $payment_note);
728 UpdateStats({
729 branch => $branch,
730 type => 'payment',
731 amount => $amount,
732 borrowernumber => $borrowernumber,
733 accountno => $accountno
736 if ( C4::Context->preference("FinesLog") ) {
737 logaction("FINES", 'CREATE',$borrowernumber,Dumper({
738 action => 'create_payment',
739 borrowernumber => $user,
740 accountno => $nextaccntno,
741 amount => 0 - $amount,
742 accounttype => 'Pay',
743 itemnumber => $data->{'itemnumber'},
744 accountlines_paid => [ $data->{'accountlines_id'} ],
745 manager_id => $manager_id,
746 }));
749 return;
752 =head2 WriteOffFee
754 WriteOffFee( $borrowernumber, $accountline_id, $itemnum, $accounttype, $amount, $branch, $payment_note );
756 Write off a fine for a patron.
757 C<$borrowernumber> is the patron's borrower number.
758 C<$accountline_id> is the accountline_id of the fee to write off.
759 C<$itemnum> is the itemnumber of of item whose fine is being written off.
760 C<$accounttype> is the account type of the fine being written off.
761 C<$amount> is a floating-point number, giving the amount that is being written off.
762 C<$branch> is the branchcode of the library where the writeoff occurred.
763 C<$payment_note> is the note to attach to this payment
765 =cut
767 sub WriteOffFee {
768 my ( $borrowernumber, $accountlines_id, $itemnum, $accounttype, $amount, $branch, $payment_note ) = @_;
769 $payment_note //= "";
770 $branch ||= C4::Context->userenv->{branch};
771 my $manager_id = 0;
772 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
774 # if no item is attached to fine, make sure to store it as a NULL
775 $itemnum ||= undef;
777 my ( $sth, $query );
778 my $dbh = C4::Context->dbh();
780 $query = "
781 UPDATE accountlines SET amountoutstanding = 0
782 WHERE accountlines_id = ? AND borrowernumber = ?
784 $sth = $dbh->prepare( $query );
785 $sth->execute( $accountlines_id, $borrowernumber );
787 if ( C4::Context->preference("FinesLog") ) {
788 logaction("FINES", 'MODIFY', $borrowernumber, Dumper({
789 action => 'fee_writeoff',
790 borrowernumber => $borrowernumber,
791 accountlines_id => $accountlines_id,
792 manager_id => $manager_id,
793 }));
796 $query ="
797 INSERT INTO accountlines
798 ( borrowernumber, accountno, itemnumber, date, amount, description, accounttype, manager_id, note )
799 VALUES ( ?, ?, ?, NOW(), ?, 'Writeoff', 'W', ?, ? )
801 $sth = $dbh->prepare( $query );
802 my $acct = getnextacctno($borrowernumber);
803 $sth->execute( $borrowernumber, $acct, $itemnum, $amount, $manager_id, $payment_note );
805 if ( C4::Context->preference("FinesLog") ) {
806 logaction("FINES", 'CREATE',$borrowernumber,Dumper({
807 action => 'create_writeoff',
808 borrowernumber => $borrowernumber,
809 accountno => $acct,
810 amount => 0 - $amount,
811 accounttype => 'W',
812 itemnumber => $itemnum,
813 accountlines_paid => [ $accountlines_id ],
814 manager_id => $manager_id,
815 }));
818 UpdateStats({
819 branch => $branch,
820 type => 'writeoff',
821 amount => $amount,
822 borrowernumber => $borrowernumber}
827 END { } # module clean-up code here (global destructor)
830 __END__
832 =head1 SEE ALSO
834 DBI(3)
836 =cut