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
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 '' ) {
142 FROM aqbookfund,aqbudget
143 WHERE aqbookfund.bookfundid=aqbudget.bookfundid
146 AND (aqbookfund.branchcode='' OR aqbookfund.branchcode= ? )
147 GROUP BY aqbookfund.bookfundid ORDER BY bookfundname";
154 WHERE aqbookfund.bookfundid=aqbudget.bookfundid
157 GROUP BY aqbookfund.bookfundid ORDER BY bookfundname
160 my $sth = $dbh->prepare($strsth);
161 if ( $branch ne '' ) {
162 $sth->execute($branch);
168 while ( my $data = $sth->fetchrow_hashref ) {
169 push( @results, $data );
175 #-------------------------------------------------------------#
179 @currencies = &GetCurrencies;
181 Returns the list of all known currencies.
183 C<$currencies> is a array; its elements are references-to-hash, whose
184 keys are the fields from the currency table in the Koha database.
189 my $dbh = C4::Context->dbh;
194 my $sth = $dbh->prepare($query);
197 while ( my $data = $sth->fetchrow_hashref ) {
198 push( @results, $data );
204 #-------------------------------------------------------------#
206 =head3 GetBookFundBreakdown
208 ( $spent, $comtd ) = &GetBookFundBreakdown( $id, $start, $end );
210 returns the total comtd & spent for a given bookfund, and a given year
211 used in acqui-home.pl
215 sub GetBookFundBreakdown {
216 my ( $id, $start, $end ) = @_;
217 my $dbh = C4::Context->dbh;
219 # if no start/end dates given defaut to everything
221 $start = '0000-00-00';
225 # do a query for spent totals.
227 SELECT quantity,datereceived,freight,unitprice,listprice,ecost,
228 quantityreceived,subscription
230 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
232 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
233 AND ((datereceived >= ? and datereceived < ?) OR (budgetdate >= ? and budgetdate < ?))
235 my $sth = $dbh->prepare($query);
236 $sth->execute( $id, $start, $end, $start, $end );
239 while ( my $data = $sth->fetchrow_hashref ) {
240 if ( $data->{'subscription'} == 1 ) {
241 $spent += $data->{'quantity'} * $data->{'unitprice'};
245 my $leftover = $data->{'quantity'} - ($data->{'quantityreceived'}?$data->{'quantityreceived'}:0);
246 $spent += ( $data->{'unitprice'} ) * ($data->{'quantityreceived'}?$data->{'quantityreceived'}:0);
251 # then do a seperate query for commited totals, (pervious single query was
252 # returning incorrect comitted results.
255 SELECT quantity,datereceived,freight,unitprice,
256 listprice,ecost,quantityreceived AS qrev,
257 subscription,title,itemtype,aqorders.biblionumber,
258 aqorders.booksellerinvoicenumber,
259 quantity-quantityreceived AS tleft,
260 aqorders.ordernumber AS ordnum,entrydate,budgetdate
262 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=aqorders.biblioitemnumber
263 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
265 AND (budgetdate >= ? AND budgetdate < ?)
266 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
269 $sth = $dbh->prepare($query);
270 # warn "$start $end";
271 $sth->execute( $id, $start, $end );
275 while ( my $data = $sth->fetchrow_hashref ) {
276 my $left = $data->{'tleft'};
277 if ( !$left || $left eq '' ) {
278 $left = $data->{'quantity'};
280 if ( $left && $left > 0 ) {
281 my $subtotal = $left * $data->{'ecost'};
282 $data->{subtotal} = $subtotal;
283 $data->{'left'} = $left;
286 # use Data::Dumper; warn Dumper($data);
290 return ( $spent, $comtd );
295 &NewBookFund(bookfundid, bookfundname, branchcode);
297 this function create a new bookfund into the database.
302 my ($bookfundid, $bookfundname, $branchcode) = @_;
303 $branchcode = undef unless $branchcode;
304 my $dbh = C4::Context->dbh;
308 (bookfundid, bookfundname, branchcode)
312 my $sth=$dbh->prepare($query);
313 $sth->execute($bookfundid,$bookfundname,"$branchcode");
316 #-------------------------------------------------------------#
320 &ModBookFund($bookfundname,$branchcode,$bookfundid);
321 this function update the bookfundname and the branchcode on aqbookfund table
327 my ($bookfundname,$bookfundid,$branchcode) = @_;
328 my $dbh = C4::Context->dbh;
335 warn "name : $bookfundname";
336 my $sth=$dbh->prepare($query);
337 $sth->execute($bookfundname,$bookfundid,"$branchcode");
338 # budgets depending on a bookfund must have the same branchcode
339 # if the bookfund branchcode is set
340 if (defined $branchcode) {
345 $sth=$dbh->prepare($query);
346 $sth->execute($branchcode);
350 #-------------------------------------------------------------#
352 =head3 SearchBookFund
354 @results = SearchBookFund(
355 $bookfundid,$filter,$filter_bookfundid,
356 $filter_bookfundname,$filter_branchcode);
358 this function searchs among the bookfunds corresponding to our filtering rules.
363 my $dbh = C4::Context->dbh;
366 $filter_bookfundname,
381 if ($filter_bookfundid) {
382 $query.= "AND bookfundid = ?";
383 push @bindings, $filter_bookfundid;
385 if ($filter_bookfundname) {
386 $query.= "AND bookfundname like ?";
387 push @bindings, '%'.$filter_bookfundname.'%';
389 if ($filter_branchcode) {
390 $query.= "AND branchcode = ?";
391 push @bindings, $filter_branchcode;
394 $query.= "ORDER BY bookfundid";
396 my $sth = $dbh->prepare($query);
397 $sth->execute(@bindings);
399 while (my $row = $sth->fetchrow_hashref) {
405 #-------------------------------------------------------------#
409 &ModCurrencies($currency, $newrate);
411 Sets the exchange rate for C<$currency> to be C<$newrate>.
416 my ( $currency, $rate ) = @_;
417 my $dbh = C4::Context->dbh;
423 my $sth = $dbh->prepare($query);
424 $sth->execute( $rate, $currency );
427 #-------------------------------------------------------------#
431 $number = Countbookfund($bookfundid);
433 this function count the number of bookfund with id given on input arg.
435 the result of the SQL query as a number.
440 my $bookfundid = shift;
441 my $branchcode = shift;
442 my $dbh = C4::Context->dbh;
449 my $sth = $dbh->prepare($query);
450 $sth->execute($bookfundid,$branchcode);
451 return $sth->fetchrow;
455 #-------------------------------------------------------------#
457 =head3 ConvertCurrency
459 $foreignprice = &ConvertCurrency($currency, $localprice);
461 Converts the price C<$localprice> to foreign currency C<$currency> by
462 dividing by the exchange rate, and returns the result.
464 If no exchange rate is found, C<&ConvertCurrency> assumes the rate is one
469 sub ConvertCurrency {
470 my ( $currency, $price ) = @_;
471 my $dbh = C4::Context->dbh;
477 my $sth = $dbh->prepare($query);
478 $sth->execute($currency);
479 my $cur = ( $sth->fetchrow_array() )[0];
483 return ( $price / $cur );
486 #-------------------------------------------------------------#
490 &DelBookFund($bookfundid);
491 this function delete a bookfund which has $bokfundid as parameter on aqbookfund table and delete the approriate budget.
496 my $bookfundid = shift;
497 my $branchcode=shift;
498 my $dbh = C4::Context->dbh;
500 DELETE FROM aqbookfund
504 my $sth=$dbh->prepare($query);
505 $sth->execute($bookfundid,$branchcode);
508 DELETE FROM aqbudget where bookfundid=? and branchcode=?
510 $sth=$dbh->prepare($query);
511 $sth->execute($bookfundid,$branchcode);
515 END { } # module clean-up code here (global destructor)
523 Koha Developement team <info@koha.org>