Added some subroutines.
[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 &CheckReserves &CancelReserve &ReserveWaiting &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       $bib = $dbh->quote($bib);
61       if ($bor ne ''){
62           $bor = $dbh->quote($bor);
63           $query .=  " where reserves.biblionumber   = $bib
64                          and borrowers.borrowernumber = $bor 
65                          and reserves.borrowernumber = borrowers.borrowernumber 
66                          and biblio.biblionumber     = $bib 
67                          and cancellationdate is NULL 
68                          and (found <> 'F' or found is NULL)";
69       } else {
70           $query .= " where reserves.borrowernumber = borrowers.borrowernumber
71                         and biblio.biblionumber     = $bib 
72                         and reserves.biblionumber   = $bib
73                         and cancellationdate is NULL 
74                         and (found <> 'F' or found is NULL)";
75       }
76   } else {
77       $query .= " where borrowers.borrowernumber = $bor 
78                     and reserves.borrowernumber  = borrowers.borrowernumber 
79                     and reserves.biblionumber    = biblio.biblionumber 
80                     and cancellationdate is NULL and 
81                     (found <> 'F' or found is NULL)";
82   }
83   $query.=" order by priority";
84   my $sth=$dbh->prepare($query);
85   $sth->execute;
86   my $i=0;
87   my @results;
88   while (my $data=$sth->fetchrow_hashref){
89     $results[$i]=$data;
90     $i++;
91   }
92 #  print $query;
93   $sth->finish;
94   $dbh->disconnect;
95   return($i,\@results);
96 }
97
98 sub CheckReserves {
99     my ($item) = @_;
100     my $dbh=C4Connect;
101     my $qitem=$dbh->quote($item);
102 # get the biblionumber...
103     my $sth=$dbh->prepare("select biblionumber from items where itemnumber=$qitem");
104     $sth->execute;
105     my ($biblio) = $sth->fetchrow_array;
106     $sth->finish;
107     $dbh->disconnect;
108 # get the reserves...
109     my ($count, $reserves) = FindReserves($biblio);
110     my $priority = 10000000; 
111     my $highest;
112     if ($count) {
113         foreach my $res (@$reserves) {
114             if ($res->{'itemnumber'} == $item) {
115                 return ("Waiting", $res);
116             } else {
117                 if ($res->{'priority'} < $priority) {
118                     $priority = $res->{'priority'};
119                     $highest = $res;
120                 }
121             }
122         }
123         $highest->{'itemnumber'} = $item;
124         return ("Reserved", $highest);
125     } else {
126         return (0, 0);
127     }
128 }
129
130 sub CancelReserve {
131     my ($biblio, $item, $borr) = @_;
132     my $dbh=C4Connect;
133     if (($item and $borr) and (not $biblio)) {
134 # removing a waiting reserve record....
135         $item = $dbh->quote($item);
136         $borr = $dbh->quote($borr);
137 # update the database...
138         my $query = "update reserves set cancellationdate = now(), 
139                                          found            = Null, 
140                                          priority         = 0 
141                                    where itemnumber       = $item 
142                                      and borrowernumber   = $borr";
143         my $sth = $dbh->prepare($query);
144         $sth->execute;
145         $sth->finish;
146     }
147     if (($biblio and $borr) and (not $item)) {
148 # removing a reserve record....
149         $biblio = $dbh->quote($biblio);
150         $borr = $dbh->quote($borr);
151 # fix up the priorities on the other records....
152         my $query = "select priority from reserves 
153                                     where biblionumber   = $biblio 
154                                       and borrowernumber = $borr
155                                       and cancellationdate is NULL 
156                                       and (found <> 'F' or found is NULL)";
157         my $sth=$dbh->prepare($query);
158         $sth->execute;
159         my ($priority) = $sth->fetchrow_array;
160         $sth->finish;
161 # update the database, removing the record...
162         my $query = "update reserves set cancellationdate = now(), 
163                                          found            = Null, 
164                                          priority         = 0 
165                                    where biblionumber     = $biblio 
166                                      and borrowernumber   = $borr
167                                      and cancellationdate is NULL 
168                                      and (found <> 'F' or found is NULL)";
169         my $sth = $dbh->prepare($query);
170         $sth->execute;
171         $sth->finish;
172 # now fix the priority on the others....
173         my ($count, $reserves) = FindReserves($biblio);
174         foreach my $rec (@$reserves) {
175             if ($rec->{'priority'} > $priority) {
176                 my $newpr = $rec->{'priority'};
177                 $newpr = $dbh->quote($newpr - 1);
178                 my $query = "update reserves set priority = $newpr 
179                                    where biblionumber     = $rec->{'biblionumber'} 
180                                      and borrowernumber   = $rec->{'borrowernumber'}
181                                      and cancellationdate is NULL 
182                                      and (found <> 'F' or found is NULL)";
183                 my $sth = $dbh->prepare($query);
184                 $sth->execute;
185                 $sth->finish;
186             }
187         } 
188     }
189     $dbh->disconnect;
190 }
191
192 sub ReserveWaiting {
193     my ($item, $borr) = @_;
194     my $dbh = C4Connect;
195     $item = $dbh->quote($item);
196     $borr = $dbh->quote($borr);
197 # get priority and biblionumber....
198     my $query = "SELECT reserves.priority     as priority, 
199                         reserves.biblionumber as biblionumber,
200                         reserves.branchcode   as branchcode 
201                       FROM reserves,items 
202                      WHERE reserves.biblionumber   = items.biblionumber 
203                        AND items.itemnumber        = $item 
204                        AND reserves.borrowernumber = $borr 
205                        AND reserves.cancellationdate is NULL
206                        AND (reserves.found <> 'F' or reserves.found is NULL)";
207     my $sth = $dbh->prepare($query);
208     $sth->execute;
209     my $data = $sth->fetchrow_hashref;
210     $sth->finish;
211     my $biblio = $data->{'biblionumber'};
212     $biblio = $dbh->quote($biblio);
213 # update reserves record....
214     $query = "update reserves set priority = 0, found = 'W', itemnumber = $item 
215                             where borrowernumber = $borr and biblionumber = $biblio";
216     $sth = $dbh->prepare($query);
217     $sth->execute;
218     $sth->finish;
219 # now fix up the remaining priorities....
220     $query = "select priority, borrowernumber from reserves where biblionumber = $biblio
221                                               and cancellationdate is NULL
222                                               and found is NULL
223                                               and priority <> 0 and priority is not NULL";
224     $sth = $dbh->prepare($query);
225     $sth->execute;
226     my $branchcode = $data->{'branchcode'};
227     my $priority = $data->{'priority'};
228     while (my $data = $sth->fetchrow_hashref) {
229         if ($data->{'priority'} > $priority) {
230             my $uquery = "update reserves set priority       = priority - 1 
231                                         where biblionumber   = $biblio 
232                                           and borrowernumber = $borr
233                                           and cancellation  is NULL";
234             my $usth->$dbh->prepare($query);
235             $usth->execute;
236             $usth->finish;
237         }
238     }
239     $sth->finish;
240     $dbh->disconnect;
241     return $branchcode;
242 }
243
244 sub Findgroupreserve {
245   my ($bibitem,$biblio)=@_;
246   my $dbh=C4Connect;
247   $bibitem=$dbh->quote($bibitem);
248   my $query="Select * from reserves 
249   left join reserveconstraints on
250   reserves.biblionumber=reserveconstraints.biblionumber
251   where
252   reserves.biblionumber=$biblio and
253   ((reserveconstraints.biblioitemnumber=$bibitem 
254   and reserves.borrowernumber=reserveconstraints.borrowernumber
255   and reserves.reservedate=reserveconstraints.reservedate)
256   or reserves.constrainttype='a')
257   and reserves.cancellationdate is NULL
258   and (reserves.found <> 'F' or reserves.found is NULL)";
259 #  print $query;
260   my $sth=$dbh->prepare($query);
261   $sth->execute;
262   my $i=0;
263   my @results;
264   while (my $data=$sth->fetchrow_hashref){
265     $results[$i]=$data;
266     $i++;
267   }
268   $sth->finish;
269   $dbh->disconnect;
270   return($i,@results);
271 }
272
273 sub CreateReserve {                                                           
274   my
275 ($env,$branch,$borrnum,$biblionumber,$constraint,$bibitems,$priority,$notes,$title)= @_;   
276   my $fee=CalcReserveFee($env,$borrnum,$biblionumber,$constraint,$bibitems);
277   my $dbh = &C4Connect;       
278   my $const = lc substr($constraint,0,1);       
279   my @datearr = localtime(time);                                
280   my $resdate =(1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];                   
281   #eval {                                                           
282   # updates take place here             
283   if ($fee > 0) {           
284 #    print $fee;
285     my $nextacctno = &getnextacctno($env,$borrnum,$dbh);   
286     my $updquery = "insert into accountlines       
287     (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)                                              
288                                                           values
289     ($borrnum,$nextacctno,now(),$fee,'Reserve Charge - $title','Res',$fee)";          
290     my $usth = $dbh->prepare($updquery);                      
291     $usth->execute;             
292     $usth->finish;                        
293   }                     
294   #if ($const eq 'a'){
295     my $query="insert into reserves
296    (borrowernumber,biblionumber,reservedate,branchcode,constrainttype,priority,reservenotes)
297     values
298 ('$borrnum','$biblionumber','$resdate','$branch','$const','$priority','$notes')";   
299     my $sth = $dbh->prepare($query);                        
300     $sth->execute();                
301     $sth->finish;
302   #}
303   if (($const eq "o") || ($const eq "e")) {     
304     my $numitems = @$bibitems;             
305     my $i = 0;                                        
306     while ($i < $numitems) {   
307       my $biblioitem = @$bibitems[$i];   
308       my $query = "insert into
309       reserveconstraints                          
310       (borrowernumber,biblionumber,reservedate,biblioitemnumber)         
311       values
312       ('$borrnum','$biblionumber','$resdate','$biblioitem')";                 
313       my $sth = $dbh->prepare($query);                    
314       $sth->execute();
315       $sth->finish;
316       $i++;                         
317     }                                   
318   } 
319 #  print $query;
320   $dbh->disconnect();         
321   return();   
322 }             
323
324 sub CalcReserveFee {
325   my ($env,$borrnum,$biblionumber,$constraint,$bibitems) = @_;        
326   #check for issues;    
327   my $dbh = &C4Connect;           
328   my $const = lc substr($constraint,0,1); 
329   my $query = "select * from borrowers,categories 
330   where (borrowernumber = '$borrnum')         
331   and (borrowers.categorycode = categories.categorycode)";   
332   my $sth = $dbh->prepare($query);                       
333   $sth->execute;                                    
334   my $data = $sth->fetchrow_hashref;                  
335   $sth->finish();       
336   my $fee = $data->{'reservefee'};         
337   my $cntitems = @->$bibitems;   
338   if ($fee > 0) {                         
339     # check for items on issue      
340     # first find biblioitem records       
341     my @biblioitems;    
342     my $query1 = "select * from biblio,biblioitems                           
343     where (biblio.biblionumber = '$biblionumber')     
344     and (biblio.biblionumber = biblioitems.biblionumber)";
345     my $sth1 = $dbh->prepare($query1);                   
346     $sth1->execute();                                     
347     while (my $data1=$sth1->fetchrow_hashref) { 
348       if ($const eq "a") {    
349         push @biblioitems,$data1;       
350       } else {                     
351         my $found = 0;        
352         my $x = 0;
353         while ($x < $cntitems) {                                             
354           if (@$bibitems->{'biblioitemnumber'} == $data->{'biblioitemnumber'}) {         
355             $found = 1;   
356           }               
357           $x++;                                       
358         }               
359         if ($const eq 'o') {
360           if ( $found == 1) {
361             push @biblioitems,$data1;
362           }                            
363         } else {
364           if ($found == 0) {
365             push @biblioitems,$data1;
366           } 
367         }     
368       }   
369     }             
370     $sth1->finish;                                  
371     my $cntitemsfound = @biblioitems; 
372     my $issues = 0;                 
373     my $x = 0;                   
374     my $allissued = 1; 
375     while ($x < $cntitemsfound) { 
376       my $bitdata = @biblioitems[$x];                                       
377       my $query2 = "select * from items                   
378       where biblioitemnumber = '$bitdata->{'biblioitemnumber'}'";     
379       my $sth2 = $dbh->prepare($query2);                       
380       $sth2->execute;   
381       while (my $itdata=$sth2->fetchrow_hashref) { 
382         my $query3 = "select * from issues
383         where itemnumber = '$itdata->{'itemnumber'}' and
384         returndate is null";
385         
386         my $sth3 = $dbh->prepare($query3);                      
387         $sth3->execute();                     
388         if (my $isdata=$sth3->fetchrow_hashref) {
389         } else {
390           $allissued = 0; 
391         }  
392       }                                                           
393       $x++;   
394     }         
395     if ($allissued == 0) { 
396       my $rquery = "select * from reserves           
397       where biblionumber = '$biblionumber'"; 
398       my $rsth = $dbh->prepare($rquery);   
399       $rsth->execute();   
400       if (my $rdata = $rsth->fetchrow_hashref) { 
401       } else {                                     
402         $fee = 0;                                                           
403       }   
404     }             
405   }                   
406 #  print "fee $fee";
407   $dbh->disconnect();   
408   return $fee;                                      
409 }                   
410
411 sub getnextacctno {                                                           
412   my ($env,$bornumber,$dbh)=@_;           
413   my $nextaccntno = 1;      
414   my $query = "select * from accountlines                             
415   where (borrowernumber = '$bornumber')                               
416   order by accountno desc";                       
417   my $sth = $dbh->prepare($query);                                  
418   $sth->execute;                    
419   if (my $accdata=$sth->fetchrow_hashref){    
420     $nextaccntno = $accdata->{'accountno'} + 1;           
421   }                       
422   $sth->finish;                                       
423   return($nextaccntno);                   
424 }              
425
426 sub updatereserves{
427   #subroutine to update a reserve 
428   my ($rank,$biblio,$borrower,$del,$branch)=@_;
429   my $dbh=C4Connect;
430   my $query="Update reserves ";
431   if ($del ==0){
432     $query.="set  priority='$rank',branchcode='$branch' where
433     biblionumber=$biblio and borrowernumber=$borrower";
434   } else {
435     $query="Select * from reserves where biblionumber=$biblio and
436     borrowernumber=$borrower";
437     my $sth=$dbh->prepare($query);
438     $sth->execute;
439     my $data=$sth->fetchrow_hashref;
440     $sth->finish;
441     $query="Select * from reserves where biblionumber=$biblio and 
442     priority > '$data->{'priority'}' and cancellationdate is NULL 
443     order by priority";
444     my $sth2=$dbh->prepare($query) || die $dbh->errstr;
445     $sth2->execute || die $sth2->errstr;
446     while (my $data=$sth2->fetchrow_hashref){
447       $data->{'priority'}--;
448       $query="Update reserves set priority=$data->{'priority'} where
449       biblionumber=$data->{'biblionumber'} and
450       borrowernumber=$data->{'borrowernumber'}";
451       my $sth3=$dbh->prepare($query);
452       $sth3->execute || die $sth3->errstr;
453       $sth3->finish;
454     }
455     $sth2->finish;
456     $query="update reserves set cancellationdate=now() where biblionumber=$biblio 
457     and borrowernumber=$borrower";    
458   }
459   my $sth=$dbh->prepare($query);
460   $sth->execute;
461   $sth->finish;  
462   $dbh->disconnect;
463 }
464
465 sub getreservetitle {
466  my ($biblio,$bor,$date,$timestamp)=@_;
467  my $dbh=C4Connect;
468  my $query="Select * from reserveconstraints,biblioitems where
469  reserveconstraints.biblioitemnumber=biblioitems.biblioitemnumber
470  and reserveconstraints.biblionumber=$biblio and reserveconstraints.borrowernumber
471  = $bor and reserveconstraints.reservedate='$date' and
472  reserveconstraints.timestamp=$timestamp";
473  my $sth=$dbh->prepare($query);
474  $sth->execute;
475  my $data=$sth->fetchrow_hashref;
476  $sth->finish;
477  $dbh->disconnect;
478 # print $query;
479  return($data);
480 }
481
482
483
484
485
486                         
487 END { }       # module clean-up code here (global destructor)