Cleaned things up a bit.
[koha.git] / C4 / Circulation / Renewals2.pm
1 package C4::Circulation::Renewals2; #assumes C4/Circulation/Renewals2.pm
2
3 #package to deal with Renewals
4 #written 7/11/99 by olwen@katipo.co.nz
5
6 #modified by chris@katipo.co.nz
7 #18/1/2000 
8 #need to update stats with renewals
9
10
11 # Copyright 2000-2002 Katipo Communications
12 #
13 # This file is part of Koha.
14 #
15 # Koha is free software; you can redistribute it and/or modify it under the
16 # terms of the GNU General Public License as published by the Free Software
17 # Foundation; either version 2 of the License, or (at your option) any later
18 # version.
19 #
20 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
21 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License along with
25 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
26 # Suite 330, Boston, MA  02111-1307 USA
27
28 use strict;
29 require Exporter;
30 use DBI;
31 use C4::Stats;
32 use C4::Accounts2;
33 use C4::Circulation::Circ2;
34
35 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
36   
37 # set the version for version checking
38 $VERSION = 0.01;
39     
40 @ISA = qw(Exporter);
41 @EXPORT = qw(&renewstatus &renewbook &calc_charges);
42 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
43                   
44 # your exported package globals go here,
45 # as well as any optionally exported functions
46
47 @EXPORT_OK   = qw($Var1 %Hashit);
48
49
50 # non-exported package globals go here
51 use vars qw(@more $stuff);
52         
53 # initalize package globals, first exported ones
54
55 my $Var1   = '';
56 my %Hashit = ();
57                     
58 # then the others (which are still accessible as $Some::Module::stuff)
59 my $stuff  = '';
60 my @more   = ();
61         
62 # all file-scoped lexicals must be created before
63 # the functions below that use them.
64                 
65 # file-private lexicals go here
66 my $priv_var    = '';
67 my %secret_hash = ();
68                             
69 # here's a file-private function as a closure,
70 # callable as &$priv_func;  it cannot be prototyped.
71 my $priv_func = sub {
72   # stuff goes here.
73 };
74                                                     
75 # make all your functions, whether exported or not;
76
77
78 sub Return  {
79   
80 }    
81
82 sub renewstatus {
83   # check renewal status
84   my ($env,$bornum,$itemno)=@_;
85   my $dbh = C4::Context->dbh;
86   my $renews = 1;
87   my $renewokay = 0;
88   my $q1 = "select * from issues 
89     where (borrowernumber = '$bornum')
90     and (itemnumber = '$itemno') 
91     and returndate is null";
92   my $sth1 = $dbh->prepare($q1);
93   $sth1->execute;
94   if (my $data1 = $sth1->fetchrow_hashref) {
95     my $q2 = "select renewalsallowed from items,biblioitems,itemtypes
96        where (items.itemnumber = '$itemno')
97        and (items.biblioitemnumber = biblioitems.biblioitemnumber) 
98        and (biblioitems.itemtype = itemtypes.itemtype)";
99     my $sth2 = $dbh->prepare($q2);
100     $sth2->execute;     
101     if (my $data2=$sth2->fetchrow_hashref) {
102       $renews = $data2->{'renewalsallowed'};
103     }
104     if ($renews > $data1->{'renewals'}) {
105       $renewokay = 1;
106     }
107     $sth2->finish;
108   }   
109   $sth1->finish;
110   return($renewokay);    
111 }
112
113
114 sub renewbook {
115   # mark book as renewed
116   my ($env,$bornum,$itemno,$datedue)=@_;
117   my $dbh = C4::Context->dbh;
118   if ($datedue eq "" ) {    
119     #debug_msg($env, "getting date");
120     my $loanlength=21;
121     my $query= "Select * from biblioitems,items,itemtypes
122        where (items.itemnumber = '$itemno')
123        and (biblioitems.biblioitemnumber = items.biblioitemnumber)
124        and (biblioitems.itemtype = itemtypes.itemtype)";
125     my $sth=$dbh->prepare($query);
126     $sth->execute;
127     if (my $data=$sth->fetchrow_hashref) {
128       $loanlength = $data->{'loanlength'}
129     }
130     $sth->finish;
131     my $ti = time;
132     my $datedu = time + ($loanlength * 86400);
133     my @datearr = localtime($datedu);
134     $datedue = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
135   }
136   my $issquery = "select * from issues where borrowernumber='$bornum' and
137     itemnumber='$itemno' and returndate is null";
138   my $sth=$dbh->prepare($issquery);
139   $sth->execute;
140   my $issuedata=$sth->fetchrow_hashref;
141   $sth->finish;
142   my $renews = $issuedata->{'renewals'} +1;
143   my $updquery = "update issues 
144     set date_due = '$datedue', renewals = '$renews'
145     where borrowernumber='$bornum' and
146     itemnumber='$itemno' and returndate is null";
147   $sth=$dbh->prepare($updquery);
148   $sth->execute;
149   $sth->finish;
150   UpdateStats($env,$env->{'branchcode'},'renew','','',$itemno);
151   my ($charge,$type)=calc_charges($env, $itemno, $bornum);  
152   if ($charge > 0){
153     my $accountno=getnextacctno($env,$bornum,$dbh);
154     my $item=getiteminformation($env, $itemno);
155     my $account="Insert into accountlines
156     (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber)
157     values 
158     ('$bornum','$accountno',now(),$charge,'Renewal of Rental Item $item->{'title'} $item->{'barcode'}','Rent',$charge,'$itemno')";
159     $sth=$dbh->prepare($account);
160     $sth->execute;
161     $sth->finish;
162 #     print $account;
163   }
164  
165 #  return();
166 }
167
168 # FIXME - This is very similar to
169 # &C4::Circulation::Issues::calc_charges and
170 # &C4::Circulation::Circ2::calc_charges.
171 # Pick one and stick with it.
172 sub calc_charges {         
173   # calculate charges due         
174   my ($env, $itemno, $bornum)=@_;           
175   my $charge=0;   
176   my $dbh = C4::Context->dbh;
177   my $item_type;               
178   my $q1 = "select itemtypes.itemtype,rentalcharge from
179   items,biblioitems,itemtypes     
180   where (items.itemnumber ='$itemno')         
181   and (biblioitems.biblioitemnumber = items.biblioitemnumber) 
182   and (biblioitems.itemtype = itemtypes.itemtype)";                 
183   my $sth1= $dbh->prepare($q1);                     
184   $sth1->execute;                       
185   if (my $data1=$sth1->fetchrow_hashref) {    
186     $item_type = $data1->{'itemtype'};     
187     $charge = $data1->{'rentalcharge'};
188     my $q2 = "select rentaldiscount from 
189     borrowers,categoryitem                        
190     where (borrowers.borrowernumber = '$bornum')         
191     and (borrowers.categorycode = categoryitem.categorycode)   
192     and (categoryitem.itemtype = '$item_type')";   
193     my $sth2=$dbh->prepare($q2);           
194     $sth2->execute;        
195     if (my$data2=$sth2->fetchrow_hashref) {                                           
196       my $discount = $data2->{'rentaldiscount'};         
197       $charge = ($charge *(100 - $discount)) / 100;                 
198     }                         
199     $sth2->finish;                              
200   }                                   
201   $sth1->finish;  
202 #  print "item $item_type";
203   return ($charge,$item_type);         
204 }       
205
206
207 END { }       # module clean-up code here (global destructor)