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