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