Getting Iteminfo in Search to only display correct reserves (mainly used on
[koha.git] / C4 / Search.pm
1 package C4::Search; #asummes C4/Search
2
3 #requires DBI.pm to be installed
4 #uses DBD:Pg
5
6 use strict;
7 require Exporter;
8 use DBI;
9 use C4::Database;
10 use C4::Reserves2;
11 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 &getacctlist &itemnodata &itemcount
21 &OpacSearch &borrdata2 &NewBorrowerNumber &bibitemdata &borrissues
22 &getboracctrecord &ItemType &itemissues &FrontSearch &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 sub OpacSearch {
120   my ($env,$type,$search,$num,$offset)=@_;
121   my $dbh = &C4Connect;
122   $search->{'keyword'}=~ s/'/\\'/g;
123   my @key=split(' ',$search->{'keyword'});
124   my $count=@key;
125   my $i=1;
126   my @results;
127   my $query ="Select count(*) from biblio where 
128   ((title like '$key[0]%' or title like '% $key[0]%')";
129   while ($i < $count){
130     $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%')";
131     $i++;
132   }
133   $query=$query.") or ((author like '$key[0]%' or author like '% $key[0]%')";
134   $i=1;
135   while ($i < $count){
136     $query=$query." and (author like '$key[$i]%' or author like '% $key[$i]%')";
137     $i++;
138   }
139   $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%')";
140   for ($i=1;$i<$count;$i++){
141     $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
142   }
143   $query.= ") or ((notes like '$key[0]%' or notes like '% $key[0]%')";
144   for ($i=1;$i<$count;$i++){
145     $query.=" and (notes like '$key[$i]%' or notes like '% $key[$i]%')";
146   }
147   $query=$query.") order by title";
148   my $sth=$dbh->prepare($query);
149   $sth->execute;
150   my $data=$sth->fetchrow_hashref;
151   my $count=$data->{'count(*)'};
152   $sth->finish;
153   $query=~ s/count\(\*\)/\*/;
154   $query= $query." limit $offset,$num";
155   $sth=$dbh->prepare($query);
156 #  print $query;
157   $sth->execute;
158   $i=0;
159   while (my $data=$sth->fetchrow_hashref){
160       my $sti=$dbh->prepare("select dewey,subclass from biblioitems where biblionumber=$data->{'biblionumber'}");
161       $sti->execute;
162       my ($dewey, $subclass) = $sti->fetchrow;
163       $dewey=~s/0*$//;
164       ($dewey == 0) && ($dewey='');
165       ($dewey) && ($dewey.=" $subclass");
166       $sti->finish;
167     $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$dewey";
168     $i++;
169   }
170   $sth->finish;
171   $dbh->disconnect;
172   return($count,@results);
173 }
174
175
176   
177 sub FrontSearch {
178   my ($env,$type,$search,$num,$offset)=@_;
179   my $dbh = &C4Connect;
180   $search->{'front'}=~ s/ +$//;
181   $search->{'front'}=~ s/'/\\'/;
182   my @key=split(' ',$search->{'front'});
183   my $count=@key;
184   my $i=1;
185   my @results;
186   my $query ="Select * from biblio,bibliosubtitle where
187   biblio.biblionumber=bibliosubtitle.biblionumber and
188   ((title like '$key[0]%' or title like '% $key[0]%'
189   or subtitle like '$key[0]%' or subtitle like '% $key[0]%'
190   or author like '$key[0]%' or author like '% $key[0]%')";
191   while ($i < $count){
192     $query=$query." and (title like '%$key[$i]%' or subtitle like '%$key[$i]%')";
193     $i++;
194   }
195   $query=$query.") group by biblio.biblionumber order by author,title";
196   print $query;
197   my $sth=$dbh->prepare($query);
198   $sth->execute;
199   $i=0;
200   while (my $data=$sth->fetchrow_hashref){
201     my $sti=$dbh->prepare("select dewey,subclass from biblioitems where biblionumber=$data->{'biblionumber'}");
202     $sti->execute;
203     my ($dewey, $subclass) = $sti->fetchrow;
204     $dewey=~s/\.*0*$//;
205     ($dewey == 0) && ($dewey='');
206     ($dewey) && ($dewey.=" $subclass");
207     $sti->finish;
208     $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey";
209 #      print $results[$i];
210     $i++;
211   }
212   $sth->finish;
213   $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
214   like '%$search->{'keyword'}%'");
215   $sth->execute;
216   while (my $data=$sth->fetchrow_hashref){
217     my $sth2=$dbh->prepare("Select * from biblio where
218     biblionumber=$data->{'biblionumber'}");
219     $sth2->execute;
220     while (my $data2=$sth2->fetchrow_hashref){
221
222 $results[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data->{'copyrightdate'}";
223 #      print $results[$i];
224       $i++;   
225     }
226     $sth2->finish;
227   }    
228   my $i2=1;
229   @results=sort @results;
230   my @res;
231   my $count=@results;
232   $i=1;
233   $res[0]=$results[0];
234   while ($i2 < $count){
235     if ($results[$i2] ne $res[$i-1]){
236       $res[$i]=$results[$i2];
237       $i++;
238     }
239     $i2++;
240   }
241   $i2=0;
242   my @res2;
243   $count=@res;
244   while ($i2 < $num && $i2 < $count){
245     $res2[$i2]=$res[$i2+$offset];
246 #    print $res2[$i2];
247     $i2++;
248   }
249   $sth->finish;
250   $dbh->disconnect;
251   return($i,@res2);
252 }
253
254   
255 sub KeywordSearch {
256   my ($env,$type,$search,$num,$offset)=@_;
257   my $dbh = &C4Connect;
258   $search->{'keyword'}=~ s/ +$//;
259   $search->{'keyword'}=~ s/'/\\'/;
260   my @key=split(' ',$search->{'keyword'});
261   my $count=@key;
262   my $i=1;
263   my @results;
264   my $query="Select biblionumber from biblio
265   where ((title like '$key[0]%' or title like '% $key[0]%')";
266   while ($i < $count){                                                  
267       $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%')";                                                   
268       $i++;                                                  
269   }
270   $query.= ") or ((biblio.notes like '$key[0]%' or biblio.notes like '% $key[0]%')";                                             
271   for ($i=1;$i<$count;$i++){                                                  
272       $query.=" and (biblio.notes like '$key[$i]%' or biblio.notes like '% $key[$i]%')";                                           
273   }
274    $query.= ") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%')";                                               
275   for ($i=1;$i<$count;$i++){                                                  
276       $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";                                             
277   }
278   $query.=" )";
279 #  print $query;
280   my $sth=$dbh->prepare($query);
281   $sth->execute;
282   my $i=0;
283   while (my @res=$sth->fetchrow_array){
284     $results[$i]=$res[0];
285     $i++;
286   }
287   $sth->finish;
288   my $set1=Set::Scalar->new(@results);
289   $query="Select biblionumber from bibliosubtitle where
290   ((subtitle like '$key[0]%' or subtitle like '% $key[0]%')";                 
291   for ($i=1;$i<$count;$i++){   
292         $query.= " and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%')";                                                  
293   }                   
294   $query.=" )";
295 #  print $query;
296   $sth=$dbh->prepare($query);
297   $sth->execute;
298   $i=0;
299   while (my @res=$sth->fetchrow_array){
300     $results[$i]=$res[0];
301     $i++;
302   }
303   $sth->finish;
304   my $set2=Set::Scalar->new(@results);
305   if ($i > 0){
306     $set1=$set1+$set2;
307   }
308   $query ="Select biblionumber from biblioitems where
309   ((biblioitems.notes like '$key[0]%' or biblioitems.notes like '% $key[0]%')";                                   
310   for ($i=1;$i<$count;$i++){                                                  
311       $query.=" and (biblioitems.notes like '$key[$i]%' or biblioitems.notes like '% $key[$i]%')";                                 
312   }            
313   $query.=" )";
314 #  print $query;
315   $sth=$dbh->prepare($query);
316   $sth->execute;
317   $i=0;
318   while (my @res=$sth->fetchrow_array){
319     $results[$i]=$res[0];
320     $i++;
321   }
322   $sth->finish;
323   my $set3=Set::Scalar->new(@results);    
324   if ($i > 0){
325     $set1=$set1+$set3;
326   }
327   $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
328   like '%$search->{'keyword'}%' group by biblionumber");
329   $sth->execute;
330   $i=0;
331   while (my @res=$sth->fetchrow_array){
332     $results[$i]=$res[0];
333     $i++;
334   }
335   $sth->finish;
336   my $set4=Set::Scalar->new(@results);    
337   if ($i > 0){
338     $set1=$set1+$set4;
339   }
340   my $i2=0;
341   my $i3=0;
342   my $i4=0;
343
344   my @res2;
345   my @res = $set1->members;
346   $count=@res;
347 #  print $set1;
348   $i=0;
349 #  print "count $count";
350   if ($search->{'class'} ne ''){ 
351     while ($i2 <$count){
352       my $query="select * from biblio,biblioitems where 
353       biblio.biblionumber='$res[$i2]' and     
354       biblio.biblionumber=biblioitems.biblionumber ";         
355       if ($search->{'class'} ne ''){             
356       my @temp=split(/\|/,$search->{'class'});
357       my $count=@temp;                       
358       $query.= "and ( itemtype='$temp[0]'";     
359       for (my $i=1;$i<$count;$i++){     
360         $query.=" or itemtype='$temp[$i]'";                                     
361       } 
362       $query.=")"; 
363       }
364        my $sth=$dbh->prepare($query);    
365        #    print $query;        
366        $sth->execute;        
367        if (my $data2=$sth->fetchrow_hashref){            
368          my $dewey= $data2->{'dewey'};                
369          my $subclass=$data2->{'subclass'};          
370          $dewey=~s/\.*0*$//;           
371          ($dewey == 0) && ($dewey=''); 
372          ($dewey) && ($dewey.=" $subclass") ;                                    
373           $sth->finish;
374           my $end=$offset +$num;
375           if ($i4 <= $offset){
376             $i4++;
377           }
378 #         print $i4;
379           if ($i4 <=$end && $i4 > $offset){
380             $res2[$i3]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";    
381             $i3++;
382             $i4++;
383 #           print "in here $i3<br>";
384           } else {
385 #           print $end;
386           }
387           $i++; 
388         }         
389      $i2++;
390      }
391      $count=$i;
392   
393    } else {
394   while ($i2 < $num && $i2 < $count){
395     my $query="select * from biblio,biblioitems where
396     biblio.biblionumber='$res[$i2+$offset]' and        
397     biblio.biblionumber=biblioitems.biblionumber ";
398     if ($search->{'class'} ne ''){
399       my @temp=split(/\|/,$search->{'class'});
400       my $count=@temp;
401       $query.= "and ( itemtype='$temp[0]'";
402       for (my $i=1;$i<$count;$i++){
403         $query.=" or itemtype='$temp[$i]'";
404       }
405       $query.=")"; 
406     }
407     if ($search->{'dewey'} ne ''){
408       $query.= "and (dewey like '$search->{'dewey'}%') ";
409     }
410
411     my $sth=$dbh->prepare($query);
412 #    print $query;
413     $sth->execute;
414     if (my $data2=$sth->fetchrow_hashref){
415         my $dewey= $data2->{'dewey'};               
416         my $subclass=$data2->{'subclass'};                   
417         $dewey=~s/\.*0*$//;     
418         ($dewey == 0) && ($dewey='');               
419         ($dewey) && ($dewey.=" $subclass") ;                      
420         $sth->finish;                                                        
421         $res2[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
422         $i++;
423     }
424     $i2++;
425     
426   }
427   }
428   $dbh->disconnect;
429
430   #$count=$i;
431   return($count,@res2);
432 }
433
434 sub KeywordSearch2 {
435   my ($env,$type,$search,$num,$offset)=@_;
436   my $dbh = &C4Connect;
437   $search->{'keyword'}=~ s/ +$//;
438   $search->{'keyword'}=~ s/'/\\'/;
439   my @key=split(' ',$search->{'keyword'});
440   my $count=@key;
441   my $i=1;
442   my @results;
443   my $query ="Select * from biblio,bibliosubtitle,biblioitems where
444   biblio.biblionumber=biblioitems.biblionumber and
445   biblio.biblionumber=bibliosubtitle.biblionumber and
446   (((title like '$key[0]%' or title like '% $key[0]%')";
447   while ($i < $count){
448     $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%')";
449     $i++;
450   }
451   $query.= ") or ((subtitle like '$key[0]%' or subtitle like '% $key[0]%')";
452   for ($i=1;$i<$count;$i++){
453     $query.= " and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%')";
454   }
455   $query.= ") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%')";
456   for ($i=1;$i<$count;$i++){
457     $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
458   }
459   $query.= ") or ((biblio.notes like '$key[0]%' or biblio.notes like '% $key[0]%')";
460   for ($i=1;$i<$count;$i++){
461     $query.=" and (biblio.notes like '$key[$i]%' or biblio.notes like '% $key[$i]%')";
462   }
463   $query.= ") or ((biblioitems.notes like '$key[0]%' or biblioitems.notes like '% $key[0]%')";
464   for ($i=1;$i<$count;$i++){
465     $query.=" and (biblioitems.notes like '$key[$i]%' or biblioitems.notes like '% $key[$i]%')";
466   }
467   if ($search->{'keyword'} =~ /new zealand/i){
468     $query.= "or (title like 'nz%' or title like '% nz %' or title like '% nz' or subtitle like 'nz%'
469     or subtitle like '% nz %' or subtitle like '% nz' or author like 'nz %' 
470     or author like '% nz %' or author like '% nz')"
471   }
472   if ($search->{'keyword'} eq  'nz' || $search->{'keyword'} eq 'NZ' ||
473   $search->{'keyword'} =~ /nz /i || $search->{'keyword'} =~ / nz /i ||
474   $search->{'keyword'} =~ / nz/i){
475     $query.= "or (title like 'new zealand%' or title like '% new zealand %'
476     or title like '% new zealand' or subtitle like 'new zealand%' or
477     subtitle like '% new zealand %'
478     or subtitle like '% new zealand' or author like 'new zealand%' 
479     or author like '% new zealand %' or author like '% new zealand' or 
480     seriestitle like 'new zealand%' or seriestitle like '% new zealand %'
481     or seriestitle like '% new zealand')"
482   }
483   $query=$query."))";
484   if ($search->{'class'} ne ''){
485     my @temp=split(/\|/,$search->{'class'});
486     my $count=@temp;
487     $query.= "and ( itemtype='$temp[0]'";
488     for (my $i=1;$i<$count;$i++){
489       $query.=" or itemtype='$temp[$i]'";
490      }
491   $query.=")"; 
492   }
493   if ($search->{'dewey'} ne ''){
494     $query.= "and (dewey like '$search->{'dewey'}%') ";
495   }
496    $query.="group by biblio.biblionumber";
497    #$query.=" order by author,title";
498 #  print $query;
499   my $sth=$dbh->prepare($query);
500   $sth->execute;
501   $i=0;
502   while (my $data=$sth->fetchrow_hashref){
503 #    my $sti=$dbh->prepare("select dewey,subclass from biblioitems where biblionumber=$data->{'biblionumber'}
504 #    ");
505 #    $sti->execute;
506 #    my ($dewey, $subclass) = $sti->fetchrow;
507     my $dewey=$data->{'dewey'};
508     my $subclass=$data->{'subclass'};
509     $dewey=~s/\.*0*$//;
510     ($dewey == 0) && ($dewey='');
511     ($dewey) && ($dewey.=" $subclass");
512 #    $sti->finish;
513     $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey";
514 #      print $results[$i];
515     $i++;
516   }
517   $sth->finish;
518   $sth=$dbh->prepare("Select biblionumber from bibliosubject where subject
519   like '%$search->{'keyword'}%' group by biblionumber");
520   $sth->execute;
521   while (my $data=$sth->fetchrow_hashref){
522     $query="Select * from biblio,biblioitems where
523     biblio.biblionumber=$data->{'biblionumber'} and
524     biblio.biblionumber=biblioitems.biblionumber ";
525     if ($search->{'class'} ne ''){
526       my @temp=split(/\|/,$search->{'class'});
527       my $count=@temp;
528       $query.= " and ( itemtype='$temp[0]'";
529       for (my $i=1;$i<$count;$i++){
530         $query.=" or itemtype='$temp[$i]'";
531       }
532       $query.=")"; 
533       
534     }
535     if ($search->{'dewey'} ne ''){
536       $query.= "and (dewey like '$search->{'dewey'}%') ";
537     }
538     my $sth2=$dbh->prepare($query);
539     $sth2->execute;
540 #    print $query;
541     while (my $data2=$sth2->fetchrow_hashref){
542       my $dewey= $data2->{'dewey'};
543       my $subclass=$data2->{'subclass'};
544       $dewey=~s/\.*0*$//;          
545       ($dewey == 0) && ($dewey='');              
546       ($dewey) && ($dewey.=" $subclass") ;                  
547 #      $sti->finish;              
548        $results[$i]="$data2->{'author'}\t$data2->{'title'}\t$data2->{'biblionumber'}\t$data2->{'copyrightdate'}\t$dewey";
549 #      print $results[$i];
550       $i++;   
551     }
552     $sth2->finish;
553   }    
554   my $i2=1;
555   @results=sort @results;
556   my @res;
557   my $count=@results;
558   $i=1;
559   if ($count > 0){
560     $res[0]=$results[0];
561   }
562   while ($i2 < $count){
563     if ($results[$i2] ne $res[$i-1]){
564       $res[$i]=$results[$i2];
565       $i++;
566     }
567     $i2++;
568   }
569   $i2=0;
570   my @res2;
571   $count=@res;
572   while ($i2 < $num && $i2 < $count){
573     $res2[$i2]=$res[$i2+$offset];
574 #    print $res2[$i2];
575     $i2++;
576   }
577   $sth->finish;
578   $dbh->disconnect;
579 #  $i--;
580 #  $i++;
581   return($i,@res2);
582 }
583
584 sub CatSearch  {
585   my ($env,$type,$search,$num,$offset)=@_;
586   my $dbh = &C4Connect;
587   my $query = '';
588     my @results;
589   $search->{'title'}=~ s/'/\\'/g;
590   $search->{'author'}=~ s/'/\\'/g;
591   $search->{'illustrator'}=~ s/'/\\'/g;
592   my $title = lc($search->{'title'}); 
593   
594   if ($type eq 'loose') {
595       if ($search->{'author'} ne ''){
596         my @key=split(' ',$search->{'author'});
597         my $count=@key;
598         my $i=1;
599         $query="select *,biblio.author,biblio.biblionumber from
600          biblioitems,biblio
601          left join additionalauthors
602          on additionalauthors.biblionumber =biblio.biblionumber
603          where biblioitems.biblionumber=biblio.biblionumber 
604          and
605          ((biblio.author like '$key[0]%' or biblio.author like '% $key[0]%' or
606          additionalauthors.author like '$key[0]%' or additionalauthors.author 
607          like '% $key[0]%'
608                  )";    
609          while ($i < $count){ 
610            $query=$query." and (
611            biblio.author like '$key[$i]%' or biblio.author like '% $key[$i]%' or
612            additionalauthors.author like '$key[$i]%' or additionalauthors.author like '% $key[$i]%'
613            )";
614            $i++;       
615          }   
616          $query=$query.")";
617          if ($search->{'title'} ne ''){ 
618            my @key=split(' ',$search->{'title'});
619            my $count=@key;
620            my $i=0;
621            $query.= " and (((title like '$key[0]%' or title like '% $key[0]%' or title like '% $key[0]')";
622             while ($i<$count){            
623               $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%' or title like '% $key[$i]')";
624               $i++; 
625             }                       
626 #           $query.=") or ((subtitle like '$key[0]%' or subtitle like '% $key[0] %' or subtitle like '% $key[0]')"; 
627 #            for ($i=1;$i<$count;$i++){
628 #             $query.=" and (subtitle like '$key[$i]%' or subtitle like '% $key[$i] %' or subtitle like '% $key[$i]')";   
629 #            }
630             $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%' or seriestitle like '% $key[0]')";  
631             for ($i=1;$i<$count;$i++){                    
632                 $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
633             }                                                             
634             $query.=") or ((unititle like '$key[0]%' or unititle like '% $key[0]%' or unititle like '% $key[0]')";                         
635             for ($i=1;$i<$count;$i++){                    
636                 $query.=" and (unititle like '$key[$i]%' or unititle like '% $key[$i]%')";   
637             }                                                             
638             $query=$query."))"; 
639            #$query=$query. " and (title like '%$search->{'title'}%' 
640            #or seriestitle like '%$search->{'title'}%')";
641          }
642          if ($search->{'class'} ne ''){
643            my @temp=split(/\|/,$search->{'class'});
644            my $count=@temp;
645            $query.= "and ( itemtype='$temp[0]'";
646            for (my $i=1;$i<$count;$i++){
647              $query.=" or itemtype='$temp[$i]'";
648            }
649            $query.=") ";
650          }
651          if ($search->{'dewey'} ne ''){
652               $query.=" and dewey='$search->{'dewey'}' ";
653          }           
654           if ($search->{'illustrator'} ne ''){
655             $query.=" and illus like '%".$search->{'illustrator'}."%' ";
656           }
657          
658          $query.=" group by biblio.biblionumber";
659       } else {
660           if ($search->{'title'} ne '') {
661            if ($search->{'ttype'} eq 'exact'){
662              $query="select * from biblio
663              where                            
664              (biblio.title='$search->{'title'}' or (biblio.unititle = '$search->{'title'}'
665              or biblio.unititle like '$search->{'title'} |%' or 
666              biblio.unititle like '%| $search->{'title'} |%' or
667              biblio.unititle like '%| $search->{'title'}') or
668              (biblio.seriestitle = '$search->{'title'}' or
669              biblio.seriestitle like '$search->{'title'} |%' or
670              biblio.seriestitle like '%| $search->{'title'} |%' or
671              biblio.seriestitle like '%| $search->{'title'}')
672              )";
673            } else {
674             my @key=split(' ',$search->{'title'});
675             my $count=@key;
676             my $i=1;
677             $query="select * from biblio,biblioitems
678             left join bibliosubtitle on
679             biblio.biblionumber=bibliosubtitle.biblionumber
680             where
681             biblioitems.biblionumber=biblio.biblionumber and
682             (((title like '$key[0]%' or title like '% $key[0]%' or title like '% $key[0]')";
683             while ($i<$count){
684               $query=$query." and (title like '$key[$i]%' or title like '% $key[$i]%' or title like '% $key[$i]')";
685               $i++;
686             }
687             $query.=") or ((subtitle like '$key[0]%' or subtitle like '% $key[0]%' or subtitle like '% $key[0]')";
688             for ($i=1;$i<$count;$i++){
689               $query.=" and (subtitle like '$key[$i]%' or subtitle like '% $key[$i]%' or subtitle like '% $key[$i]')";
690             }
691             $query.=") or ((seriestitle like '$key[0]%' or seriestitle like '% $key[0]%' or seriestitle like '% $key[0]')";
692             for ($i=1;$i<$count;$i++){
693               $query.=" and (seriestitle like '$key[$i]%' or seriestitle like '% $key[$i]%')";
694             }
695             $query.=") or ((unititle like '$key[0]%' or unititle like '% $key[0]%' or unititle like '% $key[0]')";
696             for ($i=1;$i<$count;$i++){
697               $query.=" and (unititle like '$key[$i]%' or unititle like '% $key[$i]%')";
698             }
699             $query=$query."))";
700             if ($search->{'class'} ne ''){
701               my @temp=split(/\|/,$search->{'class'});
702               my $count=@temp;
703               $query.= " and ( itemtype='$temp[0]'";
704               for (my $i=1;$i<$count;$i++){
705                $query.=" or itemtype='$temp[$i]'";
706               }
707               $query.=")";
708             }
709             if ($search->{'dewey'} ne ''){
710               $query.=" and dewey='$search->{'dewey'}' ";
711             }
712             if ($search->{'illustrator'} ne ''){
713               $query.=" and illus like '%".$search->{'illustrator'}."%' ";
714             }
715            }
716           } elsif ($search->{'class'} ne ''){
717              $query="select * from biblioitems,biblio where biblio.biblionumber=biblioitems.biblionumber";
718              my @temp=split(/\|/,$search->{'class'});
719               my $count=@temp;
720               $query.= " and ( itemtype='$temp[0]'";
721               for (my $i=1;$i<$count;$i++){
722                $query.=" or itemtype='$temp[$i]'";
723               }
724               $query.=")";
725               if ($search->{'illustrator'} ne ''){
726                 $query.=" and illus like '%".$search->{'illustrator'}."%' ";
727               }
728               if ($search->{'dewey'} ne ''){
729                 $query.=" and biblioitems.dewey like '$search->{'dewey'}%'";
730               }
731           } elsif ($search->{'dewey'} ne ''){
732              $query="select * from biblioitems,biblio 
733              where biblio.biblionumber=biblioitems.biblionumber
734              and biblioitems.dewey like '$search->{'dewey'}%'";
735           } elsif ($search->{'illustrator'} ne '') {
736           if ($search->{'illustrator'} ne ''){
737              $query="select * from biblioitems,biblio 
738              where biblio.biblionumber=biblioitems.biblionumber
739              and biblioitems.illus like '%".$search->{'illustrator'}."%'";
740           }
741         }
742           $query .=" group by biblio.biblionumber";      
743       }
744   } 
745   if ($type eq 'subject'){
746     my @key=split(' ',$search->{'subject'});
747     my $count=@key;
748     my $i=1;
749     $query="select distinct(subject) from bibliosubject where( subject like
750     '$key[0]%' or subject like '% $key[0]%' or subject like '% $key[0]' or subject like '%($key[0])%')";
751     while ($i<$count){
752       $query.=" and (subject like '$key[$i]%' or subject like '% $key[$i]%'
753       or subject like '% $key[$i]'
754       or subject like '%($key[$i])%')";
755       $i++;
756     }
757     if ($search->{'subject'} eq 'NZ' || $search->{'subject'} eq 'nz'){ 
758       $query.= " or (subject like 'NEW ZEALAND %' or subject like '% NEW ZEALAND %'
759       or subject like '% NEW ZEALAND' or subject like '%(NEW ZEALAND)%' ) ";
760     } elsif ( $search->{'subject'} =~ /^nz /i || $search->{'subject'} =~ / nz /i || $search->{'subject'} =~ / nz$/i){
761       $query=~ s/ nz/ NEW ZEALAND/ig;
762       $query=~ s/nz /NEW ZEALAND /ig;
763       $query=~ s/\(nz\)/\(NEW ZEALAND\)/gi;
764     }  
765   }
766   if ($type eq 'precise'){
767       $query="select * from items,biblio ";
768       if ($search->{'item'} ne ''){
769         my $search2=uc $search->{'item'};
770         $query=$query." where 
771         items.biblionumber=biblio.biblionumber 
772         and barcode='$search2'";
773       }
774       if ($search->{'isbn'} ne ''){
775         my $search2=uc $search->{'isbn'};
776         my $query1 = "select * from biblioitems where isbn='$search2'";
777         my $sth1=$dbh->prepare($query1);
778 #       print $query1;
779         $sth1->execute;
780         my $i2=0;
781         while (my $data=$sth1->fetchrow_hashref) {
782            $query="select * from biblioitems,biblio where
783            biblio.biblionumber = $data->{'biblionumber'}
784            and biblioitems.biblionumber = biblio.biblionumber";
785            my $sth=$dbh->prepare($query);
786            $sth->execute;
787            my $data=$sth->fetchrow_hashref;
788            my ($dewey, $subclass) = ($data->{'dewey'}, $data->{'subclass'});
789            $dewey=~s/\.*0*$//;
790            ($dewey == 0) && ($dewey='');
791            ($dewey) && ($dewey.=" $subclass");
792            $results[$i2]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'isbn'}\t$data->{'itemtype'}";
793            $i2++; 
794            $sth->finish;
795         }
796         $sth1->finish;
797       }
798   }
799 print $query;
800 if ($type ne 'precise' && $type ne 'subject'){
801   if ($search->{'author'} ne ''){   
802       $query=$query." order by biblio.author,title";
803   } else {
804       $query=$query." order by title";
805   }
806 } else {
807   if ($type eq 'subject'){
808       $query=$query." order by subject";
809   }
810 }
811 #print $query;
812 my $sth=$dbh->prepare($query);
813 $sth->execute;
814 my $count=1;
815 my $i=0;
816 my $limit= $num+$offset;
817 while (my $data=$sth->fetchrow_hashref){
818   my $sti=$dbh->prepare("select dewey,subclass from biblioitems where biblionumber=$data->{'biblionumber'}");
819   $sti->execute;
820   my ($dewey, $subclass) = $sti->fetchrow;
821   $dewey=~s/\.*0*$//;
822   ($dewey == 0) && ($dewey='');
823   ($dewey) && ($dewey.=" $subclass");
824   $sti->finish;
825   if ($count > $offset && $count <= $limit){
826     if ($type ne 'subject' && $type ne 'precise'){
827        $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'illus'}";
828     } elsif ($search->{'isbn'} ne '' || $search->{'item'} ne ''){
829        $results[$i]="$data->{'author'}\t$data->{'title'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'illus'}";
830     } else {  
831      $results[$i]="$data->{'author'}\t$data->{'subject'}\t$data->{'biblionumber'}\t$data->{'copyrightdate'}\t$dewey\t$data->{'illus'}";
832     }
833     $i++;
834   }
835   $count++;
836 }
837 $sth->finish;
838 #if ($type ne 'precise'){
839   $count--;
840 #}
841 #$count--;
842 return($count,@results);
843 }
844
845 sub updatesearchstats{
846   my ($dbh,$query)=@_;
847   
848 }
849
850 sub subsearch {
851   my ($env,$subject)=@_;
852   my $dbh=C4Connect();
853   $subject=$dbh->quote($subject);
854   my $query="Select * from biblio,bibliosubject where
855   biblio.biblionumber=bibliosubject.biblionumber and
856   bibliosubject.subject=$subject group by biblio.biblionumber
857   order by biblio.title";
858   my $sth=$dbh->prepare($query);
859   $sth->execute;
860   my $i=0;
861 #  print $query;
862   my @results;
863   while (my $data=$sth->fetchrow_hashref){
864     $results[$i]="$data->{'title'}\t$data->{'author'}\t$data->{'biblionumber'}";
865     $i++;
866   }
867   $sth->finish;
868   $dbh->disconnect;
869   return(@results);
870 }
871
872
873 sub ItemInfo {
874   my ($env,$biblionumber,$type)=@_;
875   my $dbh = &C4Connect;
876   my $query="Select * from items,biblio,biblioitems,branches 
877   where (items.biblioitemnumber = biblioitems.biblioitemnumber)
878   and biblioitems.biblionumber=biblio.biblionumber
879   and biblio.biblionumber='$biblionumber' and branches.branchcode=
880   items.holdingbranch ";
881 #  print $type;
882   if ($type ne 'intra'){
883     $query.=" and (items.itemlost<>1 or items.itemlost is NULL)
884     and (wthdrawn <> 1 or wthdrawn is NULL)";
885   }
886   $query=$query."order by items.dateaccessioned desc";
887   my $sth=$dbh->prepare($query);
888   $sth->execute;
889   my $i=0;
890   my @results;
891 #  print $query;
892   while (my $data=$sth->fetchrow_hashref){
893     my $iquery = "Select * from issues
894     where itemnumber = '$data->{'itemnumber'}'
895     and returndate is null";
896     my $datedue = '';
897     my $isth=$dbh->prepare($iquery);
898     $isth->execute;
899     if (my $idata=$isth->fetchrow_hashref){
900       my @temp=split('-',$idata->{'date_due'});
901       $datedue = "$temp[2]/$temp[1]/$temp[0]";
902     }
903     if ($data->{'itemlost'} eq '1'){
904         $datedue='Itemlost';
905     }
906     if ($data->{'wthdrawn'} eq '1'){
907       $datedue="Cancelled";
908     }
909     if ($datedue eq ''){
910        my ($rescount,$reserves)=Findgroupreserve($data->{'biblioitemnumber'},$biblionumber);
911
912        if ($rescount >0){                                
913           $datedue='Request';
914        }
915     }
916     $isth->finish;
917     my $class = $data->{'classification'};
918     my $dewey = $data->{'dewey'};
919     $dewey =~ s/0+$//;
920     if ($dewey eq "000.") { $dewey = "";};    
921     if ($dewey < 10){$dewey='00'.$dewey;}
922     if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
923     if ($dewey <= 0){
924       $dewey='';
925     }
926     $dewey=~ s/\.$//;
927     $class = $class.$dewey;
928     if ($dewey ne ''){
929       $class = $class.$data->{'subclass'};
930     }
931  #   $results[$i]="$data->{'title'}\t$data->{'barcode'}\t$datedue\t$data->{'branchname'}\t$data->{'dewey'}";
932     my @temp=split('-',$data->{'datelastseen'});
933     my $date="$temp[2]/$temp[1]/$temp[0]";
934     $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'}";
935 #    print "$results[$i] <br>";
936     $i++;
937   }
938   $sth->finish;
939   $dbh->disconnect;
940   return(@results);
941 }
942
943 sub GetItems {
944    my ($env,$biblionumber)=@_;
945    #debug_msg($env,"GetItems");
946    my $dbh = &C4Connect;
947    my $query = "Select * from biblioitems where (biblionumber = $biblionumber)";
948    #debug_msg($env,$query);
949    my $sth=$dbh->prepare($query);
950    $sth->execute;
951    #debug_msg($env,"executed query");      
952    my $i=0;
953    my @results;
954    while (my $data=$sth->fetchrow_hashref) {
955       #debug_msg($env,$data->{'biblioitemnumber'});
956       my $dewey = $data->{'dewey'};
957       $dewey =~ s/0+$//; 
958       my $line = $data->{'biblioitemnumber'}."\t".$data->{'itemtype'};
959       $line = $line."\t$data->{'classification'}\t$dewey";
960       $line = $line."\t$data->{'subclass'}\t$data->{isbn}";
961       $line = $line."\t$data->{'volume'}\t$data->{number}";
962       my $isth= $dbh->prepare("select * from items where biblioitemnumber = $data->{'biblioitemnumber'}");
963       $isth->execute;
964       while (my $idata = $isth->fetchrow_hashref) {
965         my $iline = $idata->{'barcode'}."[".$idata->{'holdingbranch'}."[";
966         if ($idata->{'notforloan'} == 1) {
967           $iline = $iline."NFL ";
968         }
969         if ($idata->{'itemlost'} == 1) {
970           $iline = $iline."LOST ";
971         }        
972         $line = $line."\t$iline"; 
973       }
974       $isth->finish;
975       $results[$i] = $line;
976       $i++;      
977    }
978    $sth->finish;
979    $dbh->disconnect;
980    return(@results);
981 }            
982   
983 sub itemdata {
984   my ($barcode)=@_;
985   my $dbh=C4Connect;
986   my $query="Select * from items,biblioitems where barcode='$barcode'
987   and items.biblioitemnumber=biblioitems.biblioitemnumber";
988 #  print $query;
989   my $sth=$dbh->prepare($query);
990   $sth->execute;
991   my $data=$sth->fetchrow_hashref;
992   $sth->finish;
993   $dbh->disconnect;
994   return($data);
995 }
996
997 sub bibdata {
998   my ($bibnum,$type)=@_;
999   my $dbh=C4Connect;
1000   my $query="Select *,biblio.notes  from biblio,biblioitems,bibliosubtitle where biblio.biblionumber=$bibnum
1001   and biblioitems.biblionumber=$bibnum and 
1002 (bibliosubtitle.biblionumber=$bibnum)"; 
1003 #  print $query;
1004   my $sth=$dbh->prepare($query);
1005   $sth->execute;
1006   my $data=$sth->fetchrow_hashref;
1007   $sth->finish;
1008   $query="Select * from bibliosubject where biblionumber='$bibnum'";
1009   $sth=$dbh->prepare($query);
1010   $sth->execute;
1011   while (my $dat=$sth->fetchrow_hashref){
1012     $data->{'subject'}.=" | $dat->{'subject'}";
1013
1014   }
1015   #print $query;
1016   $sth->finish;
1017   $dbh->disconnect;
1018   return($data);
1019 }
1020
1021 sub bibitemdata {
1022   my ($bibitem)=@_;
1023   my $dbh=C4Connect;
1024   my $query="Select *,biblioitems.notes as bnotes from biblio,biblioitems,itemtypes where biblio.biblionumber=
1025   biblioitems.biblionumber and biblioitemnumber=$bibitem and
1026   biblioitems.itemtype=itemtypes.itemtype";
1027 #  print $query;
1028   my $sth=$dbh->prepare($query);
1029   $sth->execute;
1030   my $data=$sth->fetchrow_hashref;
1031   $sth->finish;
1032   $dbh->disconnect;
1033   return($data);
1034 }
1035
1036 sub subject {
1037   my ($bibnum)=@_;
1038   my $dbh=C4Connect;
1039   my $query="Select * from bibliosubject where biblionumber=$bibnum";
1040   my $sth=$dbh->prepare($query);
1041   $sth->execute;
1042   my @results;
1043   my $i=0;
1044   while (my $data=$sth->fetchrow_hashref){
1045     $results[$i]=$data;
1046     $i++;
1047   }
1048   $sth->finish;
1049   $dbh->disconnect;
1050   return($i,\@results);
1051 }
1052
1053 sub addauthor {
1054   my ($bibnum)=@_;
1055   my $dbh=C4Connect;
1056   my $query="Select * from additionalauthors where biblionumber=$bibnum";
1057   my $sth=$dbh->prepare($query);
1058   $sth->execute;
1059   my @results;
1060   my $i=0;
1061   while (my $data=$sth->fetchrow_hashref){
1062     $results[$i]=$data;
1063     $i++;
1064   }
1065   $sth->finish;
1066   $dbh->disconnect;
1067   return($i,\@results);
1068 }
1069
1070 sub subtitle {
1071   my ($bibnum)=@_;
1072   my $dbh=C4Connect;
1073   my $query="Select * from bibliosubtitle where biblionumber=$bibnum";
1074   my $sth=$dbh->prepare($query);
1075   $sth->execute;
1076   my @results;
1077   my $i=0;
1078   while (my $data=$sth->fetchrow_hashref){
1079     $results[$i]=$data;
1080     $i++;
1081   }
1082   $sth->finish;
1083   $dbh->disconnect;
1084   return($i,\@results);
1085 }
1086
1087
1088
1089 sub itemissues {
1090   my ($bibitem,$biblio)=@_;
1091   my $dbh=C4Connect;
1092   my $query="Select * from items where 
1093   items.biblioitemnumber='$bibitem'";
1094   my $sth=$dbh->prepare($query) || die $dbh->errstr;
1095   $sth->execute || die $sth->errstr;
1096   my $i=0;
1097   my @results;
1098   while (my $data=$sth->fetchrow_hashref) {
1099     my $query2="select * from issues,borrowers where itemnumber=$data->{'itemnumber'}
1100     and returndate is NULL and issues.borrowernumber=borrowers.borrowernumber";
1101     my $sth2=$dbh->prepare($query2);
1102     $sth2->execute;
1103     if (my $data2=$sth2->fetchrow_hashref) {
1104       $data->{'date_due'}=$data2->{'date_due'};
1105       $data->{'card'}=$data2->{'cardnumber'};
1106     } else {
1107       if ($data->{'wthdrawn'} eq '1') {
1108         $data->{'date_due'}='Cancelled';
1109       } else {
1110           $data->{'date_due'}='Available';
1111       }
1112     }
1113     $sth2->finish;
1114     $query2="select * from issues,borrowers where itemnumber='$data->{'itemnumber'}'
1115     and issues.borrowernumber=borrowers.borrowernumber 
1116     order by date_due desc";
1117     my $sth2=$dbh->prepare($query2) || die $dbh->errstr;
1118     $sth2->execute || die $sth2->errstr;
1119     for (my $i2=0;$i2<2;$i2++){
1120       if (my $data2=$sth2->fetchrow_hashref){
1121         $data->{"timestamp$i2"}=$data2->{'timestamp'};
1122         $data->{"card$i2"}=$data2->{'cardnumber'};
1123         $data->{"borrower$i2"}=$data2->{'borrowernumber'};
1124       }
1125     }
1126     $sth2->finish;
1127     $results[$i]=$data;
1128     $i++;
1129   }
1130   $sth->finish;
1131   $dbh->disconnect;
1132   return(@results);
1133 }
1134
1135 sub itemnodata {
1136   my ($env,$dbh,$itemnumber) = @_;
1137   $dbh=C4Connect;
1138   my $query="Select * from biblio,items,biblioitems
1139     where items.itemnumber = '$itemnumber'
1140     and biblio.biblionumber = items.biblionumber
1141     and biblioitems.biblioitemnumber = items.biblioitemnumber";
1142   my $sth=$dbh->prepare($query);
1143 #  print $query;
1144   $sth->execute;
1145   my $data=$sth->fetchrow_hashref;
1146   $sth->finish;  
1147   $dbh->disconnect;
1148   return($data);               
1149 }
1150
1151 #used by member enquiries from the intranet
1152 #called by member.pl
1153 sub BornameSearch  {
1154   my ($env,$searchstring,$type)=@_;
1155   my $dbh = &C4Connect;
1156   $searchstring=~ s/\'/\\\'/g;
1157   my @data=split(' ',$searchstring);
1158   my $count=@data;
1159   my $query="Select * from borrowers 
1160   where ((surname like \"$data[0]%\" or surname like \"% $data[0]%\" 
1161   or firstname  like \"$data[0]%\" or firstname like \"% $data[0]%\" 
1162   or othernames like \"$data[0]%\" or othernames like \"% $data[0]%\")
1163   ";
1164   for (my $i=1;$i<$count;$i++){
1165     $query=$query." and (surname like \"$data[$i]%\" or surname like \"% $data[$i]%\"                  
1166     or firstname  like \"$data[$i]%\" or firstname like \"% $data[$i]%\"                    
1167     or othernames like \"$data[$i]%\" or othernames like \"% $data[$i]%\")";
1168   }
1169   $query=$query.") or cardnumber = \"$searchstring\"
1170   order by surname,firstname";
1171 #  print $query,"\n";
1172   my $sth=$dbh->prepare($query);
1173   $sth->execute;
1174   my @results;
1175   my $cnt=0;
1176   while (my $data=$sth->fetchrow_hashref){
1177     push(@results,$data);
1178     $cnt ++;
1179   }
1180 #  $sth->execute;
1181   $sth->finish;
1182   $dbh->disconnect;
1183   return ($cnt,\@results);
1184 }
1185
1186 sub borrdata {
1187   my ($cardnumber,$bornum)=@_;
1188   $cardnumber = uc $cardnumber;
1189   my $dbh=C4Connect;
1190   my $query;
1191   if ($bornum eq ''){
1192     $query="Select * from borrowers where cardnumber='$cardnumber'";
1193   } else {
1194       $query="Select * from borrowers where borrowernumber='$bornum'";
1195   }
1196   #print $query;
1197   my $sth=$dbh->prepare($query);
1198   $sth->execute;
1199   my $data=$sth->fetchrow_hashref;
1200   $sth->finish;
1201   $dbh->disconnect;
1202   return($data);
1203 }
1204
1205 sub borrissues {
1206   my ($bornum)=@_;
1207   my $dbh=C4Connect;
1208   my $query;
1209   $query="Select * from issues,biblio,items where borrowernumber='$bornum' and
1210 items.itemnumber=issues.itemnumber and
1211 items.biblionumber=biblio.biblionumber and issues.returndate is NULL order
1212 by date_due";
1213   #print $query;
1214   my $sth=$dbh->prepare($query);
1215     $sth->execute;
1216   my @result;
1217   my $i=0;
1218   while (my $data=$sth->fetchrow_hashref){
1219     $result[$i]=$data;;
1220     $i++;
1221   }
1222   $sth->finish;
1223   $dbh->disconnect;
1224   return($i,\@result);
1225 }
1226
1227 sub allissues { 
1228   my ($bornum,$order,$limit)=@_; 
1229   my $dbh=C4Connect;   
1230   my $query;     
1231   $query="Select * from issues,biblio,items,biblioitems       
1232   where borrowernumber='$bornum' and         
1233   items.biblioitemnumber=biblioitems.biblioitemnumber and           
1234   items.itemnumber=issues.itemnumber and             
1235   items.biblionumber=biblio.biblionumber";               
1236   $query.=" order by $order";                 
1237   if ($limit !=0){                   
1238     $query.=" limit $limit";                     
1239   }                         
1240   #print $query;                           
1241   my $sth=$dbh->prepare($query);          
1242   $sth->execute;
1243   my @result;   
1244   my $i=0;    
1245   while (my $data=$sth->fetchrow_hashref){                                      
1246     $result[$i]=$data;; 
1247     $i++;     
1248   }         
1249   $sth->finish;           
1250   $dbh->disconnect;             
1251   return($i,\@result);               
1252 }
1253
1254 sub borrdata2 {
1255   my ($env,$bornum)=@_;
1256   my $dbh=C4Connect;
1257   my $query="Select count(*) from issues where borrowernumber='$bornum' and
1258     returndate is NULL";
1259     # print $query;
1260   my $sth=$dbh->prepare($query);
1261   $sth->execute;
1262   my $data=$sth->fetchrow_hashref;
1263   $sth->finish;
1264   $sth=$dbh->prepare("Select count(*) from issues where
1265     borrowernumber='$bornum' and date_due < now() and returndate is NULL");
1266   $sth->execute;
1267   my $data2=$sth->fetchrow_hashref;
1268   $sth->finish;
1269   $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
1270     borrowernumber='$bornum'");
1271   $sth->execute;
1272   my $data3=$sth->fetchrow_hashref;
1273   $sth->finish;
1274   $dbh->disconnect;
1275
1276 return($data2->{'count(*)'},$data->{'count(*)'},$data3->{'sum(amountoutstanding)'});
1277 }
1278                   
1279 sub getacctlist {
1280    my ($env,$params) = @_;
1281    my $dbh=C4Connect;
1282    my @acctlines;
1283    my $numlines;
1284    my $query = "Select borrowernumber, accountno, date, amount, description,
1285       dispute, accounttype, amountoutstanding, barcode, title
1286       from accountlines,items,biblio   
1287       where borrowernumber = $params->{'borrowernumber'} ";
1288    if ($params->{'acctno'} ne "") {
1289       my $query = $query." and accountlines.accountno = $params->{'acctno'} ";
1290       }
1291    my $query = $query." and accountlines.itemnumber = items.itemnumber
1292       and items.biblionumber = biblio.biblionumber
1293       and accountlines.amountoutstanding<>0 order by date";
1294    my $sth=$dbh->prepare($query);
1295 #   print $query;
1296    $sth->execute;
1297    my $total=0;
1298    while (my $data=$sth->fetchrow_hashref){
1299       $acctlines[$numlines] = $data;
1300       $numlines++;
1301       $total = $total+ $data->{'amountoutstanding'};
1302    }
1303    return ($numlines,\@acctlines,$total);
1304    $sth->finish;
1305    $dbh->disconnect;
1306 }
1307
1308 sub getboracctrecord {
1309    my ($env,$params) = @_;
1310    my $dbh=C4Connect;
1311    my @acctlines;
1312    my $numlines=0;
1313    my $query= "Select * from accountlines where
1314 borrowernumber=$params->{'borrowernumber'} order by date desc,timestamp desc";
1315    my $sth=$dbh->prepare($query);
1316 #   print $query;
1317    $sth->execute;
1318    my $total=0;
1319    while (my $data=$sth->fetchrow_hashref){
1320 #      if ($data->{'itemnumber'} ne ''){
1321 #        $query="Select * from items,biblio where items.itemnumber=
1322 #       '$data->{'itemnumber'}' and biblio.biblionumber=items.biblionumber";
1323 #       my $sth2=$dbh->prepare($query);
1324 #       $sth2->execute;
1325 #       my $data2=$sth2->fetchrow_hashref;
1326 #       $sth2->finish;
1327 #       $data=$data2;
1328  #     }
1329       $acctlines[$numlines] = $data;
1330       $numlines++;
1331       $total = $total+ $data->{'amountoutstanding'};
1332    }
1333    $sth->finish;
1334    $dbh->disconnect;
1335    return ($numlines,\@acctlines,$total);
1336 }
1337
1338 sub itemcount { 
1339   my ($env,$bibnum,$type)=@_; 
1340   my $dbh=C4Connect;   
1341   my $query="Select * from items where     
1342   biblionumber=$bibnum ";
1343   if ($type ne 'intra'){
1344     $query.=" and (itemlost <>1 or itemlost is NULL) and
1345     (wthdrawn <> 1 or wthdrawn is NULL)";      
1346   }
1347   my $sth=$dbh->prepare($query);         
1348   #  print $query;           
1349   $sth->execute;           
1350   my $count=0;             
1351   my $lcount=0;               
1352   my $nacount=0;                 
1353   my $fcount=0;
1354   my $scount=0;
1355   my $lostcount=0;
1356   my $mending=0;
1357   my $transit=0;
1358   my $ocount=0;
1359   while (my $data=$sth->fetchrow_hashref){
1360     $count++;                     
1361     my $query2="select * from issues,items where issues.itemnumber=                          
1362     '$data->{'itemnumber'}' and returndate is NULL
1363     and items.itemnumber=issues.itemnumber and (items.itemlost <>1 or
1364     items.itemlost is NULL)"; 
1365     my $sth2=$dbh->prepare($query2);     
1366     $sth2->execute;         
1367     if (my $data2=$sth2->fetchrow_hashref){         
1368        $nacount++;         
1369     } else {         
1370       if ($data->{'holdingbranch'} eq 'C'){         
1371         $lcount++;               
1372       }                       
1373       if ($data->{'holdingbranch'} eq 'F' || $data->{'holdingbranch'} eq 'FP'){         
1374         $fcount++;               
1375       }                       
1376       if ($data->{'holdingbranch'} eq 'S' || $data->{'holdingbranch'} eq 'SP'){         
1377         $scount++;               
1378       }                       
1379       if ($data->{'itemlost'} eq '1'){
1380         $lostcount++;
1381       }
1382       if ($data->{'holdingbranch'} eq 'FM'){
1383         $mending++;
1384       }
1385       if ($data->{'holdingbranch'} eq 'TR'){
1386         $transit++;
1387       }
1388     }                             
1389     $sth2->finish;     
1390   } 
1391 #  if ($count == 0){
1392     my $query2="Select * from aqorders where biblionumber=$bibnum";
1393     my $sth2=$dbh->prepare($query2);
1394     $sth2->execute;
1395     if (my $data=$sth2->fetchrow_hashref){
1396       $ocount=$data->{'quantity'} - $data->{'quantityreceived'};
1397     }
1398 #    $count+=$ocount;
1399     $sth2->finish;
1400   $sth->finish; 
1401   $dbh->disconnect;                   
1402   return ($count,$lcount,$nacount,$fcount,$scount,$lostcount,$mending,$transit,$ocount); 
1403 }
1404
1405 sub ItemType {
1406   my ($type)=@_;
1407   my $dbh=C4Connect;
1408   my $query="select description from itemtypes where itemtype='$type'";
1409   my $sth=$dbh->prepare($query);
1410   $sth->execute;
1411   my $dat=$sth->fetchrow_hashref;
1412   $sth->finish;
1413   $dbh->disconnect;
1414   return ($dat->{'description'});
1415 }
1416
1417 sub bibitems {
1418   my ($bibnum)=@_;
1419   my $dbh=C4Connect;
1420   my $query="Select * from biblioitems,itemtypes,items where
1421   biblioitems.biblionumber='$bibnum' and biblioitems.itemtype=itemtypes.itemtype and
1422   biblioitems.biblioitemnumber=items.biblioitemnumber group by
1423   items.biblioitemnumber";
1424   my $sth=$dbh->prepare($query);
1425   $sth->execute;
1426   my $i=0;
1427   my @results;
1428   while (my $data=$sth->fetchrow_hashref){
1429     $results[$i]=$data;
1430     $i++;
1431   }
1432   $sth->finish;
1433   $dbh->disconnect;
1434   return($i,@results);
1435 }
1436
1437 sub barcodes{
1438   my ($biblioitemnumber)=@_;
1439   my $dbh=C4Connect;
1440   my $query="Select barcode from items where
1441    biblioitemnumber='$biblioitemnumber'";
1442   my $sth=$dbh->prepare($query);
1443   $sth->execute;
1444   my @barcodes;
1445   my $i=0;
1446   while (my $data=$sth->fetchrow_hashref){
1447     $barcodes[$i]=$data->{'barcode'};
1448     $i++;
1449   }
1450   $sth->finish;
1451   $dbh->disconnect;
1452   return(@barcodes);
1453   
1454 }
1455 END { }       # module clean-up code here (global destructor)
1456
1457
1458
1459
1460
1461