Bug 25444: More minor improvements to simplified loop
[koha.git] / C4 / Accounts.pm
1 package C4::Accounts;
2
3 # Copyright 2000-2002 Katipo Communications
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
21 use Modern::Perl;
22 use C4::Context;
23 use C4::Stats;
24 use C4::Members;
25 use C4::Log qw(logaction);
26 use Koha::Account;
27 use Koha::Account::Lines;
28 use Koha::Account::Offsets;
29 use Koha::Items;
30
31 use Mojo::Util qw(deprecated);
32 use Data::Dumper qw(Dumper);
33
34 use vars qw(@ISA @EXPORT);
35
36 BEGIN {
37     require Exporter;
38     @ISA    = qw(Exporter);
39     @EXPORT = qw(
40       &chargelostitem
41       &purge_zero_balance_fees
42     );
43 }
44
45 =head1 NAME
46
47 C4::Accounts - Functions for dealing with Koha accounts
48
49 =head1 SYNOPSIS
50
51 use C4::Accounts;
52
53 =head1 DESCRIPTION
54
55 The functions in this module deal with the monetary aspect of Koha,
56 including looking up and modifying the amount of money owed by a
57 patron.
58
59 =head1 FUNCTIONS
60
61 =head2 chargelostitem
62
63 In a default install of Koha the following lost values are set
64 1 = Lost
65 2 = Long overdue
66 3 = Lost and paid for
67
68 FIXME: itemlost should be set to 3 after payment is made, should be a warning to the interface that a charge has been added
69 FIXME : if no replacement price, borrower just doesn't get charged?
70
71 =cut
72
73 sub chargelostitem {
74     my $dbh = C4::Context->dbh();
75     my ($borrowernumber, $itemnumber, $amount, $description) = @_;
76     my $itype = Koha::ItemTypes->find({ itemtype => Koha::Items->find($itemnumber)->effective_itemtype() });
77     my $replacementprice = $amount;
78     my $defaultreplacecost = $itype->defaultreplacecost;
79     my $processfee = $itype->processfee;
80     my $usedefaultreplacementcost = C4::Context->preference("useDefaultReplacementCost");
81     my $processingfeenote = C4::Context->preference("ProcessingFeeNote");
82     if ($usedefaultreplacementcost && $amount == 0 && $defaultreplacecost){
83         $replacementprice = $defaultreplacecost;
84     }
85     my $checkout = Koha::Checkouts->find({ itemnumber => $itemnumber });
86     my $issue_id = $checkout ? $checkout->issue_id : undef;
87
88     my $account = Koha::Account->new({ patron_id => $borrowernumber });
89     # first make sure the borrower hasn't already been charged for this item (for this issuance)
90     my $existing_charges = $account->lines->search(
91         {
92             itemnumber      => $itemnumber,
93             debit_type_code => 'LOST',
94             issue_id        => $issue_id
95         }
96     )->count();
97
98     # OK, they haven't
99     unless ($existing_charges) {
100         #add processing fee
101         if ($processfee && $processfee > 0){
102             my $accountline = $account->add_debit(
103                 {
104                     amount      => $processfee,
105                     description => $description,
106                     note        => $processingfeenote,
107                     user_id     => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
108                     interface   => C4::Context->interface,
109                     library_id  => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
110                     type        => 'PROCESSING',
111                     item_id     => $itemnumber,
112                     issue_id    => $issue_id,
113                 }
114             );
115         }
116         #add replace cost
117         if ($replacementprice > 0){
118             my $accountline = $account->add_debit(
119                 {
120                     amount      => $replacementprice,
121                     description => $description,
122                     note        => undef,
123                     user_id     => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
124                     interface   => C4::Context->interface,
125                     library_id  => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
126                     type        => 'LOST',
127                     item_id     => $itemnumber,
128                     issue_id    => $issue_id,
129                 }
130             );
131         }
132     }
133 }
134
135 =head2 manualinvoice
136
137   &manualinvoice($borrowernumber, $itemnumber, $description, $type,
138                  $amount, $note);
139
140 This function is now deprecated and not used anywhere within koha. It is due for complete removal in 19.11
141
142 =cut
143
144 sub manualinvoice {
145     my ( $borrowernumber, $itemnum, $desc, $type, $amount, $note ) = @_;
146
147     deprecated "C4::Accounts::manualinvoice is deprecated in favor of Koha::Account->add_debit";
148
149     my $manager_id = C4::Context->userenv ? C4::Context->userenv->{'number'} : undef;
150     my $dbh      = C4::Context->dbh;
151     my $insert;
152     my $amountleft = $amount;
153
154     my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
155
156     my $issue_id;
157     if ( $type eq 'LOST' && $itemnum ) {
158         my $checkouts = Koha::Checkouts->search(
159             { itemnumber => $itemnum, borrowernumber => $borrowernumber } );
160         my $checkout =
161             $checkouts->count
162           ? $checkouts->next
163           : Koha::Old::Checkouts->search(
164             { itemnumber => $itemnum, borrowernumber => $borrowernumber },
165             { order_by   => { -desc => 'returndate' }, rows => 1 }
166         )->next;
167         $issue_id = $checkout ? $checkout->issue_id : undef;
168     }
169
170     my $accountline = Koha::Account::Line->new(
171         {
172             borrowernumber    => $borrowernumber,
173             date              => \'NOW()',
174             amount            => $amount,
175             description       => $desc,
176             debit_type_code   => $type,
177             amountoutstanding => $amountleft,
178             itemnumber        => $itemnum || undef,
179             issue_id          => $issue_id,
180             note              => $note,
181             manager_id        => $manager_id,
182             interface         => C4::Context->interface,
183             branchcode        => $branchcode,
184         }
185     )->store();
186
187     my $account_offset = Koha::Account::Offset->new(
188         {
189             debit_id => $accountline->id,
190             type     => 'Manual Debit',
191             amount   => $amount,
192         }
193     )->store();
194
195     if ( C4::Context->preference("FinesLog") ) {
196         logaction("FINES", 'CREATE',$borrowernumber,Dumper({
197             action            => 'create_fee',
198             borrowernumber    => $borrowernumber,
199             amount            => $amount,
200             description       => $desc,
201             debit_type_code   => $type,
202             amountoutstanding => $amountleft,
203             note              => $note,
204             itemnumber        => $itemnum,
205             manager_id        => $manager_id,
206         }));
207     }
208
209     return 0;
210 }
211
212 =head2 purge_zero_balance_fees
213
214   purge_zero_balance_fees( $days );
215
216 Delete accountlines entries where amountoutstanding is 0 or NULL which are more than a given number of days old.
217
218 B<$days> -- Zero balance fees older than B<$days> days old will be deleted.
219
220 B<Warning:> Because fines and payments are not linked in accountlines, it is
221 possible for a fine to be deleted without the accompanying payment,
222 or vise versa. This won't affect the account balance, but might be
223 confusing to staff.
224
225 =cut
226
227 sub purge_zero_balance_fees {
228     my $days  = shift;
229     my $count = 0;
230
231     my $dbh = C4::Context->dbh;
232     my $sth = $dbh->prepare(
233         q{
234             DELETE a1 FROM accountlines a1
235
236             LEFT JOIN account_offsets credit_offset ON ( a1.accountlines_id = credit_offset.credit_id )
237             LEFT JOIN accountlines a2 ON ( credit_offset.debit_id = a2.accountlines_id )
238
239             LEFT JOIN account_offsets debit_offset ON ( a1.accountlines_id = debit_offset.debit_id )
240             LEFT JOIN accountlines a3 ON ( debit_offset.credit_id = a3.accountlines_id )
241
242             WHERE a1.date < date_sub(curdate(), INTERVAL ? DAY)
243               AND ( a1.amountoutstanding = 0 OR a1.amountoutstanding IS NULL )
244               AND ( a2.amountoutstanding = 0 OR a2.amountoutstanding IS NULL )
245               AND ( a3.amountoutstanding = 0 OR a3.amountoutstanding IS NULL )
246         }
247     );
248     $sth->execute($days) or die $dbh->errstr;
249 }
250
251 END { }    # module clean-up code here (global destructor)
252
253 1;
254 __END__
255
256 =head1 SEE ALSO
257
258 DBI(3)
259
260 =cut
261