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
50 &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;
96 #-------------------------------------------------------------#
102 @results = &GetBookFunds;
104 Returns a list of all book funds.
106 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
107 alphabetically by book fund name.
115 my $dbh = C4::Context->dbh;
116 my $userenv = C4::Context->userenv;
117 my $branch = $userenv->{branch};
120 if ( $branch ne '' ) {
123 FROM aqbookfund,aqbudget
124 WHERE aqbookfund.bookfundid=aqbudget.bookfundid
127 AND (aqbookfund.branchcode IS NULL OR aqbookfund.branchcode='' OR aqbookfund.branchcode= ? )
128 GROUP BY aqbookfund.bookfundid ORDER BY bookfundname";
135 WHERE aqbookfund.bookfundid=aqbudget.bookfundid
138 GROUP BY aqbookfund.bookfundid ORDER BY bookfundname
141 my $sth = $dbh->prepare($strsth);
142 if ( $branch ne '' ) {
143 $sth->execute($branch);
149 while ( my $data = $sth->fetchrow_hashref ) {
150 push( @results, $data );
156 #-------------------------------------------------------------#
162 @currencies = &GetCurrencies;
164 Returns the list of all known currencies.
166 C<$currencies> is a array; its elements are references-to-hash, whose
167 keys are the fields from the currency table in the Koha database.
174 my $dbh = C4::Context->dbh;
179 my $sth = $dbh->prepare($query);
182 while ( my $data = $sth->fetchrow_hashref ) {
183 push( @results, $data );
189 #-------------------------------------------------------------#
191 =head3 GetBookFundBreakdown
195 ( $spent, $comtd ) = &GetBookFundBreakdown( $id, $year, $start, $end );
197 returns the total comtd & spent for a given bookfund, and a given year
198 used in acqui-home.pl
204 sub GetBookFundBreakdown {
205 my ( $id, $year, $start, $end ) = @_;
206 my $dbh = C4::Context->dbh;
208 # if no start/end dates given defaut to everything
210 $start = '0000-00-00';
214 # do a query for spent totals.
216 SELECT quantity,datereceived,freight,unitprice,listprice,ecost,
217 quantityreceived,subscription
219 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
221 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
222 AND ((datereceived >= ? and datereceived < ?) OR (budgetdate >= ? and budgetdate < ?))
224 my $sth = $dbh->prepare($query);
225 $sth->execute( $id, $start, $end, $start, $end );
228 while ( my $data = $sth->fetchrow_hashref ) {
229 if ( $data->{'subscription'} == 1 ) {
230 $spent += $data->{'quantity'} * $data->{'unitprice'};
234 my $leftover = $data->{'quantity'} - $data->{'quantityreceived'};
235 $spent += ( $data->{'unitprice'} ) * $data->{'quantityreceived'};
240 # then do a seperate query for commited totals, (pervious single query was
241 # returning incorrect comitted results.
244 SELECT quantity,datereceived,freight,unitprice,
245 listprice,ecost,quantityreceived AS qrev,
246 subscription,title,itemtype,aqorders.biblionumber,
247 aqorders.booksellerinvoicenumber,
248 quantity-quantityreceived AS tleft,
249 aqorders.ordernumber AS ordnum,entrydate,budgetdate,
250 booksellerid,aqbasket.basketno
251 FROM aqorderbreakdown,
254 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=aqorders.biblioitemnumber
256 AND aqorders.ordernumber=aqorderbreakdown.ordernumber
257 AND aqorders.basketno=aqbasket.basketno
258 AND (budgetdate >= ? AND budgetdate < ?)
259 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
262 my $sth = $dbh->prepare($query);
263 $sth->execute( $id, $start, $end );
268 while ( my $data = $sth->fetchrow_hashref ) {
269 my $left = $data->{'tleft'};
270 if ( !$left || $left eq '' ) {
271 $left = $data->{'quantity'};
273 if ( $left && $left > 0 ) {
274 my $subtotal = $left * $data->{'ecost'};
275 $data->{subtotal} = $subtotal;
276 $data->{'left'} = $left;
282 return ( $spent, $comtd );
289 &NewBookFund(bookfundid, bookfundname, branchcode);
291 this function create a new bookfund into the database.
298 my ($bookfundid, $bookfundname, $branchcode) = @_;
299 $branchcode = undef unless $branchcode;
300 my $dbh = C4::Context->dbh;
304 (bookfundid, bookfundname, branchcode)
308 my $sth=$dbh->prepare($query);
309 $sth->execute($bookfundid,$bookfundname,$branchcode);
312 #-------------------------------------------------------------#
318 &ModBookFund($bookfundname,$branchcode,$bookfundid);
319 this function update the bookfundname and the branchcode on aqbookfund table
327 my ($bookfundname,$branchcode,$bookfundid) = @_;
328 my $dbh = C4::Context->dbh;
331 SET bookfundname = ?,
335 my $sth=$dbh->prepare($query);
336 $sth->execute($bookfundname,$branchcode,$bookfundid);
337 # budgets depending on a bookfund must have the same branchcode
338 # if the bookfund branchcode is set
339 if (defined $branchcode) {
344 $sth=$dbh->prepare($query);
345 $sth->execute($branchcode);
349 #-------------------------------------------------------------#
351 =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.
365 my $dbh = C4::Context->dbh;
368 $filter_bookfundname,
383 if ($filter_bookfundid) {
384 $query.= "AND bookfundid = ?";
385 push @bindings, $filter_bookfundid;
387 if ($filter_bookfundname) {
388 $query.= "AND bookfundname like ?";
389 push @bindings, '%'.$filter_bookfundname.'%';
391 if ($filter_branchcode) {
392 $query.= "AND branchcode = ?";
393 push @bindings, $filter_branchcode;
396 $query.= "ORDER BY bookfundid";
398 my $sth = $dbh->prepare($query);
399 $sth->execute(@bindings);
401 while (my $row = $sth->fetchrow_hashref) {
407 #-------------------------------------------------------------#
413 &ModCurrencies($currency, $newrate);
415 Sets the exchange rate for C<$currency> to be C<$newrate>.
422 my ( $currency, $rate ) = @_;
423 my $dbh = C4::Context->dbh;
429 my $sth = $dbh->prepare($query);
430 $sth->execute( $rate, $currency );
433 #-------------------------------------------------------------#
439 $data = Countbookfund($bookfundid);
441 this function count the number of bookfund with id given on input arg.
443 the result of the SQL query as an hashref.
451 my $dbh = C4::Context->dbh;
457 my $sth = $dbh->prepare($query);
458 $sth->execute($bookfundid);
459 return $sth->fetchrow_hashref;
463 #-------------------------------------------------------------#
465 =head3 ConvertCurrency
469 $foreignprice = &ConvertCurrency($currency, $localprice);
471 Converts the price C<$localprice> to foreign currency C<$currency> by
472 dividing by the exchange rate, and returns the result.
474 If no exchange rate is found, C<&ConvertCurrency> assumes the rate is one
481 sub ConvertCurrency {
482 my ( $currency, $price ) = @_;
483 my $dbh = C4::Context->dbh;
489 my $sth = $dbh->prepare($query);
490 $sth->execute($currency);
491 my $cur = ( $sth->fetchrow_array() )[0];
495 return ( $price / $cur );
498 #-------------------------------------------------------------#
504 &DelBookFund($bookfundid);
505 this function delete a bookfund which has $bokfundid as parameter on aqbookfund table and delete the approriate budget.
512 my $bookfundid = shift;
513 my $dbh = C4::Context->dbh;
515 DELETE FROM aqbookfund
518 my $sth=$dbh->prepare($query);
519 $sth->execute($bookfundid);
522 DELETE FROM aqbudget where bookfundid=?
524 $sth=$dbh->prepare($query);
525 $sth->execute($bookfundid);
529 END { } # module clean-up code here (global destructor)
539 Koha Developement team <info@koha.org>