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