Added a FIXME comment.
[koha.git] / C4 / Circulation / Fines.pm
1 package C4::Circulation::Fines; #asummes C4/Circulation/Fines
2
3 #requires DBI.pm to be installed
4 #uses DBD:Pg
5
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 use strict;
25 require Exporter;
26 use DBI;
27 use C4::Context;
28 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
29
30 # set the version for version checking
31 $VERSION = 0.01;
32
33 @ISA = qw(Exporter);
34 @EXPORT = qw(&Getoverdues &CalcFine &BorType &UpdateFine &ReplacementCost);
35 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
36
37 # your exported package globals go here,
38 # as well as any optionally exported functions
39
40 @EXPORT_OK   = qw($Var1 %Hashit);
41
42
43 # non-exported package globals go here
44 use vars qw(@more $stuff);
45
46 # initalize package globals, first exported ones
47
48 my $Var1   = '';
49 my %Hashit = ();
50
51
52 # then the others (which are still accessible as $Some::Module::stuff)
53 my $stuff  = '';
54 my @more   = ();
55
56 # all file-scoped lexicals must be created before
57 # the functions below that use them.
58
59 # file-private lexicals go here
60 my $priv_var    = '';
61 my %secret_hash = ();
62
63 # here's a file-private function as a closure,
64 # callable as &$priv_func;  it cannot be prototyped.
65 my $priv_func = sub {
66   # stuff goes here.
67   };
68   
69 # make all your functions, whether exported or not;
70
71
72 sub Getoverdues{
73   my $dbh = C4::Context->dbh;
74   my $query="Select * from issues where date_due < now() and returndate is
75   NULL order by borrowernumber";
76   my $sth=$dbh->prepare($query);
77   $sth->execute;
78   my $i=0;
79   my @results;
80   while (my $data=$sth->fetchrow_hashref){
81     $results[$i]=$data;
82     $i++;
83   }
84   $sth->finish;
85 #  print @results;
86   return($i,\@results);  
87 }
88
89 sub CalcFine {
90   my ($itemnumber,$bortype,$difference)=@_;
91   my $dbh = C4::Context->dbh;
92   my $query="Select * from items,biblioitems,itemtypes,categoryitem where items.itemnumber=$itemnumber
93   and items.biblioitemnumber=biblioitems.biblioitemnumber and
94   biblioitems.itemtype=itemtypes.itemtype and
95   categoryitem.itemtype=itemtypes.itemtype and
96   categoryitem.categorycode='$bortype' and (items.itemlost <> 1 or items.itemlost is NULL)";
97   my $sth=$dbh->prepare($query);
98 #  print $query;
99   $sth->execute;
100   my $data=$sth->fetchrow_hashref;
101   $sth->finish;
102   my $amount=0;
103   my $printout;
104   if ($difference == $data->{'firstremind'}){
105     $amount=$data->{'fine'};
106     $printout="First Notice";
107   }
108   my $second=$data->{'firstremind'}+$data->{'chargeperiod'};
109   if ($difference == $second){
110     $amount=$data->{'fine'}*2;
111     $printout="Second Notice";
112   }
113   if ($difference == $data->{'accountsent'} && $data->{'fine'} > 0){
114     $amount=5;
115     $printout="Final Notice";
116   }
117   return($amount,$data->{'chargename'},$printout);
118 }
119
120 sub UpdateFine {
121   my ($itemnum,$bornum,$amount,$type,$due)=@_;
122   my $dbh = C4::Context->dbh;
123   my $query="Select * from accountlines where itemnumber=$itemnum and
124   borrowernumber=$bornum and (accounttype='FU' or accounttype='O' or
125   accounttype='F' or accounttype='M') and description like '%$due%'";
126   my $sth=$dbh->prepare($query);
127 #  print "$query\n";
128   $sth->execute;
129
130   if (my $data=$sth->fetchrow_hashref){
131 #    print "in accounts ...";
132     if ($data->{'amount'} != $amount){
133       
134 #      print "updating";
135       my $diff=$amount - $data->{'amount'};
136       my $out=$data->{'amountoutstanding'}+$diff;
137       my $query2="update accountlines set date=now(), amount=$amount,
138       amountoutstanding=$out,accounttype='FU' where
139       borrowernumber=$data->{'borrowernumber'} and itemnumber=$data->{'itemnumber'}
140       and (accounttype='FU' or accounttype='O') and description like '%$due%'";
141       my $sth2=$dbh->prepare($query2);
142       $sth2->execute;
143       $sth2->finish;      
144     } else {
145 #      print "no update needed $data->{'amount'}"
146     }
147   } else {
148     my $query2="select title from biblio,items where items.itemnumber=$itemnum
149     and biblio.biblionumber=items.biblionumber";
150     my $sth4=$dbh->prepare($query2);
151     $sth4->execute;
152     my $title=$sth4->fetchrow_hashref;
153     $sth4->finish;
154  #   print "not in account";
155     # FIXME - There's already a $query2 in this scope.
156     my $query2="Select max(accountno) from accountlines";
157     my $sth3=$dbh->prepare($query2);
158     $sth3->execute;
159     my @accountno=$sth3->fetchrow_array;
160     $sth3->finish;
161     $accountno[0]++;
162     $title->{'title'}=~ s/\'/\\\'/g;
163     $query2="Insert into accountlines
164     (borrowernumber,itemnumber,date,amount,
165     description,accounttype,amountoutstanding,accountno) values
166     ($bornum,$itemnum,now(),$amount,'$type $title->{'title'} $due','FU',
167     $amount,$accountno[0])";
168     my $sth2=$dbh->prepare($query2);
169     $sth2->execute;
170     $sth2->finish;
171   }
172   $sth->finish;
173 }
174
175 sub BorType {
176   my ($borrowernumber)=@_;
177   my $dbh = C4::Context->dbh;
178   my $query="Select * from borrowers,categories where 
179   borrowernumber=$borrowernumber and
180 borrowers.categorycode=categories.categorycode";
181   my $sth=$dbh->prepare($query);
182   $sth->execute;
183   my $data=$sth->fetchrow_hashref;
184   $sth->finish;
185   return($data);
186 }
187
188 sub ReplacementCost{
189   my ($itemnum)=@_;
190   my $dbh = C4::Context->dbh;
191   my $query="Select replacementprice from items where itemnumber='$itemnum'";
192   my $sth=$dbh->prepare($query);
193   $sth->execute;
194   my $data=$sth->fetchrow_hashref;
195   $sth->finish;
196   return($data->{'replacementprice'});
197 }
198
199 END { }       # module clean-up code here (global destructor)
200   
201