Adding the facility to search on just dewey and class
[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               if ($search->{'dewey'} ne ''){
489                 $query.=" and biblioitems.dewey like '$search->{'dewey'}%'";
490               }
491           } elsif ($search->{'dewey'} ne ''){
492              $query="select * from biblioitems,biblio 
493              where biblio.biblionumber=biblioitems.biblionumber
494              and biblioitems.dewey like '$search->{'dewey'}%'";
495           } elsif ($search->{'illustrator'} ne '') {
496           if ($search->{'illustrator'} ne ''){
497              $query="select * from biblioitems,biblio 
498              where biblio.biblionumber=biblioitems.biblionumber
499              and biblioitems.illus like '%".$search->{'illustrator'}."%'";
500           }
501           }
502           $query .=" group by biblio.biblionumber";      
503       }
504   } 
505   if ($type eq 'subject'){
506     my @key=split(' ',$search->{'subject'});
507     my $count=@key;
508     my $i=1;
509     $query="select distinct(subject) from bibliosubject where( subject like
510     '$key[0]%' or subject like '% $key[0]%' or subject like '% $key[0]' or subject like '%($key[0])%')";
511     while ($i<$count){
512       $query.=" and (subject like '$key[$i]%' or subject like '% $key[$i]%'
513       or subject like '% $key[$i]'
514       or subject like '%($key[$i])%')";
515       $i++;
516     }
517     if ($search->{'subject'} eq 'NZ' || $search->{'subject'} eq 'nz'){ 
518       $query.= " or (subject like 'NEW ZEALAND %' or subject like '% NEW ZEALAND %'
519       or subject like '% NEW ZEALAND' or subject like '%(NEW ZEALAND)%' ) ";
520     } elsif ( $search->{'subject'} =~ /^nz /i || $search->{'subject'} =~ / nz /i || $search->{'subject'} =~ / nz$/i){
521       $query=~ s/ nz/ NEW ZEALAND/ig;
522       $query=~ s/nz /NEW ZEALAND /ig;
523       $query=~ s/\(nz\)/\(NEW ZEALAND\)/gi;
524     }  
525   }
526   if ($type eq 'precise'){
527       $query="select * from items,biblio ";
528       if ($search->{'item'} ne ''){
529         my $search2=uc $search->{'item'};
530         $query=$query." where 
531         items.biblionumber=biblio.biblionumber 
532         and barcode='$search2'";
533       }
534       if ($search->{'isbn'} ne ''){
535         my $search2=uc $search->{'isbn'};
536         my $query1 = "select * from biblioitems where isbn='$search2'";
537         my $sth1=$dbh->prepare($query1);
538 #       print $query1;
539         $sth1->execute;
540         my $i2=0;
541         while (my $data=$sth1->fetchrow_hashref) {
542            $query="select * from biblioitems,biblio where
543            biblio.biblionumber = $data->{'biblionumber'}
544            and biblioitems.biblionumber = biblio.biblionumber";
545            my $sth=$dbh->prepare($query);
546            $sth->execute;
547            my $data=$sth->fetchrow_hashref;
548            my ($dewey, $subclass) = ($data->{'dewey'}, $data->{'subclass'});
549            $dewey=~s/\.*0*$//;
550            ($dewey == 0) && ($dewey='');
551            ($dewey) && ($dewey.=" $subclass");
552            $results[$i2]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'isbn'}\t$data->{'itemtype'}";
553            $i2++; 
554            $sth->finish;
555         }
556         $sth1->finish;
557       }
558   }
559 #print $query;
560 if ($type ne 'precise' && $type ne 'subject'){
561   if ($search->{'author'} ne ''){   
562       $query=$query." order by biblio.author,title";
563   } else {
564       $query=$query." order by title";
565   }
566 } else {
567   if ($type eq 'subject'){
568       $query=$query." order by subject";
569   }
570 }
571 my $sth=$dbh->prepare($query);
572 $sth->execute;
573 my $count=1;
574 my $i=0;
575 my $limit= $num+$offset;
576 while (my $data=$sth->fetchrow_hashref){
577   my $sti=$dbh->prepare("select dewey,subclass from biblioitems where biblionumber=$data->{'biblionumber'}");
578   $sti->execute;
579   my ($dewey, $subclass) = $sti->fetchrow;
580   $dewey=~s/\.*0*$//;
581   ($dewey == 0) && ($dewey='');
582   ($dewey) && ($dewey.=" $subclass");
583   $sti->finish;
584   if ($count > $offset && $count <= $limit){
585     if ($type ne 'subject' && $type ne 'precise'){
586        $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'illus'}";
587     } elsif ($search->{'isbn'} ne '' || $search->{'item'} ne ''){
588        $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'illus'}";
589     } else {  
590      $results[$i]="$data->{'author'}\t$data->{'subject'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'illus'}";
591     }
592     $i++;
593   }
594   $count++;
595 }
596 $sth->finish;
597 #if ($type ne 'precise'){
598   $count--;
599 #}
600 #$count--;
601 return($count,@results);
602 }
603
604 sub updatesearchstats{
605   my ($dbh,$query)=@_;
606   
607 }
608
609 sub subsearch {
610   my ($env,$subject)=@_;
611   my $dbh=C4Connect();
612   my $query="Select * from biblio,bibliosubject where
613   biblio.biblionumber=bibliosubject.biblionumber and
614   bibliosubject.subject='$subject' group by biblio.biblionumber
615   order by biblio.title";
616   my $sth=$dbh->prepare($query);
617   $sth->execute;
618   my $i=0;
619 #  print $query;
620   my @results;
621   while (my $data=$sth->fetchrow_hashref){
622     $results[$i]="$data->{'title'}\t$data->{'author'}\t$data->{'biblionumber'}";
623     $i++;
624   }
625   $sth->finish;
626   $dbh->disconnect;
627   return(@results);
628 }
629
630
631 sub ItemInfo {
632   my ($env,$biblionumber,$type)=@_;
633   my $dbh = &C4Connect;
634   my $query="Select * from items,biblio,biblioitems,branches 
635   where (items.biblioitemnumber = biblioitems.biblioitemnumber)
636   and biblioitems.biblionumber=biblio.biblionumber
637   and biblio.biblionumber='$biblionumber' and branches.branchcode=
638   items.holdingbranch ";
639 #  print $type;
640   if ($type ne 'intra'){
641     $query.=" and (items.itemlost<>1 or items.itemlost is NULL)
642     and (wthdrawn <> 1 or wthdrawn is NULL)";
643   }
644   $query=$query."order by items.dateaccessioned desc";
645   my $sth=$dbh->prepare($query);
646   $sth->execute;
647   my $i=0;
648   my @results;
649 #  print $query;
650   while (my $data=$sth->fetchrow_hashref){
651     my $iquery = "Select * from issues
652     where itemnumber = '$data->{'itemnumber'}'
653     and returndate is null";
654     my $datedue = '';
655     my $isth=$dbh->prepare($iquery);
656     $isth->execute;
657     if (my $idata=$isth->fetchrow_hashref){
658       my @temp=split('-',$idata->{'date_due'});
659       $datedue = "$temp[2]/$temp[1]/$temp[0]";
660     }
661     if ($data->{'itemlost'} eq '1'){
662         $datedue='Itemlost';
663     }
664     if ($data->{'wthdrawn'} eq '1'){
665       $datedue="Cancelled";
666     }
667     if ($datedue eq ''){
668        my ($rescount,$reserves)=FindReserves($biblionumber,'');   
669        if ($rescount >0){                                
670           $datedue='Request';
671        }
672     }
673     $isth->finish;
674     my $class = $data->{'classification'};
675     my $dewey = $data->{'dewey'};
676     $dewey =~ s/0+$//;
677     if ($dewey eq "000.") { $dewey = "";};    
678     if ($dewey < 10){$dewey='00'.$dewey;}
679     if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
680     if ($dewey <= 0){
681       $dewey='';
682     }
683     $dewey=~ s/\.$//;
684     $class = $class.$dewey;
685     $class = $class.$data->{'subclass'};
686  #   $results[$i]="$data->{'title'}\t$data->{'barcode'}\t$datedue\t$data->{'branchname'}\t$data->{'dewey'}";
687     my @temp=split('-',$data->{'datelastseen'});
688     my $date="$temp[2]/$temp[1]/$temp[0]";
689     $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'}";
690 #    print "$results[$i] <br>";
691     $i++;
692   }
693   $sth->finish;
694   $dbh->disconnect;
695   return(@results);
696 }
697
698 sub GetItems {
699    my ($env,$biblionumber)=@_;
700    #debug_msg($env,"GetItems");
701    my $dbh = &C4Connect;
702    my $query = "Select * from biblioitems where (biblionumber = $biblionumber)";
703    #debug_msg($env,$query);
704    my $sth=$dbh->prepare($query);
705    $sth->execute;
706    #debug_msg($env,"executed query");      
707    my $i=0;
708    my @results;
709    while (my $data=$sth->fetchrow_hashref) {
710       #debug_msg($env,$data->{'biblioitemnumber'});
711       my $dewey = $data->{'dewey'};
712       $dewey =~ s/0+$//; 
713       my $line = $data->{'biblioitemnumber'}."\t".$data->{'itemtype'};
714       $line = $line."\t$data->{'classification'}\t$dewey";
715       $line = $line."\t$data->{'subclass'}\t$data->{isbn}";
716       $line = $line."\t$data->{'volume'}\t$data->{number}";
717       my $isth= $dbh->prepare("select * from items where biblioitemnumber = $data->{'biblioitemnumber'}");
718       $isth->execute;
719       while (my $idata = $isth->fetchrow_hashref) {
720         my $iline = $idata->{'barcode'}."[".$idata->{'holdingbranch'}."[";
721         if ($idata->{'notforloan'} == 1) {
722           $iline = $iline."NFL ";
723         }
724         if ($idata->{'itemlost'} == 1) {
725           $iline = $iline."LOST ";
726         }        
727         $line = $line."\t$iline"; 
728       }
729       $isth->finish;
730       $results[$i] = $line;
731       $i++;      
732    }
733    $sth->finish;
734    $dbh->disconnect;
735    return(@results);
736 }            
737   
738 sub itemdata {
739   my ($barcode)=@_;
740   my $dbh=C4Connect;
741   my $query="Select * from items,biblioitems where barcode='$barcode'
742   and items.biblioitemnumber=biblioitems.biblioitemnumber";
743 #  print $query;
744   my $sth=$dbh->prepare($query);
745   $sth->execute;
746   my $data=$sth->fetchrow_hashref;
747   $sth->finish;
748   $dbh->disconnect;
749   return($data);
750 }
751
752 sub bibdata {
753   my ($bibnum,$type)=@_;
754   my $dbh=C4Connect;
755   my $query="Select *,biblio.notes from biblio,biblioitems,bibliosubtitle where biblio.biblionumber=$bibnum
756   and biblioitems.biblionumber=$bibnum and 
757 (bibliosubtitle.biblionumber=$bibnum)"; 
758 #  print $query;
759   my $sth=$dbh->prepare($query);
760   $sth->execute;
761   my $data=$sth->fetchrow_hashref;
762   $sth->finish;
763   $query="Select * from bibliosubject where biblionumber='$bibnum'";
764   $sth=$dbh->prepare($query);
765   $sth->execute;
766   while (my $dat=$sth->fetchrow_hashref){
767     $data->{'subject'}.=" | $dat->{'subject'}";
768
769   }
770   #print $query;
771   $sth->finish;
772   $dbh->disconnect;
773   return($data);
774 }
775
776 sub bibitemdata {
777   my ($bibitem)=@_;
778   my $dbh=C4Connect;
779   my $query="Select * from biblio,biblioitems,itemtypes where biblio.biblionumber=
780   biblioitems.biblionumber and biblioitemnumber=$bibitem and
781   biblioitems.itemtype=itemtypes.itemtype";
782 #  print $query;
783   my $sth=$dbh->prepare($query);
784   $sth->execute;
785   my $data=$sth->fetchrow_hashref;
786   $sth->finish;
787   $dbh->disconnect;
788   return($data);
789 }
790
791 sub subject {
792   my ($bibnum)=@_;
793   my $dbh=C4Connect;
794   my $query="Select * from bibliosubject where biblionumber=$bibnum";
795   my $sth=$dbh->prepare($query);
796   $sth->execute;
797   my @results;
798   my $i=0;
799   while (my $data=$sth->fetchrow_hashref){
800     $results[$i]=$data;
801     $i++;
802   }
803   $sth->finish;
804   $dbh->disconnect;
805   return($i,\@results);
806 }
807
808 sub addauthor {
809   my ($bibnum)=@_;
810   my $dbh=C4Connect;
811   my $query="Select * from additionalauthors where biblionumber=$bibnum";
812   my $sth=$dbh->prepare($query);
813   $sth->execute;
814   my @results;
815   my $i=0;
816   while (my $data=$sth->fetchrow_hashref){
817     $results[$i]=$data;
818     $i++;
819   }
820   $sth->finish;
821   $dbh->disconnect;
822   return($i,\@results);
823 }
824
825 sub subtitle {
826   my ($bibnum)=@_;
827   my $dbh=C4Connect;
828   my $query="Select * from bibliosubtitle where biblionumber=$bibnum";
829   my $sth=$dbh->prepare($query);
830   $sth->execute;
831   my @results;
832   my $i=0;
833   while (my $data=$sth->fetchrow_hashref){
834     $results[$i]=$data;
835     $i++;
836   }
837   $sth->finish;
838   $dbh->disconnect;
839   return($i,\@results);
840 }
841
842
843
844 sub itemissues {
845   my ($bibitem,$biblio)=@_;
846   my $dbh=C4Connect;
847   my $query="Select * from items where 
848   items.biblioitemnumber='$bibitem'";
849   my $sth=$dbh->prepare($query) || die $dbh->errstr;
850   $sth->execute || die $sth->errstr;
851   my $i=0;
852   my @results;
853   while (my $data=$sth->fetchrow_hashref) {
854     my $query2="select * from issues,borrowers where itemnumber=$data->{'itemnumber'}
855     and returndate is NULL and issues.borrowernumber=borrowers.borrowernumber";
856     my $sth2=$dbh->prepare($query2);
857     $sth2->execute;
858     if (my $data2=$sth2->fetchrow_hashref) {
859       $data->{'date_due'}=$data2->{'date_due'};
860       $data->{'card'}=$data2->{'cardnumber'};
861     } else {
862       if ($data->{'wthdrawn'} eq '1') {
863         $data->{'date_due'}='Cancelled';
864       } else {
865           $data->{'date_due'}='Available';
866       }
867     }
868     $sth2->finish;
869     $query2="select * from issues,borrowers where itemnumber='$data->{'itemnumber'}'
870     and issues.borrowernumber=borrowers.borrowernumber 
871     order by date_due desc";
872     my $sth2=$dbh->prepare($query2) || die $dbh->errstr;
873     $sth2->execute || die $sth2->errstr;
874     for (my $i2=0;$i2<2;$i2++){
875       if (my $data2=$sth2->fetchrow_hashref){
876         $data->{"timestamp$i2"}=$data2->{'timestamp'};
877         $data->{"card$i2"}=$data2->{'cardnumber'};
878         $data->{"borrower$i2"}=$data2->{'borrowernumber'};
879       }
880     }
881     $sth2->finish;
882     $results[$i]=$data;
883     $i++;
884   }
885   $sth->finish;
886   $dbh->disconnect;
887   return(@results);
888 }
889
890 sub itemnodata {
891   my ($env,$dbh,$itemnumber) = @_;
892   $dbh=C4Connect;
893   my $query="Select * from biblio,items,biblioitems
894     where items.itemnumber = '$itemnumber'
895     and biblio.biblionumber = items.biblionumber
896     and biblioitems.biblioitemnumber = items.biblioitemnumber";
897   my $sth=$dbh->prepare($query);
898 #  print $query;
899   $sth->execute;
900   my $data=$sth->fetchrow_hashref;
901   $sth->finish;  
902   $dbh->disconnect;
903   return($data);               
904 }
905
906 #used by member enquiries from the intranet
907 #called by member.pl
908 sub BornameSearch  {
909   my ($env,$searchstring,$type)=@_;
910   my $dbh = &C4Connect;
911   $searchstring=~ s/\'/\\\'/g;
912   my @data=split(' ',$searchstring);
913   my $count=@data;
914   my $query="Select * from borrowers 
915   where ((surname like \"$data[0]%\" or surname like \"% $data[0]%\" 
916   or firstname  like \"$data[0]%\" or firstname like \"% $data[0]%\" 
917   or othernames like \"$data[0]%\" or othernames like \"% $data[0]%\")
918   ";
919   for (my $i=1;$i<$count;$i++){
920     $query=$query." and (surname like \"$data[$i]%\" or surname like \"% $data[$i]%\"                  
921     or firstname  like \"$data[$i]%\" or firstname like \"% $data[$i]%\"                    
922     or othernames like \"$data[$i]%\" or othernames like \"% $data[$i]%\")";
923   }
924   $query=$query.") or cardnumber = \"$searchstring\"
925   order by surname,firstname";
926 #  print $query,"\n";
927   my $sth=$dbh->prepare($query);
928   $sth->execute;
929   my @results;
930   my $cnt=0;
931   while (my $data=$sth->fetchrow_hashref){
932     push(@results,$data);
933     $cnt ++;
934   }
935 #  $sth->execute;
936   $sth->finish;
937   $dbh->disconnect;
938   return ($cnt,\@results);
939 }
940
941 sub borrdata {
942   my ($cardnumber,$bornum)=@_;
943   $cardnumber = uc $cardnumber;
944   my $dbh=C4Connect;
945   my $query;
946   if ($bornum eq ''){
947     $query="Select * from borrowers where cardnumber='$cardnumber'";
948   } else {
949       $query="Select * from borrowers where borrowernumber='$bornum'";
950   }
951   #print $query;
952   my $sth=$dbh->prepare($query);
953   $sth->execute;
954   my $data=$sth->fetchrow_hashref;
955   $sth->finish;
956   $dbh->disconnect;
957   return($data);
958 }
959
960 sub borrissues {
961   my ($bornum)=@_;
962   my $dbh=C4Connect;
963   my $query;
964   $query="Select * from issues,biblio,items where borrowernumber='$bornum' and
965 items.itemnumber=issues.itemnumber and
966 items.biblionumber=biblio.biblionumber and issues.returndate is NULL order
967 by date_due";
968   #print $query;
969   my $sth=$dbh->prepare($query);
970     $sth->execute;
971   my @result;
972   my $i=0;
973   while (my $data=$sth->fetchrow_hashref){
974     $result[$i]=$data;;
975     $i++;
976   }
977   $sth->finish;
978   $dbh->disconnect;
979   return($i,\@result);
980 }
981
982 sub allissues {
983   my ($bornum)=@_;
984   my $dbh=C4Connect;
985   my $query;
986   $query="Select * from issues,biblio,items where borrowernumber='$bornum' and
987 items.itemnumber=issues.itemnumber and
988 items.biblionumber=biblio.biblionumber order
989 by date_due";
990   #print $query;
991   my $sth=$dbh->prepare($query);
992     $sth->execute;
993   my @result;
994   my $i=0;
995   while (my $data=$sth->fetchrow_hashref){
996     $result[$i]=$data;;
997     $i++;
998   }
999   $sth->finish;
1000   $dbh->disconnect;
1001   return($i,\@result);
1002 }
1003
1004
1005
1006 sub borrdata2 {
1007   my ($env,$bornum)=@_;
1008   my $dbh=C4Connect;
1009   my $query="Select count(*) from issues where borrowernumber='$bornum' and
1010     returndate is NULL";
1011     # print $query;
1012   my $sth=$dbh->prepare($query);
1013   $sth->execute;
1014   my $data=$sth->fetchrow_hashref;
1015   $sth->finish;
1016   $sth=$dbh->prepare("Select count(*) from issues where
1017     borrowernumber='$bornum' and date_due < now() and returndate is NULL");
1018   $sth->execute;
1019   my $data2=$sth->fetchrow_hashref;
1020   $sth->finish;
1021   $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
1022     borrowernumber='$bornum'");
1023   $sth->execute;
1024   my $data3=$sth->fetchrow_hashref;
1025   $sth->finish;
1026   $dbh->disconnect;
1027
1028 return($data2->{'count(*)'},$data->{'count(*)'},$data3->{'sum(amountoutstanding)'});
1029 }
1030                   
1031 sub getacctlist {
1032    my ($env,$params) = @_;
1033    my $dbh=C4Connect;
1034    my @acctlines;
1035    my $numlines;
1036    my $query = "Select borrowernumber, accountno, date, amount, description,
1037       dispute, accounttype, amountoutstanding, barcode, title
1038       from accountlines,items,biblio   
1039       where borrowernumber = $params->{'borrowernumber'} ";
1040    if ($params->{'acctno'} ne "") {
1041       my $query = $query." and accountlines.accountno = $params->{'acctno'} ";
1042       }
1043    my $query = $query." and accountlines.itemnumber = items.itemnumber
1044       and items.biblionumber = biblio.biblionumber
1045       and accountlines.amountoutstanding<>0 order by date";
1046    my $sth=$dbh->prepare($query);
1047 #   print $query;
1048    $sth->execute;
1049    my $total=0;
1050    while (my $data=$sth->fetchrow_hashref){
1051       $acctlines[$numlines] = $data;
1052       $numlines++;
1053       $total = $total+ $data->{'amountoutstanding'};
1054    }
1055    return ($numlines,\@acctlines,$total);
1056    $sth->finish;
1057    $dbh->disconnect;
1058 }
1059
1060 sub getboracctrecord {
1061    my ($env,$params) = @_;
1062    my $dbh=C4Connect;
1063    my @acctlines;
1064    my $numlines=0;
1065    my $query= "Select * from accountlines where
1066 borrowernumber=$params->{'borrowernumber'} order by date desc,timestamp desc";
1067    my $sth=$dbh->prepare($query);
1068 #   print $query;
1069    $sth->execute;
1070    my $total=0;
1071    while (my $data=$sth->fetchrow_hashref){
1072 #      if ($data->{'itemnumber'} ne ''){
1073 #        $query="Select * from items,biblio where items.itemnumber=
1074 #       '$data->{'itemnumber'}' and biblio.biblionumber=items.biblionumber";
1075 #       my $sth2=$dbh->prepare($query);
1076 #       $sth2->execute;
1077 #       my $data2=$sth2->fetchrow_hashref;
1078 #       $sth2->finish;
1079 #       $data=$data2;
1080  #     }
1081       $acctlines[$numlines] = $data;
1082       $numlines++;
1083       $total = $total+ $data->{'amountoutstanding'};
1084    }
1085    $sth->finish;
1086    $dbh->disconnect;
1087    return ($numlines,\@acctlines,$total);
1088 }
1089
1090 sub itemcount { 
1091   my ($env,$bibnum,$type)=@_; 
1092   my $dbh=C4Connect;   
1093   my $query="Select * from items where     
1094   biblionumber=$bibnum ";
1095   if ($type ne 'intra'){
1096     $query.=" and (itemlost <>1 or itemlost is NULL) and
1097     (wthdrawn <> 1 or wthdrawn is NULL)";      
1098   }
1099   my $sth=$dbh->prepare($query);         
1100   #  print $query;           
1101   $sth->execute;           
1102   my $count=0;             
1103   my $lcount=0;               
1104   my $nacount=0;                 
1105   my $fcount=0;
1106   my $scount=0;
1107   my $lostcount=0;
1108   my $mending=0;
1109   my $transit=0;
1110   my $ocount=0;
1111   while (my $data=$sth->fetchrow_hashref){
1112     $count++;                     
1113     my $query2="select * from issues,items where issues.itemnumber=                          
1114     '$data->{'itemnumber'}' and returndate is NULL
1115     and items.itemnumber=issues.itemnumber and (items.itemlost <>1 or
1116     items.itemlost is NULL)"; 
1117     my $sth2=$dbh->prepare($query2);     
1118     $sth2->execute;         
1119     if (my $data2=$sth2->fetchrow_hashref){         
1120        $nacount++;         
1121     } else {         
1122       if ($data->{'holdingbranch'} eq 'C'){         
1123         $lcount++;               
1124       }                       
1125       if ($data->{'holdingbranch'} eq 'F' || $data->{'holdingbranch'} eq 'FP'){         
1126         $fcount++;               
1127       }                       
1128       if ($data->{'holdingbranch'} eq 'S' || $data->{'holdingbranch'} eq 'SP'){         
1129         $scount++;               
1130       }                       
1131       if ($data->{'itemlost'} eq '1'){
1132         $lostcount++;
1133       }
1134       if ($data->{'holdingbranch'} eq 'FM'){
1135         $mending++;
1136       }
1137       if ($data->{'holdingbranch'} eq 'TR'){
1138         $transit++;
1139       }
1140     }                             
1141     $sth2->finish;     
1142   } 
1143 #  if ($count == 0){
1144     my $query2="Select * from aqorders where biblionumber=$bibnum";
1145     my $sth2=$dbh->prepare($query2);
1146     $sth2->execute;
1147     if (my $data=$sth2->fetchrow_hashref){
1148       $ocount=$data->{'quantity'} - $data->{'quantityreceived'};
1149     }
1150 #    $count+=$ocount;
1151     $sth2->finish;
1152   $sth->finish; 
1153   $dbh->disconnect;                   
1154   return ($count,$lcount,$nacount,$fcount,$scount,$lostcount,$mending,$transit,$ocount); 
1155 }
1156
1157 sub ItemType {
1158   my ($type)=@_;
1159   my $dbh=C4Connect;
1160   my $query="select description from itemtypes where itemtype='$type'";
1161   my $sth=$dbh->prepare($query);
1162   $sth->execute;
1163   my $dat=$sth->fetchrow_hashref;
1164   $sth->finish;
1165   $dbh->disconnect;
1166   return ($dat->{'description'});
1167 }
1168
1169 sub bibitems {
1170   my ($bibnum)=@_;
1171   my $dbh=C4Connect;
1172   my $query="Select * from biblioitems,itemtypes,items where
1173   biblioitems.biblionumber='$bibnum' and biblioitems.itemtype=itemtypes.itemtype and
1174   biblioitems.biblioitemnumber=items.biblioitemnumber group by
1175   items.biblioitemnumber";
1176   my $sth=$dbh->prepare($query);
1177   $sth->execute;
1178   my $i=0;
1179   my @results;
1180   while (my $data=$sth->fetchrow_hashref){
1181     $results[$i]=$data;
1182     $i++;
1183   }
1184   $sth->finish;
1185   $dbh->disconnect;
1186   return($i,@results);
1187 }
1188
1189 sub barcodes{
1190   my ($biblioitemnumber)=@_;
1191   my $dbh=C4Connect;
1192   my $query="Select barcode from items where
1193    biblioitemnumber='$biblioitemnumber'";
1194   my $sth=$dbh->prepare($query);
1195   $sth->execute;
1196   my @barcodes;
1197   my $i=0;
1198   while (my $data=$sth->fetchrow_hashref){
1199     $barcodes[$i]=$data->{'barcode'};
1200     $i++;
1201   }
1202   $sth->finish;
1203   $dbh->disconnect;
1204   return(@barcodes);
1205   
1206 }
1207 END { }       # module clean-up code here (global destructor)
1208
1209
1210
1211
1212
1213