Added issue date column to issues list
[koha.git] / C4 / Bookfund.pm
blob7c00067501fe56916d717de0bc63223749c43aeb
1 package C4::Bookfund;
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
10 # version.
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
21 use strict;
22 # use Smart::Comments;
24 use vars qw($VERSION @ISA @EXPORT);
26 # set the version for version checking
27 $VERSION = 3.00;
29 =head1 NAME
31 C4::Bookfund - Koha functions for dealing with bookfund, currency & money.
33 =head1 SYNOPSIS
35 use C4::Bookfund;
37 =head1 DESCRIPTION
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.
42 =cut
44 @ISA = qw(Exporter);
45 @EXPORT = qw(
46 &GetBookFund &GetBookFunds &GetBookFundsId &GetBookFundBreakdown &GetCurrencies
47 &NewBookFund
48 &ModBookFund &ModCurrencies
49 &SearchBookFund
50 &Countbookfund
51 &ConvertCurrency
52 &DelBookFund
55 =head1 FUNCTIONS
57 =cut
59 #-------------------------------------------------------------#
61 =head2 GetBookFund
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.
67 return:
68 C<$dataaqbookfund> is a hashref full of bookfundid, bookfundname, bookfundgroup,
69 and branchcode.
71 =cut
73 sub GetBookFund {
74 my $bookfundid = shift;
75 my $branchcode = shift;
76 $branchcode=($branchcode?$branchcode:'');
77 my $dbh = C4::Context->dbh;
78 my $query = "
79 SELECT
80 bookfundid,
81 bookfundname,
82 bookfundgroup,
83 branchcode
84 FROM aqbookfund
85 WHERE bookfundid = ?
86 AND branchcode = ?";
87 my $sth=$dbh->prepare($query);
88 $sth->execute($bookfundid,$branchcode);
89 my $data=$sth->fetchrow_hashref;
90 return $data;
94 =head3 GetBookFundsId
96 $sth = &GetBookFundsId
97 Read on aqbookfund table and execute a simple SQL query.
99 return:
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.
105 =cut
107 sub GetBookFundsId {
108 my @bookfundids_loop;
109 my $dbh= C4::Context->dbh;
110 my $query = "
111 SELECT bookfundid,branchcode
112 FROM aqbookfund
114 my $sth = $dbh->prepare($query);
115 $sth->execute;
116 return $sth;
119 #-------------------------------------------------------------#
121 =head3 GetBookFunds
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.
130 =cut
132 sub GetBookFunds {
133 my ($branch) = @_;
134 my $dbh = C4::Context->dbh;
135 my $userenv = C4::Context->userenv;
136 my $strsth;
138 if ( $branch ne '' ) {
139 $strsth = "
140 SELECT *
141 FROM aqbookfund
142 LEFT JOIN aqbudget ON aqbookfund.bookfundid=aqbudget.bookfundid
143 WHERE startdate<now()
144 AND enddate>now()
145 AND (aqbookfund.branchcode='' OR aqbookfund.branchcode= ? )
146 GROUP BY aqbookfund.bookfundid ORDER BY bookfundname";
148 else {
149 $strsth = "
150 SELECT *
151 FROM aqbookfund
152 LEFT JOIN aqbudget ON aqbookfund.bookfundid=aqbudget.bookfundid
153 WHERE startdate<now()
154 AND enddate>now()
155 GROUP BY aqbookfund.bookfundid ORDER BY bookfundname
158 my $sth = $dbh->prepare($strsth);
159 if ( $branch ne '' ) {
160 $sth->execute($branch);
162 else {
163 $sth->execute;
165 my @results = ();
166 while ( my $data = $sth->fetchrow_hashref ) {
167 push( @results, $data );
169 $sth->finish;
170 return @results;
173 #-------------------------------------------------------------#
175 =head3 GetCurrencies
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.
184 =cut
186 sub GetCurrencies {
187 my $dbh = C4::Context->dbh;
188 my $query = "
189 SELECT *
190 FROM currency
192 my $sth = $dbh->prepare($query);
193 $sth->execute;
194 my @results = ();
195 while ( my $data = $sth->fetchrow_hashref ) {
196 push( @results, $data );
198 $sth->finish;
199 return @results;
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
211 =cut
213 sub GetBookFundBreakdown {
214 my ( $id, $start, $end ) = @_;
215 my $dbh = C4::Context->dbh;
217 # if no start/end dates given defaut to everything
218 if ( !$start ) {
219 $start = '0000-00-00';
220 $end = 'now()';
223 # do a query for spent totals.
224 my $query = "
225 SELECT quantity,datereceived,freight,unitprice,listprice,ecost,
226 quantityreceived,subscription
227 FROM aqorders
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 );
238 my ($spent) = 0;
239 while ( my $data = $sth->fetchrow_hashref ) {
240 if ( $data->{'subscription'} == 1 ) {
241 $spent += $data->{'quantity'} * $data->{'unitprice'};
243 else {
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.
252 $query = "
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
259 FROM aqorders
260 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=aqorders.biblioitemnumber
261 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
262 WHERE bookfundid=?
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 );
271 my $comtd=0;
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;
282 $comtd += $subtotal;
284 # use Data::Dumper; warn Dumper($data);
287 $sth->finish;
288 return ( $spent, $comtd );
291 =head3 NewBookFund
293 &NewBookFund(bookfundid, bookfundname, branchcode);
295 this function create a new bookfund into the database.
297 =cut
299 sub NewBookFund{
300 my ($bookfundid, $bookfundname, $branchcode) = @_;
301 $branchcode = undef unless $branchcode;
302 my $dbh = C4::Context->dbh;
303 my $query = "
304 INSERT
305 INTO aqbookfund
306 (bookfundid, bookfundname, branchcode)
307 VALUES
308 (?, ?, ?)
310 my $sth=$dbh->prepare($query);
311 $sth->execute($bookfundid,$bookfundname,"$branchcode");
314 #-------------------------------------------------------------#
316 =head3 ModBookFund
318 &ModBookFund($bookfundname,$bookfundid,$current_branch, $branchcode) =
319 this function update the bookfundname and the branchcode on aqbookfund table on database.
321 =cut
324 sub ModBookFund {
325 my ($bookfundname,$bookfundid,$current_branch, $branchcode) = @_;
327 my $dbh = C4::Context->dbh;
329 my $retval = $dbh->do("
330 UPDATE aqbookfund
331 SET bookfundname = '$bookfundname',
332 branchcode = '$branchcode'
333 WHERE bookfundid = '$bookfundid'
334 AND branchcode = '$current_branch'
337 ### $retval
339 # budgets depending on a bookfund must have the same branchcode
341 # if the bookfund branchcode is set, and previous update is successfull, then update aqbudget.branchcode too.
342 if (defined $branchcode && $retval > 0) {
343 my $query = "UPDATE aqbudget
344 SET branchcode = ?
345 WHERE bookfundid = ? ";
347 my $sth=$dbh->prepare($query);
348 $sth->execute($branchcode, $bookfundid) ;
354 #-------------------------------------------------------------#
356 =head3 SearchBookFund
358 @results = SearchBookFund(
359 $bookfundid,$filter,$filter_bookfundid,
360 $filter_bookfundname,$filter_branchcode);
362 this function searchs among the bookfunds corresponding to our filtering rules.
364 =cut
366 sub SearchBookFund {
367 my $dbh = C4::Context->dbh;
368 my ($filter,
369 $filter_bookfundid,
370 $filter_bookfundname,
371 $filter_branchcode
372 ) = @_;
374 my @bindings;
376 my $query = "
377 SELECT bookfundid,
378 bookfundname,
379 bookfundgroup,
380 branchcode
381 FROM aqbookfund
382 WHERE 1 ";
384 if ($filter) {
385 if ($filter_bookfundid) {
386 $query.= "AND bookfundid = ?";
387 push @bindings, $filter_bookfundid;
389 if ($filter_bookfundname) {
390 $query.= "AND bookfundname like ?";
391 push @bindings, '%'.$filter_bookfundname.'%';
393 if ($filter_branchcode) {
394 $query.= "AND branchcode = ?";
395 push @bindings, $filter_branchcode;
398 $query.= "ORDER BY bookfundid";
400 my $sth = $dbh->prepare($query);
401 $sth->execute(@bindings);
402 my @results;
403 while (my $row = $sth->fetchrow_hashref) {
404 push @results, $row;
406 return @results;
409 #-------------------------------------------------------------#
411 =head3 ModCurrencies
413 &ModCurrencies($currency, $newrate);
415 Sets the exchange rate for C<$currency> to be C<$newrate>.
417 =cut
419 sub ModCurrencies {
420 my ( $currency, $rate ) = @_;
421 my $dbh = C4::Context->dbh;
422 my $query = "
423 UPDATE currency
424 SET rate=?
425 WHERE currency=?
427 my $sth = $dbh->prepare($query);
428 $sth->execute( $rate, $currency );
431 #-------------------------------------------------------------#
433 =head3 Countbookfund
435 $number = Countbookfund($bookfundid);
437 this function count the number of bookfund with id given on input arg.
438 return :
439 the result of the SQL query as a number.
441 =cut
443 sub Countbookfund {
444 my $bookfundid = shift;
445 my $branchcode = shift;
446 my $dbh = C4::Context->dbh;
447 my $query ="
448 SELECT COUNT(*)
449 FROM aqbookfund
450 WHERE bookfundid = ?
451 AND branchcode = ?
453 my $sth = $dbh->prepare($query);
454 $sth->execute($bookfundid,"$branchcode");
455 return $sth->fetchrow;
459 #-------------------------------------------------------------#
461 =head3 ConvertCurrency
463 $foreignprice = &ConvertCurrency($currency, $localprice);
465 Converts the price C<$localprice> to foreign currency C<$currency> by
466 dividing by the exchange rate, and returns the result.
468 If no exchange rate is found, C<&ConvertCurrency> assumes the rate is one
469 to one.
471 =cut
473 sub ConvertCurrency {
474 my ( $currency, $price ) = @_;
475 my $dbh = C4::Context->dbh;
476 my $query = "
477 SELECT rate
478 FROM currency
479 WHERE currency=?
481 my $sth = $dbh->prepare($query);
482 $sth->execute($currency);
483 my $cur = ( $sth->fetchrow_array() )[0];
484 unless($cur) {
485 $cur = 1;
487 return ( $price / $cur );
490 #-------------------------------------------------------------#
492 =head3 DelBookFund
494 &DelBookFund($bookfundid);
495 this function delete a bookfund which has $bokfundid as parameter on aqbookfund table and delete the approriate budget.
497 =cut
499 sub DelBookFund {
500 my $bookfundid = shift;
501 my $branchcode=shift;
502 my $dbh = C4::Context->dbh;
503 my $query = "
504 DELETE FROM aqbookfund
505 WHERE bookfundid=?
506 AND branchcode=?
508 my $sth=$dbh->prepare($query);
509 $sth->execute($bookfundid,$branchcode);
510 $sth->finish;
511 $query = "
512 DELETE FROM aqbudget where bookfundid=? and branchcode=?
514 $sth=$dbh->prepare($query);
515 $sth->execute($bookfundid,$branchcode);
516 $sth->finish;
519 END { } # module clean-up code here (global destructor)
523 __END__
525 =head1 AUTHOR
527 Koha Developement team <info@koha.org>
529 =cut