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