fix for 570 : If a subject search is done from the OPAC or Intranet Catalogue, duplicate
[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 $q1 = "select * from issues
105     where (borrowernumber = '$bornum')
106     and (itemnumber = '$itemno')
107     and returndate is null";
108   my $sth1 = $dbh->prepare($q1);
109   $sth1->execute;
110   # Found a matching item
111   if (my $data1 = $sth1->fetchrow_hashref) {
112     # See if this item may be renewed. This query is convoluted
113     # because it's a bit messy: given the item number, we need to find
114     # the biblioitem, which gives us the itemtype, which tells us
115     # whether it may be renewed.
116     my $q2 = "select renewalsallowed from items,biblioitems,itemtypes
117        where (items.itemnumber = '$itemno')
118        and (items.biblioitemnumber = biblioitems.biblioitemnumber)
119        and (biblioitems.itemtype = itemtypes.itemtype)";
120     my $sth2 = $dbh->prepare($q2);
121     $sth2->execute;
122     if (my $data2=$sth2->fetchrow_hashref) {
123       $renews = $data2->{'renewalsallowed'};
124     }
125     if ($renews > $data1->{'renewals'}) {
126       $renewokay = 1;
127     }
128     $sth2->finish;
129   }
130   $sth1->finish;
131   return($renewokay);
132 }
133
134 # FIXME - A different version of this function appears in
135 # C4::Circulation::Renewals2. Pick one and stick with it.
136 # FIXME - This function doesn't appear to be used. Presumably it's
137 # obsolete.
138 # Otherwise, it needs a POD.
139 sub renewbook {
140   # mark book as renewed
141   # FIXME - Get $dbh from C4::Context->dbh, instead of requiring
142   # an additional argument.
143   my ($env,$dbh,$bornum,$itemno,$datedue)=@_;
144   if ($datedue eq "" ) {
145     my $loanlength=21;
146     my $query= "Select * from biblioitems,items,itemtypes
147        where (items.itemnumber = '$itemno')
148        and (biblioitems.biblioitemnumber = items.biblioitemnumber)
149        and (biblioitems.itemtype = itemtypes.itemtype)";
150     my $sth=$dbh->prepare($query);
151     $sth->execute;
152     if (my $data=$sth->fetchrow_hashref) {
153       $loanlength = $data->{'loanlength'}
154     }
155     $sth->finish;
156     my $ti = time;
157     my $datedu = time + ($loanlength * 86400);
158     my @datearr = localtime($datedu);
159     $datedue = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
160   }
161   my @date = split("-",$datedue);
162   my $odatedue = (@date[2]+0)."-".(@date[1]+0)."-".@date[0];
163   my $issquery = "select * from issues where borrowernumber='$bornum' and
164     itemnumber='$itemno' and returndate is null";
165   my $sth=$dbh->prepare($issquery);
166   $sth->execute;
167   my $issuedata=$sth->fetchrow_hashref;
168   $sth->finish;
169   my $renews = $issuedata->{'renewals'} +1;
170   my $updquery = "update issues
171     set date_due = '$datedue', renewals = '$renews'
172     where borrowernumber='$bornum' and
173     itemnumber='$itemno' and returndate is null";
174   my $sth=$dbh->prepare($updquery);
175
176   $sth->execute;
177   $sth->finish;
178   return($odatedue);
179 }
180
181 # FIXME - Only used in C4:InterfaceCDK. Presumably this function is
182 # obsolete.
183 # Otherwise, it needs a POD.
184 sub bulkrenew {
185   my ($env,$dbh,$bornum,$amount,$borrower,$odues) = @_;
186   my $query = "select * from issues
187     where borrowernumber = '$bornum' and returndate is null order by date_due";
188   my $sth = $dbh->prepare($query);
189   $sth->execute();
190   my @items;
191   my @issues;
192   my @renewdef;
193   my $x;
194   my @barcodes;
195   my @rstatuses;
196   while (my $issrec = $sth->fetchrow_hashref) {
197      my $itemdata = C4::Search::itemnodata($env,$dbh,$issrec->{'itemnumber'});
198      my @date = split("-",$issrec->{'date_due'});
199      #my $line = $issrec->{'date_due'}." ";
200      my $line = @date[2]."-".@date[1]."-".@date[0]." ";
201      my $renewstatus = renewstatus($env,$dbh,$bornum,$issrec->{'itemnumber'});
202      my ($resbor,$resrec) = C4::Circulation::Main::checkreserve($env,
203         $dbh,$issrec->{'itemnumber'});
204      if ($resbor ne "") {
205        $line .= "R";
206        $rstatuses[$x] ="R";
207      } elsif ($renewstatus == 0) {
208        $line .= "N";
209        $rstatuses[$x] = "N";
210      } else {
211        $line .= "Y";
212        $rstatuses[$x] = "Y";
213      }
214      $line .= fmtdec($env,$issrec->{'renewals'},"20")." ";
215      $line .= $itemdata->{'barcode'}." ".$itemdata->{'itemtype'}." ".$itemdata->{'title'};
216      $items[$x] = $line;
217      #debug_msg($env,$line);
218      $issues[$x] = $issrec;
219      $barcodes[$x] = $itemdata->{'barcode'};
220      my $rdef = 1;
221      if ($issrec->{'renewals'} > 0) {
222        $rdef = 0;
223      }
224      $renewdef[$x] = $rdef;
225      $x++;
226   }
227   if ($x < 1) {
228      return;
229   }
230   my $renews = C4::Interface::RenewalsCDK::renew_window($env,
231      \@items,$borrower,$amount,$odues);
232   my $isscnt = $x;
233   $x =0;
234   my $y = 0;
235   my @renew_errors = "";
236   while ($x < $isscnt) {
237     if (@$renews[$x] == 1) {
238       my $issrec = $issues[$x];
239       if ($rstatuses[$x] eq "Y") {
240         renewbook($env,$dbh,$issrec->{'borrowernumber'},$issrec->{'itemnumber'},"");
241         my $charge = C4::Circulation::Issues::calc_charges($env,$dbh,
242            $issrec->{'itemnumber'},$issrec->{'borrowernumber'});
243         if ($charge > 0) {
244           C4::Circulation::Issues::createcharge($env,$dbh,
245           $issrec->{'itemnumber'},$issrec->{'borrowernumber'},$charge);
246         }
247         &UpdateStats($env,$env->{'branchcode'},'renew',$charge,'',$issrec->{'itemnumber'});
248       } elsif ($rstatuses[$x] eq "N") {
249         C4::InterfaceCDK::info_msg($env,
250            "</S>$barcodes[$x] - can't renew");
251       } else {
252         C4::InterfaceCDK::info_msg($env,
253            "</S>$barcodes[$x] - on reserve");
254       }
255     }
256     $x++;
257   }
258   $sth->finish();
259 }
260
261 1;
262 __END__
263
264 =back
265
266 =head1 AUTHOR
267
268 Koha Developement team <info@koha.org>
269
270 =cut