Bug 8508 - Holds to Pull : Library dropdown options are erroneously concatenated...
[koha.git] / C4 / Accounts.pm
bloba39ca54485f85e2ec92282739b8c5e94f52dddcb
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 &reconcileaccount
44 &getcharges
45 &ModNote
46 &getcredits
47 &getrefunds
48 &chargelostitem
49 &ReversePayment
50 &makepartialpayment
51 &recordpayment_selectaccts
52 &WriteOffFee
56 =head1 NAME
58 C4::Accounts - Functions for dealing with Koha accounts
60 =head1 SYNOPSIS
62 use C4::Accounts;
64 =head1 DESCRIPTION
66 The functions in this module deal with the monetary aspect of Koha,
67 including looking up and modifying the amount of money owed by a
68 patron.
70 =head1 FUNCTIONS
72 =head2 recordpayment
74 &recordpayment($borrowernumber, $payment);
76 Record payment by a patron. C<$borrowernumber> is the patron's
77 borrower number. C<$payment> is a floating-point number, giving the
78 amount that was paid.
80 Amounts owed are paid off oldest first. That is, if the patron has a
81 $1 fine from Feb. 1, another $1 fine from Mar. 1, and makes a payment
82 of $1.50, then the oldest fine will be paid off in full, and $0.50
83 will be credited to the next one.
85 =cut
88 sub recordpayment {
90 #here we update the account lines
91 my ( $borrowernumber, $data ) = @_;
92 my $dbh = C4::Context->dbh;
93 my $newamtos = 0;
94 my $accdata = "";
95 my $branch = C4::Context->userenv->{'branch'};
96 my $amountleft = $data;
97 my $manager_id = 0;
98 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
100 # begin transaction
101 my $nextaccntno = getnextacctno($borrowernumber);
103 # get lines with outstanding amounts to offset
104 my $sth = $dbh->prepare(
105 "SELECT * FROM accountlines
106 WHERE (borrowernumber = ?) AND (amountoutstanding<>0)
107 ORDER BY date"
109 $sth->execute($borrowernumber);
111 # offset transactions
112 my @ids;
113 while ( ( $accdata = $sth->fetchrow_hashref ) and ( $amountleft > 0 ) ) {
114 if ( $accdata->{'amountoutstanding'} < $amountleft ) {
115 $newamtos = 0;
116 $amountleft -= $accdata->{'amountoutstanding'};
118 else {
119 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
120 $amountleft = 0;
122 my $thisacct = $accdata->{accountlines_id};
123 my $usth = $dbh->prepare(
124 "UPDATE accountlines SET amountoutstanding= ?
125 WHERE (accountlines_id = ?)"
127 $usth->execute( $newamtos, $thisacct );
129 if ( C4::Context->preference("FinesLog") ) {
130 $accdata->{'amountoutstanding_new'} = $newamtos;
131 logaction("FINES", 'MODIFY', $borrowernumber, Dumper({
132 action => 'fee_payment',
133 borrowernumber => $accdata->{'borrowernumber'},
134 old_amountoutstanding => $accdata->{'amountoutstanding'},
135 new_amountoutstanding => $newamtos,
136 amount_paid => $accdata->{'amountoutstanding'} - $newamtos,
137 accountlines_id => $accdata->{'accountlines_id'},
138 accountno => $accdata->{'accountno'},
139 manager_id => $manager_id,
140 }));
141 push( @ids, $accdata->{'accountlines_id'} );
145 # create new line
146 my $usth = $dbh->prepare(
147 "INSERT INTO accountlines
148 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding,manager_id)
149 VALUES (?,?,now(),?,'Payment,thanks','Pay',?,?)"
151 $usth->execute( $borrowernumber, $nextaccntno, 0 - $data, 0 - $amountleft, $manager_id );
153 UpdateStats( $branch, 'payment', $data, '', '', '', $borrowernumber, $nextaccntno );
155 if ( C4::Context->preference("FinesLog") ) {
156 $accdata->{'amountoutstanding_new'} = $newamtos;
157 logaction("FINES", 'CREATE',$borrowernumber,Dumper({
158 action => 'create_payment',
159 borrowernumber => $borrowernumber,
160 accountno => $nextaccntno,
161 amount => $data * -1,
162 amountoutstanding => $amountleft * -1,
163 accounttype => 'Pay',
164 accountlines_paid => \@ids,
165 manager_id => $manager_id,
166 }));
171 =head2 makepayment
173 &makepayment($accountlines_id, $borrowernumber, $acctnumber, $amount, $branchcode);
175 Records the fact that a patron has paid off the entire amount he or
176 she owes.
178 C<$borrowernumber> is the patron's borrower number. C<$acctnumber> is
179 the account that was credited. C<$amount> is the amount paid (this is
180 only used to record the payment. It is assumed to be equal to the
181 amount owed). C<$branchcode> is the code of the branch where payment
182 was made.
184 =cut
187 # FIXME - I'm not at all sure about the above, because I don't
188 # understand what the acct* tables in the Koha database are for.
189 sub makepayment {
191 #here we update both the accountoffsets and the account lines
192 #updated to check, if they are paying off a lost item, we return the item
193 # from their card, and put a note on the item record
194 my ( $accountlines_id, $borrowernumber, $accountno, $amount, $user, $branch ) = @_;
195 my $dbh = C4::Context->dbh;
196 my $manager_id = 0;
197 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
199 # begin transaction
200 my $nextaccntno = getnextacctno($borrowernumber);
201 my $newamtos = 0;
202 my $sth = $dbh->prepare("SELECT * FROM accountlines WHERE accountlines_id=?");
203 $sth->execute( $accountlines_id );
204 my $data = $sth->fetchrow_hashref;
205 $sth->finish;
207 my $payment;
208 if ( $data->{'accounttype'} eq "Pay" ){
209 my $udp =
210 $dbh->prepare(
211 "UPDATE accountlines
212 SET amountoutstanding = 0, description = 'Payment,thanks'
213 WHERE accountlines_id = ?
216 $udp->execute($accountlines_id);
217 $udp->finish;
218 }else{
219 my $udp =
220 $dbh->prepare(
221 "UPDATE accountlines
222 SET amountoutstanding = 0
223 WHERE accountlines_id = ?
226 $udp->execute($accountlines_id);
227 $udp->finish;
229 # create new line
230 $payment = 0 - $amount;
232 my $ins =
233 $dbh->prepare(
234 "INSERT
235 INTO accountlines (borrowernumber, accountno, date, amount, itemnumber, description, accounttype, amountoutstanding, manager_id)
236 VALUES ( ?, ?, now(), ?, ?, 'Payment,thanks', 'Pay', 0, ?)"
238 $ins->execute($borrowernumber, $nextaccntno, $payment, $data->{'itemnumber'}, $manager_id);
239 $ins->finish;
242 if ( C4::Context->preference("FinesLog") ) {
243 logaction("FINES", 'MODIFY', $borrowernumber, Dumper({
244 action => 'fee_payment',
245 borrowernumber => $borrowernumber,
246 old_amountoutstanding => $data->{'amountoutstanding'},
247 new_amountoutstanding => 0,
248 amount_paid => $data->{'amountoutstanding'},
249 accountlines_id => $data->{'accountlines_id'},
250 accountno => $data->{'accountno'},
251 manager_id => $manager_id,
252 }));
255 logaction("FINES", 'CREATE',$borrowernumber,Dumper({
256 action => 'create_payment',
257 borrowernumber => $borrowernumber,
258 accountno => $nextaccntno,
259 amount => $payment,
260 amountoutstanding => 0,,
261 accounttype => 'Pay',
262 accountlines_paid => [$data->{'accountlines_id'}],
263 manager_id => $manager_id,
264 }));
268 # FIXME - The second argument to &UpdateStats is supposed to be the
269 # branch code.
270 # UpdateStats is now being passed $accountno too. MTJ
271 UpdateStats( $user, 'payment', $amount, '', '', '', $borrowernumber,
272 $accountno );
274 #check to see what accounttype
275 if ( $data->{'accounttype'} eq 'Rep' || $data->{'accounttype'} eq 'L' ) {
276 C4::Circulation::ReturnLostItem( $borrowernumber, $data->{'itemnumber'} );
278 my $sthr = $dbh->prepare("SELECT max(accountlines_id) AS lastinsertid FROM accountlines");
279 $sthr->execute();
280 my $datalastinsertid = $sthr->fetchrow_hashref;
281 $sthr->finish;
282 return $datalastinsertid->{'lastinsertid'};
285 =head2 getnextacctno
287 $nextacct = &getnextacctno($borrowernumber);
289 Returns the next unused account number for the patron with the given
290 borrower number.
292 =cut
295 # FIXME - Okay, so what does the above actually _mean_?
296 sub getnextacctno {
297 my ($borrowernumber) = shift or return;
298 my $sth = C4::Context->dbh->prepare(
299 "SELECT accountno+1 FROM accountlines
300 WHERE (borrowernumber = ?)
301 ORDER BY accountno DESC
302 LIMIT 1"
304 $sth->execute($borrowernumber);
305 return ($sth->fetchrow || 1);
308 =head2 fixaccounts (removed)
310 &fixaccounts($accountlines_id, $borrowernumber, $accountnumber, $amount);
313 # FIXME - I don't understand what this function does.
314 sub fixaccounts {
315 my ( $accountlines_id, $borrowernumber, $accountno, $amount ) = @_;
316 my $dbh = C4::Context->dbh;
317 my $sth = $dbh->prepare(
318 "SELECT * FROM accountlines WHERE accountlines_id=?"
320 $sth->execute( $accountlines_id );
321 my $data = $sth->fetchrow_hashref;
323 # FIXME - Error-checking
324 my $diff = $amount - $data->{'amount'};
325 my $outstanding = $data->{'amountoutstanding'} + $diff;
326 $sth->finish;
328 $dbh->do(<<EOT);
329 UPDATE accountlines
330 SET amount = '$amount',
331 amountoutstanding = '$outstanding'
332 WHERE accountlines_id = $accountlines_id
334 # FIXME: exceedingly bad form. Use prepare with placholders ("?") in query and execute args.
337 =cut
339 sub chargelostitem{
340 # lost ==1 Lost, lost==2 longoverdue, lost==3 lost and paid for
341 # FIXME: itemlost should be set to 3 after payment is made, should be a warning to the interface that
342 # a charge has been added
343 # FIXME : if no replacement price, borrower just doesn't get charged?
344 my $dbh = C4::Context->dbh();
345 my ($borrowernumber, $itemnumber, $amount, $description) = @_;
347 # first make sure the borrower hasn't already been charged for this item
348 my $sth1=$dbh->prepare("SELECT * from accountlines
349 WHERE borrowernumber=? AND itemnumber=? and accounttype='L'");
350 $sth1->execute($borrowernumber,$itemnumber);
351 my $existing_charge_hashref=$sth1->fetchrow_hashref();
353 # OK, they haven't
354 unless ($existing_charge_hashref) {
355 my $manager_id = 0;
356 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
357 # This item is on issue ... add replacement cost to the borrower's record and mark it returned
358 # Note that we add this to the account even if there's no replacement price, allowing some other
359 # process (or person) to update it, since we don't handle any defaults for replacement prices.
360 my $accountno = getnextacctno($borrowernumber);
361 my $sth2=$dbh->prepare("INSERT INTO accountlines
362 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber,manager_id)
363 VALUES (?,?,now(),?,?,'L',?,?,?)");
364 $sth2->execute($borrowernumber,$accountno,$amount,
365 $description,$amount,$itemnumber,$manager_id);
366 $sth2->finish;
368 if ( C4::Context->preference("FinesLog") ) {
369 logaction("FINES", 'CREATE', $borrowernumber, Dumper({
370 action => 'create_fee',
371 borrowernumber => $borrowernumber,
372 accountno => $accountno,
373 amount => $amount,
374 amountoutstanding => $amount,
375 description => $description,
376 accounttype => 'L',
377 itemnumber => $itemnumber,
378 manager_id => $manager_id,
379 }));
385 =head2 manualinvoice
387 &manualinvoice($borrowernumber, $itemnumber, $description, $type,
388 $amount, $note);
390 C<$borrowernumber> is the patron's borrower number.
391 C<$description> is a description of the transaction.
392 C<$type> may be one of C<CS>, C<CB>, C<CW>, C<CF>, C<CL>, C<N>, C<L>,
393 or C<REF>.
394 C<$itemnumber> is the item involved, if pertinent; otherwise, it
395 should be the empty string.
397 =cut
400 # FIXME: In Koha 3.0 , the only account adjustment 'types' passed to this function
401 # are :
402 # 'C' = CREDIT
403 # 'FOR' = FORGIVEN (Formerly 'F', but 'F' is taken to mean 'FINE' elsewhere)
404 # 'N' = New Card fee
405 # 'F' = Fine
406 # 'A' = Account Management fee
407 # 'M' = Sundry
408 # 'L' = Lost Item
411 sub manualinvoice {
412 my ( $borrowernumber, $itemnum, $desc, $type, $amount, $note ) = @_;
413 my $manager_id = 0;
414 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
415 my $dbh = C4::Context->dbh;
416 my $notifyid = 0;
417 my $insert;
418 my $accountno = getnextacctno($borrowernumber);
419 my $amountleft = $amount;
421 # if ( $type eq 'CS'
422 # || $type eq 'CB'
423 # || $type eq 'CW'
424 # || $type eq 'CF'
425 # || $type eq 'CL' )
427 # my $amount2 = $amount * -1; # FIXME - $amount2 = -$amount
428 # $amountleft =
429 # fixcredit( $borrowernumber, $amount2, $itemnum, $type, $user );
431 if ( $type eq 'N' ) {
432 $desc .= " New Card";
434 if ( $type eq 'F' ) {
435 $desc .= " Fine";
437 if ( $type eq 'A' ) {
438 $desc .= " Account Management fee";
440 if ( $type eq 'M' ) {
441 $desc .= " Sundry";
444 if ( $type eq 'L' && $desc eq '' ) {
446 $desc = " Lost Item";
448 # if ( $type eq 'REF' ) {
449 # $desc .= " Cash Refund";
450 # $amountleft = refund( '', $borrowernumber, $amount );
452 if ( ( $type eq 'L' )
453 or ( $type eq 'F' )
454 or ( $type eq 'A' )
455 or ( $type eq 'N' )
456 or ( $type eq 'M' ) )
458 $notifyid = 1;
461 if ( $itemnum ) {
462 $desc .= ' ' . $itemnum;
463 my $sth = $dbh->prepare(
464 'INSERT INTO accountlines
465 (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding, itemnumber,notify_id, note, manager_id)
466 VALUES (?, ?, now(), ?,?, ?,?,?,?,?,?)');
467 $sth->execute($borrowernumber, $accountno, $amount, $desc, $type, $amountleft, $itemnum,$notifyid, $note, $manager_id) || return $sth->errstr;
468 } else {
469 my $sth=$dbh->prepare("INSERT INTO accountlines
470 (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding,notify_id, note, manager_id)
471 VALUES (?, ?, now(), ?, ?, ?, ?,?,?,?)"
473 $sth->execute( $borrowernumber, $accountno, $amount, $desc, $type,
474 $amountleft, $notifyid, $note, $manager_id );
477 if ( C4::Context->preference("FinesLog") ) {
478 logaction("FINES", 'CREATE',$borrowernumber,Dumper({
479 action => 'create_fee',
480 borrowernumber => $borrowernumber,
481 accountno => $accountno,
482 amount => $amount,
483 description => $desc,
484 accounttype => $type,
485 amountoutstanding => $amountleft,
486 notify_id => $notifyid,
487 note => $note,
488 itemnumber => $itemnum,
489 manager_id => $manager_id,
490 }));
493 return 0;
496 =head2 fixcredit #### DEPRECATED
498 $amountleft = &fixcredit($borrowernumber, $data, $barcode, $type, $user);
500 This function is only used internally, not exported.
502 =cut
504 # This function is deprecated in 3.0
506 sub fixcredit {
508 #here we update both the accountoffsets and the account lines
509 my ( $borrowernumber, $data, $barcode, $type, $user ) = @_;
510 my $dbh = C4::Context->dbh;
511 my $newamtos = 0;
512 my $accdata = "";
513 my $amountleft = $data;
514 if ( $barcode ne '' ) {
515 my $item = GetBiblioFromItemNumber( '', $barcode );
516 my $nextaccntno = getnextacctno($borrowernumber);
517 my $query = "SELECT * FROM accountlines WHERE (borrowernumber=?
518 AND itemnumber=? AND amountoutstanding > 0)";
519 if ( $type eq 'CL' ) {
520 $query .= " AND (accounttype = 'L' OR accounttype = 'Rep')";
522 elsif ( $type eq 'CF' ) {
523 $query .= " AND (accounttype = 'F' OR accounttype = 'FU' OR
524 accounttype='Res' OR accounttype='Rent')";
526 elsif ( $type eq 'CB' ) {
527 $query .= " and accounttype='A'";
530 # print $query;
531 my $sth = $dbh->prepare($query);
532 $sth->execute( $borrowernumber, $item->{'itemnumber'} );
533 $accdata = $sth->fetchrow_hashref;
534 $sth->finish;
535 if ( $accdata->{'amountoutstanding'} < $amountleft ) {
536 $newamtos = 0;
537 $amountleft -= $accdata->{'amountoutstanding'};
539 else {
540 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
541 $amountleft = 0;
543 my $thisacct = $accdata->{accountlines_id};
544 my $usth = $dbh->prepare(
545 "UPDATE accountlines SET amountoutstanding= ?
546 WHERE (accountlines_id = ?)"
548 $usth->execute( $newamtos, $thisacct );
549 $usth->finish;
550 $usth = $dbh->prepare(
551 "INSERT INTO accountoffsets
552 (borrowernumber, accountno, offsetaccount, offsetamount)
553 VALUES (?,?,?,?)"
555 $usth->execute( $borrowernumber, $accdata->{'accountno'},
556 $nextaccntno, $newamtos );
557 $usth->finish;
560 # begin transaction
561 my $nextaccntno = getnextacctno($borrowernumber);
563 # get lines with outstanding amounts to offset
564 my $sth = $dbh->prepare(
565 "SELECT * FROM accountlines
566 WHERE (borrowernumber = ?) AND (amountoutstanding >0)
567 ORDER BY date"
569 $sth->execute($borrowernumber);
571 # print $query;
572 # offset transactions
573 while ( ( $accdata = $sth->fetchrow_hashref ) and ( $amountleft > 0 ) ) {
574 if ( $accdata->{'amountoutstanding'} < $amountleft ) {
575 $newamtos = 0;
576 $amountleft -= $accdata->{'amountoutstanding'};
578 else {
579 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
580 $amountleft = 0;
582 my $thisacct = $accdata->{accountlines_id};
583 my $usth = $dbh->prepare(
584 "UPDATE accountlines SET amountoutstanding= ?
585 WHERE (accountlines_id = ?)"
587 $usth->execute( $newamtos, $thisacct );
588 $usth->finish;
589 $usth = $dbh->prepare(
590 "INSERT INTO accountoffsets
591 (borrowernumber, accountno, offsetaccount, offsetamount)
592 VALUE (?,?,?,?)"
594 $usth->execute( $borrowernumber, $accdata->{'accountno'},
595 $nextaccntno, $newamtos );
596 $usth->finish;
598 $sth->finish;
599 $type = "Credit " . $type;
600 UpdateStats( $user, $type, $data, $user, '', '', $borrowernumber );
601 $amountleft *= -1;
602 return ($amountleft);
606 =head2 refund
608 #FIXME : DEPRECATED SUB
609 This subroutine tracks payments and/or credits against fines/charges
610 using the accountoffsets table, which is not used consistently in
611 Koha's fines management, and so is not used in 3.0
613 =cut
615 sub refund {
617 #here we update both the accountoffsets and the account lines
618 my ( $borrowernumber, $data ) = @_;
619 my $dbh = C4::Context->dbh;
620 my $newamtos = 0;
621 my $accdata = "";
622 my $amountleft = $data * -1;
624 # begin transaction
625 my $nextaccntno = getnextacctno($borrowernumber);
627 # get lines with outstanding amounts to offset
628 my $sth = $dbh->prepare(
629 "SELECT * FROM accountlines
630 WHERE (borrowernumber = ?) AND (amountoutstanding<0)
631 ORDER BY date"
633 $sth->execute($borrowernumber);
635 # print $amountleft;
636 # offset transactions
637 while ( ( $accdata = $sth->fetchrow_hashref ) and ( $amountleft < 0 ) ) {
638 if ( $accdata->{'amountoutstanding'} > $amountleft ) {
639 $newamtos = 0;
640 $amountleft -= $accdata->{'amountoutstanding'};
642 else {
643 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
644 $amountleft = 0;
647 # print $amountleft;
648 my $thisacct = $accdata->{accountlines_id};
649 my $usth = $dbh->prepare(
650 "UPDATE accountlines SET amountoutstanding= ?
651 WHERE (accountlines_id = ?)"
653 $usth->execute( $newamtos, $thisacct );
654 $usth->finish;
655 $usth = $dbh->prepare(
656 "INSERT INTO accountoffsets
657 (borrowernumber, accountno, offsetaccount, offsetamount)
658 VALUES (?,?,?,?)"
660 $usth->execute( $borrowernumber, $accdata->{'accountno'},
661 $nextaccntno, $newamtos );
662 $usth->finish;
664 $sth->finish;
665 return ($amountleft);
668 sub getcharges {
669 my ( $borrowerno, $timestamp, $accountno ) = @_;
670 my $dbh = C4::Context->dbh;
671 my $timestamp2 = $timestamp - 1;
672 my $query = "";
673 my $sth = $dbh->prepare(
674 "SELECT * FROM accountlines WHERE borrowernumber=? AND accountno = ?"
676 $sth->execute( $borrowerno, $accountno );
678 my @results;
679 while ( my $data = $sth->fetchrow_hashref ) {
680 push @results,$data;
682 return (@results);
685 sub ModNote {
686 my ( $accountlines_id, $note ) = @_;
687 my $dbh = C4::Context->dbh;
688 my $sth = $dbh->prepare('UPDATE accountlines SET note = ? WHERE accountlines_id = ?');
689 $sth->execute( $note, $accountlines_id );
692 sub getcredits {
693 my ( $date, $date2 ) = @_;
694 my $dbh = C4::Context->dbh;
695 my $sth = $dbh->prepare(
696 "SELECT * FROM accountlines,borrowers
697 WHERE amount < 0 AND accounttype <> 'Pay' AND accountlines.borrowernumber = borrowers.borrowernumber
698 AND timestamp >=TIMESTAMP(?) AND timestamp < TIMESTAMP(?)"
701 $sth->execute( $date, $date2 );
702 my @results;
703 while ( my $data = $sth->fetchrow_hashref ) {
704 $data->{'date'} = $data->{'timestamp'};
705 push @results,$data;
707 return (@results);
711 sub getrefunds {
712 my ( $date, $date2 ) = @_;
713 my $dbh = C4::Context->dbh;
715 my $sth = $dbh->prepare(
716 "SELECT *,timestamp AS datetime
717 FROM accountlines,borrowers
718 WHERE (accounttype = 'REF'
719 AND accountlines.borrowernumber = borrowers.borrowernumber
720 AND date >=? AND date <?)"
723 $sth->execute( $date, $date2 );
725 my @results;
726 while ( my $data = $sth->fetchrow_hashref ) {
727 push @results,$data;
730 return (@results);
733 sub ReversePayment {
734 my ( $accountlines_id ) = @_;
735 my $dbh = C4::Context->dbh;
737 my $sth = $dbh->prepare('SELECT * FROM accountlines WHERE accountlines_id = ?');
738 $sth->execute( $accountlines_id );
739 my $row = $sth->fetchrow_hashref();
740 my $amount_outstanding = $row->{'amountoutstanding'};
742 if ( $amount_outstanding <= 0 ) {
743 $sth = $dbh->prepare('UPDATE accountlines SET amountoutstanding = amount * -1, description = CONCAT( description, " Reversed -" ) WHERE accountlines_id = ?');
744 $sth->execute( $accountlines_id );
745 } else {
746 $sth = $dbh->prepare('UPDATE accountlines SET amountoutstanding = 0, description = CONCAT( description, " Reversed -" ) WHERE accountlines_id = ?');
747 $sth->execute( $accountlines_id );
750 if ( C4::Context->preference("FinesLog") ) {
751 my $manager_id = 0;
752 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
754 if ( $amount_outstanding <= 0 ) {
755 $row->{'amountoutstanding'} *= -1;
756 } else {
757 $row->{'amountoutstanding'} = '0';
759 $row->{'description'} .= ' Reversed -';
760 logaction("FINES", 'MODIFY', $row->{'borrowernumber'}, Dumper({
761 action => 'reverse_fee_payment',
762 borrowernumber => $row->{'borrowernumber'},
763 old_amountoutstanding => $row->{'amountoutstanding'},
764 new_amountoutstanding => 0 - $amount_outstanding,,
765 accountlines_id => $row->{'accountlines_id'},
766 accountno => $row->{'accountno'},
767 manager_id => $manager_id,
768 }));
774 =head2 recordpayment_selectaccts
776 recordpayment_selectaccts($borrowernumber, $payment,$accts);
778 Record payment by a patron. C<$borrowernumber> is the patron's
779 borrower number. C<$payment> is a floating-point number, giving the
780 amount that was paid. C<$accts> is an array ref to a list of
781 accountnos which the payment can be recorded against
783 Amounts owed are paid off oldest first. That is, if the patron has a
784 $1 fine from Feb. 1, another $1 fine from Mar. 1, and makes a payment
785 of $1.50, then the oldest fine will be paid off in full, and $0.50
786 will be credited to the next one.
788 =cut
790 sub recordpayment_selectaccts {
791 my ( $borrowernumber, $amount, $accts ) = @_;
793 my $dbh = C4::Context->dbh;
794 my $newamtos = 0;
795 my $accdata = q{};
796 my $branch = C4::Context->userenv->{branch};
797 my $amountleft = $amount;
798 my $manager_id = 0;
799 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
800 my $sql = 'SELECT * FROM accountlines WHERE (borrowernumber = ?) ' .
801 'AND (amountoutstanding<>0) ';
802 if (@{$accts} ) {
803 $sql .= ' AND accountno IN ( ' . join ',', @{$accts};
804 $sql .= ' ) ';
806 $sql .= ' ORDER BY date';
807 # begin transaction
808 my $nextaccntno = getnextacctno($borrowernumber);
810 # get lines with outstanding amounts to offset
811 my $rows = $dbh->selectall_arrayref($sql, { Slice => {} }, $borrowernumber);
813 # offset transactions
814 my $sth = $dbh->prepare('UPDATE accountlines SET amountoutstanding= ? ' .
815 'WHERE accountlines_id=?');
817 my @ids;
818 for my $accdata ( @{$rows} ) {
819 if ($amountleft == 0) {
820 last;
822 if ( $accdata->{amountoutstanding} < $amountleft ) {
823 $newamtos = 0;
824 $amountleft -= $accdata->{amountoutstanding};
826 else {
827 $newamtos = $accdata->{amountoutstanding} - $amountleft;
828 $amountleft = 0;
830 my $thisacct = $accdata->{accountlines_id};
831 $sth->execute( $newamtos, $thisacct );
833 if ( C4::Context->preference("FinesLog") ) {
834 logaction("FINES", 'MODIFY', $borrowernumber, Dumper({
835 action => 'fee_payment',
836 borrowernumber => $borrowernumber,
837 old_amountoutstanding => $accdata->{'amountoutstanding'},
838 new_amountoutstanding => $newamtos,
839 amount_paid => $accdata->{'amountoutstanding'} - $newamtos,
840 accountlines_id => $accdata->{'accountlines_id'},
841 accountno => $accdata->{'accountno'},
842 manager_id => $manager_id,
843 }));
844 push( @ids, $accdata->{'accountlines_id'} );
849 # create new line
850 $sql = 'INSERT INTO accountlines ' .
851 '(borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding,manager_id) ' .
852 q|VALUES (?,?,now(),?,'Payment,thanks','Pay',?,?)|;
853 $dbh->do($sql,{},$borrowernumber, $nextaccntno, 0 - $amount, 0 - $amountleft, $manager_id );
854 UpdateStats( $branch, 'payment', $amount, '', '', '', $borrowernumber, $nextaccntno );
856 if ( C4::Context->preference("FinesLog") ) {
857 logaction("FINES", 'CREATE',$borrowernumber,Dumper({
858 action => 'create_payment',
859 borrowernumber => $borrowernumber,
860 accountno => $nextaccntno,
861 amount => 0 - $amount,
862 amountoutstanding => 0 - $amountleft,
863 accounttype => 'Pay',
864 accountlines_paid => \@ids,
865 manager_id => $manager_id,
866 }));
869 return;
872 # makepayment needs to be fixed to handle partials till then this separate subroutine
873 # fills in
874 sub makepartialpayment {
875 my ( $accountlines_id, $borrowernumber, $accountno, $amount, $user, $branch ) = @_;
876 my $manager_id = 0;
877 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
878 if (!$amount || $amount < 0) {
879 return;
881 my $dbh = C4::Context->dbh;
883 my $nextaccntno = getnextacctno($borrowernumber);
884 my $newamtos = 0;
886 my $data = $dbh->selectrow_hashref(
887 'SELECT * FROM accountlines WHERE accountlines_id=?',undef,$accountlines_id);
888 my $new_outstanding = $data->{amountoutstanding} - $amount;
890 my $update = 'UPDATE accountlines SET amountoutstanding = ? WHERE accountlines_id = ? ';
891 $dbh->do( $update, undef, $new_outstanding, $accountlines_id);
893 if ( C4::Context->preference("FinesLog") ) {
894 logaction("FINES", 'MODIFY', $borrowernumber, Dumper({
895 action => 'fee_payment',
896 borrowernumber => $borrowernumber,
897 old_amountoutstanding => $data->{'amountoutstanding'},
898 new_amountoutstanding => $new_outstanding,
899 amount_paid => $data->{'amountoutstanding'} - $new_outstanding,
900 accountlines_id => $data->{'accountlines_id'},
901 accountno => $data->{'accountno'},
902 manager_id => $manager_id,
903 }));
906 # create new line
907 my $insert = 'INSERT INTO accountlines (borrowernumber, accountno, date, amount, '
908 . 'description, accounttype, amountoutstanding, itemnumber, manager_id) '
909 . ' VALUES (?, ?, now(), ?, ?, ?, 0, ?, ?)';
911 $dbh->do( $insert, undef, $borrowernumber, $nextaccntno, 0 - $amount,
912 "Payment, thanks - $user", 'Pay', $data->{'itemnumber'}, $manager_id);
914 UpdateStats( $user, 'payment', $amount, '', '', '', $borrowernumber, $accountno );
916 if ( C4::Context->preference("FinesLog") ) {
917 logaction("FINES", 'CREATE',$borrowernumber,Dumper({
918 action => 'create_payment',
919 borrowernumber => $user,
920 accountno => $nextaccntno,
921 amount => 0 - $amount,
922 accounttype => 'Pay',
923 itemnumber => $data->{'itemnumber'},
924 accountlines_paid => [ $data->{'accountlines_id'} ],
925 manager_id => $manager_id,
926 }));
929 return;
932 =head2 WriteOffFee
934 WriteOff( $borrowernumber, $accountline_id, $itemnum, $accounttype, $amount, $branch );
936 Write off a fine for a patron.
937 C<$borrowernumber> is the patron's borrower number.
938 C<$accountline_id> is the accountline_id of the fee to write off.
939 C<$itemnum> is the itemnumber of of item whose fine is being written off.
940 C<$accounttype> is the account type of the fine being written off.
941 C<$amount> is a floating-point number, giving the amount that is being written off.
942 C<$branch> is the branchcode of the library where the writeoff occurred.
944 =cut
946 sub WriteOffFee {
947 my ( $borrowernumber, $accountlines_id, $itemnum, $accounttype, $amount, $branch ) = @_;
948 $branch ||= C4::Context->userenv->{branch};
949 my $manager_id = 0;
950 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
952 # if no item is attached to fine, make sure to store it as a NULL
953 $itemnum ||= undef;
955 my ( $sth, $query );
956 my $dbh = C4::Context->dbh();
958 $query = "
959 UPDATE accountlines SET amountoutstanding = 0
960 WHERE accountlines_id = ? AND borrowernumber = ?
962 $sth = $dbh->prepare( $query );
963 $sth->execute( $accountlines_id, $borrowernumber );
965 if ( C4::Context->preference("FinesLog") ) {
966 logaction("FINES", 'MODIFY', $borrowernumber, Dumper({
967 action => 'fee_writeoff',
968 borrowernumber => $borrowernumber,
969 accountlines_id => $accountlines_id,
970 manager_id => $manager_id,
971 }));
974 $query ="
975 INSERT INTO accountlines
976 ( borrowernumber, accountno, itemnumber, date, amount, description, accounttype, manager_id )
977 VALUES ( ?, ?, ?, NOW(), ?, 'Writeoff', 'W', ? )
979 $sth = $dbh->prepare( $query );
980 my $acct = getnextacctno($borrowernumber);
981 $sth->execute( $borrowernumber, $acct, $itemnum, $amount, $manager_id );
983 if ( C4::Context->preference("FinesLog") ) {
984 logaction("FINES", 'CREATE',$borrowernumber,Dumper({
985 action => 'create_writeoff',
986 borrowernumber => $borrowernumber,
987 accountno => $acct,
988 amount => 0 - $amount,
989 accounttype => 'W',
990 itemnumber => $itemnum,
991 accountlines_paid => [ $accountlines_id ],
992 manager_id => $manager_id,
993 }));
996 UpdateStats( $branch, 'writeoff', $amount, q{}, q{}, q{}, $borrowernumber );
1000 END { } # module clean-up code here (global destructor)
1003 __END__
1005 =head1 SEE ALSO
1007 DBI(3)
1009 =cut