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