Added a couple of comments.
[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 # ***
22 # NOTE: This module is deprecated in Koha 1.3.x, and will shortly be
23 # deleted.
24 # ***
25
26 use strict;
27 require Exporter;
28 use C4::Context;
29  #use C4::Biblio;
30 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
31
32 # set the version for version checking
33 $VERSION = 0.01;
34
35 =head1 NAME
36
37 C4::Acquisitions - FIXME
38
39 =head1 SYNOPSIS
40
41   use C4::Acquisitions;
42
43 =head1 DESCRIPTION
44
45 FIXME
46
47 =head1 FUNCTIONS
48
49 =over 2
50
51 =cut
52
53 @ISA = qw(Exporter);
54 @EXPORT = qw(&getorders &bookseller &breakdown &basket &newbasket &bookfunds
55 &ordersearch &newbiblio &newbiblioitem &newsubject &newsubtitle &neworder
56 &newordernum &modbiblio &modorder &getsingleorder &invoice &receiveorder
57 &bookfundbreakdown &curconvert &updatesup &insertsup &newitems &modbibitem
58 &getcurrencies &modsubtitle &modsubject &modaddauthor &moditem &countitems 
59 &findall &needsmod &delitem &deletebiblioitem &delbiblio &delorder &branches
60 &getallorders &getrecorders &updatecurrencies &getorder &getcurrency &updaterecorder
61 &updatecost &checkitems &modnote &getitemtypes &getbiblio
62 &getbiblioitembybiblionumber
63 &getbiblioitem &getitemsbybiblioitem &isbnsearch
64 &websitesearch &addwebsite &updatewebsite &deletewebsite);
65 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
66
67 # your exported package globals go here,
68 # as well as any optionally exported functions
69
70 @EXPORT_OK   = qw($Var1 %Hashit);       # FIXME - Never used
71
72
73 # non-exported package globals go here
74 use vars qw(@more $stuff);              # FIXME - Never used
75
76 # initalize package globals, first exported ones
77 # FIXME - Never used
78 my $Var1   = '';
79 my %Hashit = ();
80
81
82
83 # then the others (which are still accessible as $Some::Module::stuff)
84 # FIXME - Never used
85 my $stuff  = '';
86 my @more   = ();
87
88 # all file-scoped lexicals must be created before
89 # the functions below that use them.
90
91 # file-private lexicals go here
92 # FIXME - Never used
93 my $priv_var    = '';
94 my %secret_hash = ();
95
96 # FIXME - Never used
97 # here's a file-private function as a closure,
98 # callable as &$priv_func;  it cannot be prototyped.
99 my $priv_func = sub {
100   # stuff goes here.
101   };
102   
103 # make all your functions, whether exported or not;
104
105 =item getorders
106
107   ($count, $orders) = &getorders($booksellerid);
108
109 Finds pending orders from the bookseller with the given ID. Ignores
110 completed and cancelled orders.
111
112 C<$count> is the number of elements in C<@{$orders}>.
113
114 C<$orders> is a reference-to-array; each element is a
115 reference-to-hash with the following fields:
116
117 =over 4
118
119 =item C<count(*)>
120
121 Gives the number of orders in with this basket number.
122
123 =item C<authorizedby>
124
125 =item C<entrydate>
126
127 =item C<basketno>
128
129 These give the value of the corresponding field in the aqorders table
130 of the Koha database.
131
132 =back
133
134 Results are ordered from most to least recent.
135
136 =cut
137 #'
138 # FIXME - This exact function already exists in C4::Catalogue
139 sub getorders {
140   my ($supplierid)=@_;
141   my $dbh = C4::Context->dbh;
142   my $query = "Select count(*),authorisedby,entrydate,basketno from aqorders where 
143   booksellerid='$supplierid' and (quantity > quantityreceived or
144   quantityreceived is NULL)
145   and (datecancellationprinted is NULL or datecancellationprinted = '0000-00-00')";
146   $query.=" group by basketno order by entrydate desc";
147   #print $query;
148   my $sth=$dbh->prepare($query);
149   $sth->execute;
150   my @results;
151   my $i=0;
152   while (my $data=$sth->fetchrow_hashref){
153     $results[$i]=$data;
154     $i++;
155   }
156   $sth->finish;
157   return ($i,\@results);
158 }
159
160 # Only used internally
161 # FIXME - This is the same as &C4::Biblio::itemcount, but not
162 # the same as &C4::Search::itemcount
163 sub itemcount{
164   my ($biblio)=@_;
165   my $dbh = C4::Context->dbh;
166   my $query="Select count(*) from items where biblionumber=$biblio";
167 #  print $query;
168   my $sth=$dbh->prepare($query);
169   $sth->execute;
170   my $data=$sth->fetchrow_hashref;
171   $sth->finish;
172   return($data->{'count(*)'});
173 }
174
175 =item getorder
176
177   ($order, $ordernumber) = &getorder($biblioitemnumber, $biblionumber);
178
179 Looks up the order with the given biblionumber and biblioitemnumber.
180
181 Returns a two-element array. C<$ordernumber> is the order number.
182 C<$order> is a reference-to-hash describing the order; its keys are
183 fields from the biblio, biblioitems, aqorders, and aqorderbreakdown
184 tables of the Koha database.
185
186 =cut
187 #'
188 # FIXME - There are functions &getorder and &getorders. Isn't this
189 # somewhat likely to cause confusion?
190 # FIXME - Almost the exact same function is already in C4::Catalogue
191 sub getorder{
192   my ($bi,$bib)=@_;
193   my $dbh = C4::Context->dbh;
194   my $query="Select ordernumber 
195         from aqorders 
196         where biblionumber=? and biblioitemnumber=?";
197   my $sth=$dbh->prepare($query);
198   $sth->execute($bib,$bi);
199   my $ordnum=$sth->fetchrow_hashref;
200   $sth->finish;
201   my $order=getsingleorder($ordnum->{'ordernumber'});
202 #  print $query;
203   return ($order,$ordnum->{'ordernumber'});
204 }
205
206 =item getsingleorder
207
208   $order = &getsingleorder($ordernumber);
209
210 Looks up an order by order number.
211
212 Returns a reference-to-hash describing the order. The keys of
213 C<$order> are fields from the biblio, biblioitems, aqorders, and
214 aqorderbreakdown tables of the Koha database.
215
216 =cut
217 #'
218 # FIXME - This is practically the same function as
219 # &C4::Catalogue::getsingleorder and &C4::Biblio::getsingleorder.
220 sub getsingleorder {
221   my ($ordnum)=@_;
222   my $dbh = C4::Context->dbh;
223   my $query="Select * from biblio,biblioitems,aqorders,aqorderbreakdown 
224   where aqorders.ordernumber=? 
225   and biblio.biblionumber=aqorders.biblionumber and
226   biblioitems.biblioitemnumber=aqorders.biblioitemnumber and
227   aqorders.ordernumber=aqorderbreakdown.ordernumber";
228   my $sth=$dbh->prepare($query);
229   $sth->execute($ordnum);
230   my $data=$sth->fetchrow_hashref;
231   $sth->finish;
232   return($data);
233 }
234
235 =item invoice
236
237   ($count, @results) = &invoice($booksellerinvoicenumber);
238
239 Looks up orders by invoice number.
240
241 Returns an array. C<$count> is the number of elements in C<@results>.
242 C<@results> is an array of references-to-hash; the keys of each
243 elements are fields from the aqorders, biblio, and biblioitems tables
244 of the Koha database.
245
246 =cut
247 #'
248 # FIXME - This exact function is already in C4::Catalogue
249 sub invoice {
250   my ($invoice)=@_;
251   my $dbh = C4::Context->dbh;
252   my $query="Select * from aqorders,biblio,biblioitems where
253   booksellerinvoicenumber='$invoice'
254   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=
255   aqorders.biblioitemnumber group by aqorders.ordernumber,aqorders.biblioitemnumber";
256   my $i=0;
257   my @results;
258   my $sth=$dbh->prepare($query);
259   $sth->execute;
260   while (my $data=$sth->fetchrow_hashref){
261     $results[$i]=$data;
262     $i++;
263   }
264   $sth->finish;
265   return($i,@results);
266 }
267
268 =item getallorders
269
270   ($count, @results) = &getallorders($booksellerid);
271
272 Looks up all of the pending orders from the supplier with the given
273 bookseller ID. Ignores cancelled orders.
274
275 C<$count> is the number of elements in C<@results>. C<@results> is an
276 array of references-to-hash. The keys of each element are fields from
277 the aqorders, biblio, and biblioitems tables of the Koha database.
278
279 C<@results> is sorted alphabetically by book title.
280
281 =cut
282 #'
283 # FIXME - Almost (but not quite) the same function appears in C4::Catalogue
284 # That one only lists incomplete orders.
285 sub getallorders {
286   #gets all orders from a certain supplier, orders them alphabetically
287   my ($supid)=@_;
288   my $dbh = C4::Context->dbh;
289   my $query="Select * from aqorders,biblio,biblioitems where booksellerid='$supid'
290   and (cancelledby is NULL or cancelledby = '')
291   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=                    
292   aqorders.biblioitemnumber 
293   group by aqorders.biblioitemnumber 
294   order by
295   biblio.title";
296   my $i=0;
297   my @results;
298   my $sth=$dbh->prepare($query);
299   $sth->execute;
300   while (my $data=$sth->fetchrow_hashref){
301     $results[$i]=$data;
302     $i++;
303   }
304   $sth->finish;
305   return($i,@results);
306 }
307
308 # FIXME - There's a getrecorders in C4::Catalogue
309 # FIXME - Never used (neither is the other one, actually)
310 sub getrecorders {
311   #gets all orders from a certain supplier, orders them alphabetically
312   my ($supid)=@_;
313   my $dbh = C4::Context->dbh;
314   my $query="Select * from aqorders,biblio,biblioitems where booksellerid='$supid'
315   and (cancelledby is NULL or cancelledby = '')
316   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=                    
317   aqorders.biblioitemnumber and
318   aqorders.quantityreceived>0
319   and aqorders.datereceived >=now()
320   group by aqorders.biblioitemnumber 
321   order by
322   biblio.title";
323   my $i=0;
324   my @results;
325   my $sth=$dbh->prepare($query);
326   $sth->execute;
327   while (my $data=$sth->fetchrow_hashref){
328     $results[$i]=$data;
329     $i++;
330   }
331   $sth->finish;
332   return($i,@results);
333 }
334
335 =item ordersearch
336
337   ($count, @results) = &ordersearch($search, $biblionumber, $complete);
338
339 Searches for orders.
340
341 C<$search> may take one of several forms: if it is an ISBN,
342 C<&ordersearch> returns orders with that ISBN. If C<$search> is an
343 order number, C<&ordersearch> returns orders with that order number
344 and biblionumber C<$biblionumber>. Otherwise, C<$search> is considered
345 to be a space-separated list of search terms; in this case, all of the
346 terms must appear in the title (matching the beginning of title
347 words).
348
349 If C<$complete> is C<yes>, the results will include only completed
350 orders. In any case, C<&ordersearch> ignores cancelled orders.
351
352 C<&ordersearch> returns an array. C<$count> is the number of elements
353 in C<@results>. C<@results> is an array of references-to-hash with the
354 following keys:
355
356 =over 4
357
358 =item C<author>
359
360 =item C<seriestitle>
361
362 =item C<branchcode>
363
364 =item C<bookfundid>
365
366 =back
367
368 =cut
369 #'
370 # FIXME - The same function (modulo whitespace) appears in C4::Catalogue
371 sub ordersearch {
372   my ($search,$biblio,$catview)=@_;
373   my $dbh = C4::Context->dbh;
374   my $query="Select *,biblio.title from aqorders,biblioitems,biblio
375         where aqorders.biblioitemnumber = biblioitems.biblioitemnumber
376         and biblio.biblionumber=aqorders.biblionumber
377         and ((datecancellationprinted is NULL)
378         or (datecancellationprinted = '0000-00-00'))
379   and ((";
380   my @data=split(' ',$search);
381   my $count=@data;
382   for (my $i=0;$i<$count;$i++){
383     $query.= "(biblio.title like '$data[$i]%' or biblio.title like '% $data[$i]%') and ";
384   }
385   $query=~ s/ and $//;
386   $query.=" ) or biblioitems.isbn='$search' 
387   or (aqorders.ordernumber='$search' and aqorders.biblionumber='$biblio')) ";
388   if ($catview ne 'yes'){
389     $query.=" and (quantityreceived < quantity or quantityreceived is NULL)";
390   }
391   $query.=" group by aqorders.ordernumber";
392   my $sth=$dbh->prepare($query);
393 #  print $query;
394   $sth->execute;
395   my $i=0;
396   my @results;
397   while (my $data=$sth->fetchrow_hashref){
398      my $sth2=$dbh->prepare("Select * from biblio where
399      biblionumber='$data->{'biblionumber'}'");
400      $sth2->execute;
401      my $data2=$sth2->fetchrow_hashref;
402      $sth2->finish;
403      $data->{'author'}=$data2->{'author'};
404      $data->{'seriestitle'}=$data2->{'seriestitle'};
405      $sth2=$dbh->prepare("Select * from aqorderbreakdown where
406     ordernumber=$data->{'ordernumber'}");
407     $sth2->execute;
408     $data2=$sth2->fetchrow_hashref;
409     $sth2->finish;
410     $data->{'branchcode'}=$data2->{'branchcode'};
411     $data->{'bookfundid'}=$data2->{'bookfundid'};
412     $results[$i]=$data;
413     $i++;
414   }
415   $sth->finish;
416   return($i,@results);
417 }
418
419 =item bookseller
420
421   ($count, @results) = &bookseller($searchstring);
422
423 Looks up a book seller. C<$searchstring> may be either a book seller
424 ID, or a string to look for in the book seller's name.
425
426 C<$count> is the number of elements in C<@results>. C<@results> is an
427 array of references-to-hash, whose keys are the fields of of the
428 aqbooksellers table in the Koha database.
429
430 =cut
431 #'
432 # FIXME - This function appears in C4::Catalogue
433 sub bookseller {
434   my ($searchstring)=@_;
435   my $dbh = C4::Context->dbh;
436   my $query="Select * from aqbooksellers where name like '%$searchstring%' or
437   id = '$searchstring'";
438   my $sth=$dbh->prepare($query);
439   $sth->execute;
440   my @results;
441   my $i=0;
442   while (my $data=$sth->fetchrow_hashref){
443     $results[$i]=$data;
444     $i++;
445   }
446   $sth->finish;
447   return($i,@results);
448 }
449
450 =item breakdown
451
452   ($count, $results) = &breakdown($ordernumber);
453
454 Looks up an order by order ID, and returns its breakdown.
455
456 C<$count> is the number of elements in C<$results>. C<$results> is a
457 reference-to-array; its elements are references-to-hash, whose keys
458 are the fields of the aqorderbreakdown table in the Koha database.
459
460 =cut
461 #'
462 # FIXME - This function appears in C4::Catalogue.
463 sub breakdown {
464   my ($id)=@_;
465   my $dbh = C4::Context->dbh;
466   my $query="Select * from aqorderbreakdown where ordernumber='$id'";
467   my $sth=$dbh->prepare($query);
468   $sth->execute;
469   my @results;
470   my $i=0;
471   while (my $data=$sth->fetchrow_hashref){
472     $results[$i]=$data;
473     $i++;
474   }
475   $sth->finish;
476   return($i,\@results);
477 }
478
479 =item basket
480
481   ($count, @orders) = &basket($basketnumber, $booksellerID);
482
483 Looks up the pending (non-cancelled) orders with the given basket
484 number. If C<$booksellerID> is non-empty, only orders from that seller
485 are returned.
486
487 C<&basket> returns a two-element array. C<@orders> is an array of
488 references-to-hash, whose keys are the fields from the aqorders,
489 biblio, and biblioitems tables in the Koha database. C<$count> is the
490 number of elements in C<@orders>.
491
492 =cut
493 #'
494 # FIXME - Almost the same function (with less error-checking) appears in
495 # C4::Catalogue.pm
496 sub basket {
497   my ($basketno,$supplier)=@_;
498   my $dbh = C4::Context->dbh;
499   my $query="Select *,biblio.title from aqorders,biblio,biblioitems 
500   where basketno='$basketno'
501   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber
502   =aqorders.biblioitemnumber 
503   and (datecancellationprinted is NULL or datecancellationprinted =
504   '0000-00-00')";
505   if (defined $supplier && $supplier ne ''){
506     $query.=" and aqorders.booksellerid='$supplier'";
507   } 
508   $query.=" group by aqorders.ordernumber";
509   my $sth=$dbh->prepare($query);
510   $sth->execute;
511   my @results;
512 #  print $query;
513   my $i=0;
514   while (my $data=$sth->fetchrow_hashref){
515     $results[$i]=$data;
516     $i++;
517   }
518   $sth->finish;
519   return($i,@results);
520 }
521
522 =item newbasket
523
524   $basket = &newbasket();
525
526 Finds the next unused basket number in the aqorders table of the Koha
527 database, and returns it.
528
529 =cut
530 #'
531 # FIXME - There's a race condition here:
532 #       A calls &newbasket
533 #       B calls &newbasket (gets the same number as A)
534 #       A updates the basket
535 #       B updates the basket, and clobbers A's result.
536 # A better approach might be to create a dummy order (with, say,
537 # requisitionedby == "Dummy-$$" or notes == "dummy <time> <pid>"), and
538 # see which basket number it gets. Then have a cron job periodically
539 # remove out-of-date dummy orders.
540 # FIXME - This function appears in C4::Catalogue.pm
541 sub newbasket {
542   my $dbh = C4::Context->dbh;
543   my $query="Select max(basketno) from aqorders";
544   my $sth=$dbh->prepare($query);
545   $sth->execute;
546   my $data=$sth->fetchrow_arrayref;
547   my $basket=$$data[0];
548   $basket++;
549   $sth->finish;
550   return($basket);
551 }
552
553 =item bookfunds
554
555   ($count, @results) = &bookfunds();
556
557 Returns a list of all book funds started on Sep 1, 2001.
558
559 C<$count> is the number of elements in C<@results>. C<@results> is an
560 array of references-to-hash, whose keys are fields from the aqbookfund
561 and aqbudget tables of the Koha database. Results are ordered
562 alphabetically by book fund name.
563
564 =cut
565 #'
566 # FIXME - An identical function (without the hardcoded date) appears in
567 # C4::Catalogue
568 sub bookfunds {
569   my $dbh = C4::Context->dbh;
570   my $query="Select * from aqbookfund,aqbudget where aqbookfund.bookfundid
571   =aqbudget.bookfundid 
572    and aqbudget.startdate='2001-07-01'
573   group by aqbookfund.bookfundid order by bookfundname";
574   my $sth=$dbh->prepare($query);
575   $sth->execute;
576   my @results;
577   my $i=0;
578   while (my $data=$sth->fetchrow_hashref){
579     $results[$i]=$data;
580     $i++;
581   }
582   $sth->finish;
583   return($i,@results);
584 }
585
586 =item branches
587
588   ($count, @results) = &branches();
589
590 Returns a list of all library branches.
591
592 C<$count> is the number of elements in C<@results>. C<@results> is an
593 array of references-to-hash, whose keys are the fields of the branches
594 table of the Koha database.
595
596 =cut
597 #'
598 # FIXME - This function (modulo whitespace) appears in C4::Catalogue
599 sub branches {
600   my $dbh = C4::Context->dbh;
601   my $query="Select * from branches";
602   my $sth=$dbh->prepare($query);
603   my $i=0;
604   my @results;
605
606     $sth->execute;
607   while (my $data = $sth->fetchrow_hashref){
608     $results[$i]=$data;
609     $i++;
610     } # while
611
612   $sth->finish;
613   return($i, @results);
614 } # sub branches
615
616 # FIXME - POD. But I can't figure out what this function is doing
617 # FIXME - An almost identical function appears in C4::Catalogue
618 sub bookfundbreakdown {
619   my ($id)=@_;
620   my $dbh = C4::Context->dbh;
621   my $query="Select quantity,datereceived,freight,unitprice,listprice,ecost,quantityreceived,subscription
622   from aqorders,aqorderbreakdown where bookfundid='$id' and 
623    aqorders.ordernumber=aqorderbreakdown.ordernumber and ((budgetdate >=
624    '2001-07-01' and budgetdate <'2002-07-01') or
625    (datereceived >= '2001-07-01' and datereceived < '2002-07-01'))
626   and (datecancellationprinted is NULL or
627   datecancellationprinted='0000-00-00')";
628   my $sth=$dbh->prepare($query);
629   $sth->execute;
630   my $comtd=0;
631   my $spent=0;
632   while (my $data=$sth->fetchrow_hashref){
633     if ($data->{'subscription'} == 1){
634       $spent+=$data->{'quantity'}*$data->{'unitprice'};
635     } else {
636       my $leftover=$data->{'quantity'}-$data->{'quantityreceived'};
637       $comtd+=($data->{'ecost'})*$leftover;
638       $spent+=($data->{'unitprice'})*$data->{'quantityreceived'};
639     }
640   }
641   $sth->finish;
642   return($spent,$comtd);
643 }
644
645 # FIXME - This is in effect identical to &C4::Biblio::newbiblio.
646 # Pick one and stick with it.
647 # XXX - POD
648 sub newbiblio {
649   my ($biblio) = @_;
650   my $dbh    = C4::Context->dbh;
651   my $query  = "Select max(biblionumber) from biblio";
652   my $sth    = $dbh->prepare($query);
653   $sth->execute;
654   my $data   = $sth->fetchrow_arrayref;
655   my $bibnum = $$data[0] + 1;
656   my $series = 0;
657
658   $biblio->{'title'}       = $dbh->quote($biblio->{'title'});
659   $biblio->{'author'}      = $dbh->quote($biblio->{'author'});
660   $biblio->{'copyright'}   = $dbh->quote($biblio->{'copyright'});
661   $biblio->{'seriestitle'} = $dbh->quote($biblio->{'seriestitle'});
662   $biblio->{'notes'}       = $dbh->quote($biblio->{'notes'});
663   $biblio->{'abstract'}    = $dbh->quote($biblio->{'abstract'});
664   if ($biblio->{'seriestitle'}) { $series = 1 };
665
666   $sth->finish;
667
668   $dbh->do(<<EOT);
669         INSERT INTO     biblio
670         SET             biblionumber  = $bibnum,
671                         title         = $biblio->{'title'},
672                         author        = $biblio->{'author'},
673                         copyrightdate = $biblio->{'copyright'},
674                         serial        = $series,
675                         seriestitle   = $biblio->{'seriestitle'},
676                         notes         = $biblio->{'notes'},
677                         abstract      = $biblio->{'abstract'}
678 EOT
679
680   return($bibnum);
681 }
682
683 # FIXME - This is in effect the same as &C4::Biblio::modbiblio.
684 # Pick one and stick with it.
685 # XXX - POD
686 sub modbiblio {
687   my ($biblio) = @_;
688   my $dbh   = C4::Context->dbh;
689   my $query;
690   my $sth;
691   
692   $biblio->{'title'}         = $dbh->quote($biblio->{'title'});
693   $biblio->{'author'}        = $dbh->quote($biblio->{'author'});
694   $biblio->{'abstract'}      = $dbh->quote($biblio->{'abstract'});
695   $biblio->{'copyrightdate'} = $dbh->quote($biblio->{'copyrightdate'});
696   $biblio->{'seriestitle'}   = $dbh->quote($biblio->{'serirestitle'});
697   $biblio->{'serial'}        = $dbh->quote($biblio->{'serial'});
698   $biblio->{'unititle'}      = $dbh->quote($biblio->{'unititle'});
699   $biblio->{'notes'}         = $dbh->quote($biblio->{'notes'});
700
701   $query = "Update biblio set
702 title         = $biblio->{'title'},
703 author        = $biblio->{'author'},
704 abstract      = $biblio->{'abstract'},
705 copyrightdate = $biblio->{'copyrightdate'},
706 seriestitle   = $biblio->{'seriestitle'},
707 serial        = $biblio->{'serial'},
708 unititle      = $biblio->{'unititle'},
709 notes         = $biblio->{'notes'}
710 where biblionumber = $biblio->{'biblionumber'}";
711   $sth   = $dbh->prepare($query);
712
713   $sth->execute;
714
715   $sth->finish;
716   return($biblio->{'biblionumber'});
717 } # sub modbiblio
718
719 # FIXME - This is in effect identical to &C4::Biblio::modsubtitle.
720 # Pick one and stick with it.
721 # XXX - POD
722 sub modsubtitle {
723   my ($bibnum, $subtitle) = @_;
724   my $dbh   = C4::Context->dbh;
725   my $query = "update bibliosubtitle set
726 subtitle = '$subtitle'
727 where biblionumber = $bibnum";
728   my $sth   = $dbh->prepare($query);
729
730   $sth->execute;
731   $sth->finish;
732 } # sub modsubtitle
733
734 # XXX - POD
735 # FIXME - This is functionally identical to &C4::Biblio::modaddauthor
736 # Pick one and stick with it.
737 sub modaddauthor {
738     my ($bibnum, $author) = @_;
739     my $dbh   = C4::Context->dbh;
740     my $query = "Delete from additionalauthors where biblionumber = $bibnum";
741     my $sth = $dbh->prepare($query);
742
743     $sth->execute;
744     $sth->finish;
745
746     if ($author ne '') {
747         $query = "Insert into additionalauthors set
748 author       = '$author',
749 biblionumber = '$bibnum'";
750         $sth   = $dbh->prepare($query);
751
752         $sth->execute;
753
754         $sth->finish;
755     } # if
756 } # sub modaddauthor
757
758 # FIXME - This is in effect identical to &C4::Biblio::modsubject.
759 # Pick one and stick with it.
760 # XXX - POD
761 sub modsubject {
762   my ($bibnum, $force, @subject) = @_;
763   my $dbh   = C4::Context->dbh;
764   my $count = @subject;
765   my $error;
766   for (my $i = 0; $i < $count; $i++) {
767     $subject[$i] =~ s/^ //g;
768     $subject[$i] =~ s/ $//g;
769     my $query = "select * from catalogueentry
770 where entrytype = 's'
771 and catalogueentry = '$subject[$i]'";
772     my $sth   = $dbh->prepare($query);
773     $sth->execute;
774
775     if (my $data = $sth->fetchrow_hashref) {
776     } else {
777       if ($force eq $subject[$i]) {
778
779          # subject not in aut, chosen to force anway
780          # so insert into cataloguentry so its in auth file
781          $query = "Insert into catalogueentry
782 (entrytype,catalogueentry)
783 values ('s','$subject[$i]')";
784          my $sth2 = $dbh->prepare($query);
785
786          $sth2->execute;
787          $sth2->finish;
788
789       } else {
790
791         $error = "$subject[$i]\n does not exist in the subject authority file";
792         $query = "Select * from catalogueentry
793 where entrytype = 's'
794 and (catalogueentry like '$subject[$i] %'
795 or catalogueentry like '% $subject[$i] %'
796 or catalogueentry like '% $subject[$i]')";
797         my $sth2 = $dbh->prepare($query);
798
799         $sth2->execute;
800         while (my $data = $sth2->fetchrow_hashref) {
801           $error = $error."<br>$data->{'catalogueentry'}";
802         } # while
803         $sth2->finish;
804       } # else
805     } # else
806     $sth->finish;
807   } # else
808
809   if ($error eq '') {
810     my $query = "Delete from bibliosubject where biblionumber = $bibnum";
811     my $sth   = $dbh->prepare($query);
812
813     $sth->execute;
814     $sth->finish;
815
816     for (my $i = 0; $i < $count; $i++) {
817       $sth = $dbh->prepare("Insert into bibliosubject
818 values ('$subject[$i]', $bibnum)");
819
820       $sth->execute;
821       $sth->finish;
822     } # for
823   } # if
824
825   return($error);
826 } # sub modsubject
827
828 # FIXME - This is very similar to &C4::Biblio::modbibitem.
829 # Pick one and stick with it.
830 # XXX - POD
831 sub modbibitem {
832     my ($biblioitem) = @_;
833     my $dbh   = C4::Context->dbh;
834
835     # FIXME -
836     #   foreach my $field (qw( ... ))
837     #   {
838     #           $biblioitem->{$field} = $dbh->quote($biblioitem->{$field});
839     #   }
840     $biblioitem->{'itemtype'}        = $dbh->quote($biblioitem->{'itemtype'});
841     $biblioitem->{'url'}             = $dbh->quote($biblioitem->{'url'});
842     $biblioitem->{'isbn'}            = $dbh->quote($biblioitem->{'isbn'});
843     $biblioitem->{'publishercode'}   = $dbh->quote($biblioitem->{'publishercode'});
844     $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
845     $biblioitem->{'classification'}  = $dbh->quote($biblioitem->{'classification'});
846     $biblioitem->{'dewey'}           = $dbh->quote($biblioitem->{'dewey'});
847     $biblioitem->{'subclass'}        = $dbh->quote($biblioitem->{'subclass'});
848     $biblioitem->{'illus'}           = $dbh->quote($biblioitem->{'illus'});
849     $biblioitem->{'pages'}           = $dbh->quote($biblioitem->{'pages'});
850     $biblioitem->{'volumeddesc'}     = $dbh->quote($biblioitem->{'volumeddesc'});
851     $biblioitem->{'notes'}           = $dbh->quote($biblioitem->{'notes'});
852     $biblioitem->{'size'}            = $dbh->quote($biblioitem->{'size'});
853     $biblioitem->{'place'}           = $dbh->quote($biblioitem->{'place'});
854
855     $dbh->do(<<EOT);
856         UPDATE  biblioitems
857         SET     itemtype        = $biblioitem->{'itemtype'},
858                 url             = $biblioitem->{'url'},
859                 isbn            = $biblioitem->{'isbn'},
860                 publishercode   = $biblioitem->{'publishercode'},
861                 publicationyear = $biblioitem->{'publicationyear'},
862                 classification  = $biblioitem->{'classification'},
863                 dewey           = $biblioitem->{'dewey'},
864                 subclass        = $biblioitem->{'subclass'},
865                 illus           = $biblioitem->{'illus'},
866                 pages           = $biblioitem->{'pages'},
867                 volumeddesc     = $biblioitem->{'volumeddesc'},
868                 notes           = $biblioitem->{'notes'},
869                 size            = $biblioitem->{'size'},
870                 place           = $biblioitem->{'place'}
871         WHERE   biblioitemnumber = $biblioitem->{'biblioitemnumber'}
872 EOT
873 } # sub modbibitem
874
875 # FIXME - This is in effect identical to &C4::Biblio::modnote.
876 # Pick one and stick with it.
877 # XXX - POD
878 sub modnote {
879   my ($bibitemnum,$note)=@_;
880   my $dbh = C4::Context->dbh;
881
882   $dbh->do(<<EOT);
883         UPDATE  biblioitems
884         SET     notes = '$note'
885         WHERE   biblioitemnumber = '$bibitemnum'
886 EOT
887 }
888
889 # XXX - POD
890 # FIXME - &C4::Biblio::newbiblioitem is quite similar to this
891 sub newbiblioitem {
892   my ($biblioitem) = @_;
893   my $dbh   = C4::Context->dbh;
894   my $query = "Select max(biblioitemnumber) from biblioitems";
895   my $sth   = $dbh->prepare($query);
896   my $data;
897   my $bibitemnum;
898
899   $biblioitem->{'volume'}          = $dbh->quote($biblioitem->{'volume'});
900   $biblioitem->{'number'}          = $dbh->quote($biblioitem->{'number'});
901   $biblioitem->{'classification'}  = $dbh->quote($biblioitem->{'classification'});
902   $biblioitem->{'itemtype'}        = $dbh->quote($biblioitem->{'itemtype'});
903   $biblioitem->{'url'}             = $dbh->quote($biblioitem->{'url'});
904   $biblioitem->{'isbn'}            = $dbh->quote($biblioitem->{'isbn'});
905   $biblioitem->{'issn'}            = $dbh->quote($biblioitem->{'issn'});
906   $biblioitem->{'dewey'}           = $dbh->quote($biblioitem->{'dewey'});
907   $biblioitem->{'subclass'}        = $dbh->quote($biblioitem->{'subclass'});
908   $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
909   $biblioitem->{'publishercode'}   = $dbh->quote($biblioitem->{'publishercode'});
910   $biblioitem->{'volumedate'}      = $dbh->quote($biblioitem->{'volumedate'});
911   $biblioitem->{'volumeddesc'}     = $dbh->quote($biblioitem->{'volumeddesc'});  $biblioitem->{'illus'}            = $dbh->quote($biblioitem->{'illus'});
912   $biblioitem->{'illus'}           = $dbh->quote($biblioitem->{'illus'});
913   $biblioitem->{'pages'}           = $dbh->quote($biblioitem->{'pages'});
914   $biblioitem->{'notes'}           = $dbh->quote($biblioitem->{'notes'});
915   $biblioitem->{'size'}            = $dbh->quote($biblioitem->{'size'});
916   $biblioitem->{'place'}           = $dbh->quote($biblioitem->{'place'});
917   $biblioitem->{'lccn'}            = $dbh->quote($biblioitem->{'lccn'});
918   $biblioitem->{'marc'}            = $dbh->quote($biblioitem->{'marc'});
919   
920   $sth->execute;
921   $data       = $sth->fetchrow_arrayref;
922   $bibitemnum = $$data[0] + 1;
923
924   $sth->finish;
925
926   $query = "insert into biblioitems set
927 biblioitemnumber = $bibitemnum,
928 biblionumber     = $biblioitem->{'biblionumber'},
929 volume           = $biblioitem->{'volume'},
930 number           = $biblioitem->{'number'},
931 classification   = $biblioitem->{'classification'},
932 itemtype         = $biblioitem->{'itemtype'},
933 url              = $biblioitem->{'url'},
934 isbn             = $biblioitem->{'isbn'},
935 issn             = $biblioitem->{'issn'},
936 dewey            = $biblioitem->{'dewey'},
937 subclass         = $biblioitem->{'subclass'},
938 publicationyear  = $biblioitem->{'publicationyear'},
939 publishercode    = $biblioitem->{'publishercode'},
940 volumedate       = $biblioitem->{'volumedate'},
941 volumeddesc      = $biblioitem->{'volumeddesc'},
942 illus            = $biblioitem->{'illus'},
943 pages            = $biblioitem->{'pages'},
944 notes            = $biblioitem->{'notes'},
945 size             = $biblioitem->{'size'},
946 lccn             = $biblioitem->{'lccn'},
947 marc             = $biblioitem->{'marc'},
948 place            = $biblioitem->{'place'}";
949
950   $sth = $dbh->prepare($query);
951   $sth->execute;
952
953   $sth->finish;
954   return($bibitemnum);
955 }
956
957 # FIXME - This is in effect identical to &C4::Biblio::newsubject.
958 # Pick one and stick with it.
959 # XXX - POD
960 sub newsubject {
961   my ($bibnum)=@_;
962   my $dbh = C4::Context->dbh;
963   my $query="insert into bibliosubject (biblionumber) values
964   ($bibnum)";
965   my $sth=$dbh->prepare($query);
966 #  print $query;
967   $sth->execute;
968   $sth->finish;
969 }
970
971 # XXX - POD
972 # FIXME - This is in effect the same as &C4::Biblio::newsubtitle
973 # Pick one and stick with it.
974 sub newsubtitle {
975   my ($bibnum, $subtitle) = @_;
976   my $dbh   = C4::Context->dbh;
977   $subtitle = $dbh->quote($subtitle);
978   my $query = "insert into bibliosubtitle set
979 biblionumber = $bibnum,
980 subtitle = $subtitle";
981   my $sth   = $dbh->prepare($query);
982
983   $sth->execute;
984
985   $sth->finish;
986 }
987
988 =item neworder
989
990   &neworder($biblionumber, $title, $ordnum, $basket, $quantity, $listprice,
991         $booksellerid, $who, $notes, $bookfund, $biblioitemnumber, $rrp,
992         $ecost, $gst, $budget, $unitprice, $subscription,
993         $booksellerinvoicenumber);
994
995 Adds a new order to the database. Any argument that isn't described
996 below is the new value of the field with the same name in the aqorders
997 table of the Koha database.
998
999 C<$ordnum> is a "minimum order number." After adding the new entry to
1000 the aqorders table, C<&neworder> finds the first entry in aqorders
1001 with order number greater than or equal to C<$ordnum>, and adds an
1002 entry to the aqorderbreakdown table, with the order number just found,
1003 and the book fund ID of the newly-added order.
1004
1005 C<$budget> is effectively ignored.
1006
1007 C<$subscription> may be either "yes", or anything else for "no".
1008
1009 =cut
1010 #'
1011 # FIXME - This function appears in C4::Catalogue
1012 sub neworder {
1013   my ($bibnum,$title,$ordnum,$basket,$quantity,$listprice,$supplier,$who,$notes,$bookfund,$bibitemnum,$rrp,$ecost,$gst,$budget,$cost,$sub,$invoice)=@_;
1014   if ($budget eq 'now'){
1015     $budget="now()";
1016   } else {
1017     $budget="'2001-07-01'";
1018   }
1019   if ($sub eq 'yes'){
1020     $sub=1;
1021   } else {
1022     $sub=0;
1023   }
1024   my $dbh = C4::Context->dbh;
1025   my $query="insert into aqorders (biblionumber,title,basketno,
1026   quantity,listprice,booksellerid,entrydate,requisitionedby,authorisedby,notes,
1027   biblioitemnumber,rrp,ecost,gst,unitprice,subscription,booksellerinvoicenumber)
1028
1029   values
1030   ($bibnum,'$title',$basket,$quantity,$listprice,'$supplier',now(),
1031   '$who','$who','$notes',$bibitemnum,'$rrp','$ecost','$gst','$cost',
1032   '$sub','$invoice')";
1033   my $sth=$dbh->prepare($query);
1034 #  print $query;
1035   $sth->execute;
1036   $sth->finish;
1037   $query="select * from aqorders where
1038   biblionumber=$bibnum and basketno=$basket and ordernumber >=$ordnum";
1039   $sth=$dbh->prepare($query);
1040   $sth->execute;
1041   my $data=$sth->fetchrow_hashref;
1042   $sth->finish;
1043   $ordnum=$data->{'ordernumber'};
1044   $query="insert into aqorderbreakdown (ordernumber,bookfundid) values
1045   ($ordnum,'$bookfund')";
1046   $sth=$dbh->prepare($query);
1047 #  print $query;
1048   $sth->execute;
1049   $sth->finish;
1050 }
1051
1052 =item delorder
1053
1054   &delorder($biblionumber, $ordernumber);
1055
1056 Cancel the order with the given order and biblio numbers. It does not
1057 delete any entries in the aqorders table, it merely marks them as
1058 cancelled.
1059
1060 If there are no items remaining with the given biblionumber,
1061 C<&delorder> also deletes them from the marc_subfield_table and
1062 marc_biblio tables of the Koha database.
1063
1064 =cut
1065 #'
1066 # FIXME - This function appears in C4::Catalogue
1067 sub delorder {
1068   my ($bibnum,$ordnum)=@_;
1069   my $dbh = C4::Context->dbh;
1070   my $query="update aqorders set datecancellationprinted=now()
1071   where biblionumber='$bibnum' and
1072   ordernumber='$ordnum'";
1073   my $sth=$dbh->prepare($query);
1074   #print $query;
1075   $sth->execute;
1076   $sth->finish;
1077   my $count=itemcount($bibnum);
1078   if ($count == 0){
1079     delbiblio($bibnum);
1080   }
1081 }
1082
1083 =item modorder
1084
1085   &modorder($title, $ordernumber, $quantity, $listprice,
1086         $biblionumber, $basketno, $supplier, $who, $notes,
1087         $bookfundid, $bibitemnum, $rrp, $ecost, $gst, $budget,
1088         $unitprice, $booksellerinvoicenumber);
1089
1090 Modifies an existing order. Updates the order with order number
1091 C<$ordernumber> and biblionumber C<$biblionumber>. All other arguments
1092 update the fields with the same name in the aqorders table of the Koha
1093 database.
1094
1095 Entries with order number C<$ordernumber> in the aqorderbreakdown
1096 table are also updated to the new book fund ID.
1097
1098 =cut
1099 #'
1100 # FIXME - This function appears in C4::Catalogue
1101 sub modorder {
1102   my ($title,$ordnum,$quantity,$listprice,$bibnum,$basketno,$supplier,$who,$notes,$bookfund,$bibitemnum,$rrp,$ecost,$gst,$budget,$cost,$invoice)=@_;
1103   my $dbh = C4::Context->dbh;
1104   my $query="update aqorders set title='$title',
1105   quantity='$quantity',listprice='$listprice',basketno='$basketno', 
1106   rrp='$rrp',ecost='$ecost',unitprice='$cost',
1107   booksellerinvoicenumber='$invoice'
1108   where
1109   ordernumber=$ordnum and biblionumber=$bibnum";
1110   my $sth=$dbh->prepare($query);
1111 #  print $query;
1112   $sth->execute;
1113   $sth->finish;
1114   $query="update aqorderbreakdown set bookfundid=$bookfund where
1115   ordernumber=$ordnum";
1116   $sth=$dbh->prepare($query);
1117 #  print $query;
1118   $sth->execute;
1119   $sth->finish;
1120 }
1121
1122 =item newordernum
1123
1124   $order = &newordernum();
1125
1126 Finds the next unused order number in the aqorders table of the Koha
1127 database, and returns it.
1128
1129 =cut
1130 #'
1131 # FIXME - Race condition
1132 # FIXME - This function appears in C4::Catalogue
1133 sub newordernum {
1134   my $dbh = C4::Context->dbh;
1135   my $query="Select max(ordernumber) from aqorders";
1136   my $sth=$dbh->prepare($query);
1137   $sth->execute;
1138   my $data=$sth->fetchrow_arrayref;
1139   my $ordnum=$$data[0];
1140   $ordnum++;
1141   $sth->finish;
1142   return($ordnum);
1143 }
1144
1145 =item receiveorder
1146
1147   &receiveorder($biblionumber, $ordernumber, $quantityreceived, $user,
1148         $unitprice, $booksellerinvoicenumber, $biblioitemnumber,
1149         $freight, $bookfund, $rrp);
1150
1151 Updates an order, to reflect the fact that it was received, at least
1152 in part. All arguments not mentioned below update the fields with the
1153 same name in the aqorders table of the Koha database.
1154
1155 Updates the order with bibilionumber C<$biblionumber> and ordernumber
1156 C<$ordernumber>.
1157
1158 Also updates the book fund ID in the aqorderbreakdown table.
1159
1160 =cut
1161 #'
1162 # FIXME - This function appears in C4::Catalogue
1163 sub receiveorder {
1164   my ($biblio,$ordnum,$quantrec,$user,$cost,$invoiceno,$bibitemno,$freight,$bookfund,$rrp)=@_;
1165   my $dbh = C4::Context->dbh;
1166   my $query="update aqorders set quantityreceived='$quantrec',
1167   datereceived=now(),booksellerinvoicenumber='$invoiceno',
1168   biblioitemnumber=$bibitemno,unitprice='$cost',freight='$freight',
1169   rrp='$rrp'
1170   where biblionumber=$biblio and ordernumber=$ordnum
1171   ";
1172 #  print $query;
1173   my $sth=$dbh->prepare($query);
1174   $sth->execute;
1175   $sth->finish;
1176   $query="update aqorderbreakdown set bookfundid=$bookfund where
1177   ordernumber=$ordnum";
1178   $sth=$dbh->prepare($query);
1179 #  print $query;
1180   $sth->execute;
1181   $sth->finish;  
1182 }
1183
1184 =item updaterecorder
1185
1186   &updaterecorder($biblionumber, $ordernumber, $user, $unitprice,
1187         $bookfundid, $rrp);
1188
1189 Updates the order with biblionumber C<$biblionumber> and order number
1190 C<$ordernumber>. C<$bookfundid> is the new value for the book fund ID
1191 in the aqorderbreakdown table of the Koha database. All other
1192 arguments update the fields with the same name in the aqorders table.
1193
1194 C<$user> is ignored.
1195
1196 =cut
1197 #'
1198 # FIXME - This function appears in C4::Catalogue
1199 sub updaterecorder{
1200   my($biblio,$ordnum,$user,$cost,$bookfund,$rrp)=@_;
1201   my $dbh = C4::Context->dbh;
1202   my $query="update aqorders set
1203   unitprice='$cost', rrp='$rrp'
1204   where biblionumber=$biblio and ordernumber=$ordnum
1205   ";
1206 #  print $query;
1207   my $sth=$dbh->prepare($query);
1208   $sth->execute;
1209   $sth->finish;
1210   $query="update aqorderbreakdown set bookfundid=$bookfund where
1211   ordernumber=$ordnum";
1212   $sth=$dbh->prepare($query);
1213 #  print $query;
1214   $sth->execute;
1215   $sth->finish;  
1216 }
1217
1218 =item curconvert
1219
1220   $foreignprice = &curconvert($currency, $localprice);
1221
1222 Converts the price C<$localprice> to foreign currency C<$currency> by
1223 dividing by the exchange rate, and returns the result.
1224
1225 If no exchange rate is found, C<&curconvert> assumes the rate is one
1226 to one.
1227
1228 =cut
1229 #'
1230 # FIXME - An almost identical version of this function appears in
1231 # C4::Catalogue
1232 sub curconvert {
1233   my ($currency,$price)=@_;
1234   my $dbh = C4::Context->dbh;
1235   my $query="Select rate from currency where currency='$currency'";
1236   my $sth=$dbh->prepare($query);
1237   $sth->execute;
1238   my $data=$sth->fetchrow_hashref;
1239   $sth->finish;
1240   my $cur=$data->{'rate'};
1241   if ($cur==0){
1242     $cur=1;
1243   }
1244   $price=$price / $cur;
1245   return($price);
1246 }
1247
1248 =item getcurrencies
1249
1250   ($count, $currencies) = &getcurrencies();
1251
1252 Returns the list of all known currencies.
1253
1254 C<$count> is the number of elements in C<$currencies>. C<$currencies>
1255 is a reference-to-array; its elements are references-to-hash, whose
1256 keys are the fields from the currency table in the Koha database.
1257
1258 =cut
1259 #'
1260 # FIXME - This function appears in C4::Catalogue
1261 sub getcurrencies {
1262   my $dbh = C4::Context->dbh;
1263   my $query="Select * from currency";
1264   my $sth=$dbh->prepare($query);
1265   $sth->execute;
1266   my @results;
1267   my $i=0;
1268   while (my $data=$sth->fetchrow_hashref){
1269     $results[$i]=$data;
1270     $i++;
1271   }
1272   $sth->finish;
1273   return($i,\@results);
1274
1275
1276 # FIXME - This function appears in C4::Catalogue. Neither one is used.
1277 sub getcurrency {
1278   my ($cur)=@_;
1279   my $dbh = C4::Context->dbh;
1280   my $query="Select * from currency where currency='$cur'";
1281   my $sth=$dbh->prepare($query);
1282   $sth->execute;
1283
1284   my $data=$sth->fetchrow_hashref;
1285   $sth->finish;
1286   return($data);
1287
1288
1289 =item updatecurrencies
1290
1291   &updatecurrencies($currency, $newrate);
1292
1293 Sets the exchange rate for C<$currency> to be C<$newrate>.
1294
1295 =cut
1296 #'
1297 # FIXME - This function appears in C4::Catalogue
1298 sub updatecurrencies {
1299   my ($currency,$rate)=@_;
1300   my $dbh = C4::Context->dbh;
1301   my $query="update currency set rate=$rate where currency='$currency'";
1302   my $sth=$dbh->prepare($query);
1303   $sth->execute;
1304   $sth->finish;
1305
1306
1307 =item updatesup
1308
1309   &updatesup($bookseller);
1310
1311 Updates the information for a given bookseller. C<$bookseller> is a
1312 reference-to-hash whose keys are the fields of the aqbooksellers table
1313 in the Koha database. It must contain entries for all of the fields.
1314 The entry to modify is determined by C<$bookseller-E<gt>{id}>.
1315
1316 The easiest way to get all of the necessary fields is to look up a
1317 book seller with C<&booksellers>, modify what's necessary, then call
1318 C<&updatesup> with the result.
1319
1320 =cut
1321 #'
1322 # FIXME - This function appears in C4::Catalogue
1323 sub updatesup {
1324    my ($data)=@_;
1325    my $dbh = C4::Context->dbh;
1326    my $query="Update aqbooksellers set
1327    name='$data->{'name'}',address1='$data->{'address1'}',address2='$data->{'address2'}',
1328    address3='$data->{'address3'}',address4='$data->{'address4'}',postal='$data->{'postal'}',
1329    phone='$data->{'phone'}',fax='$data->{'fax'}',url='$data->{'url'}',
1330    contact='$data->{'contact'}',contpos='$data->{'contpos'}',
1331    contphone='$data->{'contphone'}', contfax='$data->{'contfax'}', contaltphone=
1332    '$data->{'contaltphone'}', contemail='$data->{'contemail'}', contnotes=
1333    '$data->{'contnotes'}', active=$data->{'active'},
1334    listprice='$data->{'listprice'}', invoiceprice='$data->{'invoiceprice'}',
1335    gstreg=$data->{'gstreg'}, listincgst=$data->{'listincgst'},
1336    invoiceincgst=$data->{'invoiceincgst'}, specialty='$data->{'specialty'}',
1337    discount='$data->{'discount'}',invoicedisc='$data->{'invoicedisc'}',
1338    nocalc='$data->{'nocalc'}'
1339    where id='$data->{'id'}'";
1340    my $sth=$dbh->prepare($query);
1341    $sth->execute;
1342    $sth->finish;
1343 #   print $query;
1344 }
1345
1346 # XXX - POD
1347 sub insertsup {
1348   my ($data)=@_;
1349   my $dbh = C4::Context->dbh;
1350   my $sth=$dbh->prepare("Select max(id) from aqbooksellers");
1351   $sth->execute;
1352   my $data2=$sth->fetchrow_hashref;
1353   $sth->finish;
1354   $data2->{'max(id)'}++;
1355   $sth=$dbh->prepare("Insert into aqbooksellers (id) values ($data2->{'max(id)'})");
1356   $sth->execute;
1357   $sth->finish;
1358   $data->{'id'}=$data2->{'max(id)'};
1359   updatesup($data);
1360   return($data->{'id'});
1361 }
1362
1363 =item insertsup
1364
1365   $id = &insertsup($bookseller);
1366
1367 Creates a new bookseller. C<$bookseller> is a reference-to-hash whose
1368 keys are the fields of the aqbooksellers table in the Koha database.
1369 All fields must be present.
1370
1371 Returns the ID of the newly-created bookseller.
1372
1373 =cut
1374 #'
1375 # FIXME - This function appears in C4::Catalogue
1376 # FIXME - This is different from &C4::Biblio::newitems, though both
1377 # are exported.
1378 sub newitems {
1379   my ($item, @barcodes) = @_;
1380   my $dbh   = C4::Context->dbh;
1381   my $query = "Select max(itemnumber) from items";
1382   my $sth   = $dbh->prepare($query);
1383   my $data;
1384   my $itemnumber;
1385   my $error;
1386
1387   $sth->execute;
1388   $data       = $sth->fetchrow_hashref;
1389   $itemnumber = $data->{'max(itemnumber)'} + 1;
1390   $sth->finish;
1391   
1392   $item->{'booksellerid'}     = $dbh->quote($item->{'booksellerid'});
1393   $item->{'homebranch'}       = $dbh->quote($item->{'homebranch'});
1394   $item->{'price'}            = $dbh->quote($item->{'price'});
1395   $item->{'replacementprice'} = $dbh->quote($item->{'replacementprice'});
1396   $item->{'itemnotes'}        = $dbh->quote($item->{'itemnotes'});
1397
1398   foreach my $barcode (@barcodes) {
1399     $barcode = uc($barcode);
1400     $barcode = $dbh->quote($barcode);
1401     $query   = "Insert into items set
1402 itemnumber           = $itemnumber,
1403 biblionumber         = $item->{'biblionumber'},
1404 biblioitemnumber     = $item->{'biblioitemnumber'},
1405 barcode              = $barcode,
1406 booksellerid         = $item->{'booksellerid'},
1407 dateaccessioned      = NOW(),
1408 homebranch           = $item->{'homebranch'},
1409 holdingbranch        = $item->{'homebranch'},
1410 price                = $item->{'price'},
1411 replacementprice     = $item->{'replacementprice'},
1412 replacementpricedate = NOW(),
1413 itemnotes            = $item->{'itemnotes'}";
1414
1415     if ($item->{'loan'}) {
1416       $query .= ",
1417 notforloan           = $item->{'loan'}";
1418     } # if
1419
1420     $sth = $dbh->prepare($query);
1421     $sth->execute;
1422
1423     $error .= $sth->errstr;
1424
1425     $sth->finish;
1426     $itemnumber++;
1427   } # for
1428
1429   return($error);
1430 }
1431
1432 # FIXME - This is the same as &C4::Biblio::Checkitems.
1433 # Pick one and stick with it.
1434 # XXX - POD
1435 sub checkitems{
1436   my ($count,@barcodes)=@_;
1437   my $dbh = C4::Context->dbh;
1438   my $error;
1439   for (my $i=0;$i<$count;$i++){
1440     $barcodes[$i]=uc $barcodes[$i];
1441     my $query="Select * from items where barcode='$barcodes[$i]'";
1442     my $sth=$dbh->prepare($query);
1443     $sth->execute;
1444     if (my $data=$sth->fetchrow_hashref){
1445       $error.=" Duplicate Barcode: $barcodes[$i]";
1446     }
1447     $sth->finish;
1448   }
1449   return($error);
1450 }
1451
1452 # FIXME - This appears to be functionally equivalent to
1453 # &C4::Biblio::moditem.
1454 # Pick one and stick with it.
1455 # XXX - POD
1456 sub moditem {
1457   my ($loan,$itemnum,$bibitemnum,$barcode,$notes,$homebranch,$lost,$wthdrawn,$replacement)=@_;
1458   my $dbh = C4::Context->dbh;
1459   my $query="update items set biblioitemnumber=$bibitemnum,
1460   barcode='$barcode',itemnotes='$notes'
1461   where itemnumber=$itemnum";
1462   if ($barcode eq ''){
1463     $query="update items set biblioitemnumber=$bibitemnum,notforloan=$loan where itemnumber=$itemnum";
1464   }
1465   if ($lost ne ''){
1466     $query="update items set biblioitemnumber=$bibitemnum,
1467       barcode='$barcode',itemnotes='$notes',homebranch='$homebranch',
1468       itemlost='$lost',wthdrawn='$wthdrawn' where itemnumber=$itemnum";
1469   }
1470   if ($replacement ne ''){
1471     $query=~ s/ where/,replacementprice='$replacement' where/;
1472   }
1473
1474   my $sth=$dbh->prepare($query);
1475   $sth->execute;
1476   $sth->finish;
1477 }
1478
1479 # FIXME - This function appears in C4::Catalogue. Neither one is used
1480 sub updatecost{
1481   my($price,$rrp,$itemnum)=@_;
1482   my $dbh = C4::Context->dbh;
1483   my $query="update items set price='$price',replacementprice='$rrp'
1484   where itemnumber=$itemnum";
1485   my $sth=$dbh->prepare($query);
1486   $sth->execute;
1487   $sth->finish;
1488 }
1489
1490 # FIXME - This is identical to &C4::Biblio::countitems.
1491 # Pick one and stick with it.
1492 # XXX - POD
1493 sub countitems{
1494   my ($bibitemnum)=@_;
1495   my $dbh = C4::Context->dbh;
1496   my $query="Select count(*) from items where biblioitemnumber='$bibitemnum'";
1497   my $sth=$dbh->prepare($query);
1498   $sth->execute;
1499   my $data=$sth->fetchrow_hashref;
1500   $sth->finish;
1501   return($data->{'count(*)'});
1502 }
1503
1504 # FIXME - This function appears in C4::Catalogue. Neither one is used.
1505 sub findall {
1506   my ($biblionumber)=@_;
1507   my $dbh = C4::Context->dbh;
1508   my $query="Select * from biblioitems,items,itemtypes where 
1509   biblioitems.biblionumber=$biblionumber 
1510   and biblioitems.biblioitemnumber=items.biblioitemnumber and
1511   itemtypes.itemtype=biblioitems.itemtype
1512   order by items.biblioitemnumber";
1513   my $sth=$dbh->prepare($query);
1514   $sth->execute;
1515   my @results;
1516   my $i;
1517   while (my $data=$sth->fetchrow_hashref){
1518     $results[$i]=$data;
1519     $i++;
1520   }
1521   $sth->finish;
1522   return(@results);
1523 }
1524
1525 # FIXME - This function appears in C4::Catalogue. Neither one is used
1526 sub needsmod{
1527   my ($bibitemnum,$itemtype)=@_;
1528   my $dbh = C4::Context->dbh;
1529   my $query="Select * from biblioitems where biblioitemnumber=$bibitemnum
1530   and itemtype='$itemtype'";
1531   my $sth=$dbh->prepare($query);
1532   $sth->execute;
1533   my $result=0;
1534   if (my $data=$sth->fetchrow_hashref){
1535     $result=1;
1536   }
1537   $sth->finish;
1538   return($result);
1539 }
1540
1541 # FIXME - A nearly-identical function, appears in C4::Biblio
1542 # Pick one and stick with it.
1543 # XXX - POD
1544 sub delitem{
1545   my ($itemnum)=@_;
1546   my $dbh = C4::Context->dbh;
1547   my $query="select * from items where itemnumber=$itemnum";
1548   my $sth=$dbh->prepare($query);
1549   $sth->execute;
1550   my @data=$sth->fetchrow_array;
1551   $sth->finish;
1552   $query="Insert into deleteditems values (";
1553   foreach my $temp (@data){
1554     $query=$query."'$temp',";
1555   }
1556   $query=~ s/\,$/\)/;
1557 #  print $query;
1558   $sth=$dbh->prepare($query);
1559   $sth->execute;
1560   $sth->finish;
1561   $query = "Delete from items where itemnumber=$itemnum";
1562   $sth=$dbh->prepare($query);
1563   $sth->execute;
1564   $sth->finish;
1565 }
1566
1567 # FIXME - This is functionally identical to &C4::Biblio::deletebiblioitem.
1568 # Pick one and stick with it.
1569 # XXX - POD
1570 sub deletebiblioitem {
1571     my ($biblioitemnumber) = @_;
1572     my $dbh   = C4::Context->dbh;
1573     my $query = "Select * from biblioitems
1574 where biblioitemnumber = $biblioitemnumber";
1575     my $sth   = $dbh->prepare($query);
1576     my @results;
1577
1578     $sth->execute;
1579   
1580     if (@results = $sth->fetchrow_array) {
1581
1582         $query = "Insert into deletedbiblioitems values (";
1583         foreach my $value (@results) {
1584             $value  = $dbh->quote($value);
1585             $query .= "$value,";
1586         } # foreach
1587
1588         $query =~ s/\,$/\)/;
1589         $dbh->do($query);
1590
1591         $dbh->do(<<EOT);
1592                 DELETE FROM     biblioitems
1593                 WHERE           biblioitemnumber = $biblioitemnumber
1594 EOT
1595     } # if
1596
1597     $sth->finish;
1598
1599 # Now delete all the items attached to the biblioitem
1600
1601     $query = "Select * from items where biblioitemnumber = $biblioitemnumber";
1602     $sth   = $dbh->prepare($query);
1603
1604     $sth->execute;
1605
1606     while (@results = $sth->fetchrow_array) {
1607
1608         $query = "Insert into deleteditems values (";
1609         foreach my $value (@results) {
1610             $value  = $dbh->quote($value);
1611             $query .= "$value,";
1612         } # foreach
1613
1614         $query =~ s/\,$/\)/;
1615         $dbh->do($query);
1616     } # while
1617
1618     $sth->finish;       # FIXME - This is bogus, isn't it?
1619
1620     $dbh->do(<<EOT);
1621         DELETE FROM     items
1622         WHERE           biblioitemnumber = $biblioitemnumber
1623 EOT
1624     
1625 } # sub deletebiblioitem
1626
1627 # FIXME - This is functionally identical to &C4::Biblio::delbiblio.
1628 # Pick one and stick with it.
1629 # XXX - POD
1630 sub delbiblio{
1631   my ($biblio)=@_;
1632   my $dbh = C4::Context->dbh;
1633   my $query="select * from biblio where biblionumber=$biblio";
1634   my $sth=$dbh->prepare($query);
1635   $sth->execute;
1636   if (my @data=$sth->fetchrow_array){
1637     $sth->finish;
1638     $query="Insert into deletedbiblio values (";
1639     foreach my $temp (@data){
1640       $temp=~ s/\'/\\\'/g;
1641       $query=$query."'$temp',";
1642     }
1643     $query=~ s/\,$/\)/;
1644 #   print $query;
1645     $sth=$dbh->prepare($query);
1646     $sth->execute;
1647     $sth->finish;
1648     $query = "Delete from biblio where biblionumber=$biblio";
1649     $sth=$dbh->prepare($query);
1650     $sth->execute;
1651     $sth->finish;
1652   }
1653
1654   $sth->finish;
1655 }
1656
1657 # XXX - POD
1658 sub getitemtypes {
1659   my $dbh   = C4::Context->dbh;
1660   my $query = "select * from itemtypes";
1661   my $sth   = $dbh->prepare($query);
1662     # || die "Cannot prepare $query" . $dbh->errstr;
1663   my $count = 0;
1664   my @results;
1665   
1666   $sth->execute;
1667     # || die "Cannot execute $query\n" . $sth->errstr;
1668   while (my $data = $sth->fetchrow_hashref) {
1669     $results[$count] = $data;
1670     $count++;
1671   } # while
1672   
1673   $sth->finish;
1674   return($count, @results);
1675 } # sub getitemtypes
1676
1677 # FIXME - This is identical to &C4::Biblio::getitemtypes.
1678 # Pick one and stick with it.
1679 # XXX - POD
1680 sub getbiblio {
1681     my ($biblionumber) = @_;
1682     my $dbh   = C4::Context->dbh;
1683     my $query = "Select * from biblio where biblionumber = $biblionumber";
1684     my $sth   = $dbh->prepare($query);
1685       # || die "Cannot prepare $query\n" . $dbh->errstr;
1686     my $count = 0;
1687     my @results;
1688     
1689     $sth->execute;
1690       # || die "Cannot execute $query\n" . $sth->errstr;
1691     while (my $data = $sth->fetchrow_hashref) {
1692       $results[$count] = $data;
1693       $count++;
1694     } # while
1695     
1696     $sth->finish;
1697     return($count, @results);
1698 } # sub getbiblio
1699
1700 # XXX - POD
1701 sub getbiblioitem {
1702     my ($biblioitemnum) = @_;
1703     my $dbh   = C4::Context->dbh;
1704     my $query = "Select * from biblioitems where
1705 biblioitemnumber = $biblioitemnum";
1706     my $sth   = $dbh->prepare($query);
1707     my $count = 0;
1708     my @results;
1709
1710     $sth->execute;
1711
1712     while (my $data = $sth->fetchrow_hashref) {
1713         $results[$count] = $data;
1714         $count++;
1715     } # while
1716
1717     $sth->finish;
1718     return($count, @results);
1719 } # sub getbiblioitem
1720
1721 # FIXME - This is identical to &C4::Biblio::getbiblioitem.
1722 # Pick one and stick with it.
1723 # XXX - POD
1724 sub getbiblioitembybiblionumber {
1725     my ($biblionumber) = @_;
1726     my $dbh   = C4::Context->dbh;
1727     my $query = "Select * from biblioitems where biblionumber =
1728 $biblionumber";
1729     my $sth   = $dbh->prepare($query);
1730     my $count = 0;
1731     my @results;
1732
1733     $sth->execute;
1734
1735     while (my $data = $sth->fetchrow_hashref) {
1736         $results[$count] = $data;
1737         $count++;
1738     } # while
1739
1740     $sth->finish;
1741     return($count, @results);
1742 } # sub
1743
1744 # FIXME - This is identical to
1745 # &C4::Biblio::getbiblioitembybiblionumber.
1746 # Pick one and stick with it.
1747 # XXX - POD
1748 sub getitemsbybiblioitem {
1749     my ($biblioitemnum) = @_;
1750     my $dbh   = C4::Context->dbh;
1751     my $query = "Select * from items, biblio where
1752 biblio.biblionumber = items.biblionumber and biblioitemnumber
1753 = $biblioitemnum";
1754     my $sth   = $dbh->prepare($query);
1755       # || die "Cannot prepare $query\n" . $dbh->errstr;
1756     my $count = 0;
1757     my @results;
1758     
1759     $sth->execute;
1760       # || die "Cannot execute $query\n" . $sth->errstr;
1761     while (my $data = $sth->fetchrow_hashref) {
1762       $results[$count] = $data;
1763       $count++;
1764     } # while
1765     
1766     $sth->finish;
1767     return($count, @results);
1768 } # sub getitemsbybiblioitem
1769
1770 # FIXME - This is identical to &C4::Biblio::isbnsearch.
1771 # Pick one and stick with it.
1772 # XXX - POD
1773 sub isbnsearch {
1774     my ($isbn) = @_;
1775     my $dbh   = C4::Context->dbh;
1776     my $count = 0;
1777     my $query;
1778     my $sth;
1779     my @results;
1780     
1781     $isbn  = $dbh->quote($isbn);
1782     $query = "Select biblio.* from biblio, biblioitems where
1783 biblio.biblionumber = biblioitems.biblionumber
1784 and isbn = $isbn";
1785     $sth   = $dbh->prepare($query);
1786     
1787     $sth->execute;
1788     while (my $data = $sth->fetchrow_hashref) {
1789         $results[$count] = $data;
1790         $count++;
1791     } # while
1792
1793     $sth->finish;
1794     return($count, @results);
1795 } # sub isbnsearch
1796
1797 =item websitesearch
1798
1799   ($count, @results) = &websitesearch($keywordlist);
1800
1801 Looks up biblioitems by URL.
1802
1803 C<$keywordlist> is a space-separated list of search terms.
1804 C<&websitesearch> returns those biblioitems whose URL contains at
1805 least one of the search terms.
1806
1807 C<$count> is the number of elements in C<@results>. C<@results> is an
1808 array of references-to-hash, whose keys are the fields of the biblio
1809 and biblioitems tables in the Koha database.
1810
1811 =cut
1812 #'
1813 # FIXME - This function appears in C4::Catalogue
1814 sub websitesearch {
1815     my ($keywordlist) = @_;
1816     my $dbh   = C4::Context->dbh;
1817     my $query = "Select distinct biblio.* from biblio, biblioitems where
1818 biblio.biblionumber = biblioitems.biblionumber and (";
1819     my $count = 0;
1820     my $sth;
1821     my @results;
1822     my @keywords = split(/ +/, $keywordlist);
1823     my $keyword = shift(@keywords);
1824
1825     $keyword =~ s/%/\\%/g;
1826     $keyword =~ s/_/\\_/;
1827     $keyword = "%" . $keyword . "%";
1828     $keyword = $dbh->quote($keyword);
1829     $query  .= " (url like $keyword)";
1830
1831     foreach $keyword (@keywords) {
1832         $keyword =~ s/%/\\%/;
1833         $keyword =~ s/_/\\_/;
1834         $keyword = "%" . $keyword . "%";
1835         $keyword = $dbh->quote($keyword);
1836         $query  .= " or (url like $keyword)";
1837     } # foreach
1838
1839     $query .= ")";
1840     $sth    = $dbh->prepare($query);
1841     $sth->execute;
1842
1843     while (my $data = $sth->fetchrow_hashref) {
1844         $results[$count] = $data;
1845         $count++;
1846     } # while
1847
1848     $sth->finish;
1849     return($count, @results);
1850 } # sub websitesearch
1851
1852 =item addwebsite
1853
1854   &addwebsite($website);
1855
1856 Adds a new web site. C<$website> is a reference-to-hash, with the keys
1857 C<biblionumber>, C<title>, C<description>, and C<url>. All of these
1858 are mandatory.
1859
1860 =cut
1861 #'
1862 # FIXME - This function appears in C4::Catalogue
1863 sub addwebsite {
1864     my ($website) = @_;
1865     my $dbh = C4::Context->dbh;
1866     
1867     $website->{'biblionumber'} = $dbh->quote($website->{'biblionumber'});
1868     $website->{'title'}        = $dbh->quote($website->{'title'});
1869     $website->{'description'}  = $dbh->quote($website->{'description'});
1870     $website->{'url'}          = $dbh->quote($website->{'url'});
1871     
1872     $dbh->do(<<EOT);
1873         INSERT INTO     websites
1874         SET             biblionumber = $website->{'biblionumber'},
1875                         title        = $website->{'title'},
1876                         description  = $website->{'description'},
1877                         url          = $website->{'url'}
1878 EOT
1879 } # sub website
1880
1881 =item updatewebsite
1882
1883   &updatewebsite($website);
1884
1885 Updates an existing web site. C<$website> is a reference-to-hash with
1886 the keys C<websitenumber>, C<title>, C<description>, and C<url>. All
1887 of these are mandatory. C<$website-E<gt>{websitenumber}> identifies
1888 the entry to update.
1889
1890 =cut
1891 #'
1892 # FIXME - This function appears in C4::Catalogue
1893 sub updatewebsite {
1894     my ($website) = @_;
1895     my $dbh = C4::Context->dbh;
1896     
1897     $website->{'title'}      = $dbh->quote($website->{'title'});
1898     $website->{'description'} = $dbh->quote($website->{'description'});
1899     $website->{'url'}        = $dbh->quote($website->{'url'});
1900     
1901     $dbh->do(<<EOT);
1902         UPDATE  websites
1903         SET     title       = $website->{'title'},
1904                 description = $website->{'description'},
1905                 url         = $website->{'url'}
1906                 where websitenumber = $website->{'websitenumber'}
1907 EOT
1908 } # sub updatewebsite
1909
1910 =item deletewebsite
1911
1912   &deletewebsite($websitenumber);
1913
1914 Deletes the web site with number C<$websitenumber>.
1915
1916 =cut
1917 #'
1918 # FIXME - This function appears in C4::Catalogue
1919 sub deletewebsite {
1920     my ($websitenumber) = @_;
1921     my $dbh = C4::Context->dbh;
1922
1923     $dbh->do(<<EOT);
1924         DELETE FROM     websites
1925         WHERE           websitenumber = $websitenumber
1926 EOT
1927 } # sub deletewebsite
1928
1929
1930 END { }       # module clean-up code here (global destructor)
1931
1932 1;
1933 __END__
1934
1935 =back
1936
1937 =head1 AUTHOR
1938
1939 Koha Developement team <info@koha.org>
1940
1941 =head1 SEE ALSO
1942
1943 L<perl>.
1944
1945 =cut