Bug 9302: (QA follow-up) Copy merged patrons to deletedborrowers table
[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 use Data::Dumper;
22
23 use C4::Log qw(logaction);
24
25 use Koha::Database;
26 use Koha::Items;
27 use Koha::Account::Offsets;
28
29 use base qw(Koha::Object);
30
31 =head1 NAME
32
33 Koha::Account::Lines - Koha accountline Object class
34
35 =head1 API
36
37 =head2 Class Methods
38
39 =cut
40
41 =head3 item
42
43 Return the item linked to this account line if exists
44
45 =cut
46
47 sub item {
48     my ( $self ) = @_;
49     my $rs = $self->_result->itemnumber;
50     return Koha::Item->_new_from_dbic( $rs );
51 }
52
53 =head3 void
54
55 $payment_accountline->void();
56
57 =cut
58
59 sub void {
60     my ($self) = @_;
61
62     # Make sure it is a payment we are voiding
63     return unless $self->accounttype =~ /^Pay/;
64
65     my @account_offsets =
66       Koha::Account::Offsets->search(
67         { credit_id => $self->id, type => 'Payment' } );
68
69     $self->_result->result_source->schema->txn_do(
70         sub {
71             foreach my $account_offset (@account_offsets) {
72                 my $fee_paid =
73                   Koha::Account::Lines->find( $account_offset->debit_id );
74
75                 next unless $fee_paid;
76
77                 my $amount_paid = $account_offset->amount * -1; # amount paid is stored as a negative amount
78                 my $new_amount = $fee_paid->amountoutstanding + $amount_paid;
79                 $fee_paid->amountoutstanding($new_amount);
80                 $fee_paid->store();
81
82                 Koha::Account::Offset->new(
83                     {
84                         credit_id => $self->id,
85                         debit_id  => $fee_paid->id,
86                         amount    => $amount_paid,
87                         type      => 'Void Payment',
88                     }
89                 )->store();
90             }
91
92             if ( C4::Context->preference("FinesLog") ) {
93                 logaction(
94                     "FINES", 'VOID',
95                     $self->borrowernumber,
96                     Dumper(
97                         {
98                             action         => 'void_payment',
99                             borrowernumber => $self->borrowernumber,
100                             amount            => $self->amount,
101                             amountoutstanding => $self->amountoutstanding,
102                             description       => $self->description,
103                             accounttype       => $self->accounttype,
104                             payment_type      => $self->payment_type,
105                             note              => $self->note,
106                             itemnumber        => $self->itemnumber,
107                             manager_id        => $self->manager_id,
108                             offsets =>
109                               [ map { $_->unblessed } @account_offsets ],
110                         }
111                     )
112                 );
113             }
114
115             $self->set(
116                 {
117                     accounttype       => 'VOID',
118                     amountoutstanding => 0,
119                     amount            => 0,
120                 }
121             );
122             $self->store();
123         }
124     );
125
126 }
127
128 =head3 _type
129
130 =cut
131
132 sub _type {
133     return 'Accountline';
134 }
135
136 1;