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>.
26 use Koha::Account::Lines;
27 use Koha::Account::Offsets;
31 use vars qw(@ISA @EXPORT);
38 purge_zero_balance_fees
44 C4::Accounts - Functions for dealing with Koha accounts
52 The functions in this module deal with the monetary aspect of Koha,
53 including looking up and modifying the amount of money owed by a
60 In a default install of Koha the following lost values are set
65 FIXME: itemlost should be set to 3 after payment is made, should be a warning to the interface that a charge has been added
66 FIXME : if no replacement price, borrower just doesn't get charged?
71 my $dbh = C4::Context->dbh();
72 my ($borrowernumber, $itemnumber, $replacementprice, $description) = @_;
73 my $item = Koha::Items->find($itemnumber);
74 my $itype = $item->itemtype;
75 $replacementprice //= 0;
76 my $defaultreplacecost = $itype->defaultreplacecost;
77 my $processfee = $itype->processfee;
78 my $usedefaultreplacementcost = C4::Context->preference("useDefaultReplacementCost");
79 my $processingfeenote = C4::Context->preference("ProcessingFeeNote");
80 if ($usedefaultreplacementcost && $replacementprice == 0 && $defaultreplacecost){
81 $replacementprice = $defaultreplacecost;
83 my $checkout = Koha::Checkouts->find({ itemnumber => $itemnumber });
84 if ( !$checkout && $item->in_bundle ) {
85 my $host = $item->bundle_host;
86 $checkout = $host->checkout;
88 my $issue_id = $checkout ? $checkout->issue_id : undef;
90 my $account = Koha::Account->new({ patron_id => $borrowernumber });
91 # first make sure the borrower hasn't already been charged for this item (for this issuance)
92 my $existing_charges = $account->lines->search(
94 itemnumber => $itemnumber,
95 debit_type_code => 'LOST',
101 unless ($existing_charges) {
103 if ($processfee && $processfee > 0){
104 my $accountline = $account->add_debit(
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,
119 if ($replacementprice > 0){
120 my $accountline = $account->add_debit(
122 amount => $replacementprice,
123 description => $description,
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,
129 item_id => $itemnumber,
130 issue_id => $issue_id,
137 =head2 purge_zero_balance_fees
139 purge_zero_balance_fees( $days );
141 Delete accountlines entries where amountoutstanding is 0 or NULL which are more than a given number of days old.
143 B<$days> -- Zero balance fees older than B<$days> days old will be deleted.
145 B<Warning:> Because fines and payments are not linked in accountlines, it is
146 possible for a fine to be deleted without the accompanying payment,
147 or vise versa. This won't affect the account balance, but might be
152 sub purge_zero_balance_fees {
156 my $dbh = C4::Context->dbh;
157 my $sth = $dbh->prepare(
159 DELETE a1 FROM accountlines a1
161 LEFT JOIN account_offsets credit_offset ON ( a1.accountlines_id = credit_offset.credit_id )
162 LEFT JOIN accountlines a2 ON ( credit_offset.debit_id = a2.accountlines_id )
164 LEFT JOIN account_offsets debit_offset ON ( a1.accountlines_id = debit_offset.debit_id )
165 LEFT JOIN accountlines a3 ON ( debit_offset.credit_id = a3.accountlines_id )
167 WHERE a1.date < date_sub(curdate(), INTERVAL ? DAY)
168 AND ( a1.amountoutstanding = 0 OR a1.amountoutstanding IS NULL )
169 AND ( a2.amountoutstanding = 0 OR a2.amountoutstanding IS NULL )
170 AND ( a3.amountoutstanding = 0 OR a3.amountoutstanding IS NULL )
173 $sth->execute($days) or die $dbh->errstr;
176 END { } # module clean-up code here (global destructor)