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
22 # use Smart::Comments;
24 use vars
qw($VERSION @ISA @EXPORT);
26 # set the version for version checking
31 C4::Bookfund - Koha functions for dealing with bookfund, currency & money.
39 the functions in this modules deal with bookfund, currency and money.
40 They allow to get and/or set some informations for a specific budget or currency.
46 &GetBookFund &GetBookFunds &GetBookFundsId &GetBookFundBreakdown &GetCurrencies
48 &ModBookFund &ModCurrencies
60 #-------------------------------------------------------------#
64 $dataaqbookfund = &GetBookFund($bookfundid);
66 this function get the bookfundid, bookfundname, the bookfundgroup, the branchcode
67 from aqbookfund table for bookfundid given on input arg.
69 C<$dataaqbookfund> is a hashref full of bookfundid, bookfundname, bookfundgroup,
75 my $bookfundid = shift;
76 my $branchcode = shift;
77 $branchcode=($branchcode?
$branchcode:'');
78 my $dbh = C4
::Context
->dbh;
88 my $sth=$dbh->prepare($query);
89 $sth->execute($bookfundid,$branchcode);
90 my $data=$sth->fetchrow_hashref;
97 $sth = &GetBookFundsId
98 Read on aqbookfund table and execute a simple SQL query.
101 $sth->execute. Don't forget to fetch row from the database after using
102 this function by using, for example, $sth->fetchrow_hashref;
104 C<@results> is an array of id existing on the database.
109 my @bookfundids_loop;
110 my $dbh= C4
::Context
->dbh;
112 SELECT bookfundid,branchcode
115 my $sth = $dbh->prepare($query);
120 #-------------------------------------------------------------#
124 @results = &GetBookFunds;
126 Returns a list of all book funds.
128 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
129 alphabetically by book fund name.
135 my $dbh = C4
::Context
->dbh;
136 my $userenv = C4
::Context
->userenv;
139 if ( $branch ne '' ) {
143 LEFT JOIN aqbudget ON aqbookfund.bookfundid=aqbudget.bookfundid
144 WHERE startdate<now()
146 AND (aqbookfund.branchcode='' OR aqbookfund.branchcode= ? )
147 GROUP BY aqbookfund.bookfundid ORDER BY bookfundname";
153 LEFT JOIN aqbudget ON aqbookfund.bookfundid=aqbudget.bookfundid
154 WHERE startdate<now()
156 GROUP BY aqbookfund.bookfundid ORDER BY bookfundname
159 my $sth = $dbh->prepare($strsth);
160 if ( $branch ne '' ) {
161 $sth->execute($branch);
167 while ( my $data = $sth->fetchrow_hashref ) {
168 push( @results, $data );
174 #-------------------------------------------------------------#
178 @currencies = &GetCurrencies;
180 Returns the list of all known currencies.
182 C<$currencies> is a array; its elements are references-to-hash, whose
183 keys are the fields from the currency table in the Koha database.
188 my $dbh = C4
::Context
->dbh;
193 my $sth = $dbh->prepare($query);
196 while ( my $data = $sth->fetchrow_hashref ) {
197 push( @results, $data );
203 #-------------------------------------------------------------#
205 =head3 GetLocalCurrency
207 $currency = GetLocalCurrency;
209 Returns the currency with exchange rate 1 (the local currency).
211 $currency is a reference-to-hash, whose keys are the fields from the currency
212 table in the Koha database.
216 sub GetLocalCurrency
{
217 my $dbh = C4
::Context
->dbh;
219 my $result = $dbh->selectrow_hashref("
221 currency, symbol, timestamp, rate
226 die "No currency found with rate 1" if ( !defined( $result ) );
231 #-------------------------------------------------------------#
233 =head3 GetBookFundBreakdown
235 ( $spent, $comtd ) = &GetBookFundBreakdown( $id, $start, $end );
237 returns the total comtd & spent for a given bookfund, and a given year
238 used in acqui-home.pl
242 sub GetBookFundBreakdown
{
243 my ( $id, $start, $end ) = @_;
244 my $dbh = C4
::Context
->dbh;
246 # if no start/end dates given defaut to everything
248 $start = '0000-00-00';
252 # do a query for spent totals.
254 SELECT quantity,datereceived,freight,unitprice,listprice,ecost,
255 quantityreceived,subscription
257 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
258 LEFT JOIN aqbookfund ON (aqorderbreakdown.bookfundid=aqbookfund.bookfundid and aqorderbreakdown.branchcode=aqbookfund.branchcode)
259 LEFT JOIN aqbudget ON (aqbudget.bookfundid=aqbookfund.bookfundid and aqbudget.branchcode=aqbookfund.branchcode)
260 WHERE aqorderbreakdown.bookfundid=?
261 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
262 AND ((budgetdate >= ? and budgetdate < ?) OR (startdate>=? and enddate<=?))
264 my $sth = $dbh->prepare($query);
265 $sth->execute( $id, $start, $end, $start, $end );
268 while ( my $data = $sth->fetchrow_hashref ) {
269 if ( $data->{'subscription'} == 1 ) {
270 $spent += $data->{'quantity'} * $data->{'unitprice'};
273 $spent += ( $data->{'unitprice'} ) * ($data->{'quantityreceived'}?
$data->{'quantityreceived'}:0);
278 # then do a seperate query for commited totals, (pervious single query was
279 # returning incorrect comitted results.
282 SELECT quantity,datereceived,freight,unitprice,
283 listprice,ecost,quantityreceived AS qrev,
284 subscription,title,itemtype,aqorders.biblionumber,
285 aqorders.booksellerinvoicenumber,
286 quantity-quantityreceived AS tleft,
287 aqorders.ordernumber AS ordnum,entrydate,budgetdate
289 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=aqorders.biblioitemnumber
290 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
292 AND (budgetdate >= ? AND budgetdate < ?)
293 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
296 $sth = $dbh->prepare($query);
297 # warn "$start $end";
298 $sth->execute( $id, $start, $end );
302 while ( my $data = $sth->fetchrow_hashref ) {
303 my $left = $data->{'tleft'};
304 if ( !$left || $left eq '' ) {
305 $left = $data->{'quantity'};
307 if ( $left && $left > 0 ) {
308 my $subtotal = $left * $data->{'ecost'};
309 $data->{subtotal
} = $subtotal;
310 $data->{'left'} = $left;
313 # use Data::Dumper; warn Dumper($data);
317 return ( $spent, $comtd );
322 &NewBookFund(bookfundid, bookfundname, branchcode);
324 this function create a new bookfund into the database.
329 my ($bookfundid, $bookfundname, $branchcode) = @_;
330 $branchcode = undef unless $branchcode;
331 my $dbh = C4
::Context
->dbh;
335 (bookfundid, bookfundname, branchcode)
339 my $sth=$dbh->prepare($query);
340 $sth->execute($bookfundid,$bookfundname,"$branchcode");
343 #-------------------------------------------------------------#
347 &ModBookFund($bookfundname,$bookfundid,$current_branch, $branchcode)
349 This function updates the bookfundname and the branchcode in the aqbookfund table.
353 # FIXME: use placeholders, ->prepare(), ->execute()
356 my ($bookfundname,$bookfundid,$current_branch, $branchcode) = @_;
358 my $dbh = C4
::Context
->dbh;
360 my $retval = $dbh->do("
362 SET bookfundname = '$bookfundname',
363 branchcode = '$branchcode'
364 WHERE bookfundid = '$bookfundid'
365 AND branchcode = '$current_branch'
370 # budgets depending on a bookfund must have the same branchcode
372 # if the bookfund branchcode is set, and previous update is successfull, then update aqbudget.branchcode too.
373 if (defined $branchcode && $retval > 0) {
374 my $query = "UPDATE aqbudget
376 WHERE bookfundid = ? ";
378 my $sth=$dbh->prepare($query);
379 $sth->execute($branchcode, $bookfundid) ;
383 #-------------------------------------------------------------#
385 =head3 SearchBookFund
387 @results = SearchBookFund(
388 $bookfundid,$filter,$filter_bookfundid,
389 $filter_bookfundname,$filter_branchcode);
391 this function searchs among the bookfunds corresponding to our filtering rules.
396 my $dbh = C4
::Context
->dbh;
399 $filter_bookfundname,
414 if ($filter_bookfundid) {
415 $query.= "AND bookfundid = ?";
416 push @bindings, $filter_bookfundid;
418 if ($filter_bookfundname) {
419 $query.= "AND bookfundname like ?";
420 push @bindings, '%'.$filter_bookfundname.'%';
422 if ($filter_branchcode) {
423 $query.= "AND branchcode = ?";
424 push @bindings, $filter_branchcode;
427 $query.= "ORDER BY bookfundid";
429 my $sth = $dbh->prepare($query);
430 $sth->execute(@bindings);
432 while (my $row = $sth->fetchrow_hashref) {
438 #-------------------------------------------------------------#
442 &ModCurrencies($currency, $newrate);
444 Sets the exchange rate for C<$currency> to be C<$newrate>.
449 my ( $currency, $rate ) = @_;
450 my $dbh = C4
::Context
->dbh;
456 my $sth = $dbh->prepare($query);
457 $sth->execute( $rate, $currency );
460 #-------------------------------------------------------------#
464 $number = Countbookfund($bookfundid);
466 this function count the number of bookfund with id given on input arg.
468 the result of the SQL query as a number.
473 my $bookfundid = shift;
474 my $branchcode = shift;
475 my $dbh = C4
::Context
->dbh;
482 my $sth = $dbh->prepare($query);
483 $sth->execute($bookfundid,"$branchcode");
484 return $sth->fetchrow;
488 #-------------------------------------------------------------#
490 =head3 ConvertCurrency
492 $foreignprice = &ConvertCurrency($currency, $localprice);
494 Converts the price C<$localprice> to foreign currency C<$currency> by
495 dividing by the exchange rate, and returns the result.
497 If no exchange rate is found, C<&ConvertCurrency> assumes the rate is one
502 sub ConvertCurrency
{
503 my ( $currency, $price ) = @_;
504 my $dbh = C4
::Context
->dbh;
510 my $sth = $dbh->prepare($query);
511 $sth->execute($currency);
512 my $cur = ( $sth->fetchrow_array() )[0];
516 return ( $price / $cur );
519 #-------------------------------------------------------------#
523 &DelBookFund($bookfundid);
524 this function delete a bookfund which has $bokfundid as parameter on aqbookfund table and delete the approriate budget.
529 my $bookfundid = shift;
530 my $branchcode=shift;
531 my $dbh = C4
::Context
->dbh;
533 DELETE FROM aqbookfund
537 my $sth=$dbh->prepare($query);
538 $sth->execute($bookfundid,$branchcode);
541 DELETE FROM aqbudget where bookfundid=? and branchcode=?
543 $sth=$dbh->prepare($query);
544 $sth->execute($bookfundid,$branchcode);
548 END { } # module clean-up code here (global destructor)
556 Koha Developement team <info@koha.org>