Continuing work on Z39.50 search tool. Daemon now forks up to 12 processes
[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 use strict;
7 require Exporter;
8 use DBI;
9 use C4::Database;
10 use C4::Format;
11 use C4::Accounts;
12 use C4::InterfaceCDK;
13 use C4::Interface::RenewalsCDK;
14 use C4::Circulation::Issues;
15 use C4::Circulation::Main;
16
17 use C4::Search;
18 use C4::Scan;
19 use C4::Stats;
20 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
21   
22 # set the version for version checking
23 $VERSION = 0.01;
24     
25 @ISA = qw(Exporter);
26 @EXPORT = qw(&renewstatus &renewbook &bulkrenew);
27 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
28                   
29 # your exported package globals go here,
30 # as well as any optionally exported functions
31
32 @EXPORT_OK   = qw($Var1 %Hashit);
33
34
35 # non-exported package globals go here
36 use vars qw(@more $stuff);
37         
38 # initalize package globals, first exported ones
39
40 my $Var1   = '';
41 my %Hashit = ();
42                     
43 # then the others (which are still accessible as $Some::Module::stuff)
44 my $stuff  = '';
45 my @more   = ();
46         
47 # all file-scoped lexicals must be created before
48 # the functions below that use them.
49                 
50 # file-private lexicals go here
51 my $priv_var    = '';
52 my %secret_hash = ();
53                             
54 # here's a file-private function as a closure,
55 # callable as &$priv_func;  it cannot be prototyped.
56 my $priv_func = sub {
57   # stuff goes here.
58 };
59                                                     
60 # make all your functions, whether exported or not;
61
62
63 sub Return  {
64   
65 }    
66
67 sub renewstatus {
68   # check renewal status
69   my ($env,$dbh,$bornum,$itemno)=@_;
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   return($renewokay);    
95 }
96
97
98 sub renewbook {
99   # mark book as renewed
100   my ($env,$dbh,$bornum,$itemno,$datedue)=@_;
101   if ($datedue eq "" ) {    
102     my $loanlength=21;
103     my $query= "Select * from biblioitems,items,itemtypes
104        where (items.itemnumber = '$itemno')
105        and (biblioitems.biblioitemnumber = items.biblioitemnumber)
106        and (biblioitems.itemtype = itemtypes.itemtype)";
107     my $sth=$dbh->prepare($query);
108     $sth->execute;
109     if (my $data=$sth->fetchrow_hashref) {
110       $loanlength = $data->{'loanlength'}
111     }
112     $sth->finish;
113     my $ti = time;
114     my $datedu = time + ($loanlength * 86400);
115     my @datearr = localtime($datedu);
116     $datedue = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
117   }
118   my @date = split("-",$datedue);
119   my $odatedue = (@date[2]+0)."-".(@date[1]+0)."-".@date[0];
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   
133   $sth->execute;
134   $sth->finish;
135   return($odatedue);
136 }
137
138 sub bulkrenew {
139   my ($env,$dbh,$bornum,$amount,$borrower,$odues) = @_;
140   my $query = "select * from issues 
141     where borrowernumber = '$bornum' and returndate is null order by date_due";
142   my $sth = $dbh->prepare($query);
143   $sth->execute();
144   my @items;
145   my @issues;
146   my @renewdef;
147   my $x;
148   my @barcodes;
149   my @rstatuses;
150   while (my $issrec = $sth->fetchrow_hashref) {
151      my $itemdata = C4::Search::itemnodata($env,$dbh,$issrec->{'itemnumber'});
152      my @date = split("-",$issrec->{'date_due'});
153      #my $line = $issrec->{'date_due'}." ";
154      my $line = @date[2]."-".@date[1]."-".@date[0]." ";
155      my $renewstatus = renewstatus($env,$dbh,$bornum,$issrec->{'itemnumber'});
156      my ($resbor,$resrec) = C4::Circulation::Main::checkreserve($env,
157         $dbh,$issrec->{'itemnumber'});
158      if ($resbor ne "") {
159        $line = $line."R";
160        $rstatuses[$x] ="R";
161      } elsif ($renewstatus == 0) {
162        $line = $line."N";
163        $rstatuses[$x] = "N";
164      } else {
165        $line = $line."Y";
166        $rstatuses[$x] = "Y";
167      }  
168      $line = $line.fmtdec($env,$issrec->{'renewals'},"20")." ";
169      $line = $line.$itemdata->{'barcode'}." ".$itemdata->{'itemtype'}." ".$itemdata->{'title'};
170      $items[$x] = $line;
171      #debug_msg($env,$line);
172      $issues[$x] = $issrec;
173      $barcodes[$x] = $itemdata->{'barcode'};
174      my $rdef = 1;
175      if ($issrec->{'renewals'} > 0) {
176        $rdef = 0;
177      }
178      $renewdef[$x] = $rdef;
179      $x++;
180   }  
181   if ($x < 1) { 
182      return;
183   }   
184   my $renews = C4::Interface::RenewalsCDK::renew_window($env,
185      \@items,$borrower,$amount,$odues);
186   my $isscnt = $x;
187   $x =0;
188   my $y = 0;
189   my @renew_errors = "";
190   while ($x < $isscnt) {
191     if (@$renews[$x] == 1) {
192       my $issrec = $issues[$x];
193       if ($rstatuses[$x] eq "Y") {
194         renewbook($env,$dbh,$issrec->{'borrowernumber'},$issrec->{'itemnumber'},"");
195         my $charge = C4::Circulation::Issues::calc_charges($env,$dbh,
196            $issrec->{'itemnumber'},$issrec->{'borrowernumber'});
197         if ($charge > 0) {
198           C4::Circulation::Issues::createcharge($env,$dbh,
199           $issrec->{'itemnumber'},$issrec->{'borrowernumber'},$charge);
200         }
201         &UpdateStats($env,$env->{'branchcode'},'renew',$charge,'',$issrec->{'itemnumber'});
202       } elsif ($rstatuses[$x] eq "N") {
203         C4::InterfaceCDK::info_msg($env,
204            "</S>$barcodes[$x] - can't renew");  
205       } else {
206         C4::InterfaceCDK::info_msg($env,
207            "</S>$barcodes[$x] - on reserve");
208       }
209     }  
210     $x++;
211   }
212   $sth->finish();
213 }
214 END { }       # module clean-up code here (global destructor)