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