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