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