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