Calculate itemcount in Search.pm if using new template based search interface
[koha.git] / C4 / Search.pm
1 package C4::Search; #asummes C4/Search
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::Reserves2;
11 use Set::Scalar;
12
13 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
14   
15 # set the version for version checking
16 $VERSION = 0.02;
17     
18 @ISA = qw(Exporter);
19 @EXPORT = qw(&CatSearch &BornameSearch &ItemInfo &KeywordSearch &subsearch
20 &itemdata &bibdata &GetItems &borrdata &itemnodata &itemcount
21 &borrdata2 &NewBorrowerNumber &bibitemdata &borrissues
22 &getboracctrecord &ItemType &itemissues &subject &subtitle
23 &addauthor &bibitems &barcodes &findguarantees &allissues &systemprefs
24 &findguarantor &getwebsites &getwebbiblioitems);
25 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
26                   
27 # your exported package globals go here,
28 # as well as any optionally exported functions
29
30 @EXPORT_OK   = qw($Var1 %Hashit);
31
32
33 # non-exported package globals go here
34 use vars qw(@more $stuff);
35         
36 # initalize package globals, first exported ones
37
38 my $Var1   = '';
39 my %Hashit = ();
40                     
41 # then the others (which are still accessible as $Some::Module::stuff)
42 my $stuff  = '';
43 my @more   = ();
44         
45 # all file-scoped lexicals must be created before
46 # the functions below that use them.
47                 
48 # file-private lexicals go here
49 my $priv_var    = '';
50 my %secret_hash = ();
51                             
52 # here's a file-private function as a closure,
53 # callable as &$priv_func;  it cannot be prototyped.
54 my $priv_func = sub {
55   # stuff goes here.
56 };
57                                                     
58 # make all your functions, whether exported or not;
59 sub findguarantees{         
60   my ($bornum)=@_;         
61   my $dbh=C4Connect;           
62   my $query="select cardnumber,borrowernumber from borrowers where    
63   guarantor='$bornum'";               
64   my $sth=$dbh->prepare($query);                 
65   $sth->execute;                   
66   my @dat;                     
67   my $i=0;                       
68   while (my $data=$sth->fetchrow_hashref){    
69     $dat[$i]=$data;                           
70     $i++;                               
71   }                                   
72   $sth->finish; 
73   $dbh->disconnect;         
74   return($i,\@dat);             
75 }
76 sub findguarantor{  
77   my ($bornum)=@_;  
78   my $dbh=C4Connect;    
79   my $query="select guarantor from borrowers where      
80   borrowernumber='$bornum'";        
81   my $sth=$dbh->prepare($query);          
82   $sth->execute;            
83   my $data=$sth->fetchrow_hashref;              
84   $sth->finish;                
85   $query="Select * from borrowers where
86   borrowernumber='$data->{'guarantor'}'";  
87   $sth=$dbh->prepare($query);  
88   $sth->execute;    
89   $data=$sth->fetchrow_hashref;      
90   $sth->finish;        
91   $dbh->disconnect;          
92   return($data);            
93 }
94
95 sub systemprefs {
96     my %systemprefs;
97     my $dbh=C4Connect;
98     my $sth=$dbh->prepare("select variable,value from systempreferences");
99     $sth->execute;
100     while (my ($variable,$value)=$sth->fetchrow) {
101         $systemprefs{$variable}=$value;
102     }
103     $sth->finish;
104     $dbh->disconnect;
105     return(%systemprefs);
106 }
107
108 sub NewBorrowerNumber {           
109   my $dbh=C4Connect;        
110   my $sth=$dbh->prepare("Select max(borrowernumber) from borrowers");     
111   $sth->execute;            
112   my $data=$sth->fetchrow_hashref;                                  
113   $sth->finish;                   
114   $data->{'max(borrowernumber)'}++;         
115   $dbh->disconnect;
116   return($data->{'max(borrowernumber)'}); 
117 }    
118
119   
120 sub KeywordSearch {
121   my ($env,$type,$search,$num,$offset)=@_;
122   my $dbh = &C4Connect;
123   $search->{'keyword'}=~ s/ +$//;
124   $search->{'keyword'}=~ s/'/\\'/;
125   my @key=split(' ',$search->{'keyword'});
126   my $count=@key;
127   my $i=1;
128   my @results;
129   my $query="Select biblionumber from biblio
130   where ((title like '$key[0]%' or title like '% $key[0]%')";
131   while ($i < $count){                                                  
132       $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%')";                                                   
133       $i++;                                                  
134   }
135   $query.= ") or ((biblio.notes like '$key[0]%' or biblio.notes like '% $key[0]%')";                                             
136   for ($i=1;$i<$count;$i++){                                                  
137       $query.=" and (biblio.notes like '$key[$i]%' or biblio.notes like '% $key[$i]%')";                                           
138   }
139    $query.= ") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%')";                                               
140   for ($i=1;$i<$count;$i++){                                                  
141       $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";                                             
142   }
143   $query.=" )";
144 #  print $query;
145   my $sth=$dbh->prepare($query);
146   $sth->execute;
147   my $i=0;
148   while (my @res=$sth->fetchrow_array){
149     $results[$i]=$res[0];
150     $i++;
151   }
152   $sth->finish;
153   my $set1=Set::Scalar->new(@results);
154   $query="Select biblionumber from bibliosubtitle where
155   ((subtitle like '$key[0]%' or subtitle like '% $key[0]%')";                 
156   for ($i=1;$i<$count;$i++){   
157         $query.= " and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%')";                                                  
158   }                   
159   $query.=" )";
160 #  print $query;
161   $sth=$dbh->prepare($query);
162   $sth->execute;
163   $i=0;
164   while (my @res=$sth->fetchrow_array){
165     $results[$i]=$res[0];
166     $i++;
167   }
168   $sth->finish;
169   my $set2=Set::Scalar->new(@results);
170   if ($i > 0){
171     $set1=$set1+$set2;
172   }
173   $query ="Select biblionumber from biblioitems where
174   ((biblioitems.notes like '$key[0]%' or biblioitems.notes like '% $key[0]%')";                                   
175   for ($i=1;$i<$count;$i++){                                                  
176       $query.=" and (biblioitems.notes like '$key[$i]%' or biblioitems.notes like '% $key[$i]%')";                                 
177   }            
178   $query.=" )";
179 #  print $query;
180   $sth=$dbh->prepare($query);
181   $sth->execute;
182   $i=0;
183   while (my @res=$sth->fetchrow_array){
184     $results[$i]=$res[0];
185     $i++;
186   }
187   $sth->finish;
188   my $set3=Set::Scalar->new(@results);    
189   if ($i > 0){
190     $set1=$set1+$set3;
191   }
192   $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
193   like '%$search->{'keyword'}%' group by biblionumber");
194   $sth->execute;
195   $i=0;
196   while (my @res=$sth->fetchrow_array){
197     $results[$i]=$res[0];
198     $i++;
199   }
200   $sth->finish;
201   my $set4=Set::Scalar->new(@results);    
202   if ($i > 0){
203     $set1=$set1+$set4;
204   }
205   my $i2=0;
206   my $i3=0;
207   my $i4=0;
208
209   my @res2;
210   my @res = $set1->members;
211   $count=@res;
212 #  print $set1;
213   $i=0;
214 #  print "count $count";
215   if ($search->{'class'} ne ''){ 
216     while ($i2 <$count){
217       my $query="select * from biblio,biblioitems where 
218       biblio.biblionumber='$res[$i2]' and     
219       biblio.biblionumber=biblioitems.biblionumber ";         
220       if ($search->{'class'} ne ''){             
221       my @temp=split(/\|/,$search->{'class'});
222       my $count=@temp;                       
223       $query.= "and ( itemtype='$temp[0]'";     
224       for (my $i=1;$i<$count;$i++){     
225         $query.=" or itemtype='$temp[$i]'";                                     
226       } 
227       $query.=")"; 
228       }
229        my $sth=$dbh->prepare($query);    
230        #    print $query;        
231        $sth->execute;        
232        if (my $data2=$sth->fetchrow_hashref){            
233          my $dewey= $data2->{'dewey'};                
234          my $subclass=$data2->{'subclass'};          
235          $dewey=~s/\.*0*$//;           
236          ($dewey == 0) && ($dewey=''); 
237          ($dewey) && ($dewey.=" $subclass") ;                                    
238           $sth->finish;
239           my $end=$offset +$num;
240           if ($i4 <= $offset){
241             $i4++;
242           }
243 #         print $i4;
244           if ($i4 <=$end && $i4 > $offset){
245             $data2->{'dewey'}=$dewey;
246             $res2[$i3]=$data2;
247             
248 #           $res2[$i3]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";    
249             $i3++;
250             $i4++;
251 #           print "in here $i3<br>";
252           } else {
253 #           print $end;
254           }
255           $i++; 
256         }         
257      $i2++;
258      }
259      $count=$i;
260   
261    } else {
262   while ($i2 < $num && $i2 < $count){
263     my $query="select * from biblio,biblioitems where
264     biblio.biblionumber='$res[$i2+$offset]' and        
265     biblio.biblionumber=biblioitems.biblionumber ";
266     if ($search->{'class'} ne ''){
267       my @temp=split(/\|/,$search->{'class'});
268       my $count=@temp;
269       $query.= "and ( itemtype='$temp[0]'";
270       for (my $i=1;$i<$count;$i++){
271         $query.=" or itemtype='$temp[$i]'";
272       }
273       $query.=")"; 
274     }
275     if ($search->{'dewey'} ne ''){
276       $query.= "and (dewey like '$search->{'dewey'}%') ";
277     }
278
279     my $sth=$dbh->prepare($query);
280 #    print $query;
281     $sth->execute;
282     if (my $data2=$sth->fetchrow_hashref){
283         my $dewey= $data2->{'dewey'};               
284         my $subclass=$data2->{'subclass'};                   
285         $dewey=~s/\.*0*$//;     
286         ($dewey == 0) && ($dewey='');               
287         ($dewey) && ($dewey.=" $subclass") ;                      
288         $sth->finish;                                             
289         $data2->{'dewey'}=$dewey;
290         if ($env->{itemcount}) {
291             my ($count, $lcount, $nacount, $fcount, $scount, $lostcount, $mending, $transit, $ocount) = itemcount($env, $data2->{'biblionumber'}, 'intra');
292             $data2->{'itemcount'}=$count;
293         }
294         $res2[$i]=$data2;
295 #       $res2[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
296         $i++;
297     }
298     $i2++;
299     
300   }
301   }
302   $dbh->disconnect;
303
304   #$count=$i;
305   return($count,@res2);
306 }
307
308 sub CatSearch  {
309   my ($env,$type,$search,$num,$offset)=@_;
310   my $dbh = &C4Connect;
311   my $query = '';
312     my @results;
313   $search->{'title'}=~ s/'/\\'/g;
314   $search->{'author'}=~ s/'/\\'/g;
315   $search->{'illustrator'}=~ s/'/\\'/g;
316   my $title = lc($search->{'title'}); 
317   
318   if ($type eq 'loose') {
319       if ($search->{'author'} ne ''){
320         my @key=split(' ',$search->{'author'});
321         my $count=@key;
322         my $i=1;
323         $query="select *,biblio.author,biblio.biblionumber from
324          biblio
325          left join additionalauthors
326          on additionalauthors.biblionumber =biblio.biblionumber
327          where
328          ((biblio.author like '$key[0]%' or biblio.author like '% $key[0]%' or
329          additionalauthors.author like '$key[0]%' or additionalauthors.author 
330          like '% $key[0]%'
331                  )";    
332          while ($i < $count){ 
333            $query=$query." and (
334            biblio.author like '$key[$i]%' or biblio.author like '% $key[$i]%' or
335            additionalauthors.author like '$key[$i]%' or additionalauthors.author like '% $key[$i]%'
336            )";
337            $i++;       
338          }   
339          $query=$query.")";
340          if ($search->{'title'} ne ''){ 
341            my @key=split(' ',$search->{'title'});
342            my $count=@key;
343            my $i=0;
344            $query.= " and (((title like '$key[0]%' or title like '% $key[0]%' or title like '% $key[0]')";
345             while ($i<$count){            
346               $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%' or title like '% $key[$i]')";
347               $i++; 
348             }                       
349 #           $query.=") or ((subtitle like '$key[0]%' or subtitle like '% $key[0] %' or subtitle like '% $key[0]')"; 
350 #            for ($i=1;$i<$count;$i++){
351 #             $query.=" and (subtitle like '$key[$i]%' or subtitle like '% $key[$i] %' or subtitle like '% $key[$i]')";   
352 #            }
353             $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%' or seriestitle like '% $key[0]')";  
354             for ($i=1;$i<$count;$i++){                    
355                 $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
356             }                                                             
357             $query.=") or ((unititle like '$key[0]%' or unititle like '% $key[0]%' or unititle like '% $key[0]')";                         
358             for ($i=1;$i<$count;$i++){                    
359                 $query.=" and (unititle like '$key[$i]%' or unititle like '% $key[$i]%')";   
360             }                                                             
361             $query=$query."))"; 
362            #$query=$query. " and (title like '%$search->{'title'}%' 
363            #or seriestitle like '%$search->{'title'}%')";
364          }
365                  
366          $query.=" group by biblio.biblionumber";
367       } else {
368           if ($search->{'title'} ne '') {
369            if ($search->{'ttype'} eq 'exact'){
370              $query="select * from biblio
371              where                            
372              (biblio.title='$search->{'title'}' or (biblio.unititle = '$search->{'title'}'
373              or biblio.unititle like '$search->{'title'} |%' or 
374              biblio.unititle like '%| $search->{'title'} |%' or
375              biblio.unititle like '%| $search->{'title'}') or
376              (biblio.seriestitle = '$search->{'title'}' or
377              biblio.seriestitle like '$search->{'title'} |%' or
378              biblio.seriestitle like '%| $search->{'title'} |%' or
379              biblio.seriestitle like '%| $search->{'title'}')
380              )";
381            } else {
382             my @key=split(' ',$search->{'title'});
383             my $count=@key;
384             my $i=1;
385             $query="select * from biblio
386             left join bibliosubtitle on
387             biblio.biblionumber=bibliosubtitle.biblionumber
388             where
389             (((title like '$key[0]%' or title like '% $key[0]%' or title like '% $key[0]')";
390             while ($i<$count){
391               $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%' or title like '% $key[$i]')";
392               $i++;
393             }
394             $query.=") or ((subtitle like '$key[0]%' or subtitle like '% $key[0]%' or subtitle like '% $key[0]')";
395             for ($i=1;$i<$count;$i++){
396               $query.=" and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%' or subtitle like '% $key[$i]')";
397             }
398             $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%' or seriestitle like '% $key[0]')";
399             for ($i=1;$i<$count;$i++){
400               $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
401             }
402             $query.=") or ((unititle like '$key[0]%' or unititle like '% $key[0]%' or unititle like '% $key[0]')";
403             for ($i=1;$i<$count;$i++){
404               $query.=" and (unititle like '$key[$i]%' or unititle like '% $key[$i]%')";
405             }
406             $query=$query."))";
407            }
408           } elsif ($search->{'class'} ne ''){
409              $query="select * from biblioitems,biblio where biblio.biblionumber=biblioitems.biblionumber";
410              my @temp=split(/\|/,$search->{'class'});
411               my $count=@temp;
412               $query.= " and ( itemtype='$temp[0]'";
413               for (my $i=1;$i<$count;$i++){
414                $query.=" or itemtype='$temp[$i]'";
415               }
416               $query.=")";
417               if ($search->{'illustrator'} ne ''){
418                 $query.=" and illus like '%".$search->{'illustrator'}."%' ";
419               }
420               if ($search->{'dewey'} ne ''){
421                 $query.=" and biblioitems.dewey like '$search->{'dewey'}%'";
422               }
423           } elsif ($search->{'dewey'} ne ''){
424              $query="select * from biblioitems,biblio 
425              where biblio.biblionumber=biblioitems.biblionumber
426              and biblioitems.dewey like '$search->{'dewey'}%'";
427           } elsif ($search->{'illustrator'} ne '') {
428           if ($search->{'illustrator'} ne ''){
429              $query="select * from biblioitems,biblio 
430              where biblio.biblionumber=biblioitems.biblionumber
431              and biblioitems.illus like '%".$search->{'illustrator'}."%'";
432           }
433         }
434           $query .=" group by biblio.biblionumber";      
435       }
436   } 
437   if ($type eq 'subject'){
438     my @key=split(' ',$search->{'subject'});
439     my $count=@key;
440     my $i=1;
441     $query="select distinct(subject) from bibliosubject where( subject like
442     '$key[0]%' or subject like '% $key[0]%' or subject like '% $key[0]' or subject like '%($key[0])%')";
443     while ($i<$count){
444       $query.=" and (subject like '$key[$i]%' or subject like '% $key[$i]%'
445       or subject like '% $key[$i]'
446       or subject like '%($key[$i])%')";
447       $i++;
448     }
449     if ($search->{'subject'} eq 'NZ' || $search->{'subject'} eq 'nz'){ 
450       $query.= " or (subject like 'NEW ZEALAND %' or subject like '% NEW ZEALAND %'
451       or subject like '% NEW ZEALAND' or subject like '%(NEW ZEALAND)%' ) ";
452     } elsif ( $search->{'subject'} =~ /^nz /i || $search->{'subject'} =~ / nz /i || $search->{'subject'} =~ / nz$/i){
453       $query=~ s/ nz/ NEW ZEALAND/ig;
454       $query=~ s/nz /NEW ZEALAND /ig;
455       $query=~ s/\(nz\)/\(NEW ZEALAND\)/gi;
456     }  
457   }
458   if ($type eq 'precise'){
459       $query="select * from items,biblio ";
460       if ($search->{'item'} ne ''){
461         my $search2=uc $search->{'item'};
462         $query=$query." where 
463         items.biblionumber=biblio.biblionumber 
464         and barcode='$search2'";
465       }
466       if ($search->{'isbn'} ne ''){
467         my $search2=uc $search->{'isbn'};
468         my $query1 = "select * from biblioitems where isbn='$search2'";
469         my $sth1=$dbh->prepare($query1);
470 #       print $query1;
471         $sth1->execute;
472         my $i2=0;
473         while (my $data=$sth1->fetchrow_hashref) {
474            $query="select * from biblioitems,biblio where
475            biblio.biblionumber = $data->{'biblionumber'}
476            and biblioitems.biblionumber = biblio.biblionumber";
477            my $sth=$dbh->prepare($query);
478            $sth->execute;
479            my $data=$sth->fetchrow_hashref;
480            my ($dewey, $subclass) = ($data->{'dewey'}, $data->{'subclass'});
481            $dewey=~s/\.*0*$//;
482            ($dewey == 0) && ($dewey='');
483            ($dewey) && ($dewey.=" $subclass");
484            $data->{'dewey'}=$dewey;
485            $results[$i2]=$data;
486 #           $results[$i2]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'isbn'}\t$data->{'itemtype'}";
487            $i2++; 
488            $sth->finish;
489         }
490         $sth1->finish;
491       }
492   }
493 #print $query;
494 if ($type ne 'precise' && $type ne 'subject'){
495   if ($search->{'author'} ne ''){   
496       $query=$query." order by biblio.author,title";
497   } else {
498       $query=$query." order by title";
499   }
500 } else {
501   if ($type eq 'subject'){
502       $query=$query." order by subject";
503   }
504 }
505 #print $query;
506 my $sth=$dbh->prepare($query);
507 $sth->execute;
508 my $count=1;
509 my $i=0;
510 my $limit= $num+$offset;
511 while (my $data=$sth->fetchrow_hashref){
512   my $query="select dewey,subclass from biblioitems where biblionumber=$data->{'biblionumber'}";
513             if ($search->{'class'} ne ''){
514               my @temp=split(/\|/,$search->{'class'});
515               my $count=@temp;
516               $query.= " and ( itemtype='$temp[0]'";
517               for (my $i=1;$i<$count;$i++){
518                $query.=" or itemtype='$temp[$i]'";
519               }
520               $query.=")";
521             }
522             if ($search->{'dewey'} ne ''){
523               $query.=" and dewey='$search->{'dewey'}' ";
524             }
525             if ($search->{'illustrator'} ne ''){
526               $query.=" and illus like '%".$search->{'illustrator'}."%' ";
527             }
528 #print $query;
529   my $sti=$dbh->prepare($query);
530   $sti->execute;
531   my $dewey;
532   my $subclass;
533   my $true=0;
534   if (($dewey, $subclass) = $sti->fetchrow || $type eq 'subject'){
535     $true=1;
536   }
537   $dewey=~s/\.*0*$//;
538   ($dewey == 0) && ($dewey='');
539   ($dewey) && ($dewey.=" $subclass");
540   $data->{'dewey'}=$dewey;
541   $sti->finish;
542   if ($true == 1){
543   if ($count > $offset && $count <= $limit){
544 #    if ($type ne 'subject' && $type ne 'precise'){
545 #       $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'illus'}";
546 #    } elsif ($search->{'isbn'} ne '' || $search->{'item'} ne ''){
547 #       $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'illus'}";
548 #    } else {  
549 #     $results[$i]="$data->{'author'}\t$data->{'subject'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'illus'}";
550 #    }
551     $results[$i]=$data;
552     $i++;
553   }
554   $count++;
555   }
556 }
557 $sth->finish;
558 #if ($type ne 'precise'){
559   $count--;
560 #}
561 #$count--;
562 return($count,@results);
563 }
564
565 sub updatesearchstats{
566   my ($dbh,$query)=@_;
567   
568 }
569
570 sub subsearch {
571   my ($env,$subject)=@_;
572   my $dbh=C4Connect();
573   $subject=$dbh->quote($subject);
574   my $query="Select * from biblio,bibliosubject where
575   biblio.biblionumber=bibliosubject.biblionumber and
576   bibliosubject.subject=$subject group by biblio.biblionumber
577   order by biblio.title";
578   my $sth=$dbh->prepare($query);
579   $sth->execute;
580   my $i=0;
581 #  print $query;
582   my @results;
583   while (my $data=$sth->fetchrow_hashref){
584     $results[$i]="$data->{'title'}\t$data->{'author'}\t$data->{'biblionumber'}";
585     $i++;
586   }
587   $sth->finish;
588   $dbh->disconnect;
589   return(@results);
590 }
591
592
593 sub ItemInfo {
594     my ($env,$biblionumber,$type) = @_;
595     my $dbh   = &C4Connect;
596     my $query = "Select * from items, biblio, biblioitems, branches
597 where (items.biblioitemnumber = biblioitems.biblioitemnumber)
598 and biblioitems.biblionumber = biblio.biblionumber
599 and biblio.biblionumber = '$biblionumber'
600 and branches.branchcode = items.holdingbranch";
601
602   if ($type ne 'intra'){
603     $query .= " and ((items.itemlost<>1 and items.itemlost <> 2)
604     or items.itemlost is NULL)
605     and (wthdrawn <> 1 or wthdrawn is NULL)";
606   }
607   $query .= " order by items.dateaccessioned desc";
608   my $sth=$dbh->prepare($query);
609   $sth->execute;
610   my $i=0;
611   my @results;
612 #  print $query;
613   while (my $data=$sth->fetchrow_hashref){
614     my $iquery = "Select * from issues
615     where itemnumber = '$data->{'itemnumber'}'
616     and returndate is null";
617     my $datedue = '';
618     my $isth=$dbh->prepare($iquery);
619     $isth->execute;
620     if (my $idata=$isth->fetchrow_hashref){
621       my @temp=split('-',$idata->{'date_due'});
622       $datedue = "$temp[2]/$temp[1]/$temp[0]";
623     }
624     if ($data->{'itemlost'} eq '1' || $data->{'itemlost'} eq '2'){
625         $datedue='Itemlost';
626     }
627     if ($data->{'wthdrawn'} eq '1'){
628       $datedue="Cancelled";
629     }
630     if ($datedue eq ''){
631        my ($rescount,$reserves)=Findgroupreserve($data->{'biblioitemnumber'},$biblionumber);
632
633        if ($rescount >0){                                
634           $datedue='Request';
635        }
636     }
637     $isth->finish;
638     my $class = $data->{'classification'};
639     my $dewey = $data->{'dewey'};
640     $dewey =~ s/0+$//;
641     if ($dewey eq "000.") { $dewey = "";};    
642     if ($dewey < 10){$dewey='00'.$dewey;}
643     if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
644     if ($dewey <= 0){
645       $dewey='';
646     }
647     $dewey=~ s/\.$//;
648     $class = $class.$dewey;
649     if ($dewey ne ''){
650       $class = $class.$data->{'subclass'};
651     }
652  #   $results[$i]="$data->{'title'}\t$data->{'barcode'}\t$datedue\t$data->{'branchname'}\t$data->{'dewey'}";
653     my @temp=split('-',$data->{'datelastseen'});
654     my $date="$temp[2]/$temp[1]/$temp[0]";
655     $results[$i]="$data->{'title'}\t$data->{'barcode'}\t$datedue\t$data->{'branchname'}\t$class\t$data->{'itemnumber'}\t$data->{'itemtype'}\t$date\t$data->{'biblioitemnumber'}\t$data->{'volumeddesc'}";
656 #    print "$results[$i] <br>";
657     $i++;
658   }
659  $sth->finish;
660   my $query2="Select * from aqorders where biblionumber=$biblionumber";
661   my $sth2=$dbh->prepare($query2);         
662   $sth2->execute;                                        
663   my $data;
664   my $ocount;
665   if ($data=$sth2->fetchrow_hashref){                   
666     $ocount=$data->{'quantity'} - $data->{'quantityreceived'};                                                  
667     if ($ocount > 0){
668       $results[$i]="$data->{'title'}\t$data->{'barcode'}\t$ocount\tOn Order\t\t$data->{'itemnumber'}\t$data->{'itemtype'}\t\t$data->{'biblioitemnumber'}\t$data->{'volumeddesc'}";
669     }
670   } 
671   $sth2->finish;
672
673   $dbh->disconnect;
674   return(@results);
675 }
676
677 sub GetItems {
678    my ($env,$biblionumber)=@_;
679    #debug_msg($env,"GetItems");
680    my $dbh = &C4Connect;
681    my $query = "Select * from biblioitems where (biblionumber = $biblionumber)";
682    #debug_msg($env,$query);
683    my $sth=$dbh->prepare($query);
684    $sth->execute;
685    #debug_msg($env,"executed query");      
686    my $i=0;
687    my @results;
688    while (my $data=$sth->fetchrow_hashref) {
689       #debug_msg($env,$data->{'biblioitemnumber'});
690       my $dewey = $data->{'dewey'};
691       $dewey =~ s/0+$//; 
692       my $line = $data->{'biblioitemnumber'}."\t".$data->{'itemtype'};
693       $line = $line."\t$data->{'classification'}\t$dewey";
694       $line = $line."\t$data->{'subclass'}\t$data->{isbn}";
695       $line = $line."\t$data->{'volume'}\t$data->{number}";
696       my $isth= $dbh->prepare("select * from items where biblioitemnumber = $data->{'biblioitemnumber'}");
697       $isth->execute;
698       while (my $idata = $isth->fetchrow_hashref) {
699         my $iline = $idata->{'barcode'}."[".$idata->{'holdingbranch'}."[";
700         if ($idata->{'notforloan'} == 1) {
701           $iline = $iline."NFL ";
702         }
703         if ($idata->{'itemlost'} == 1) {
704           $iline = $iline."LOST ";
705         }        
706         $line = $line."\t$iline"; 
707       }
708       $isth->finish;
709       $results[$i] = $line;
710       $i++;      
711    }
712    $sth->finish;
713    $dbh->disconnect;
714    return(@results);
715 }            
716   
717 sub itemdata {
718   my ($barcode)=@_;
719   my $dbh=C4Connect;
720   my $query="Select * from items,biblioitems where barcode='$barcode'
721   and items.biblioitemnumber=biblioitems.biblioitemnumber";
722 #  print $query;
723   my $sth=$dbh->prepare($query);
724   $sth->execute;
725   my $data=$sth->fetchrow_hashref;
726   $sth->finish;
727   $dbh->disconnect;
728   return($data);
729 }
730
731
732 sub bibdata {
733     my ($bibnum, $type) = @_;
734     my $dbh   = C4Connect;
735     my $query = "Select *, biblio.notes  
736 from biblio, biblioitems 
737 left join bibliosubtitle on
738 biblio.biblionumber = bibliosubtitle.biblionumber
739 where biblio.biblionumber = $bibnum
740 and biblioitems.biblionumber = $bibnum";
741     my $sth   = $dbh->prepare($query);
742     my $data;
743
744     $sth->execute;
745     $data  = $sth->fetchrow_hashref;
746     $sth->finish;
747
748     $query = "Select * from bibliosubject where biblionumber = '$bibnum'";
749     $sth   = $dbh->prepare($query);
750     $sth->execute;
751     while (my $dat = $sth->fetchrow_hashref){
752         $data->{'subject'} .= " | $dat->{'subject'}";
753     } # while
754
755     $sth->finish;
756     $dbh->disconnect;
757     return($data);
758 } # sub bibdata
759
760
761 sub bibitemdata {
762     my ($bibitem) = @_;
763     my $dbh   = C4Connect;
764     my $query = "Select *,biblioitems.notes as bnotes from biblio, biblioitems,itemtypes
765 where biblio.biblionumber = biblioitems.biblionumber
766 and biblioitemnumber = $bibitem
767 and biblioitems.itemtype = itemtypes.itemtype";
768     my $sth   = $dbh->prepare($query);
769     my $data;
770
771     $sth->execute;
772
773     $data = $sth->fetchrow_hashref;
774
775     $sth->finish;
776     $dbh->disconnect;
777     return($data);
778 } # sub bibitemdata
779
780
781 sub subject {
782   my ($bibnum)=@_;
783   my $dbh=C4Connect;
784   my $query="Select * from bibliosubject where biblionumber=$bibnum";
785   my $sth=$dbh->prepare($query);
786   $sth->execute;
787   my @results;
788   my $i=0;
789   while (my $data=$sth->fetchrow_hashref){
790     $results[$i]=$data;
791     $i++;
792   }
793   $sth->finish;
794   $dbh->disconnect;
795   return($i,\@results);
796 }
797
798 sub addauthor {
799   my ($bibnum)=@_;
800   my $dbh=C4Connect;
801   my $query="Select * from additionalauthors where biblionumber=$bibnum";
802   my $sth=$dbh->prepare($query);
803   $sth->execute;
804   my @results;
805   my $i=0;
806   while (my $data=$sth->fetchrow_hashref){
807     $results[$i]=$data;
808     $i++;
809   }
810   $sth->finish;
811   $dbh->disconnect;
812   return($i,\@results);
813 }
814
815 sub subtitle {
816   my ($bibnum)=@_;
817   my $dbh=C4Connect;
818   my $query="Select * from bibliosubtitle where biblionumber=$bibnum";
819   my $sth=$dbh->prepare($query);
820   $sth->execute;
821   my @results;
822   my $i=0;
823   while (my $data=$sth->fetchrow_hashref){
824     $results[$i]=$data;
825     $i++;
826   }
827   $sth->finish;
828   $dbh->disconnect;
829   return($i,\@results);
830 }
831
832
833
834 sub itemissues {
835     my ($bibitem, $biblio)=@_;
836     my $dbh   = C4Connect;
837     my $query = "Select * from items where 
838 items.biblioitemnumber = '$bibitem'";
839     my $sth   = $dbh->prepare($query)
840       || die $dbh->errstr;
841     my $i     = 0;
842     my @results;
843   
844     $sth->execute
845       || die $sth->errstr;
846
847     while (my $data = $sth->fetchrow_hashref) {
848         my $query2 = "select * from issues,borrowers
849 where itemnumber = $data->{'itemnumber'}
850 and returndate is NULL
851 and issues.borrowernumber = borrowers.borrowernumber";
852         my $sth2   = $dbh->prepare($query2);
853
854         $sth2->execute; 
855         if (my $data2 = $sth2->fetchrow_hashref) {
856             $data->{'date_due'} = $data2->{'date_due'};
857             $data->{'card'}     = $data2->{'cardnumber'};
858         } else {
859             if ($data->{'wthdrawn'} eq '1') {
860                 $data->{'date_due'} = 'Cancelled';
861             } else {
862                 $data->{'date_due'} = 'Available';
863             } # else
864         } # else
865
866         $sth2->finish;
867         $query2 = "select * from issues, borrowers
868 where itemnumber = '$data->{'itemnumber'}'
869 and issues.borrowernumber = borrowers.borrowernumber 
870 order by date_due desc";
871         $sth2 = $dbh->prepare($query2)
872           || die $dbh->errstr;
873         $sth2->execute
874           || die $sth2->errstr;
875
876         for (my $i2 = 0; $i2 < 2; $i2++) {
877             if (my $data2 = $sth2->fetchrow_hashref) {
878                 $data->{"timestamp$i2"} = $data2->{'timestamp'};
879                 $data->{"card$i2"}      = $data2->{'cardnumber'};
880                 $data->{"borrower$i2"}  = $data2->{'borrowernumber'};
881             } # if
882         } # for
883
884         $sth2->finish;
885         $results[$i] = $data;
886         $i++;
887     }
888
889     $sth->finish;
890     $dbh->disconnect;
891     return(@results);
892 }
893
894
895 sub itemnodata {
896   my ($env,$dbh,$itemnumber) = @_;
897   $dbh=C4Connect;
898   my $query="Select * from biblio,items,biblioitems
899     where items.itemnumber = '$itemnumber'
900     and biblio.biblionumber = items.biblionumber
901     and biblioitems.biblioitemnumber = items.biblioitemnumber";
902   my $sth=$dbh->prepare($query);
903 #  print $query;
904   $sth->execute;
905   my $data=$sth->fetchrow_hashref;
906   $sth->finish;  
907   $dbh->disconnect;
908   return($data);               
909 }
910
911 #used by member enquiries from the intranet
912 #called by member.pl
913 sub BornameSearch  {
914   my ($env,$searchstring,$type)=@_;
915   my $dbh = &C4Connect;
916   $searchstring=~ s/\'/\\\'/g;
917   my @data=split(' ',$searchstring);
918   my $count=@data;
919   my $query="Select * from borrowers 
920   where ((surname like \"$data[0]%\" or surname like \"% $data[0]%\" 
921   or firstname  like \"$data[0]%\" or firstname like \"% $data[0]%\" 
922   or othernames like \"$data[0]%\" or othernames like \"% $data[0]%\")
923   ";
924   for (my $i=1;$i<$count;$i++){
925     $query=$query." and (surname like \"$data[$i]%\" or surname like \"% $data[$i]%\"                  
926     or firstname  like \"$data[$i]%\" or firstname like \"% $data[$i]%\"                    
927     or othernames like \"$data[$i]%\" or othernames like \"% $data[$i]%\")";
928   }
929   $query=$query.") or cardnumber = \"$searchstring\"
930   order by surname,firstname";
931 #  print $query,"\n";
932   my $sth=$dbh->prepare($query);
933   $sth->execute;
934   my @results;
935   my $cnt=0;
936   while (my $data=$sth->fetchrow_hashref){
937     push(@results,$data);
938     $cnt ++;
939   }
940 #  $sth->execute;
941   $sth->finish;
942   $dbh->disconnect;
943   return ($cnt,\@results);
944 }
945
946 sub borrdata {
947   my ($cardnumber,$bornum)=@_;
948   $cardnumber = uc $cardnumber;
949   my $dbh=C4Connect;
950   my $query;
951   if ($bornum eq ''){
952     $query="Select * from borrowers where cardnumber='$cardnumber'";
953   } else {
954       $query="Select * from borrowers where borrowernumber='$bornum'";
955   }
956   #print $query;
957   my $sth=$dbh->prepare($query);
958   $sth->execute;
959   my $data=$sth->fetchrow_hashref;
960   $sth->finish;
961   $dbh->disconnect;
962   return($data);
963 }
964
965 sub borrissues {
966   my ($bornum)=@_;
967   my $dbh=C4Connect;
968   my $query;
969   $query="Select * from issues,biblio,items where borrowernumber='$bornum' and
970 items.itemnumber=issues.itemnumber and
971 items.biblionumber=biblio.biblionumber and issues.returndate is NULL order
972 by date_due";
973   #print $query;
974   my $sth=$dbh->prepare($query);
975     $sth->execute;
976   my @result;
977   my $i=0;
978   while (my $data=$sth->fetchrow_hashref){
979     $result[$i]=$data;;
980     $i++;
981   }
982   $sth->finish;
983   $dbh->disconnect;
984   return($i,\@result);
985 }
986
987 sub allissues { 
988   my ($bornum,$order,$limit)=@_; 
989   my $dbh=C4Connect;   
990   my $query;     
991   $query="Select * from issues,biblio,items,biblioitems       
992   where borrowernumber='$bornum' and         
993   items.biblioitemnumber=biblioitems.biblioitemnumber and           
994   items.itemnumber=issues.itemnumber and             
995   items.biblionumber=biblio.biblionumber";               
996   $query.=" order by $order";                 
997   if ($limit !=0){                   
998     $query.=" limit $limit";                     
999   }                         
1000   #print $query;                           
1001   my $sth=$dbh->prepare($query);          
1002   $sth->execute;
1003   my @result;   
1004   my $i=0;    
1005   while (my $data=$sth->fetchrow_hashref){                                      
1006     $result[$i]=$data;; 
1007     $i++;     
1008   }         
1009   $sth->finish;           
1010   $dbh->disconnect;             
1011   return($i,\@result);               
1012 }
1013
1014 sub borrdata2 {
1015   my ($env,$bornum)=@_;
1016   my $dbh=C4Connect;
1017   my $query="Select count(*) from issues where borrowernumber='$bornum' and
1018     returndate is NULL";
1019     # print $query;
1020   my $sth=$dbh->prepare($query);
1021   $sth->execute;
1022   my $data=$sth->fetchrow_hashref;
1023   $sth->finish;
1024   $sth=$dbh->prepare("Select count(*) from issues where
1025     borrowernumber='$bornum' and date_due < now() and returndate is NULL");
1026   $sth->execute;
1027   my $data2=$sth->fetchrow_hashref;
1028   $sth->finish;
1029   $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
1030     borrowernumber='$bornum'");
1031   $sth->execute;
1032   my $data3=$sth->fetchrow_hashref;
1033   $sth->finish;
1034   $dbh->disconnect;
1035
1036 return($data2->{'count(*)'},$data->{'count(*)'},$data3->{'sum(amountoutstanding)'});
1037 }
1038         
1039
1040 sub getboracctrecord {
1041    my ($env,$params) = @_;
1042    my $dbh=C4Connect;
1043    my @acctlines;
1044    my $numlines=0;
1045    my $query= "Select * from accountlines where
1046 borrowernumber=$params->{'borrowernumber'} order by date desc,timestamp desc";
1047    my $sth=$dbh->prepare($query);
1048 #   print $query;
1049    $sth->execute;
1050    my $total=0;
1051    while (my $data=$sth->fetchrow_hashref){
1052 #      if ($data->{'itemnumber'} ne ''){
1053 #        $query="Select * from items,biblio where items.itemnumber=
1054 #       '$data->{'itemnumber'}' and biblio.biblionumber=items.biblionumber";
1055 #       my $sth2=$dbh->prepare($query);
1056 #       $sth2->execute;
1057 #       my $data2=$sth2->fetchrow_hashref;
1058 #       $sth2->finish;
1059 #       $data=$data2;
1060  #     }
1061       $acctlines[$numlines] = $data;
1062       $numlines++;
1063       $total = $total+ $data->{'amountoutstanding'};
1064    }
1065    $sth->finish;
1066    $dbh->disconnect;
1067    return ($numlines,\@acctlines,$total);
1068 }
1069
1070 sub itemcount { 
1071   my ($env,$bibnum,$type)=@_; 
1072   my $dbh=C4Connect;   
1073   my $query="Select * from items where     
1074   biblionumber=$bibnum ";
1075   if ($type ne 'intra'){
1076     $query.=" and ((itemlost <>1 and itemlost <> 2) or itemlost is NULL) and
1077     (wthdrawn <> 1 or wthdrawn is NULL)";      
1078   }
1079   my $sth=$dbh->prepare($query);         
1080   #  print $query;           
1081   $sth->execute;           
1082   my $count=0;             
1083   my $lcount=0;               
1084   my $nacount=0;                 
1085   my $fcount=0;
1086   my $scount=0;
1087   my $lostcount=0;
1088   my $mending=0;
1089   my $transit=0;
1090   my $ocount=0;
1091   while (my $data=$sth->fetchrow_hashref){
1092     $count++;                     
1093     my $query2="select * from issues,items where issues.itemnumber=                          
1094     '$data->{'itemnumber'}' and returndate is NULL
1095     and items.itemnumber=issues.itemnumber and ((items.itemlost <>1 and
1096     items.itemlost <> 2) or items.itemlost is NULL) 
1097     and (wthdrawn <> 1 or wthdrawn is NULL)"; 
1098     
1099     my $sth2=$dbh->prepare($query2);     
1100     $sth2->execute;         
1101     if (my $data2=$sth2->fetchrow_hashref){         
1102        $nacount++;         
1103     } else {         
1104       if ($data->{'holdingbranch'} eq 'C'){         
1105         $lcount++;               
1106       }                       
1107       if ($data->{'holdingbranch'} eq 'F' || $data->{'holdingbranch'} eq 'FP'){         
1108         $fcount++;               
1109       }                       
1110       if ($data->{'holdingbranch'} eq 'S' || $data->{'holdingbranch'} eq 'SP'){         
1111         $scount++;               
1112       }                       
1113       if ($data->{'itemlost'} eq '1'){
1114         $lostcount++;
1115       }
1116       if ($data->{'itemlost'} eq '2'){
1117         $lostcount++;
1118       }
1119       if ($data->{'holdingbranch'} eq 'FM'){
1120         $mending++;
1121       }
1122       if ($data->{'holdingbranch'} eq 'TR'){
1123         $transit++;
1124       }
1125     }                             
1126     $sth2->finish;     
1127   } 
1128 #  if ($count == 0){
1129     my $query2="Select * from aqorders where biblionumber=$bibnum";
1130     my $sth2=$dbh->prepare($query2);
1131     $sth2->execute;
1132     if (my $data=$sth2->fetchrow_hashref){
1133       $ocount=$data->{'quantity'} - $data->{'quantityreceived'};
1134     }
1135 #    $count+=$ocount;
1136     $sth2->finish;
1137   $sth->finish; 
1138   $dbh->disconnect;                   
1139   return ($count,$lcount,$nacount,$fcount,$scount,$lostcount,$mending,$transit,$ocount); 
1140 }
1141
1142 sub ItemType {
1143   my ($type)=@_;
1144   my $dbh=C4Connect;
1145   my $query="select description from itemtypes where itemtype='$type'";
1146   my $sth=$dbh->prepare($query);
1147   $sth->execute;
1148   my $dat=$sth->fetchrow_hashref;
1149   $sth->finish;
1150   $dbh->disconnect;
1151   return ($dat->{'description'});
1152 }
1153
1154
1155 sub bibitems {
1156     my ($bibnum) = @_;
1157     my $dbh   = C4Connect;
1158     my $query = "Select * from biblioitems, itemtypes, items
1159 where biblioitems.biblionumber = '$bibnum'
1160 and biblioitems.itemtype = itemtypes.itemtype
1161 and biblioitems.biblioitemnumber = items.biblioitemnumber
1162 group by items.biblioitemnumber";
1163     my $sth   = $dbh->prepare($query);
1164     my $count = 0;
1165     my @results;
1166
1167     $sth->execute;
1168
1169     while (my $data = $sth->fetchrow_hashref) {
1170         $results[$count] = $data;
1171         $count++;
1172     } # while
1173     
1174     $sth->finish;
1175     $dbh->disconnect;
1176     return($count, @results);
1177 } # sub bibitems
1178
1179
1180 sub barcodes{
1181   #called from request.pl
1182   my ($biblioitemnumber)=@_;
1183   my $dbh=C4Connect;
1184   my $query="Select barcode from items where
1185    biblioitemnumber='$biblioitemnumber'
1186    and ((itemlost <> 1 and itemlost <> 2) or itemlost is NULL) and
1187    (wthdrawn <> 1 or wthdrawn is NULL)";
1188
1189   my $sth=$dbh->prepare($query);
1190   $sth->execute;
1191   my @barcodes;
1192   my $i=0;
1193   while (my $data=$sth->fetchrow_hashref){
1194     $barcodes[$i]=$data->{'barcode'};
1195     $i++;
1196   }
1197   $sth->finish;
1198   $dbh->disconnect;
1199   return(@barcodes);
1200   
1201 }
1202
1203
1204 sub getwebsites {
1205     my ($biblionumber) = @_;
1206     my $dbh   = C4Connect;
1207     my $query = "Select * from websites where biblionumber = $biblionumber";
1208     my $sth   = $dbh->prepare($query);
1209     my $count = 0;
1210     my @results;
1211
1212     $sth->execute;
1213     while (my $data = $sth->fetchrow_hashref) {
1214         $results[$count] = $data;
1215         $count++;
1216     } # while
1217
1218     $sth->finish;
1219     $dbh->disconnect;
1220     return($count, @results);
1221 } # sub getwebsites
1222
1223
1224 sub getwebbiblioitems {
1225     my ($biblionumber) = @_;
1226     my $dbh   = C4Connect;
1227     my $query = "Select * from biblioitems where biblionumber = $biblionumber
1228 and itemtype = 'WEB'";
1229     my $sth   = $dbh->prepare($query);
1230     my $count = 0;
1231     my @results;
1232     
1233     $sth->execute;
1234     while (my $data = $sth->fetchrow_hashref) {
1235         $results[$count] = $data;
1236         $count++;
1237     } # while
1238     
1239     $sth->finish;
1240     $dbh->disconnect;
1241     return($count, @results);
1242 } # sub getwebbiblioitems
1243
1244
1245 END { }       # module clean-up code here (global destructor)
1246
1247 =head1 NAME
1248
1249 C4::Search - Module that provides Catalog searching for Koha
1250
1251 =head1 SYNOPSIS
1252
1253   use C4::Search;
1254   my ($count,@results)=KeywordSearch($env,$type,$search,$num,$offset);
1255   my ($count,@results)=CatSearch($env,$type,$search,$num,$offset);
1256
1257 =head1 DESCRIPTION
1258
1259 This module provides the searching facilities for the Catalog.
1260 Here I should go through and document each function thats exported and what it does. But I havent yet.
1261
1262 =head2 EXPORT
1263
1264 KeywordSearch
1265 CatSearch
1266 ItemInfo
1267
1268 =head1 AUTHOR
1269
1270 Koha Developement team <info@koha.org>
1271
1272 =head1 SEE ALSO
1273
1274 L<perl>.
1275
1276 =cut