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