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
26 use vars qw($VERSION @ISA @EXPORT);
28 # set the version for version checking
29 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v ); };
33 C4::Bookfund - Koha functions for dealing with bookfund, currency & money.
41 the functions in this modules deal with bookfund, currency and money.
42 They allow to get and/or set some informations for a specific budget or currency.
48 &GetBookFund &GetBookFunds &GetBookFundBreakdown &GetCurrencies
49 &ModBookFund &ModCurrencies
60 #-------------------------------------------------------------#
66 $dataaqbookfund = &GetBookFund($bookfundid);
68 this function get the bookfundid, bookfundname, the bookfundgroup, the branchcode
69 from aqbookfund table for bookfundid given on input arg.
71 C<$dataaqbookfund> is a hashref full of bookfundid, bookfundname, bookfundgroup,
80 my $dbh = C4::Context->dbh;
90 my $sth=$dbh->prepare($query);
91 return $sth->fetchrow_hashref;
94 #-------------------------------------------------------------#
100 @results = &GetBookFunds;
102 Returns a list of all book funds.
104 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
105 alphabetically by book fund name.
113 my $dbh = C4::Context->dbh;
114 my $userenv = C4::Context->userenv;
115 my $branch = $userenv->{branch};
118 if ( $branch ne '' ) {
121 FROM aqbookfund,aqbudget
122 WHERE aqbookfund.bookfundid=aqbudget.bookfundid
125 AND (aqbookfund.branchcode IS NULL OR aqbookfund.branchcode='' OR aqbookfund.branchcode= ? )
126 GROUP BY aqbookfund.bookfundid ORDER BY bookfundname";
133 WHERE aqbookfund.bookfundid=aqbudget.bookfundid
136 GROUP BY aqbookfund.bookfundid ORDER BY bookfundname
139 my $sth = $dbh->prepare($strsth);
140 if ( $branch ne '' ) {
141 $sth->execute($branch);
147 while ( my $data = $sth->fetchrow_hashref ) {
148 push( @results, $data );
154 #-------------------------------------------------------------#
160 @currencies = &GetCurrencies;
162 Returns the list of all known currencies.
164 C<$currencies> is a array; its elements are references-to-hash, whose
165 keys are the fields from the currency table in the Koha database.
172 my $dbh = C4::Context->dbh;
177 my $sth = $dbh->prepare($query);
180 while ( my $data = $sth->fetchrow_hashref ) {
181 push( @results, $data );
187 #-------------------------------------------------------------#
189 =head3 GetBookFundBreakdown
193 ( $spent, $comtd ) = &GetBookFundBreakdown( $id, $year, $start, $end );
195 returns the total comtd & spent for a given bookfund, and a given year
196 used in acqui-home.pl
202 sub GetBookFundBreakdown {
203 my ( $id, $year, $start, $end ) = @_;
204 my $dbh = C4::Context->dbh;
206 # if no start/end dates given defaut to everything
208 $start = '0000-00-00';
212 # do a query for spent totals.
214 SELECT quantity,datereceived,freight,unitprice,listprice,ecost,
215 quantityreceived,subscription
217 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
219 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
220 AND ((datereceived >= ? and datereceived < ?) OR (budgetdate >= ? and budgetdate < ?))
222 my $sth = $dbh->prepare($query);
223 $sth->execute( $id, $start, $end, $start, $end );
226 while ( my $data = $sth->fetchrow_hashref ) {
227 if ( $data->{'subscription'} == 1 ) {
228 $spent += $data->{'quantity'} * $data->{'unitprice'};
232 my $leftover = $data->{'quantity'} - $data->{'quantityreceived'};
233 $spent += ( $data->{'unitprice'} ) * $data->{'quantityreceived'};
238 # then do a seperate query for commited totals, (pervious single query was
239 # returning incorrect comitted results.
242 SELECT quantity,datereceived,freight,unitprice,
243 listprice,ecost,quantityreceived AS qrev,
244 subscription,title,itemtype,aqorders.biblionumber,
245 aqorders.booksellerinvoicenumber,
246 quantity-quantityreceived AS tleft,
247 aqorders.ordernumber AS ordnum,entrydate,budgetdate,
248 booksellerid,aqbasket.basketno
249 FROM aqorderbreakdown,
252 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=aqorders.biblioitemnumber
254 AND aqorders.ordernumber=aqorderbreakdown.ordernumber
255 AND aqorders.basketno=aqbasket.basketno
256 AND (budgetdate >= ? AND budgetdate < ?)
257 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
260 my $sth = $dbh->prepare($query);
261 $sth->execute( $id, $start, $end );
266 while ( my $data = $sth->fetchrow_hashref ) {
267 my $left = $data->{'tleft'};
268 if ( !$left || $left eq '' ) {
269 $left = $data->{'quantity'};
271 if ( $left && $left > 0 ) {
272 my $subtotal = $left * $data->{'ecost'};
273 $data->{subtotal} = $subtotal;
274 $data->{'left'} = $left;
280 return ( $spent, $comtd );
283 #-------------------------------------------------------------#
289 &ModBookFund($bookfundname,$branchcode,$bookfundid);
290 this function update the bookfundname and the branchcode on aqbookfund table
298 my ($bookfundname,$branchcode,$bookfundid) = @_;
299 my $dbh = C4::Context->dbh;
302 SET bookfundname = ?,
306 my $sth=$dbh->prepare($query);
307 $sth->execute($bookfundname,$branchcode,$bookfundid);
308 # budgets depending on a bookfund must have the same branchcode
309 # if the bookfund branchcode is set
310 if (defined $branchcode) {
315 $sth=$dbh->prepare($query);
316 $sth->execute($branchcode);
320 #-------------------------------------------------------------#
326 &ModCurrencies($currency, $newrate);
328 Sets the exchange rate for C<$currency> to be C<$newrate>.
335 my ( $currency, $rate ) = @_;
336 my $dbh = C4::Context->dbh;
342 my $sth = $dbh->prepare($query);
343 $sth->execute( $rate, $currency );
346 #-------------------------------------------------------------#
352 $data = Countbookfund($bookfundid);
354 this function count the number of bookfund with id given on input arg.
356 the result of the SQL query as an hashref.
364 my $dbh = C4::Context->dbh;
370 my $sth = $dbh->prepare($query);
371 $sth->execute($bookfundid);
372 return $sth->fetchrow_hashref;
376 #-------------------------------------------------------------#
378 =head3 ConvertCurrency
382 $foreignprice = &ConvertCurrency($currency, $localprice);
384 Converts the price C<$localprice> to foreign currency C<$currency> by
385 dividing by the exchange rate, and returns the result.
387 If no exchange rate is found, C<&ConvertCurrency> assumes the rate is one
394 sub ConvertCurrency {
395 my ( $currency, $price ) = @_;
396 my $dbh = C4::Context->dbh;
402 my $sth = $dbh->prepare($query);
403 $sth->execute($currency);
404 my $cur = ( $sth->fetchrow_array() )[0];
408 return ( $price / $cur );
412 END { } # module clean-up code here (global destructor)
422 Koha Developement team <info@koha.org>