New function "GetBookFundsId" : this function get the list of bookfund id into an...
[koha.git] / C4 / Bookfund.pm
1 package C4::Bookfund;
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
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
19
20 # $Id$
21
22 use strict;
23
24
25 use vars qw($VERSION @ISA @EXPORT);
26
27 # set the version for version checking
28 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v ); };
29
30 =head1 NAME
31
32 C4::Bookfund - Koha functions for dealing with bookfund, currency & money.
33
34 =head1 SYNOPSIS
35
36 use C4::Bookfund;
37
38 =head1 DESCRIPTION
39
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.
42
43 =cut
44
45 @ISA    = qw(Exporter);
46 @EXPORT = qw(
47     &GetBookFund &GetBookFunds &GetBookFundsId &GetBookFundBreakdown &GetCurrencies
48     &NewBookFund
49     &ModBookFund &ModCurrencies
50     &SearchBookFund
51     &Countbookfund 
52     &ConvertCurrency
53     &DelBookFund
54 );
55
56 =head1 FUNCTIONS
57
58 =over 2
59
60 =cut
61
62 #-------------------------------------------------------------#
63
64 =head3 GetBookFund
65
66 =over 4
67
68 $dataaqbookfund = &GetBookFund($bookfundid);
69
70 this function get the bookfundid, bookfundname, the bookfundgroup,  the branchcode
71 from aqbookfund table for bookfundid given on input arg.
72 return: 
73 C<$dataaqbookfund> is a hashref full of bookfundid, bookfundname, bookfundgroup,
74 and branchcode.
75
76 =back
77
78 =cut
79
80 sub GetBookFund {
81     my $bookfundid = @_;
82     my $dbh = C4::Context->dbh;
83     my $query = "
84         SELECT
85             bookfundid,
86             bookfundname,
87             bookfundgroup,
88             branchcode
89         FROM aqbookfund
90         WHERE bookfundid = ?
91     ";
92     my $sth=$dbh->prepare($query);
93     return $sth->fetchrow_hashref;
94 }
95
96
97 =head3 GetBookFundsId
98
99 =over 4
100
101 $sth = &GetBookFundsId
102 Read on aqbookfund table and execute a simple SQL query.
103
104 return:
105 $sth->execute. Don't forget to fetch row from the database after using
106 this function by using, for example, $sth->fetchrow_hashref;
107
108 C<@results> is an array of id existing on the database.
109
110 =back
111
112 =cut
113
114 sub GetBookFundsId {
115     my @bookfundids_loop;
116     my $dbh= C4::Context->dbh;
117     my $query = "
118         SELECT bookfundid
119         FROM aqbookfund
120     ";
121     my $sth = $dbh->prepare($query);
122     $sth->execute;
123     return $sth;
124 }
125
126 #-------------------------------------------------------------#
127
128 =head3 GetBookFunds
129
130 =over 4
131
132 @results = &GetBookFunds;
133
134 Returns a list of all book funds.
135
136 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
137 alphabetically by book fund name.
138
139 =back
140
141 =cut
142
143 sub GetBookFunds {
144     my ($branch) = @_;
145     my $dbh      = C4::Context->dbh;
146     my $userenv  = C4::Context->userenv;
147     my $branch   = $userenv->{branch};
148     my $strsth;
149
150     if ( $branch ne '' ) {
151         $strsth = "
152         SELECT *
153         FROM   aqbookfund,aqbudget
154         WHERE  aqbookfund.bookfundid=aqbudget.bookfundid
155             AND startdate<now()
156             AND enddate>now()
157             AND (aqbookfund.branchcode IS NULL OR aqbookfund.branchcode='' OR aqbookfund.branchcode= ? )
158       GROUP BY aqbookfund.bookfundid ORDER BY bookfundname";
159     }
160     else {
161         $strsth = "
162             SELECT *
163             FROM   aqbookfund,
164                    aqbudget
165             WHERE aqbookfund.bookfundid=aqbudget.bookfundid
166                 AND startdate<now()
167                 AND enddate>now()
168             GROUP BY aqbookfund.bookfundid ORDER BY bookfundname
169         ";
170     }
171     my $sth = $dbh->prepare($strsth);
172     if ( $branch ne '' ) {
173         $sth->execute($branch);
174     }
175     else {
176         $sth->execute;
177     }
178     my @results = ();
179     while ( my $data = $sth->fetchrow_hashref ) {
180         push( @results, $data );
181     }
182     $sth->finish;
183     return @results;
184 }
185
186 #-------------------------------------------------------------#
187
188 =head3 GetCurrencies
189
190 =over 4
191
192 @currencies = &GetCurrencies;
193
194 Returns the list of all known currencies.
195
196 C<$currencies> is a array; its elements are references-to-hash, whose
197 keys are the fields from the currency table in the Koha database.
198
199 =back
200
201 =cut
202
203 sub GetCurrencies {
204     my $dbh = C4::Context->dbh;
205     my $query = "
206         SELECT *
207         FROM   currency
208     ";
209     my $sth = $dbh->prepare($query);
210     $sth->execute;
211     my @results = ();
212     while ( my $data = $sth->fetchrow_hashref ) {
213         push( @results, $data );
214     }
215     $sth->finish;
216     return @results;
217 }
218
219 #-------------------------------------------------------------#
220
221 =head3 GetBookFundBreakdown
222
223 =over 4
224
225 ( $spent, $comtd ) = &GetBookFundBreakdown( $id, $year, $start, $end );
226
227 returns the total comtd & spent for a given bookfund, and a given year
228 used in acqui-home.pl
229
230 =back
231
232 =cut
233
234 sub GetBookFundBreakdown {
235     my ( $id, $year, $start, $end ) = @_;
236     my $dbh = C4::Context->dbh;
237
238     # if no start/end dates given defaut to everything
239     if ( !$start ) {
240         $start = '0000-00-00';
241         $end   = 'now()';
242     }
243
244     # do a query for spent totals.
245     my $query = "
246         SELECT quantity,datereceived,freight,unitprice,listprice,ecost,
247                quantityreceived,subscription
248         FROM   aqorders
249         LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
250         WHERE  bookfundid=?
251             AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
252             AND ((datereceived >= ? and datereceived < ?) OR (budgetdate >= ? and budgetdate < ?))
253     ";
254     my $sth = $dbh->prepare($query);
255     $sth->execute( $id, $start, $end, $start, $end );
256
257     my $spent = 0;
258     while ( my $data = $sth->fetchrow_hashref ) {
259         if ( $data->{'subscription'} == 1 ) {
260             $spent += $data->{'quantity'} * $data->{'unitprice'};
261         }
262         else {
263
264             my $leftover = $data->{'quantity'} - $data->{'quantityreceived'};
265             $spent += ( $data->{'unitprice'} ) * $data->{'quantityreceived'};
266
267         }
268     }
269
270     # then do a seperate query for commited totals, (pervious single query was
271     # returning incorrect comitted results.
272
273     my $query = "
274         SELECT  quantity,datereceived,freight,unitprice,
275                 listprice,ecost,quantityreceived AS qrev,
276                 subscription,title,itemtype,aqorders.biblionumber,
277                 aqorders.booksellerinvoicenumber,
278                 quantity-quantityreceived AS tleft,
279                 aqorders.ordernumber AS ordnum,entrydate,budgetdate,
280                 booksellerid,aqbasket.basketno
281         FROM    aqorderbreakdown,
282                 aqbasket,
283                 aqorders
284         LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=aqorders.biblioitemnumber
285         WHERE   bookfundid=?
286             AND aqorders.ordernumber=aqorderbreakdown.ordernumber
287             AND aqorders.basketno=aqbasket.basketno
288             AND (budgetdate >= ? AND budgetdate < ?)
289             AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
290     ";
291
292     my $sth = $dbh->prepare($query);
293     $sth->execute( $id, $start, $end );
294
295     my $comtd;
296
297     my $total = 0;
298     while ( my $data = $sth->fetchrow_hashref ) {
299         my $left = $data->{'tleft'};
300         if ( !$left || $left eq '' ) {
301             $left = $data->{'quantity'};
302         }
303         if ( $left && $left > 0 ) {
304             my $subtotal = $left * $data->{'ecost'};
305             $data->{subtotal} = $subtotal;
306             $data->{'left'} = $left;
307             $comtd += $subtotal;
308         }
309     }
310
311     $sth->finish;
312     return ( $spent, $comtd );
313 }
314
315 =head3 NewBookFund
316
317 =over 4
318
319 &NewBookFund(bookfundid, bookfundname, branchcode);
320
321 this function create a new bookfund into the database.
322
323 =back
324
325 =cut 
326
327 sub NewBookFund{
328     my ($bookfundid, $bookfundname, $branchcode) = @_;
329     $branchcode = undef unless $branchcode;
330     my $dbh = C4::Context->dbh;
331     my $query = "
332         INSERT
333         INTO aqbookfund
334             (bookfundid, bookfundname, branchcode)
335         VALUES
336             (?, ?, ?)
337     ";
338     my $sth=$dbh->prepare($query);
339     $sth->execute($bookfundid,$bookfundname,$branchcode);
340 }
341
342 #-------------------------------------------------------------#
343
344 =head3 ModBookFund
345
346 =over 4
347
348 &ModBookFund($bookfundname,$branchcode,$bookfundid);
349 this function update the bookfundname and the branchcode on aqbookfund table
350 on database.
351
352 =back
353
354 =cut
355
356 sub ModBookFund {
357     my ($bookfundname,$branchcode,$bookfundid) = @_;
358     my $dbh = C4::Context->dbh;
359     my $query = "
360         UPDATE aqbookfund
361         SET    bookfundname = ?,
362                branchcode = ?
363         WHERE  bookfundid = ?
364     ";
365     my $sth=$dbh->prepare($query);
366     $sth->execute($bookfundname,$branchcode,$bookfundid);
367 # budgets depending on a bookfund must have the same branchcode
368 # if the bookfund branchcode is set
369     if (defined $branchcode) {
370         $query = "
371             UPDATE aqbudget
372             SET branchcode = ?
373         ";
374         $sth=$dbh->prepare($query);
375         $sth->execute($branchcode);
376     }
377 }
378
379 #-------------------------------------------------------------#
380
381 =head3 SearchBookFund
382
383 =over 4
384 @results = SearchBookFund(
385         $bookfundid,$filter,$filter_bookfundid,
386         $filter_bookfundname,$filter_branchcode);
387
388 this function searchs among the bookfunds corresponding to our filtering rules.
389
390 =back
391
392 =cut
393
394 sub SearchBookFund {
395     my $dbh = C4::Context->dbh;
396     my ($filter,
397         $filter_bookfundid,
398         $filter_bookfundname,
399         $filter_branchcode
400        ) = @_;
401
402     my @bindings;
403
404     my $query = "
405         SELECT  bookfundid,
406                 bookfundname,
407                 bookfundgroup,
408                 branchcode
409         FROM aqbookfund
410         WHERE 1 = 1 ";
411
412     if ($filter) {
413         if ($filter_bookfundid) {
414             $query.= "AND bookfundid = ?";
415             push @bindings, $filter_bookfundid;
416         }
417         if ($filter_bookfundname) {
418             $query.= "AND bookfundname like ?";
419             push @bindings, '%'.$filter_bookfundname.'%';
420         }
421         if ($filter_branchcode) {
422             $query.= "AND branchcode = ?";
423             push @bindings, $filter_branchcode;
424         }
425     }
426     $query.= "ORDER BY bookfundid";
427
428     my $sth = $dbh->prepare($query);
429     $sth->execute(@bindings);
430     my @results;
431     while (my $row = $sth->fetchrow_hashref) {
432         push @results, $row;
433     }
434     return @results;
435 }
436
437 #-------------------------------------------------------------#
438
439 =head3 ModCurrencies
440
441 =over 4
442
443 &ModCurrencies($currency, $newrate);
444
445 Sets the exchange rate for C<$currency> to be C<$newrate>.
446
447 =back
448
449 =cut
450
451 sub ModCurrencies {
452     my ( $currency, $rate ) = @_;
453     my $dbh = C4::Context->dbh;
454     my $query = "
455         UPDATE currency
456         SET    rate=?
457         WHERE  currency=?
458     ";
459     my $sth = $dbh->prepare($query);
460     $sth->execute( $rate, $currency );
461 }
462
463 #-------------------------------------------------------------#
464
465 =head3 Countbookfund
466
467 =over 4
468
469 $number = Countbookfund($bookfundid);
470
471 this function count the number of bookfund with id given on input arg.
472 return :
473 the result of the SQL query as a number.
474
475 =back
476
477 =cut
478
479 sub Countbookfund {
480     my $bookfundid = shift;
481     my $dbh = C4::Context->dbh;
482     my $query ="
483         SELECT COUNT(*)
484         FROM   aqbookfund
485         WHERE bookfundid = ?
486     ";
487     my $sth = $dbh->prepare($query);
488     $sth->execute($bookfundid);
489     return $sth->fetchrow;
490 }
491
492
493 #-------------------------------------------------------------#
494
495 =head3 ConvertCurrency
496
497 =over 4
498
499 $foreignprice = &ConvertCurrency($currency, $localprice);
500
501 Converts the price C<$localprice> to foreign currency C<$currency> by
502 dividing by the exchange rate, and returns the result.
503
504 If no exchange rate is found, C<&ConvertCurrency> assumes the rate is one
505 to one.
506
507 =back
508
509 =cut
510
511 sub ConvertCurrency {
512     my ( $currency, $price ) = @_;
513     my $dbh = C4::Context->dbh;
514     my $query = "
515         SELECT rate
516         FROM   currency
517         WHERE  currency=?
518     ";
519     my $sth = $dbh->prepare($query);
520     $sth->execute($currency);
521     my $cur = ( $sth->fetchrow_array() )[0];
522     if ( $cur == 0 ) {
523         $cur = 1;
524     }
525     return ( $price / $cur );
526 }
527
528 #-------------------------------------------------------------#
529
530 =head3 DelBookFund
531
532 =over 4
533
534 &DelBookFund($bookfundid);
535 this function delete a bookfund which has $bokfundid as parameter on aqbookfund table and delete the approriate budget.
536
537 =back
538
539 =cut
540
541 sub DelBookFund {
542     my $bookfundid = shift;
543     my $dbh = C4::Context->dbh;
544     my $query = "
545         DELETE FROM aqbookfund
546         WHERE bookfundid=?
547     ";
548     my $sth=$dbh->prepare($query);
549     $sth->execute($bookfundid);
550     $sth->finish;
551     $query = "
552         DELETE FROM aqbudget where bookfundid=?
553     ";
554     $sth=$dbh->prepare($query);
555     $sth->execute($bookfundid);
556     $sth->finish;
557 }
558
559 END { }    # module clean-up code here (global destructor)
560
561 1;
562
563 __END__
564
565 =back
566
567 =head1 AUTHOR
568
569 Koha Developement team <info@koha.org>
570
571 =cut