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