Bug 21650: Remove the subroutine C4::Items::GetLastAcquisitions
[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 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       &manualinvoice
41       &getnextacctno
42       &chargelostitem
43       &ReversePayment
44       &purge_zero_balance_fees
45     );
46 }
47
48 =head1 NAME
49
50 C4::Accounts - Functions for dealing with Koha accounts
51
52 =head1 SYNOPSIS
53
54 use C4::Accounts;
55
56 =head1 DESCRIPTION
57
58 The functions in this module deal with the monetary aspect of Koha,
59 including looking up and modifying the amount of money owed by a
60 patron.
61
62 =head1 FUNCTIONS
63
64 =head2 getnextacctno
65
66   $nextacct = &getnextacctno($borrowernumber);
67
68 Returns the next unused account number for the patron with the given
69 borrower number.
70
71 =cut
72
73 #'
74 # FIXME - Okay, so what does the above actually _mean_?
75 sub getnextacctno {
76     my ($borrowernumber) = shift or return;
77     my $sth = C4::Context->dbh->prepare(
78         "SELECT accountno+1 FROM accountlines
79             WHERE    (borrowernumber = ?)
80             ORDER BY accountno DESC
81             LIMIT 1"
82     );
83     $sth->execute($borrowernumber);
84     return ($sth->fetchrow || 1);
85 }
86
87 =head2 chargelostitem
88
89 In a default install of Koha the following lost values are set
90 1 = Lost
91 2 = Long overdue
92 3 = Lost and paid for
93
94 FIXME: itemlost should be set to 3 after payment is made, should be a warning to the interface that a charge has been added
95 FIXME : if no replacement price, borrower just doesn't get charged?
96
97 =cut
98
99 sub chargelostitem{
100     my $dbh = C4::Context->dbh();
101     my ($borrowernumber, $itemnumber, $amount, $description) = @_;
102     my $itype = Koha::ItemTypes->find({ itemtype => Koha::Items->find($itemnumber)->effective_itemtype() });
103     my $replacementprice = $amount;
104     my $defaultreplacecost = $itype->defaultreplacecost;
105     my $processfee = $itype->processfee;
106     my $usedefaultreplacementcost = C4::Context->preference("useDefaultReplacementCost");
107     my $processingfeenote = C4::Context->preference("ProcessingFeeNote");
108     if ($usedefaultreplacementcost && $amount == 0 && $defaultreplacecost){
109         $replacementprice = $defaultreplacecost;
110     }
111     # first make sure the borrower hasn't already been charged for this item
112     # FIXME this should be more exact
113     #       there is no reason a user can't lose an item, find and return it, and lost it again
114     my $existing_charges = Koha::Account::Lines->search(
115         {
116             borrowernumber => $borrowernumber,
117             itemnumber     => $itemnumber,
118             accounttype    => 'L',
119         }
120     )->count();
121
122     # OK, they haven't
123     unless ($existing_charges) {
124         #add processing fee
125         if ($processfee && $processfee > 0){
126             my $accountline = Koha::Account::Line->new(
127                 {
128                     borrowernumber    => $borrowernumber,
129                     accountno         => getnextacctno($borrowernumber),
130                     date              => \'NOW()',
131                     amount            => $processfee,
132                     description       => $description,
133                     accounttype       => 'PF',
134                     amountoutstanding => $processfee,
135                     itemnumber        => $itemnumber,
136                     note              => $processingfeenote,
137                     manager_id        => C4::Context->userenv ? C4::Context->userenv->{'number'} : 0,
138                 }
139             )->store();
140
141             my $account_offset = Koha::Account::Offset->new(
142                 {
143                     debit_id => $accountline->id,
144                     type     => 'Processing Fee',
145                     amount   => $accountline->amount,
146                 }
147             )->store();
148
149             if ( C4::Context->preference("FinesLog") ) {
150                 logaction("FINES", 'CREATE',$borrowernumber,Dumper({
151                     action            => 'create_fee',
152                     borrowernumber    => $accountline->borrowernumber,,
153                     accountno         => $accountline->accountno,
154                     amount            => $accountline->amount,
155                     description       => $accountline->description,
156                     accounttype       => $accountline->accounttype,
157                     amountoutstanding => $accountline->amountoutstanding,
158                     note              => $accountline->note,
159                     itemnumber        => $accountline->itemnumber,
160                     manager_id        => $accountline->manager_id,
161                 }));
162             }
163         }
164         #add replace cost
165         if ($replacementprice > 0){
166             my $accountline = Koha::Account::Line->new(
167                 {
168                     borrowernumber    => $borrowernumber,
169                     accountno         => getnextacctno($borrowernumber),
170                     date              => \'NOW()',
171                     amount            => $replacementprice,
172                     description       => $description,
173                     accounttype       => 'L',
174                     amountoutstanding => $replacementprice,
175                     itemnumber        => $itemnumber,
176                     manager_id        => C4::Context->userenv ? C4::Context->userenv->{'number'} : 0,
177                 }
178             )->store();
179
180             my $account_offset = Koha::Account::Offset->new(
181                 {
182                     debit_id => $accountline->id,
183                     type     => 'Lost Item',
184                     amount   => $accountline->amount,
185                 }
186             )->store();
187
188             if ( C4::Context->preference("FinesLog") ) {
189                 logaction("FINES", 'CREATE',$borrowernumber,Dumper({
190                     action            => 'create_fee',
191                     borrowernumber    => $accountline->borrowernumber,,
192                     accountno         => $accountline->accountno,
193                     amount            => $accountline->amount,
194                     description       => $accountline->description,
195                     accounttype       => $accountline->accounttype,
196                     amountoutstanding => $accountline->amountoutstanding,
197                     note              => $accountline->note,
198                     itemnumber        => $accountline->itemnumber,
199                     manager_id        => $accountline->manager_id,
200                 }));
201             }
202         }
203     }
204 }
205
206 =head2 manualinvoice
207
208   &manualinvoice($borrowernumber, $itemnumber, $description, $type,
209                  $amount, $note);
210
211 C<$borrowernumber> is the patron's borrower number.
212 C<$description> is a description of the transaction.
213 C<$type> may be one of C<CS>, C<CB>, C<CW>, C<CF>, C<CL>, C<N>, C<L>,
214 or C<REF>.
215 C<$itemnumber> is the item involved, if pertinent; otherwise, it
216 should be the empty string.
217
218 =cut
219
220 #'
221 # FIXME: In Koha 3.0 , the only account adjustment 'types' passed to this function
222 # are:
223 #               'C' = CREDIT
224 #               'FOR' = FORGIVEN  (Formerly 'F', but 'F' is taken to mean 'FINE' elsewhere)
225 #               'N' = New Card fee
226 #               'F' = Fine
227 #               'A' = Account Management fee
228 #               'M' = Sundry
229 #               'L' = Lost Item
230 #
231
232 sub manualinvoice {
233     my ( $borrowernumber, $itemnum, $desc, $type, $amount, $note ) = @_;
234     my $manager_id = 0;
235     $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
236     my $dbh      = C4::Context->dbh;
237     my $insert;
238     my $accountno  = getnextacctno($borrowernumber);
239     my $amountleft = $amount;
240
241     my $accountline = Koha::Account::Line->new(
242         {
243             borrowernumber    => $borrowernumber,
244             accountno         => $accountno,
245             date              => \'NOW()',
246             amount            => $amount,
247             description       => $desc,
248             accounttype       => $type,
249             amountoutstanding => $amountleft,
250             itemnumber        => $itemnum || undef,
251             note              => $note,
252             manager_id        => $manager_id,
253         }
254     )->store();
255
256     my $account_offset = Koha::Account::Offset->new(
257         {
258             debit_id => $accountline->id,
259             type     => 'Manual Debit',
260             amount   => $amount,
261         }
262     )->store();
263
264     if ( C4::Context->preference("FinesLog") ) {
265         logaction("FINES", 'CREATE',$borrowernumber,Dumper({
266             action            => 'create_fee',
267             borrowernumber    => $borrowernumber,
268             accountno         => $accountno,
269             amount            => $amount,
270             description       => $desc,
271             accounttype       => $type,
272             amountoutstanding => $amountleft,
273             note              => $note,
274             itemnumber        => $itemnum,
275             manager_id        => $manager_id,
276         }));
277     }
278
279     return 0;
280 }
281
282 #FIXME: ReversePayment should be replaced with a Void Payment feature
283 sub ReversePayment {
284     my ($accountlines_id) = @_;
285     my $dbh = C4::Context->dbh;
286
287     my $accountline        = Koha::Account::Lines->find($accountlines_id);
288     my $amount_outstanding = $accountline->amountoutstanding;
289
290     my $new_amountoutstanding =
291       $amount_outstanding <= 0 ? $accountline->amount * -1 : 0;
292
293     $accountline->description( $accountline->description . " Reversed -" );
294     $accountline->amountoutstanding($new_amountoutstanding);
295     $accountline->store();
296
297     my $account_offset = Koha::Account::Offset->new(
298         {
299             credit_id => $accountline->id,
300             type      => 'Reverse Payment',
301             amount    => $amount_outstanding - $new_amountoutstanding,
302         }
303     )->store();
304
305     if ( C4::Context->preference("FinesLog") ) {
306         my $manager_id = 0;
307         $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
308
309         logaction(
310             "FINES", 'MODIFY',
311             $accountline->borrowernumber,
312             Dumper(
313                 {
314                     action                => 'reverse_fee_payment',
315                     borrowernumber        => $accountline->borrowernumber,
316                     old_amountoutstanding => $amount_outstanding,
317                     new_amountoutstanding => $new_amountoutstanding,
318                     ,
319                     accountlines_id => $accountline->id,
320                     accountno       => $accountline->accountno,
321                     manager_id      => $manager_id,
322                 }
323             )
324         );
325     }
326 }
327
328 =head2 purge_zero_balance_fees
329
330   purge_zero_balance_fees( $days );
331
332 Delete accountlines entries where amountoutstanding is 0 or NULL which are more than a given number of days old.
333
334 B<$days> -- Zero balance fees older than B<$days> days old will be deleted.
335
336 B<Warning:> Because fines and payments are not linked in accountlines, it is
337 possible for a fine to be deleted without the accompanying payment,
338 or vise versa. This won't affect the account balance, but might be
339 confusing to staff.
340
341 =cut
342
343 sub purge_zero_balance_fees {
344     my $days  = shift;
345     my $count = 0;
346
347     my $dbh = C4::Context->dbh;
348     my $sth = $dbh->prepare(
349         q{
350             DELETE a1 FROM accountlines a1
351
352             LEFT JOIN account_offsets credit_offset ON ( a1.accountlines_id = credit_offset.credit_id )
353             LEFT JOIN accountlines a2 ON ( credit_offset.debit_id = a2.accountlines_id )
354
355             LEFT JOIN account_offsets debit_offset ON ( a1.accountlines_id = debit_offset.debit_id )
356             LEFT JOIN accountlines a3 ON ( debit_offset.credit_id = a3.accountlines_id )
357
358             WHERE a1.date < date_sub(curdate(), INTERVAL ? DAY)
359               AND ( a1.amountoutstanding = 0 OR a1.amountoutstanding IS NULL )
360               AND ( a2.amountoutstanding = 0 OR a2.amountoutstanding IS NULL )
361               AND ( a3.amountoutstanding = 0 OR a3.amountoutstanding IS NULL )
362         }
363     );
364     $sth->execute($days) or die $dbh->errstr;
365 }
366
367 END { }    # module clean-up code here (global destructor)
368
369 1;
370 __END__
371
372 =head1 SEE ALSO
373
374 DBI(3)
375
376 =cut
377