1 package C4::Circulation::Fines;
5 # Copyright 2000-2002 Katipo Communications
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA 02111-1307 USA
27 use vars qw($VERSION @ISA @EXPORT);
29 # set the version for version checking
34 C4::Circulation::Fines - Koha module dealing with fines
38 use C4::Circulation::Fines;
42 This module contains several functions for dealing with fines for
43 overdue items. It is primarily used by the 'misc/fines2.pl' script.
52 @EXPORT = qw(&Getoverdues &CalcFine &BorType &UpdateFine &ReplacementCost &GetFine &ReplacementCost2);
56 ($count, $overdues) = &Getoverdues();
58 Returns the list of all overdue books.
60 C<$count> is the number of elements in C<@{$overdues}>.
62 C<$overdues> is a reference-to-array. Each element is a
63 reference-to-hash whose keys are the fields of the issues table in the
69 my $dbh = C4::Context->dbh;
70 my $sth=$dbh->prepare("Select * from issues where date_due < now() and returndate is NULL order by borrowernumber");
72 # FIXME - Use push @results
75 while (my $data=$sth->fetchrow_hashref){
85 ($amount, $chargename, $message) =
86 &CalcFine($itemnumber, $borrowercode, $days_overdue);
88 Calculates the fine for a book.
90 The issuingrules table in the Koha database is a fine matrix, listing
91 the penalties for each type of patron for each type of item and each branch (e.g., the
92 standard fine for books might be $0.50, but $1.50 for DVDs, or staff
93 members might get a longer grace period between the first and second
94 reminders that a book is overdue).
96 The fine is calculated as follows: if it is time for the first
97 reminder, the fine is the value listed for the given (branch, item type,
98 borrower code) combination. If it is time for the second reminder, the
99 fine is doubled. Finally, if it is time to send the account to a
100 collection agency, the fine is set to 5 local monetary units (a really
101 good deal for the patron if the library is in Italy). Otherwise, the
104 Note that the way this function is currently implemented, it only
105 returns a nonzero value on the notable days listed above. That is, if
106 the issuingruless entry says to send a first reminder 7 days after the
107 book is due, then if you call C<&CalcFine> 7 days after the book is
108 due, it will give a nonzero fine. If you call C<&CalcFine> the next
109 day, however, it will say that the fine is 0.
111 C<$itemnumber> is the book's item number.
113 C<$borrowercode> is the borrower code of the patron who currently has
116 C<$days_overdue> is the number of days elapsed since the book's due
119 C<&CalcFine> returns a list of three values:
121 C<$amount> is the fine owed by the patron (see above).
123 C<$chargename> is the chargename field from the applicable record in
124 the issuingrules table, whatever that is.
126 C<$message> is a text message, either "First Notice", "Second Notice",
132 my ($itemnumber,$bortype,$difference)=@_;
133 my $dbh = C4::Context->dbh;
134 # Look up the issuingrules record for this book's item type and the
135 # given borrwer type.
136 # The reason this query is so messy is that it's a messy question:
137 # given the barcode, we can find the book's items record. This gives
138 # us the biblio record, which gives us a set of issuingrules
139 # records. Then we select the one that corresponds to the desired
142 # FIXME - Is it really necessary to get absolutely everything from
143 # all four tables? It looks as if this code only wants
144 # firstremind, chargeperiod, accountsent, and chargename from the
145 # issuingrules table.
147 my $sth=$dbh->prepare("Select * from items,biblio,itemtypes,issuingrules where items.itemnumber=?
148 and items.biblionumber=biblio.biblionumber and
149 biblio.itemtype=itemtypes.itemtype and
150 issuingrules.itemtype=itemtypes.itemtype and
151 issuingrules.categorycode=? ");
153 $sth->execute($itemnumber,$bortype);
154 my $data=$sth->fetchrow_hashref;
155 # FIXME - Error-checking: the item might be lost, or there
156 # might not be an entry in 'issuingrules' for this item type
162 # Is it time to send out the first reminder?
163 # FIXME - I'm not sure the "=="s are correct here. Let's say that
164 # $data->{firstremind} is today, but 'fines2.pl' doesn't run for
165 # some reason (the cron daemon died, the server crashed, the
166 # sysadmin had the machine down for maintenance, or whatever).
168 # Then the next day, the book is $data->{firstremind}+1 days
169 # overdue. But this function returns $amount == 0, $printout ==
170 # undef, on the assumption that 'fines2.pl' ran the previous day. So
171 # the first thing the patron gets is a second notice, but that's a
172 # week after the server crash, so people may not connect the two
174 if ($difference >= $data->{'firstremind'}){
175 # Yes. Set the fine as listed.
176 $amount=$data->{'fine'}* $difference;
177 $printout="First Notice";
180 # Is it time to send out a second reminder?
181 # my $second=$data->{'firstremind'}+$data->{'chargeperiod'};
182 # if ($difference == $second){
183 # # Yes. The fine is double.
184 # $amount=$data->{'fine'}*2;
185 # $printout="Second Notice";
188 # Is it time to send the account to a collection agency?
189 # FIXME - At least, I *think* that's what this code is doing.
190 if ($difference == $data->{'accountsent'} && $data->{'fine'} > 0){
191 # Yes. Set the fine at 5 local monetary units.
192 # FIXME - This '5' shouldn't be hard-wired.
193 $amount=$data->{'fine'}* $difference;
194 $printout="Final Notice";
196 return($amount,$data->{'chargename'},$printout);
201 &UpdateFine($itemnumber, $borrowernumber, $amount, $type, $description);
203 (Note: the following is mostly conjecture and guesswork.)
205 Updates the fine owed on an overdue book.
207 C<$itemnumber> is the book's item number.
209 C<$borrowernumber> is the borrower number of the patron who currently
210 has the book on loan.
212 C<$amount> is the current amount owed by the patron.
214 C<$type> will be used in the description of the fine.
216 C<$description> is a string that must be present in the description of
217 the fine. I think this is expected to be a date in DD/MM/YYYY format.
219 C<&UpdateFine> looks up the amount currently owed on the given item
220 and sets it to C<$amount>, creating, if necessary, a new entry in the
221 accountlines table of the Koha database.
225 # FIXME - This API doesn't look right: why should the caller have to
226 # specify both the item number and the borrower number? A book can't
227 # be on loan to two different people, so the item number should be
230 my ($itemnum,$bornum,$amount,$type,$due)=@_;
231 my $dbh = C4::Context->dbh;
232 # FIXME - What exactly is this query supposed to do? It looks up an
233 # entry in accountlines that matches the given item and borrower
234 # numbers, where the description contains $due, and where the
235 # account type has one of several values, but what does this _mean_?
236 # Does it look up existing fines for this item?
237 # FIXME - What are these various account types? ("FU", "O", "F", "M")
239 my $sth=$dbh->prepare("Select * from accountlines where itemnumber=? and
240 borrowernumber=? and (accounttype='FU' or accounttype='O' or
241 accounttype='F' or accounttype='M') ");
242 $sth->execute($itemnum,$bornum);
244 if (my $data=$sth->fetchrow_hashref){
245 # I think this if-clause deals with the case where we're updating
247 # print "in accounts ...";
248 if ($data->{'amount'} != $amount){
251 my $diff=$amount - $data->{'amount'};
252 my $out=$data->{'amountoutstanding'}+$diff;
253 my $sth2=$dbh->prepare("update accountlines set date=now(), amount=?,
254 amountoutstanding=?,accounttype='FU' where
256 $sth2->execute($amount,$out,$data->{'accountno'});
259 print "no update needed $data->{'amount'} \n";
262 # I think this else-clause deals with the case where we're adding
264 my $sth4=$dbh->prepare("select biblio.marc from biblio ,items where items.itemnumber=?
265 and biblio.biblionumber=items.biblionumber");
266 $sth4->execute($itemnum);
267 my $marc=$sth4->fetchrow;
269 my $record=MARC::File::USMARC::decode($marc,\&func_title);
270 my $title=$record->title();
271 # print "not in account";
272 my $sth3=$dbh->prepare("Select max(accountno) from accountlines");
274 # FIXME - Make $accountno a scalar.
275 my $accountno=$sth3->fetchrow;
278 my $sth2=$dbh->prepare("Insert into accountlines
279 (borrowernumber,itemnumber,date,amount,
280 description,accounttype,amountoutstanding,accountno) values
281 (?,?,now(),?,?,'FU',?,?)");
282 $sth2->execute($bornum,$itemnum,$amount,"$type $title $due",$amount,$accountno);
289 my ($tagno,$tagdata) = @_;
290 my ($titlef,$subf)=&MARCfind_marc_from_kohafield("title","biblios");
291 return ($tagno == $titlef );
296 $borrower = &BorType($borrowernumber);
298 Looks up a patron by borrower number.
300 C<$borrower> is a reference-to-hash whose keys are all of the fields
301 from the borrowers and categories tables of the Koha database. Thus,
302 C<$borrower> contains all information about both the borrower and
303 category he or she belongs to.
308 my ($borrowernumber)=@_;
309 my $dbh = C4::Context->dbh;
310 my $sth=$dbh->prepare("Select * from borrowers,categories where
312 borrowers.categorycode=categories.categorycode");
313 $sth->execute($borrowernumber);
314 my $data=$sth->fetchrow_hashref;
319 =item ReplacementCost
321 $cost = &ReplacementCost($itemnumber);
323 Returns the replacement cost of the item with the given item number.
329 my $dbh = C4::Context->dbh;
330 my ($itemrecord)=MARCgetitem($dbh,$itemnumber);
331 my $data=MARCmarc2koha($dbh,$itemrecord,"holdings");
332 return($data->{'replacementprice'});
335 my ( $itemnum, $bornum ) = @_;
336 my $dbh = C4::Context->dbh();
337 my $query = "SELECT sum(amountoutstanding) FROM accountlines
338 where accounttype like 'F%'
339 AND amountoutstanding > 0 AND itemnumber = ? AND borrowernumber=?";
340 my $sth = $dbh->prepare($query);
341 $sth->execute( $itemnum, $bornum );
342 my $data = $sth->fetchrow_hashref();
345 return ( $data->{'sum(amountoutstanding)'} );
348 sub ReplacementCost2 {
349 my ( $itemnum, $bornum ) = @_;
350 my $dbh = C4::Context->dbh();
351 my $query = "SELECT amountoutstanding FROM accountlines
352 where accounttype like 'L' AND amountoutstanding > 0 AND
353 itemnumber = ? AND borrowernumber= ?";
354 my $sth = $dbh->prepare($query);
355 $sth->execute( $itemnum, $bornum );
356 my $data = $sth->fetchrow_hashref();
359 return ( $data->{'amountoutstanding'} );
367 Koha Developement team <info@koha.org>