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