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