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);
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).
98 C<$itemnumber> is the book's item number.
100 C<$borrowercode> is the borrower code of the patron who currently has
103 C<$days_overdue> is the number of days elapsed since the book's due
106 C<&CalcFine> returns a list of three values:
108 C<$amount> is the fine owed by the patron (see above).
110 C<$chargename> is the chargename field from the applicable record in
111 the issuingrules table, whatever that is.
113 C<$message> is a text message, either "First Notice", "Second Notice",
119 my ($itemnumber,$bortype,$difference)=@_;
120 my $dbh = C4::Context->dbh;
121 # Look up the issuingrules record for this book's item type and the
122 # given borrwer type.
125 my $sth=$dbh->prepare("Select * from items,biblio,itemtypes,issuingrules where items.itemnumber=?
126 and items.biblionumber=biblio.biblionumber and
127 biblio.itemtype=itemtypes.itemtype and
128 issuingrules.itemtype=itemtypes.itemtype and
129 issuingrules.categorycode=? ");
131 $sth->execute($itemnumber,$bortype);
132 my $data=$sth->fetchrow_hashref;
133 # FIXME - Error-checking: the item might be lost, or there
134 # might not be an entry in 'issuingrules' for this item type
140 if ($difference > $data->{'firstremind'}){
141 # Yes. Set the fine as listed.
142 $amount=$data->{'fine'}* $difference;
144 $printout="First Notice";
147 # Is it time to send out a second reminder?
148 my $second=$data->{'firstremind'}+$data->{chargeperiod};
149 if ($difference == $second){
150 $amount=$data->{'fine'}* $difference;
152 $printout="Second Notice";
155 # Is it time to send the account to a collection agency?
156 # FIXME -This $data->{'accountsent'} is not seemed to be set in the DB
157 if ($difference == $data->{'accountsent'}){
158 $amount=$data->{'fine'}* $difference;
160 $printout="Final Notice";
162 return($amount,$data->{'chargename'},$printout);
167 &UpdateFine($itemnumber, $borrowernumber, $amount, $type, $description);
169 (Note: the following is mostly conjecture and guesswork.)
171 Updates the fine owed on an overdue book.
173 C<$itemnumber> is the book's item number.
175 C<$borrowernumber> is the borrower number of the patron who currently
176 has the book on loan.
178 C<$amount> is the current amount owed by the patron.
180 C<$type> will be used in the description of the fine.
182 C<$description> is a string that must be present in the description of
183 the fine. I think this is expected to be a date in DD/MM/YYYY format.
185 C<&UpdateFine> looks up the amount currently owed on the given item
186 and sets it to C<$amount>, creating, if necessary, a new entry in the
187 accountlines table of the Koha database.
191 # FIXME - This API doesn't look right: why should the caller have to
192 # specify both the item number and the borrower number? A book can't
193 # be on loan to two different people, so the item number should be
196 my ($itemnum,$bornum,$amount,$type,$due)=@_;
197 my $dbh = C4::Context->dbh;
198 # FIXME - What exactly is this query supposed to do? It looks up an
199 # entry in accountlines that matches the given item and borrower
200 # numbers, where the description contains $due, and where the
201 # account type has one of several values, but what does this _mean_?
202 # Does it look up existing fines for this item?
203 # FIXME - What are these various account types? ("FU", "O", "F", "M")
205 my $sth=$dbh->prepare("Select * from accountlines where itemnumber=? and
206 borrowernumber=? and (accounttype='FU' or accounttype='O' or
207 accounttype='F' or accounttype='M') ");
208 $sth->execute($itemnum,$bornum);
210 if (my $data=$sth->fetchrow_hashref){
211 # I think this if-clause deals with the case where we're updating
213 # print "in accounts ...";
214 if ($data->{'amount'} != $amount){
217 my $diff=$amount - $data->{'amount'};
218 my $out=$data->{'amountoutstanding'}+$diff;
219 my $sth2=$dbh->prepare("update accountlines set date=now(), amount=?,
220 amountoutstanding=?,accounttype='FU' where
222 $sth2->execute($amount,$out,$data->{'accountid'});
225 print "no update needed $data->{'amount'} \n";
228 # I think this else-clause deals with the case where we're adding
230 my $sth4=$dbh->prepare("select title from biblio ,items where items.itemnumber=?
231 and biblio.biblionumber=items.biblionumber");
232 $sth4->execute($itemnum);
233 my $title=$sth4->fetchrow;
235 # print "not in account";
236 my $sth3=$dbh->prepare("Select max(accountno) from accountlines");
238 # FIXME - Make $accountno a scalar.
239 my $accountno=$sth3->fetchrow;
242 my $sth2=$dbh->prepare("Insert into accountlines
243 (borrowernumber,itemnumber,date,amount,
244 description,accounttype,amountoutstanding,accountno) values
245 (?,?,now(),?,?,'FU',?,?)");
246 $sth2->execute($bornum,$itemnum,$amount,"$type $title $due",$amount,$accountno);
256 $borrower = &BorType($borrowernumber);
258 Looks up a patron by borrower number.
260 C<$borrower> is a reference-to-hash whose keys are all of the fields
261 from the borrowers and categories tables of the Koha database. Thus,
262 C<$borrower> contains all information about both the borrower and
263 category he or she belongs to.
268 my ($borrowernumber)=@_;
269 my $dbh = C4::Context->dbh;
270 my $sth=$dbh->prepare("Select * from borrowers,categories where
272 borrowers.categorycode=categories.categorycode");
273 $sth->execute($borrowernumber);
274 my $data=$sth->fetchrow_hashref;
279 =item ReplacementCost
281 $cost = &ReplacementCost($itemnumber);
283 Returns the replacement cost of the item with the given item number.
289 my $dbh = C4::Context->dbh;
290 my ($itemrecord)=XMLgetitem($dbh,$itemnumber);
291 $itemrecord=XML_xml2hash_onerecord($itemrecord);
292 my $replacementprice=XML_readline_onerecord($itemrecord,"replacementprice","holdings");
293 return($replacementprice);
303 Koha Developement team <info@koha.org>