Bug 18790: Add ability to void payments
[koha.git] / Koha / Account / Line.pm
1 package Koha::Account::Line;
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 Carp;
21
22 use Koha::Database;
23 use Koha::Items;
24 use Koha::Account::Offsets;
25
26 use base qw(Koha::Object);
27
28 =head1 NAME
29
30 Koha::Account::Lines - Koha accountline Object class
31
32 =head1 API
33
34 =head2 Class Methods
35
36 =cut
37
38 =head3 item
39
40 Return the item linked to this account line if exists
41
42 =cut
43
44 sub item {
45     my ( $self ) = @_;
46     my $rs = $self->_result->itemnumber;
47     return Koha::Item->_new_from_dbic( $rs );
48 }
49
50 =head3 void
51
52 $payment_accountline->void();
53
54 =cut
55
56 sub void {
57     my ($self) = @_;
58
59     # Make sure it is a payment we are voiding
60     return unless $self->accounttype =~ /^Pay/;
61
62     my @account_offsets =
63       Koha::Account::Offsets->search( { credit_id => $self->id, type => 'Payment' } );
64
65     foreach my $account_offset (@account_offsets) {
66         my $fee_paid = Koha::Account::Lines->find( $account_offset->debit_id );
67
68         next unless $fee_paid;
69
70         my $amount_paid = $account_offset->amount * -1; # amount paid is stored as a negative amount
71         my $new_amount = $fee_paid->amountoutstanding + $amount_paid;
72         $fee_paid->amountoutstanding($new_amount);
73         $fee_paid->store();
74
75         Koha::Account::Offset->new(
76             {
77                 credit_id => $self->id,
78                 debit_id  => $fee_paid->id,
79                 amount    => $amount_paid,
80                 type      => 'Void Payment',
81             }
82         )->store();
83     }
84
85     $self->set(
86         {
87             accounttype       => 'VOID',
88             amountoutstanding => 0,
89             amount            => 0,
90         }
91     );
92     $self->store();
93 }
94
95 =head3 _type
96
97 =cut
98
99 sub _type {
100     return 'Accountline';
101 }
102
103 1;