Replaced expressions of the form "$x = $x <op> $y" with "$x <op>= $y".
[koha.git] / C4 / Reserves.pm
1 package C4::Reserves;
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 # FIXME - I suspect that this module is obsolete.
23
24 use strict;
25 require Exporter;
26 use DBI;
27 use C4::Context;
28 use C4::Format;
29 use C4::Accounts;
30 use C4::Stats;
31 use C4::InterfaceCDK;
32 use C4::Interface::ReserveentCDK;
33 use C4::Circulation::Main;
34 use C4::Circulation::Borrower;
35 use C4::Search;
36 use vars qw($VERSION @ISA @EXPORT);
37
38 # set the version for version checking
39 $VERSION = 0.01;
40
41 @ISA = qw(Exporter);
42 @EXPORT = qw(&EnterReserves CalcReserveFee CreateReserve );
43
44 # FIXME - This doesn't appear to ever be used, except in modules that
45 # appear to be obsolete.
46 sub EnterReserves{
47   my ($env)=@_;
48   my $titlepanel = titlepanel($env,"Reserves","Enter Selection");
49   my @flds = ("No of entries","Barcode","ISBN","Title","Keywords","Author","Subject");
50   my @fldlens = ("5","15","15","50","50","50","50");
51   my ($reason,$num,$itemnumber,$isbn,$title,$keyword,$author,$subject) =
52      FindBiblioScreen($env,"Reserves",7,\@flds,\@fldlens);
53   my $donext ="Circ";
54   if ($reason ne "") {
55     $donext = $reason;
56   } else {
57     my %search;
58     $search{'title'}= $title;
59     $search{'keyword'}=$keyword;
60     $search{'author'}=$author;
61     $search{'subject'}=$subject;
62     $search{'item'}=$itemnumber;
63     $search{'isbn'}=$isbn;
64     my @results;
65     my $count;
66     if ($num < 1 ) {
67       $num = 30;
68     }
69     my $offset = 0;
70     my $title = titlepanel($env,"Reserves","Searching");
71     if ($itemnumber ne '' || $isbn ne ''){
72       ($count,@results)=&CatSearch($env,'precise',\%search,$num,$offset);
73     } else {
74       if ($subject ne ''){
75         ($count,@results)=&CatSearch($env,'subject',\%search,$num,$offset);
76       } else {
77         if ($keyword ne ''){
78           ($count,@results)=&KeywordSearch($env,'intra',\%search,$num,$offset);
79         } else {
80           ($count,@results)=&CatSearch($env,'loose',\%search,$num,$offset);
81         }
82       }
83     }
84     my $no_ents = @results;
85     my $biblionumber;
86     if ($no_ents > 0) {
87       if ($no_ents == 1) {
88         my @ents = split("\t",@results[0]);
89         $biblionumber  = @ents[2];
90       } else {
91         my %biblio_xref;
92         my @bibtitles;
93         my $i = 0;
94         my $line;
95         while ($i < $no_ents) {
96           my @ents = split("\t",@results[$i]);
97           $line = fmtstr($env,@ents[1],"L70");
98           my $auth = substr(@ents[0],0,30);
99           substr($line,(70-length($auth)-2),length($auth)+2) = "  ".$auth;
100           @bibtitles[$i]=$line;
101           $biblio_xref{$line}=@ents[2];
102           $i++;
103         }
104         my $title = titlepanel($env,"Reserves","Select Title");
105         my ($results,$bibres) = SelectBiblio($env,$count,\@bibtitles);
106         if ($results eq "") {
107           $biblionumber = $biblio_xref{$bibres};
108         } else {
109           $donext = $results;
110         }
111       }
112
113       if ($biblionumber eq "") {
114         error_msg($env,"No items found");
115       } else {
116         my @items = GetItems($env,$biblionumber);
117         my $cnt_it = @items;
118         my $dbh = C4::Context->dbh;
119         my $query = "Select * from biblio where biblionumber = $biblionumber";
120         my $sth = $dbh->prepare($query);
121         $sth->execute;
122         my $data=$sth->fetchrow_hashref;
123         $sth->finish;
124         my @branches;
125         my $query = "select * from branches where issuing=1 order by branchname";
126         my $sth=$dbh->prepare($query);
127         $sth->execute;
128         while (my $branchrec=$sth->fetchrow_hashref) {
129           my $branchdet =
130             fmtstr($env,$branchrec->{'branchcode'},"L2")." ".$branchrec->{'branchname'};
131           push @branches,$branchdet;
132         }
133         $sth->finish;
134         $donext = "";
135         while ($donext eq "") {
136           my $title = titlepanel($env,"Reserves","Create Reserve");
137           my ($reason,$borcode,$branch,$constraint,$bibitems) =
138             MakeReserveScreen($env, $data, \@items, \@branches);
139           if ($borcode ne "") {
140             my ($borrnum,$borrower) = findoneborrower($env,$dbh,$borcode);
141             if ($reason eq "") {
142               if ($borrnum ne "") {
143                 my $fee =
144                   CalcReserveFee($env,$borrnum,$biblionumber,$constraint,$bibitems);
145                   CreateReserve($env,$branch,$borrnum,$biblionumber,$constraint,$bibitems,$fee);
146                 $donext = "Circ"
147               }
148
149             } else {
150               $donext = $reason;
151             }
152           } else { $donext = "Circ" }
153         }
154       }
155     }
156   }
157   return ($donext);
158 }
159
160 # FIXME - A functionally identical version of this function appears in
161 # C4::Reserves2. Pick one and stick with it.
162 sub CalcReserveFee {
163   my ($env,$borrnum,$biblionumber,$constraint,$bibitems) = @_;
164   #check for issues;
165   my $dbh = C4::Context->dbh;
166   my $const = lc substr($constraint,0,1);
167   my $query = "select * from borrowers,categories
168     where (borrowernumber = '$borrnum')
169     and (borrowers.categorycode = categories.categorycode)";
170   my $sth = $dbh->prepare($query);
171   $sth->execute;
172   my $data = $sth->fetchrow_hashref;
173   $sth->finish();
174   my $fee = $data->{'reservefee'};
175   my $cntitems = @->$bibitems;
176   if ($fee > 0) {
177     # check for items on issue
178     # first find biblioitem records
179     my @biblioitems;
180     my $query1 = "select * from biblio,biblioitems
181        where (biblio.biblionumber = '$biblionumber')
182        and (biblio.biblionumber = biblioitems.biblionumber)";
183     my $sth1 = $dbh->prepare($query1);
184     $sth1->execute();
185     while (my $data1=$sth1->fetchrow_hashref) {
186       if ($const eq "a") {
187         push @biblioitems,$data1;
188      } else {
189         my $found = 0;
190         my $x = 0;
191         while ($x < $cntitems) {
192           if (@$bibitems->{'biblioitemnumber'} == $data->{'biblioitemnumber'}) {
193             $found = 1;
194           }
195           $x++;
196         }
197         if ($const eq 'o') {if ($found == 1) {push @biblioitems,$data;}
198         } else {if ($found == 0) {push @biblioitems,$data;} }
199       }
200     }
201     $sth1->finish;
202     my $cntitemsfound = @biblioitems;
203     my $issues = 0;
204     my $x = 0;
205     my $allissued = 1;
206     while ($x < $cntitemsfound) {
207       my $bitdata = @biblioitems[$x];
208       my $query2 = "select * from items
209         where biblioitemnumber = '$bitdata->{'biblioitemnumber'}'";
210       my $sth2 = $dbh->prepare($query2);
211       $sth2->execute;
212       while (my $itdata=$sth2->fetchrow_hashref) {
213         my $query3 = "select * from issues
214            where itemnumber = '$itdata->{'itemnumber'}' and returndate is null";
215         my $sth3 = $dbh->prepare($query3);
216         $sth3->execute();
217         if (my $isdata=$sth3->fetchrow_hashref) { } else {$allissued = 0; }
218       }
219       $x++;
220     }
221     if ($allissued == 0) {
222       my $rquery = "select * from reserves
223         where biblionumber = '$biblionumber'";
224       my $rsth = $dbh->prepare($rquery);
225       $rsth->execute();
226       if (my $rdata = $rsth->fetchrow_hashref) { } else {
227         $fee = 0;
228       }
229     }
230   }
231   return $fee;
232 } # end CalcReserveFee
233
234 # FIXME - A somewhat different version of this function appears in
235 # C4::Reserves2. Pick one and stick with it.
236 sub CreateReserve {
237   my ($env,$branch,$borrnum,$biblionumber,$constraint,$bibitems,$fee) = @_;
238   my $dbh = C4::Context->dbh;
239   #$dbh->{RaiseError} = 1;
240   #$dbh->{AutoCommit} = 0;
241   my $const = lc substr($constraint,0,1);
242   my @datearr = localtime(time);
243   my $resdate = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
244   #eval {
245     # updates take place here
246     if ($fee > 0) {
247       my $nextacctno = &getnextacctno($env,$borrnum,$dbh);
248       my $updquery = "insert into accountlines
249          (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
250           values ($borrnum,$nextacctno,now(),$fee,'Reserve Charge','Res',$fee)";
251       my $usth = $dbh->prepare($updquery);
252       $usth->execute;
253       $usth->finish;
254     }
255     my $query="insert into reserves (borrowernumber,biblionumber,reservedate,branchcode,constrainttype) values ('$borrnum','$biblionumber','$resdate','$branch','$const')";
256     my $sth = $dbh->prepare($query);
257     $sth->execute();
258     if (($const eq "o") || ($const eq "e")) {
259       my $numitems = @$bibitems;
260       my $i = 0;
261       while ($i < $numitems) {
262         my $biblioitem = @$bibitems[$i];
263         my $query = "insert into reserveconstraints
264            (borrowernumber,biblionumber,reservedate,biblioitemnumber)
265            values ('$borrnum','$biblionumber','$resdate','$biblioitem')";
266         my $sth = $dbh->prepare($query);
267         $sth->execute();
268         $i++;
269       }
270     }
271   UpdateStats($env,'branch','reserve',$fee);
272   #$dbh->commit();
273   #};
274   #if (@_) {
275   #  # update failed
276   #  my $temp = @_;
277   #  #  error_msg($env,"Update failed");
278   #  $dbh->rollback();
279   #}
280   return();
281 } # end CreateReserve