[followup](bug #3348) fix spent values and spent resume
[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     
218     # if no start/end dates given defaut to everything
219     if ( !$start ) {
220         $start = '0000-00-00';
221         $end   = 'now()';
222     }
223
224     # do a query for spent totals.
225     my $query = "
226     SELECT quantity,datereceived,freight,unitprice,listprice,ecost,
227         quantityreceived AS qrev,subscription,title,aqorders.biblionumber,
228         aqorders.booksellerinvoicenumber,quantity-quantityreceived as tleft,
229         aqorders.ordernumber as ordnum,entrydate,budgetdate,aqbasket.booksellerid,
230         aqbasket.basketno
231     FROM aqorders
232         LEFT JOIN aqorderbreakdown USING (ordernumber)
233         LEFT JOIN aqbasket USING (basketno)
234         LEFT JOIN aqbudget USING (bookfundid)
235     WHERE aqbudgetid=?
236         AND (datecancellationprinted IS NULL OR datecancellationprinted = '0000-00-00')
237         AND closedate BETWEEN startdate AND enddate 
238         AND creationdate > startdate
239         
240     ORDER BY datereceived
241     ";
242     my $sth = $dbh->prepare($query);
243     $sth->execute($id);
244
245     my ($spent, $comtd) = (0, 0);
246     while ( my $data = $sth->fetchrow_hashref ) {
247         
248         my $recv  = $data->{'qrev'};
249         my $left = $data->{'tleft'};
250         my $ecost = $data->{'ecost'};
251         
252         
253         if($data->{datereceived}){
254             if ( $recv > 0 ) {
255                 $spent += $recv * $data->{'unitprice'};
256             }
257         }
258         $left = $data->{quantity} if(not $recv);
259
260         $comtd += $left * $ecost;
261
262     }
263
264     $sth->finish;
265     return ( $spent, $comtd );
266 }
267
268 =head3 NewBookFund
269
270 &NewBookFund(bookfundid, bookfundname, branchcode);
271
272 this function create a new bookfund into the database.
273
274 =cut 
275
276 sub NewBookFund{
277     my ($bookfundid, $bookfundname, $branchcode) = @_;
278     $branchcode = undef unless $branchcode;
279     my $dbh = C4::Context->dbh;
280     my $query = "
281         INSERT
282         INTO aqbookfund
283             (bookfundid, bookfundname, branchcode)
284         VALUES
285             (?, ?, ?)
286     ";
287     my $sth=$dbh->prepare($query);
288     $sth->execute($bookfundid,$bookfundname,"$branchcode");
289 }
290
291 #-------------------------------------------------------------#
292
293 =head3 ModBookFund
294
295 &ModBookFund($bookfundname,$bookfundid,$current_branch, $branchcode)
296
297 This function updates the bookfundname and the branchcode in the aqbookfund table.
298
299 =cut
300
301 # FIXME: use placeholders,  ->prepare(), ->execute()
302
303 sub ModBookFund {
304     my ($bookfundname,$bookfundid,$current_branch, $branchcode) = @_;
305
306     my $dbh = C4::Context->dbh;
307
308     my $retval = $dbh->do("
309      UPDATE aqbookfund
310         SET    bookfundname = '$bookfundname', 
311                branchcode = '$branchcode'
312         WHERE  bookfundid = '$bookfundid'
313         AND branchcode = '$current_branch'
314     ");
315
316     ### $retval
317
318     # budgets depending on a bookfund must have the same branchcode
319
320     # if the bookfund branchcode is set, and previous update is successfull, then update aqbudget.branchcode too.
321     if (defined $branchcode && $retval > 0) {
322         my $query = "UPDATE  aqbudget  
323             SET     branchcode = ?
324             WHERE   bookfundid = ? ";
325
326         my $sth=$dbh->prepare($query);
327         $sth->execute($branchcode, $bookfundid) ;
328     }
329 }
330
331 #-------------------------------------------------------------#
332
333 =head3 SearchBookFund
334
335 @results = SearchBookFund(
336         $bookfundid,$filter,$filter_bookfundid,
337         $filter_bookfundname,$filter_branchcode);
338
339 this function searchs among the bookfunds corresponding to our filtering rules.
340
341 =cut
342
343 sub SearchBookFund {
344     my $dbh = C4::Context->dbh;
345     my ($filter,
346         $filter_bookfundid,
347         $filter_bookfundname,
348         $filter_branchcode
349        ) = @_;
350
351     my @bindings;
352
353     my $query = "
354         SELECT  bookfundid,
355                 bookfundname,
356                 bookfundgroup,
357                 branchcode
358         FROM aqbookfund
359         WHERE 1 ";
360
361     if ($filter) {
362         if ($filter_bookfundid) {
363             $query.= "AND bookfundid = ?";
364             push @bindings, $filter_bookfundid;
365         }
366         if ($filter_bookfundname) {
367             $query.= "AND bookfundname like ?";
368             push @bindings, '%'.$filter_bookfundname.'%';
369         }
370         if ($filter_branchcode) {
371             $query.= "AND branchcode = ?";
372             push @bindings, $filter_branchcode;
373         }
374     }
375     $query.= "ORDER BY bookfundid";
376
377     my $sth = $dbh->prepare($query);
378     $sth->execute(@bindings);
379     my @results;
380     while (my $row = $sth->fetchrow_hashref) {
381         push @results, $row;
382     }
383     return @results;
384 }
385
386 #-------------------------------------------------------------#
387
388 =head3 ModCurrencies
389
390 &ModCurrencies($currency, $newrate);
391
392 Sets the exchange rate for C<$currency> to be C<$newrate>.
393
394 =cut
395
396 sub ModCurrencies {
397     my ( $currency, $rate ) = @_;
398     my $dbh = C4::Context->dbh;
399     my $query = "
400         UPDATE currency
401         SET    rate=?
402         WHERE  currency=?
403     ";
404     my $sth = $dbh->prepare($query);
405     $sth->execute( $rate, $currency );
406 }
407
408 #-------------------------------------------------------------#
409
410 =head3 Countbookfund
411
412 $number = Countbookfund($bookfundid);
413
414 this function count the number of bookfund with id given on input arg.
415 return :
416 the result of the SQL query as a number.
417
418 =cut
419
420 sub Countbookfund {
421     my $bookfundid = shift;
422     my $branchcode = shift;
423     my $dbh = C4::Context->dbh;
424     my $query ="
425         SELECT COUNT(*)
426         FROM  aqbookfund
427         WHERE bookfundid = ?
428         AND   branchcode = ?
429     ";
430     my $sth = $dbh->prepare($query);
431     $sth->execute($bookfundid,"$branchcode");
432     return $sth->fetchrow;
433 }
434
435
436 #-------------------------------------------------------------#
437
438 =head3 ConvertCurrency
439
440 $foreignprice = &ConvertCurrency($currency, $localprice);
441
442 Converts the price C<$localprice> to foreign currency C<$currency> by
443 dividing by the exchange rate, and returns the result.
444
445 If no exchange rate is found, C<&ConvertCurrency> assumes the rate is one
446 to one.
447
448 =cut
449
450 sub ConvertCurrency {
451     my ( $currency, $price ) = @_;
452     my $dbh = C4::Context->dbh;
453     my $query = "
454         SELECT rate
455         FROM   currency
456         WHERE  currency=?
457     ";
458     my $sth = $dbh->prepare($query);
459     $sth->execute($currency);
460     my $cur = ( $sth->fetchrow_array() )[0];
461     unless($cur) {
462         $cur = 1;
463     }
464     return ( $price / $cur );
465 }
466
467 #-------------------------------------------------------------#
468
469 =head3 DelBookFund
470
471 &DelBookFund($bookfundid);
472 this function delete a bookfund which has $bokfundid as parameter on aqbookfund table and delete the approriate budget.
473
474 =cut
475
476 sub DelBookFund {
477     my $bookfundid = shift;
478     my $branchcode=shift;
479     my $dbh = C4::Context->dbh;
480     my $query = "
481         DELETE FROM aqbookfund
482         WHERE bookfundid=?
483         AND branchcode=?
484     ";
485     my $sth=$dbh->prepare($query);
486     $sth->execute($bookfundid,$branchcode);
487     $sth->finish;
488     $query = "
489         DELETE FROM aqbudget where bookfundid=? and branchcode=?
490     ";
491     $sth=$dbh->prepare($query);
492     $sth->execute($bookfundid,$branchcode);
493     $sth->finish;
494 }
495
496 END { }    # module clean-up code here (global destructor)
497
498 1;
499
500 __END__
501
502 =head1 AUTHOR
503
504 Koha Developement team <info@koha.org>
505
506 =cut