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