* removing the hard coded $
[koha.git] / C4 / Acquisition.pm
1 package C4::Acquisition;
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 require Exporter;
22 use C4::Context;
23 use MARC::Record;
24 # use C4::Biblio;
25
26 use vars qw($VERSION @ISA @EXPORT);
27
28 # set the version for version checking
29 $VERSION = 0.01;
30
31 =head1 NAME
32
33 C4::Acquisition - Koha functions for dealing with orders and acquisitions
34
35 =head1 SYNOPSIS
36
37   use C4::Acquisition;
38
39 =head1 DESCRIPTION
40
41 The functions in this module deal with acquisitions, managing book
42 orders, converting money to different currencies, and so forth.
43
44 =head1 FUNCTIONS
45
46 =over 2
47
48 =cut
49
50 @ISA = qw(Exporter);
51 @EXPORT = qw(
52                 &getbasket &getbasketcontent &newbasket &closebasket
53
54                 &getorders &getallorders &getrecorders
55                 &getorder &neworder &delorder
56                 &ordersearch
57                 &modorder &getsingleorder &invoice &receiveorder
58                 &updaterecorder &newordernum
59
60                 &bookfunds &curconvert &getcurrencies &bookfundbreakdown
61                 &updatecurrencies &getcurrency
62
63                 &branches &updatesup &insertsup
64                 &bookseller &breakdown
65 );
66
67 #
68 #
69 #
70 # BASKETS
71 #
72 #
73 #
74 =item getbasket
75
76   $aqbasket = &getbasket($basketnumber);
77
78 get all basket informations in aqbasket for a given basket
79 =cut
80
81 sub getbasket {
82         my ($basketno)=@_;
83         my $dbh=C4::Context->dbh;
84         my $sth=$dbh->prepare("select aqbasket.*,borrowers.firstname+' '+borrowers.surname as authorisedbyname from aqbasket left join borrowers on aqbasket.authorisedby=borrowers.borrowernumber where basketno=?");
85         $sth->execute($basketno);
86         return($sth->fetchrow_hashref);
87 }
88
89 =item getbasketcontent
90
91   ($count, @orders) = &getbasketcontent($basketnumber, $booksellerID);
92
93 Looks up the pending (non-cancelled) orders with the given basket
94 number. If C<$booksellerID> is non-empty, only orders from that seller
95 are returned.
96
97 C<&basket> returns a two-element array. C<@orders> is an array of
98 references-to-hash, whose keys are the fields from the aqorders,
99 biblio, and biblioitems tables in the Koha database. C<$count> is the
100 number of elements in C<@orders>.
101
102 =cut
103 #'
104 sub getbasketcontent {
105         my ($basketno,$supplier)=@_;
106         my $dbh = C4::Context->dbh;
107         my $query="Select *,biblio.title from aqorders,biblio,biblioitems
108         left join aqorderbreakdown on aqorderbreakdown.ordernumber=aqorders.ordernumber
109         where basketno='$basketno'
110         and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber
111         =aqorders.biblioitemnumber
112         and (datecancellationprinted is NULL or datecancellationprinted =
113         '0000-00-00')";
114         if ($supplier ne ''){
115                 $query.=" and aqorders.booksellerid='$supplier'";
116         }
117         $query.=" order by biblioitems.publishercode";
118         my $sth=$dbh->prepare($query);
119         $sth->execute;
120         my @results;
121         #  print $query;
122         my $i=0;
123         while (my $data=$sth->fetchrow_hashref){
124                 $results[$i]=$data;
125                 $i++;
126         }
127         $sth->finish;
128         return($i,@results);
129 }
130
131 =item newbasket
132
133   $basket = &newbasket();
134
135 Create a new basket in aqbasket table
136 =cut
137
138 sub newbasket {
139         my ($booksellerid,$authorisedby) = @_;
140         my $dbh = C4::Context->dbh;
141         my $sth=$dbh->do("insert into aqbasket (creationdate,booksellerid,authorisedby) values(now(),'$booksellerid','$authorisedby')");
142         #find & return basketno MYSQL dependant, but $dbh->last_insert_id always returns null :-(
143         my $basket = $dbh->{'mysql_insertid'};
144         return($basket);
145 }
146
147 =item closebasket
148
149   &newbasket($basketno);
150
151 close a basket (becomes unmodifiable,except for recieves
152 =cut
153
154 sub closebasket {
155         my ($basketno) = @_;
156         my $dbh = C4::Context->dbh;
157         my $sth=$dbh->prepare("update aqbasket set closedate=now() where basketno=?");
158         $sth->execute($basketno);
159 }
160
161 =item neworder
162
163   &neworder($basket, $biblionumber, $title, $quantity, $listprice,
164         $booksellerid, $who, $notes, $bookfund, $biblioitemnumber, $rrp,
165         $ecost, $gst, $budget, $unitprice, $subscription,
166         $booksellerinvoicenumber);
167
168 Adds a new order to the database. Any argument that isn't described
169 below is the new value of the field with the same name in the aqorders
170 table of the Koha database.
171
172 C<$ordnum> is a "minimum order number." After adding the new entry to
173 the aqorders table, C<&neworder> finds the first entry in aqorders
174 with order number greater than or equal to C<$ordnum>, and adds an
175 entry to the aqorderbreakdown table, with the order number just found,
176 and the book fund ID of the newly-added order.
177
178 C<$budget> is effectively ignored.
179
180 C<$subscription> may be either "yes", or anything else for "no".
181
182 =cut
183 #'
184 sub neworder {
185         my ($basketno,$bibnum,$title,$quantity,$listprice,$booksellerid,$authorisedby,$notes,$bookfund,$bibitemnum,$rrp,$ecost,$gst,$budget,$cost,$sub,$invoice,$sort1,$sort2)=@_;
186         if ($budget eq 'now'){
187                 $budget="now()";
188         } else {
189                 $budget="'2001-07-01'";
190         }
191         if ($sub eq 'yes'){
192                 $sub=1;
193         } else {
194                 $sub=0;
195         }
196         # if $basket empty, it's also a new basket, create it
197         unless ($basketno) {
198                 $basketno=newbasket($booksellerid,$authorisedby);
199         }
200         my $dbh = C4::Context->dbh;
201         my $sth=$dbh->prepare("insert into aqorders 
202                                                                 (biblionumber,title,basketno,quantity,listprice,notes,
203                                                                 biblioitemnumber,rrp,ecost,gst,unitprice,subscription,sort1,sort2)
204                                                                 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
205         $sth->execute($bibnum,$title,$basketno,$quantity,$listprice,$notes,
206                                         $bibitemnum,$rrp,$ecost,$gst,$cost,$sub,$sort1,$sort2);
207         $sth->finish;
208         #get ordnum MYSQL dependant, but $dbh->last_insert_id returns null
209         my $ordnum = $dbh->{'mysql_insertid'};
210         $sth=$dbh->prepare("insert into aqorderbreakdown (ordernumber,bookfundid) values
211         (?,?)");
212         $sth->execute($ordnum,$bookfund);
213         $sth->finish;
214         return $basketno;
215 }
216
217 =item delorder
218
219   &delorder($biblionumber, $ordernumber);
220
221 Cancel the order with the given order and biblio numbers. It does not
222 delete any entries in the aqorders table, it merely marks them as
223 cancelled.
224
225 =cut
226 #'
227 sub delorder {
228   my ($bibnum,$ordnum)=@_;
229   my $dbh = C4::Context->dbh;
230   my $sth=$dbh->prepare("update aqorders set datecancellationprinted=now()
231   where biblionumber=? and ordernumber=?");
232   $sth->execute($bibnum,$ordnum);
233   $sth->finish;
234 }
235
236 =item modorder
237
238   &modorder($title, $ordernumber, $quantity, $listprice,
239         $biblionumber, $basketno, $supplier, $who, $notes,
240         $bookfundid, $bibitemnum, $rrp, $ecost, $gst, $budget,
241         $unitprice, $booksellerinvoicenumber);
242
243 Modifies an existing order. Updates the order with order number
244 C<$ordernumber> and biblionumber C<$biblionumber>. All other arguments
245 update the fields with the same name in the aqorders table of the Koha
246 database.
247
248 Entries with order number C<$ordernumber> in the aqorderbreakdown
249 table are also updated to the new book fund ID.
250
251 =cut
252 #'
253 sub modorder {
254   my ($title,$ordnum,$quantity,$listprice,$bibnum,$basketno,$supplier,$who,$notes,$bookfund,$bibitemnum,$rrp,$ecost,$gst,$budget,$cost,$invoice,$sort1,$sort2)=@_;
255   my $dbh = C4::Context->dbh;
256   my $sth=$dbh->prepare("update aqorders set title=?,
257   quantity=?,listprice=?,basketno=?,
258   rrp=?,ecost=?,unitprice=?,booksellerinvoicenumber=?,
259   notes=?,sort1=?, sort2=?
260   where
261   ordernumber=? and biblionumber=?");
262   $sth->execute($title,$quantity,$listprice,$basketno,$rrp,$ecost,$cost,$invoice,$notes,$sort1,$sort2,$ordnum,$bibnum);
263   $sth->finish;
264   $sth=$dbh->prepare("update aqorderbreakdown set bookfundid=? where
265   ordernumber=?");
266   if ($sth->execute($bookfund,$ordnum) == 0) { # zero rows affected [Bug 734]
267     my $query="insert into aqorderbreakdown (ordernumber,bookfundid) values (?,?)";
268     $sth=$dbh->prepare($query);
269     $sth->execute($ordnum,$bookfund);
270   }
271   $sth->finish;
272 }
273
274 =item newordernum
275
276   $order = &newordernum();
277
278 Finds the next unused order number in the aqorders table of the Koha
279 database, and returns it.
280
281 =cut
282 #'
283 # FIXME - Race condition
284 sub newordernum {
285   my $dbh = C4::Context->dbh;
286   my $sth=$dbh->prepare("Select max(ordernumber) from aqorders");
287   $sth->execute;
288   my $data=$sth->fetchrow_arrayref;
289   my $ordnum=$$data[0];
290   $ordnum++;
291   $sth->finish;
292   return($ordnum);
293 }
294
295 =item receiveorder
296
297   &receiveorder($biblionumber, $ordernumber, $quantityreceived, $user,
298         $unitprice, $booksellerinvoicenumber, $biblioitemnumber,
299         $freight, $bookfund, $rrp);
300
301 Updates an order, to reflect the fact that it was received, at least
302 in part. All arguments not mentioned below update the fields with the
303 same name in the aqorders table of the Koha database.
304
305 Updates the order with bibilionumber C<$biblionumber> and ordernumber
306 C<$ordernumber>.
307
308 Also updates the book fund ID in the aqorderbreakdown table.
309
310 =cut
311 #'
312 sub receiveorder {
313         my ($biblio,$ordnum,$quantrec,$user,$cost,$invoiceno,$freight,$rrp)=@_;
314         my $dbh = C4::Context->dbh;
315         my $sth=$dbh->prepare("update aqorders set quantityreceived=?,datereceived=now(),booksellerinvoicenumber=?,
316                                                                                         unitprice=?,freight=?,rrp=?
317                                                         where biblionumber=? and ordernumber=?");
318         $sth->execute($quantrec,$invoiceno,$cost,$freight,$rrp,$biblio,$ordnum);
319         $sth->finish;
320 }
321
322 =item updaterecorder
323
324   &updaterecorder($biblionumber, $ordernumber, $user, $unitprice,
325         $bookfundid, $rrp);
326
327 Updates the order with biblionumber C<$biblionumber> and order number
328 C<$ordernumber>. C<$bookfundid> is the new value for the book fund ID
329 in the aqorderbreakdown table of the Koha database. All other
330 arguments update the fields with the same name in the aqorders table.
331
332 C<$user> is ignored.
333
334 =cut
335 #'
336 sub updaterecorder{
337   my($biblio,$ordnum,$user,$cost,$bookfund,$rrp)=@_;
338   my $dbh = C4::Context->dbh;
339   my $sth=$dbh->prepare("update aqorders set
340   unitprice=?, rrp=?
341   where biblionumber=? and ordernumber=?
342   ");
343   $sth->execute($cost,$rrp,$biblio,$ordnum);
344   $sth->finish;
345   $sth=$dbh->prepare("update aqorderbreakdown set bookfundid=? where ordernumber=?");
346   $sth->execute($bookfund,$ordnum);
347   $sth->finish;
348 }
349
350 #
351 #
352 # ORDERS
353 #
354 #
355
356 =item getorders
357
358   ($count, $orders) = &getorders($booksellerid);
359
360 Finds pending orders from the bookseller with the given ID. Ignores
361 completed and cancelled orders.
362
363 C<$count> is the number of elements in C<@{$orders}>.
364
365 C<$orders> is a reference-to-array; each element is a
366 reference-to-hash with the following fields:
367
368 =over 4
369
370 =item C<count(*)>
371
372 Gives the number of orders in with this basket number.
373
374 =item C<authorizedby>
375
376 =item C<entrydate>
377
378 =item C<basketno>
379
380 These give the value of the corresponding field in the aqorders table
381 of the Koha database.
382
383 =back
384
385 Results are ordered from most to least recent.
386
387 =cut
388 #'
389 sub getorders {
390         my ($supplierid)=@_;
391         my $dbh = C4::Context->dbh;
392         my $sth=$dbh->prepare("Select count(*),authorisedby,creationdate,aqbasket.basketno,
393                 closedate,surname,firstname 
394                 from aqorders 
395                 left join aqbasket on aqbasket.basketno=aqorders.basketno 
396                 left join borrowers on aqbasket.authorisedby=borrowers.borrowernumber
397                 where booksellerid=? and (quantity > quantityreceived or
398                 quantityreceived is NULL)
399                 group by basketno order by aqbasket.basketno");
400         $sth->execute($supplierid);
401         my @results = ();
402         while (my $data=$sth->fetchrow_hashref){
403                 push(@results,$data);
404         }
405         $sth->finish;
406         return (scalar(@results),\@results);
407 }
408
409 =item getorder
410
411   ($order, $ordernumber) = &getorder($biblioitemnumber, $biblionumber);
412
413 Looks up the order with the given biblionumber and biblioitemnumber.
414
415 Returns a two-element array. C<$ordernumber> is the order number.
416 C<$order> is a reference-to-hash describing the order; its keys are
417 fields from the biblio, biblioitems, aqorders, and aqorderbreakdown
418 tables of the Koha database.
419
420 =cut
421
422 sub getorder{
423   my ($bi,$bib)=@_;
424   my $dbh = C4::Context->dbh;
425   my $sth=$dbh->prepare("Select ordernumber from aqorders where biblionumber=? and biblioitemnumber=?");
426   $sth->execute($bib,$bi);
427   # FIXME - Use fetchrow_array(), since we're only interested in the one
428   # value.
429   my $ordnum=$sth->fetchrow_hashref;
430   $sth->finish;
431   my $order=getsingleorder($ordnum->{'ordernumber'});
432   return ($order,$ordnum->{'ordernumber'});
433 }
434
435 =item getsingleorder
436
437   $order = &getsingleorder($ordernumber);
438
439 Looks up an order by order number.
440
441 Returns a reference-to-hash describing the order. The keys of
442 C<$order> are fields from the biblio, biblioitems, aqorders, and
443 aqorderbreakdown tables of the Koha database.
444
445 =cut
446
447 sub getsingleorder {
448   my ($ordnum)=@_;
449   my $dbh = C4::Context->dbh;
450   my $sth=$dbh->prepare("Select * from biblio,biblioitems,aqorders left join aqorderbreakdown
451   on aqorders.ordernumber=aqorderbreakdown.ordernumber
452   where aqorders.ordernumber=?
453   and biblio.biblionumber=aqorders.biblionumber and
454   biblioitems.biblioitemnumber=aqorders.biblioitemnumber");
455   $sth->execute($ordnum);
456   my $data=$sth->fetchrow_hashref;
457   $sth->finish;
458   return($data);
459 }
460
461 =item getallorders
462
463   ($count, @results) = &getallorders($booksellerid);
464
465 Looks up all of the pending orders from the supplier with the given
466 bookseller ID. Ignores cancelled and completed orders.
467
468 C<$count> is the number of elements in C<@results>. C<@results> is an
469 array of references-to-hash. The keys of each element are fields from
470 the aqorders, biblio, and biblioitems tables of the Koha database.
471
472 C<@results> is sorted alphabetically by book title.
473
474 =cut
475 #'
476 sub getallorders {
477   #gets all orders from a certain supplier, orders them alphabetically
478   my ($supid)=@_;
479   my $dbh = C4::Context->dbh;
480   my @results = ();
481   my $sth=$dbh->prepare("Select * from aqorders,biblio,biblioitems,aqbasket where aqbasket.basketno=aqorders.basketno
482   and booksellerid=?
483   and (cancelledby is NULL or cancelledby = '')
484   and (quantityreceived < quantity or quantityreceived is NULL)
485   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=
486   aqorders.biblioitemnumber
487   group by aqorders.biblioitemnumber
488   order by
489   biblio.title");
490   $sth->execute($supid);
491   while (my $data=$sth->fetchrow_hashref){
492     push(@results,$data);
493   }
494   $sth->finish;
495   return(scalar(@results),@results);
496 }
497
498 # FIXME - Never used
499 sub getrecorders {
500   #gets all orders from a certain supplier, orders them alphabetically
501   my ($supid)=@_;
502   my $dbh = C4::Context->dbh;
503   my @results= ();
504   my $sth=$dbh->prepare("Select * from aqorders,biblio,biblioitems where booksellerid=?
505   and (cancelledby is NULL or cancelledby = '')
506   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=
507   aqorders.biblioitemnumber and
508   aqorders.quantityreceived>0
509   and aqorders.datereceived >=now()
510   group by aqorders.biblioitemnumber
511   order by
512   biblio.title");
513   $sth->execute($supid);
514   while (my $data=$sth->fetchrow_hashref){
515     push(@results,$data);
516   }
517   $sth->finish;
518   return(scalar(@results),@results);
519 }
520
521 =item ordersearch
522
523   ($count, @results) = &ordersearch($search, $biblionumber, $complete);
524
525 Searches for orders.
526
527 C<$search> may take one of several forms: if it is an ISBN,
528 C<&ordersearch> returns orders with that ISBN. If C<$search> is an
529 order number, C<&ordersearch> returns orders with that order number
530 and biblionumber C<$biblionumber>. Otherwise, C<$search> is considered
531 to be a space-separated list of search terms; in this case, all of the
532 terms must appear in the title (matching the beginning of title
533 words).
534
535 If C<$complete> is C<yes>, the results will include only completed
536 orders. In any case, C<&ordersearch> ignores cancelled orders.
537
538 C<&ordersearch> returns an array. C<$count> is the number of elements
539 in C<@results>. C<@results> is an array of references-to-hash with the
540 following keys:
541
542 =over 4
543
544 =item C<author>
545
546 =item C<seriestitle>
547
548 =item C<branchcode>
549
550 =item C<bookfundid>
551
552 =back
553
554 =cut
555 #'
556 sub ordersearch {
557         my ($search,$id,$biblio,$catview) = @_;
558         my $dbh   = C4::Context->dbh;
559         my @data  = split(' ',$search);
560         my @searchterms = ($id);
561         map { push(@searchterms,"$_%","% $_%") } @data;
562         push(@searchterms,$search,$search,$biblio);
563         my $sth=$dbh->prepare("Select *,biblio.title from aqorders,biblioitems,biblio,aqbasket
564                 where aqorders.biblioitemnumber = biblioitems.biblioitemnumber and
565                 aqorders.basketno = aqbasket.basketno
566                 and aqbasket.booksellerid = ?
567                 and biblio.biblionumber=aqorders.biblionumber
568                 and ((datecancellationprinted is NULL)
569                 or (datecancellationprinted = '0000-00-00'))
570                 and (("
571                 .(join(" and ",map { "(biblio.title like ? or biblio.title like ?)" } @data))
572                 .") or biblioitems.isbn=? or (aqorders.ordernumber=? and aqorders.biblionumber=?)) "
573                 .(($catview ne 'yes')?" and (quantityreceived < quantity or quantityreceived is NULL)":"")
574                 ." group by aqorders.ordernumber");
575         $sth->execute(@searchterms);
576         my @results = ();
577         my $sth2=$dbh->prepare("Select * from biblio where biblionumber=?");
578         my $sth3=$dbh->prepare("Select * from aqorderbreakdown where ordernumber=?");
579         while (my $data=$sth->fetchrow_hashref){
580                 $sth2->execute($data->{'biblionumber'});
581                 my $data2=$sth2->fetchrow_hashref;
582                 $data->{'author'}=$data2->{'author'};
583                 $data->{'seriestitle'}=$data2->{'seriestitle'};
584                 $sth3->execute($data->{'ordernumber'});
585                 my $data3=$sth3->fetchrow_hashref;
586                 $data->{'branchcode'}=$data3->{'branchcode'};
587                 $data->{'bookfundid'}=$data3->{'bookfundid'};
588                 push(@results,$data);
589         }
590         $sth->finish;
591         $sth2->finish;
592         $sth3->finish;
593         return(scalar(@results),@results);
594 }
595
596 #
597 #
598 # MONEY
599 #
600 #
601 =item invoice
602
603   ($count, @results) = &invoice($booksellerinvoicenumber);
604
605 Looks up orders by invoice number.
606
607 Returns an array. C<$count> is the number of elements in C<@results>.
608 C<@results> is an array of references-to-hash; the keys of each
609 elements are fields from the aqorders, biblio, and biblioitems tables
610 of the Koha database.
611
612 =cut
613 #'
614 sub invoice {
615   my ($invoice)=@_;
616   my $dbh = C4::Context->dbh;
617   my @results = ();
618   my $sth=$dbh->prepare("Select * from aqorders,biblio,biblioitems where
619   booksellerinvoicenumber=?
620   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=
621   aqorders.biblioitemnumber group by aqorders.ordernumber,aqorders.biblioitemnumber");
622   $sth->execute($invoice);
623   while (my $data=$sth->fetchrow_hashref){
624     push(@results,$data);
625   }
626   $sth->finish;
627   return(scalar(@results),@results);
628 }
629
630 =item bookfunds
631
632   ($count, @results) = &bookfunds();
633
634 Returns a list of all book funds.
635
636 C<$count> is the number of elements in C<@results>. C<@results> is an
637 array of references-to-hash, whose keys are fields from the aqbookfund
638 and aqbudget tables of the Koha database. Results are ordered
639 alphabetically by book fund name.
640
641 =cut
642 #'
643 sub bookfunds {
644   my $dbh = C4::Context->dbh;
645   my $sth=$dbh->prepare("Select * from aqbookfund,aqbudget where aqbookfund.bookfundid
646   =aqbudget.bookfundid
647   group by aqbookfund.bookfundid order by bookfundname");
648   $sth->execute;
649   my @results = ();
650   while (my $data=$sth->fetchrow_hashref){
651     push(@results,$data);
652   }
653   $sth->finish;
654   return(scalar(@results),@results);
655 }
656
657 =item bookfundbreakdown
658
659         returns the total comtd & spent for a given bookfund
660         used in acqui-home.pl
661 =cut
662 #'
663
664 sub bookfundbreakdown {
665   my ($id)=@_;
666   my $dbh = C4::Context->dbh;
667   my $sth=$dbh->prepare("Select quantity,datereceived,freight,unitprice,listprice,ecost,quantityreceived,subscription
668   from aqorders,aqorderbreakdown where bookfundid=? and
669   aqorders.ordernumber=aqorderbreakdown.ordernumber
670   and (datecancellationprinted is NULL or
671   datecancellationprinted='0000-00-00')");
672   $sth->execute($id);
673   my $comtd=0;
674   my $spent=0;
675   while (my $data=$sth->fetchrow_hashref){
676     if ($data->{'subscription'} == 1){
677       $spent+=$data->{'quantity'}*$data->{'unitprice'};
678     } else {
679       my $leftover=$data->{'quantity'}-$data->{'quantityreceived'};
680       $comtd+=($data->{'ecost'})*$leftover;
681       $spent+=($data->{'unitprice'})*$data->{'quantityreceived'};
682     }
683   }
684   $sth->finish;
685   return($spent,$comtd);
686 }
687
688
689
690 =item curconvert
691
692   $foreignprice = &curconvert($currency, $localprice);
693
694 Converts the price C<$localprice> to foreign currency C<$currency> by
695 dividing by the exchange rate, and returns the result.
696
697 If no exchange rate is found, C<&curconvert> assumes the rate is one
698 to one.
699
700 =cut
701 #'
702 sub curconvert {
703   my ($currency,$price)=@_;
704   my $dbh = C4::Context->dbh;
705   my $sth=$dbh->prepare("Select rate from currency where currency=?");
706   $sth->execute($currency);
707   my $cur=($sth->fetchrow_array())[0];
708   $sth->finish;
709   if ($cur==0){
710     $cur=1;
711   }
712   return($price / $cur);
713 }
714
715 =item getcurrencies
716
717   ($count, $currencies) = &getcurrencies();
718
719 Returns the list of all known currencies.
720
721 C<$count> is the number of elements in C<$currencies>. C<$currencies>
722 is a reference-to-array; its elements are references-to-hash, whose
723 keys are the fields from the currency table in the Koha database.
724
725 =cut
726 #'
727 sub getcurrencies {
728   my $dbh = C4::Context->dbh;
729   my $sth=$dbh->prepare("Select * from currency");
730   $sth->execute;
731   my @results = ();
732   while (my $data=$sth->fetchrow_hashref){
733     push(@results,$data);
734   }
735   $sth->finish;
736   return(scalar(@results),\@results);
737 }
738
739 =item updatecurrencies
740
741   &updatecurrencies($currency, $newrate);
742
743 Sets the exchange rate for C<$currency> to be C<$newrate>.
744
745 =cut
746 #'
747 sub updatecurrencies {
748   my ($currency,$rate)=@_;
749   my $dbh = C4::Context->dbh;
750   my $sth=$dbh->prepare("update currency set rate=? where currency=?");
751   $sth->execute($rate,$currency);
752   $sth->finish;
753 }
754
755 #
756 #
757 # OTHERS
758 #
759 #
760
761 =item bookseller
762
763   ($count, @results) = &bookseller($searchstring);
764
765 Looks up a book seller. C<$searchstring> may be either a book seller
766 ID, or a string to look for in the book seller's name.
767
768 C<$count> is the number of elements in C<@results>. C<@results> is an
769 array of references-to-hash, whose keys are the fields of of the
770 aqbooksellers table in the Koha database.
771
772 =cut
773 #'
774 sub bookseller {
775   my ($searchstring)=@_;
776   my $dbh = C4::Context->dbh;
777   my $sth=$dbh->prepare("Select * from aqbooksellers where name like ? or id = ?");
778   $sth->execute("$searchstring%",$searchstring);
779   my @results;
780   while (my $data=$sth->fetchrow_hashref){
781     push(@results,$data);
782   }
783   $sth->finish;
784   return(scalar(@results),@results);
785 }
786
787 =item breakdown
788
789   ($count, $results) = &breakdown($ordernumber);
790
791 Looks up an order by order ID, and returns its breakdown.
792
793 C<$count> is the number of elements in C<$results>. C<$results> is a
794 reference-to-array; its elements are references-to-hash, whose keys
795 are the fields of the aqorderbreakdown table in the Koha database.
796
797 =cut
798 #'
799 sub breakdown {
800   my ($id)=@_;
801   my $dbh = C4::Context->dbh;
802   my $sth=$dbh->prepare("Select * from aqorderbreakdown where ordernumber=?");
803   $sth->execute($id);
804   my @results = ();
805   while (my $data=$sth->fetchrow_hashref){
806     push(@results,$data);
807   }
808   $sth->finish;
809   return(scalar(@results),\@results);
810 }
811
812 =item branches
813
814   ($count, @results) = &branches();
815
816 Returns a list of all library branches.
817
818 C<$count> is the number of elements in C<@results>. C<@results> is an
819 array of references-to-hash, whose keys are the fields of the branches
820 table of the Koha database.
821
822 =cut
823 #'
824 sub branches {
825     my $dbh   = C4::Context->dbh;
826     my $sth   = $dbh->prepare("Select * from branches order by branchname");
827     my @results = ();
828
829     $sth->execute();
830     while (my $data = $sth->fetchrow_hashref) {
831         push(@results,$data);
832     } # while
833
834     $sth->finish;
835     return(scalar(@results), @results);
836 } # sub branches
837
838 =item updatesup
839
840   &updatesup($bookseller);
841
842 Updates the information for a given bookseller. C<$bookseller> is a
843 reference-to-hash whose keys are the fields of the aqbooksellers table
844 in the Koha database. It must contain entries for all of the fields.
845 The entry to modify is determined by C<$bookseller-E<gt>{id}>.
846
847 The easiest way to get all of the necessary fields is to look up a
848 book seller with C<&booksellers>, modify what's necessary, then call
849 C<&updatesup> with the result.
850
851 =cut
852 #'
853 sub updatesup {
854    my ($data)=@_;
855    my $dbh = C4::Context->dbh;
856    my $sth=$dbh->prepare("Update aqbooksellers set
857    name=?,address1=?,address2=?,address3=?,address4=?,postal=?,
858    phone=?,fax=?,url=?,contact=?,contpos=?,contphone=?,contfax=?,contaltphone=?,
859    contemail=?,contnotes=?,active=?,
860    listprice=?, invoiceprice=?,gstreg=?, listincgst=?,
861    invoiceincgst=?, specialty=?,discount=?,invoicedisc=?,
862    nocalc=?
863    where id=?");
864    $sth->execute($data->{'name'},$data->{'address1'},$data->{'address2'},
865    $data->{'address3'},$data->{'address4'},$data->{'postal'},$data->{'phone'},
866    $data->{'fax'},$data->{'url'},$data->{'contact'},$data->{'contpos'},
867    $data->{'contphone'},$data->{'contfax'},$data->{'contaltphone'},
868    $data->{'contemail'},
869    $data->{'contnote'},$data->{'active'},$data->{'listprice'},
870    $data->{'invoiceprice'},$data->{'gstreg'},$data->{'listincgst'},
871    $data->{'invoiceincgst'},$data->{'specialty'},$data->{'discount'},
872    $data->{'invoicedisc'},$data->{'nocalc'},$data->{'id'});
873    $sth->finish;
874 }
875
876 =item insertsup
877
878   $id = &insertsup($bookseller);
879
880 Creates a new bookseller. C<$bookseller> is a reference-to-hash whose
881 keys are the fields of the aqbooksellers table in the Koha database.
882 All fields must be present.
883
884 Returns the ID of the newly-created bookseller.
885
886 =cut
887 #'
888 sub insertsup {
889   my ($data)=@_;
890   my $dbh = C4::Context->dbh;
891   my $sth=$dbh->prepare("Select max(id) from aqbooksellers");
892   $sth->execute;
893   my $data2=$sth->fetchrow_hashref;
894   $sth->finish;
895   $data2->{'max(id)'}++;
896   $sth=$dbh->prepare("Insert into aqbooksellers (id) values (?)");
897   $sth->execute($data2->{'max(id)'});
898   $sth->finish;
899   $data->{'id'}=$data2->{'max(id)'};
900   updatesup($data);
901   return($data->{'id'});
902 }
903
904 END { }       # module clean-up code here (global destructor)
905
906 1;
907 __END__
908
909 =back
910
911 =head1 AUTHOR
912
913 Koha Developement team <info@koha.org>
914
915 =cut