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