Few more changes... not much to look at yet, still wrapping my head around the
[koha.git] / C4 / Acquisitions.pm
1 package C4::Acquisitions; #asummes C4/Acquisitions.pm
2
3 use strict;
4 require Exporter;
5 use C4::Database;
6
7 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
8
9 # set the version for version checking
10 $VERSION = 0.01;
11
12 @ISA = qw(Exporter);
13 @EXPORT = qw(&getorders &bookseller &breakdown &basket &newbasket &bookfunds
14 &ordersearch &newbiblio &newbiblioitem &newsubject &newsubtitle &neworder
15 &newordernum &modbiblio &modorder &getsingleorder &invoice &receiveorder
16 &bookfundbreakdown &curconvert &updatesup &insertsup &newitems &modbibitem
17 &getcurrencies &modsubtitle &modsubject &modaddauthor &moditem &countitems 
18 &findall &needsmod &delitem &deletebiblioitem &delbiblio &delorder &branches
19 &getallorders &getrecorders &updatecurrencies &getorder &getcurrency &updaterecorder
20 &updatecost &checkitems &modnote &getitemtypes &getbiblio
21 &getbiblioitem &getitemsbybiblioitem &isbnsearch &keywordsearch
22 &websitesearch &addwebsite &updatewebsite &deletewebsite);
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
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
60 sub getorders {
61   my ($supplierid)=@_;
62   my $dbh=C4Connect;
63   my $query = "Select count(*),authorisedby,entrydate,basketno from aqorders where 
64   booksellerid='$supplierid' and (quantity > quantityreceived or
65   quantityreceived is NULL)
66   and (datecancellationprinted is NULL or datecancellationprinted = '0000-00-00')";
67   $query.=" group by basketno order by entrydate desc";
68   #print $query;
69   my $sth=$dbh->prepare($query);
70   $sth->execute;
71   my @results;
72   my $i=0;
73   while (my $data=$sth->fetchrow_hashref){
74     $results[$i]=$data;
75     $i++;
76   }
77   $sth->finish;
78   $dbh->disconnect;
79   return ($i,\@results);
80 }
81
82 sub itemcount{
83   my ($biblio)=@_;
84   my $dbh=C4Connect;
85   my $query="Select count(*) from items where biblionumber=$biblio";
86 #  print $query;
87   my $sth=$dbh->prepare($query);
88   $sth->execute;
89   my $data=$sth->fetchrow_hashref;
90   $sth->finish;
91   $dbh->disconnect;
92   return($data->{'count(*)'});
93 }
94
95 sub getorder{
96   my ($bi,$bib)=@_;
97   my $dbh=C4Connect;
98   my $query="Select ordernumber from aqorders where biblionumber=$bib and
99   biblioitemnumber='$bi'";
100   my $sth=$dbh->prepare($query);
101   $sth->execute;
102   my $ordnum=$sth->fetchrow_hashref;
103   $sth->finish;
104   my $order=getsingleorder($ordnum->{'ordernumber'});
105   $dbh->disconnect;
106 #  print $query;
107   return ($order,$ordnum->{'ordernumber'});
108 }
109
110 sub getsingleorder {
111   my ($ordnum)=@_;
112   my $dbh=C4Connect;
113   my $query="Select * from biblio,biblioitems,aqorders,aqorderbreakdown 
114   where aqorders.ordernumber='$ordnum' 
115   and biblio.biblionumber=aqorders.biblionumber and
116   biblioitems.biblioitemnumber=aqorders.biblioitemnumber and
117   aqorders.ordernumber=aqorderbreakdown.ordernumber";
118   my $sth=$dbh->prepare($query);
119   $sth->execute;
120   my $data=$sth->fetchrow_hashref;
121   $sth->finish;
122   $dbh->disconnect;
123   return($data);
124 }
125
126 sub invoice {
127   my ($invoice)=@_;
128   my $dbh=C4Connect;
129   my $query="Select * from aqorders,biblio,biblioitems where
130   booksellerinvoicenumber='$invoice'
131   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=
132   aqorders.biblioitemnumber group by aqorders.ordernumber,aqorders.biblioitemnumber";
133   my $i=0;
134   my @results;
135   my $sth=$dbh->prepare($query);
136   $sth->execute;
137   while (my $data=$sth->fetchrow_hashref){
138     $results[$i]=$data;
139     $i++;
140   }
141   $sth->finish;
142   $dbh->disconnect;
143   return($i,@results);
144 }
145
146 sub getallorders {
147   #gets all orders from a certain supplier, orders them alphabetically
148   my ($supid)=@_;
149   my $dbh=C4Connect;
150   my $query="Select * from aqorders,biblio,biblioitems where booksellerid='$supid'
151   and (cancelledby is NULL or cancelledby = '')
152   and (quantityreceived < quantity or quantityreceived is NULL)
153   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=
154   aqorders.biblioitemnumber
155   group by aqorders.biblioitemnumber
156   order by
157   biblio.title";
158   my $i=0;
159   my @results;
160   my $sth=$dbh->prepare($query);
161   $sth->execute;
162   while (my $data=$sth->fetchrow_hashref){
163     $results[$i]=$data;
164     $i++;
165   }
166   $sth->finish;
167   $dbh->disconnect;
168   return($i,@results);
169 }
170
171 sub getrecorders {
172   #gets all orders from a certain supplier, orders them alphabetically
173   my ($supid)=@_;
174   my $dbh=C4Connect;
175   my $query="Select * from aqorders,biblio,biblioitems where booksellerid='$supid'
176   and (cancelledby is NULL or cancelledby = '')
177   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=                    
178   aqorders.biblioitemnumber and
179   aqorders.quantityreceived>0
180   and aqorders.datereceived >=now()
181   group by aqorders.biblioitemnumber 
182   order by
183   biblio.title";
184   my $i=0;
185   my @results;
186   my $sth=$dbh->prepare($query);
187   $sth->execute;
188   while (my $data=$sth->fetchrow_hashref){
189     $results[$i]=$data;
190     $i++;
191   }
192   $sth->finish;
193   $dbh->disconnect;
194   return($i,@results);
195 }
196
197 sub ordersearch {
198   my ($search,$biblio,$catview) = @_;
199   my $dbh   = C4Connect;
200   my $query = "Select *,biblio.title from aqorders,biblioitems,biblio
201 where aqorders.biblioitemnumber = biblioitems.biblioitemnumber
202 and biblio.biblionumber=aqorders.biblionumber
203 and ((datecancellationprinted is NULL)
204 or (datecancellationprinted = '0000-00-00'))
205 and ((";
206   my @data  = split(' ',$search);
207   my $count = @data;
208   for (my $i = 0; $i < $count; $i++) {
209     $query .= "(biblio.title like '$data[$i]%' or biblio.title like '% $data[$i]%') and ";
210   }
211   $query=~ s/ and $//;
212   $query.=" ) or biblioitems.isbn='$search'
213   or (aqorders.ordernumber='$search' and aqorders.biblionumber='$biblio')) ";
214   if ($catview ne 'yes'){
215     $query.=" and (quantityreceived < quantity or quantityreceived is NULL)";
216   }
217   $query.=" group by aqorders.ordernumber";
218   my $sth=$dbh->prepare($query);
219   $sth->execute;
220   my $i=0;
221   my @results;
222   while (my $data=$sth->fetchrow_hashref){
223      my $sth2=$dbh->prepare("Select * from biblio where
224      biblionumber='$data->{'biblionumber'}'");
225      $sth2->execute;
226      my $data2=$sth2->fetchrow_hashref;
227      $sth2->finish;
228      $data->{'author'}=$data2->{'author'};
229      $data->{'seriestitle'}=$data2->{'seriestitle'};
230      $sth2=$dbh->prepare("Select * from aqorderbreakdown where
231     ordernumber=$data->{'ordernumber'}");
232     $sth2->execute;
233     $data2=$sth2->fetchrow_hashref;
234     $sth2->finish;
235     $data->{'branchcode'}=$data2->{'branchcode'};
236     $data->{'bookfundid'}=$data2->{'bookfundid'};
237     $results[$i]=$data;
238     $i++;
239   }
240   $sth->finish;
241   $dbh->disconnect;
242   return($i,@results);
243 }
244
245
246 sub bookseller {
247   my ($searchstring)=@_;
248   my $dbh=C4Connect;
249   my $query="Select * from aqbooksellers where name like '%$searchstring%' or
250   id = '$searchstring'";
251   my $sth=$dbh->prepare($query);
252   $sth->execute;
253   my @results;
254   my $i=0;
255   while (my $data=$sth->fetchrow_hashref){
256     $results[$i]=$data;
257     $i++;
258   }
259   $sth->finish;
260   $dbh->disconnect;
261   return($i,@results);
262 }
263
264 sub breakdown {
265   my ($id)=@_;
266   my $dbh=C4Connect;
267   my $query="Select * from aqorderbreakdown where ordernumber='$id'";
268   my $sth=$dbh->prepare($query);
269   $sth->execute;
270   my @results;
271   my $i=0;
272   while (my $data=$sth->fetchrow_hashref){
273     $results[$i]=$data;
274     $i++;
275   }
276   $sth->finish;
277   $dbh->disconnect;
278   return($i,\@results);
279 }
280
281 sub basket {
282   my ($basketno,$supplier)=@_;
283   my $dbh=C4Connect;
284   my $query="Select *,biblio.title from aqorders,biblio,biblioitems 
285   where basketno='$basketno'
286   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber
287   =aqorders.biblioitemnumber 
288   and (datecancellationprinted is NULL or datecancellationprinted =
289   '0000-00-00')";
290   if ($supplier ne ''){
291     $query.=" and aqorders.booksellerid='$supplier'";
292   }
293   $query.=" group by aqorders.ordernumber";
294   my $sth=$dbh->prepare($query);
295   $sth->execute;
296   my @results;
297 #  print $query;
298   my $i=0;
299   while (my $data=$sth->fetchrow_hashref){
300     $results[$i]=$data;
301     $i++;
302   }
303   $sth->finish;
304   $dbh->disconnect;
305   return($i,@results);
306 }
307
308 sub newbasket {
309   my $dbh=C4Connect;
310   my $query="Select max(basketno) from aqorders";
311   my $sth=$dbh->prepare($query);
312   $sth->execute;
313   my $data=$sth->fetchrow_arrayref;
314   my $basket=$$data[0];
315   $basket++;
316   $sth->finish;
317   $dbh->disconnect;
318   return($basket);
319 }
320
321 sub bookfunds {
322   my $dbh=C4Connect;
323   my $query="Select * from aqbookfund,aqbudget where aqbookfund.bookfundid
324   =aqbudget.bookfundid
325   group by aqbookfund.bookfundid order by bookfundname";
326   my $sth=$dbh->prepare($query);
327   $sth->execute;
328   my @results;
329   my $i=0;
330   while (my $data=$sth->fetchrow_hashref){
331     $results[$i]=$data;
332     $i++;
333   }
334   $sth->finish;
335   $dbh->disconnect;
336   return($i,@results);
337 }
338
339 sub branches {
340     my $dbh   = C4Connect;
341     my $query = "Select * from branches";
342     my $sth   = $dbh->prepare($query);
343     my $i     = 0;
344     my @results;
345
346     $sth->execute;
347     while (my $data = $sth->fetchrow_hashref) {
348         $results[$i] = $data;
349         $i++;
350     } # while
351
352     $sth->finish;
353     $dbh->disconnect;
354     return($i, @results);
355 } # sub branches
356
357 sub bookfundbreakdown {
358   my ($id)=@_;
359   my $dbh=C4Connect;
360   my $query="Select quantity,datereceived,freight,unitprice,listprice,ecost,quantityreceived,subscription
361   from aqorders,aqorderbreakdown where bookfundid='$id' and 
362   aqorders.ordernumber=aqorderbreakdown.ordernumber
363   and (datecancellationprinted is NULL or
364   datecancellationprinted='0000-00-00')";
365   my $sth=$dbh->prepare($query);
366   $sth->execute;
367   my $comtd=0;
368   my $spent=0;
369   while (my $data=$sth->fetchrow_hashref){
370     if ($data->{'subscription'} == 1){
371       $spent+=$data->{'quantity'}*$data->{'unitprice'};
372     } else {
373       my $leftover=$data->{'quantity'}-$data->{'quantityreceived'};
374       $comtd+=($data->{'ecost'})*$leftover;
375       $spent+=($data->{'unitprice'})*$data->{'quantityreceived'};
376     }
377   }
378   $sth->finish;
379   $dbh->disconnect;
380   return($spent,$comtd);
381 }
382       
383
384 sub newbiblio {
385   my ($biblio) = @_;
386   my $dbh    = &C4Connect;
387   my $query  = "Select max(biblionumber) from biblio";
388   my $sth    = $dbh->prepare($query);
389   $sth->execute;
390   my $data   = $sth->fetchrow_arrayref;
391   my $bibnum = $$data[0] + 1;
392   my $series = 0;
393
394   $biblio->{'title'}       = $dbh->quote($biblio->{'title'});
395   $biblio->{'author'}      = $dbh->quote($biblio->{'author'});
396   $biblio->{'copyright'}   = $dbh->quote($biblio->{'copyright'});
397   $biblio->{'seriestitle'} = $dbh->quote($biblio->{'seriestitle'});
398   $biblio->{'notes'}       = $dbh->quote($biblio->{'notes'});
399   $biblio->{'abstract'}    = $dbh->quote($biblio->{'abstract'});
400   if ($biblio->{'seriestitle'}) { $series = 1 };
401
402   $sth->finish;
403   $query = "insert into biblio set
404 biblionumber  = $bibnum,
405 title         = $biblio->{'title'},
406 author        = $biblio->{'author'},
407 copyrightdate = $biblio->{'copyright'},
408 serial        = $series,
409 seriestitle   = $biblio->{'seriestitle'},
410 notes         = $biblio->{'notes'},
411 abstract      = $biblio->{'abstract'}";
412
413   $sth = $dbh->prepare($query);
414   $sth->execute;
415
416   $sth->finish;
417   $dbh->disconnect;
418   return($bibnum);
419 }
420
421
422 sub modbiblio {
423   my ($biblio) = @_;
424   my $dbh   = C4Connect;
425   my $query;
426   my $sth;
427   
428   $biblio->{'title'}         = $dbh->quote($biblio->{'title'});
429   $biblio->{'author'}        = $dbh->quote($biblio->{'author'});
430   $biblio->{'abstract'}      = $dbh->quote($biblio->{'abstract'});
431   $biblio->{'copyrightdate'} = $dbh->quote($biblio->{'copyrightdate'});
432   $biblio->{'seriestitle'}   = $dbh->quote($biblio->{'serirestitle'});
433   $biblio->{'serial'}        = $dbh->quote($biblio->{'serial'});
434   $biblio->{'unititle'}      = $dbh->quote($biblio->{'unititle'});
435   $biblio->{'notes'}         = $dbh->quote($biblio->{'notes'});
436
437   $query = "Update biblio set
438 title         = $biblio->{'title'},
439 author        = $biblio->{'author'},
440 abstract      = $biblio->{'abstract'},
441 copyrightdate = $biblio->{'copyrightdate'},
442 seriestitle   = $biblio->{'seriestitle'},
443 serial        = $biblio->{'serial'},
444 unititle      = $biblio->{'unititle'},
445 notes         = $biblio->{'notes'}
446 where biblionumber = $biblio->{'biblionumber'}";
447   $sth   = $dbh->prepare($query);
448
449   $sth->execute;
450
451   $sth->finish;
452   $dbh->disconnect;
453   return($biblio->{'biblionumber'});
454 } # sub modbiblio
455
456
457 sub modsubtitle {
458   my ($bibnum, $subtitle) = @_;
459   my $dbh   = C4Connect;
460   my $query = "update bibliosubtitle set
461 subtitle = '$subtitle'
462 where biblionumber = $bibnum";
463   my $sth   = $dbh->prepare($query);
464
465   $sth->execute;
466   $sth->finish;
467   $dbh->disconnect;
468 } # sub modsubtitle
469
470
471 sub modaddauthor {
472     my ($bibnum, $author) = @_;
473     my $dbh   = C4Connect;
474     my $query = "Delete from additionalauthors where biblionumber = $bibnum";
475     my $sth = $dbh->prepare($query);
476
477     $sth->execute;
478     $sth->finish;
479
480     if ($author ne '') {
481         $query = "Insert into additionalauthors set
482 author       = '$author',
483 biblionumber = '$bibnum'";
484         $sth   = $dbh->prepare($query);
485
486         $sth->execute;
487
488         $sth->finish;
489     } # if
490
491   $dbh->disconnect;
492 } # sub modaddauthor
493
494
495 sub modsubject {
496   my ($bibnum, $force, @subject) = @_;
497   my $dbh   = C4Connect;
498   my $count = @subject;
499   my $error;
500   for (my $i = 0; $i < $count; $i++) {
501     $subject[$i] =~ s/^ //g;
502     $subject[$i] =~ s/ $//g;
503     my $query = "select * from catalogueentry
504 where entrytype = 's'
505 and catalogueentry = '$subject[$i]'";
506     my $sth   = $dbh->prepare($query);
507     $sth->execute;
508
509     if (my $data = $sth->fetchrow_hashref) {
510     } else {
511       if ($force eq $subject[$i]) {
512
513          # subject not in aut, chosen to force anway
514          # so insert into cataloguentry so its in auth file
515          $query = "Insert into catalogueentry
516 (entrytype,catalogueentry)
517 values ('s','$subject[$i]')";
518          my $sth2 = $dbh->prepare($query);
519
520          $sth2->execute;
521          $sth2->finish;
522
523       } else {
524
525         $error = "$subject[$i]\n does not exist in the subject authority file";
526         $query = "Select * from catalogueentry
527 where entrytype = 's'
528 and (catalogueentry like '$subject[$i] %'
529 or catalogueentry like '% $subject[$i] %'
530 or catalogueentry like '% $subject[$i]')";
531         my $sth2 = $dbh->prepare($query);
532
533         $sth2->execute;
534         while (my $data = $sth2->fetchrow_hashref) {
535           $error = $error."<br>$data->{'catalogueentry'}";
536         } # while
537         $sth2->finish;
538       } # else
539     } # else
540     $sth->finish;
541   } # else
542
543   if ($error eq '') {
544     my $query = "Delete from bibliosubject where biblionumber = $bibnum";
545     my $sth   = $dbh->prepare($query);
546
547     $sth->execute;
548     $sth->finish;
549
550     for (my $i = 0; $i < $count; $i++) {
551       $sth = $dbh->prepare("Insert into bibliosubject
552 values ('$subject[$i]', $bibnum)");
553
554       $sth->execute;
555       $sth->finish;
556     } # for
557   } # if
558
559   $dbh->disconnect;
560   return($error);
561 } # sub modsubject
562
563
564 sub modbibitem {
565     my ($biblioitem) = @_;
566     my $dbh   = C4Connect;
567     my $query;
568
569     $biblioitem->{'itemtype'}        = $dbh->quote($biblioitem->{'itemtype'});
570     $biblioitem->{'url'}             = $dbh->quote($biblioitem->{'url'});
571     $biblioitem->{'isbn'}            = $dbh->quote($biblioitem->{'isbn'});
572     $biblioitem->{'publishercode'}   = $dbh->quote($biblioitem->{'publishercode'});
573     $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
574     $biblioitem->{'classification'}  = $dbh->quote($biblioitem->{'classification'});
575     $biblioitem->{'dewey'}           = $dbh->quote($biblioitem->{'dewey'});
576     $biblioitem->{'subclass'}        = $dbh->quote($biblioitem->{'subclass'});
577     $biblioitem->{'illus'}           = $dbh->quote($biblioitem->{'illus'});
578     $biblioitem->{'pages'}           = $dbh->quote($biblioitem->{'pages'});
579     $biblioitem->{'volumeddesc'}     = $dbh->quote($biblioitem->{'volumeddesc'});
580     $biblioitem->{'notes'}           = $dbh->quote($biblioitem->{'notes'});
581     $biblioitem->{'size'}            = $dbh->quote($biblioitem->{'size'});
582     $biblioitem->{'place'}           = $dbh->quote($biblioitem->{'place'});
583
584     $query = "Update biblioitems set
585 itemtype        = $biblioitem->{'itemtype'},
586 url             = $biblioitem->{'url'},
587 isbn            = $biblioitem->{'isbn'},
588 publishercode   = $biblioitem->{'publishercode'},
589 publicationyear = $biblioitem->{'publicationyear'},
590 classification  = $biblioitem->{'classification'},
591 dewey           = $biblioitem->{'dewey'},
592 subclass        = $biblioitem->{'subclass'},
593 illus           = $biblioitem->{'illus'},
594 pages           = $biblioitem->{'pages'},
595 volumeddesc     = $biblioitem->{'volumeddesc'},
596 notes           = $biblioitem->{'notes'},
597 size            = $biblioitem->{'size'},
598 place           = $biblioitem->{'place'}
599 where biblioitemnumber = $biblioitem->{'biblioitemnumber'}";
600
601     $dbh->do($query);
602
603     $dbh->disconnect;
604 } # sub modbibitem
605
606
607 sub modnote {
608   my ($bibitemnum,$note)=@_;
609   my $dbh=C4Connect;
610   my $query="update biblioitems set notes='$note' where
611   biblioitemnumber='$bibitemnum'";
612   my $sth=$dbh->prepare($query);
613   $sth->execute;
614   $sth->finish;
615   $dbh->disconnect;
616 }
617
618 sub newbiblioitem {
619   my ($biblioitem) = @_;
620   my $dbh   = C4Connect;
621   my $query = "Select max(biblioitemnumber) from biblioitems";
622   my $sth   = $dbh->prepare($query);
623   my $data;
624   my $bibitemnum;
625
626   $biblioitem->{'volume'}          = $dbh->quote($biblioitem->{'volume'});
627   $biblioitem->{'number'}          = $dbh->quote($biblioitem->{'number'});
628   $biblioitem->{'classification'}  = $dbh->quote($biblioitem->{'classification'});
629   $biblioitem->{'itemtype'}        = $dbh->quote($biblioitem->{'itemtype'});
630   $biblioitem->{'url'}             = $dbh->quote($biblioitem->{'url'});
631   $biblioitem->{'isbn'}            = $dbh->quote($biblioitem->{'isbn'});
632   $biblioitem->{'issn'}            = $dbh->quote($biblioitem->{'issn'});
633   $biblioitem->{'dewey'}           = $dbh->quote($biblioitem->{'dewey'});
634   $biblioitem->{'subclass'}        = $dbh->quote($biblioitem->{'subclass'});
635   $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
636   $biblioitem->{'publishercode'}   = $dbh->quote($biblioitem->{'publishercode'});
637   $biblioitem->{'volumedate'}      = $dbh->quote($biblioitem->{'volumedate'});
638   $biblioitem->{'volumeddesc'}     = $dbh->quote($biblioitem->{'volumeddesc'});  $biblioitem->{'illus'}            = $dbh->quote($biblioitem->{'illus'});
639   $biblioitem->{'illus'}           = $dbh->quote($biblioitem->{'illus'});
640   $biblioitem->{'pages'}           = $dbh->quote($biblioitem->{'pages'});
641   $biblioitem->{'notes'}           = $dbh->quote($biblioitem->{'notes'});
642   $biblioitem->{'size'}            = $dbh->quote($biblioitem->{'size'});
643   $biblioitem->{'place'}           = $dbh->quote($biblioitem->{'place'});
644   
645   $sth->execute;
646   $data       = $sth->fetchrow_arrayref;
647   $bibitemnum = $$data[0] + 1;
648
649   $sth->finish;
650
651   $query = "insert into biblioitems set
652 biblioitemnumber = $bibitemnum,
653 biblionumber     = $biblioitem->{'biblionumber'},
654 volume           = $biblioitem->{'volume'},
655 number           = $biblioitem->{'number'},
656 classification   = $biblioitem->{'classification'},
657 itemtype         = $biblioitem->{'itemtype'},
658 url              = $biblioitem->{'url'},
659 isbn             = $biblioitem->{'isbn'},
660 issn             = $biblioitem->{'issn'},
661 dewey            = $biblioitem->{'dewey'},
662 subclass         = $biblioitem->{'subclass'},
663 publicationyear  = $biblioitem->{'publicationyear'},
664 publishercode    = $biblioitem->{'publishercode'},
665 volumedate       = $biblioitem->{'volumedate'},
666 volumeddesc      = $biblioitem->{'volumeddesc'},
667 illus            = $biblioitem->{'illus'},
668 pages            = $biblioitem->{'pages'},
669 notes            = $biblioitem->{'notes'},
670 size             = $biblioitem->{'size'},
671 place            = $biblioitem->{'place'}";
672
673   $sth = $dbh->prepare($query);
674   $sth->execute;
675
676   $sth->finish;
677   $dbh->disconnect;
678   return($bibitemnum);
679 }
680
681 sub newsubject {
682   my ($bibnum)=@_;
683   my $dbh=C4Connect;
684   my $query="insert into bibliosubject (biblionumber) values
685   ($bibnum)";
686   my $sth=$dbh->prepare($query);
687 #  print $query;
688   $sth->execute;
689   $sth->finish;
690   $dbh->disconnect;
691 }
692
693 sub newsubtitle {
694   my ($bibnum, $subtitle) = @_;
695   my $dbh   = C4Connect;
696   $subtitle = $dbh->quote($subtitle);
697   my $query = "insert into bibliosubtitle set
698 biblionumber = $bibnum,
699 subtitle = $subtitle";
700   my $sth   = $dbh->prepare($query);
701
702   $sth->execute;
703
704   $sth->finish;
705   $dbh->disconnect;
706 }
707
708 sub neworder {
709   my ($bibnum,$title,$ordnum,$basket,$quantity,$listprice,$supplier,$who,$notes,$bookfund,$bibitemnum,$rrp,$ecost,$gst,$budget,$cost,$sub,$invoice)=@_;
710   if ($budget eq 'now'){
711     $budget="now()";
712   } else {
713     $budget="'2001-07-01'";
714   }
715   if ($sub eq 'yes'){
716     $sub=1;
717   } else {
718     $sub=0;
719   }
720   my $dbh=C4Connect;
721   my $query="insert into aqorders (biblionumber,title,basketno,
722   quantity,listprice,booksellerid,entrydate,requisitionedby,authorisedby,notes,
723   biblioitemnumber,rrp,ecost,gst,unitprice,subscription,booksellerinvoicenumber)
724
725   values
726   ($bibnum,'$title',$basket,$quantity,$listprice,'$supplier',now(),
727   '$who','$who','$notes',$bibitemnum,'$rrp','$ecost','$gst','$cost',
728   '$sub','$invoice')";
729   my $sth=$dbh->prepare($query);
730 #  print $query;
731   $sth->execute;
732   $sth->finish;
733   $query="select * from aqorders where
734   biblionumber=$bibnum and basketno=$basket and ordernumber >=$ordnum";
735   $sth=$dbh->prepare($query);
736   $sth->execute;
737   my $data=$sth->fetchrow_hashref;
738   $sth->finish;
739   $ordnum=$data->{'ordernumber'};
740   $query="insert into aqorderbreakdown (ordernumber,bookfundid) values
741   ($ordnum,'$bookfund')";
742   $sth=$dbh->prepare($query);
743 #  print $query;
744   $sth->execute;
745   $sth->finish;
746   $dbh->disconnect;
747 }
748
749 sub delorder {
750   my ($bibnum,$ordnum)=@_;
751   my $dbh=C4Connect;
752   my $query="update aqorders set datecancellationprinted=now()
753   where biblionumber='$bibnum' and
754   ordernumber='$ordnum'";
755   my $sth=$dbh->prepare($query);
756   #print $query;
757   $sth->execute;
758   $sth->finish;
759   my $count=itemcount($bibnum);
760   if ($count == 0){
761     delbiblio($bibnum);
762   }
763   $dbh->disconnect;
764 }
765
766 sub modorder {
767   my ($title,$ordnum,$quantity,$listprice,$bibnum,$basketno,$supplier,$who,$notes,$bookfund,$bibitemnum,$rrp,$ecost,$gst,$budget,$cost,$invoice)=@_;
768   my $dbh=C4Connect;
769   my $query="update aqorders set title='$title',
770   quantity='$quantity',listprice='$listprice',basketno='$basketno', 
771   rrp='$rrp',ecost='$ecost',unitprice='$cost',
772   booksellerinvoicenumber='$invoice'
773   where
774   ordernumber=$ordnum and biblionumber=$bibnum";
775   my $sth=$dbh->prepare($query);
776 #  print $query;
777   $sth->execute;
778   $sth->finish;
779   $query="update aqorderbreakdown set bookfundid=$bookfund where
780   ordernumber=$ordnum";
781   $sth=$dbh->prepare($query);
782 #  print $query;
783   $sth->execute;
784   $sth->finish;
785   $dbh->disconnect;
786 }
787
788 sub newordernum {
789   my $dbh=C4Connect;
790   my $query="Select max(ordernumber) from aqorders";
791   my $sth=$dbh->prepare($query);
792   $sth->execute;
793   my $data=$sth->fetchrow_arrayref;
794   my $ordnum=$$data[0];
795   $ordnum++;
796   $sth->finish;
797   $dbh->disconnect;
798   return($ordnum);
799 }
800
801 sub receiveorder {
802   my ($biblio,$ordnum,$quantrec,$user,$cost,$invoiceno,$bibitemno,$freight,$bookfund,$rrp)=@_;
803   my $dbh=C4Connect;
804   my $query="update aqorders set quantityreceived='$quantrec',
805   datereceived=now(),booksellerinvoicenumber='$invoiceno',
806   biblioitemnumber=$bibitemno,unitprice='$cost',freight='$freight',
807   rrp='$rrp'
808   where biblionumber=$biblio and ordernumber=$ordnum
809   ";
810 #  print $query;
811   my $sth=$dbh->prepare($query);
812   $sth->execute;
813   $sth->finish;
814   $query="update aqorderbreakdown set bookfundid=$bookfund where
815   ordernumber=$ordnum";
816   $sth=$dbh->prepare($query);
817 #  print $query;
818   $sth->execute;
819   $sth->finish;  
820   $dbh->disconnect;
821 }
822 sub updaterecorder{
823   my($biblio,$ordnum,$user,$cost,$bookfund,$rrp)=@_;
824   my $dbh=C4Connect;
825   my $query="update aqorders set
826   unitprice='$cost', rrp='$rrp'
827   where biblionumber=$biblio and ordernumber=$ordnum
828   ";
829 #  print $query;
830   my $sth=$dbh->prepare($query);
831   $sth->execute;
832   $sth->finish;
833   $query="update aqorderbreakdown set bookfundid=$bookfund where
834   ordernumber=$ordnum";
835   $sth=$dbh->prepare($query);
836 #  print $query;
837   $sth->execute;
838   $sth->finish;  
839   $dbh->disconnect;
840 }
841
842 sub curconvert {
843   my ($currency,$price)=@_;
844   my $dbh=C4Connect;
845   my $query="Select rate from currency where currency='$currency'";
846   my $sth=$dbh->prepare($query);
847   $sth->execute;
848   my $data=$sth->fetchrow_hashref;
849   $sth->finish;
850   $dbh->disconnect;
851   my $cur=$data->{'rate'};
852   if ($cur==0){
853     $cur=1;
854   }
855   my $price=$price / $cur;
856   return($price);
857 }
858
859 sub getcurrencies {
860   my $dbh=C4Connect;
861   my $query="Select * from currency";
862   my $sth=$dbh->prepare($query);
863   $sth->execute;
864   my @results;
865   my $i=0;
866   while (my $data=$sth->fetchrow_hashref){
867     $results[$i]=$data;
868     $i++;
869   }
870   $sth->finish;
871   $dbh->disconnect;
872   return($i,\@results);
873
874
875 sub getcurrency {
876   my ($cur)=@_;
877   my $dbh=C4Connect;
878   my $query="Select * from currency where currency='$cur'";
879   my $sth=$dbh->prepare($query);
880   $sth->execute;
881
882   my $data=$sth->fetchrow_hashref;
883   $sth->finish;
884   $dbh->disconnect;
885   return($data);
886
887
888 sub updatecurrencies {
889   my ($currency,$rate)=@_;
890   my $dbh=C4Connect;
891   my $query="update currency set rate=$rate where currency='$currency'";
892   my $sth=$dbh->prepare($query);
893   $sth->execute;
894   $sth->finish;
895   $dbh->disconnect;
896
897
898 sub updatesup {
899    my ($data)=@_;
900    my $dbh=C4Connect;
901    my $query="Update aqbooksellers set
902    name='$data->{'name'}',address1='$data->{'address1'}',address2='$data->{'address2'}',
903    address3='$data->{'address3'}',address4='$data->{'address4'}',postal='$data->{'postal'}',
904    phone='$data->{'phone'}',fax='$data->{'fax'}',url='$data->{'url'}',
905    contact='$data->{'contact'}',contpos='$data->{'contpos'}',
906    contphone='$data->{'contphone'}', contfax='$data->{'contfax'}', contaltphone=
907    '$data->{'contaltphone'}', contemail='$data->{'contemail'}', contnotes=
908    '$data->{'contnotes'}', active=$data->{'active'},
909    listprice='$data->{'listprice'}', invoiceprice='$data->{'invoiceprice'}',
910    gstreg=$data->{'gstreg'}, listincgst=$data->{'listincgst'},
911    invoiceincgst=$data->{'invoiceincgst'}, specialty='$data->{'specialty'}',
912    discount='$data->{'discount'}',invoicedisc='$data->{'invoicedisc'}',
913    nocalc='$data->{'nocalc'}'
914    where id='$data->{'id'}'";
915    my $sth=$dbh->prepare($query);
916    $sth->execute;
917    $sth->finish;
918    $dbh->disconnect;
919 #   print $query;
920 }
921
922 sub insertsup {
923   my ($data)=@_;
924   my $dbh=C4Connect;
925   my $sth=$dbh->prepare("Select max(id) from aqbooksellers");
926   $sth->execute;
927   my $data2=$sth->fetchrow_hashref;
928   $sth->finish;
929   $data2->{'max(id)'}++;
930   $sth=$dbh->prepare("Insert into aqbooksellers (id) values ($data2->{'max(id)'})");
931   $sth->execute;
932   $sth->finish;
933   $data->{'id'}=$data2->{'max(id)'};
934   $dbh->disconnect;
935   updatesup($data);
936   return($data->{'id'});
937 }
938
939
940 sub newitems {
941   my ($item, @barcodes) = @_;
942   my $dbh   = C4Connect;
943   my $query = "Select max(itemnumber) from items";
944   my $sth   = $dbh->prepare($query);
945   my $data;
946   my $itemnumber;
947   my $error;
948
949   $sth->execute;
950   $data       = $sth->fetchrow_hashref;
951   $itemnumber = $data->{'max(itemnumber)'} + 1;
952   $sth->finish;
953   
954   $item->{'booksellerid'}     = $dbh->quote($item->{'bookselletid'});
955   $item->{'homebranch'}       = $dbh->quote($item->{'homebranch'});
956   $item->{'price'}            = $dbh->quote($item->{'price'});
957   $item->{'replacementprice'} = $dbh->quote($item->{'replacementprice'});
958   $item->{'itemnotes'}        = $dbh->quote($item->{'itemnotes'});
959
960   foreach my $barcode (@barcodes) {
961     $barcode = uc($barcode);
962     $query   = "Insert into items set
963 itemnumber           = $itemnumber,
964 biblionumber         = $item->{'biblionumber'},
965 biblioitemnumber     = $item->{'biblioitemnumber'},
966 barcode              = $barcode,
967 booksellerid         = $item->{'booksellerid'},
968 dateaccessioned      = NOW(),
969 homebranch           = $item->{'homebranch'},
970 holdingbranch        = $item->{'homebranch'},
971 price                = $item->{'price'},
972 replacementprice     = $item->{'replacementprice'},
973 replacementpricedate = NOW(),
974 itemnotes            = $item->{'itemnotes'}";
975
976     if ($item->{'loan'}) {
977       $query .= ",
978 notforloan           = $item->{'loan'}";
979     } # if
980
981     $sth = $dbh->prepare($query);
982     $sth->execute;
983
984     $error .= $sth->errstr;
985
986     $sth->finish;
987     $itemnumber++;
988   } # for
989
990   $dbh->disconnect;
991   return($error);
992 }
993
994 sub checkitems{
995   my ($count,@barcodes)=@_;
996   my $dbh=C4Connect;
997   my $error;
998   for (my $i=0;$i<$count;$i++){
999     $barcodes[$i]=uc $barcodes[$i];
1000     my $query="Select * from items where barcode='$barcodes[$i]'";
1001     my $sth=$dbh->prepare($query);
1002     $sth->execute;
1003     if (my $data=$sth->fetchrow_hashref){
1004       $error.=" Duplicate Barcode: $barcodes[$i]";
1005     }
1006     $sth->finish;
1007   }
1008   $dbh->disconnect;
1009   return($error);
1010 }
1011
1012 sub moditem {
1013   my ($loan,$itemnum,$bibitemnum,$barcode,$notes,$homebranch,$lost,$wthdrawn,$replacement)=@_;
1014   my $dbh=C4Connect;
1015   my $query="update items set biblioitemnumber=$bibitemnum,
1016   barcode='$barcode',itemnotes='$notes'
1017   where itemnumber=$itemnum";
1018   if ($barcode eq ''){
1019     $query="update items set biblioitemnumber=$bibitemnum,notforloan=$loan where itemnumber=$itemnum";
1020   }
1021   if ($lost ne ''){
1022     $query="update items set biblioitemnumber=$bibitemnum,
1023       barcode='$barcode',itemnotes='$notes',homebranch='$homebranch',
1024       itemlost='$lost',wthdrawn='$wthdrawn' where itemnumber=$itemnum";
1025   }
1026   if ($replacement ne ''){
1027     $query=~ s/ where/,replacementprice='$replacement' where/;
1028   }
1029
1030   my $sth=$dbh->prepare($query);
1031   $sth->execute;
1032   $sth->finish;
1033   $dbh->disconnect;
1034 }
1035
1036 sub updatecost{
1037   my($price,$rrp,$itemnum)=@_;
1038   my $dbh=C4Connect;
1039   my $query="update items set price='$price',replacementprice='$rrp'
1040   where itemnumber=$itemnum";
1041   my $sth=$dbh->prepare($query);
1042   $sth->execute;
1043   $sth->finish;
1044   $dbh->disconnect;
1045 }
1046 sub countitems{
1047   my ($bibitemnum)=@_;
1048   my $dbh=C4Connect;
1049   my $query="Select count(*) from items where biblioitemnumber='$bibitemnum'";
1050   my $sth=$dbh->prepare($query);
1051   $sth->execute;
1052   my $data=$sth->fetchrow_hashref;
1053   $sth->finish;
1054   $dbh->disconnect;
1055   return($data->{'count(*)'});
1056 }
1057
1058 sub findall {
1059   my ($biblionumber)=@_;
1060   my $dbh=C4Connect;
1061   my $query="Select * from biblioitems,items,itemtypes where 
1062   biblioitems.biblionumber=$biblionumber 
1063   and biblioitems.biblioitemnumber=items.biblioitemnumber and
1064   itemtypes.itemtype=biblioitems.itemtype
1065   order by items.biblioitemnumber";
1066   my $sth=$dbh->prepare($query);
1067   $sth->execute;
1068   my @results;
1069   my $i;
1070   while (my $data=$sth->fetchrow_hashref){
1071     $results[$i]=$data;
1072     $i++;
1073   }
1074   $sth->finish;
1075   $dbh->disconnect;
1076   return(@results);
1077 }
1078
1079 sub needsmod{
1080   my ($bibitemnum,$itemtype)=@_;
1081   my $dbh=C4Connect;
1082   my $query="Select * from biblioitems where biblioitemnumber=$bibitemnum
1083   and itemtype='$itemtype'";
1084   my $sth=$dbh->prepare($query);
1085   $sth->execute;
1086   my $result=0;
1087   if (my $data=$sth->fetchrow_hashref){
1088     $result=1;
1089   }
1090   $sth->finish;
1091   $dbh->disconnect;
1092   return($result);
1093 }
1094
1095 sub delitem{
1096   my ($itemnum)=@_;
1097   my $dbh=C4Connect;
1098   my $query="select * from items where itemnumber=$itemnum";
1099   my $sth=$dbh->prepare($query);
1100   $sth->execute;
1101   my @data=$sth->fetchrow_array;
1102   $sth->finish;
1103   $query="Insert into deleteditems values (";
1104   foreach my $temp (@data){
1105     $query=$query."'$temp',";
1106   }
1107   $query=~ s/\,$/\)/;
1108 #  print $query;
1109   $sth=$dbh->prepare($query);
1110   $sth->execute;
1111   $sth->finish;
1112   $query = "Delete from items where itemnumber=$itemnum";
1113   $sth=$dbh->prepare($query);
1114   $sth->execute;
1115   $sth->finish;
1116   $dbh->disconnect;
1117 }
1118
1119
1120 sub deletebiblioitem {
1121     my ($biblioitemnumber) = @_;
1122     my $dbh   = C4Connect;
1123     my $query = "Select * from biblioitems
1124 where biblioitemnumber = $biblioitemnumber";
1125     my $sth   = $dbh->prepare($query);
1126     my @results;
1127
1128     $sth->execute;
1129   
1130     if (@results = $sth->fetchrow_array) {
1131
1132         $query = "Insert into deletedbiblioitems values (";
1133         foreach my $value (@results) {
1134             $value  = $dbh->quote($value);
1135             $query .= "$value,";
1136         } # foreach
1137
1138         $query =~ s/\,$/\)/;
1139         $dbh->do($query);
1140
1141         $query = "Delete from biblioitems
1142 where biblioitemnumber = $biblioitemnumber";
1143         $dbh->do($query);
1144     } # if
1145
1146     $sth->finish;
1147
1148 # Now delete all the items attached to the biblioitem
1149
1150     $query = "Select * from items where biblioitemnumber = $biblioitemnumber";
1151     $sth   = $dbh->prepare($query);
1152
1153     $sth->execute;
1154
1155     while (@results = $sth->fetchrow_array) {
1156
1157         $query = "Insert into deleteditems values (";
1158         foreach my $value (@results) {
1159             $value  = $dbh->quote($value);
1160             $query .= "$value,";
1161         } # foreach
1162
1163         $query =~ s/\,$/\)/;
1164         $dbh->do($query);
1165     } # while
1166
1167     $sth->finish;
1168
1169     $query = "Delete from items where biblioitemnumber = $biblioitemnumber";
1170     $dbh->do($query);
1171     
1172     $dbh->disconnect;
1173 } # sub deletebiblioitem
1174
1175
1176 sub delbiblio{
1177   my ($biblio)=@_;
1178   my $dbh=C4Connect;
1179   my $query="select * from biblio where biblionumber=$biblio";
1180   my $sth=$dbh->prepare($query);
1181   $sth->execute;
1182   if (my @data=$sth->fetchrow_array){
1183     $sth->finish;
1184     $query="Insert into deletedbiblio values (";
1185     foreach my $temp (@data){
1186       $temp=~ s/\'/\\\'/g;
1187       $query=$query."'$temp',";
1188     }
1189     $query=~ s/\,$/\)/;
1190 #   print $query;
1191     $sth=$dbh->prepare($query);
1192     $sth->execute;
1193     $sth->finish;
1194     $query = "Delete from biblio where biblionumber=$biblio";
1195     $sth=$dbh->prepare($query);
1196     $sth->execute;
1197     $sth->finish;
1198   }
1199
1200   $sth->finish;
1201   $dbh->disconnect;
1202 }
1203
1204 sub getitemtypes {
1205   my $dbh   = C4Connect;
1206   my $query = "select * from itemtypes";
1207   my $sth   = $dbh->prepare($query);
1208     # || die "Cannot prepare $query" . $dbh->errstr;
1209   my $count = 0;
1210   my @results;
1211   
1212   $sth->execute;
1213     # || die "Cannot execute $query\n" . $sth->errstr;
1214   while (my $data = $sth->fetchrow_hashref) {
1215     @results[$count] = $data;
1216     $count++;
1217   } # while
1218   
1219   $sth->finish;
1220   $dbh->disconnect;
1221   return($count, @results);
1222 } # sub getitemtypes
1223
1224
1225 sub getbiblio {
1226     my ($biblionumber) = @_;
1227     my $dbh   = C4Connect;
1228     my $query = "Select * from biblio where biblionumber = $biblionumber";
1229     my $sth   = $dbh->prepare($query);
1230       # || die "Cannot prepare $query\n" . $dbh->errstr;
1231     my $count = 0;
1232     my @results;
1233     
1234     $sth->execute;
1235       # || die "Cannot execute $query\n" . $sth->errstr;
1236     while (my $data = $sth->fetchrow_hashref) {
1237       $results[$count] = $data;
1238       $count++;
1239     } # while
1240     
1241     $sth->finish;
1242     $dbh->disconnect;
1243     return($count, @results);
1244 } # sub getbiblio
1245
1246
1247 sub getbiblioitem {
1248     my ($biblioitemnum) = @_;
1249     my $dbh   = C4Connect;
1250     my $query = "Select * from biblioitems where
1251 biblioitemnumber = $biblioitemnum";
1252     my $sth   = $dbh->prepare($query);
1253     my $count = 0;
1254     my @results;
1255
1256     $sth->execute;
1257
1258     while (my $data = $sth->fetchrow_hashref) {
1259         $results[$count] = $data;
1260         $count++;
1261     } # while
1262
1263     $sth->finish;
1264     $dbh->disconnect;
1265     return($count, @results);
1266 } # sub getbiblioitem
1267
1268
1269 sub getitemsbybiblioitem {
1270     my ($biblioitemnum) = @_;
1271     my $dbh   = C4Connect;
1272     my $query = "Select * from items, biblio where
1273 biblio.biblionumber = items.biblionumber and biblioitemnumber
1274 = $biblioitemnum";
1275     my $sth   = $dbh->prepare($query);
1276       # || die "Cannot prepare $query\n" . $dbh->errstr;
1277     my $count = 0;
1278     my @results;
1279     
1280     $sth->execute;
1281       # || die "Cannot execute $query\n" . $sth->errstr;
1282     while (my $data = $sth->fetchrow_hashref) {
1283       $results[$count] = $data;
1284       $count++;
1285     } # while
1286     
1287     $sth->finish;
1288     $dbh->disconnect;
1289     return($count, @results);
1290 } # sub getitemsbybiblioitem
1291
1292
1293 sub isbnsearch {
1294     my ($isbn) = @_;
1295     my $dbh   = C4Connect;
1296     my $count = 0;
1297     my $query;
1298     my $sth;
1299     my @results;
1300     
1301     $isbn  = $dbh->quote($isbn);
1302     $query = "Select * from biblioitems where isbn = $isbn";
1303     $sth   = $dbh->prepare($query);
1304     
1305     $sth->execute;
1306     while (my $data = $sth->fetchrow_hashref) {
1307         $results[$count] = $data;
1308         $count++;
1309     } # while
1310
1311     $sth->finish;
1312     $dbh->disconnect;
1313     return($count, @results);
1314 } # sub isbnsearch
1315
1316
1317 sub keywordsearch {
1318   my ($keywordlist) = @_;
1319   my $dbh   = C4Connect;
1320   my $query = "Select * from biblio where";
1321   my $count = 0;
1322   my $sth;
1323   my @results;
1324   my @keywords = split(/ +/, $keywordlist);
1325   my $keyword = shift(@keywords);
1326
1327   $keyword =~ s/%/\\%/g;
1328   $keyword =~ s/_/\\_/;
1329   $keyword = "%" . $keyword . "%";
1330   $keyword = $dbh->quote($keyword);
1331   $query  .= " (author like $keyword) or
1332 (title like $keyword) or (unititle like $keyword) or
1333 (notes like $keyword) or (seriestitle like $keyword) or
1334 (abstract like $keyword)";
1335
1336   foreach $keyword (@keywords) {
1337     $keyword =~ s/%/\\%/;
1338     $keyword =~ s/_/\\_/;
1339     $keyword = "%" . $keyword . "%";
1340     $keyword = $dbh->quote($keyword);
1341     $query  .= " or (author like $keyword) or
1342 (title like $keyword) or (unititle like $keyword) or 
1343 (notes like $keyword) or (seriestitle like $keyword) or
1344 (abstract like $keyword)";
1345   } # foreach
1346   
1347   $sth = $dbh->prepare($query);
1348   $sth->execute;
1349   
1350   while (my $data = $sth->fetchrow_hashref) {
1351     $results[$count] = $data;
1352     $count++;
1353   } # while
1354   
1355   $sth->finish;
1356   $dbh->disconnect;
1357   return($count, @results);
1358 } # sub keywordsearch
1359
1360
1361 sub websitesearch {
1362     my ($keywordlist) = @_;
1363     my $dbh   = C4Connect;
1364     my $query = "Select distinct biblio.* from biblio, biblioitems where
1365 biblio.biblionumber = biblioitems.biblionumber and (";
1366     my $count = 0;
1367     my $sth;
1368     my @results;
1369     my @keywords = split(/ +/, $keywordlist);
1370     my $keyword = shift(@keywords);
1371
1372     $keyword =~ s/%/\\%/g;
1373     $keyword =~ s/_/\\_/;
1374     $keyword = "%" . $keyword . "%";
1375     $keyword = $dbh->quote($keyword);
1376     $query  .= " (url like $keyword)";
1377
1378     foreach $keyword (@keywords) {
1379         $keyword =~ s/%/\\%/;
1380         $keyword =~ s/_/\\_/;
1381         $keyword = "%" . $keyword . "%";
1382         $keyword = $dbh->quote($keyword);
1383         $query  .= " or (url like $keyword)";
1384     } # foreach
1385
1386     $query .= ")";
1387     $sth    = $dbh->prepare($query);
1388     $sth->execute;
1389
1390     while (my $data = $sth->fetchrow_hashref) {
1391         $results[$count] = $data;
1392         $count++;
1393     } # while
1394
1395     $sth->finish;
1396     $dbh->disconnect;
1397     return($count, @results);
1398 } # sub websitesearch
1399
1400
1401 sub addwebsite {
1402     my ($website) = @_;
1403     my $dbh = C4Connect;
1404     my $query;
1405     
1406     $website->{'biblionumber'} = $dbh->quote($website->{'biblionumber'});
1407     $website->{'title'}        = $dbh->quote($website->{'title'});
1408     $website->{'description'}  = $dbh->quote($website->{'description'});
1409     $website->{'url'}          = $dbh->quote($website->{'url'});
1410     
1411     $query = "Insert into websites set
1412 biblionumber = $website->{'biblionumber'},
1413 title        = $website->{'title'},
1414 description  = $website->{'description'},
1415 url          = $website->{'url'}";
1416     
1417     $dbh->do($query);
1418     
1419     $dbh->disconnect;
1420 } # sub website
1421
1422
1423 sub updatewebsite {
1424     my ($website) = @_;
1425     my $dbh = C4Connect;
1426     my $query;
1427     
1428     $website->{'title'}      = $dbh->quote($website->{'title'});
1429     $website->{'description'} = $dbh->quote($website->{'description'});
1430     $website->{'url'}        = $dbh->quote($website->{'url'});
1431     
1432     $query = "Update websites set
1433 title       = $website->{'title'},
1434 description = $website->{'description'},
1435 url         = $website->{'url'}
1436 where websitenumber = $website->{'websitenumber'}";
1437
1438     $dbh->do($query);
1439     
1440     $dbh->disconnect;
1441 } # sub updatewebsite
1442
1443
1444 sub deletewebsite {
1445     my ($websitenumber) = @_;
1446     my $dbh = C4Connect;
1447     my $query = "Delete from websites where websitenumber = $websitenumber";
1448     
1449     $dbh->do($query);
1450     
1451     $dbh->disconnect;
1452 } # sub deletewebsite
1453
1454
1455 END { }       # module clean-up code here (global destructor)