Getting Iteminfo in Search to only display correct reserves (mainly used on
[koha.git] / C4 / Reserves2.pm
1 package C4::Reserves2; #asummes C4/Reserves2
2
3 #requires DBI.pm to be installed
4 #uses DBD:Pg
5
6 use strict;
7 require Exporter;
8 use DBI;
9 use C4::Database;
10 #use C4::Accounts;
11
12 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
13   
14 # set the version for version checking
15 $VERSION = 0.01;
16     
17 @ISA = qw(Exporter);
18 @EXPORT = qw(&FindReserves &CreateReserve &updatereserves &getreservetitle
19 Findgroupreserve);
20 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
21                   
22 # your exported package globals go here,
23 # as well as any optionally exported functions
24
25 @EXPORT_OK   = qw($Var1 %Hashit);
26
27
28 # non-exported package globals go here
29 use vars qw(@more $stuff);
30         
31 # initalize package globals, first exported ones
32
33 my $Var1   = '';
34 my %Hashit = ();
35                     
36 # then the others (which are still accessible as $Some::Module::stuff)
37 my $stuff  = '';
38 my @more   = ();
39         
40 # all file-scoped lexicals must be created before
41 # the functions below that use them.
42                 
43 # file-private lexicals go here
44 my $priv_var    = '';
45 my %secret_hash = ();
46                             
47 # here's a file-private function as a closure,
48 # callable as &$priv_func;  it cannot be prototyped.
49 my $priv_func = sub {
50   # stuff goes here.
51 };
52                                                     
53 # make all your functions, whether exported or not;
54
55 sub FindReserves {
56   my ($bib,$bor)=@_;
57   my $dbh=C4Connect;
58   my $query="Select *,reserves.branchcode,biblio.title as btitle
59   from reserves,borrowers,biblio ";
60   if ($bib ne ''){
61     if ($bor ne ''){
62        $query=$query." where reserves.biblionumber=$bib and
63        reserves.borrowernumber=borrowers.borrowernumber and
64        biblio.biblionumber=$bib and cancellationdate is NULL and 
65        (found <> 'F' or found is NULL)";
66     } else {
67        $query=$query." where reserves.borrowernumber=borrowers.borrowernumber
68        and biblio.biblionumber=$bib and reserves.biblionumber=$bib
69        and cancellationdate is NULL and 
70        (found <> 'F' or found is NULL)";
71     }
72   } else {
73     $query=$query." where borrowers.borrowernumber=$bor and
74     reserves.borrowernumber=borrowers.borrowernumber and reserves.biblionumber
75     =biblio.biblionumber and cancellationdate is NULL and 
76     (found <> 'F' or found is NULL)";
77   }
78   $query.=" order by priority";
79   my $sth=$dbh->prepare($query);
80   $sth->execute;
81   my $i=0;
82   my @results;
83   while (my $data=$sth->fetchrow_hashref){
84     $results[$i]=$data;
85     $i++;
86   }
87 #  print $query;
88   $sth->finish;
89   $dbh->disconnect;
90   return($i,\@results);
91 }
92
93 sub Findgroupreserve {
94   my ($bibitem,$biblio)=@_;
95   my $dbh=C4Connect;
96   $bibitem=$dbh->quote($bibitem);
97   my $query="Select * from reserves 
98   left join reserveconstraints on
99   reserves.biblionumber=reserveconstraints.biblionumber
100   where
101   reserves.biblionumber=$biblio and
102   ((reserveconstraints.biblioitemnumber=$bibitem 
103   and reserves.borrowernumber=reserveconstraints.borrowernumber
104   and reserves.reservedate=reserveconstraints.reservedate)
105   or reserves.constrainttype='a')
106   and reserves.cancellationdate is NULL
107   and (reserves.found <> 'F' or reserves.found is NULL)";
108 #  print $query;
109   my $sth=$dbh->prepare($query);
110   $sth->execute;
111   my $i=0;
112   my @results;
113   while (my $data=$sth->fetchrow_hashref){
114     $results[$i]=$data;
115     $i++;
116   }
117   $sth->finish;
118   $dbh->disconnect;
119   return($i,@results);
120 }
121
122 sub CreateReserve {                                                           
123   my
124 ($env,$branch,$borrnum,$biblionumber,$constraint,$bibitems,$priority,$notes,$title)= @_;   
125   my $fee=CalcReserveFee($env,$borrnum,$biblionumber,$constraint,$bibitems);
126   my $dbh = &C4Connect;       
127   my $const = lc substr($constraint,0,1);       
128   my @datearr = localtime(time);                                
129   my $resdate =(1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];                   
130   #eval {                                                           
131   # updates take place here             
132   if ($fee > 0) {           
133 #    print $fee;
134     my $nextacctno = &getnextacctno($env,$borrnum,$dbh);   
135     my $updquery = "insert into accountlines       
136     (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)                                              
137                                                           values
138     ($borrnum,$nextacctno,now(),$fee,'Reserve Charge - $title','Res',$fee)";          
139     my $usth = $dbh->prepare($updquery);                      
140     $usth->execute;             
141     $usth->finish;                        
142   }                     
143   #if ($const eq 'a'){
144     my $query="insert into reserves
145    (borrowernumber,biblionumber,reservedate,branchcode,constrainttype,priority,reservenotes)
146     values
147 ('$borrnum','$biblionumber','$resdate','$branch','$const','$priority','$notes')";   
148     my $sth = $dbh->prepare($query);                        
149     $sth->execute();                
150     $sth->finish;
151   #}
152   if (($const eq "o") || ($const eq "e")) {     
153     my $numitems = @$bibitems;             
154     my $i = 0;                                        
155     while ($i < $numitems) {   
156       my $biblioitem = @$bibitems[$i];   
157       my $query = "insert into
158       reserveconstraints                          
159       (borrowernumber,biblionumber,reservedate,biblioitemnumber)         
160       values
161       ('$borrnum','$biblionumber','$resdate','$biblioitem')";                 
162       my $sth = $dbh->prepare($query);                    
163       $sth->execute();
164       $sth->finish;
165       $i++;                         
166     }                                   
167   } 
168 #  print $query;
169   $dbh->disconnect();         
170   return();   
171 }             
172
173 sub CalcReserveFee {
174   my ($env,$borrnum,$biblionumber,$constraint,$bibitems) = @_;        
175   #check for issues;    
176   my $dbh = &C4Connect;           
177   my $const = lc substr($constraint,0,1); 
178   my $query = "select * from borrowers,categories 
179   where (borrowernumber = '$borrnum')         
180   and (borrowers.categorycode = categories.categorycode)";   
181   my $sth = $dbh->prepare($query);                       
182   $sth->execute;                                    
183   my $data = $sth->fetchrow_hashref;                  
184   $sth->finish();       
185   my $fee = $data->{'reservefee'};         
186   my $cntitems = @->$bibitems;   
187   if ($fee > 0) {                         
188     # check for items on issue      
189     # first find biblioitem records       
190     my @biblioitems;    
191     my $query1 = "select * from biblio,biblioitems                           
192     where (biblio.biblionumber = '$biblionumber')     
193     and (biblio.biblionumber = biblioitems.biblionumber)";
194     my $sth1 = $dbh->prepare($query1);                   
195     $sth1->execute();                                     
196     while (my $data1=$sth1->fetchrow_hashref) { 
197       if ($const eq "a") {    
198         push @biblioitems,$data1;       
199       } else {                     
200         my $found = 0;        
201         my $x = 0;
202         while ($x < $cntitems) {                                             
203           if (@$bibitems->{'biblioitemnumber'} == $data->{'biblioitemnumber'}) {         
204             $found = 1;   
205           }               
206           $x++;                                       
207         }               
208         if ($const eq 'o') {
209           if ( $found == 1) {
210             push @biblioitems,$data1;
211           }                            
212         } else {
213           if ($found == 0) {
214             push @biblioitems,$data1;
215           } 
216         }     
217       }   
218     }             
219     $sth1->finish;                                  
220     my $cntitemsfound = @biblioitems; 
221     my $issues = 0;                 
222     my $x = 0;                   
223     my $allissued = 1; 
224     while ($x < $cntitemsfound) { 
225       my $bitdata = @biblioitems[$x];                                       
226       my $query2 = "select * from items                   
227       where biblioitemnumber = '$bitdata->{'biblioitemnumber'}'";     
228       my $sth2 = $dbh->prepare($query2);                       
229       $sth2->execute;   
230       while (my $itdata=$sth2->fetchrow_hashref) { 
231         my $query3 = "select * from issues
232         where itemnumber = '$itdata->{'itemnumber'}' and
233         returndate is null";
234         
235         my $sth3 = $dbh->prepare($query3);                      
236         $sth3->execute();                     
237         if (my $isdata=$sth3->fetchrow_hashref) {
238         } else {
239           $allissued = 0; 
240         }  
241       }                                                           
242       $x++;   
243     }         
244     if ($allissued == 0) { 
245       my $rquery = "select * from reserves           
246       where biblionumber = '$biblionumber'"; 
247       my $rsth = $dbh->prepare($rquery);   
248       $rsth->execute();   
249       if (my $rdata = $rsth->fetchrow_hashref) { 
250       } else {                                     
251         $fee = 0;                                                           
252       }   
253     }             
254   }                   
255 #  print "fee $fee";
256   $dbh->disconnect();   
257   return $fee;                                      
258 }                   
259
260 sub getnextacctno {                                                           
261   my ($env,$bornumber,$dbh)=@_;           
262   my $nextaccntno = 1;      
263   my $query = "select * from accountlines                             
264   where (borrowernumber = '$bornumber')                               
265   order by accountno desc";                       
266   my $sth = $dbh->prepare($query);                                  
267   $sth->execute;                    
268   if (my $accdata=$sth->fetchrow_hashref){    
269     $nextaccntno = $accdata->{'accountno'} + 1;           
270   }                       
271   $sth->finish;                                       
272   return($nextaccntno);                   
273 }              
274
275 sub updatereserves{
276   #subroutine to update a reserve 
277   my ($rank,$biblio,$borrower,$del,$branch)=@_;
278   my $dbh=C4Connect;
279   my $query="Update reserves ";
280   if ($del ==0){
281     $query.="set  priority='$rank',branchcode='$branch' where
282     biblionumber=$biblio and borrowernumber=$borrower";
283   } else {
284     $query="Select * from reserves where biblionumber=$biblio and
285     borrowernumber=$borrower";
286     my $sth=$dbh->prepare($query);
287     $sth->execute;
288     my $data=$sth->fetchrow_hashref;
289     $sth->finish;
290     $query="Select * from reserves where biblionumber=$biblio and 
291     priority > '$data->{'priority'}' and cancellationdate is NULL 
292     order by priority";
293     my $sth2=$dbh->prepare($query) || die $dbh->errstr;
294     $sth2->execute || die $sth2->errstr;
295     while (my $data=$sth2->fetchrow_hashref){
296       $data->{'priority'}--;
297       $query="Update reserves set priority=$data->{'priority'} where
298       biblionumber=$data->{'biblionumber'} and
299       borrowernumber=$data->{'borrowernumber'}";
300       my $sth3=$dbh->prepare($query);
301       $sth3->execute || die $sth3->errstr;
302       $sth3->finish;
303     }
304     $sth2->finish;
305     $query="update reserves set cancellationdate=now() where biblionumber=$biblio 
306     and borrowernumber=$borrower";    
307   }
308   my $sth=$dbh->prepare($query);
309   $sth->execute;
310   $sth->finish;  
311   $dbh->disconnect;
312 }
313
314 sub getreservetitle {
315  my ($biblio,$bor,$date,$timestamp)=@_;
316  my $dbh=C4Connect;
317  my $query="Select * from reserveconstraints,biblioitems where
318  reserveconstraints.biblioitemnumber=biblioitems.biblioitemnumber
319  and reserveconstraints.biblionumber=$biblio and reserveconstraints.borrowernumber
320  = $bor and reserveconstraints.reservedate='$date' and
321  reserveconstraints.timestamp=$timestamp";
322  my $sth=$dbh->prepare($query);
323  $sth->execute;
324  my $data=$sth->fetchrow_hashref;
325  $sth->finish;
326  $dbh->disconnect;
327 # print $query;
328  return($data);
329 }
330
331
332
333
334
335                         
336 END { }       # module clean-up code here (global destructor)