Made some changes to checkreserve and find_reserves, so that items are always display...
[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 use strict;
11 require Exporter;
12 use DBI;
13 use C4::Database;
14 use C4::Stats;
15 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
16   
17 # set the version for version checking
18 $VERSION = 0.01;
19     
20 @ISA = qw(Exporter);
21 @EXPORT = qw(&renewstatus &renewbook &calc_charges);
22 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
23                   
24 # your exported package globals go here,
25 # as well as any optionally exported functions
26
27 @EXPORT_OK   = qw($Var1 %Hashit);
28
29
30 # non-exported package globals go here
31 use vars qw(@more $stuff);
32         
33 # initalize package globals, first exported ones
34
35 my $Var1   = '';
36 my %Hashit = ();
37                     
38 # then the others (which are still accessible as $Some::Module::stuff)
39 my $stuff  = '';
40 my @more   = ();
41         
42 # all file-scoped lexicals must be created before
43 # the functions below that use them.
44                 
45 # file-private lexicals go here
46 my $priv_var    = '';
47 my %secret_hash = ();
48                             
49 # here's a file-private function as a closure,
50 # callable as &$priv_func;  it cannot be prototyped.
51 my $priv_func = sub {
52   # stuff goes here.
53 };
54                                                     
55 # make all your functions, whether exported or not;
56
57
58 sub Return  {
59   
60 }    
61
62 sub renewstatus {
63   # check renewal status
64   my ($env,$bornum,$itemno)=@_;
65   my $dbh=C4Connect;
66   my $renews = 1;
67   my $renewokay = 0;
68   my $q1 = "select * from issues 
69     where (borrowernumber = '$bornum')
70     and (itemnumber = '$itemno') 
71     and returndate is null";
72   my $sth1 = $dbh->prepare($q1);
73   $sth1->execute;
74   if (my $data1 = $sth1->fetchrow_hashref) {
75     my $q2 = "select renewalsallowed from items,biblioitems,itemtypes
76        where (items.itemnumber = '$itemno')
77        and (items.biblioitemnumber = biblioitems.biblioitemnumber) 
78        and (biblioitems.itemtype = itemtypes.itemtype)";
79     my $sth2 = $dbh->prepare($q2);
80     $sth2->execute;     
81     if (my $data2=$sth2->fetchrow_hashref) {
82       $renews = $data2->{'renewalsallowed'};
83     }
84     if ($renews > $data1->{'renewals'}) {
85       $renewokay = 1;
86     }
87     $sth2->finish;
88   }   
89   $sth1->finish;
90   $dbh->disconnect;
91   return($renewokay);    
92 }
93
94
95 sub renewbook {
96   # mark book as renewed
97   my ($env,$bornum,$itemno,$datedue)=@_;
98   my $dbh=C4Connect;
99   if ($datedue eq "" ) {    
100     #debug_msg($env, "getting date");
101     my $loanlength=21;
102     my $query= "Select * from biblioitems,items,itemtypes
103        where (items.itemnumber = '$itemno')
104        and (biblioitems.biblioitemnumber = items.biblioitemnumber)
105        and (biblioitems.itemtype = itemtypes.itemtype)";
106     my $sth=$dbh->prepare($query);
107     $sth->execute;
108     if (my $data=$sth->fetchrow_hashref) {
109       $loanlength = $data->{'loanlength'}
110     }
111     $sth->finish;
112     my $ti = time;
113     my $datedu = time + ($loanlength * 86400);
114     my @datearr = localtime($datedu);
115     $datedue = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
116   }
117   my $issquery = "select * from issues where borrowernumber='$bornum' and
118     itemnumber='$itemno' and returndate is null";
119   my $sth=$dbh->prepare($issquery);
120   $sth->execute;
121   my $issuedata=$sth->fetchrow_hashref;
122   $sth->finish;
123   my $renews = $issuedata->{'renewals'} +1;
124   my $updquery = "update issues 
125     set date_due = '$datedue', renewals = '$renews'
126     where borrowernumber='$bornum' and
127     itemnumber='$itemno' and returndate is null";
128   my $sth=$dbh->prepare($updquery);
129   $sth->execute;
130   $sth->finish;
131   UpdateStats($env,$env->{'branchcode'},'renew','','',$itemno);
132   $dbh->disconnect;
133 #  return();
134 }
135
136
137 sub calc_charges {         
138   # calculate charges due         
139   my ($env, $itemno, $bornum)=@_;           
140   my $charge=0;   
141   my $dbh=C4Connect;
142   my $item_type;               
143   my $q1 = "select itemtypes.itemtype,rentalcharge from
144   items,biblioitems,itemtypes     
145   where (items.itemnumber ='$itemno')         
146   and (biblioitems.biblioitemnumber = items.biblioitemnumber) 
147   and (biblioitems.itemtype = itemtypes.itemtype)";                 
148   my $sth1= $dbh->prepare($q1);                     
149   $sth1->execute;                       
150   if (my $data1=$sth1->fetchrow_hashref) {    
151     $item_type = $data1->{'itemtype'};     
152     $charge = $data1->{'rentalcharge'};
153     my $q2 = "select rentaldiscount from 
154     borrowers,categoryitem                        
155     where (borrowers.borrowernumber = '$bornum')         
156     and (borrowers.categorycode = categoryitem.categorycode)   
157     and (categoryitem.itemtype = '$item_type')";   
158     my $sth2=$dbh->prepare($q2);           
159     $sth2->execute;        
160     if (my$data2=$sth2->fetchrow_hashref) {                                           
161       my $discount = $data2->{'rentaldiscount'};         
162       $charge = ($charge *(100 - $discount)) / 100;                 
163     }                         
164     $sth2->{'finish'};                              
165   }                                   
166   $sth1->finish;  
167   $dbh->disconnect;
168 #  print "item $item_type";
169   return ($charge,$item_type);         
170 }       
171
172
173 END { }       # module clean-up code here (global destructor)