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 Data::Dumper qw(Dumper);
34 use vars qw(@ISA @EXPORT);
43 &purge_zero_balance_fees
49 C4::Accounts - Functions for dealing with Koha accounts
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
65 $nextacct = &getnextacctno($borrowernumber);
67 Returns the next unused account number for the patron with the given
73 # FIXME - Okay, so what does the above actually _mean_?
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
82 $sth->execute($borrowernumber);
83 return ($sth->fetchrow || 1);
88 In a default install of Koha the following lost values are set
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?
99 my $dbh = C4::Context->dbh();
100 my ($borrowernumber, $itemnumber, $issue_id, $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;
110 # first make sure the borrower hasn't already been charged for this item
111 # FIXME this should be more exact
112 # there is no reason a user can't lose an item, find and return it, and lost it again
113 my $existing_charges = Koha::Account::Lines->search(
115 borrowernumber => $borrowernumber,
116 itemnumber => $itemnumber,
122 unless ($existing_charges) {
124 if ($processfee && $processfee > 0){
125 my $accountline = Koha::Account::Line->new(
127 borrowernumber => $borrowernumber,
128 issue_id => $issue_id,
129 accountno => getnextacctno($borrowernumber),
131 amount => $processfee,
132 description => $description,
134 amountoutstanding => $processfee,
135 itemnumber => $itemnumber,
136 note => $processingfeenote,
137 manager_id => C4::Context->userenv ? C4::Context->userenv->{'number'} : 0,
141 my $account_offset = Koha::Account::Offset->new(
143 debit_id => $accountline->id,
144 type => 'Processing Fee',
145 amount => $accountline->amount,
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,
165 if ($replacementprice > 0){
166 my $accountline = Koha::Account::Line->new(
168 borrowernumber => $borrowernumber,
169 issue_id => $issue_id,
170 accountno => getnextacctno($borrowernumber),
172 amount => $replacementprice,
173 description => $description,
175 amountoutstanding => $replacementprice,
176 itemnumber => $itemnumber,
177 manager_id => C4::Context->userenv ? C4::Context->userenv->{'number'} : 0,
181 my $account_offset = Koha::Account::Offset->new(
183 debit_id => $accountline->id,
185 amount => $accountline->amount,
189 if ( C4::Context->preference("FinesLog") ) {
190 logaction("FINES", 'CREATE',$borrowernumber,Dumper({
191 action => 'create_fee',
192 borrowernumber => $accountline->borrowernumber,,
193 accountno => $accountline->accountno,
194 amount => $accountline->amount,
195 description => $accountline->description,
196 accounttype => $accountline->accounttype,
197 amountoutstanding => $accountline->amountoutstanding,
198 note => $accountline->note,
199 itemnumber => $accountline->itemnumber,
200 manager_id => $accountline->manager_id,
209 &manualinvoice($borrowernumber, $itemnumber, $description, $type,
212 C<$borrowernumber> is the patron's borrower number.
213 C<$description> is a description of the transaction.
214 C<$type> may be one of C<CS>, C<CB>, C<CW>, C<CF>, C<CL>, C<N>, C<L>,
216 C<$itemnumber> is the item involved, if pertinent; otherwise, it
217 should be the empty string.
222 # FIXME: In Koha 3.0 , the only account adjustment 'types' passed to this function
225 # 'FOR' = FORGIVEN (Formerly 'F', but 'F' is taken to mean 'FINE' elsewhere)
228 # 'A' = Account Management fee
234 my ( $borrowernumber, $itemnum, $desc, $type, $amount, $note ) = @_;
236 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
237 my $dbh = C4::Context->dbh;
239 my $accountno = getnextacctno($borrowernumber);
240 my $amountleft = $amount;
242 my $accountline = Koha::Account::Line->new(
244 borrowernumber => $borrowernumber,
245 accountno => $accountno,
248 description => $desc,
249 accounttype => $type,
250 amountoutstanding => $amountleft,
251 itemnumber => $itemnum || undef,
253 manager_id => $manager_id,
257 my $account_offset = Koha::Account::Offset->new(
259 debit_id => $accountline->id,
260 type => 'Manual Debit',
265 if ( C4::Context->preference("FinesLog") ) {
266 logaction("FINES", 'CREATE',$borrowernumber,Dumper({
267 action => 'create_fee',
268 borrowernumber => $borrowernumber,
269 accountno => $accountno,
271 description => $desc,
272 accounttype => $type,
273 amountoutstanding => $amountleft,
275 itemnumber => $itemnum,
276 manager_id => $manager_id,
283 =head2 purge_zero_balance_fees
285 purge_zero_balance_fees( $days );
287 Delete accountlines entries where amountoutstanding is 0 or NULL which are more than a given number of days old.
289 B<$days> -- Zero balance fees older than B<$days> days old will be deleted.
291 B<Warning:> Because fines and payments are not linked in accountlines, it is
292 possible for a fine to be deleted without the accompanying payment,
293 or vise versa. This won't affect the account balance, but might be
298 sub purge_zero_balance_fees {
302 my $dbh = C4::Context->dbh;
303 my $sth = $dbh->prepare(
305 DELETE a1 FROM accountlines a1
307 LEFT JOIN account_offsets credit_offset ON ( a1.accountlines_id = credit_offset.credit_id )
308 LEFT JOIN accountlines a2 ON ( credit_offset.debit_id = a2.accountlines_id )
310 LEFT JOIN account_offsets debit_offset ON ( a1.accountlines_id = debit_offset.debit_id )
311 LEFT JOIN accountlines a3 ON ( debit_offset.credit_id = a3.accountlines_id )
313 WHERE a1.date < date_sub(curdate(), INTERVAL ? DAY)
314 AND ( a1.amountoutstanding = 0 OR a1.amountoutstanding IS NULL )
315 AND ( a2.amountoutstanding = 0 OR a2.amountoutstanding IS NULL )
316 AND ( a3.amountoutstanding = 0 OR a3.amountoutstanding IS NULL )
319 $sth->execute($days) or die $dbh->errstr;
322 END { } # module clean-up code here (global destructor)