DBI changes fixing bug 662
[koha.git] / C4 / Circulation / Renewals.pm
1 package C4::Circulation::Renewals;
2
3 # $Id$
4
5 #package to deal with Renewals
6 #written 7/11/99 by olwen@katipo.co.nz
7
8
9 # Copyright 2000-2002 Katipo Communications
10 #
11 # This file is part of Koha.
12 #
13 # Koha is free software; you can redistribute it and/or modify it under the
14 # terms of the GNU General Public License as published by the Free Software
15 # Foundation; either version 2 of the License, or (at your option) any later
16 # version.
17 #
18 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
19 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License along with
23 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
24 # Suite 330, Boston, MA  02111-1307 USA
25
26 use strict;
27 require Exporter;
28 use DBI;
29 use C4::Format;
30 use C4::Accounts;
31 use C4::InterfaceCDK;
32 use C4::Interface::RenewalsCDK;
33 use C4::Circulation::Issues;
34 use C4::Circulation::Main;
35         # FIXME - C4::Circulation::Main and C4::Circulation::Renewals
36         # use each other, so functions get redefined.
37 use C4::Search;
38 use C4::Scan;
39 use C4::Stats;
40 use vars qw($VERSION @ISA @EXPORT);
41
42 # set the version for version checking
43 $VERSION = 0.01;
44
45 =head1 NAME
46
47 C4::Circulation::Renewals - Old Koha module dealing with renewals
48
49 =head1 SYNOPSIS
50
51   use C4::Circulation::Renewals;
52
53 =head1 DESCRIPTION
54
55 This module contains a function for checking whether a book may be
56 renewed.
57
58 =head1 FUNCTIONS
59
60 =over 2
61
62 =cut
63
64 @ISA = qw(Exporter);
65 @EXPORT = qw(&renewstatus &renewbook &bulkrenew);
66
67 =item renewstatus
68
69   $ok = &renewstatus($env, $dbh, $borrowernumber, $itemnumber);
70
71 Find out whether a borrowed item may be renewed.
72
73 C<$env> is ignored.
74
75 C<$dbh> is a DBI handle to the Koha database.
76
77 C<$borrowernumber> is the borrower number of the patron who currently
78 has the item on loan.
79
80 C<$itemnumber> is the number of the item to renew.
81
82 C<$renewstatus> returns a true value iff the item may be renewed. The
83 item must currently be on loan to the specified borrower; renewals
84 must be allowed for the item's type; and the borrower must not have
85 already renewed the loan.
86
87 =cut
88 #'
89 # FIXME - This is identical to &C4::Circulation::Circ2::renewstatus,
90 # and virtually identical to &C4::Circulation::Renewals2::renewstatus.
91 # Pick one and stick with it.
92 sub renewstatus {
93   # check renewal status
94   # FIXME - Two people can't borrow the same book at once, so
95   # presumably we can get $bornum from $itemno.
96   my ($env,$dbh,$bornum,$itemno)=@_;
97   my $renews = 1;               # FIXME - I think this is the maximum
98                                 # number of allowed renewals.
99   # FIXME - I think this function could be redone to use only one SQL
100   # call.
101   my $renewokay = 0;
102   # Look in the issues table for this item, lent to this borrower,
103   # and not yet returned.
104   my $sth1 = $dbh->prepare("select * from issues
105     where (borrowernumber = '$bornum')
106     and (itemnumber = '$itemno')
107     and returndate is null");
108   $sth1->execute($bornum,$itemno);
109   # Found a matching item
110   if (my $data1 = $sth1->fetchrow_hashref) {
111     # See if this item may be renewed. This query is convoluted
112     # because it's a bit messy: given the item number, we need to find
113     # the biblioitem, which gives us the itemtype, which tells us
114     # whether it may be renewed.
115     my $sth2 = $dbh->prepare("select renewalsallowed from items,biblioitems,itemtypes
116        where (items.itemnumber = ?)
117        and (items.biblioitemnumber = biblioitems.biblioitemnumber)
118        and (biblioitems.itemtype = itemtypes.itemtype)");
119     $sth2->execute($itemno);
120     if (my $data2=$sth2->fetchrow_hashref) {
121       $renews = $data2->{'renewalsallowed'};
122     }
123     if ($renews > $data1->{'renewals'}) {
124       $renewokay = 1;
125     }
126     $sth2->finish;
127   }
128   $sth1->finish;
129   return($renewokay);
130 }
131
132 # FIXME - A different version of this function appears in
133 # C4::Circulation::Renewals2. Pick one and stick with it.
134 # FIXME - This function doesn't appear to be used. Presumably it's
135 # obsolete.
136 # Otherwise, it needs a POD.
137 sub renewbook {
138   # mark book as renewed
139   # FIXME - Get $dbh from C4::Context->dbh, instead of requiring
140   # an additional argument.
141   my ($env,$dbh,$bornum,$itemno,$datedue)=@_;
142   if ($datedue eq "" ) {
143     my $loanlength=21;
144     my $sth=$dbh->prepare("Select * from biblioitems,items,itemtypes
145        where (items.itemnumber = ?)
146        and (biblioitems.biblioitemnumber = items.biblioitemnumber)
147        and (biblioitems.itemtype = itemtypes.itemtype)");
148     $sth->execute($itemno);
149     if (my $data=$sth->fetchrow_hashref) {
150       $loanlength = $data->{'loanlength'}
151     }
152     $sth->finish;
153     my $ti = time;
154     my $datedu = time + ($loanlength * 86400);
155     my @datearr = localtime($datedu);
156     $datedue = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
157   }
158   my @date = split("-",$datedue);
159   my $odatedue = (@date[2]+0)."-".(@date[1]+0)."-".@date[0];
160   my $sth=$dbh->prepare("select * from issues where borrowernumber=? and
161     itemnumber=? and returndate is null");
162   $sth->execute($bornum,$itemno);
163   my $issuedata=$sth->fetchrow_hashref;
164   $sth->finish;
165   my $renews = $issuedata->{'renewals'} +1;
166   my $sth=$dbh->prepare("update issues
167     set date_due = ?, renewals = ?
168     where borrowernumber=? and
169     itemnumber=? and returndate is null");
170
171   $sth->execute($datedue,$renews,$bornum,$itemno);
172   $sth->finish;
173   return($odatedue);
174 }
175
176 # FIXME - Only used in C4:InterfaceCDK. Presumably this function is
177 # obsolete.
178 # Otherwise, it needs a POD.
179 sub bulkrenew {
180   my ($env,$dbh,$bornum,$amount,$borrower,$odues) = @_;
181   my $sth = $dbh->prepare("select * from issues where borrowernumber = ? and returndate is null order by date_due");
182   $sth->execute($bornum);
183   my @items;
184   my @issues;
185   my @renewdef;
186   my $x;
187   my @barcodes;
188   my @rstatuses;
189   while (my $issrec = $sth->fetchrow_hashref) {
190      my $itemdata = C4::Search::itemnodata($env,$dbh,$issrec->{'itemnumber'});
191      my @date = split("-",$issrec->{'date_due'});
192      #my $line = $issrec->{'date_due'}." ";
193      my $line = @date[2]."-".@date[1]."-".@date[0]." ";
194      my $renewstatus = renewstatus($env,$dbh,$bornum,$issrec->{'itemnumber'});
195      my ($resbor,$resrec) = C4::Circulation::Main::checkreserve($env,
196         $dbh,$issrec->{'itemnumber'});
197      if ($resbor ne "") {
198        $line .= "R";
199        $rstatuses[$x] ="R";
200      } elsif ($renewstatus == 0) {
201        $line .= "N";
202        $rstatuses[$x] = "N";
203      } else {
204        $line .= "Y";
205        $rstatuses[$x] = "Y";
206      }
207      $line .= fmtdec($env,$issrec->{'renewals'},"20")." ";
208      $line .= $itemdata->{'barcode'}." ".$itemdata->{'itemtype'}." ".$itemdata->{'title'};
209      $items[$x] = $line;
210      #debug_msg($env,$line);
211      $issues[$x] = $issrec;
212      $barcodes[$x] = $itemdata->{'barcode'};
213      my $rdef = 1;
214      if ($issrec->{'renewals'} > 0) {
215        $rdef = 0;
216      }
217      $renewdef[$x] = $rdef;
218      $x++;
219   }
220   if ($x < 1) {
221      return;
222   }
223   my $renews = C4::Interface::RenewalsCDK::renew_window($env,
224      \@items,$borrower,$amount,$odues);
225   my $isscnt = $x;
226   $x =0;
227   my $y = 0;
228   my @renew_errors = "";
229   while ($x < $isscnt) {
230     if (@$renews[$x] == 1) {
231       my $issrec = $issues[$x];
232       if ($rstatuses[$x] eq "Y") {
233         renewbook($env,$dbh,$issrec->{'borrowernumber'},$issrec->{'itemnumber'},"");
234         my $charge = C4::Circulation::Issues::calc_charges($env,$dbh,
235            $issrec->{'itemnumber'},$issrec->{'borrowernumber'});
236         if ($charge > 0) {
237           C4::Circulation::Issues::createcharge($env,$dbh,
238           $issrec->{'itemnumber'},$issrec->{'borrowernumber'},$charge);
239         }
240         &UpdateStats($env,$env->{'branchcode'},'renew',$charge,'',$issrec->{'itemnumber'});
241       } elsif ($rstatuses[$x] eq "N") {
242         C4::InterfaceCDK::info_msg($env,
243            "</S>$barcodes[$x] - can't renew");
244       } else {
245         C4::InterfaceCDK::info_msg($env,
246            "</S>$barcodes[$x] - on reserve");
247       }
248     }
249     $x++;
250   }
251   $sth->finish();
252 }
253
254 1;
255 __END__
256
257 =back
258
259 =head1 AUTHOR
260
261 Koha Developement team <info@koha.org>
262
263 =cut