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