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