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