View | Details | Raw Unified | Return to bug 20997
Collapse All | Expand All

(-)a/Koha/Account/Line.pm (-2 / +83 lines)
Lines 22-30 use Data::Dumper; Link Here
22
22
23
use C4::Log qw(logaction);
23
use C4::Log qw(logaction);
24
24
25
use Koha::Account::Offsets;
25
use Koha::Database;
26
use Koha::Database;
27
use Koha::Exceptions::Account;
26
use Koha::Items;
28
use Koha::Items;
27
use Koha::Account::Offsets;
28
29
29
use base qw(Koha::Object);
30
use base qw(Koha::Object);
30
31
Lines 34-40 Koha::Account::Lines - Koha accountline Object class Link Here
34
35
35
=head1 API
36
=head1 API
36
37
37
=head2 Class Methods
38
=head2 Class methods
38
39
39
=cut
40
=cut
40
41
Lines 125-130 sub void { Link Here
125
126
126
}
127
}
127
128
129
=head3 apply
130
131
    my $outstanding_amount = $credit->apply({ debit =>  $debit, [ offset_type => $offset_type ] });
132
133
=cut
134
135
sub apply {
136
    my ( $self, $params ) = @_;
137
138
    my $debit       = $params->{debit};
139
    my $offset_type = $params->{offset_type} // 'credit_applied';
140
141
    unless ( $self->is_credit ) {
142
        Koha::Exceptions::Account::IsNotCredit->throw(
143
            error => 'Account line ' . $self->id . ' is not a credit'
144
        );
145
    }
146
147
    unless ( !$debit->is_credit ) {
148
        Koha::Exceptions::Account::IsNotDebit->throw(
149
            error => 'Account line ' . $debit->id . 'is not a debit'
150
        );
151
    }
152
153
    my $available_credit = $self->amountoutstanding * -1;
154
155
    unless ( $available_credit > 0 ) {
156
        Koha::Exceptions::Account::NoAvailableCredit->throw(
157
            error => 'Outstanding credit is ' . $available_credit . ' and cannot be applied'
158
        );
159
    }
160
161
    my $schema = Koha::Database->new->schema;
162
163
    $schema->txn_do( sub {
164
165
        my $amount_to_cancel;
166
        my $owed = $debit->amountoutstanding;
167
168
        if ( $available_credit >= $owed ) {
169
            $amount_to_cancel = $owed;
170
        }
171
        else {    # $available_credit < $debit->amountoutstanding
172
            $amount_to_cancel = $available_credit;
173
        }
174
175
        # record the account offset
176
        Koha::Account::Offset->new(
177
            {   credit_id => $self->id,
178
                debit_id  => $debit->id,
179
                amount    => $amount_to_cancel,
180
                type      => $offset_type,
181
            }
182
        )->store();
183
184
        $available_credit -= $amount_to_cancel;
185
186
        $self->amountoutstanding( $available_credit * -1 )->store;
187
        $debit->amountoutstanding( $owed - $amount_to_cancel )->store;
188
    });
189
190
    return $available_credit;
191
}
192
193
=head3 is_credit
194
195
    my $bool = $line->is_credit;
196
197
=cut
198
199
sub is_credit {
200
    my ($self) = @_;
201
202
    return ( $self->amount < 0 );
203
}
204
205
=head2 Internal methods
206
207
=cut
208
128
=head3 _type
209
=head3 _type
129
210
130
=cut
211
=cut
(-)a/Koha/Exceptions/Account.pm (-1 / +66 lines)
Line 0 Link Here
0
- 
1
package Koha::Exceptions::Account;
2
3
# This file is part of Koha.
4
#
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
8
# version.
9
#
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.
13
#
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.
17
18
use Modern::Perl;
19
20
use Exception::Class (
21
22
    'Koha::Exceptions::Account' => {
23
        description => 'Something went wrong!',
24
    },
25
    'Koha::Exceptions::Account::IsNotCredit' => {
26
        isa => 'Koha::Exceptions::Account',
27
        description => 'Account line is not a credit'
28
    },
29
    'Koha::Exceptions::Account::IsNotDebit' => {
30
        isa => 'Koha::Exceptions::Account',
31
        description => 'Account line is not a credit'
32
    },
33
    'Koha::Exceptions::Account::NoAvailableCredit' => {
34
        isa => 'Koha::Exceptions::Account',
35
        description => 'No outstanding credit'
36
    }
37
);
38
39
=head1 NAME
40
41
Koha::Exceptions::Account - Base class for Account exceptions
42
43
=head1 Exceptions
44
45
=head2 Koha::Exceptions::Account
46
47
Generic Account exception
48
49
=head2 Koha::Exceptions::Account::IsNotCredit
50
51
Exception to be used when an action on an account line requires it to be a
52
credit and it isn't.
53
54
=head2 Koha::Exceptions::Account::IsNotDebit
55
56
Exception to be used when an action on an account line requires it to be a
57
debit and it isn't.
58
59
=head2 Koha::Exceptions::Account::NoAvailableCredit
60
61
Exception to be used when a credit has no amount outstanding and is required
62
to be applied to outstanding debits.
63
64
=cut
65
66
1;

Return to bug 20997