Deals with lost book charges now
[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 use strict;
7 require Exporter;
8 use DBI;
9 use C4::Database;
10 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
11
12 # set the version for version checking
13 $VERSION = 0.01;
14
15 @ISA = qw(Exporter);
16 @EXPORT = qw(&Getoverdues &CalcFine &BorType &UpdateFine &ReplacementCost);
17 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
18
19 # your exported package globals go here,
20 # as well as any optionally exported functions
21
22 @EXPORT_OK   = qw($Var1 %Hashit);
23
24
25 # non-exported package globals go here
26 use vars qw(@more $stuff);
27
28 # initalize package globals, first exported ones
29
30 my $Var1   = '';
31 my %Hashit = ();
32
33
34 # then the others (which are still accessible as $Some::Module::stuff)
35 my $stuff  = '';
36 my @more   = ();
37
38 # all file-scoped lexicals must be created before
39 # the functions below that use them.
40
41 # file-private lexicals go here
42 my $priv_var    = '';
43 my %secret_hash = ();
44
45 # here's a file-private function as a closure,
46 # callable as &$priv_func;  it cannot be prototyped.
47 my $priv_func = sub {
48   # stuff goes here.
49   };
50   
51 # make all your functions, whether exported or not;
52
53
54 sub Getoverdues{
55   my $dbh=C4Connect;
56   my $query="Select * from issues where date_due < now() and returndate is
57   NULL order by borrowernumber";
58   my $sth=$dbh->prepare($query);
59   $sth->execute;
60   my $i=0;
61   my @results;
62   while (my $data=$sth->fetchrow_hashref){
63     $results[$i]=$data;
64     $i++;
65   }
66   $sth->finish;
67   $dbh->disconnect;
68 #  print @results;
69   return($i,\@results);  
70 }
71
72 sub CalcFine {
73   my ($itemnumber,$bortype,$difference)=@_;
74   my $dbh=C4Connect;
75   my $query="Select * from items,biblioitems,itemtypes,categoryitem where items.itemnumber=$itemnumber
76   and items.biblioitemnumber=biblioitems.biblioitemnumber and
77   biblioitems.itemtype=itemtypes.itemtype and
78   categoryitem.itemtype=itemtypes.itemtype and
79   categoryitem.categorycode='$bortype' and (items.itemlost <> 1 or items.itemlost is NULL)";
80   my $sth=$dbh->prepare($query);
81 #  print $query;
82   $sth->execute;
83   my $data=$sth->fetchrow_hashref;
84   $sth->finish;
85   my $amount=0;
86   my $printout;
87   if ($difference == $data->{'firstremind'}){
88     $amount=$data->{'fine'};
89     $printout="First Notice";
90   }
91   my $second=$data->{'firstremind'}+$data->{'chargeperiod'};
92   if ($difference == $second){
93     $amount=$data->{'fine'}*2;
94     $printout="Second Notice";
95   }
96   if ($difference == $data->{'accountsent'} && $data->{'fine'} > 0){
97     $amount=5;
98     $printout="Final Notice";
99   }
100   $dbh->disconnect;
101   return($amount,$data->{'chargename'},$printout);
102 }
103
104 sub UpdateFine {
105   my ($itemnum,$bornum,$amount,$type,$due)=@_;
106   my $dbh=C4Connect;
107   my $query="Select * from accountlines where itemnumber=$itemnum and
108   borrowernumber=$bornum and (accounttype='FU' or accounttype='O' or
109   accounttype='F' or accounttype='M') and description like '%$due%'";
110   my $sth=$dbh->prepare($query);
111 #  print "$query\n";
112   $sth->execute;
113
114   if (my $data=$sth->fetchrow_hashref){
115 #    print "in accounts ...";
116     if ($data->{'amount'} != $amount){
117       
118 #      print "updating";
119       my $diff=$amount - $data->{'amount'};
120       my $out=$data->{'amountoutstanding'}+$diff;
121       my $query2="update accountlines set date=now(), amount=$amount,
122       amountoutstanding=$out,accounttype='FU' where
123       borrowernumber=$data->{'borrowernumber'} and itemnumber=$data->{'itemnumber'}
124       and (accounttype='FU' or accounttype='O') and description like '%$due%'";
125       my $sth2=$dbh->prepare($query2);
126       $sth2->execute;
127       $sth2->finish;      
128     } else {
129 #      print "no update needed $data->{'amount'}"
130     }
131   } else {
132     my $query2="select title from biblio,items where items.itemnumber=$itemnum
133     and biblio.biblionumber=items.biblionumber";
134     my $sth4=$dbh->prepare($query2);
135     $sth4->execute;
136     my $title=$sth4->fetchrow_hashref;
137     $sth4->finish;
138  #   print "not in account";
139     my $query2="Select max(accountno) from accountlines";
140     my $sth3=$dbh->prepare($query2);
141     $sth3->execute;
142     my @accountno=$sth3->fetchrow_array;
143     $sth3->finish;
144     $accountno[0]++;
145     $title->{'title'}=~ s/\'/\\\'/g;
146     $query2="Insert into accountlines
147     (borrowernumber,itemnumber,date,amount,
148     description,accounttype,amountoutstanding,accountno) values
149     ($bornum,$itemnum,now(),$amount,'$type $title->{'title'} $due','FU',
150     $amount,$accountno[0])";
151     my $sth2=$dbh->prepare($query2);
152     $sth2->execute;
153     $sth2->finish;
154   }
155   $sth->finish;
156   $dbh->disconnect;
157 }
158
159 sub BorType {
160   my ($borrowernumber)=@_;
161   my $dbh=C4Connect;
162   my $query="Select * from borrowers,categories where 
163   borrowernumber=$borrowernumber and
164 borrowers.categorycode=categories.categorycode";
165   my $sth=$dbh->prepare($query);
166   $sth->execute;
167   my $data=$sth->fetchrow_hashref;
168   $sth->finish;
169   $dbh->disconnect;
170   return($data);
171 }
172
173 sub ReplacementCost{
174   my ($itemnum)=@_;
175   my $dbh=C4Connect;
176   my $query="Select replacementprice from items where itemnumber='$itemnum'";
177   my $sth=$dbh->prepare($query);
178   $sth->execute;
179   my $data=$sth->fetchrow_hashref;
180   $sth->finish;
181   $dbh->disconnect;
182   return($data->{'replacementprice'});
183 }
184
185 END { }       # module clean-up code here (global destructor)
186   
187