1 package Koha::Account::Line;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 use C4::Log qw(logaction);
25 use Koha::Account::Offsets;
27 use Koha::Exceptions::Account;
30 use base qw(Koha::Object);
34 Koha::Account::Line - Koha accountline Object class
44 Return the item linked to this account line if exists
50 my $rs = $self->_result->itemnumber;
51 return Koha::Item->_new_from_dbic( $rs );
56 $payment_accountline->void();
63 # Make sure it is a payment we are voiding
64 return unless $self->amount < 0;
67 Koha::Account::Offsets->search(
68 { credit_id => $self->id, amount => { '<' => 0 } } );
70 $self->_result->result_source->schema->txn_do(
72 foreach my $account_offset (@account_offsets) {
74 Koha::Account::Lines->find( $account_offset->debit_id );
76 next unless $fee_paid;
78 my $amount_paid = $account_offset->amount * -1; # amount paid is stored as a negative amount
79 my $new_amount = $fee_paid->amountoutstanding + $amount_paid;
80 $fee_paid->amountoutstanding($new_amount);
83 Koha::Account::Offset->new(
85 credit_id => $self->id,
86 debit_id => $fee_paid->id,
87 amount => $amount_paid,
88 type => 'Void Payment',
93 if ( C4::Context->preference("FinesLog") ) {
96 $self->borrowernumber,
99 action => 'void_payment',
100 borrowernumber => $self->borrowernumber,
101 amount => $self->amount,
102 amountoutstanding => $self->amountoutstanding,
103 description => $self->description,
104 accounttype => $self->accounttype,
105 payment_type => $self->payment_type,
107 itemnumber => $self->itemnumber,
108 manager_id => $self->manager_id,
110 [ map { $_->unblessed } @account_offsets ],
118 accounttype => 'VOID',
119 amountoutstanding => 0,
131 my $debits = $account->outstanding_debits;
132 my $outstanding_amount = $credit->apply({ debits => $debits, [ offset_type => $offset_type ] });
137 my ( $self, $params ) = @_;
139 my $debits = $params->{debits};
140 my $offset_type = $params->{offset_type} // 'credit_applied';
142 unless ( $self->is_credit ) {
143 Koha::Exceptions::Account::IsNotCredit->throw(
144 error => 'Account line ' . $self->id . ' is not a credit'
148 my $available_credit = $self->amountoutstanding * -1;
150 unless ( $available_credit > 0 ) {
151 Koha::Exceptions::Account::NoAvailableCredit->throw(
152 error => 'Outstanding credit is ' . $available_credit . ' and cannot be applied'
156 my $schema = Koha::Database->new->schema;
158 $schema->txn_do( sub {
159 while ( my $debit = $debits->next ) {
161 unless ( $debit->is_debit ) {
162 Koha::Exceptions::Account::IsNotDebit->throw(
163 error => 'Account line ' . $debit->id . 'is not a debit'
166 my $amount_to_cancel;
167 my $owed = $debit->amountoutstanding;
169 if ( $available_credit >= $owed ) {
170 $amount_to_cancel = $owed;
172 else { # $available_credit < $debit->amountoutstanding
173 $amount_to_cancel = $available_credit;
176 # record the account offset
177 Koha::Account::Offset->new(
178 { credit_id => $self->id,
179 debit_id => $debit->id,
180 amount => $amount_to_cancel,
181 type => $offset_type,
185 $available_credit -= $amount_to_cancel;
187 $self->amountoutstanding( $available_credit * -1 )->store;
188 $debit->amountoutstanding( $owed - $amount_to_cancel )->store;
192 return $available_credit;
197 my $bool = $line->is_credit;
204 return ( $self->amount < 0 );
209 my $bool = $line->is_debit;
216 return !$self->is_credit;
219 =head2 Internal methods
228 return 'Accountline';