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 '' ) {
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=aqbudget.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
326 my ($bookfundname,$bookfundid,$branchcode) = @_;
327 my $dbh = C4::Context->dbh;
334 warn "name : $bookfundname";
335 my $sth=$dbh->prepare($query);
336 $sth->execute($bookfundname,$bookfundid,"$branchcode");
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
353 @results = SearchBookFund(
354 $bookfundid,$filter,$filter_bookfundid,
355 $filter_bookfundname,$filter_branchcode);
357 this function searchs among the bookfunds corresponding to our filtering rules.
362 my $dbh = C4::Context->dbh;
365 $filter_bookfundname,
380 if ($filter_bookfundid) {
381 $query.= "AND bookfundid = ?";
382 push @bindings, $filter_bookfundid;
384 if ($filter_bookfundname) {
385 $query.= "AND bookfundname like ?";
386 push @bindings, '%'.$filter_bookfundname.'%';
388 if ($filter_branchcode) {
389 $query.= "AND branchcode = ?";
390 push @bindings, $filter_branchcode;
393 $query.= "ORDER BY bookfundid";
395 my $sth = $dbh->prepare($query);
396 $sth->execute(@bindings);
398 while (my $row = $sth->fetchrow_hashref) {
404 #-------------------------------------------------------------#
408 &ModCurrencies($currency, $newrate);
410 Sets the exchange rate for C<$currency> to be C<$newrate>.
415 my ( $currency, $rate ) = @_;
416 my $dbh = C4::Context->dbh;
422 my $sth = $dbh->prepare($query);
423 $sth->execute( $rate, $currency );
426 #-------------------------------------------------------------#
430 $number = Countbookfund($bookfundid);
432 this function count the number of bookfund with id given on input arg.
434 the result of the SQL query as a number.
439 my $bookfundid = shift;
440 my $branchcode = shift;
441 my $dbh = C4::Context->dbh;
448 my $sth = $dbh->prepare($query);
449 $sth->execute($bookfundid,$branchcode);
450 return $sth->fetchrow;
454 #-------------------------------------------------------------#
456 =head3 ConvertCurrency
458 $foreignprice = &ConvertCurrency($currency, $localprice);
460 Converts the price C<$localprice> to foreign currency C<$currency> by
461 dividing by the exchange rate, and returns the result.
463 If no exchange rate is found, C<&ConvertCurrency> assumes the rate is one
468 sub ConvertCurrency {
469 my ( $currency, $price ) = @_;
470 my $dbh = C4::Context->dbh;
476 my $sth = $dbh->prepare($query);
477 $sth->execute($currency);
478 my $cur = ( $sth->fetchrow_array() )[0];
482 return ( $price / $cur );
485 #-------------------------------------------------------------#
489 &DelBookFund($bookfundid);
490 this function delete a bookfund which has $bokfundid as parameter on aqbookfund table and delete the approriate budget.
495 my $bookfundid = shift;
496 my $branchcode=shift;
497 my $dbh = C4::Context->dbh;
499 DELETE FROM aqbookfund
503 my $sth=$dbh->prepare($query);
504 $sth->execute($bookfundid,$branchcode);
507 DELETE FROM aqbudget where bookfundid=? and branchcode=?
509 $sth=$dbh->prepare($query);
510 $sth->execute($bookfundid,$branchcode);
514 END { } # module clean-up code here (global destructor)
522 Koha Developement team <info@koha.org>