Bug 15895 - Add Koha::Account module, use Koha::Account::pay internally for recordpayment
[koha-equinox.git] / Koha / Account.pm
1 package Koha::Account;
2
3 # Copyright 2016 ByWater Solutions
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Carp;
23 use Data::Dumper;
24
25 use C4::Log qw( logaction );
26 use C4::Stats qw( UpdateStats );
27
28 use Koha::Account::Line;
29 use Koha::Account::Lines;
30 use Koha::DateUtils qw( dt_from_string );
31
32 =head1 NAME
33
34 Koha::Accounts - Module for managing payments and fees for patrons
35
36 =cut
37
38 sub new {
39     my ( $class, $params ) = @_;
40
41     Carp::croak("No patron id passed in!") unless $params->{patron_id};
42
43     return bless( $params, $class );
44 }
45
46 =head2 pay
47
48 This method allows payments to be made against feees
49
50 =cut
51
52 sub pay {
53     my ( $self, $params ) = @_;
54
55     my $amount = $params->{amount};
56     my $sip    = $params->{sip};
57     my $note   = $params->{note} || q{};
58
59     my $userenv = C4::Context->userenv;
60
61     # We should remove accountno, it is no longer needed
62     my $last = Koha::Account::Lines->search(
63         {
64             borrowernumber => $self->{patron_id}
65         },
66         {
67             order_by => 'accountno'
68         }
69     )->next();
70     my $accountno = $last ? $last->accountno + 1 : 1;
71
72     my $manager_id = $userenv ? $userenv->{number} : 0;
73
74     my @outstanding_fines = Koha::Account::Lines->search(
75         {
76             borrowernumber    => $self->{patron_id},
77             amountoutstanding => { '>' => 0 },
78         }
79     );
80
81     my $balance_remaining = $amount;
82     my @fines_paid;
83     foreach my $fine (@outstanding_fines) {
84         my $amount_to_pay =
85             $fine->amountoutstanding > $balance_remaining
86           ? $balance_remaining
87           : $fine->amountoutstanding;
88
89         my $old_amountoutstanding = $fine->amountoutstanding;
90         $fine->amountoutstanding( $old_amountoutstanding - $amount_to_pay );
91         $fine->store();
92
93         if ( C4::Context->preference("FinesLog") ) {
94             logaction(
95                 "FINES", 'MODIFY',
96                 $self->{patron_id},
97                 Dumper(
98                     {
99                         action                => 'fee_payment',
100                         borrowernumber        => $fine->borrowernumber,
101                         old_amountoutstanding => $old_amountoutstanding,
102                         new_amountoutstanding => $fine->amountoutstanding,
103                         amount_paid           => $amount_to_pay,
104                         accountlines_id       => $fine->id,
105                         accountno             => $fine->accountno,
106                         manager_id            => $manager_id,
107                         note                  => $note,
108                     }
109                 )
110             );
111             push( @fines_paid, $fine->id );
112         }
113
114         $balance_remaining = $balance_remaining - $amount_to_pay;
115         last unless $balance_remaining > 0;
116     }
117
118     my $account_type = defined($sip) ? "Pay$sip" : 'Pay';
119
120     my $payment = Koha::Account::Line->new(
121         {
122             borrowernumber    => $self->{patron_id},
123             accountno         => $accountno,
124             date              => dt_from_string(),
125             amount            => 0 - $amount,
126             description       => q{},
127             accounttype       => $account_type,
128             amountoutstanding => 0 - $balance_remaining,
129             manager_id        => $manager_id,
130             note              => $note,
131         }
132     )->store();
133
134     my $branch = $userenv ? $userenv->{'branch'} : undef;
135     UpdateStats(
136         {
137             branch         => $branch,
138             type           => 'payment',
139             amount         => $amount,
140             borrowernumber => $self->{patron_id},
141             accountno      => $accountno,
142         }
143     );
144
145     if ( C4::Context->preference("FinesLog") ) {
146         logaction(
147             "FINES", 'CREATE',
148             $self->{patron_id},
149             Dumper(
150                 {
151                     action            => 'create_payment',
152                     borrowernumber    => $self->{patron_id},
153                     accountno         => $accountno,
154                     amount            => 0 - $amount,
155                     amountoutstanding => 0 - $balance_remaining,
156                     accounttype       => 'Pay',
157                     accountlines_paid => \@fines_paid,
158                     manager_id        => $manager_id,
159                 }
160             )
161         );
162     }
163 }
164
165 1;
166
167 =head1 AUTHOR
168
169 Kyle M Hall <kyle.m.hall@gmail.com>
170
171 =cut