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