Fix so that reserves are only showing for the biblioitem not the biblio 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)=@_;
95   my $dbh=C4Connect;
96   $bibitem=$dbh->quote($bibitem);
97   my $query="Select * from reserves,reserveconstraints where
98   reserves.biblionumber=reservesconstraints.biblionumber and
99   and reserveconstraints.biblioitemnumber=$bibitem
100   and reserves.cancellationdate is NULL
101   and (reserves.found <> 'F' or reserves.found is NULL)";
102   my $sth=$dbh->prepare($query);
103   $sth->execute;
104   my $i=0;
105   my @results;
106   while (my $data=$sth->fetchrow_hashref){
107     $results[$i]=$data;
108     $i++;
109   }
110   $sth->finish;
111   $dbh->disconnect;
112   return($i,@results);
113 }
114
115 sub CreateReserve {                                                           
116   my
117 ($env,$branch,$borrnum,$biblionumber,$constraint,$bibitems,$priority,$notes,$title)= @_;   
118   my $fee=CalcReserveFee($env,$borrnum,$biblionumber,$constraint,$bibitems);
119   my $dbh = &C4Connect;       
120   my $const = lc substr($constraint,0,1);       
121   my @datearr = localtime(time);                                
122   my $resdate =(1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];                   
123   #eval {                                                           
124   # updates take place here             
125   if ($fee > 0) {           
126 #    print $fee;
127     my $nextacctno = &getnextacctno($env,$borrnum,$dbh);   
128     my $updquery = "insert into accountlines       
129     (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)                                              
130                                                           values
131     ($borrnum,$nextacctno,now(),$fee,'Reserve Charge - $title','Res',$fee)";          
132     my $usth = $dbh->prepare($updquery);                      
133     $usth->execute;             
134     $usth->finish;                        
135   }                     
136   #if ($const eq 'a'){
137     my $query="insert into reserves
138    (borrowernumber,biblionumber,reservedate,branchcode,constrainttype,priority,reservenotes)
139     values
140 ('$borrnum','$biblionumber','$resdate','$branch','$const','$priority','$notes')";   
141     my $sth = $dbh->prepare($query);                        
142     $sth->execute();                
143     $sth->finish;
144   #}
145   if (($const eq "o") || ($const eq "e")) {     
146     my $numitems = @$bibitems;             
147     my $i = 0;                                        
148     while ($i < $numitems) {   
149       my $biblioitem = @$bibitems[$i];   
150       my $query = "insert into
151       reserveconstraints                          
152       (borrowernumber,biblionumber,reservedate,biblioitemnumber)         
153       values
154       ('$borrnum','$biblionumber','$resdate','$biblioitem')";                 
155       my $sth = $dbh->prepare($query);                    
156       $sth->execute();
157       $sth->finish;
158       $i++;                         
159     }                                   
160   } 
161 #  print $query;
162   $dbh->disconnect();         
163   return();   
164 }             
165
166 sub CalcReserveFee {
167   my ($env,$borrnum,$biblionumber,$constraint,$bibitems) = @_;        
168   #check for issues;    
169   my $dbh = &C4Connect;           
170   my $const = lc substr($constraint,0,1); 
171   my $query = "select * from borrowers,categories 
172   where (borrowernumber = '$borrnum')         
173   and (borrowers.categorycode = categories.categorycode)";   
174   my $sth = $dbh->prepare($query);                       
175   $sth->execute;                                    
176   my $data = $sth->fetchrow_hashref;                  
177   $sth->finish();       
178   my $fee = $data->{'reservefee'};         
179   my $cntitems = @->$bibitems;   
180   if ($fee > 0) {                         
181     # check for items on issue      
182     # first find biblioitem records       
183     my @biblioitems;    
184     my $query1 = "select * from biblio,biblioitems                           
185     where (biblio.biblionumber = '$biblionumber')     
186     and (biblio.biblionumber = biblioitems.biblionumber)";
187     my $sth1 = $dbh->prepare($query1);                   
188     $sth1->execute();                                     
189     while (my $data1=$sth1->fetchrow_hashref) { 
190       if ($const eq "a") {    
191         push @biblioitems,$data1;       
192       } else {                     
193         my $found = 0;        
194         my $x = 0;
195         while ($x < $cntitems) {                                             
196           if (@$bibitems->{'biblioitemnumber'} == $data->{'biblioitemnumber'}) {         
197             $found = 1;   
198           }               
199           $x++;                                       
200         }               
201         if ($const eq 'o') {
202           if ( $found == 1) {
203             push @biblioitems,$data1;
204           }                            
205         } else {
206           if ($found == 0) {
207             push @biblioitems,$data1;
208           } 
209         }     
210       }   
211     }             
212     $sth1->finish;                                  
213     my $cntitemsfound = @biblioitems; 
214     my $issues = 0;                 
215     my $x = 0;                   
216     my $allissued = 1; 
217     while ($x < $cntitemsfound) { 
218       my $bitdata = @biblioitems[$x];                                       
219       my $query2 = "select * from items                   
220       where biblioitemnumber = '$bitdata->{'biblioitemnumber'}'";     
221       my $sth2 = $dbh->prepare($query2);                       
222       $sth2->execute;   
223       while (my $itdata=$sth2->fetchrow_hashref) { 
224         my $query3 = "select * from issues
225         where itemnumber = '$itdata->{'itemnumber'}' and
226         returndate is null";
227         
228         my $sth3 = $dbh->prepare($query3);                      
229         $sth3->execute();                     
230         if (my $isdata=$sth3->fetchrow_hashref) {
231         } else {
232           $allissued = 0; 
233         }  
234       }                                                           
235       $x++;   
236     }         
237     if ($allissued == 0) { 
238       my $rquery = "select * from reserves           
239       where biblionumber = '$biblionumber'"; 
240       my $rsth = $dbh->prepare($rquery);   
241       $rsth->execute();   
242       if (my $rdata = $rsth->fetchrow_hashref) { 
243       } else {                                     
244         $fee = 0;                                                           
245       }   
246     }             
247   }                   
248 #  print "fee $fee";
249   $dbh->disconnect();   
250   return $fee;                                      
251 }                   
252
253 sub getnextacctno {                                                           
254   my ($env,$bornumber,$dbh)=@_;           
255   my $nextaccntno = 1;      
256   my $query = "select * from accountlines                             
257   where (borrowernumber = '$bornumber')                               
258   order by accountno desc";                       
259   my $sth = $dbh->prepare($query);                                  
260   $sth->execute;                    
261   if (my $accdata=$sth->fetchrow_hashref){    
262     $nextaccntno = $accdata->{'accountno'} + 1;           
263   }                       
264   $sth->finish;                                       
265   return($nextaccntno);                   
266 }              
267
268 sub updatereserves{
269   #subroutine to update a reserve 
270   my ($rank,$biblio,$borrower,$del,$branch)=@_;
271   my $dbh=C4Connect;
272   my $query="Update reserves ";
273   if ($del ==0){
274     $query.="set  priority='$rank',branchcode='$branch' where
275     biblionumber=$biblio and borrowernumber=$borrower";
276   } else {
277     $query="Select * from reserves where biblionumber=$biblio and
278     borrowernumber=$borrower";
279     my $sth=$dbh->prepare($query);
280     $sth->execute;
281     my $data=$sth->fetchrow_hashref;
282     $sth->finish;
283     $query="Select * from reserves where biblionumber=$biblio and 
284     priority > '$data->{'priority'}' and cancellationdate is NULL 
285     order by priority";
286     my $sth2=$dbh->prepare($query) || die $dbh->errstr;
287     $sth2->execute || die $sth2->errstr;
288     while (my $data=$sth2->fetchrow_hashref){
289       $data->{'priority'}--;
290       $query="Update reserves set priority=$data->{'priority'} where
291       biblionumber=$data->{'biblionumber'} and
292       borrowernumber=$data->{'borrowernumber'}";
293       my $sth3=$dbh->prepare($query);
294       $sth3->execute || die $sth3->errstr;
295       $sth3->finish;
296     }
297     $sth2->finish;
298     $query="update reserves set cancellationdate=now() where biblionumber=$biblio 
299     and borrowernumber=$borrower";    
300   }
301   my $sth=$dbh->prepare($query);
302   $sth->execute;
303   $sth->finish;  
304   $dbh->disconnect;
305 }
306
307 sub getreservetitle {
308  my ($biblio,$bor,$date,$timestamp)=@_;
309  my $dbh=C4Connect;
310  my $query="Select * from reserveconstraints,biblioitems where
311  reserveconstraints.biblioitemnumber=biblioitems.biblioitemnumber
312  and reserveconstraints.biblionumber=$biblio and reserveconstraints.borrowernumber
313  = $bor and reserveconstraints.reservedate='$date' and
314  reserveconstraints.timestamp=$timestamp";
315  my $sth=$dbh->prepare($query);
316  $sth->execute;
317  my $data=$sth->fetchrow_hashref;
318  $sth->finish;
319  $dbh->disconnect;
320 # print $query;
321  return($data);
322 }
323
324
325
326
327
328                         
329 END { }       # module clean-up code here (global destructor)