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
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
59 #-------------------------------------------------------------#
63 $dataaqbookfund = &GetBookFund($bookfundid);
65 this function get the bookfundid, bookfundname, the bookfundgroup, the branchcode
66 from aqbookfund table for bookfundid given on input arg.
68 C<$dataaqbookfund> is a hashref full of bookfundid, bookfundname, bookfundgroup,
74 my $bookfundid = shift;
75 my $branchcode = shift;
76 $branchcode=($branchcode?$branchcode:'');
77 my $dbh = C4::Context->dbh;
87 my $sth=$dbh->prepare($query);
88 $sth->execute($bookfundid,$branchcode);
89 my $data=$sth->fetchrow_hashref;
96 $sth = &GetBookFundsId
97 Read on aqbookfund table and execute a simple SQL query.
100 $sth->execute. Don't forget to fetch row from the database after using
101 this function by using, for example, $sth->fetchrow_hashref;
103 C<@results> is an array of id existing on the database.
108 my @bookfundids_loop;
109 my $dbh= C4::Context->dbh;
111 SELECT bookfundid,branchcode
114 my $sth = $dbh->prepare($query);
119 #-------------------------------------------------------------#
123 @results = &GetBookFunds;
125 Returns a list of all book funds.
127 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
128 alphabetically by book fund name.
134 my $dbh = C4::Context->dbh;
135 my $userenv = C4::Context->userenv;
138 if ( $branch ne '' ) {
142 LEFT JOIN aqbudget ON aqbookfund.bookfundid=aqbudget.bookfundid
143 WHERE startdate<now()
145 AND (aqbookfund.branchcode='' OR aqbookfund.branchcode= ? )
146 GROUP BY aqbookfund.bookfundid ORDER BY bookfundname";
152 LEFT JOIN aqbudget ON aqbookfund.bookfundid=aqbudget.bookfundid
153 WHERE startdate<now()
155 GROUP BY aqbookfund.bookfundid ORDER BY bookfundname
158 my $sth = $dbh->prepare($strsth);
159 if ( $branch ne '' ) {
160 $sth->execute($branch);
166 while ( my $data = $sth->fetchrow_hashref ) {
167 push( @results, $data );
173 #-------------------------------------------------------------#
177 @currencies = &GetCurrencies;
179 Returns the list of all known currencies.
181 C<$currencies> is a array; its elements are references-to-hash, whose
182 keys are the fields from the currency table in the Koha database.
187 my $dbh = C4::Context->dbh;
192 my $sth = $dbh->prepare($query);
195 while ( my $data = $sth->fetchrow_hashref ) {
196 push( @results, $data );
202 #-------------------------------------------------------------#
204 =head3 GetBookFundBreakdown
206 ( $spent, $comtd ) = &GetBookFundBreakdown( $id, $start, $end );
208 returns the total comtd & spent for a given bookfund, and a given year
209 used in acqui-home.pl
213 sub GetBookFundBreakdown {
214 my ( $id, $start, $end ) = @_;
215 my $dbh = C4::Context->dbh;
217 # if no start/end dates given defaut to everything
219 $start = '0000-00-00';
223 # do a query for spent totals.
225 SELECT quantity,datereceived,freight,unitprice,listprice,ecost,
226 quantityreceived,subscription
228 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
229 LEFT JOIN aqbookfund ON (aqorderbreakdown.bookfundid=aqbookfund.bookfundid and aqorderbreakdown.branchcode=aqbookfund.branchcode)
230 LEFT JOIN aqbudget ON (aqbudget.bookfundid=aqbookfund.bookfundid and aqbudget.branchcode=aqbookfund.branchcode)
231 WHERE aqorderbreakdown.bookfundid=?
232 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
233 AND ((budgetdate >= ? and budgetdate < ?) OR (startdate>=? and enddate<=?))
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'};
244 $spent += ( $data->{'unitprice'} ) * ($data->{'quantityreceived'}?$data->{'quantityreceived'}:0);
249 # then do a seperate query for commited totals, (pervious single query was
250 # returning incorrect comitted results.
253 SELECT quantity,datereceived,freight,unitprice,
254 listprice,ecost,quantityreceived AS qrev,
255 subscription,title,itemtype,aqorders.biblionumber,
256 aqorders.booksellerinvoicenumber,
257 quantity-quantityreceived AS tleft,
258 aqorders.ordernumber AS ordnum,entrydate,budgetdate
260 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=aqorders.biblioitemnumber
261 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
263 AND (budgetdate >= ? AND budgetdate < ?)
264 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
267 $sth = $dbh->prepare($query);
268 # warn "$start $end";
269 $sth->execute( $id, $start, $end );
273 while ( my $data = $sth->fetchrow_hashref ) {
274 my $left = $data->{'tleft'};
275 if ( (!$left && (!$data->{'datereceived'}||$data->{'datereceived'} eq '0000-00-00') ) || $left eq '' ) {
276 $left = $data->{'quantity'};
278 if ( $left && $left > 0 ) {
279 my $subtotal = $left * $data->{'ecost'};
280 $data->{subtotal} = $subtotal;
281 $data->{'left'} = $left;
284 # use Data::Dumper; warn Dumper($data);
288 return ( $spent, $comtd );
293 &NewBookFund(bookfundid, bookfundname, branchcode);
295 this function create a new bookfund into the database.
300 my ($bookfundid, $bookfundname, $branchcode) = @_;
301 $branchcode = undef unless $branchcode;
302 my $dbh = C4::Context->dbh;
306 (bookfundid, bookfundname, branchcode)
310 my $sth=$dbh->prepare($query);
311 $sth->execute($bookfundid,$bookfundname,"$branchcode");
314 #-------------------------------------------------------------#
318 &ModBookFund($bookfundname,$branchcode,$bookfundid);
319 this function update the bookfundname and the branchcode on aqbookfund table
325 my ($bookfundname,$bookfundid,$branchcode) = @_;
326 my $dbh = C4::Context->dbh;
333 warn "name : $bookfundname";
334 my $sth=$dbh->prepare($query);
335 $sth->execute($bookfundname,$bookfundid,"$branchcode");
336 # budgets depending on a bookfund must have the same branchcode
337 # if the bookfund branchcode is set
338 if (defined $branchcode) {
343 $sth=$dbh->prepare($query);
344 $sth->execute($branchcode);
348 #-------------------------------------------------------------#
350 =head3 SearchBookFund
352 @results = SearchBookFund(
353 $bookfundid,$filter,$filter_bookfundid,
354 $filter_bookfundname,$filter_branchcode);
356 this function searchs among the bookfunds corresponding to our filtering rules.
361 my $dbh = C4::Context->dbh;
364 $filter_bookfundname,
379 if ($filter_bookfundid) {
380 $query.= "AND bookfundid = ?";
381 push @bindings, $filter_bookfundid;
383 if ($filter_bookfundname) {
384 $query.= "AND bookfundname like ?";
385 push @bindings, '%'.$filter_bookfundname.'%';
387 if ($filter_branchcode) {
388 $query.= "AND branchcode = ?";
389 push @bindings, $filter_branchcode;
392 $query.= "ORDER BY bookfundid";
394 my $sth = $dbh->prepare($query);
395 $sth->execute(@bindings);
397 while (my $row = $sth->fetchrow_hashref) {
403 #-------------------------------------------------------------#
407 &ModCurrencies($currency, $newrate);
409 Sets the exchange rate for C<$currency> to be C<$newrate>.
414 my ( $currency, $rate ) = @_;
415 my $dbh = C4::Context->dbh;
421 my $sth = $dbh->prepare($query);
422 $sth->execute( $rate, $currency );
425 #-------------------------------------------------------------#
429 $number = Countbookfund($bookfundid);
431 this function count the number of bookfund with id given on input arg.
433 the result of the SQL query as a number.
438 my $bookfundid = shift;
439 my $branchcode = shift;
440 my $dbh = C4::Context->dbh;
447 my $sth = $dbh->prepare($query);
448 $sth->execute($bookfundid,$branchcode);
449 return $sth->fetchrow;
453 #-------------------------------------------------------------#
455 =head3 ConvertCurrency
457 $foreignprice = &ConvertCurrency($currency, $localprice);
459 Converts the price C<$localprice> to foreign currency C<$currency> by
460 dividing by the exchange rate, and returns the result.
462 If no exchange rate is found, C<&ConvertCurrency> assumes the rate is one
467 sub ConvertCurrency {
468 my ( $currency, $price ) = @_;
469 my $dbh = C4::Context->dbh;
475 my $sth = $dbh->prepare($query);
476 $sth->execute($currency);
477 my $cur = ( $sth->fetchrow_array() )[0];
481 return ( $price / $cur );
484 #-------------------------------------------------------------#
488 &DelBookFund($bookfundid);
489 this function delete a bookfund which has $bokfundid as parameter on aqbookfund table and delete the approriate budget.
494 my $bookfundid = shift;
495 my $branchcode=shift;
496 my $dbh = C4::Context->dbh;
498 DELETE FROM aqbookfund
502 my $sth=$dbh->prepare($query);
503 $sth->execute($bookfundid,$branchcode);
506 DELETE FROM aqbudget where bookfundid=? and branchcode=?
508 $sth=$dbh->prepare($query);
509 $sth->execute($bookfundid,$branchcode);
513 END { } # module clean-up code here (global destructor)
521 Koha Developement team <info@koha.org>