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