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