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