Added code to allow deleting of biblioitems
[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 biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=                    
153   aqorders.biblioitemnumber 
154   group by aqorders.biblioitemnumber 
155   order by
156   biblio.title";
157   my $i=0;
158   my @results;
159   my $sth=$dbh->prepare($query);
160   $sth->execute;
161   while (my $data=$sth->fetchrow_hashref){
162     $results[$i]=$data;
163     $i++;
164   }
165   $sth->finish;
166   $dbh->disconnect;
167   return($i,@results);
168 }
169
170 sub getrecorders {
171   #gets all orders from a certain supplier, orders them alphabetically
172   my ($supid)=@_;
173   my $dbh=C4Connect;
174   my $query="Select * from aqorders,biblio,biblioitems where booksellerid='$supid'
175   and (cancelledby is NULL or cancelledby = '')
176   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=                    
177   aqorders.biblioitemnumber and
178   aqorders.quantityreceived>0
179   and aqorders.datereceived >=now()
180   group by aqorders.biblioitemnumber 
181   order by
182   biblio.title";
183   my $i=0;
184   my @results;
185   my $sth=$dbh->prepare($query);
186   $sth->execute;
187   while (my $data=$sth->fetchrow_hashref){
188     $results[$i]=$data;
189     $i++;
190   }
191   $sth->finish;
192   $dbh->disconnect;
193   return($i,@results);
194 }
195
196 sub ordersearch {
197   my ($search,$biblio,$catview) = @_;
198   my $dbh   = C4Connect;
199   my $query = "Select *,biblio.title from aqorders,biblioitems,biblio
200 where aqorders.biblioitemnumber = biblioitems.biblioitemnumber
201 and biblio.biblionumber=aqorders.biblionumber
202 and ((datecancellationprinted is NULL)
203 or (datecancellationprinted = '0000-00-00')
204 and ((";
205   my @data  = split(' ',$search);
206   my $count = @data;
207   for (my $i = 0; $i < $count; $i++) {
208     $query .= "(biblio.title like '$data[$i]%' or biblio.title like '% $data[$i]%') and ";
209   }
210   $query=~ s/ and $//;
211   $query.=" ) or biblioitems.isbn='$search' 
212   or (aqorders.ordernumber='$search' and aqorders.biblionumber='$biblio')) ";
213   if ($catview ne 'yes'){
214     $query.=" and (quantityreceived < quantity or quantityreceived is NULL)";
215   }
216   $query.=" group by aqorders.ordernumber";
217   my $sth=$dbh->prepare($query);
218 #  print $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   $biblioitem->{'volume'}          = $dbh->quote($biblioitem->{'volume'});
626   $biblioitem->{'number'}          = $dbh->quote($biblioitem->{'number'});
627   $biblioitem->{'classification'}  = $dbh->quote($biblioitem->{'classification'});
628   $biblioitem->{'itemtype'}        = $dbh->quote($biblioitem->{'itemtype'});
629   $biblioitem->{'url'}             = $dbh->quote($biblioitem->{'url'});
630   $biblioitem->{'isbn'}            = $dbh->quote($biblioitem->{'isbn'});
631   $biblioitem->{'issn'}            = $dbh->quote($biblioitem->{'issn'});
632   $biblioitem->{'dewey'}           = $dbh->quote($biblioitem->{'dewey'});
633   $biblioitem->{'subclass'}        = $dbh->quote($biblioitem->{'subclass'});
634   $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
635   $biblioitem->{'publishercode'}   = $dbh->quote($biblioitem->{'publishercode'});
636   $biblioitem->{'volumedate'}      = $dbh->quote($biblioitem->{'volumedate'});
637   $biblioitem->{'volumeddesc'}     = $dbh->quote($biblioitem->{'volumeddesc'});  $biblioitem->{'illus'}            = $dbh->quote($biblioitem->{'illus'});
638   $biblioitem->{'illus'}           = $dbh->quote($biblioitem->{'illus'});
639   $biblioitem->{'pages'}           = $dbh->quote($biblioitem->{'pages'});
640   $biblioitem->{'notes'}           = $dbh->quote($biblioitem->{'notes'});
641   $biblioitem->{'size'}            = $dbh->quote($biblioitem->{'size'});
642   $biblioitem->{'place'}           = $dbh->quote($biblioitem->{'place'});
643   
644   $sth->execute;
645   $data       = $sth->fetchrow_arrayref;
646   $bibitemnum = $$data[0] + 1;
647
648   $sth->finish;
649
650   $query = "insert into biblioitems set
651 biblioitemnumber = $bibitemnum,
652 biblionumber     = $biblioitem->{'biblionumber'},
653 volume           = $biblioitem->{'volume'},
654 number           = $biblioitem->{'number'},
655 classification   = $biblioitem->{'classification'},
656 itemtype         = $biblioitem->{'itemtype'},
657 url              = $biblioitem->{'url'},
658 isbn             = $biblioitem->{'isbn'},
659 issn             = $biblioitem->{'issn'},
660 dewey            = $biblioitem->{'dewey'},
661 subclass         = $biblioitem->{'subclass'},
662 publicationyear  = $biblioitem->{'publicationyear'},
663 publishercode    = $biblioitem->{'publishercode'},
664 volumedate       = $biblioitem->{'volumedate'},
665 volumeddesc      = $biblioitem->{'volumeddesc'},
666 illus            = $biblioitem->{'illus'},
667 pages            = $biblioitem->{'pages'},
668 notes            = $biblioitem->{'notes'},
669 size             = $biblioitem->{'size'},
670 place            = $biblioitem->{'place'}";
671
672   $sth = $dbh->prepare($query);
673   $sth->execute;
674
675   $sth->finish;
676   $dbh->disconnect;
677   return($bibitemnum);
678 }
679
680 sub newsubject {
681   my ($bibnum)=@_;
682   my $dbh=C4Connect;
683   my $query="insert into bibliosubject (biblionumber) values
684   ($bibnum)";
685   my $sth=$dbh->prepare($query);
686 #  print $query;
687   $sth->execute;
688   $sth->finish;
689   $dbh->disconnect;
690 }
691
692 sub newsubtitle {
693   my ($bibnum, $subtitle) = @_;
694   my $dbh   = C4Connect;
695   $subtitle = $dbh->quote($subtitle);
696   my $query = "insert into bibliosubtitle set
697 biblionumber = $bibnum,
698 subtitle = $subtitle";
699   my $sth   = $dbh->prepare($query);
700
701   $sth->execute;
702
703   $sth->finish;
704   $dbh->disconnect;
705 }
706
707 sub neworder {
708   my ($bibnum,$title,$ordnum,$basket,$quantity,$listprice,$supplier,$who,$notes,$bookfund,$bibitemnum,$rrp,$ecost,$gst,$budget,$cost,$sub,$invoice)=@_;
709   if ($budget eq 'now'){
710     $budget="now()";
711   } else {
712     $budget="'2001-07-01'";
713   }
714   if ($sub eq 'yes'){
715     $sub=1;
716   } else {
717     $sub=0;
718   }
719   my $dbh=C4Connect;
720   my $query="insert into aqorders (biblionumber,title,basketno,
721   quantity,listprice,booksellerid,entrydate,requisitionedby,authorisedby,notes,
722   biblioitemnumber,rrp,ecost,gst,unitprice,subscription,booksellerinvoicenumber)
723
724   values
725   ($bibnum,'$title',$basket,$quantity,$listprice,'$supplier',now(),
726   '$who','$who','$notes',$bibitemnum,'$rrp','$ecost','$gst','$cost',
727   '$sub','$invoice')";
728   my $sth=$dbh->prepare($query);
729 #  print $query;
730   $sth->execute;
731   $sth->finish;
732   $query="select * from aqorders where
733   biblionumber=$bibnum and basketno=$basket and ordernumber >=$ordnum";
734   $sth=$dbh->prepare($query);
735   $sth->execute;
736   my $data=$sth->fetchrow_hashref;
737   $sth->finish;
738   $ordnum=$data->{'ordernumber'};
739   $query="insert into aqorderbreakdown (ordernumber,bookfundid) values
740   ($ordnum,'$bookfund')";
741   $sth=$dbh->prepare($query);
742 #  print $query;
743   $sth->execute;
744   $sth->finish;
745   $dbh->disconnect;
746 }
747
748 sub delorder {
749   my ($bibnum,$ordnum)=@_;
750   my $dbh=C4Connect;
751   my $query="update aqorders set datecancellationprinted=now()
752   where biblionumber='$bibnum' and
753   ordernumber='$ordnum'";
754   my $sth=$dbh->prepare($query);
755   #print $query;
756   $sth->execute;
757   $sth->finish;
758   my $count=itemcount($bibnum);
759   if ($count == 0){
760     delbiblio($bibnum);
761   }
762   $dbh->disconnect;
763 }
764
765 sub modorder {
766   my ($title,$ordnum,$quantity,$listprice,$bibnum,$basketno,$supplier,$who,$notes,$bookfund,$bibitemnum,$rrp,$ecost,$gst,$budget,$cost,$invoice)=@_;
767   my $dbh=C4Connect;
768   my $query="update aqorders set title='$title',
769   quantity='$quantity',listprice='$listprice',basketno='$basketno', 
770   rrp='$rrp',ecost='$ecost',unitprice='$cost',
771   booksellerinvoicenumber='$invoice'
772   where
773   ordernumber=$ordnum and biblionumber=$bibnum";
774   my $sth=$dbh->prepare($query);
775 #  print $query;
776   $sth->execute;
777   $sth->finish;
778   $query="update aqorderbreakdown set bookfundid=$bookfund where
779   ordernumber=$ordnum";
780   $sth=$dbh->prepare($query);
781 #  print $query;
782   $sth->execute;
783   $sth->finish;
784   $dbh->disconnect;
785 }
786
787 sub newordernum {
788   my $dbh=C4Connect;
789   my $query="Select max(ordernumber) from aqorders";
790   my $sth=$dbh->prepare($query);
791   $sth->execute;
792   my $data=$sth->fetchrow_arrayref;
793   my $ordnum=$$data[0];
794   $ordnum++;
795   $sth->finish;
796   $dbh->disconnect;
797   return($ordnum);
798 }
799
800 sub receiveorder {
801   my ($biblio,$ordnum,$quantrec,$user,$cost,$invoiceno,$bibitemno,$freight,$bookfund,$rrp)=@_;
802   my $dbh=C4Connect;
803   my $query="update aqorders set quantityreceived='$quantrec',
804   datereceived=now(),booksellerinvoicenumber='$invoiceno',
805   biblioitemnumber=$bibitemno,unitprice='$cost',freight='$freight',
806   rrp='$rrp'
807   where biblionumber=$biblio and ordernumber=$ordnum
808   ";
809 #  print $query;
810   my $sth=$dbh->prepare($query);
811   $sth->execute;
812   $sth->finish;
813   $query="update aqorderbreakdown set bookfundid=$bookfund where
814   ordernumber=$ordnum";
815   $sth=$dbh->prepare($query);
816 #  print $query;
817   $sth->execute;
818   $sth->finish;  
819   $dbh->disconnect;
820 }
821 sub updaterecorder{
822   my($biblio,$ordnum,$user,$cost,$bookfund,$rrp)=@_;
823   my $dbh=C4Connect;
824   my $query="update aqorders set
825   unitprice='$cost', rrp='$rrp'
826   where biblionumber=$biblio and ordernumber=$ordnum
827   ";
828 #  print $query;
829   my $sth=$dbh->prepare($query);
830   $sth->execute;
831   $sth->finish;
832   $query="update aqorderbreakdown set bookfundid=$bookfund where
833   ordernumber=$ordnum";
834   $sth=$dbh->prepare($query);
835 #  print $query;
836   $sth->execute;
837   $sth->finish;  
838   $dbh->disconnect;
839 }
840
841 sub curconvert {
842   my ($currency,$price)=@_;
843   my $dbh=C4Connect;
844   my $query="Select rate from currency where currency='$currency'";
845   my $sth=$dbh->prepare($query);
846   $sth->execute;
847   my $data=$sth->fetchrow_hashref;
848   $sth->finish;
849   $dbh->disconnect;
850   my $cur=$data->{'rate'};
851   if ($cur==0){
852     $cur=1;
853   }
854   my $price=$price / $cur;
855   return($price);
856 }
857
858 sub getcurrencies {
859   my $dbh=C4Connect;
860   my $query="Select * from currency";
861   my $sth=$dbh->prepare($query);
862   $sth->execute;
863   my @results;
864   my $i=0;
865   while (my $data=$sth->fetchrow_hashref){
866     $results[$i]=$data;
867     $i++;
868   }
869   $sth->finish;
870   $dbh->disconnect;
871   return($i,\@results);
872
873
874 sub getcurrency {
875   my ($cur)=@_;
876   my $dbh=C4Connect;
877   my $query="Select * from currency where currency='$cur'";
878   my $sth=$dbh->prepare($query);
879   $sth->execute;
880
881   my $data=$sth->fetchrow_hashref;
882   $sth->finish;
883   $dbh->disconnect;
884   return($data);
885
886
887 sub updatecurrencies {
888   my ($currency,$rate)=@_;
889   my $dbh=C4Connect;
890   my $query="update currency set rate=$rate where currency='$currency'";
891   my $sth=$dbh->prepare($query);
892   $sth->execute;
893   $sth->finish;
894   $dbh->disconnect;
895
896
897 sub updatesup {
898    my ($data)=@_;
899    my $dbh=C4Connect;
900    my $query="Update aqbooksellers set
901    name='$data->{'name'}',address1='$data->{'address1'}',address2='$data->{'address2'}',
902    address3='$data->{'address3'}',address4='$data->{'address4'}',postal='$data->{'postal'}',
903    phone='$data->{'phone'}',fax='$data->{'fax'}',url='$data->{'url'}',
904    contact='$data->{'contact'}',contpos='$data->{'contpos'}',
905    contphone='$data->{'contphone'}', contfax='$data->{'contfax'}', contaltphone=
906    '$data->{'contaltphone'}', contemail='$data->{'contemail'}', contnotes=
907    '$data->{'contnotes'}', active=$data->{'active'},
908    listprice='$data->{'listprice'}', invoiceprice='$data->{'invoiceprice'}',
909    gstreg=$data->{'gstreg'}, listincgst=$data->{'listincgst'},
910    invoiceincgst=$data->{'invoiceincgst'}, specialty='$data->{'specialty'}',
911    discount='$data->{'discount'}',invoicedisc='$data->{'invoicedisc'}',
912    nocalc='$data->{'nocalc'}'
913    where id='$data->{'id'}'";
914    my $sth=$dbh->prepare($query);
915    $sth->execute;
916    $sth->finish;
917    $dbh->disconnect;
918 #   print $query;
919 }
920
921 sub insertsup {
922   my ($data)=@_;
923   my $dbh=C4Connect;
924   my $sth=$dbh->prepare("Select max(id) from aqbooksellers");
925   $sth->execute;
926   my $data2=$sth->fetchrow_hashref;
927   $sth->finish;
928   $data2->{'max(id)'}++;
929   $sth=$dbh->prepare("Insert into aqbooksellers (id) values ($data2->{'max(id)'})");
930   $sth->execute;
931   $sth->finish;
932   $data->{'id'}=$data2->{'max(id)'};
933   $dbh->disconnect;
934   updatesup($data);
935   return($data->{'id'});
936 }
937
938
939 sub newitems {
940   my ($item, @barcodes) = @_;
941   my $dbh   = C4Connect;
942   my $query = "Select max(itemnumber) from items";
943   my $sth   = $dbh->prepare($query);
944   my $data;
945   my $itemnumber;
946   my $error;
947
948   $sth->execute;
949   $data       = $sth->fetchrow_hashref;
950   $itemnumber = $data->{'max(itemnumber)'} + 1;
951   $sth->finish;
952   
953   $item->{'booksellerid'}     = $dbh->quote($item->{'bookselletid'});
954   $item->{'homebranch'}       = $dbh->quote($item->{'homebranch'});
955   $item->{'price'}            = $dbh->quote($item->{'price'});
956   $item->{'replacementprice'} = $dbh->quote($item->{'replacementprice'});
957   $item->{'itemnotes'}        = $dbh->quote($item->{'itemnotes'});
958
959   foreach my $barcode (@barcodes) {
960     $barcode = uc($barcode);
961     $query   = "Insert into items set
962 itemnumber           = $itemnumber,
963 biblionumber         = $item->{'biblionumber'},
964 biblioitemnumber     = $item->{'biblioitemnumber'},
965 barcode              = $barcode,
966 booksellerid         = $item->{'booksellerid'},
967 dateaccessioned      = NOW(),
968 homebranch           = $item->{'homebranch'},
969 holdingbranch        = $item->{'homebranch'},
970 price                = $item->{'price'},
971 replacementprice     = $item->{'replacementprice'},
972 replacementpricedate = NOW(),
973 itemnotes            = $item->{'itemnotes'}";
974
975     if ($item->{'loan'}) {
976       $query .= ",
977 notforloan           = $item->{'loan'}";
978     } # if
979
980     $sth = $dbh->prepare($query);
981     $sth->execute;
982
983     $error .= $sth->errstr;
984
985     $sth->finish;
986     $itemnumber++;
987   } # for
988
989   $dbh->disconnect;
990   return($error);
991 }
992
993 sub checkitems{
994   my ($count,@barcodes)=@_;
995   my $dbh=C4Connect;
996   my $error;
997   for (my $i=0;$i<$count;$i++){
998     $barcodes[$i]=uc $barcodes[$i];
999     my $query="Select * from items where barcode='$barcodes[$i]'";
1000     my $sth=$dbh->prepare($query);
1001     $sth->execute;
1002     if (my $data=$sth->fetchrow_hashref){
1003       $error.=" Duplicate Barcode: $barcodes[$i]";
1004     }
1005     $sth->finish;
1006   }
1007   $dbh->disconnect;
1008   return($error);
1009 }
1010
1011 sub moditem {
1012   my ($loan,$itemnum,$bibitemnum,$barcode,$notes,$homebranch,$lost,$wthdrawn,$replacement)=@_;
1013   my $dbh=C4Connect;
1014   my $query="update items set biblioitemnumber=$bibitemnum,
1015   barcode='$barcode',itemnotes='$notes'
1016   where itemnumber=$itemnum";
1017   if ($barcode eq ''){
1018     $query="update items set biblioitemnumber=$bibitemnum,notforloan=$loan where itemnumber=$itemnum";
1019   }
1020   if ($lost ne ''){
1021     $query="update items set biblioitemnumber=$bibitemnum,
1022       barcode='$barcode',itemnotes='$notes',homebranch='$homebranch',
1023       itemlost='$lost',wthdrawn='$wthdrawn' where itemnumber=$itemnum";
1024   }
1025   if ($replacement ne ''){
1026     $query=~ s/ where/,replacementprice='$replacement' where/;
1027   }
1028
1029   my $sth=$dbh->prepare($query);
1030   $sth->execute;
1031   $sth->finish;
1032   $dbh->disconnect;
1033 }
1034
1035 sub updatecost{
1036   my($price,$rrp,$itemnum)=@_;
1037   my $dbh=C4Connect;
1038   my $query="update items set price='$price',replacementprice='$rrp'
1039   where itemnumber=$itemnum";
1040   my $sth=$dbh->prepare($query);
1041   $sth->execute;
1042   $sth->finish;
1043   $dbh->disconnect;
1044 }
1045 sub countitems{
1046   my ($bibitemnum)=@_;
1047   my $dbh=C4Connect;
1048   my $query="Select count(*) from items where biblioitemnumber='$bibitemnum'";
1049   my $sth=$dbh->prepare($query);
1050   $sth->execute;
1051   my $data=$sth->fetchrow_hashref;
1052   $sth->finish;
1053   $dbh->disconnect;
1054   return($data->{'count(*)'});
1055 }
1056
1057 sub findall {
1058   my ($biblionumber)=@_;
1059   my $dbh=C4Connect;
1060   my $query="Select * from biblioitems,items,itemtypes where 
1061   biblioitems.biblionumber=$biblionumber 
1062   and biblioitems.biblioitemnumber=items.biblioitemnumber and
1063   itemtypes.itemtype=biblioitems.itemtype
1064   order by items.biblioitemnumber";
1065   my $sth=$dbh->prepare($query);
1066   $sth->execute;
1067   my @results;
1068   my $i;
1069   while (my $data=$sth->fetchrow_hashref){
1070     $results[$i]=$data;
1071     $i++;
1072   }
1073   $sth->finish;
1074   $dbh->disconnect;
1075   return(@results);
1076 }
1077
1078 sub needsmod{
1079   my ($bibitemnum,$itemtype)=@_;
1080   my $dbh=C4Connect;
1081   my $query="Select * from biblioitems where biblioitemnumber=$bibitemnum
1082   and itemtype='$itemtype'";
1083   my $sth=$dbh->prepare($query);
1084   $sth->execute;
1085   my $result=0;
1086   if (my $data=$sth->fetchrow_hashref){
1087     $result=1;
1088   }
1089   $sth->finish;
1090   $dbh->disconnect;
1091   return($result);
1092 }
1093
1094 sub delitem{
1095   my ($itemnum)=@_;
1096   my $dbh=C4Connect;
1097   my $query="select * from items where itemnumber=$itemnum";
1098   my $sth=$dbh->prepare($query);
1099   $sth->execute;
1100   my @data=$sth->fetchrow_array;
1101   $sth->finish;
1102   $query="Insert into deleteditems values (";
1103   foreach my $temp (@data){
1104     $query=$query."'$temp',";
1105   }
1106   $query=~ s/\,$/\)/;
1107 #  print $query;
1108   $sth=$dbh->prepare($query);
1109   $sth->execute;
1110   $sth->finish;
1111   $query = "Delete from items where itemnumber=$itemnum";
1112   $sth=$dbh->prepare($query);
1113   $sth->execute;
1114   $sth->finish;
1115   $dbh->disconnect;
1116 }
1117
1118
1119 sub deletebiblioitem {
1120     my ($biblioitemnumber) = @_;
1121     my $dbh   = C4Connect;
1122     my $query = "Select * from biblioitems
1123 where biblioitemnumber = $biblioitemnumber";
1124     my $sth   = $dbh->prepare($query);
1125     my @results;
1126
1127     $sth->execute;
1128   
1129     if (@results = $sth->fetchrow_array) {
1130
1131         $query = "Insert into deletedbiblioitems values (";
1132         foreach my $value (@results) {
1133             $value  = $dbh->quote($value);
1134             $query .= "$value,";
1135         } # foreach
1136
1137         $query =~ s/\,$/\)/;
1138         $dbh->do($query);
1139
1140         $query = "Delete from biblioitems
1141 where biblioitemnumber = $biblioitemnumber";
1142         $dbh->do($query);
1143     } # if
1144
1145     $sth->finish;
1146
1147 # Now delete all the items attached to the biblioitem
1148
1149     $query = "Select * from items where biblioitemnumber = $biblioitemnumber";
1150     $sth   = $dbh->prepare($query);
1151
1152     $sth->execute;
1153
1154     while (@results = $sth->fetchrow_array) {
1155
1156         $query = "Insert into deleteditems values (";
1157         foreach my $value (@results) {
1158             $value  = $dbh->quote($value);
1159             $query .= "$value,";
1160         } # foreach
1161
1162         $query =~ s/\,$/\)/;
1163         $dbh->do($query);
1164     } # while
1165
1166     $sth->finish;
1167
1168     $query = "Delete from items where biblioitemnumber = $biblioitemnumber";
1169     $dbh->do($query);
1170     
1171     $dbh->disconnect;
1172 } # sub deletebiblioitem
1173
1174
1175 sub delbiblio{
1176   my ($biblio)=@_;
1177   my $dbh=C4Connect;
1178   my $query="select * from biblio where biblionumber=$biblio";
1179   my $sth=$dbh->prepare($query);
1180   $sth->execute;
1181   if (my @data=$sth->fetchrow_array){
1182     $sth->finish;
1183     $query="Insert into deletedbiblio values (";
1184     foreach my $temp (@data){
1185       $temp=~ s/\'/\\\'/g;
1186       $query=$query."'$temp',";
1187     }
1188     $query=~ s/\,$/\)/;
1189 #   print $query;
1190     $sth=$dbh->prepare($query);
1191     $sth->execute;
1192     $sth->finish;
1193     $query = "Delete from biblio where biblionumber=$biblio";
1194     $sth=$dbh->prepare($query);
1195     $sth->execute;
1196     $sth->finish;
1197   }
1198
1199   $sth->finish;
1200   $dbh->disconnect;
1201 }
1202
1203 sub getitemtypes {
1204   my $dbh   = C4Connect;
1205   my $query = "select * from itemtypes";
1206   my $sth   = $dbh->prepare($query);
1207     # || die "Cannot prepare $query" . $dbh->errstr;
1208   my $count = 0;
1209   my @results;
1210   
1211   $sth->execute;
1212     # || die "Cannot execute $query\n" . $sth->errstr;
1213   while (my $data = $sth->fetchrow_hashref) {
1214     @results[$count] = $data;
1215     $count++;
1216   } # while
1217   
1218   $sth->finish;
1219   $dbh->disconnect;
1220   return($count, @results);
1221 } # sub getitemtypes
1222
1223
1224 sub getbiblio {
1225     my ($biblionumber) = @_;
1226     my $dbh   = C4Connect;
1227     my $query = "Select * from biblio where biblionumber = $biblionumber";
1228     my $sth   = $dbh->prepare($query);
1229       # || die "Cannot prepare $query\n" . $dbh->errstr;
1230     my $count = 0;
1231     my @results;
1232     
1233     $sth->execute;
1234       # || die "Cannot execute $query\n" . $sth->errstr;
1235     while (my $data = $sth->fetchrow_hashref) {
1236       $results[$count] = $data;
1237       $count++;
1238     } # while
1239     
1240     $sth->finish;
1241     $dbh->disconnect;
1242     return($count, @results);
1243 } # sub getbiblio
1244
1245
1246 sub getbiblioitem {
1247     my ($biblioitemnum) = @_;
1248     my $dbh   = C4Connect;
1249     my $query = "Select * from biblioitems where
1250 biblioitemnumber = $biblioitemnum";
1251     my $sth   = $dbh->prepare($query);
1252     my $count = 0;
1253     my @results;
1254
1255     $sth->execute;
1256
1257     while (my $data = $sth->fetchrow_hashref) {
1258         $results[$count] = $data;
1259         $count++;
1260     } # while
1261
1262     $sth->finish;
1263     $dbh->disconnect;
1264     return($count, @results);
1265 } # sub getbiblioitem
1266
1267
1268 sub getitemsbybiblioitem {
1269     my ($biblioitemnum) = @_;
1270     my $dbh   = C4Connect;
1271     my $query = "Select * from items, biblio where
1272 biblio.biblionumber = items.biblionumber and biblioitemnumber
1273 = $biblioitemnum";
1274     my $sth   = $dbh->prepare($query);
1275       # || die "Cannot prepare $query\n" . $dbh->errstr;
1276     my $count = 0;
1277     my @results;
1278     
1279     $sth->execute;
1280       # || die "Cannot execute $query\n" . $sth->errstr;
1281     while (my $data = $sth->fetchrow_hashref) {
1282       $results[$count] = $data;
1283       $count++;
1284     } # while
1285     
1286     $sth->finish;
1287     $dbh->disconnect;
1288     return($count, @results);
1289 } # sub getitemsbybiblioitem
1290
1291
1292 sub isbnsearch {
1293     my ($isbn) = @_;
1294     my $dbh   = C4Connect;
1295     my $count = 0;
1296     my $query;
1297     my $sth;
1298     my @results;
1299     
1300     $isbn  = $dbh->quote($isbn);
1301     $query = "Select * from biblioitems where isbn = $isbn";
1302     $sth   = $dbh->prepare($query);
1303     
1304     $sth->execute;
1305     while (my $data = $sth->fetchrow_hashref) {
1306         $results[$count] = $data;
1307         $count++;
1308     } # while
1309
1310     $sth->finish;
1311     $dbh->disconnect;
1312     return($count, @results);
1313 } # sub isbnsearch
1314
1315
1316 sub keywordsearch {
1317   my ($keywordlist) = @_;
1318   my $dbh   = C4Connect;
1319   my $query = "Select * from biblio where";
1320   my $count = 0;
1321   my $sth;
1322   my @results;
1323   my @keywords = split(/ +/, $keywordlist);
1324   my $keyword = shift(@keywords);
1325
1326   $keyword =~ s/%/\\%/g;
1327   $keyword =~ s/_/\\_/;
1328   $keyword = "%" . $keyword . "%";
1329   $keyword = $dbh->quote($keyword);
1330   $query  .= " (author like $keyword) or
1331 (title like $keyword) or (unititle like $keyword) or
1332 (notes like $keyword) or (seriestitle like $keyword) or
1333 (abstract like $keyword)";
1334
1335   foreach $keyword (@keywords) {
1336     $keyword =~ s/%/\\%/;
1337     $keyword =~ s/_/\\_/;
1338     $keyword = "%" . $keyword . "%";
1339     $keyword = $dbh->quote($keyword);
1340     $query  .= " or (author like $keyword) or
1341 (title like $keyword) or (unititle like $keyword) or 
1342 (notes like $keyword) or (seriestitle like $keyword) or
1343 (abstract like $keyword)";
1344   } # foreach
1345   
1346   $sth = $dbh->prepare($query);
1347   $sth->execute;
1348   
1349   while (my $data = $sth->fetchrow_hashref) {
1350     $results[$count] = $data;
1351     $count++;
1352   } # while
1353   
1354   $sth->finish;
1355   $dbh->disconnect;
1356   return($count, @results);
1357 } # sub keywordsearch
1358
1359
1360 sub websitesearch {
1361     my ($keywordlist) = @_;
1362     my $dbh   = C4Connect;
1363     my $query = "Select distinct biblio.* from biblio, biblioitems where
1364 biblio.biblionumber = biblioitems.biblionumber and (";
1365     my $count = 0;
1366     my $sth;
1367     my @results;
1368     my @keywords = split(/ +/, $keywordlist);
1369     my $keyword = shift(@keywords);
1370
1371     $keyword =~ s/%/\\%/g;
1372     $keyword =~ s/_/\\_/;
1373     $keyword = "%" . $keyword . "%";
1374     $keyword = $dbh->quote($keyword);
1375     $query  .= " (url like $keyword)";
1376
1377     foreach $keyword (@keywords) {
1378         $keyword =~ s/%/\\%/;
1379         $keyword =~ s/_/\\_/;
1380         $keyword = "%" . $keyword . "%";
1381         $keyword = $dbh->quote($keyword);
1382         $query  .= " or (url like $keyword)";
1383     } # foreach
1384
1385     $query .= ")";
1386     $sth    = $dbh->prepare($query);
1387     $sth->execute;
1388
1389     while (my $data = $sth->fetchrow_hashref) {
1390         $results[$count] = $data;
1391         $count++;
1392     } # while
1393
1394     $sth->finish;
1395     $dbh->disconnect;
1396     return($count, @results);
1397 } # sub websitesearch
1398
1399
1400 sub addwebsite {
1401     my ($website) = @_;
1402     my $dbh = C4Connect;
1403     my $query;
1404     
1405     $website->{'biblionumber'} = $dbh->quote($website->{'biblionumber'});
1406     $website->{'title'}        = $dbh->quote($website->{'title'});
1407     $website->{'description'}  = $dbh->quote($website->{'description'});
1408     $website->{'url'}          = $dbh->quote($website->{'url'});
1409     
1410     $query = "Insert into websites set
1411 biblionumber = $website->{'biblionumber'},
1412 title        = $website->{'title'},
1413 description  = $website->{'description'},
1414 url          = $website->{'url'}";
1415     
1416     $dbh->do($query);
1417     
1418     $dbh->disconnect;
1419 } # sub website
1420
1421
1422 sub updatewebsite {
1423     my ($website) = @_;
1424     my $dbh = C4Connect;
1425     my $query;
1426     
1427     $website->{'title'}      = $dbh->quote($website->{'title'});
1428     $website->{'description'} = $dbh->quote($website->{'description'});
1429     $website->{'url'}        = $dbh->quote($website->{'url'});
1430     
1431     $query = "Update websites set
1432 title       = $website->{'title'},
1433 description = $website->{'description'},
1434 url         = $website->{'url'}
1435 where websitenumber = $website->{'websitenumber'}";
1436
1437     $dbh->do($query);
1438     
1439     $dbh->disconnect;
1440 } # sub updatewebsite
1441
1442
1443 sub deletewebsite {
1444     my ($websitenumber) = @_;
1445     my $dbh = C4Connect;
1446     my $query = "Delete from websites where websitenumber = $websitenumber";
1447     
1448     $dbh->do($query);
1449     
1450     $dbh->disconnect;
1451 } # sub deletewebsite
1452
1453
1454 END { }       # module clean-up code here (global destructor)