Added POD.
[koha.git] / C4 / Catalogue.pm
1 package C4::Catalogue; #assumes C4/Acquisitions.pm
2
3 # Continue working on updateItem!!!!!!
4 #
5 # updateItem is looking not bad.  Need to add addSubfield and deleteSubfield
6 # functions
7 #
8 # Trying to track down $dbh's that aren't disconnected....
9 #
10
11
12
13 # Copyright 2000-2002 Katipo Communications
14 #
15 # This file is part of Koha.
16 #
17 # Koha is free software; you can redistribute it and/or modify it under the
18 # terms of the GNU General Public License as published by the Free Software
19 # Foundation; either version 2 of the License, or (at your option) any later
20 # version.
21 #
22 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
23 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
24 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License along with
27 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
28 # Suite 330, Boston, MA  02111-1307 USA
29
30 use strict;
31 require Exporter;
32 use C4::Database;
33 use MARC::Record;
34 use C4::Biblio;
35
36 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
37
38 # set the version for version checking
39 $VERSION = 0.01;
40
41 =head1 NAME
42
43 C4::Catalogue - FIXME
44
45 =head1 SYNOPSIS
46
47   use C4::Catalogue;
48
49 =head1 DESCRIPTION
50
51 FIXME
52
53 =head1 FUNCTIONS
54
55 =over 2
56
57 =cut
58
59 @ISA = qw(Exporter);
60 @EXPORT = qw(
61              &basket &newbasket
62
63              &getorders &getallorders &getrecorders
64              &getorder &neworder &delorder
65              &ordersearch
66              &modorder &getsingleorder &invoice &receiveorder
67              &updaterecorder &newordernum
68
69              &bookfunds &bookfundbreakdown &updatecost
70              &curconvert &getcurrencies &updatecurrencies &getcurrency
71
72              &findall &needsmod &branches &updatesup &insertsup
73              &bookseller &breakdown &checkitems
74              &websitesearch &addwebsite &updatewebsite &deletewebsite
75 );
76 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
77
78 # your exported package globals go here,
79 # as well as any optionally exported functions
80
81 @EXPORT_OK   = qw($Var1 %Hashit);       # FIXME - Unused
82
83
84 # non-exported package globals go here
85 use vars qw(@more $stuff);              # FIXME - Unused
86
87 # initalize package globals, first exported ones
88 # FIXME - Unused
89 my $Var1   = '';
90 my %Hashit = ();
91
92
93 # then the others (which are still accessible as $Some::Module::stuff)
94 # FIXME - Unused
95 my $stuff  = '';
96 my @more   = ();
97
98 # all file-scoped lexicals must be created before
99 # the functions below that use them.
100
101 # file-private lexicals go here
102 # FIXME - Unused
103 my $priv_var    = '';
104 my %secret_hash = ();
105
106 # here's a file-private function as a closure,
107 # callable as &$priv_func;  it cannot be prototyped.
108 # FIXME - Unused
109 my $priv_func = sub {
110   # stuff goes here.
111   };
112
113 # make all your functions, whether exported or not;
114
115
116 #
117 #
118 #
119 # BASKETS
120 #
121 #
122 #
123 =item basket
124
125   ($count, @orders) = &basket($basketnumber, $booksellerID);
126
127 Looks up the pending (non-cancelled) orders with the given basket
128 number. If C<$booksellerID> is non-empty, only orders from that seller
129 are returned.
130
131 C<&basket> returns a two-element array. C<@orders> is an array of
132 references-to-hash, whose keys are the fields from the aqorders,
133 biblio, and biblioitems tables in the Koha database. C<$count> is the
134 number of elements in C<@orders>.
135
136 =cut
137 #'
138 sub basket {
139   my ($basketno,$supplier)=@_;
140   my $dbh=C4Connect;
141   my $query="Select *,biblio.title from aqorders,biblio,biblioitems
142   where basketno='$basketno'
143   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber
144   =aqorders.biblioitemnumber
145   and (datecancellationprinted is NULL or datecancellationprinted =
146   '0000-00-00')";
147   if ($supplier ne ''){
148     $query.=" and aqorders.booksellerid='$supplier'";
149   }
150   $query.=" group by aqorders.ordernumber";
151   my $sth=$dbh->prepare($query);
152   $sth->execute;
153   my @results;
154 #  print $query;
155   my $i=0;
156   while (my $data=$sth->fetchrow_hashref){
157     $results[$i]=$data;
158     $i++;
159   }
160   $sth->finish;
161   $dbh->disconnect;
162   return($i,@results);
163 }
164
165 =item newbasket
166
167   $basket = &newbasket();
168
169 Finds the next unused basket number in the aqorders table of the Koha
170 database, and returns it.
171
172 =cut
173 #'
174 # FIXME - There's a race condition here:
175 #       A calls &newbasket
176 #       B calls &newbasket (gets the same number as A)
177 #       A updates the basket
178 #       B updates the basket, and clobbers A's result.
179 # A better approach might be to create a dummy order (with, say,
180 # requisitionedby == "Dummy-$$" or notes == "dummy <time> <pid>"), and
181 # see which basket number it gets. Then have a cron job periodically
182 # remove out-of-date dummy orders.
183 sub newbasket {
184   my $dbh=C4Connect;
185   my $query="Select max(basketno) from aqorders";
186   my $sth=$dbh->prepare($query);
187   $sth->execute;
188   my $data=$sth->fetchrow_arrayref;
189   my $basket=$$data[0];
190   $basket++;
191   $sth->finish;
192   $dbh->disconnect;
193   return($basket);
194 }
195
196 =item neworder
197
198   &neworder($biblionumber, $title, $ordnum, $basket, $quantity, $listprice,
199         $booksellerid, $who, $notes, $bookfund, $biblioitemnumber, $rrp,
200         $ecost, $gst, $budget, $unitprice, $subscription,
201         $booksellerinvoicenumber);
202
203 Adds a new order to the database. Any argument that isn't described
204 below is the new value of the field with the same name in the aqorders
205 table of the Koha database.
206
207 C<$ordnum> is a "minimum order number." After adding the new entry to
208 the aqorders table, C<&neworder> finds the first entry in aqorders
209 with order number greater than or equal to C<$ordnum>, and adds an
210 entry to the aqorderbreakdown table, with the order number just found,
211 and the book fund ID of the newly-added order.
212
213 C<$budget> is effectively ignored.
214
215 C<$subscription> may be either "yes", or anything else for "no".
216
217 =cut
218 #'
219 sub neworder {
220   my ($bibnum,$title,$ordnum,$basket,$quantity,$listprice,$supplier,$who,$notes,$bookfund,$bibitemnum,$rrp,$ecost,$gst,$budget,$cost,$sub,$invoice)=@_;
221   if ($budget eq 'now'){
222     $budget="now()";
223   } else {
224     $budget="'2001-07-01'";
225   }
226   if ($sub eq 'yes'){
227     $sub=1;
228   } else {
229     $sub=0;
230   }
231   my $dbh=C4Connect;
232   my $query="insert into aqorders (biblionumber,title,basketno,
233   quantity,listprice,booksellerid,entrydate,requisitionedby,authorisedby,notes,
234   biblioitemnumber,rrp,ecost,gst,unitprice,subscription,booksellerinvoicenumber)
235
236   values
237   ($bibnum,'$title',$basket,$quantity,$listprice,'$supplier',now(),
238   '$who','$who','$notes',$bibitemnum,'$rrp','$ecost','$gst','$cost',
239   '$sub','$invoice')";
240   my $sth=$dbh->prepare($query);
241 #  print $query;
242   $sth->execute;
243   $sth->finish;
244   $query="select * from aqorders where
245   biblionumber=$bibnum and basketno=$basket and ordernumber >=$ordnum";
246   $sth=$dbh->prepare($query);
247   $sth->execute;
248   my $data=$sth->fetchrow_hashref;
249   $sth->finish;
250   $ordnum=$data->{'ordernumber'};
251   $query="insert into aqorderbreakdown (ordernumber,bookfundid) values
252   ($ordnum,'$bookfund')";
253   $sth=$dbh->prepare($query);
254 #  print $query;
255   $sth->execute;
256   $sth->finish;
257   $dbh->disconnect;
258 }
259
260 =item delorder
261
262   &delorder($biblionumber, $ordernumber);
263
264 Cancel the order with the given order and biblio numbers. It does not
265 delete any entries in the aqorders table, it merely marks them as
266 cancelled.
267
268 If there are no items remaining with the given biblionumber,
269 C<&delorder> also deletes them from the marc_subfield_table and
270 marc_biblio tables of the Koha database.
271
272 =cut
273 #'
274 sub delorder {
275   my ($bibnum,$ordnum)=@_;
276   my $dbh=C4Connect;
277   my $query="update aqorders set datecancellationprinted=now()
278   where biblionumber='$bibnum' and
279   ordernumber='$ordnum'";
280   my $sth=$dbh->prepare($query);
281   #print $query;
282   $sth->execute;
283   $sth->finish;
284   my $count=itemcount($bibnum);
285   if ($count == 0){
286     delbiblio($bibnum);         # This is C4::Biblio::delbiblio, not
287                                 # C4::Acquisitions::delbiblio
288   }
289   $dbh->disconnect;
290 }
291
292 =item modorder
293
294   &modorder($title, $ordernumber, $quantity, $listprice,
295         $biblionumber, $basketno, $supplier, $who, $notes,
296         $bookfundid, $bibitemnum, $rrp, $ecost, $gst, $budget,
297         $unitprice, $booksellerinvoicenumber);
298
299 Modifies an existing order. Updates the order with order number
300 C<$ordernumber> and biblionumber C<$biblionumber>. All other arguments
301 update the fields with the same name in the aqorders table of the Koha
302 database.
303
304 Entries with order number C<$ordernumber> in the aqorderbreakdown
305 table are also updated to the new book fund ID.
306
307 =cut
308 #'
309 sub modorder {
310   my ($title,$ordnum,$quantity,$listprice,$bibnum,$basketno,$supplier,$who,$notes,$bookfund,$bibitemnum,$rrp,$ecost,$gst,$budget,$cost,$invoice)=@_;
311   my $dbh=C4Connect;
312   my $query="update aqorders set title='$title',
313   quantity='$quantity',listprice='$listprice',basketno='$basketno',
314   rrp='$rrp',ecost='$ecost',unitprice='$cost',
315   booksellerinvoicenumber='$invoice'
316   where
317   ordernumber=$ordnum and biblionumber=$bibnum";
318   my $sth=$dbh->prepare($query);
319 #  print $query;
320   $sth->execute;
321   $sth->finish;
322   $query="update aqorderbreakdown set bookfundid=$bookfund where
323   ordernumber=$ordnum";
324   $sth=$dbh->prepare($query);
325 #  print $query;
326   $sth->execute;
327   $sth->finish;
328   $dbh->disconnect;
329 }
330
331 =item newordernum
332
333   $order = &newordernum();
334
335 Finds the next unused order number in the aqorders table of the Koha
336 database, and returns it.
337
338 =cut
339 #'
340 # FIXME - Race condition
341 sub newordernum {
342   my $dbh=C4Connect;
343   my $query="Select max(ordernumber) from aqorders";
344   my $sth=$dbh->prepare($query);
345   $sth->execute;
346   my $data=$sth->fetchrow_arrayref;
347   my $ordnum=$$data[0];
348   $ordnum++;
349   $sth->finish;
350   $dbh->disconnect;
351   return($ordnum);
352 }
353
354 =item receiveorder
355
356   &receiveorder($biblionumber, $ordernumber, $quantityreceived, $user,
357         $unitprice, $booksellerinvoicenumber, $biblioitemnumber,
358         $freight, $bookfund, $rrp);
359
360 Updates an order, to reflect the fact that it was received, at least
361 in part. All arguments not mentioned below update the fields with the
362 same name in the aqorders table of the Koha database.
363
364 Updates the order with bibilionumber C<$biblionumber> and ordernumber
365 C<$ordernumber>.
366
367 Also updates the book fund ID in the aqorderbreakdown table.
368
369 =cut
370 #'
371 sub receiveorder {
372   my ($biblio,$ordnum,$quantrec,$user,$cost,$invoiceno,$bibitemno,$freight,$bookfund,$rrp)=@_;
373   my $dbh=C4Connect;
374   my $query="update aqorders set quantityreceived='$quantrec',
375   datereceived=now(),booksellerinvoicenumber='$invoiceno',
376   biblioitemnumber=$bibitemno,unitprice='$cost',freight='$freight',
377   rrp='$rrp'
378   where biblionumber=$biblio and ordernumber=$ordnum
379   ";
380 #  print $query;
381   my $sth=$dbh->prepare($query);
382   $sth->execute;
383   $sth->finish;
384   $query="update aqorderbreakdown set bookfundid=$bookfund where
385   ordernumber=$ordnum";
386   $sth=$dbh->prepare($query);
387 #  print $query;
388   $sth->execute;
389   $sth->finish;
390   $dbh->disconnect;
391 }
392
393 =item updaterecorder
394
395   &updaterecorder($biblionumber, $ordernumber, $user, $unitprice,
396         $bookfundid, $rrp);
397
398 Updates the order with biblionumber C<$biblionumber> and order number
399 C<$ordernumber>. C<$bookfundid> is the new value for the book fund ID
400 in the aqorderbreakdown table of the Koha database. All other
401 arguments update the fields with the same name in the aqorders table.
402
403 C<$user> is ignored.
404
405 =cut
406 #'
407 sub updaterecorder{
408   my($biblio,$ordnum,$user,$cost,$bookfund,$rrp)=@_;
409   my $dbh=C4Connect;
410   my $query="update aqorders set
411   unitprice='$cost', rrp='$rrp'
412   where biblionumber=$biblio and ordernumber=$ordnum
413   ";
414 #  print $query;
415   my $sth=$dbh->prepare($query);
416   $sth->execute;
417   $sth->finish;
418   $query="update aqorderbreakdown set bookfundid=$bookfund where
419   ordernumber=$ordnum";
420   $sth=$dbh->prepare($query);
421 #  print $query;
422   $sth->execute;
423   $sth->finish;
424   $dbh->disconnect;
425 }
426
427 #
428 #
429 # ORDERS
430 #
431 #
432
433 =item getorders
434
435   ($count, $orders) = &getorders($booksellerid);
436
437 Finds pending orders from the bookseller with the given ID. Ignores
438 completed and cancelled orders.
439
440 C<$count> is the number of elements in C<@{$orders}>.
441
442 C<$orders> is a reference-to-array; each element is a
443 reference-to-hash with the following fields:
444
445 =over 4
446
447 =item C<count(*)>
448
449 Gives the number of orders in with this basket number.
450
451 =item C<authorizedby>
452
453 =item C<entrydate>
454
455 =item C<basketno>
456
457 These give the value of the corresponding field in the aqorders table
458 of the Koha database.
459
460 =back
461
462 Results are ordered from most to least recent.
463
464 =cut
465 #'
466 sub getorders {
467   my ($supplierid)=@_;
468   my $dbh=C4Connect;
469   my $query = "Select count(*),authorisedby,entrydate,basketno from aqorders where
470   booksellerid='$supplierid' and (quantity > quantityreceived or
471   quantityreceived is NULL)
472   and (datecancellationprinted is NULL or datecancellationprinted = '0000-00-00')";
473   $query.=" group by basketno order by entrydate desc";
474   #print $query;
475   my $sth=$dbh->prepare($query);
476   $sth->execute;
477   my @results;
478   my $i=0;
479   while (my $data=$sth->fetchrow_hashref){
480     $results[$i]=$data;
481     $i++;
482   }
483   $sth->finish;
484   $dbh->disconnect;
485   return ($i,\@results);
486 }
487
488 =item getorder
489
490   ($order, $ordernumber) = &getorder($biblioitemnumber, $biblionumber);
491
492 Looks up the order with the given biblionumber and biblioitemnumber.
493
494 Returns a two-element array. C<$ordernumber> is the order number.
495 C<$order> is a reference-to-hash describing the order; its keys are
496 fields from the biblio, biblioitems, aqorders, and aqorderbreakdown
497 tables of the Koha database.
498
499 =cut
500 #'
501 sub getorder{
502   my ($bi,$bib)=@_;
503   my $dbh=C4Connect;
504   my $query="Select ordernumber from aqorders where biblionumber=$bib and
505   biblioitemnumber='$bi'";
506   my $sth=$dbh->prepare($query);
507   $sth->execute;
508   # FIXME - Use fetchrow_array(), since we're only interested in the one
509   # value.
510   my $ordnum=$sth->fetchrow_hashref;
511   $sth->finish;
512   my $order=getsingleorder($ordnum->{'ordernumber'});
513   $dbh->disconnect;
514 #  print $query;
515   return ($order,$ordnum->{'ordernumber'});
516 }
517
518 =item getsingleorder
519
520   $order = &getsingleorder($ordernumber);
521
522 Looks up an order by order number.
523
524 Returns a reference-to-hash describing the order. The keys of
525 C<$order> are fields from the biblio, biblioitems, aqorders, and
526 aqorderbreakdown tables of the Koha database.
527
528 =cut
529 #'
530 # FIXME - This is basically the same thing as
531 # C4::Acquisitions::getsingleorder. Figure out where it goes and nuke
532 # the other one.
533 sub getsingleorder {
534   my ($ordnum)=@_;
535   my $dbh=C4Connect;
536   my $query="Select * from biblio,biblioitems,aqorders,aqorderbreakdown
537   where aqorders.ordernumber='$ordnum'
538   and biblio.biblionumber=aqorders.biblionumber and
539   biblioitems.biblioitemnumber=aqorders.biblioitemnumber and
540   aqorders.ordernumber=aqorderbreakdown.ordernumber";
541   my $sth=$dbh->prepare($query);
542   $sth->execute;
543   my $data=$sth->fetchrow_hashref;
544   $sth->finish;
545   $dbh->disconnect;
546   return($data);
547 }
548
549 =item getallorders
550
551   ($count, @results) = &getallorders($booksellerid);
552
553 Looks up all of the pending orders from the supplier with the given
554 bookseller ID. Ignores cancelled and completed orders.
555
556 C<$count> is the number of elements in C<@results>. C<@results> is an
557 array of references-to-hash. The keys of each element are fields from
558 the aqorders, biblio, and biblioitems tables of the Koha database.
559
560 C<@results> is sorted alphabetically by book title.
561
562 =cut
563 #'
564 sub getallorders {
565   #gets all orders from a certain supplier, orders them alphabetically
566   my ($supid)=@_;
567   my $dbh=C4Connect;
568   my $query="Select * from aqorders,biblio,biblioitems where booksellerid='$supid'
569   and (cancelledby is NULL or cancelledby = '')
570   and (quantityreceived < quantity or quantityreceived is NULL)
571   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=
572   aqorders.biblioitemnumber
573   group by aqorders.biblioitemnumber
574   order by
575   biblio.title";
576   my $i=0;
577   my @results;
578   my $sth=$dbh->prepare($query);
579   $sth->execute;
580   while (my $data=$sth->fetchrow_hashref){
581     $results[$i]=$data;
582     $i++;
583   }
584   $sth->finish;
585   $dbh->disconnect;
586   return($i,@results);
587 }
588
589 # FIXME - Never used
590 sub getrecorders {
591   #gets all orders from a certain supplier, orders them alphabetically
592   my ($supid)=@_;
593   my $dbh=C4Connect;
594   my $query="Select * from aqorders,biblio,biblioitems where booksellerid='$supid'
595   and (cancelledby is NULL or cancelledby = '')
596   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=
597   aqorders.biblioitemnumber and
598   aqorders.quantityreceived>0
599   and aqorders.datereceived >=now()
600   group by aqorders.biblioitemnumber
601   order by
602   biblio.title";
603   my $i=0;
604   my @results;
605   my $sth=$dbh->prepare($query);
606   $sth->execute;
607   while (my $data=$sth->fetchrow_hashref){
608     $results[$i]=$data;
609     $i++;
610   }
611   $sth->finish;
612   $dbh->disconnect;
613   return($i,@results);
614 }
615
616 =item ordersearch
617
618   ($count, @results) = &ordersearch($search, $biblionumber, $complete);
619
620 Searches for orders.
621
622 C<$search> may take one of several forms: if it is an ISBN,
623 C<&ordersearch> returns orders with that ISBN. If C<$search> is an
624 order number, C<&ordersearch> returns orders with that order number
625 and biblionumber C<$biblionumber>. Otherwise, C<$search> is considered
626 to be a space-separated list of search terms; in this case, all of the
627 terms must appear in the title (matching the beginning of title
628 words).
629
630 If C<$complete> is C<yes>, the results will include only completed
631 orders. In any case, C<&ordersearch> ignores cancelled orders.
632
633 C<&ordersearch> returns an array. C<$count> is the number of elements
634 in C<@results>. C<@results> is an array of references-to-hash with the
635 following keys:
636
637 =over 4
638
639 =item C<author>
640
641 =item C<seriestitle>
642
643 =item C<branchcode>
644
645 =item C<bookfundid>
646
647 =back
648
649 =cut
650 #'
651 sub ordersearch {
652   my ($search,$biblio,$catview) = @_;
653   my $dbh   = C4Connect;
654   my $query = "Select *,biblio.title from aqorders,biblioitems,biblio
655 where aqorders.biblioitemnumber = biblioitems.biblioitemnumber
656 and biblio.biblionumber=aqorders.biblionumber
657 and ((datecancellationprinted is NULL)
658 or (datecancellationprinted = '0000-00-00'))
659 and ((";
660   my @data  = split(' ',$search);
661   my $count = @data;
662   for (my $i = 0; $i < $count; $i++) {
663     $query .= "(biblio.title like '$data[$i]%' or biblio.title like '% $data[$i]%') and ";
664   }
665   $query=~ s/ and $//;
666                 # FIXME - Redo this properly instead of hacking off the
667                 # trailing 'and'.
668   $query.=" ) or biblioitems.isbn='$search'
669   or (aqorders.ordernumber='$search' and aqorders.biblionumber='$biblio')) ";
670   if ($catview ne 'yes'){
671     $query.=" and (quantityreceived < quantity or quantityreceived is NULL)";
672   }
673   $query.=" group by aqorders.ordernumber";
674   my $sth=$dbh->prepare($query);
675   $sth->execute;
676   my $i=0;
677   my @results;
678   while (my $data=$sth->fetchrow_hashref){
679      my $sth2=$dbh->prepare("Select * from biblio where
680      biblionumber='$data->{'biblionumber'}'");
681      $sth2->execute;
682      my $data2=$sth2->fetchrow_hashref;
683      $sth2->finish;
684      $data->{'author'}=$data2->{'author'};
685      $data->{'seriestitle'}=$data2->{'seriestitle'};
686      $sth2=$dbh->prepare("Select * from aqorderbreakdown where
687     ordernumber=$data->{'ordernumber'}");
688     $sth2->execute;
689     $data2=$sth2->fetchrow_hashref;
690     $sth2->finish;
691     $data->{'branchcode'}=$data2->{'branchcode'};
692     $data->{'bookfundid'}=$data2->{'bookfundid'};
693     $results[$i]=$data;
694     $i++;
695   }
696   $sth->finish;
697   $dbh->disconnect;
698   return($i,@results);
699 }
700
701 #
702 #
703 # MONEY
704 #
705 #
706 =item invoice
707
708   ($count, @results) = &invoice($booksellerinvoicenumber);
709
710 Looks up orders by invoice number.
711
712 Returns an array. C<$count> is the number of elements in C<@results>.
713 C<@results> is an array of references-to-hash; the keys of each
714 elements are fields from the aqorders, biblio, and biblioitems tables
715 of the Koha database.
716
717 =cut
718 #'
719 sub invoice {
720   my ($invoice)=@_;
721   my $dbh=C4Connect;
722   my $query="Select * from aqorders,biblio,biblioitems where
723   booksellerinvoicenumber='$invoice'
724   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=
725   aqorders.biblioitemnumber group by aqorders.ordernumber,aqorders.biblioitemnumber";
726   my $i=0;
727   my @results;
728   my $sth=$dbh->prepare($query);
729   $sth->execute;
730   while (my $data=$sth->fetchrow_hashref){
731     $results[$i]=$data;
732     $i++;
733   }
734   $sth->finish;
735   $dbh->disconnect;
736   return($i,@results);
737 }
738
739 =item bookfunds
740
741   ($count, @results) = &bookfunds();
742
743 Returns a list of all book funds.
744
745 C<$count> is the number of elements in C<@results>. C<@results> is an
746 array of references-to-hash, whose keys are fields from the aqbookfund
747 and aqbudget tables of the Koha database. Results are ordered
748 alphabetically by book fund name.
749
750 =cut
751 #'
752 sub bookfunds {
753   my $dbh=C4Connect;
754   my $query="Select * from aqbookfund,aqbudget where aqbookfund.bookfundid
755   =aqbudget.bookfundid
756   group by aqbookfund.bookfundid order by bookfundname";
757   my $sth=$dbh->prepare($query);
758   $sth->execute;
759   my @results;
760   my $i=0;
761   while (my $data=$sth->fetchrow_hashref){
762     $results[$i]=$data;
763     $i++;
764   }
765   $sth->finish;
766   $dbh->disconnect;
767   return($i,@results);
768 }
769
770 # FIXME - POD. I can't figure out what this function is doing. Then
771 # again, I don't think it's being used (anymore).
772 sub bookfundbreakdown {
773   my ($id)=@_;
774   my $dbh=C4Connect;
775   my $query="Select quantity,datereceived,freight,unitprice,listprice,ecost,quantityreceived,subscription
776   from aqorders,aqorderbreakdown where bookfundid='$id' and
777   aqorders.ordernumber=aqorderbreakdown.ordernumber
778   and (datecancellationprinted is NULL or
779   datecancellationprinted='0000-00-00')";
780   my $sth=$dbh->prepare($query);
781   $sth->execute;
782   my $comtd=0;
783   my $spent=0;
784   while (my $data=$sth->fetchrow_hashref){
785     if ($data->{'subscription'} == 1){
786       $spent+=$data->{'quantity'}*$data->{'unitprice'};
787     } else {
788       my $leftover=$data->{'quantity'}-$data->{'quantityreceived'};
789       $comtd+=($data->{'ecost'})*$leftover;
790       $spent+=($data->{'unitprice'})*$data->{'quantityreceived'};
791     }
792   }
793   $sth->finish;
794   $dbh->disconnect;
795   return($spent,$comtd);
796 }
797
798 =item curconvert
799
800   $foreignprice = &curconvert($currency, $localprice);
801
802 Converts the price C<$localprice> to foreign currency C<$currency> by
803 dividing by the exchange rate, and returns the result.
804
805 If no exchange rate is found, C<&curconvert> assumes the rate is one
806 to one.
807
808 =cut
809 #'
810 sub curconvert {
811   my ($currency,$price)=@_;
812   my $dbh=C4Connect;
813   my $query="Select rate from currency where currency='$currency'";
814   my $sth=$dbh->prepare($query);
815   $sth->execute;
816   my $data=$sth->fetchrow_hashref;
817   $sth->finish;
818   $dbh->disconnect;
819   my $cur=$data->{'rate'};
820   if ($cur==0){
821     $cur=1;
822   }
823   my $price=$price / $cur;
824   return($price);
825 }
826
827 =item getcurrencies
828
829   ($count, $currencies) = &getcurrencies();
830
831 Returns the list of all known currencies.
832
833 C<$count> is the number of elements in C<$currencies>. C<$currencies>
834 is a reference-to-array; its elements are references-to-hash, whose
835 keys are the fields from the currency table in the Koha database.
836
837 =cut
838 #'
839 sub getcurrencies {
840   my $dbh=C4Connect;
841   my $query="Select * from currency";
842   my $sth=$dbh->prepare($query);
843   $sth->execute;
844   my @results;
845   my $i=0;
846   while (my $data=$sth->fetchrow_hashref){
847     $results[$i]=$data;
848     $i++;
849   }
850   $sth->finish;
851   $dbh->disconnect;
852   return($i,\@results);
853 }
854
855 # FIXME - Never used
856 sub getcurrency {
857   my ($cur)=@_;
858   my $dbh=C4Connect;
859   my $query="Select * from currency where currency='$cur'";
860   my $sth=$dbh->prepare($query);
861   $sth->execute;
862
863   my $data=$sth->fetchrow_hashref;
864   $sth->finish;
865   $dbh->disconnect;
866   return($data);
867 }
868
869 =item updatecurrencies
870
871   &updatecurrencies($currency, $newrate);
872
873 Sets the exchange rate for C<$currency> to be C<$newrate>.
874
875 =cut
876 #'
877 sub updatecurrencies {
878   my ($currency,$rate)=@_;
879   my $dbh=C4Connect;
880   my $query="update currency set rate=$rate where currency='$currency'";
881   my $sth=$dbh->prepare($query);
882   $sth->execute;
883   $sth->finish;
884   $dbh->disconnect;
885 }
886
887 # FIXME - Never used
888 sub updatecost{
889   my($price,$rrp,$itemnum)=@_;
890   my $dbh=C4Connect;
891   my $query="update items set price='$price',replacementprice='$rrp'
892   where itemnumber=$itemnum";
893   my $sth=$dbh->prepare($query);
894   $sth->execute;
895   $sth->finish;
896   $dbh->disconnect;
897 }
898
899 #
900 #
901 # OTHERS
902 #
903 #
904
905 =item bookseller
906
907   ($count, @results) = &bookseller($searchstring);
908
909 Looks up a book seller. C<$searchstring> may be either a book seller
910 ID, or a string to look for in the book seller's name.
911
912 C<$count> is the number of elements in C<@results>. C<@results> is an
913 array of references-to-hash, whose keys are the fields of of the
914 aqbooksellers table in the Koha database.
915
916 =cut
917 #'
918 sub bookseller {
919   my ($searchstring)=@_;
920   my $dbh=C4Connect;
921   my $query="Select * from aqbooksellers where name like '%$searchstring%' or
922   id = '$searchstring'";
923   my $sth=$dbh->prepare($query);
924   $sth->execute;
925   my @results;
926   my $i=0;
927   while (my $data=$sth->fetchrow_hashref){
928     $results[$i]=$data;
929     $i++;
930   }
931   $sth->finish;
932   $dbh->disconnect;
933   return($i,@results);
934 }
935
936 =item breakdown
937
938   ($count, $results) = &breakdown($ordernumber);
939
940 Looks up an order by order ID, and returns its breakdown.
941
942 C<$count> is the number of elements in C<$results>. C<$results> is a
943 reference-to-array; its elements are references-to-hash, whose keys
944 are the fields of the aqorderbreakdown table in the Koha database.
945
946 =cut
947 #'
948 sub breakdown {
949   my ($id)=@_;
950   my $dbh=C4Connect;
951   my $query="Select * from aqorderbreakdown where ordernumber='$id'";
952   my $sth=$dbh->prepare($query);
953   $sth->execute;
954   my @results;
955   my $i=0;
956   while (my $data=$sth->fetchrow_hashref){
957     $results[$i]=$data;
958     $i++;
959   }
960   $sth->finish;
961   $dbh->disconnect;
962   return($i,\@results);
963 }
964
965 =item branches
966
967   ($count, @results) = &branches();
968
969 Returns a list of all library branches.
970
971 C<$count> is the number of elements in C<@results>. C<@results> is an
972 array of references-to-hash, whose keys are the fields of the branches
973 table of the Koha database.
974
975 =cut
976 #'
977 sub branches {
978     my $dbh   = C4Connect;
979     my $query = "Select * from branches";
980     my $sth   = $dbh->prepare($query);
981     my $i     = 0;
982     my @results;
983
984     $sth->execute;
985     while (my $data = $sth->fetchrow_hashref) {
986         $results[$i] = $data;
987         $i++;
988     } # while
989
990     $sth->finish;
991     $dbh->disconnect;
992     return($i, @results);
993 } # sub branches
994
995 # FIXME - Never used
996 sub findall {
997   my ($biblionumber)=@_;
998   my $dbh=C4Connect;
999   my $query="Select * from biblioitems,items,itemtypes where
1000   biblioitems.biblionumber=$biblionumber
1001   and biblioitems.biblioitemnumber=items.biblioitemnumber and
1002   itemtypes.itemtype=biblioitems.itemtype
1003   order by items.biblioitemnumber";
1004   my $sth=$dbh->prepare($query);
1005   $sth->execute;
1006   my @results;
1007   my $i;
1008   while (my $data=$sth->fetchrow_hashref){
1009     $results[$i]=$data;
1010     $i++;
1011   }
1012   $sth->finish;
1013   $dbh->disconnect;
1014   return(@results);
1015 }
1016
1017 # FIXME - Never used
1018 sub needsmod{
1019   my ($bibitemnum,$itemtype)=@_;
1020   my $dbh=C4Connect;
1021   my $query="Select * from biblioitems where biblioitemnumber=$bibitemnum
1022   and itemtype='$itemtype'";
1023   my $sth=$dbh->prepare($query);
1024   $sth->execute;
1025   my $result=0;
1026   if (my $data=$sth->fetchrow_hashref){
1027     $result=1;
1028   }
1029   $sth->finish;
1030   $dbh->disconnect;
1031   return($result);
1032 }
1033
1034 =item updatesup
1035
1036   &updatesup($bookseller);
1037
1038 Updates the information for a given bookseller. C<$bookseller> is a
1039 reference-to-hash whose keys are the fields of the aqbooksellers table
1040 in the Koha database. It must contain entries for all of the fields.
1041 The entry to modify is determined by C<$bookseller-E<gt>{id}>.
1042
1043 The easiest way to get all of the necessary fields is to look up a
1044 book seller with C<&booksellers>, modify what's necessary, then call
1045 C<&updatesup> with the result.
1046
1047 =cut
1048 #'
1049 sub updatesup {
1050    my ($data)=@_;
1051    my $dbh=C4Connect;
1052    my $query="Update aqbooksellers set
1053    name='$data->{'name'}',address1='$data->{'address1'}',address2='$data->{'address2'}',
1054    address3='$data->{'address3'}',address4='$data->{'address4'}',postal='$data->{'postal'}',
1055    phone='$data->{'phone'}',fax='$data->{'fax'}',url='$data->{'url'}',
1056    contact='$data->{'contact'}',contpos='$data->{'contpos'}',
1057    contphone='$data->{'contphone'}', contfax='$data->{'contfax'}', contaltphone=
1058    '$data->{'contaltphone'}', contemail='$data->{'contemail'}', contnotes=
1059    '$data->{'contnotes'}', active=$data->{'active'},
1060    listprice='$data->{'listprice'}', invoiceprice='$data->{'invoiceprice'}',
1061    gstreg=$data->{'gstreg'}, listincgst=$data->{'listincgst'},
1062    invoiceincgst=$data->{'invoiceincgst'}, specialty='$data->{'specialty'}',
1063    discount='$data->{'discount'}',invoicedisc='$data->{'invoicedisc'}',
1064    nocalc='$data->{'nocalc'}'
1065    where id='$data->{'id'}'";
1066    my $sth=$dbh->prepare($query);
1067    $sth->execute;
1068    $sth->finish;
1069    $dbh->disconnect;
1070 #   print $query;
1071 }
1072
1073 =item insertsup
1074
1075   $id = &insertsup($bookseller);
1076
1077 Creates a new bookseller. C<$bookseller> is a reference-to-hash whose
1078 keys are the fields of the aqbooksellers table in the Koha database.
1079 All fields must be present.
1080
1081 Returns the ID of the newly-created bookseller.
1082
1083 =cut
1084 #'
1085 sub insertsup {
1086   my ($data)=@_;
1087   my $dbh=C4Connect;
1088   my $sth=$dbh->prepare("Select max(id) from aqbooksellers");
1089   $sth->execute;
1090   my $data2=$sth->fetchrow_hashref;
1091   $sth->finish;
1092   $data2->{'max(id)'}++;
1093   $sth=$dbh->prepare("Insert into aqbooksellers (id) values ($data2->{'max(id)'})");
1094   $sth->execute;
1095   $sth->finish;
1096   $data->{'id'}=$data2->{'max(id)'};
1097   $dbh->disconnect;
1098   updatesup($data);
1099   return($data->{'id'});
1100 }
1101
1102 =item websitesearch
1103
1104   ($count, @results) = &websitesearch($keywordlist);
1105
1106 Looks up biblioitems by URL.
1107
1108 C<$keywordlist> is a space-separated list of search terms.
1109 C<&websitesearch> returns those biblioitems whose URL contains at
1110 least one of the search terms.
1111
1112 C<$count> is the number of elements in C<@results>. C<@results> is an
1113 array of references-to-hash, whose keys are the fields of the biblio
1114 and biblioitems tables in the Koha database.
1115
1116 =cut
1117 #'
1118 sub websitesearch {
1119     my ($keywordlist) = @_;
1120     my $dbh   = C4Connect;
1121     my $query = "Select distinct biblio.* from biblio, biblioitems where
1122 biblio.biblionumber = biblioitems.biblionumber and (";
1123     my $count = 0;
1124     my $sth;
1125     my @results;
1126     my @keywords = split(/ +/, $keywordlist);
1127     my $keyword = shift(@keywords);
1128
1129     # FIXME - Can use
1130     #   $query .= join(" and ",
1131     #           apply { url like "%$_%" } @keywords
1132
1133     $keyword =~ s/%/\\%/g;
1134     $keyword =~ s/_/\\_/;
1135     $keyword = "%" . $keyword . "%";
1136     $keyword = $dbh->quote($keyword);
1137     $query  .= " (url like $keyword)";
1138
1139     foreach $keyword (@keywords) {
1140         $keyword =~ s/%/\\%/;
1141         $keyword =~ s/_/\\_/;
1142         $keyword = "%" . $keyword . "%";
1143         $keyword = $dbh->quote($keyword);
1144         $query  .= " or (url like $keyword)";
1145     } # foreach
1146
1147     $query .= ")";
1148     $sth    = $dbh->prepare($query);
1149     $sth->execute;
1150
1151     while (my $data = $sth->fetchrow_hashref) {
1152         $results[$count] = $data;
1153         $count++;
1154     } # while
1155
1156     $sth->finish;
1157     $dbh->disconnect;
1158     return($count, @results);
1159 } # sub websitesearch
1160
1161 =item addwebsite
1162
1163   &addwebsite($website);
1164
1165 Adds a new web site. C<$website> is a reference-to-hash, with the keys
1166 C<biblionumber>, C<title>, C<description>, and C<url>. All of these
1167 are mandatory.
1168
1169 =cut
1170 #'
1171 sub addwebsite {
1172     my ($website) = @_;
1173     my $dbh = C4Connect;
1174     my $query;
1175
1176     # FIXME -
1177     #   for (qw( biblionumber title description url )) # and any others
1178     #   {
1179     #           $website->{$_} = $dbh->quote($_);
1180     #   }
1181     # Perhaps extend this to building the query as well. This might allow
1182     # some of the fields to be optional.
1183     $website->{'biblionumber'} = $dbh->quote($website->{'biblionumber'});
1184     $website->{'title'}        = $dbh->quote($website->{'title'});
1185     $website->{'description'}  = $dbh->quote($website->{'description'});
1186     $website->{'url'}          = $dbh->quote($website->{'url'});
1187
1188     $query = "Insert into websites set
1189 biblionumber = $website->{'biblionumber'},
1190 title        = $website->{'title'},
1191 description  = $website->{'description'},
1192 url          = $website->{'url'}";
1193
1194     $dbh->do($query);
1195
1196     $dbh->disconnect;
1197 } # sub website
1198
1199 =item updatewebsite
1200
1201   &updatewebsite($website);
1202
1203 Updates an existing web site. C<$website> is a reference-to-hash with
1204 the keys C<websitenumber>, C<title>, C<description>, and C<url>. All
1205 of these are mandatory. C<$website-E<gt>{websitenumber}> identifies
1206 the entry to update.
1207
1208 =cut
1209 #'
1210 sub updatewebsite {
1211     my ($website) = @_;
1212     my $dbh = C4Connect;
1213     my $query;
1214
1215     $website->{'title'}      = $dbh->quote($website->{'title'});
1216     $website->{'description'} = $dbh->quote($website->{'description'});
1217     $website->{'url'}        = $dbh->quote($website->{'url'});
1218
1219     $query = "Update websites set
1220 title       = $website->{'title'},
1221 description = $website->{'description'},
1222 url         = $website->{'url'}
1223 where websitenumber = $website->{'websitenumber'}";
1224
1225     $dbh->do($query);
1226
1227     $dbh->disconnect;
1228 } # sub updatewebsite
1229
1230 =item deletewebsite
1231
1232   &deletewebsite($websitenumber);
1233
1234 Deletes the web site with number C<$websitenumber>.
1235
1236 =cut
1237 #'
1238 sub deletewebsite {
1239     my ($websitenumber) = @_;
1240     my $dbh = C4Connect;
1241     my $query = "Delete from websites where websitenumber = $websitenumber";
1242
1243     $dbh->do($query);
1244
1245     $dbh->disconnect;
1246 } # sub deletewebsite
1247
1248 END { }       # module clean-up code here (global destructor)
1249
1250 1;
1251 __END__
1252
1253 =back
1254
1255 =head1 AUTHOR
1256
1257 Koha Developement team <info@koha.org>
1258
1259 =head1 SEE ALSO
1260
1261 L<perl>.
1262
1263 =cut