Bug 21396: Add 2 missing use statements in Koha::Account
[koha.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 use List::MoreUtils qw(uniq);
25
26 use C4::Circulation qw( ReturnLostItem );
27 use C4::Log qw( logaction );
28 use C4::Stats qw( UpdateStats );
29
30 use Koha::Account::Lines;
31 use Koha::Account::Offsets;
32 use Koha::DateUtils qw( dt_from_string );
33
34 =head1 NAME
35
36 Koha::Accounts - Module for managing payments and fees for patrons
37
38 =cut
39
40 sub new {
41     my ( $class, $params ) = @_;
42
43     Carp::croak("No patron id passed in!") unless $params->{patron_id};
44
45     return bless( $params, $class );
46 }
47
48 =head2 pay
49
50 This method allows payments to be made against fees/fines
51
52 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
53     {
54         amount      => $amount,
55         sip         => $sipmode,
56         note        => $note,
57         description => $description,
58         library_id  => $branchcode,
59         lines        => $lines, # Arrayref of Koha::Account::Line objects to pay
60         account_type => $type,  # accounttype code
61         offset_type => $offset_type,    # offset type code
62     }
63 );
64
65 =cut
66
67 sub pay {
68     my ( $self, $params ) = @_;
69
70     my $amount       = $params->{amount};
71     my $sip          = $params->{sip};
72     my $description  = $params->{description};
73     my $note         = $params->{note} || q{};
74     my $library_id   = $params->{library_id};
75     my $lines        = $params->{lines};
76     my $type         = $params->{type} || 'payment';
77     my $account_type = $params->{account_type};
78     my $offset_type  = $params->{offset_type} || $type eq 'writeoff' ? 'Writeoff' : 'Payment';
79
80     my $userenv = C4::Context->userenv;
81
82     # We should remove accountno, it is no longer needed
83     my $last = Koha::Account::Lines->search(
84         {
85             borrowernumber => $self->{patron_id}
86         },
87         {
88             order_by => 'accountno'
89         }
90     )->next();
91     my $accountno = $last ? $last->accountno + 1 : 1;
92
93     my $manager_id = $userenv ? $userenv->{number} : 0;
94
95     my @fines_paid; # List of account lines paid on with this payment
96
97     my $balance_remaining = $amount; # Set it now so we can adjust the amount if necessary
98     $balance_remaining ||= 0;
99
100     my @account_offsets;
101
102     # We were passed a specific line to pay
103     foreach my $fine ( @$lines ) {
104         my $amount_to_pay =
105             $fine->amountoutstanding > $balance_remaining
106           ? $balance_remaining
107           : $fine->amountoutstanding;
108
109         my $old_amountoutstanding = $fine->amountoutstanding;
110         my $new_amountoutstanding = $old_amountoutstanding - $amount_to_pay;
111         $fine->amountoutstanding($new_amountoutstanding)->store();
112         $balance_remaining = $balance_remaining - $amount_to_pay;
113
114         if ( $fine->itemnumber && $fine->accounttype && ( $fine->accounttype eq 'Rep' || $fine->accounttype eq 'L' ) )
115         {
116             C4::Circulation::ReturnLostItem( $self->{patron_id}, $fine->itemnumber );
117         }
118
119         my $account_offset = Koha::Account::Offset->new(
120             {
121                 debit_id => $fine->id,
122                 type     => $offset_type,
123                 amount   => $amount_to_pay * -1,
124             }
125         );
126         push( @account_offsets, $account_offset );
127
128         if ( C4::Context->preference("FinesLog") ) {
129             logaction(
130                 "FINES", 'MODIFY',
131                 $self->{patron_id},
132                 Dumper(
133                     {
134                         action                => 'fee_payment',
135                         borrowernumber        => $fine->borrowernumber,
136                         old_amountoutstanding => $old_amountoutstanding,
137                         new_amountoutstanding => 0,
138                         amount_paid           => $old_amountoutstanding,
139                         accountlines_id       => $fine->id,
140                         accountno             => $fine->accountno,
141                         manager_id            => $manager_id,
142                         note                  => $note,
143                     }
144                 )
145             );
146             push( @fines_paid, $fine->id );
147         }
148     }
149
150     # Were not passed a specific line to pay, or the payment was for more
151     # than the what was owed on the given line. In that case pay down other
152     # lines with remaining balance.
153     my @outstanding_fines;
154     @outstanding_fines = Koha::Account::Lines->search(
155         {
156             borrowernumber    => $self->{patron_id},
157             amountoutstanding => { '>' => 0 },
158         }
159     ) if $balance_remaining > 0;
160
161     foreach my $fine (@outstanding_fines) {
162         my $amount_to_pay =
163             $fine->amountoutstanding > $balance_remaining
164           ? $balance_remaining
165           : $fine->amountoutstanding;
166
167         my $old_amountoutstanding = $fine->amountoutstanding;
168         $fine->amountoutstanding( $old_amountoutstanding - $amount_to_pay );
169         $fine->store();
170
171         my $account_offset = Koha::Account::Offset->new(
172             {
173                 debit_id => $fine->id,
174                 type     => $offset_type,
175                 amount   => $amount_to_pay * -1,
176             }
177         );
178         push( @account_offsets, $account_offset );
179
180         if ( C4::Context->preference("FinesLog") ) {
181             logaction(
182                 "FINES", 'MODIFY',
183                 $self->{patron_id},
184                 Dumper(
185                     {
186                         action                => "fee_$type",
187                         borrowernumber        => $fine->borrowernumber,
188                         old_amountoutstanding => $old_amountoutstanding,
189                         new_amountoutstanding => $fine->amountoutstanding,
190                         amount_paid           => $amount_to_pay,
191                         accountlines_id       => $fine->id,
192                         accountno             => $fine->accountno,
193                         manager_id            => $manager_id,
194                         note                  => $note,
195                     }
196                 )
197             );
198             push( @fines_paid, $fine->id );
199         }
200
201         $balance_remaining = $balance_remaining - $amount_to_pay;
202         last unless $balance_remaining > 0;
203     }
204
205     $account_type ||=
206         $type eq 'writeoff' ? 'W'
207       : defined($sip)       ? "Pay$sip"
208       :                       'Pay';
209
210     $description ||= $type eq 'writeoff' ? 'Writeoff' : q{};
211
212     my $payment = Koha::Account::Line->new(
213         {
214             borrowernumber    => $self->{patron_id},
215             accountno         => $accountno,
216             date              => dt_from_string(),
217             amount            => 0 - $amount,
218             description       => $description,
219             accounttype       => $account_type,
220             amountoutstanding => 0 - $balance_remaining,
221             manager_id        => $manager_id,
222             note              => $note,
223         }
224     )->store();
225
226     foreach my $o ( @account_offsets ) {
227         $o->credit_id( $payment->id() );
228         $o->store();
229     }
230
231     $library_id ||= $userenv ? $userenv->{'branch'} : undef;
232
233     UpdateStats(
234         {
235             branch         => $library_id,
236             type           => $type,
237             amount         => $amount,
238             borrowernumber => $self->{patron_id},
239             accountno      => $accountno,
240         }
241     );
242
243     if ( C4::Context->preference("FinesLog") ) {
244         logaction(
245             "FINES", 'CREATE',
246             $self->{patron_id},
247             Dumper(
248                 {
249                     action            => "create_$type",
250                     borrowernumber    => $self->{patron_id},
251                     accountno         => $accountno,
252                     amount            => 0 - $amount,
253                     amountoutstanding => 0 - $balance_remaining,
254                     accounttype       => $account_type,
255                     accountlines_paid => \@fines_paid,
256                     manager_id        => $manager_id,
257                 }
258             )
259         );
260     }
261
262     return $payment->id;
263 }
264
265 =head3 balance
266
267 my $balance = $self->balance
268
269 Return the balance (sum of amountoutstanding columns)
270
271 =cut
272
273 sub balance {
274     my ($self) = @_;
275     my $fines = Koha::Account::Lines->search(
276         {
277             borrowernumber => $self->{patron_id},
278         },
279         {
280             select => [ { sum => 'amountoutstanding' } ],
281             as => ['total_amountoutstanding'],
282         }
283     );
284     return $fines->count
285       ? $fines->next->get_column('total_amountoutstanding')
286       : 0;
287 }
288
289 sub non_issues_charges {
290     my ($self) = @_;
291
292     # FIXME REMOVE And add a warning in the about page + update DB if length(MANUAL_INV) > 5
293     my $ACCOUNT_TYPE_LENGTH = 5;    # this is plain ridiculous...
294
295     my @not_fines;
296     push @not_fines, 'Res'
297       unless C4::Context->preference('HoldsInNoissuesCharge');
298     push @not_fines, 'Rent'
299       unless C4::Context->preference('RentalsInNoissuesCharge');
300     unless ( C4::Context->preference('ManInvInNoissuesCharge') ) {
301         my $dbh = C4::Context->dbh;
302         push @not_fines,
303           @{
304             $dbh->selectcol_arrayref(q|
305                 SELECT authorised_value FROM authorised_values WHERE category = 'MANUAL_INV'
306             |)
307           };
308     }
309     @not_fines = map { substr( $_, 0, $ACCOUNT_TYPE_LENGTH ) } uniq(@not_fines);
310
311     my $non_issues_charges = Koha::Account::Lines->search(
312         {
313             borrowernumber => $self->{patron_id},
314             accounttype    => { -not_in => \@not_fines }
315         },
316         {
317             select => [ { sum => 'amountoutstanding' } ],
318             as     => ['non_issues_charges'],
319         }
320     );
321     return $non_issues_charges->count
322       ? $non_issues_charges->next->get_column('non_issues_charges') + 0
323       : 0;
324 }
325
326 1;
327
328 =head1 AUTHOR
329
330 Kyle M Hall <kyle.m.hall@gmail.com>
331
332 =cut