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