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