Bug 14598 [QA Followup] - Make unit test pass
[koha.git] / Koha / Account.pm
blobb7341b10d4e75e07c005aa4adfb366043390b6a2
1 package Koha::Account;
3 # Copyright 2016 ByWater Solutions
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>.
20 use Modern::Perl;
22 use Carp;
23 use Data::Dumper;
25 use C4::Log qw( logaction );
26 use C4::Stats qw( UpdateStats );
28 use Koha::Account::Line;
29 use Koha::Account::Lines;
30 use Koha::DateUtils qw( dt_from_string );
32 =head1 NAME
34 Koha::Accounts - Module for managing payments and fees for patrons
36 =cut
38 sub new {
39 my ( $class, $params ) = @_;
41 Carp::croak("No patron id passed in!") unless $params->{patron_id};
43 return bless( $params, $class );
46 =head2 pay
48 This method allows payments to be made against feees
50 =cut
52 sub pay {
53 my ( $self, $params ) = @_;
55 my $amount = $params->{amount};
56 my $sip = $params->{sip};
57 my $note = $params->{note} || q{};
59 my $userenv = C4::Context->userenv;
61 # We should remove accountno, it is no longer needed
62 my $last = Koha::Account::Lines->search(
64 borrowernumber => $self->{patron_id}
67 order_by => 'accountno'
69 )->next();
70 my $accountno = $last ? $last->accountno + 1 : 1;
72 my $manager_id = $userenv ? $userenv->{number} : 0;
74 my @outstanding_fines = Koha::Account::Lines->search(
76 borrowernumber => $self->{patron_id},
77 amountoutstanding => { '>' => 0 },
81 my $balance_remaining = $amount;
82 my @fines_paid;
83 foreach my $fine (@outstanding_fines) {
84 my $amount_to_pay =
85 $fine->amountoutstanding > $balance_remaining
86 ? $balance_remaining
87 : $fine->amountoutstanding;
89 my $old_amountoutstanding = $fine->amountoutstanding;
90 $fine->amountoutstanding( $old_amountoutstanding - $amount_to_pay );
91 $fine->store();
93 if ( C4::Context->preference("FinesLog") ) {
94 logaction(
95 "FINES", 'MODIFY',
96 $self->{patron_id},
97 Dumper(
99 action => 'fee_payment',
100 borrowernumber => $fine->borrowernumber,
101 old_amountoutstanding => $old_amountoutstanding,
102 new_amountoutstanding => $fine->amountoutstanding,
103 amount_paid => $amount_to_pay,
104 accountlines_id => $fine->id,
105 accountno => $fine->accountno,
106 manager_id => $manager_id,
107 note => $note,
111 push( @fines_paid, $fine->id );
114 $balance_remaining = $balance_remaining - $amount_to_pay;
115 last unless $balance_remaining > 0;
118 my $account_type = defined($sip) ? "Pay$sip" : 'Pay';
120 my $payment = Koha::Account::Line->new(
122 borrowernumber => $self->{patron_id},
123 accountno => $accountno,
124 date => dt_from_string(),
125 amount => 0 - $amount,
126 description => q{},
127 accounttype => $account_type,
128 amountoutstanding => 0 - $balance_remaining,
129 manager_id => $manager_id,
130 note => $note,
132 )->store();
134 my $branch = $userenv ? $userenv->{'branch'} : undef;
135 UpdateStats(
137 branch => $branch,
138 type => 'payment',
139 amount => $amount,
140 borrowernumber => $self->{patron_id},
141 accountno => $accountno,
145 if ( C4::Context->preference("FinesLog") ) {
146 logaction(
147 "FINES", 'CREATE',
148 $self->{patron_id},
149 Dumper(
151 action => 'create_payment',
152 borrowernumber => $self->{patron_id},
153 accountno => $accountno,
154 amount => 0 - $amount,
155 amountoutstanding => 0 - $balance_remaining,
156 accounttype => 'Pay',
157 accountlines_paid => \@fines_paid,
158 manager_id => $manager_id,
167 =head1 AUTHOR
169 Kyle M Hall <kyle.m.hall@gmail.com>
171 =cut