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
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 '' ) {
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 GetBookFundBreakdown
207 ( $spent, $comtd ) = &GetBookFundBreakdown( $id, $start, $end );
209 returns the total comtd & spent for a given bookfund, and a given year
210 used in acqui-home.pl
214 sub GetBookFundBreakdown {
215 my ( $id, $start, $end ) = @_;
216 my $dbh = C4::Context->dbh;
218 # if no start/end dates given defaut to everything
220 $start = '0000-00-00';
224 # do a query for spent totals.
226 SELECT quantity,datereceived,freight,unitprice,listprice,ecost,
227 quantityreceived,subscription
229 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
230 LEFT JOIN aqbookfund ON (aqorderbreakdown.bookfundid=aqbookfund.bookfundid and aqorderbreakdown.branchcode=aqbookfund.branchcode)
231 LEFT JOIN aqbudget ON (aqbudget.bookfundid=aqbookfund.bookfundid and aqbudget.branchcode=aqbookfund.branchcode)
232 WHERE aqorderbreakdown.bookfundid=?
233 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
234 AND ((budgetdate >= ? and budgetdate < ?) OR (startdate>=? and enddate<=?))
236 my $sth = $dbh->prepare($query);
237 $sth->execute( $id, $start, $end, $start, $end );
240 while ( my $data = $sth->fetchrow_hashref ) {
241 if ( $data->{'subscription'} == 1 ) {
242 $spent += $data->{'quantity'} * $data->{'unitprice'};
245 $spent += ( $data->{'unitprice'} ) * ($data->{'quantityreceived'}?$data->{'quantityreceived'}:0);
250 # then do a seperate query for commited totals, (pervious single query was
251 # returning incorrect comitted results.
254 SELECT quantity,datereceived,freight,unitprice,
255 listprice,ecost,quantityreceived AS qrev,
256 subscription,title,itemtype,aqorders.biblionumber,
257 aqorders.booksellerinvoicenumber,
258 quantity-quantityreceived AS tleft,
259 aqorders.ordernumber AS ordnum,entrydate,budgetdate
261 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=aqorders.biblioitemnumber
262 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
264 AND (budgetdate >= ? AND budgetdate < ?)
265 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
268 $sth = $dbh->prepare($query);
269 # warn "$start $end";
270 $sth->execute( $id, $start, $end );
274 while ( my $data = $sth->fetchrow_hashref ) {
275 my $left = $data->{'tleft'};
276 if ( (!$left && (!$data->{'datereceived'}||$data->{'datereceived'} eq '0000-00-00') ) || $left eq '' ) {
277 $left = $data->{'quantity'};
279 if ( $left && $left > 0 ) {
280 my $subtotal = $left * $data->{'ecost'};
281 $data->{subtotal} = $subtotal;
282 $data->{'left'} = $left;
285 # use Data::Dumper; warn Dumper($data);
289 return ( $spent, $comtd );
294 &NewBookFund(bookfundid, bookfundname, branchcode);
296 this function create a new bookfund into the database.
301 my ($bookfundid, $bookfundname, $branchcode) = @_;
302 $branchcode = undef unless $branchcode;
303 my $dbh = C4::Context->dbh;
307 (bookfundid, bookfundname, branchcode)
311 my $sth=$dbh->prepare($query);
312 $sth->execute($bookfundid,$bookfundname,"$branchcode");
315 #-------------------------------------------------------------#
319 &ModBookFund($bookfundname,$branchcode,$bookfundid);
320 this function update the bookfundname and the branchcode on aqbookfund table
327 my ($bookfundname,$bookfundid,$current_branch, $branchcode) = @_;
329 my $dbh = C4::Context->dbh;
331 my $retval = $dbh->do("
333 SET bookfundname = '$bookfundname',
334 branchcode = '$branchcode'
335 WHERE bookfundid = '$bookfundid'
336 AND branchcode = '$current_branch'
341 # budgets depending on a bookfund must have the same branchcode
342 # if the bookfund branchcode is set
343 if (defined $branchcode && $retval > 0) {
344 my $query = "UPDATE aqbudget
346 WHERE bookfundid = ? ";
348 my $sth=$dbh->prepare($query);
349 $sth->execute($branchcode, $bookfundid) ;
355 #-------------------------------------------------------------#
357 =head3 SearchBookFund
359 @results = SearchBookFund(
360 $bookfundid,$filter,$filter_bookfundid,
361 $filter_bookfundname,$filter_branchcode);
363 this function searchs among the bookfunds corresponding to our filtering rules.
368 my $dbh = C4::Context->dbh;
371 $filter_bookfundname,
386 if ($filter_bookfundid) {
387 $query.= "AND bookfundid = ?";
388 push @bindings, $filter_bookfundid;
390 if ($filter_bookfundname) {
391 $query.= "AND bookfundname like ?";
392 push @bindings, '%'.$filter_bookfundname.'%';
394 if ($filter_branchcode) {
395 $query.= "AND branchcode = ?";
396 push @bindings, $filter_branchcode;
399 $query.= "ORDER BY bookfundid";
401 my $sth = $dbh->prepare($query);
402 $sth->execute(@bindings);
404 while (my $row = $sth->fetchrow_hashref) {
410 #-------------------------------------------------------------#
414 &ModCurrencies($currency, $newrate);
416 Sets the exchange rate for C<$currency> to be C<$newrate>.
421 my ( $currency, $rate ) = @_;
422 my $dbh = C4::Context->dbh;
428 my $sth = $dbh->prepare($query);
429 $sth->execute( $rate, $currency );
432 #-------------------------------------------------------------#
436 $number = Countbookfund($bookfundid);
438 this function count the number of bookfund with id given on input arg.
440 the result of the SQL query as a number.
445 my $bookfundid = shift;
446 my $branchcode = shift;
447 my $dbh = C4::Context->dbh;
454 my $sth = $dbh->prepare($query);
455 $sth->execute($bookfundid,"$branchcode");
456 return $sth->fetchrow;
460 #-------------------------------------------------------------#
462 =head3 ConvertCurrency
464 $foreignprice = &ConvertCurrency($currency, $localprice);
466 Converts the price C<$localprice> to foreign currency C<$currency> by
467 dividing by the exchange rate, and returns the result.
469 If no exchange rate is found, C<&ConvertCurrency> assumes the rate is one
474 sub ConvertCurrency {
475 my ( $currency, $price ) = @_;
476 my $dbh = C4::Context->dbh;
482 my $sth = $dbh->prepare($query);
483 $sth->execute($currency);
484 my $cur = ( $sth->fetchrow_array() )[0];
488 return ( $price / $cur );
491 #-------------------------------------------------------------#
495 &DelBookFund($bookfundid);
496 this function delete a bookfund which has $bokfundid as parameter on aqbookfund table and delete the approriate budget.
501 my $bookfundid = shift;
502 my $branchcode=shift;
503 my $dbh = C4::Context->dbh;
505 DELETE FROM aqbookfund
509 my $sth=$dbh->prepare($query);
510 $sth->execute($bookfundid,$branchcode);
513 DELETE FROM aqbudget where bookfundid=? and branchcode=?
515 $sth=$dbh->prepare($query);
516 $sth->execute($bookfundid,$branchcode);
520 END { } # module clean-up code here (global destructor)
528 Koha Developement team <info@koha.org>