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