Added copyright statement to all .pl and .pm files
[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::Database;
28 use C4::Format;
29 use C4::Accounts;
30 use C4::InterfaceCDK;
31 use C4::Interface::RenewalsCDK;
32 use C4::Circulation::Issues;
33 use C4::Circulation::Main;
34
35 use C4::Search;
36 use C4::Scan;
37 use C4::Stats;
38 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
39   
40 # set the version for version checking
41 $VERSION = 0.01;
42     
43 @ISA = qw(Exporter);
44 @EXPORT = qw(&renewstatus &renewbook &bulkrenew);
45 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
46                   
47 # your exported package globals go here,
48 # as well as any optionally exported functions
49
50 @EXPORT_OK   = qw($Var1 %Hashit);
51
52
53 # non-exported package globals go here
54 use vars qw(@more $stuff);
55         
56 # initalize package globals, first exported ones
57
58 my $Var1   = '';
59 my %Hashit = ();
60                     
61 # then the others (which are still accessible as $Some::Module::stuff)
62 my $stuff  = '';
63 my @more   = ();
64         
65 # all file-scoped lexicals must be created before
66 # the functions below that use them.
67                 
68 # file-private lexicals go here
69 my $priv_var    = '';
70 my %secret_hash = ();
71                             
72 # here's a file-private function as a closure,
73 # callable as &$priv_func;  it cannot be prototyped.
74 my $priv_func = sub {
75   # stuff goes here.
76 };
77                                                     
78 # make all your functions, whether exported or not;
79
80
81 sub Return  {
82   
83 }    
84
85 sub renewstatus {
86   # check renewal status
87   my ($env,$dbh,$bornum,$itemno)=@_;
88   my $renews = 1;
89   my $renewokay = 0;
90   my $q1 = "select * from issues 
91     where (borrowernumber = '$bornum')
92     and (itemnumber = '$itemno') 
93     and returndate is null";
94   my $sth1 = $dbh->prepare($q1);
95   $sth1->execute;
96   if (my $data1 = $sth1->fetchrow_hashref) {
97     my $q2 = "select renewalsallowed from items,biblioitems,itemtypes
98        where (items.itemnumber = '$itemno')
99        and (items.biblioitemnumber = biblioitems.biblioitemnumber) 
100        and (biblioitems.itemtype = itemtypes.itemtype)";
101     my $sth2 = $dbh->prepare($q2);
102     $sth2->execute;     
103     if (my $data2=$sth2->fetchrow_hashref) {
104       $renews = $data2->{'renewalsallowed'};
105     }
106     if ($renews > $data1->{'renewals'}) {
107       $renewokay = 1;
108     }
109     $sth2->finish;
110   }   
111   $sth1->finish;
112   return($renewokay);    
113 }
114
115
116 sub renewbook {
117   # mark book as renewed
118   my ($env,$dbh,$bornum,$itemno,$datedue)=@_;
119   if ($datedue eq "" ) {    
120     my $loanlength=21;
121     my $query= "Select * from biblioitems,items,itemtypes
122        where (items.itemnumber = '$itemno')
123        and (biblioitems.biblioitemnumber = items.biblioitemnumber)
124        and (biblioitems.itemtype = itemtypes.itemtype)";
125     my $sth=$dbh->prepare($query);
126     $sth->execute;
127     if (my $data=$sth->fetchrow_hashref) {
128       $loanlength = $data->{'loanlength'}
129     }
130     $sth->finish;
131     my $ti = time;
132     my $datedu = time + ($loanlength * 86400);
133     my @datearr = localtime($datedu);
134     $datedue = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
135   }
136   my @date = split("-",$datedue);
137   my $odatedue = (@date[2]+0)."-".(@date[1]+0)."-".@date[0];
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   my $sth=$dbh->prepare($updquery);
150   
151   $sth->execute;
152   $sth->finish;
153   return($odatedue);
154 }
155
156 sub bulkrenew {
157   my ($env,$dbh,$bornum,$amount,$borrower,$odues) = @_;
158   my $query = "select * from issues 
159     where borrowernumber = '$bornum' and returndate is null order by date_due";
160   my $sth = $dbh->prepare($query);
161   $sth->execute();
162   my @items;
163   my @issues;
164   my @renewdef;
165   my $x;
166   my @barcodes;
167   my @rstatuses;
168   while (my $issrec = $sth->fetchrow_hashref) {
169      my $itemdata = C4::Search::itemnodata($env,$dbh,$issrec->{'itemnumber'});
170      my @date = split("-",$issrec->{'date_due'});
171      #my $line = $issrec->{'date_due'}." ";
172      my $line = @date[2]."-".@date[1]."-".@date[0]." ";
173      my $renewstatus = renewstatus($env,$dbh,$bornum,$issrec->{'itemnumber'});
174      my ($resbor,$resrec) = C4::Circulation::Main::checkreserve($env,
175         $dbh,$issrec->{'itemnumber'});
176      if ($resbor ne "") {
177        $line = $line."R";
178        $rstatuses[$x] ="R";
179      } elsif ($renewstatus == 0) {
180        $line = $line."N";
181        $rstatuses[$x] = "N";
182      } else {
183        $line = $line."Y";
184        $rstatuses[$x] = "Y";
185      }  
186      $line = $line.fmtdec($env,$issrec->{'renewals'},"20")." ";
187      $line = $line.$itemdata->{'barcode'}." ".$itemdata->{'itemtype'}." ".$itemdata->{'title'};
188      $items[$x] = $line;
189      #debug_msg($env,$line);
190      $issues[$x] = $issrec;
191      $barcodes[$x] = $itemdata->{'barcode'};
192      my $rdef = 1;
193      if ($issrec->{'renewals'} > 0) {
194        $rdef = 0;
195      }
196      $renewdef[$x] = $rdef;
197      $x++;
198   }  
199   if ($x < 1) { 
200      return;
201   }   
202   my $renews = C4::Interface::RenewalsCDK::renew_window($env,
203      \@items,$borrower,$amount,$odues);
204   my $isscnt = $x;
205   $x =0;
206   my $y = 0;
207   my @renew_errors = "";
208   while ($x < $isscnt) {
209     if (@$renews[$x] == 1) {
210       my $issrec = $issues[$x];
211       if ($rstatuses[$x] eq "Y") {
212         renewbook($env,$dbh,$issrec->{'borrowernumber'},$issrec->{'itemnumber'},"");
213         my $charge = C4::Circulation::Issues::calc_charges($env,$dbh,
214            $issrec->{'itemnumber'},$issrec->{'borrowernumber'});
215         if ($charge > 0) {
216           C4::Circulation::Issues::createcharge($env,$dbh,
217           $issrec->{'itemnumber'},$issrec->{'borrowernumber'},$charge);
218         }
219         &UpdateStats($env,$env->{'branchcode'},'renew',$charge,'',$issrec->{'itemnumber'});
220       } elsif ($rstatuses[$x] eq "N") {
221         C4::InterfaceCDK::info_msg($env,
222            "</S>$barcodes[$x] - can't renew");  
223       } else {
224         C4::InterfaceCDK::info_msg($env,
225            "</S>$barcodes[$x] - on reserve");
226       }
227     }  
228     $x++;
229   }
230   $sth->finish();
231 }
232 END { }       # module clean-up code here (global destructor)