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