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