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