Fixing a glitch in the fines routine, was failing if adding a new fine to
[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);
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
80 items.itemlost is NULL)";
81   my $sth=$dbh->prepare($query);
82 #  print $query;
83   $sth->execute;
84   my $data=$sth->fetchrow_hashref;
85   $sth->finish;
86   my $amount=0;
87   my $printout;
88   if ($difference == $data->{'firstremind'}){
89     $amount=$data->{'fine'};
90     $printout="First Notice";
91   }
92   my $second=$data->{'firstremind'}+$data->{'chargeperiod'};
93   if ($difference == $second){
94     $amount=$data->{'fine'}*2;
95     $printout="Second Notice";
96   }
97   if ($difference == $data->{'accountsent'} && $data->{'fine'} > 0){
98     $amount=5;
99     $printout="Final Notice";
100   }
101   $dbh->disconnect;
102   return($amount,$data->{'chargename'},$printout);
103 }
104
105 sub UpdateFine {
106   my ($itemnum,$bornum,$amount,$type,$due)=@_;
107   my $dbh=C4Connect;
108   my $query="Select * from accountlines where itemnumber=$itemnum and
109   borrowernumber=$bornum and (accounttype='FU' or accounttype='O' or
110   accounttype='F' or accounttype='M') and description like '%$due%'";
111   my $sth=$dbh->prepare($query);
112 #  print "$query\n";
113   $sth->execute;
114
115   if (my $data=$sth->fetchrow_hashref){
116 #    print "in accounts ...";
117     if ($data->{'amount'} != $amount){
118       
119 #      print "updating";
120       my $diff=$amount - $data->{'amount'};
121       my $out=$data->{'amountoutstanding'}+$diff;
122       my $query2="update accountlines set date=now(), amount=$amount,
123       amountoutstanding=$out,accounttype='FU' where
124       borrowernumber=$data->{'borrowernumber'} and itemnumber=$data->{'itemnumber'}
125       and (accounttype='FU' or accounttype='O');";
126       my $sth2=$dbh->prepare($query2);
127       $sth2->execute;
128       $sth2->finish;      
129     } else {
130 #      print "no update needed $data->{'amount'}"
131     }
132   } else {
133     my $query2="select title from biblio,items where items.itemnumber=$itemnum
134     and biblio.biblionumber=items.biblionumber";
135     my $sth4=$dbh->prepare($query2);
136     $sth4->execute;
137     my $title=$sth4->fetchrow_hashref;
138     $sth4->finish;
139  #   print "not in account";
140     my $query2="Select max(accountno) from accountlines";
141     my $sth3=$dbh->prepare($query2);
142     $sth3->execute;
143     my @accountno=$sth3->fetchrow_array;
144     $sth3->finish;
145     $accountno[0]++;
146     $title->{'title'}=~ s/\'/\\\'/g;
147     $query2="Insert into accountlines
148     (borrowernumber,itemnumber,date,amount,
149     description,accounttype,amountoutstanding,accountno) values
150     ($bornum,$itemnum,now(),$amount,'$type $title->{'title'} $due','FU',
151     $amount,$accountno[0])";
152     my $sth2=$dbh->prepare($query2);
153     $sth2->execute;
154     $sth2->finish;
155   }
156   $sth->finish;
157   $dbh->disconnect;
158 }
159
160 sub BorType {
161   my ($borrowernumber)=@_;
162   my $dbh=C4Connect;
163   my $query="Select * from borrowers,categories where 
164   borrowernumber=$borrowernumber and
165 borrowers.categorycode=categories.categorycode";
166   my $sth=$dbh->prepare($query);
167   $sth->execute;
168   my $data=$sth->fetchrow_hashref;
169   $sth->finish;
170   $dbh->disconnect;
171   return($data);
172 }
173
174
175 END { }       # module clean-up code here (global destructor)
176   
177