3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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.
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.
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>.
22 #use warnings; FIXME - Bug 2505
26 use C4::Log qw(logaction);
28 use Koha::Account::Lines;
29 use Koha::Account::Offsets;
32 use Mojo::Util qw(deprecated);
33 use Data::Dumper qw(Dumper);
35 use vars qw(@ISA @EXPORT);
42 &purge_zero_balance_fees
48 C4::Accounts - Functions for dealing with Koha accounts
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
64 In a default install of Koha the following lost values are set
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?
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;
86 my $checkout = Koha::Checkouts->find({ itemnumber => $itemnumber });
87 my $issue_id = $checkout ? $checkout->issue_id : undef;
89 my $account = Koha::Account->new({ patron_id => $borrowernumber });
90 # first make sure the borrower hasn't already been charged for this item
91 # FIXME this should be more exact
92 # there is no reason a user can't lose an item, find and return it, and lost it again
93 my $existing_charges = $account->lines->search(
95 itemnumber => $itemnumber,
96 accounttype => 'LOST',
102 unless ($existing_charges) {
104 if ($processfee && $processfee > 0){
105 my $accountline = $account->add_debit(
107 amount => $processfee,
108 description => $description,
109 note => $processingfeenote,
110 user_id => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
111 interface => C4::Context->interface,
112 library_id => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
113 type => 'processing',
114 item_id => $itemnumber,
115 issue_id => $issue_id,
120 if ($replacementprice > 0){
121 my $accountline = $account->add_debit(
123 amount => $replacementprice,
124 description => $description,
126 user_id => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
127 interface => C4::Context->interface,
128 library_id => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
130 item_id => $itemnumber,
131 issue_id => $issue_id,
140 &manualinvoice($borrowernumber, $itemnumber, $description, $type,
143 This function is now deprecated and not used anywhere within koha. It is due for complete removal in 19.11
148 my ( $borrowernumber, $itemnum, $desc, $type, $amount, $note ) = @_;
150 deprecated "C4::Accounts::manualinvoice is deprecated in favor of Koha::Account->add_debit";
152 my $manager_id = C4::Context->userenv ? C4::Context->userenv->{'number'} : undef;
153 my $dbh = C4::Context->dbh;
155 my $amountleft = $amount;
157 my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
159 my $accountline = Koha::Account::Line->new(
161 borrowernumber => $borrowernumber,
164 description => $desc,
165 accounttype => $type,
166 amountoutstanding => $amountleft,
167 itemnumber => $itemnum || undef,
169 manager_id => $manager_id,
170 interface => C4::Context->interface,
171 branchcode => $branchcode,
175 my $account_offset = Koha::Account::Offset->new(
177 debit_id => $accountline->id,
178 type => 'Manual Debit',
183 if ( C4::Context->preference("FinesLog") ) {
184 logaction("FINES", 'CREATE',$borrowernumber,Dumper({
185 action => 'create_fee',
186 borrowernumber => $borrowernumber,
188 description => $desc,
189 accounttype => $type,
190 amountoutstanding => $amountleft,
192 itemnumber => $itemnum,
193 manager_id => $manager_id,
200 =head2 purge_zero_balance_fees
202 purge_zero_balance_fees( $days );
204 Delete accountlines entries where amountoutstanding is 0 or NULL which are more than a given number of days old.
206 B<$days> -- Zero balance fees older than B<$days> days old will be deleted.
208 B<Warning:> Because fines and payments are not linked in accountlines, it is
209 possible for a fine to be deleted without the accompanying payment,
210 or vise versa. This won't affect the account balance, but might be
215 sub purge_zero_balance_fees {
219 my $dbh = C4::Context->dbh;
220 my $sth = $dbh->prepare(
222 DELETE a1 FROM accountlines a1
224 LEFT JOIN account_offsets credit_offset ON ( a1.accountlines_id = credit_offset.credit_id )
225 LEFT JOIN accountlines a2 ON ( credit_offset.debit_id = a2.accountlines_id )
227 LEFT JOIN account_offsets debit_offset ON ( a1.accountlines_id = debit_offset.debit_id )
228 LEFT JOIN accountlines a3 ON ( debit_offset.credit_id = a3.accountlines_id )
230 WHERE a1.date < date_sub(curdate(), INTERVAL ? DAY)
231 AND ( a1.amountoutstanding = 0 OR a1.amountoutstanding IS NULL )
232 AND ( a2.amountoutstanding = 0 OR a2.amountoutstanding IS NULL )
233 AND ( a3.amountoutstanding = 0 OR a3.amountoutstanding IS NULL )
236 $sth->execute($days) or die $dbh->errstr;
239 END { } # module clean-up code here (global destructor)