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
22 # use Smart::Comments;
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 );
172 #-------------------------------------------------------------#
176 @currencies = &GetCurrencies;
178 Returns the list of all known currencies.
180 C<$currencies> is a array; its elements are references-to-hash, whose
181 keys are the fields from the currency table in the Koha database.
186 my $dbh = C4::Context->dbh;
187 my $results = $dbh->selectall_arrayref(
188 'SELECT * FROM currency', { Slice => {} } );
192 #-------------------------------------------------------------#
194 =head3 GetBookFundBreakdown
196 ( $spent, $comtd ) = &GetBookFundBreakdown( $id, $start, $end );
198 returns the total comtd & spent for a given bookfund, and a given year
199 used in acqui-home.pl
203 sub GetBookFundBreakdown {
204 my ( $id, $start, $end ) = @_;
205 my $dbh = C4::Context->dbh;
207 # if no start/end dates given defaut to everything
209 $start = '0000-00-00';
213 # do a query for spent totals.
215 SELECT quantity,datereceived,freight,unitprice,listprice,ecost,
216 quantityreceived,subscription
218 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
219 LEFT JOIN aqbookfund ON (aqorderbreakdown.bookfundid=aqbookfund.bookfundid and aqorderbreakdown.branchcode=aqbookfund.branchcode)
220 LEFT JOIN aqbudget ON (aqbudget.bookfundid=aqbookfund.bookfundid and aqbudget.branchcode=aqbookfund.branchcode)
221 WHERE aqorderbreakdown.bookfundid=?
222 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
223 AND ((budgetdate >= ? and budgetdate < ?) OR (startdate>=? and enddate<=?))
225 my $sth = $dbh->prepare($query);
226 $sth->execute( $id, $start, $end, $start, $end );
229 while ( my $data = $sth->fetchrow_hashref ) {
230 if ( $data->{'subscription'} == 1 ) {
231 $spent += $data->{'quantity'} * $data->{'unitprice'};
234 $spent += ( $data->{'unitprice'} ) * ($data->{'quantityreceived'}?$data->{'quantityreceived'}:0);
239 # then do a seperate query for commited totals, (pervious single query was
240 # returning incorrect comitted results.
243 SELECT quantity,datereceived,freight,unitprice,
244 listprice,ecost,quantityreceived AS qrev,
245 subscription,title,itemtype,aqorders.biblionumber,
246 aqorders.booksellerinvoicenumber,
247 quantity-quantityreceived AS tleft,
248 aqorders.ordernumber AS ordnum,entrydate,budgetdate
250 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=aqorders.biblioitemnumber
251 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
253 AND (budgetdate >= ? AND budgetdate < ?)
254 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
257 $sth = $dbh->prepare($query);
258 # warn "$start $end";
259 $sth->execute( $id, $start, $end );
263 while ( my $data = $sth->fetchrow_hashref ) {
264 my $left = $data->{'tleft'};
265 if ( !$left || $left eq '' ) {
266 $left = $data->{'quantity'};
268 if ( $left && $left > 0 ) {
269 my $subtotal = $left * $data->{'ecost'};
270 $data->{subtotal} = $subtotal;
271 $data->{'left'} = $left;
274 # use Data::Dumper; warn Dumper($data);
277 return ( $spent, $comtd );
282 &NewBookFund(bookfundid, bookfundname, branchcode);
284 this function create a new bookfund into the database.
289 my ($bookfundid, $bookfundname, $branchcode) = @_;
290 $branchcode = undef unless $branchcode;
291 my $dbh = C4::Context->dbh;
295 (bookfundid, bookfundname, branchcode)
299 my $sth=$dbh->prepare($query);
300 $sth->execute($bookfundid,$bookfundname,"$branchcode");
303 #-------------------------------------------------------------#
307 &ModBookFund($bookfundname,$bookfundid,$current_branch, $branchcode)
309 This function updates the bookfundname and the branchcode in the aqbookfund table.
313 # FIXME: use placeholders, ->prepare(), ->execute()
316 my ($bookfundname,$bookfundid,$current_branch, $branchcode) = @_;
318 my $dbh = C4::Context->dbh;
320 my $retval = $dbh->do("
322 SET bookfundname = '$bookfundname',
323 branchcode = '$branchcode'
324 WHERE bookfundid = '$bookfundid'
325 AND branchcode = '$current_branch'
330 # budgets depending on a bookfund must have the same branchcode
332 # if the bookfund branchcode is set, and previous update is successfull, then update aqbudget.branchcode too.
333 if (defined $branchcode && $retval > 0) {
334 my $query = "UPDATE aqbudget
336 WHERE bookfundid = ? ";
338 my $sth=$dbh->prepare($query);
339 $sth->execute($branchcode, $bookfundid) ;
343 #-------------------------------------------------------------#
345 =head3 SearchBookFund
347 @results = SearchBookFund(
348 $bookfundid,$filter,$filter_bookfundid,
349 $filter_bookfundname,$filter_branchcode);
351 this function searchs among the bookfunds corresponding to our filtering rules.
356 my $dbh = C4::Context->dbh;
359 $filter_bookfundname,
374 if ($filter_bookfundid) {
375 $query.= "AND bookfundid = ?";
376 push @bindings, $filter_bookfundid;
378 if ($filter_bookfundname) {
379 $query.= "AND bookfundname like ?";
380 push @bindings, '%'.$filter_bookfundname.'%';
382 if ($filter_branchcode) {
383 $query.= "AND branchcode = ?";
384 push @bindings, $filter_branchcode;
387 $query.= "ORDER BY bookfundid";
389 my $sth = $dbh->prepare($query);
390 $sth->execute(@bindings);
392 while (my $row = $sth->fetchrow_hashref) {
398 #-------------------------------------------------------------#
402 &ModCurrencies($currency, $newrate);
404 Sets the exchange rate for C<$currency> to be C<$newrate>.
409 my ( $currency, $rate ) = @_;
410 my $dbh = C4::Context->dbh;
411 $dbh->do('UPDATE currency SET rate=? WHERE currency=?', {}, $rate, $currency );
415 #-------------------------------------------------------------#
419 $number = Countbookfund($bookfundid);
421 this function count the number of bookfund with id given on input arg.
423 the result of the SQL query as a number.
428 my $bookfundid = shift;
429 my $branchcode = shift;
430 my $dbh = C4::Context->dbh;
437 my $sth = $dbh->prepare($query);
438 $sth->execute($bookfundid,"$branchcode");
439 return $sth->fetchrow;
443 #-------------------------------------------------------------#
445 =head3 ConvertCurrency
447 $foreignprice = &ConvertCurrency($currency, $localprice);
449 Converts the price C<$localprice> to foreign currency C<$currency> by
450 dividing by the exchange rate, and returns the result.
452 If no exchange rate is found, C<&ConvertCurrency> assumes the rate is one
457 sub ConvertCurrency {
458 my ( $currency, $price ) = @_;
459 my $dbh = C4::Context->dbh;
465 my $sth = $dbh->prepare($query);
466 $sth->execute($currency);
467 my $cur = ( $sth->fetchrow_array() )[0];
471 return ( $price / $cur );
474 #-------------------------------------------------------------#
478 &DelBookFund($bookfundid);
479 this function delete a bookfund which has $bokfundid as parameter on aqbookfund table and delete the approriate budget.
484 my $bookfundid = shift;
485 my $branchcode=shift;
486 my $dbh = C4::Context->dbh;
487 $dbh->do('DELETE FROM aqbookfund WHERE bookfundid=? AND branchcode=?', {}, $bookfundid,$branchcode);
488 $dbh->do('DELETE FROM aqbudget where bookfundid=? and branchcode=?', {}, $bookfundid,$branchcode);
492 END { } # module clean-up code here (global destructor)
500 Koha Developement team <info@koha.org>