Making a "fix" to normal title search
[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)=FindReserves($biblionumber,'');   
911        if ($rescount >0){                                
912           $datedue='Request';
913        }
914     }
915     $isth->finish;
916     my $class = $data->{'classification'};
917     my $dewey = $data->{'dewey'};
918     $dewey =~ s/0+$//;
919     if ($dewey eq "000.") { $dewey = "";};    
920     if ($dewey < 10){$dewey='00'.$dewey;}
921     if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
922     if ($dewey <= 0){
923       $dewey='';
924     }
925     $dewey=~ s/\.$//;
926     $class = $class.$dewey;
927     if ($dewey ne ''){
928       $class = $class.$data->{'subclass'};
929     }
930  #   $results[$i]="$data->{'title'}\t$data->{'barcode'}\t$datedue\t$data->{'branchname'}\t$data->{'dewey'}";
931     my @temp=split('-',$data->{'datelastseen'});
932     my $date="$temp[2]/$temp[1]/$temp[0]";
933     $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'}";
934 #    print "$results[$i] <br>";
935     $i++;
936   }
937   $sth->finish;
938   $dbh->disconnect;
939   return(@results);
940 }
941
942 sub GetItems {
943    my ($env,$biblionumber)=@_;
944    #debug_msg($env,"GetItems");
945    my $dbh = &C4Connect;
946    my $query = "Select * from biblioitems where (biblionumber = $biblionumber)";
947    #debug_msg($env,$query);
948    my $sth=$dbh->prepare($query);
949    $sth->execute;
950    #debug_msg($env,"executed query");      
951    my $i=0;
952    my @results;
953    while (my $data=$sth->fetchrow_hashref) {
954       #debug_msg($env,$data->{'biblioitemnumber'});
955       my $dewey = $data->{'dewey'};
956       $dewey =~ s/0+$//; 
957       my $line = $data->{'biblioitemnumber'}."\t".$data->{'itemtype'};
958       $line = $line."\t$data->{'classification'}\t$dewey";
959       $line = $line."\t$data->{'subclass'}\t$data->{isbn}";
960       $line = $line."\t$data->{'volume'}\t$data->{number}";
961       my $isth= $dbh->prepare("select * from items where biblioitemnumber = $data->{'biblioitemnumber'}");
962       $isth->execute;
963       while (my $idata = $isth->fetchrow_hashref) {
964         my $iline = $idata->{'barcode'}."[".$idata->{'holdingbranch'}."[";
965         if ($idata->{'notforloan'} == 1) {
966           $iline = $iline."NFL ";
967         }
968         if ($idata->{'itemlost'} == 1) {
969           $iline = $iline."LOST ";
970         }        
971         $line = $line."\t$iline"; 
972       }
973       $isth->finish;
974       $results[$i] = $line;
975       $i++;      
976    }
977    $sth->finish;
978    $dbh->disconnect;
979    return(@results);
980 }            
981   
982 sub itemdata {
983   my ($barcode)=@_;
984   my $dbh=C4Connect;
985   my $query="Select * from items,biblioitems where barcode='$barcode'
986   and items.biblioitemnumber=biblioitems.biblioitemnumber";
987 #  print $query;
988   my $sth=$dbh->prepare($query);
989   $sth->execute;
990   my $data=$sth->fetchrow_hashref;
991   $sth->finish;
992   $dbh->disconnect;
993   return($data);
994 }
995
996 sub bibdata {
997   my ($bibnum,$type)=@_;
998   my $dbh=C4Connect;
999   my $query="Select *,biblio.notes  from biblio,biblioitems,bibliosubtitle where biblio.biblionumber=$bibnum
1000   and biblioitems.biblionumber=$bibnum and 
1001 (bibliosubtitle.biblionumber=$bibnum)"; 
1002 #  print $query;
1003   my $sth=$dbh->prepare($query);
1004   $sth->execute;
1005   my $data=$sth->fetchrow_hashref;
1006   $sth->finish;
1007   $query="Select * from bibliosubject where biblionumber='$bibnum'";
1008   $sth=$dbh->prepare($query);
1009   $sth->execute;
1010   while (my $dat=$sth->fetchrow_hashref){
1011     $data->{'subject'}.=" | $dat->{'subject'}";
1012
1013   }
1014   #print $query;
1015   $sth->finish;
1016   $dbh->disconnect;
1017   return($data);
1018 }
1019
1020 sub bibitemdata {
1021   my ($bibitem)=@_;
1022   my $dbh=C4Connect;
1023   my $query="Select *,biblioitems.notes as bnotes from biblio,biblioitems,itemtypes where biblio.biblionumber=
1024   biblioitems.biblionumber and biblioitemnumber=$bibitem and
1025   biblioitems.itemtype=itemtypes.itemtype";
1026 #  print $query;
1027   my $sth=$dbh->prepare($query);
1028   $sth->execute;
1029   my $data=$sth->fetchrow_hashref;
1030   $sth->finish;
1031   $dbh->disconnect;
1032   return($data);
1033 }
1034
1035 sub subject {
1036   my ($bibnum)=@_;
1037   my $dbh=C4Connect;
1038   my $query="Select * from bibliosubject where biblionumber=$bibnum";
1039   my $sth=$dbh->prepare($query);
1040   $sth->execute;
1041   my @results;
1042   my $i=0;
1043   while (my $data=$sth->fetchrow_hashref){
1044     $results[$i]=$data;
1045     $i++;
1046   }
1047   $sth->finish;
1048   $dbh->disconnect;
1049   return($i,\@results);
1050 }
1051
1052 sub addauthor {
1053   my ($bibnum)=@_;
1054   my $dbh=C4Connect;
1055   my $query="Select * from additionalauthors where biblionumber=$bibnum";
1056   my $sth=$dbh->prepare($query);
1057   $sth->execute;
1058   my @results;
1059   my $i=0;
1060   while (my $data=$sth->fetchrow_hashref){
1061     $results[$i]=$data;
1062     $i++;
1063   }
1064   $sth->finish;
1065   $dbh->disconnect;
1066   return($i,\@results);
1067 }
1068
1069 sub subtitle {
1070   my ($bibnum)=@_;
1071   my $dbh=C4Connect;
1072   my $query="Select * from bibliosubtitle where biblionumber=$bibnum";
1073   my $sth=$dbh->prepare($query);
1074   $sth->execute;
1075   my @results;
1076   my $i=0;
1077   while (my $data=$sth->fetchrow_hashref){
1078     $results[$i]=$data;
1079     $i++;
1080   }
1081   $sth->finish;
1082   $dbh->disconnect;
1083   return($i,\@results);
1084 }
1085
1086
1087
1088 sub itemissues {
1089   my ($bibitem,$biblio)=@_;
1090   my $dbh=C4Connect;
1091   my $query="Select * from items where 
1092   items.biblioitemnumber='$bibitem'";
1093   my $sth=$dbh->prepare($query) || die $dbh->errstr;
1094   $sth->execute || die $sth->errstr;
1095   my $i=0;
1096   my @results;
1097   while (my $data=$sth->fetchrow_hashref) {
1098     my $query2="select * from issues,borrowers where itemnumber=$data->{'itemnumber'}
1099     and returndate is NULL and issues.borrowernumber=borrowers.borrowernumber";
1100     my $sth2=$dbh->prepare($query2);
1101     $sth2->execute;
1102     if (my $data2=$sth2->fetchrow_hashref) {
1103       $data->{'date_due'}=$data2->{'date_due'};
1104       $data->{'card'}=$data2->{'cardnumber'};
1105     } else {
1106       if ($data->{'wthdrawn'} eq '1') {
1107         $data->{'date_due'}='Cancelled';
1108       } else {
1109           $data->{'date_due'}='Available';
1110       }
1111     }
1112     $sth2->finish;
1113     $query2="select * from issues,borrowers where itemnumber='$data->{'itemnumber'}'
1114     and issues.borrowernumber=borrowers.borrowernumber 
1115     order by date_due desc";
1116     my $sth2=$dbh->prepare($query2) || die $dbh->errstr;
1117     $sth2->execute || die $sth2->errstr;
1118     for (my $i2=0;$i2<2;$i2++){
1119       if (my $data2=$sth2->fetchrow_hashref){
1120         $data->{"timestamp$i2"}=$data2->{'timestamp'};
1121         $data->{"card$i2"}=$data2->{'cardnumber'};
1122         $data->{"borrower$i2"}=$data2->{'borrowernumber'};
1123       }
1124     }
1125     $sth2->finish;
1126     $results[$i]=$data;
1127     $i++;
1128   }
1129   $sth->finish;
1130   $dbh->disconnect;
1131   return(@results);
1132 }
1133
1134 sub itemnodata {
1135   my ($env,$dbh,$itemnumber) = @_;
1136   $dbh=C4Connect;
1137   my $query="Select * from biblio,items,biblioitems
1138     where items.itemnumber = '$itemnumber'
1139     and biblio.biblionumber = items.biblionumber
1140     and biblioitems.biblioitemnumber = items.biblioitemnumber";
1141   my $sth=$dbh->prepare($query);
1142 #  print $query;
1143   $sth->execute;
1144   my $data=$sth->fetchrow_hashref;
1145   $sth->finish;  
1146   $dbh->disconnect;
1147   return($data);               
1148 }
1149
1150 #used by member enquiries from the intranet
1151 #called by member.pl
1152 sub BornameSearch  {
1153   my ($env,$searchstring,$type)=@_;
1154   my $dbh = &C4Connect;
1155   $searchstring=~ s/\'/\\\'/g;
1156   my @data=split(' ',$searchstring);
1157   my $count=@data;
1158   my $query="Select * from borrowers 
1159   where ((surname like \"$data[0]%\" or surname like \"% $data[0]%\" 
1160   or firstname  like \"$data[0]%\" or firstname like \"% $data[0]%\" 
1161   or othernames like \"$data[0]%\" or othernames like \"% $data[0]%\")
1162   ";
1163   for (my $i=1;$i<$count;$i++){
1164     $query=$query." and (surname like \"$data[$i]%\" or surname like \"% $data[$i]%\"                  
1165     or firstname  like \"$data[$i]%\" or firstname like \"% $data[$i]%\"                    
1166     or othernames like \"$data[$i]%\" or othernames like \"% $data[$i]%\")";
1167   }
1168   $query=$query.") or cardnumber = \"$searchstring\"
1169   order by surname,firstname";
1170 #  print $query,"\n";
1171   my $sth=$dbh->prepare($query);
1172   $sth->execute;
1173   my @results;
1174   my $cnt=0;
1175   while (my $data=$sth->fetchrow_hashref){
1176     push(@results,$data);
1177     $cnt ++;
1178   }
1179 #  $sth->execute;
1180   $sth->finish;
1181   $dbh->disconnect;
1182   return ($cnt,\@results);
1183 }
1184
1185 sub borrdata {
1186   my ($cardnumber,$bornum)=@_;
1187   $cardnumber = uc $cardnumber;
1188   my $dbh=C4Connect;
1189   my $query;
1190   if ($bornum eq ''){
1191     $query="Select * from borrowers where cardnumber='$cardnumber'";
1192   } else {
1193       $query="Select * from borrowers where borrowernumber='$bornum'";
1194   }
1195   #print $query;
1196   my $sth=$dbh->prepare($query);
1197   $sth->execute;
1198   my $data=$sth->fetchrow_hashref;
1199   $sth->finish;
1200   $dbh->disconnect;
1201   return($data);
1202 }
1203
1204 sub borrissues {
1205   my ($bornum)=@_;
1206   my $dbh=C4Connect;
1207   my $query;
1208   $query="Select * from issues,biblio,items where borrowernumber='$bornum' and
1209 items.itemnumber=issues.itemnumber and
1210 items.biblionumber=biblio.biblionumber and issues.returndate is NULL order
1211 by date_due";
1212   #print $query;
1213   my $sth=$dbh->prepare($query);
1214     $sth->execute;
1215   my @result;
1216   my $i=0;
1217   while (my $data=$sth->fetchrow_hashref){
1218     $result[$i]=$data;;
1219     $i++;
1220   }
1221   $sth->finish;
1222   $dbh->disconnect;
1223   return($i,\@result);
1224 }
1225
1226 sub allissues { 
1227   my ($bornum,$order,$limit)=@_; 
1228   my $dbh=C4Connect;   
1229   my $query;     
1230   $query="Select * from issues,biblio,items,biblioitems       
1231   where borrowernumber='$bornum' and         
1232   items.biblioitemnumber=biblioitems.biblioitemnumber and           
1233   items.itemnumber=issues.itemnumber and             
1234   items.biblionumber=biblio.biblionumber";               
1235   $query.=" order by $order";                 
1236   if ($limit !=0){                   
1237     $query.=" limit $limit";                     
1238   }                         
1239   #print $query;                           
1240   my $sth=$dbh->prepare($query);          
1241   $sth->execute;
1242   my @result;   
1243   my $i=0;    
1244   while (my $data=$sth->fetchrow_hashref){                                      
1245     $result[$i]=$data;; 
1246     $i++;     
1247   }         
1248   $sth->finish;           
1249   $dbh->disconnect;             
1250   return($i,\@result);               
1251 }
1252
1253 sub borrdata2 {
1254   my ($env,$bornum)=@_;
1255   my $dbh=C4Connect;
1256   my $query="Select count(*) from issues where borrowernumber='$bornum' and
1257     returndate is NULL";
1258     # print $query;
1259   my $sth=$dbh->prepare($query);
1260   $sth->execute;
1261   my $data=$sth->fetchrow_hashref;
1262   $sth->finish;
1263   $sth=$dbh->prepare("Select count(*) from issues where
1264     borrowernumber='$bornum' and date_due < now() and returndate is NULL");
1265   $sth->execute;
1266   my $data2=$sth->fetchrow_hashref;
1267   $sth->finish;
1268   $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
1269     borrowernumber='$bornum'");
1270   $sth->execute;
1271   my $data3=$sth->fetchrow_hashref;
1272   $sth->finish;
1273   $dbh->disconnect;
1274
1275 return($data2->{'count(*)'},$data->{'count(*)'},$data3->{'sum(amountoutstanding)'});
1276 }
1277                   
1278 sub getacctlist {
1279    my ($env,$params) = @_;
1280    my $dbh=C4Connect;
1281    my @acctlines;
1282    my $numlines;
1283    my $query = "Select borrowernumber, accountno, date, amount, description,
1284       dispute, accounttype, amountoutstanding, barcode, title
1285       from accountlines,items,biblio   
1286       where borrowernumber = $params->{'borrowernumber'} ";
1287    if ($params->{'acctno'} ne "") {
1288       my $query = $query." and accountlines.accountno = $params->{'acctno'} ";
1289       }
1290    my $query = $query." and accountlines.itemnumber = items.itemnumber
1291       and items.biblionumber = biblio.biblionumber
1292       and accountlines.amountoutstanding<>0 order by date";
1293    my $sth=$dbh->prepare($query);
1294 #   print $query;
1295    $sth->execute;
1296    my $total=0;
1297    while (my $data=$sth->fetchrow_hashref){
1298       $acctlines[$numlines] = $data;
1299       $numlines++;
1300       $total = $total+ $data->{'amountoutstanding'};
1301    }
1302    return ($numlines,\@acctlines,$total);
1303    $sth->finish;
1304    $dbh->disconnect;
1305 }
1306
1307 sub getboracctrecord {
1308    my ($env,$params) = @_;
1309    my $dbh=C4Connect;
1310    my @acctlines;
1311    my $numlines=0;
1312    my $query= "Select * from accountlines where
1313 borrowernumber=$params->{'borrowernumber'} order by date desc,timestamp desc";
1314    my $sth=$dbh->prepare($query);
1315 #   print $query;
1316    $sth->execute;
1317    my $total=0;
1318    while (my $data=$sth->fetchrow_hashref){
1319 #      if ($data->{'itemnumber'} ne ''){
1320 #        $query="Select * from items,biblio where items.itemnumber=
1321 #       '$data->{'itemnumber'}' and biblio.biblionumber=items.biblionumber";
1322 #       my $sth2=$dbh->prepare($query);
1323 #       $sth2->execute;
1324 #       my $data2=$sth2->fetchrow_hashref;
1325 #       $sth2->finish;
1326 #       $data=$data2;
1327  #     }
1328       $acctlines[$numlines] = $data;
1329       $numlines++;
1330       $total = $total+ $data->{'amountoutstanding'};
1331    }
1332    $sth->finish;
1333    $dbh->disconnect;
1334    return ($numlines,\@acctlines,$total);
1335 }
1336
1337 sub itemcount { 
1338   my ($env,$bibnum,$type)=@_; 
1339   my $dbh=C4Connect;   
1340   my $query="Select * from items where     
1341   biblionumber=$bibnum ";
1342   if ($type ne 'intra'){
1343     $query.=" and (itemlost <>1 or itemlost is NULL) and
1344     (wthdrawn <> 1 or wthdrawn is NULL)";      
1345   }
1346   my $sth=$dbh->prepare($query);         
1347   #  print $query;           
1348   $sth->execute;           
1349   my $count=0;             
1350   my $lcount=0;               
1351   my $nacount=0;                 
1352   my $fcount=0;
1353   my $scount=0;
1354   my $lostcount=0;
1355   my $mending=0;
1356   my $transit=0;
1357   my $ocount=0;
1358   while (my $data=$sth->fetchrow_hashref){
1359     $count++;                     
1360     my $query2="select * from issues,items where issues.itemnumber=                          
1361     '$data->{'itemnumber'}' and returndate is NULL
1362     and items.itemnumber=issues.itemnumber and (items.itemlost <>1 or
1363     items.itemlost is NULL)"; 
1364     my $sth2=$dbh->prepare($query2);     
1365     $sth2->execute;         
1366     if (my $data2=$sth2->fetchrow_hashref){         
1367        $nacount++;         
1368     } else {         
1369       if ($data->{'holdingbranch'} eq 'C'){         
1370         $lcount++;               
1371       }                       
1372       if ($data->{'holdingbranch'} eq 'F' || $data->{'holdingbranch'} eq 'FP'){         
1373         $fcount++;               
1374       }                       
1375       if ($data->{'holdingbranch'} eq 'S' || $data->{'holdingbranch'} eq 'SP'){         
1376         $scount++;               
1377       }                       
1378       if ($data->{'itemlost'} eq '1'){
1379         $lostcount++;
1380       }
1381       if ($data->{'holdingbranch'} eq 'FM'){
1382         $mending++;
1383       }
1384       if ($data->{'holdingbranch'} eq 'TR'){
1385         $transit++;
1386       }
1387     }                             
1388     $sth2->finish;     
1389   } 
1390 #  if ($count == 0){
1391     my $query2="Select * from aqorders where biblionumber=$bibnum";
1392     my $sth2=$dbh->prepare($query2);
1393     $sth2->execute;
1394     if (my $data=$sth2->fetchrow_hashref){
1395       $ocount=$data->{'quantity'} - $data->{'quantityreceived'};
1396     }
1397 #    $count+=$ocount;
1398     $sth2->finish;
1399   $sth->finish; 
1400   $dbh->disconnect;                   
1401   return ($count,$lcount,$nacount,$fcount,$scount,$lostcount,$mending,$transit,$ocount); 
1402 }
1403
1404 sub ItemType {
1405   my ($type)=@_;
1406   my $dbh=C4Connect;
1407   my $query="select description from itemtypes where itemtype='$type'";
1408   my $sth=$dbh->prepare($query);
1409   $sth->execute;
1410   my $dat=$sth->fetchrow_hashref;
1411   $sth->finish;
1412   $dbh->disconnect;
1413   return ($dat->{'description'});
1414 }
1415
1416 sub bibitems {
1417   my ($bibnum)=@_;
1418   my $dbh=C4Connect;
1419   my $query="Select * from biblioitems,itemtypes,items where
1420   biblioitems.biblionumber='$bibnum' and biblioitems.itemtype=itemtypes.itemtype and
1421   biblioitems.biblioitemnumber=items.biblioitemnumber group by
1422   items.biblioitemnumber";
1423   my $sth=$dbh->prepare($query);
1424   $sth->execute;
1425   my $i=0;
1426   my @results;
1427   while (my $data=$sth->fetchrow_hashref){
1428     $results[$i]=$data;
1429     $i++;
1430   }
1431   $sth->finish;
1432   $dbh->disconnect;
1433   return($i,@results);
1434 }
1435
1436 sub barcodes{
1437   my ($biblioitemnumber)=@_;
1438   my $dbh=C4Connect;
1439   my $query="Select barcode from items where
1440    biblioitemnumber='$biblioitemnumber'";
1441   my $sth=$dbh->prepare($query);
1442   $sth->execute;
1443   my @barcodes;
1444   my $i=0;
1445   while (my $data=$sth->fetchrow_hashref){
1446     $barcodes[$i]=$data->{'barcode'};
1447     $i++;
1448   }
1449   $sth->finish;
1450   $dbh->disconnect;
1451   return(@barcodes);
1452   
1453 }
1454 END { }       # module clean-up code here (global destructor)
1455
1456
1457
1458
1459
1460