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