Savannah seems out of sync. reloading.Mainly code cleaning and removing of Date:...
[koha.git] / C4 / Circulation / Fines.pm
1 package C4::Circulation::Fines;
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
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
12 # version.
13 #
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.
17 #
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
21
22 use strict;
23 require Exporter;
24
25 use C4::Context;
26 use C4::Biblio;
27 use vars qw($VERSION @ISA @EXPORT);
28
29 # set the version for version checking
30 $VERSION = 0.01;
31
32 =head1 NAME
33
34 C4::Circulation::Fines - Koha module dealing with fines
35
36 =head1 SYNOPSIS
37
38   use C4::Circulation::Fines;
39
40 =head1 DESCRIPTION
41
42 This module contains several functions for dealing with fines for
43 overdue items. It is primarily used by the 'misc/fines2.pl' script.
44
45 =head1 FUNCTIONS
46
47 =over 2
48
49 =cut
50
51 @ISA = qw(Exporter);
52 @EXPORT = qw(&Getoverdues &CalcFine &BorType &UpdateFine &ReplacementCost);
53
54 =item Getoverdues
55
56   ($count, $overdues) = &Getoverdues();
57
58 Returns the list of all overdue books.
59
60 C<$count> is the number of elements in C<@{$overdues}>.
61
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
64 Koha database.
65
66 =cut
67 #'
68 sub Getoverdues{
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");
71   $sth->execute;
72   # FIXME - Use push @results
73   my $i=0;
74   my @results;
75   while (my $data=$sth->fetchrow_hashref){
76   push  @results,$data;
77     $i++;
78   }
79   $sth->finish;
80   return($i,\@results);
81 }
82
83 =item CalcFine
84
85   ($amount, $chargename, $message) =
86         &CalcFine($itemnumber, $borrowercode, $days_overdue);
87
88 Calculates the fine for a book.
89
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).
95
96
97
98 C<$itemnumber> is the book's item number.
99
100 C<$borrowercode> is the borrower code of the patron who currently has
101 the book.
102
103 C<$days_overdue> is the number of days elapsed since the book's due
104 date.
105
106 C<&CalcFine> returns a list of three values:
107
108 C<$amount> is the fine owed by the patron (see above).
109
110 C<$chargename> is the chargename field from the applicable record in
111 the issuingrules table, whatever that is.
112
113 C<$message> is a text message, either "First Notice", "Second Notice",
114 or "Final Notice".
115
116 =cut
117 #'
118 sub CalcFine {
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.
123  
124
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=? ");
130 #  print $query;
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
135         # or borrower type.
136   $sth->finish;
137   my $amount=0;
138   my $printout;
139
140   if ($difference > $data->{'firstremind'}){
141     # Yes. Set the fine as listed.
142 $amount=$data->{'fine'}* $difference;
143
144     $printout="First Notice";
145   }
146
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;
151
152     $printout="Second Notice";
153   }
154
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;
159
160     $printout="Final Notice";
161   }
162   return($amount,$data->{'chargename'},$printout);
163 }
164
165 =item UpdateFine
166
167   &UpdateFine($itemnumber, $borrowernumber, $amount, $type, $description);
168
169 (Note: the following is mostly conjecture and guesswork.)
170
171 Updates the fine owed on an overdue book.
172
173 C<$itemnumber> is the book's item number.
174
175 C<$borrowernumber> is the borrower number of the patron who currently
176 has the book on loan.
177
178 C<$amount> is the current amount owed by the patron.
179
180 C<$type> will be used in the description of the fine.
181
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.
184
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.
188
189 =cut
190 #'
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
194 # sufficient.
195 sub UpdateFine {
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")
204
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);
209
210   if (my $data=$sth->fetchrow_hashref){
211     # I think this if-clause deals with the case where we're updating
212     # an existing fine.
213 #    print "in accounts ...";
214     if ($data->{'amount'} != $amount){
215
216 #     print "updating";
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
221       accountid=?");
222       $sth2->execute($amount,$out,$data->{'accountid'});
223       $sth2->finish;
224    } else {
225       print "no update needed $data->{'amount'} \n";
226     }
227   } else {
228     # I think this else-clause deals with the case where we're adding
229     # a new fine.
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;
234     $sth4->finish;
235  #   print "not in account";
236     my $sth3=$dbh->prepare("Select max(accountno) from accountlines");
237     $sth3->execute;
238     # FIXME - Make $accountno a scalar.
239     my $accountno=$sth3->fetchrow;
240     $sth3->finish;
241     $accountno++;
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);
247     $sth2->finish;
248   }
249   $sth->finish;
250 }
251
252
253
254 =item BorType
255
256   $borrower = &BorType($borrowernumber);
257
258 Looks up a patron by borrower number.
259
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.
264
265 =cut
266 #'
267 sub BorType {
268   my ($borrowernumber)=@_;
269   my $dbh = C4::Context->dbh;
270   my $sth=$dbh->prepare("Select * from borrowers,categories where
271   borrowernumber=? and
272 borrowers.categorycode=categories.categorycode");
273   $sth->execute($borrowernumber);
274   my $data=$sth->fetchrow_hashref;
275   $sth->finish;
276   return($data);
277 }
278
279 =item ReplacementCost
280
281   $cost = &ReplacementCost($itemnumber);
282
283 Returns the replacement cost of the item with the given item number.
284
285 =cut
286 #'
287 sub ReplacementCost{
288   my ($itemnumber)=@_;
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);
294 }
295
296 1;
297 __END__
298
299 =back
300
301 =head1 AUTHOR
302
303 Koha Developement team <info@koha.org>
304
305 =cut