3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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
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.
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
25 use vars qw($VERSION @ISA @EXPORT);
27 # set the version for version checking
28 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v ); };
32 C4::Bookfund - Koha functions for dealing with bookfund, currency & money.
40 the functions in this modules deal with bookfund, currency and money.
41 They allow to get and/or set some informations for a specific budget or currency.
47 &GetBookFund &GetBookFunds &GetBookFundsId &GetBookFundBreakdown &GetCurrencies
49 &ModBookFund &ModCurrencies
62 #-------------------------------------------------------------#
68 $dataaqbookfund = &GetBookFund($bookfundid);
70 this function get the bookfundid, bookfundname, the bookfundgroup, the branchcode
71 from aqbookfund table for bookfundid given on input arg.
73 C<$dataaqbookfund> is a hashref full of bookfundid, bookfundname, bookfundgroup,
82 my $dbh = C4::Context->dbh;
92 my $sth=$dbh->prepare($query);
93 return $sth->fetchrow_hashref;
101 $sth = &GetBookFundsId
102 Read on aqbookfund table and execute a simple SQL query.
105 $sth->execute. Don't forget to fetch row from the database after using
106 this function by using, for example, $sth->fetchrow_hashref;
108 C<@results> is an array of id existing on the database.
115 my @bookfundids_loop;
116 my $dbh= C4::Context->dbh;
121 my $sth = $dbh->prepare($query);
126 #-------------------------------------------------------------#
132 @results = &GetBookFunds;
134 Returns a list of all book funds.
136 C<@results> is an array of references-to-hash, whose keys are fields from the aqbookfund and aqbudget tables of the Koha database. Results are ordered
137 alphabetically by book fund name.
145 my $dbh = C4::Context->dbh;
146 my $userenv = C4::Context->userenv;
147 my $branch = $userenv->{branch};
150 if ( $branch ne '' ) {
153 FROM aqbookfund,aqbudget
154 WHERE aqbookfund.bookfundid=aqbudget.bookfundid
157 AND (aqbookfund.branchcode IS NULL OR aqbookfund.branchcode='' OR aqbookfund.branchcode= ? )
158 GROUP BY aqbookfund.bookfundid ORDER BY bookfundname";
165 WHERE aqbookfund.bookfundid=aqbudget.bookfundid
168 GROUP BY aqbookfund.bookfundid ORDER BY bookfundname
171 my $sth = $dbh->prepare($strsth);
172 if ( $branch ne '' ) {
173 $sth->execute($branch);
179 while ( my $data = $sth->fetchrow_hashref ) {
180 push( @results, $data );
186 #-------------------------------------------------------------#
192 @currencies = &GetCurrencies;
194 Returns the list of all known currencies.
196 C<$currencies> is a array; its elements are references-to-hash, whose
197 keys are the fields from the currency table in the Koha database.
204 my $dbh = C4::Context->dbh;
209 my $sth = $dbh->prepare($query);
212 while ( my $data = $sth->fetchrow_hashref ) {
213 push( @results, $data );
219 #-------------------------------------------------------------#
221 =head3 GetBookFundBreakdown
225 ( $spent, $comtd ) = &GetBookFundBreakdown( $id, $year, $start, $end );
227 returns the total comtd & spent for a given bookfund, and a given year
228 used in acqui-home.pl
234 sub GetBookFundBreakdown {
235 my ( $id, $year, $start, $end ) = @_;
236 my $dbh = C4::Context->dbh;
238 # if no start/end dates given defaut to everything
240 $start = '0000-00-00';
244 # do a query for spent totals.
246 SELECT quantity,datereceived,freight,unitprice,listprice,ecost,
247 quantityreceived,subscription
249 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
251 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
252 AND ((datereceived >= ? and datereceived < ?) OR (budgetdate >= ? and budgetdate < ?))
254 my $sth = $dbh->prepare($query);
255 $sth->execute( $id, $start, $end, $start, $end );
258 while ( my $data = $sth->fetchrow_hashref ) {
259 if ( $data->{'subscription'} == 1 ) {
260 $spent += $data->{'quantity'} * $data->{'unitprice'};
264 my $leftover = $data->{'quantity'} - $data->{'quantityreceived'};
265 $spent += ( $data->{'unitprice'} ) * $data->{'quantityreceived'};
270 # then do a seperate query for commited totals, (pervious single query was
271 # returning incorrect comitted results.
274 SELECT quantity,datereceived,freight,unitprice,
275 listprice,ecost,quantityreceived AS qrev,
276 subscription,biblio.title,itemtype,aqorders.biblionumber,
277 aqorders.booksellerinvoicenumber,
278 quantity-quantityreceived AS tleft,
279 aqorders.ordernumber AS ordnum,entrydate,budgetdate,
280 booksellerid,aqbasket.basketno
281 FROM aqorderbreakdown,
284 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
286 AND aqorders.ordernumber=aqorderbreakdown.ordernumber
287 AND aqorders.basketno=aqbasket.basketno
288 AND (budgetdate >= ? AND budgetdate < ?)
289 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
292 my $sth = $dbh->prepare($query);
293 $sth->execute( $id, $start, $end );
298 while ( my $data = $sth->fetchrow_hashref ) {
299 my $left = $data->{'tleft'};
300 if ( !$left || $left eq '' ) {
301 $left = $data->{'quantity'};
303 if ( $left && $left > 0 ) {
304 my $subtotal = $left * $data->{'ecost'};
305 $data->{subtotal} = $subtotal;
306 $data->{'left'} = $left;
312 return ( $spent, $comtd );
319 &NewBookFund(bookfundid, bookfundname, branchcode);
321 this function create a new bookfund into the database.
328 my ($bookfundid, $bookfundname, $branchcode) = @_;
329 $branchcode = undef unless $branchcode;
330 my $dbh = C4::Context->dbh;
334 (bookfundid, bookfundname, branchcode)
338 my $sth=$dbh->prepare($query);
339 $sth->execute($bookfundid,$bookfundname,$branchcode);
342 #-------------------------------------------------------------#
348 &ModBookFund($bookfundname,$branchcode,$bookfundid);
349 this function update the bookfundname and the branchcode on aqbookfund table
357 my ($bookfundname,$branchcode,$bookfundid) = @_;
358 my $dbh = C4::Context->dbh;
361 SET bookfundname = ?,
365 my $sth=$dbh->prepare($query);
366 $sth->execute($bookfundname,$branchcode,$bookfundid);
367 # budgets depending on a bookfund must have the same branchcode
368 # if the bookfund branchcode is set
369 if (defined $branchcode) {
374 $sth=$dbh->prepare($query);
375 $sth->execute($branchcode);
379 #-------------------------------------------------------------#
381 =head3 SearchBookFund
384 @results = SearchBookFund(
385 $bookfundid,$filter,$filter_bookfundid,
386 $filter_bookfundname,$filter_branchcode);
388 this function searchs among the bookfunds corresponding to our filtering rules.
395 my $dbh = C4::Context->dbh;
398 $filter_bookfundname,
413 if ($filter_bookfundid) {
414 $query.= "AND bookfundid = ?";
415 push @bindings, $filter_bookfundid;
417 if ($filter_bookfundname) {
418 $query.= "AND bookfundname like ?";
419 push @bindings, '%'.$filter_bookfundname.'%';
421 if ($filter_branchcode) {
422 $query.= "AND branchcode = ?";
423 push @bindings, $filter_branchcode;
426 $query.= "ORDER BY bookfundid";
428 my $sth = $dbh->prepare($query);
429 $sth->execute(@bindings);
431 while (my $row = $sth->fetchrow_hashref) {
437 #-------------------------------------------------------------#
443 &ModCurrencies($currency, $newrate);
445 Sets the exchange rate for C<$currency> to be C<$newrate>.
452 my ( $currency, $rate ) = @_;
453 my $dbh = C4::Context->dbh;
459 my $sth = $dbh->prepare($query);
460 $sth->execute( $rate, $currency );
463 #-------------------------------------------------------------#
469 $number = Countbookfund($bookfundid);
471 this function count the number of bookfund with id given on input arg.
473 the result of the SQL query as a number.
480 my $bookfundid = shift;
481 my $dbh = C4::Context->dbh;
487 my $sth = $dbh->prepare($query);
488 $sth->execute($bookfundid);
489 return $sth->fetchrow;
493 #-------------------------------------------------------------#
495 =head3 ConvertCurrency
499 $foreignprice = &ConvertCurrency($currency, $localprice);
501 Converts the price C<$localprice> to foreign currency C<$currency> by
502 dividing by the exchange rate, and returns the result.
504 If no exchange rate is found, C<&ConvertCurrency> assumes the rate is one
511 sub ConvertCurrency {
512 my ( $currency, $price ) = @_;
513 my $dbh = C4::Context->dbh;
519 my $sth = $dbh->prepare($query);
520 $sth->execute($currency);
521 my $cur = ( $sth->fetchrow_array() )[0];
525 return ( $price / $cur );
528 #-------------------------------------------------------------#
534 &DelBookFund($bookfundid);
535 this function delete a bookfund which has $bokfundid as parameter on aqbookfund table and delete the approriate budget.
542 my $bookfundid = shift;
543 my $dbh = C4::Context->dbh;
545 DELETE FROM aqbookfund
548 my $sth=$dbh->prepare($query);
549 $sth->execute($bookfundid);
552 DELETE FROM aqbudget where bookfundid=?
554 $sth=$dbh->prepare($query);
555 $sth->execute($bookfundid);
559 END { } # module clean-up code here (global destructor)
569 Koha Developement team <info@koha.org>