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>.
25 use C4
::Log
qw( logaction );
26 use C4
::Stats
qw( UpdateStats );
28 use Koha
::Account
::Lines
;
29 use Koha
::Account
::Offsets
;
30 use Koha
::DateUtils
qw( dt_from_string );
34 Koha::Accounts - Module for managing payments and fees for patrons
39 my ( $class, $params ) = @_;
41 Carp
::croak
("No patron id passed in!") unless $params->{patron_id
};
43 return bless( $params, $class );
48 This method allows payments to be made against fees/fines
50 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
55 description => $description,
56 library_id => $branchcode,
57 lines => $lines, # Arrayref of Koha::Account::Line objects to pay
58 account_type => $type, # accounttype code
59 offset_type => $offset_type, # offset type code
66 my ( $self, $params ) = @_;
68 my $amount = $params->{amount
};
69 my $sip = $params->{sip
};
70 my $description = $params->{description
};
71 my $note = $params->{note
} || q{};
72 my $library_id = $params->{library_id
};
73 my $lines = $params->{lines
};
74 my $type = $params->{type
} || 'payment';
75 my $account_type = $params->{account_type
};
76 my $offset_type = $params->{offset_type
} || $type eq 'writeoff' ?
'Writeoff' : 'Payment';
78 my $userenv = C4
::Context
->userenv;
80 # We should remove accountno, it is no longer needed
81 my $last = Koha
::Account
::Lines
->search(
83 borrowernumber
=> $self->{patron_id
}
86 order_by
=> 'accountno'
89 my $accountno = $last ?
$last->accountno + 1 : 1;
91 my $manager_id = $userenv ?
$userenv->{number
} : 0;
93 my @fines_paid; # List of account lines paid on with this payment
95 my $balance_remaining = $amount; # Set it now so we can adjust the amount if necessary
96 $balance_remaining ||= 0;
100 # We were passed a specific line to pay
101 foreach my $fine ( @
$lines ) {
103 $fine->amountoutstanding > $balance_remaining
105 : $fine->amountoutstanding;
107 my $old_amountoutstanding = $fine->amountoutstanding;
108 my $new_amountoutstanding = $old_amountoutstanding - $amount_to_pay;
109 $fine->amountoutstanding($new_amountoutstanding)->store();
110 $balance_remaining = $balance_remaining - $amount_to_pay;
112 if ( $fine->itemnumber && $fine->accounttype && ( $fine->accounttype eq 'Rep' || $fine->accounttype eq 'L' ) )
114 C4
::Circulation
::ReturnLostItem
( $self->{patron_id
}, $fine->itemnumber );
117 my $account_offset = Koha
::Account
::Offset
->new(
119 debit_id
=> $fine->id,
120 type
=> $offset_type,
121 amount
=> $amount_to_pay * -1,
124 push( @account_offsets, $account_offset );
126 if ( C4
::Context
->preference("FinesLog") ) {
132 action
=> 'fee_payment',
133 borrowernumber
=> $fine->borrowernumber,
134 old_amountoutstanding
=> $old_amountoutstanding,
135 new_amountoutstanding
=> 0,
136 amount_paid
=> $old_amountoutstanding,
137 accountlines_id
=> $fine->id,
138 accountno
=> $fine->accountno,
139 manager_id
=> $manager_id,
144 push( @fines_paid, $fine->id );
148 # Were not passed a specific line to pay, or the payment was for more
149 # than the what was owed on the given line. In that case pay down other
150 # lines with remaining balance.
151 my @outstanding_fines;
152 @outstanding_fines = Koha
::Account
::Lines
->search(
154 borrowernumber
=> $self->{patron_id
},
155 amountoutstanding
=> { '>' => 0 },
157 ) if $balance_remaining > 0;
159 foreach my $fine (@outstanding_fines) {
161 $fine->amountoutstanding > $balance_remaining
163 : $fine->amountoutstanding;
165 my $old_amountoutstanding = $fine->amountoutstanding;
166 $fine->amountoutstanding( $old_amountoutstanding - $amount_to_pay );
169 my $account_offset = Koha
::Account
::Offset
->new(
171 debit_id
=> $fine->id,
172 type
=> $offset_type,
173 amount
=> $amount_to_pay * -1,
176 push( @account_offsets, $account_offset );
178 if ( C4
::Context
->preference("FinesLog") ) {
184 action
=> "fee_$type",
185 borrowernumber
=> $fine->borrowernumber,
186 old_amountoutstanding
=> $old_amountoutstanding,
187 new_amountoutstanding
=> $fine->amountoutstanding,
188 amount_paid
=> $amount_to_pay,
189 accountlines_id
=> $fine->id,
190 accountno
=> $fine->accountno,
191 manager_id
=> $manager_id,
196 push( @fines_paid, $fine->id );
199 $balance_remaining = $balance_remaining - $amount_to_pay;
200 last unless $balance_remaining > 0;
204 $type eq 'writeoff' ?
'W'
205 : defined($sip) ?
"Pay$sip"
208 $description ||= $type eq 'writeoff' ?
'Writeoff' : q{};
210 my $payment = Koha
::Account
::Line
->new(
212 borrowernumber
=> $self->{patron_id
},
213 accountno
=> $accountno,
214 date
=> dt_from_string
(),
215 amount
=> 0 - $amount,
216 description
=> $description,
217 accounttype
=> $account_type,
218 amountoutstanding
=> 0 - $balance_remaining,
219 manager_id
=> $manager_id,
224 foreach my $o ( @account_offsets ) {
225 $o->credit_id( $payment->id() );
229 $library_id ||= $userenv ?
$userenv->{'branch'} : undef;
233 branch
=> $library_id,
236 borrowernumber
=> $self->{patron_id
},
237 accountno
=> $accountno,
241 if ( C4
::Context
->preference("FinesLog") ) {
247 action
=> "create_$type",
248 borrowernumber
=> $self->{patron_id
},
249 accountno
=> $accountno,
250 amount
=> 0 - $amount,
251 amountoutstanding
=> 0 - $balance_remaining,
252 accounttype
=> $account_type,
253 accountlines_paid
=> \
@fines_paid,
254 manager_id
=> $manager_id,
265 my $balance = $self->balance
267 Return the balance (sum of amountoutstanding columns)
273 my $fines = Koha
::Account
::Lines
->search(
275 borrowernumber
=> $self->{patron_id
},
278 select => [ { sum
=> 'amountoutstanding' } ],
279 as
=> ['total_amountoutstanding'],
283 ?
$fines->next->get_column('total_amountoutstanding')
291 Kyle M Hall <kyle.m.hall@gmail.com>