Bug that prevented msgid's with French characters from being translated
[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 = "";
120         my $sth = $dbh->prepare("Select * from biblio where biblionumber = ?");
121         $sth->execute($biblionumber);
122         my $data=$sth->fetchrow_hashref;
123         $sth->finish;
124         my @branches;
125         my $sth=$dbh->prepare("select * from branches where issuing=1 order by branchname");
126         $sth->execute;
127         while (my $branchrec=$sth->fetchrow_hashref) {
128           my $branchdet =
129             fmtstr($env,$branchrec->{'branchcode'},"L2")." ".$branchrec->{'branchname'};
130           push @branches,$branchdet;
131         }
132         $sth->finish;
133         $donext = "";
134         while ($donext eq "") {
135           my $title = titlepanel($env,"Reserves","Create Reserve");
136           my ($reason,$borcode,$branch,$constraint,$bibitems) =
137             MakeReserveScreen($env, $data, \@items, \@branches);
138           if ($borcode ne "") {
139             my ($borrnum,$borrower) = findoneborrower($env,$dbh,$borcode);
140             if ($reason eq "") {
141               if ($borrnum ne "") {
142                 my $fee =
143                   CalcReserveFee($env,$borrnum,$biblionumber,$constraint,$bibitems);
144                   CreateReserve($env,$branch,$borrnum,$biblionumber,$constraint,$bibitems,$fee);
145                 $donext = "Circ"
146               }
147
148             } else {
149               $donext = $reason;
150             }
151           } else { $donext = "Circ" }
152         }
153       }
154     }
155   }
156   return ($donext);
157 }
158
159 # FIXME - A functionally identical version of this function appears in
160 # C4::Reserves2. Pick one and stick with it.
161 sub CalcReserveFee {
162   my ($env,$borrnum,$biblionumber,$constraint,$bibitems) = @_;
163   #check for issues;
164   my $dbh = C4::Context->dbh;
165   my $const = lc substr($constraint,0,1);
166   my $sth = $dbh->prepare("select * from borrowers,categories
167     where (borrowernumber = ?)
168     and (borrowers.categorycode = categories.categorycode)");
169   $sth->execute($borrnum);
170   my $data = $sth->fetchrow_hashref;
171   $sth->finish();
172   my $fee = $data->{'reservefee'};
173   my $cntitems = @->$bibitems;
174   if ($fee > 0) {
175     # check for items on issue
176     # first find biblioitem records
177     my @biblioitems;
178     my $sth1 = $dbh->prepare("select * from biblio,biblioitems
179        where (biblio.biblionumber = ?)
180        and (biblio.biblionumber = biblioitems.biblionumber)");
181     $sth1->execute($biblionumber);
182     while (my $data1=$sth1->fetchrow_hashref) {
183       if ($const eq "a") {
184         push @biblioitems,$data1;
185      } else {
186         my $found = 0;
187         my $x = 0;
188         while ($x < $cntitems) {
189           if (@$bibitems->{'biblioitemnumber'} == $data->{'biblioitemnumber'}) {
190             $found = 1;
191           }
192           $x++;
193         }
194         if ($const eq 'o') {if ($found == 1) {push @biblioitems,$data;}
195         } else {if ($found == 0) {push @biblioitems,$data;} }
196       }
197     }
198     $sth1->finish;
199     my $cntitemsfound = @biblioitems;
200     my $issues = 0;
201     my $x = 0;
202     my $allissued = 1;
203     while ($x < $cntitemsfound) {
204       my $bitdata = @biblioitems[$x];
205       my $sth2 = $dbh->prepare("select * from items
206         where biblioitemnumber = ?");
207       $sth2->execute($bitdata->{'biblioitemnumber'});
208       while (my $itdata=$sth2->fetchrow_hashref) {
209         my $sth3 = $dbh->prepare("select * from issues
210            where itemnumber = ? and returndate is null");
211         $sth3->execute($itdata->{'itemnumber'});
212         if (my $isdata=$sth3->fetchrow_hashref) { } else {$allissued = 0; }
213       }
214       $x++;
215     }
216     if ($allissued == 0) {
217       my $rsth = $dbh->prepare("select * from reserves
218         where biblionumber = ?");
219       $rsth->execute($biblionumber);
220       if (my $rdata = $rsth->fetchrow_hashref) { } else {
221         $fee = 0;
222       }
223     }
224   }
225   return $fee;
226 } # end CalcReserveFee
227
228 # FIXME - A somewhat different version of this function appears in
229 # C4::Reserves2. Pick one and stick with it.
230 sub CreateReserve {
231   my ($env,$branch,$borrnum,$biblionumber,$constraint,$bibitems,$fee) = @_;
232   my $dbh = C4::Context->dbh;
233   #$dbh->{RaiseError} = 1;
234   #$dbh->{AutoCommit} = 0;
235   my $const = lc substr($constraint,0,1);
236   my @datearr = localtime(time);
237   my $resdate = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
238   #eval {
239     # updates take place here
240     if ($fee > 0) {
241       my $nextacctno = &getnextacctno($env,$borrnum,$dbh);
242       my $usth = $dbh->prepare("insert into accountlines
243          (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
244           values (?,?,now(),?,'Reserve Charge','Res',?)");
245       $usth->execute($borrnum,$nextacctno,$fee,$fee);
246       $usth->finish;
247     }
248     my $sth = $dbh->prepare("insert into reserves (borrowernumber,biblionumber,reservedate,branchcode,constrainttype) values (?,?,?,?,?)");
249     $sth->execute($borrnum,$biblionumber,$resdate,$branch,$const);
250     if (($const eq "o") || ($const eq "e")) {
251       my $numitems = @$bibitems;
252       my $i = 0;
253       while ($i < $numitems) {
254         my $biblioitem = @$bibitems[$i];
255         my $sth = $dbh->prepare("insert into reserveconstraints
256            (borrowernumber,biblionumber,reservedate,biblioitemnumber)
257            values (?,?,?,?)");
258         $sth->execute($borrnum,$biblionumber,$resdate,$biblioitem);
259         $i++;
260       }
261     }
262   UpdateStats($env,'branch','reserve',$fee);
263   #$dbh->commit();
264   #};
265   #if (@_) {
266   #  # update failed
267   #  my $temp = @_;
268   #  #  error_msg($env,"Update failed");
269   #  $dbh->rollback();
270   #}
271   return();
272 } # end CreateReserve