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