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