Added an incomplete POD, as well as some FIXME comments (which, as it
[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::Database;
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=C4Connect;
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   $dbh->disconnect;
86 #  print @results;
87   return($i,\@results);  
88 }
89
90 sub CalcFine {
91   my ($itemnumber,$bortype,$difference)=@_;
92   my $dbh=C4Connect;
93   my $query="Select * from items,biblioitems,itemtypes,categoryitem where items.itemnumber=$itemnumber
94   and items.biblioitemnumber=biblioitems.biblioitemnumber and
95   biblioitems.itemtype=itemtypes.itemtype and
96   categoryitem.itemtype=itemtypes.itemtype and
97   categoryitem.categorycode='$bortype' and (items.itemlost <> 1 or items.itemlost is NULL)";
98   my $sth=$dbh->prepare($query);
99 #  print $query;
100   $sth->execute;
101   my $data=$sth->fetchrow_hashref;
102   $sth->finish;
103   my $amount=0;
104   my $printout;
105   if ($difference == $data->{'firstremind'}){
106     $amount=$data->{'fine'};
107     $printout="First Notice";
108   }
109   my $second=$data->{'firstremind'}+$data->{'chargeperiod'};
110   if ($difference == $second){
111     $amount=$data->{'fine'}*2;
112     $printout="Second Notice";
113   }
114   if ($difference == $data->{'accountsent'} && $data->{'fine'} > 0){
115     $amount=5;
116     $printout="Final Notice";
117   }
118   $dbh->disconnect;
119   return($amount,$data->{'chargename'},$printout);
120 }
121
122 sub UpdateFine {
123   my ($itemnum,$bornum,$amount,$type,$due)=@_;
124   my $dbh=C4Connect;
125   my $query="Select * from accountlines where itemnumber=$itemnum and
126   borrowernumber=$bornum and (accounttype='FU' or accounttype='O' or
127   accounttype='F' or accounttype='M') and description like '%$due%'";
128   my $sth=$dbh->prepare($query);
129 #  print "$query\n";
130   $sth->execute;
131
132   if (my $data=$sth->fetchrow_hashref){
133 #    print "in accounts ...";
134     if ($data->{'amount'} != $amount){
135       
136 #      print "updating";
137       my $diff=$amount - $data->{'amount'};
138       my $out=$data->{'amountoutstanding'}+$diff;
139       my $query2="update accountlines set date=now(), amount=$amount,
140       amountoutstanding=$out,accounttype='FU' where
141       borrowernumber=$data->{'borrowernumber'} and itemnumber=$data->{'itemnumber'}
142       and (accounttype='FU' or accounttype='O') and description like '%$due%'";
143       my $sth2=$dbh->prepare($query2);
144       $sth2->execute;
145       $sth2->finish;      
146     } else {
147 #      print "no update needed $data->{'amount'}"
148     }
149   } else {
150     my $query2="select title from biblio,items where items.itemnumber=$itemnum
151     and biblio.biblionumber=items.biblionumber";
152     my $sth4=$dbh->prepare($query2);
153     $sth4->execute;
154     my $title=$sth4->fetchrow_hashref;
155     $sth4->finish;
156  #   print "not in account";
157     my $query2="Select max(accountno) from accountlines";
158     my $sth3=$dbh->prepare($query2);
159     $sth3->execute;
160     my @accountno=$sth3->fetchrow_array;
161     $sth3->finish;
162     $accountno[0]++;
163     $title->{'title'}=~ s/\'/\\\'/g;
164     $query2="Insert into accountlines
165     (borrowernumber,itemnumber,date,amount,
166     description,accounttype,amountoutstanding,accountno) values
167     ($bornum,$itemnum,now(),$amount,'$type $title->{'title'} $due','FU',
168     $amount,$accountno[0])";
169     my $sth2=$dbh->prepare($query2);
170     $sth2->execute;
171     $sth2->finish;
172   }
173   $sth->finish;
174   $dbh->disconnect;
175 }
176
177 sub BorType {
178   my ($borrowernumber)=@_;
179   my $dbh=C4Connect;
180   my $query="Select * from borrowers,categories where 
181   borrowernumber=$borrowernumber and
182 borrowers.categorycode=categories.categorycode";
183   my $sth=$dbh->prepare($query);
184   $sth->execute;
185   my $data=$sth->fetchrow_hashref;
186   $sth->finish;
187   $dbh->disconnect;
188   return($data);
189 }
190
191 sub ReplacementCost{
192   my ($itemnum)=@_;
193   my $dbh=C4Connect;
194   my $query="Select replacementprice from items where itemnumber='$itemnum'";
195   my $sth=$dbh->prepare($query);
196   $sth->execute;
197   my $data=$sth->fetchrow_hashref;
198   $sth->finish;
199   $dbh->disconnect;
200   return($data->{'replacementprice'});
201 }
202
203 END { }       # module clean-up code here (global destructor)
204   
205