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