Bug 9261: (follow-up) make title required when adding a new suggestion
[koha.git] / C4 / Accounts.pm
blobc34b33083a82acbc17dfac244cb9644b3b343ee1
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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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);
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.
79 Amounts owed are paid off oldest first. That is, if the patron has a
80 $1 fine from Feb. 1, another $1 fine from Mar. 1, and makes a payment
81 of $1.50, then the oldest fine will be paid off in full, and $0.50
82 will be credited to the next one.
84 =cut
87 sub recordpayment {
89 #here we update the account lines
90 my ( $borrowernumber, $data ) = @_;
91 my $dbh = C4::Context->dbh;
92 my $newamtos = 0;
93 my $accdata = "";
94 my $branch = C4::Context->userenv->{'branch'};
95 my $amountleft = $data;
96 my $manager_id = 0;
97 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
99 # begin transaction
100 my $nextaccntno = getnextacctno($borrowernumber);
102 # get lines with outstanding amounts to offset
103 my $sth = $dbh->prepare(
104 "SELECT * FROM accountlines
105 WHERE (borrowernumber = ?) AND (amountoutstanding<>0)
106 ORDER BY date"
108 $sth->execute($borrowernumber);
110 # offset transactions
111 my @ids;
112 while ( ( $accdata = $sth->fetchrow_hashref ) and ( $amountleft > 0 ) ) {
113 if ( $accdata->{'amountoutstanding'} < $amountleft ) {
114 $newamtos = 0;
115 $amountleft -= $accdata->{'amountoutstanding'};
117 else {
118 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
119 $amountleft = 0;
121 my $thisacct = $accdata->{accountlines_id};
122 my $usth = $dbh->prepare(
123 "UPDATE accountlines SET amountoutstanding= ?
124 WHERE (accountlines_id = ?)"
126 $usth->execute( $newamtos, $thisacct );
128 if ( C4::Context->preference("FinesLog") ) {
129 $accdata->{'amountoutstanding_new'} = $newamtos;
130 logaction("FINES", 'MODIFY', $borrowernumber, Dumper({
131 action => 'fee_payment',
132 borrowernumber => $accdata->{'borrowernumber'},
133 old_amountoutstanding => $accdata->{'amountoutstanding'},
134 new_amountoutstanding => $newamtos,
135 amount_paid => $accdata->{'amountoutstanding'} - $newamtos,
136 accountlines_id => $accdata->{'accountlines_id'},
137 accountno => $accdata->{'accountno'},
138 manager_id => $manager_id,
139 }));
140 push( @ids, $accdata->{'accountlines_id'} );
144 # create new line
145 my $usth = $dbh->prepare(
146 "INSERT INTO accountlines
147 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding,manager_id)
148 VALUES (?,?,now(),?,'Payment,thanks','Pay',?,?)"
150 $usth->execute( $borrowernumber, $nextaccntno, 0 - $data, 0 - $amountleft, $manager_id );
152 UpdateStats( $branch, 'payment', $data, '', '', '', $borrowernumber, $nextaccntno );
154 if ( C4::Context->preference("FinesLog") ) {
155 $accdata->{'amountoutstanding_new'} = $newamtos;
156 logaction("FINES", 'CREATE',$borrowernumber,Dumper({
157 action => 'create_payment',
158 borrowernumber => $borrowernumber,
159 accountno => $nextaccntno,
160 amount => $data * -1,
161 amountoutstanding => $amountleft * -1,
162 accounttype => 'Pay',
163 accountlines_paid => \@ids,
164 manager_id => $manager_id,
165 }));
170 =head2 makepayment
172 &makepayment($accountlines_id, $borrowernumber, $acctnumber, $amount, $branchcode);
174 Records the fact that a patron has paid off the entire amount he or
175 she owes.
177 C<$borrowernumber> is the patron's borrower number. C<$acctnumber> is
178 the account that was credited. C<$amount> is the amount paid (this is
179 only used to record the payment. It is assumed to be equal to the
180 amount owed). C<$branchcode> is the code of the branch where payment
181 was made.
183 =cut
186 # FIXME - I'm not at all sure about the above, because I don't
187 # understand what the acct* tables in the Koha database are for.
188 sub makepayment {
190 #here we update both the accountoffsets and the account lines
191 #updated to check, if they are paying off a lost item, we return the item
192 # from their card, and put a note on the item record
193 my ( $accountlines_id, $borrowernumber, $accountno, $amount, $user, $branch, $payment_note ) = @_;
194 my $dbh = C4::Context->dbh;
195 my $manager_id = 0;
196 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
198 # begin transaction
199 my $nextaccntno = getnextacctno($borrowernumber);
200 my $newamtos = 0;
201 my $sth = $dbh->prepare("SELECT * FROM accountlines WHERE accountlines_id=?");
202 $sth->execute( $accountlines_id );
203 my $data = $sth->fetchrow_hashref;
205 my $payment;
206 if ( $data->{'accounttype'} eq "Pay" ){
207 my $udp =
208 $dbh->prepare(
209 "UPDATE accountlines
210 SET amountoutstanding = 0, description = 'Payment,thanks'
211 WHERE accountlines_id = ?
214 $udp->execute($accountlines_id);
215 }else{
216 my $udp =
217 $dbh->prepare(
218 "UPDATE accountlines
219 SET amountoutstanding = 0
220 WHERE accountlines_id = ?
223 $udp->execute($accountlines_id);
225 # create new line
226 my $payment = 0 - $amount;
227 $payment_note //= "";
229 my $ins =
230 $dbh->prepare(
231 "INSERT
232 INTO accountlines (borrowernumber, accountno, date, amount, itemnumber, description, accounttype, amountoutstanding, manager_id, note)
233 VALUES ( ?, ?, now(), ?, ?, 'Payment,thanks', 'Pay', 0, ?, ?)"
235 $ins->execute($borrowernumber, $nextaccntno, $payment, $data->{'itemnumber'}, $manager_id, $payment_note);
238 if ( C4::Context->preference("FinesLog") ) {
239 logaction("FINES", 'MODIFY', $borrowernumber, Dumper({
240 action => 'fee_payment',
241 borrowernumber => $borrowernumber,
242 old_amountoutstanding => $data->{'amountoutstanding'},
243 new_amountoutstanding => 0,
244 amount_paid => $data->{'amountoutstanding'},
245 accountlines_id => $data->{'accountlines_id'},
246 accountno => $data->{'accountno'},
247 manager_id => $manager_id,
248 }));
251 logaction("FINES", 'CREATE',$borrowernumber,Dumper({
252 action => 'create_payment',
253 borrowernumber => $borrowernumber,
254 accountno => $nextaccntno,
255 amount => $payment,
256 amountoutstanding => 0,,
257 accounttype => 'Pay',
258 accountlines_paid => [$data->{'accountlines_id'}],
259 manager_id => $manager_id,
260 }));
264 # FIXME - The second argument to &UpdateStats is supposed to be the
265 # branch code.
266 # UpdateStats is now being passed $accountno too. MTJ
267 UpdateStats( $user, 'payment', $amount, '', '', '', $borrowernumber,
268 $accountno );
270 #check to see what accounttype
271 if ( $data->{'accounttype'} eq 'Rep' || $data->{'accounttype'} eq 'L' ) {
272 C4::Circulation::ReturnLostItem( $borrowernumber, $data->{'itemnumber'} );
274 my $sthr = $dbh->prepare("SELECT max(accountlines_id) AS lastinsertid FROM accountlines");
275 $sthr->execute();
276 my $datalastinsertid = $sthr->fetchrow_hashref;
277 return $datalastinsertid->{'lastinsertid'};
280 =head2 getnextacctno
282 $nextacct = &getnextacctno($borrowernumber);
284 Returns the next unused account number for the patron with the given
285 borrower number.
287 =cut
290 # FIXME - Okay, so what does the above actually _mean_?
291 sub getnextacctno {
292 my ($borrowernumber) = shift or return;
293 my $sth = C4::Context->dbh->prepare(
294 "SELECT accountno+1 FROM accountlines
295 WHERE (borrowernumber = ?)
296 ORDER BY accountno DESC
297 LIMIT 1"
299 $sth->execute($borrowernumber);
300 return ($sth->fetchrow || 1);
303 =head2 fixaccounts (removed)
305 &fixaccounts($accountlines_id, $borrowernumber, $accountnumber, $amount);
308 # FIXME - I don't understand what this function does.
309 sub fixaccounts {
310 my ( $accountlines_id, $borrowernumber, $accountno, $amount ) = @_;
311 my $dbh = C4::Context->dbh;
312 my $sth = $dbh->prepare(
313 "SELECT * FROM accountlines WHERE accountlines_id=?"
315 $sth->execute( $accountlines_id );
316 my $data = $sth->fetchrow_hashref;
318 # FIXME - Error-checking
319 my $diff = $amount - $data->{'amount'};
320 my $outstanding = $data->{'amountoutstanding'} + $diff;
321 $sth->finish;
323 $dbh->do(<<EOT);
324 UPDATE accountlines
325 SET amount = '$amount',
326 amountoutstanding = '$outstanding'
327 WHERE accountlines_id = $accountlines_id
329 # FIXME: exceedingly bad form. Use prepare with placholders ("?") in query and execute args.
332 =cut
334 sub chargelostitem{
335 # lost ==1 Lost, lost==2 longoverdue, lost==3 lost and paid for
336 # FIXME: itemlost should be set to 3 after payment is made, should be a warning to the interface that
337 # a charge has been added
338 # FIXME : if no replacement price, borrower just doesn't get charged?
339 my $dbh = C4::Context->dbh();
340 my ($borrowernumber, $itemnumber, $amount, $description) = @_;
342 # first make sure the borrower hasn't already been charged for this item
343 my $sth1=$dbh->prepare("SELECT * from accountlines
344 WHERE borrowernumber=? AND itemnumber=? and accounttype='L'");
345 $sth1->execute($borrowernumber,$itemnumber);
346 my $existing_charge_hashref=$sth1->fetchrow_hashref();
348 # OK, they haven't
349 unless ($existing_charge_hashref) {
350 my $manager_id = 0;
351 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
352 # This item is on issue ... add replacement cost to the borrower's record and mark it returned
353 # Note that we add this to the account even if there's no replacement price, allowing some other
354 # process (or person) to update it, since we don't handle any defaults for replacement prices.
355 my $accountno = getnextacctno($borrowernumber);
356 my $sth2=$dbh->prepare("INSERT INTO accountlines
357 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber,manager_id)
358 VALUES (?,?,now(),?,?,'L',?,?,?)");
359 $sth2->execute($borrowernumber,$accountno,$amount,
360 $description,$amount,$itemnumber,$manager_id);
362 if ( C4::Context->preference("FinesLog") ) {
363 logaction("FINES", 'CREATE', $borrowernumber, Dumper({
364 action => 'create_fee',
365 borrowernumber => $borrowernumber,
366 accountno => $accountno,
367 amount => $amount,
368 amountoutstanding => $amount,
369 description => $description,
370 accounttype => 'L',
371 itemnumber => $itemnumber,
372 manager_id => $manager_id,
373 }));
379 =head2 manualinvoice
381 &manualinvoice($borrowernumber, $itemnumber, $description, $type,
382 $amount, $note);
384 C<$borrowernumber> is the patron's borrower number.
385 C<$description> is a description of the transaction.
386 C<$type> may be one of C<CS>, C<CB>, C<CW>, C<CF>, C<CL>, C<N>, C<L>,
387 or C<REF>.
388 C<$itemnumber> is the item involved, if pertinent; otherwise, it
389 should be the empty string.
391 =cut
394 # FIXME: In Koha 3.0 , the only account adjustment 'types' passed to this function
395 # are :
396 # 'C' = CREDIT
397 # 'FOR' = FORGIVEN (Formerly 'F', but 'F' is taken to mean 'FINE' elsewhere)
398 # 'N' = New Card fee
399 # 'F' = Fine
400 # 'A' = Account Management fee
401 # 'M' = Sundry
402 # 'L' = Lost Item
405 sub manualinvoice {
406 my ( $borrowernumber, $itemnum, $desc, $type, $amount, $note ) = @_;
407 my $manager_id = 0;
408 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
409 my $dbh = C4::Context->dbh;
410 my $notifyid = 0;
411 my $insert;
412 my $accountno = getnextacctno($borrowernumber);
413 my $amountleft = $amount;
415 if ( $type eq 'N' ) {
416 $desc .= " New Card";
418 if ( $type eq 'F' ) {
419 $desc .= " Fine";
421 if ( $type eq 'A' ) {
422 $desc .= " Account Management fee";
424 if ( $type eq 'M' ) {
425 $desc .= " Sundry";
428 if ( $type eq 'L' && $desc eq '' ) {
430 $desc = " Lost Item";
432 if ( ( $type eq 'L' )
433 or ( $type eq 'F' )
434 or ( $type eq 'A' )
435 or ( $type eq 'N' )
436 or ( $type eq 'M' ) )
438 $notifyid = 1;
441 if ( $itemnum ) {
442 $desc .= ' ' . $itemnum;
443 my $sth = $dbh->prepare(
444 'INSERT INTO accountlines
445 (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding, itemnumber,notify_id, note, manager_id)
446 VALUES (?, ?, now(), ?,?, ?,?,?,?,?,?)');
447 $sth->execute($borrowernumber, $accountno, $amount, $desc, $type, $amountleft, $itemnum,$notifyid, $note, $manager_id) || return $sth->errstr;
448 } else {
449 my $sth=$dbh->prepare("INSERT INTO accountlines
450 (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding,notify_id, note, manager_id)
451 VALUES (?, ?, now(), ?, ?, ?, ?,?,?,?)"
453 $sth->execute( $borrowernumber, $accountno, $amount, $desc, $type,
454 $amountleft, $notifyid, $note, $manager_id );
457 if ( C4::Context->preference("FinesLog") ) {
458 logaction("FINES", 'CREATE',$borrowernumber,Dumper({
459 action => 'create_fee',
460 borrowernumber => $borrowernumber,
461 accountno => $accountno,
462 amount => $amount,
463 description => $desc,
464 accounttype => $type,
465 amountoutstanding => $amountleft,
466 notify_id => $notifyid,
467 note => $note,
468 itemnumber => $itemnum,
469 manager_id => $manager_id,
470 }));
473 return 0;
476 sub getcharges {
477 my ( $borrowerno, $timestamp, $accountno ) = @_;
478 my $dbh = C4::Context->dbh;
479 my $timestamp2 = $timestamp - 1;
480 my $query = "";
481 my $sth = $dbh->prepare(
482 "SELECT * FROM accountlines WHERE borrowernumber=? AND accountno = ?"
484 $sth->execute( $borrowerno, $accountno );
486 my @results;
487 while ( my $data = $sth->fetchrow_hashref ) {
488 push @results,$data;
490 return (@results);
493 sub ModNote {
494 my ( $accountlines_id, $note ) = @_;
495 my $dbh = C4::Context->dbh;
496 my $sth = $dbh->prepare('UPDATE accountlines SET note = ? WHERE accountlines_id = ?');
497 $sth->execute( $note, $accountlines_id );
500 sub getcredits {
501 my ( $date, $date2 ) = @_;
502 my $dbh = C4::Context->dbh;
503 my $sth = $dbh->prepare(
504 "SELECT * FROM accountlines,borrowers
505 WHERE amount < 0 AND accounttype <> 'Pay' AND accountlines.borrowernumber = borrowers.borrowernumber
506 AND timestamp >=TIMESTAMP(?) AND timestamp < TIMESTAMP(?)"
509 $sth->execute( $date, $date2 );
510 my @results;
511 while ( my $data = $sth->fetchrow_hashref ) {
512 $data->{'date'} = $data->{'timestamp'};
513 push @results,$data;
515 return (@results);
519 sub getrefunds {
520 my ( $date, $date2 ) = @_;
521 my $dbh = C4::Context->dbh;
523 my $sth = $dbh->prepare(
524 "SELECT *,timestamp AS datetime
525 FROM accountlines,borrowers
526 WHERE (accounttype = 'REF'
527 AND accountlines.borrowernumber = borrowers.borrowernumber
528 AND date >=? AND date <?)"
531 $sth->execute( $date, $date2 );
533 my @results;
534 while ( my $data = $sth->fetchrow_hashref ) {
535 push @results,$data;
538 return (@results);
541 sub ReversePayment {
542 my ( $accountlines_id ) = @_;
543 my $dbh = C4::Context->dbh;
545 my $sth = $dbh->prepare('SELECT * FROM accountlines WHERE accountlines_id = ?');
546 $sth->execute( $accountlines_id );
547 my $row = $sth->fetchrow_hashref();
548 my $amount_outstanding = $row->{'amountoutstanding'};
550 if ( $amount_outstanding <= 0 ) {
551 $sth = $dbh->prepare('UPDATE accountlines SET amountoutstanding = amount * -1, description = CONCAT( description, " Reversed -" ) WHERE accountlines_id = ?');
552 $sth->execute( $accountlines_id );
553 } else {
554 $sth = $dbh->prepare('UPDATE accountlines SET amountoutstanding = 0, description = CONCAT( description, " Reversed -" ) WHERE accountlines_id = ?');
555 $sth->execute( $accountlines_id );
558 if ( C4::Context->preference("FinesLog") ) {
559 my $manager_id = 0;
560 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
562 if ( $amount_outstanding <= 0 ) {
563 $row->{'amountoutstanding'} *= -1;
564 } else {
565 $row->{'amountoutstanding'} = '0';
567 $row->{'description'} .= ' Reversed -';
568 logaction("FINES", 'MODIFY', $row->{'borrowernumber'}, Dumper({
569 action => 'reverse_fee_payment',
570 borrowernumber => $row->{'borrowernumber'},
571 old_amountoutstanding => $row->{'amountoutstanding'},
572 new_amountoutstanding => 0 - $amount_outstanding,,
573 accountlines_id => $row->{'accountlines_id'},
574 accountno => $row->{'accountno'},
575 manager_id => $manager_id,
576 }));
582 =head2 recordpayment_selectaccts
584 recordpayment_selectaccts($borrowernumber, $payment,$accts);
586 Record payment by a patron. C<$borrowernumber> is the patron's
587 borrower number. C<$payment> is a floating-point number, giving the
588 amount that was paid. C<$accts> is an array ref to a list of
589 accountnos which the payment can be recorded against
591 Amounts owed are paid off oldest first. That is, if the patron has a
592 $1 fine from Feb. 1, another $1 fine from Mar. 1, and makes a payment
593 of $1.50, then the oldest fine will be paid off in full, and $0.50
594 will be credited to the next one.
596 =cut
598 sub recordpayment_selectaccts {
599 my ( $borrowernumber, $amount, $accts, $note ) = @_;
601 my $dbh = C4::Context->dbh;
602 my $newamtos = 0;
603 my $accdata = q{};
604 my $branch = C4::Context->userenv->{branch};
605 my $amountleft = $amount;
606 my $manager_id = 0;
607 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
608 my $sql = 'SELECT * FROM accountlines WHERE (borrowernumber = ?) ' .
609 'AND (amountoutstanding<>0) ';
610 if (@{$accts} ) {
611 $sql .= ' AND accountno IN ( ' . join ',', @{$accts};
612 $sql .= ' ) ';
614 $sql .= ' ORDER BY date';
615 # begin transaction
616 my $nextaccntno = getnextacctno($borrowernumber);
618 # get lines with outstanding amounts to offset
619 my $rows = $dbh->selectall_arrayref($sql, { Slice => {} }, $borrowernumber);
621 # offset transactions
622 my $sth = $dbh->prepare('UPDATE accountlines SET amountoutstanding= ? ' .
623 'WHERE accountlines_id=?');
625 my @ids;
626 for my $accdata ( @{$rows} ) {
627 if ($amountleft == 0) {
628 last;
630 if ( $accdata->{amountoutstanding} < $amountleft ) {
631 $newamtos = 0;
632 $amountleft -= $accdata->{amountoutstanding};
634 else {
635 $newamtos = $accdata->{amountoutstanding} - $amountleft;
636 $amountleft = 0;
638 my $thisacct = $accdata->{accountlines_id};
639 $sth->execute( $newamtos, $thisacct );
641 if ( C4::Context->preference("FinesLog") ) {
642 logaction("FINES", 'MODIFY', $borrowernumber, Dumper({
643 action => 'fee_payment',
644 borrowernumber => $borrowernumber,
645 old_amountoutstanding => $accdata->{'amountoutstanding'},
646 new_amountoutstanding => $newamtos,
647 amount_paid => $accdata->{'amountoutstanding'} - $newamtos,
648 accountlines_id => $accdata->{'accountlines_id'},
649 accountno => $accdata->{'accountno'},
650 manager_id => $manager_id,
651 }));
652 push( @ids, $accdata->{'accountlines_id'} );
657 # create new line
658 $sql = 'INSERT INTO accountlines ' .
659 '(borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding,manager_id,note) ' .
660 q|VALUES (?,?,now(),?,'Payment,thanks','Pay',?,?,?)|;
661 $dbh->do($sql,{},$borrowernumber, $nextaccntno, 0 - $amount, 0 - $amountleft, $manager_id, $note );
662 UpdateStats( $branch, 'payment', $amount, '', '', '', $borrowernumber, $nextaccntno );
664 if ( C4::Context->preference("FinesLog") ) {
665 logaction("FINES", 'CREATE',$borrowernumber,Dumper({
666 action => 'create_payment',
667 borrowernumber => $borrowernumber,
668 accountno => $nextaccntno,
669 amount => 0 - $amount,
670 amountoutstanding => 0 - $amountleft,
671 accounttype => 'Pay',
672 accountlines_paid => \@ids,
673 manager_id => $manager_id,
674 }));
677 return;
680 # makepayment needs to be fixed to handle partials till then this separate subroutine
681 # fills in
682 sub makepartialpayment {
683 my ( $accountlines_id, $borrowernumber, $accountno, $amount, $user, $branch, $payment_note ) = @_;
684 my $manager_id = 0;
685 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
686 if (!$amount || $amount < 0) {
687 return;
689 $payment_note //= "";
690 my $dbh = C4::Context->dbh;
692 my $nextaccntno = getnextacctno($borrowernumber);
693 my $newamtos = 0;
695 my $data = $dbh->selectrow_hashref(
696 'SELECT * FROM accountlines WHERE accountlines_id=?',undef,$accountlines_id);
697 my $new_outstanding = $data->{amountoutstanding} - $amount;
699 my $update = 'UPDATE accountlines SET amountoutstanding = ? WHERE accountlines_id = ? ';
700 $dbh->do( $update, undef, $new_outstanding, $accountlines_id);
702 if ( C4::Context->preference("FinesLog") ) {
703 logaction("FINES", 'MODIFY', $borrowernumber, Dumper({
704 action => 'fee_payment',
705 borrowernumber => $borrowernumber,
706 old_amountoutstanding => $data->{'amountoutstanding'},
707 new_amountoutstanding => $new_outstanding,
708 amount_paid => $data->{'amountoutstanding'} - $new_outstanding,
709 accountlines_id => $data->{'accountlines_id'},
710 accountno => $data->{'accountno'},
711 manager_id => $manager_id,
712 }));
715 # create new line
716 my $insert = 'INSERT INTO accountlines (borrowernumber, accountno, date, amount, '
717 . 'description, accounttype, amountoutstanding, itemnumber, manager_id, note) '
718 . ' VALUES (?, ?, now(), ?, ?, ?, 0, ?, ?, ?)';
720 $dbh->do( $insert, undef, $borrowernumber, $nextaccntno, $amount,
721 "Payment, thanks - $user", 'Pay', $data->{'itemnumber'}, $manager_id, $payment_note);
723 UpdateStats( $user, 'payment', $amount, '', '', '', $borrowernumber, $accountno );
725 if ( C4::Context->preference("FinesLog") ) {
726 logaction("FINES", 'CREATE',$borrowernumber,Dumper({
727 action => 'create_payment',
728 borrowernumber => $user,
729 accountno => $nextaccntno,
730 amount => 0 - $amount,
731 accounttype => 'Pay',
732 itemnumber => $data->{'itemnumber'},
733 accountlines_paid => [ $data->{'accountlines_id'} ],
734 manager_id => $manager_id,
735 }));
738 return;
741 =head2 WriteOffFee
743 WriteOffFee( $borrowernumber, $accountline_id, $itemnum, $accounttype, $amount, $branch, $payment_note );
745 Write off a fine for a patron.
746 C<$borrowernumber> is the patron's borrower number.
747 C<$accountline_id> is the accountline_id of the fee to write off.
748 C<$itemnum> is the itemnumber of of item whose fine is being written off.
749 C<$accounttype> is the account type of the fine being written off.
750 C<$amount> is a floating-point number, giving the amount that is being written off.
751 C<$branch> is the branchcode of the library where the writeoff occurred.
752 C<$payment_note> is the note to attach to this payment
754 =cut
756 sub WriteOffFee {
757 my ( $borrowernumber, $accountlines_id, $itemnum, $accounttype, $amount, $branch, $payment_note ) = @_;
758 $payment_note //= "";
759 $branch ||= C4::Context->userenv->{branch};
760 my $manager_id = 0;
761 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
763 # if no item is attached to fine, make sure to store it as a NULL
764 $itemnum ||= undef;
766 my ( $sth, $query );
767 my $dbh = C4::Context->dbh();
769 $query = "
770 UPDATE accountlines SET amountoutstanding = 0
771 WHERE accountlines_id = ? AND borrowernumber = ?
773 $sth = $dbh->prepare( $query );
774 $sth->execute( $accountlines_id, $borrowernumber );
776 if ( C4::Context->preference("FinesLog") ) {
777 logaction("FINES", 'MODIFY', $borrowernumber, Dumper({
778 action => 'fee_writeoff',
779 borrowernumber => $borrowernumber,
780 accountlines_id => $accountlines_id,
781 manager_id => $manager_id,
782 }));
785 $query ="
786 INSERT INTO accountlines
787 ( borrowernumber, accountno, itemnumber, date, amount, description, accounttype, manager_id, note )
788 VALUES ( ?, ?, ?, NOW(), ?, 'Writeoff', 'W', ?, ? )
790 $sth = $dbh->prepare( $query );
791 my $acct = getnextacctno($borrowernumber);
792 $sth->execute( $borrowernumber, $acct, $itemnum, $amount, $manager_id, $payment_note );
794 if ( C4::Context->preference("FinesLog") ) {
795 logaction("FINES", 'CREATE',$borrowernumber,Dumper({
796 action => 'create_writeoff',
797 borrowernumber => $borrowernumber,
798 accountno => $acct,
799 amount => 0 - $amount,
800 accounttype => 'W',
801 itemnumber => $itemnum,
802 accountlines_paid => [ $accountlines_id ],
803 manager_id => $manager_id,
804 }));
807 UpdateStats( $branch, 'writeoff', $amount, q{}, q{}, q{}, $borrowernumber );
811 END { } # module clean-up code here (global destructor)
814 __END__
816 =head1 SEE ALSO
818 DBI(3)
820 =cut