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