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>.
24 use List
::MoreUtils
qw( uniq );
26 use C4
::Log
qw( logaction );
27 use C4
::Stats
qw( UpdateStats );
29 use Koha
::Account
::Lines
;
30 use Koha
::Account
::Offsets
;
31 use Koha
::DateUtils
qw( dt_from_string );
35 Koha::Accounts - Module for managing payments and fees for patrons
40 my ( $class, $params ) = @_;
42 Carp
::croak
("No patron id passed in!") unless $params->{patron_id
};
44 return bless( $params, $class );
49 This method allows payments to be made against fees/fines
51 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
56 description => $description,
57 library_id => $branchcode,
58 lines => $lines, # Arrayref of Koha::Account::Line objects to pay
59 account_type => $type, # accounttype code
60 offset_type => $offset_type, # offset type code
67 my ( $self, $params ) = @_;
69 my $amount = $params->{amount
};
70 my $sip = $params->{sip
};
71 my $description = $params->{description
};
72 my $note = $params->{note
} || q{};
73 my $library_id = $params->{library_id
};
74 my $lines = $params->{lines
};
75 my $type = $params->{type
} || 'payment';
76 my $account_type = $params->{account_type
};
77 my $offset_type = $params->{offset_type
} || $type eq 'writeoff' ?
'Writeoff' : 'Payment';
79 my $userenv = C4
::Context
->userenv;
81 # We should remove accountno, it is no longer needed
82 my $last = Koha
::Account
::Lines
->search(
84 borrowernumber
=> $self->{patron_id
}
87 order_by
=> 'accountno'
90 my $accountno = $last ?
$last->accountno + 1 : 1;
92 my $manager_id = $userenv ?
$userenv->{number
} : 0;
94 my @fines_paid; # List of account lines paid on with this payment
96 my $balance_remaining = $amount; # Set it now so we can adjust the amount if necessary
97 $balance_remaining ||= 0;
101 # We were passed a specific line to pay
102 foreach my $fine ( @
$lines ) {
104 $fine->amountoutstanding > $balance_remaining
106 : $fine->amountoutstanding;
108 my $old_amountoutstanding = $fine->amountoutstanding;
109 my $new_amountoutstanding = $old_amountoutstanding - $amount_to_pay;
110 $fine->amountoutstanding($new_amountoutstanding)->store();
111 $balance_remaining = $balance_remaining - $amount_to_pay;
113 if ( $fine->itemnumber && $fine->accounttype && ( $fine->accounttype eq 'Rep' || $fine->accounttype eq 'L' ) )
115 C4
::Circulation
::ReturnLostItem
( $self->{patron_id
}, $fine->itemnumber );
118 my $account_offset = Koha
::Account
::Offset
->new(
120 debit_id
=> $fine->id,
121 type
=> $offset_type,
122 amount
=> $amount_to_pay * -1,
125 push( @account_offsets, $account_offset );
127 if ( C4
::Context
->preference("FinesLog") ) {
133 action
=> 'fee_payment',
134 borrowernumber
=> $fine->borrowernumber,
135 old_amountoutstanding
=> $old_amountoutstanding,
136 new_amountoutstanding
=> 0,
137 amount_paid
=> $old_amountoutstanding,
138 accountlines_id
=> $fine->id,
139 accountno
=> $fine->accountno,
140 manager_id
=> $manager_id,
145 push( @fines_paid, $fine->id );
149 # Were not passed a specific line to pay, or the payment was for more
150 # than the what was owed on the given line. In that case pay down other
151 # lines with remaining balance.
152 my @outstanding_fines;
153 @outstanding_fines = Koha
::Account
::Lines
->search(
155 borrowernumber
=> $self->{patron_id
},
156 amountoutstanding
=> { '>' => 0 },
158 ) if $balance_remaining > 0;
160 foreach my $fine (@outstanding_fines) {
162 $fine->amountoutstanding > $balance_remaining
164 : $fine->amountoutstanding;
166 my $old_amountoutstanding = $fine->amountoutstanding;
167 $fine->amountoutstanding( $old_amountoutstanding - $amount_to_pay );
170 my $account_offset = Koha
::Account
::Offset
->new(
172 debit_id
=> $fine->id,
173 type
=> $offset_type,
174 amount
=> $amount_to_pay * -1,
177 push( @account_offsets, $account_offset );
179 if ( C4
::Context
->preference("FinesLog") ) {
185 action
=> "fee_$type",
186 borrowernumber
=> $fine->borrowernumber,
187 old_amountoutstanding
=> $old_amountoutstanding,
188 new_amountoutstanding
=> $fine->amountoutstanding,
189 amount_paid
=> $amount_to_pay,
190 accountlines_id
=> $fine->id,
191 accountno
=> $fine->accountno,
192 manager_id
=> $manager_id,
197 push( @fines_paid, $fine->id );
200 $balance_remaining = $balance_remaining - $amount_to_pay;
201 last unless $balance_remaining > 0;
205 $type eq 'writeoff' ?
'W'
206 : defined($sip) ?
"Pay$sip"
209 $description ||= $type eq 'writeoff' ?
'Writeoff' : q{};
211 my $payment = Koha
::Account
::Line
->new(
213 borrowernumber
=> $self->{patron_id
},
214 accountno
=> $accountno,
215 date
=> dt_from_string
(),
216 amount
=> 0 - $amount,
217 description
=> $description,
218 accounttype
=> $account_type,
219 amountoutstanding
=> 0 - $balance_remaining,
220 manager_id
=> $manager_id,
225 foreach my $o ( @account_offsets ) {
226 $o->credit_id( $payment->id() );
230 $library_id ||= $userenv ?
$userenv->{'branch'} : undef;
234 branch
=> $library_id,
237 borrowernumber
=> $self->{patron_id
},
238 accountno
=> $accountno,
242 if ( C4
::Context
->preference("FinesLog") ) {
248 action
=> "create_$type",
249 borrowernumber
=> $self->{patron_id
},
250 accountno
=> $accountno,
251 amount
=> 0 - $amount,
252 amountoutstanding
=> 0 - $balance_remaining,
253 accounttype
=> $account_type,
254 accountlines_paid
=> \
@fines_paid,
255 manager_id
=> $manager_id,
266 my $balance = $self->balance
268 Return the balance (sum of amountoutstanding columns)
274 my $fines = Koha
::Account
::Lines
->search(
276 borrowernumber
=> $self->{patron_id
},
279 select => [ { sum
=> 'amountoutstanding' } ],
280 as
=> ['total_amountoutstanding'],
284 return ( $fines->count )
285 ?
$fines->next->get_column('total_amountoutstanding') + 0
289 =head3 non_issues_charges
291 my $non_issues_charges = $self->non_issues_charges
293 Calculates amount immediately owing by the patron - non-issue charges.
295 Charges exempt from non-issue are:
296 * Res (holds) if HoldsInNoissuesCharge syspref is set to false
297 * Rent (rental) if RentalsInNoissuesCharge syspref is set to false
298 * Manual invoices if ManInvInNoissuesCharge syspref is set to false
302 sub non_issues_charges
{
305 # FIXME REMOVE And add a warning in the about page + update DB if length(MANUAL_INV) > 5
306 my $ACCOUNT_TYPE_LENGTH = 5; # this is plain ridiculous...
309 push @not_fines, 'Res'
310 unless C4
::Context
->preference('HoldsInNoissuesCharge');
311 push @not_fines, 'Rent'
312 unless C4
::Context
->preference('RentalsInNoissuesCharge');
313 unless ( C4
::Context
->preference('ManInvInNoissuesCharge') ) {
314 my $dbh = C4
::Context
->dbh;
317 $dbh->selectcol_arrayref(q
|
318 SELECT authorised_value FROM authorised_values WHERE category
= 'MANUAL_INV'
322 @not_fines = map { substr( $_, 0, $ACCOUNT_TYPE_LENGTH ) } uniq
(@not_fines);
324 my $non_issues_charges = Koha
::Account
::Lines
->search(
326 borrowernumber
=> $self->{patron_id
},
327 accounttype
=> { -not_in
=> \
@not_fines }
330 select => [ { sum
=> 'amountoutstanding' } ],
331 as
=> ['non_issues_charges'],
334 return $non_issues_charges->count
335 ?
$non_issues_charges->next->get_column('non_issues_charges') + 0
343 Kyle M Hall <kyle.m.hall@gmail.com>