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