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