Modbookfund() now correctly updating all aqbudget.branchcode's linked to mod-ed bookfund
[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
25 use vars qw($VERSION @ISA @EXPORT);
26
27 # set the version for version checking
28 $VERSION = 3.00;
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
143         LEFT JOIN aqbudget ON aqbookfund.bookfundid=aqbudget.bookfundid
144         WHERE  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             LEFT JOIN aqbudget ON aqbookfund.bookfundid=aqbudget.bookfundid
154             WHERE startdate<now()
155                 AND enddate>now()
156             GROUP BY aqbookfund.bookfundid ORDER BY bookfundname
157         ";
158     }
159     my $sth = $dbh->prepare($strsth);
160     if ( $branch ne '' ) {
161         $sth->execute($branch);
162     }
163     else {
164         $sth->execute;
165     }
166     my @results = ();
167     while ( my $data = $sth->fetchrow_hashref ) {
168         push( @results, $data );
169     }
170     $sth->finish;
171     return @results;
172 }
173
174 #-------------------------------------------------------------#
175
176 =head3 GetCurrencies
177
178 @currencies = &GetCurrencies;
179
180 Returns the list of all known currencies.
181
182 C<$currencies> is a array; its elements are references-to-hash, whose
183 keys are the fields from the currency table in the Koha database.
184
185 =cut
186
187 sub GetCurrencies {
188     my $dbh = C4::Context->dbh;
189     my $query = "
190         SELECT *
191         FROM   currency
192     ";
193     my $sth = $dbh->prepare($query);
194     $sth->execute;
195     my @results = ();
196     while ( my $data = $sth->fetchrow_hashref ) {
197         push( @results, $data );
198     }
199     $sth->finish;
200     return @results;
201 }
202
203 #-------------------------------------------------------------#
204
205 =head3 GetBookFundBreakdown
206
207 ( $spent, $comtd ) = &GetBookFundBreakdown( $id, $start, $end );
208
209 returns the total comtd & spent for a given bookfund, and a given year
210 used in acqui-home.pl
211
212 =cut
213
214 sub GetBookFundBreakdown {
215     my ( $id, $start, $end ) = @_;
216     my $dbh = C4::Context->dbh;
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,subscription
228         FROM   aqorders
229         LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
230         LEFT JOIN aqbookfund ON (aqorderbreakdown.bookfundid=aqbookfund.bookfundid and aqorderbreakdown.branchcode=aqbookfund.branchcode)
231         LEFT JOIN aqbudget ON (aqbudget.bookfundid=aqbookfund.bookfundid and aqbudget.branchcode=aqbookfund.branchcode)
232         WHERE  aqorderbreakdown.bookfundid=?
233             AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
234             AND ((budgetdate >= ? and budgetdate < ?) OR (startdate>=? and enddate<=?))
235     ";
236     my $sth = $dbh->prepare($query);
237     $sth->execute( $id, $start, $end, $start, $end );
238
239     my ($spent) = 0;
240     while ( my $data = $sth->fetchrow_hashref ) {
241         if ( $data->{'subscription'} == 1 ) {
242             $spent += $data->{'quantity'} * $data->{'unitprice'};
243         }
244         else {
245             $spent += ( $data->{'unitprice'} ) * ($data->{'quantityreceived'}?$data->{'quantityreceived'}:0);
246
247         }
248     }
249
250     # then do a seperate query for commited totals, (pervious single query was
251     # returning incorrect comitted results.
252
253     $query = "
254         SELECT  quantity,datereceived,freight,unitprice,
255                 listprice,ecost,quantityreceived AS qrev,
256                 subscription,title,itemtype,aqorders.biblionumber,
257                 aqorders.booksellerinvoicenumber,
258                 quantity-quantityreceived AS tleft,
259                 aqorders.ordernumber AS ordnum,entrydate,budgetdate
260         FROM    aqorders
261         LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=aqorders.biblioitemnumber
262         LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
263         WHERE   bookfundid=?
264             AND (budgetdate >= ? AND budgetdate < ?)
265             AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
266     ";
267
268     $sth = $dbh->prepare($query);
269 #      warn "$start $end";     
270     $sth->execute( $id, $start, $end );
271
272     my $comtd=0;
273
274     while ( my $data = $sth->fetchrow_hashref ) {
275         my $left = $data->{'tleft'};
276         if ( (!$left && (!$data->{'datereceived'}||$data->{'datereceived'} eq '0000-00-00') ) || $left eq '' ) {
277             $left = $data->{'quantity'};
278         }
279         if ( $left && $left > 0 ) {
280             my $subtotal = $left * $data->{'ecost'};
281             $data->{subtotal} = $subtotal;
282             $data->{'left'} = $left;
283             $comtd += $subtotal;
284         }
285 #         use Data::Dumper; warn Dumper($data);    
286     }
287
288     $sth->finish;
289     return ( $spent, $comtd );
290 }
291
292 =head3 NewBookFund
293
294 &NewBookFund(bookfundid, bookfundname, branchcode);
295
296 this function create a new bookfund into the database.
297
298 =cut 
299
300 sub NewBookFund{
301     my ($bookfundid, $bookfundname, $branchcode) = @_;
302     $branchcode = undef unless $branchcode;
303     my $dbh = C4::Context->dbh;
304     my $query = "
305         INSERT
306         INTO aqbookfund
307             (bookfundid, bookfundname, branchcode)
308         VALUES
309             (?, ?, ?)
310     ";
311     my $sth=$dbh->prepare($query);
312     $sth->execute($bookfundid,$bookfundname,"$branchcode");
313 }
314
315 #-------------------------------------------------------------#
316
317 =head3 ModBookFund
318
319 &ModBookFund($bookfundname,$branchcode,$bookfundid);
320 this function update the bookfundname and the branchcode on aqbookfund table
321 on database.
322
323 =cut
324
325
326 sub ModBookFund {
327     my ($bookfundname,$bookfundid,$current_branch, $branchcode) = @_;
328
329     my $dbh = C4::Context->dbh;
330
331     my $retval = $dbh->do("
332      UPDATE aqbookfund
333         SET    bookfundname = '$bookfundname', 
334                branchcode = '$branchcode'
335         WHERE  bookfundid = '$bookfundid'
336         AND branchcode = '$current_branch'
337     ");
338
339     ### $retval
340
341     # budgets depending on a bookfund must have the same branchcode
342     # if the bookfund branchcode is set
343     if (defined $branchcode && $retval > 0) {
344         my $query = "UPDATE  aqbudget  
345             SET     branchcode = ?
346             WHERE   bookfundid = ? ";
347
348         my $sth=$dbh->prepare($query);
349         $sth->execute($branchcode, $bookfundid) ;
350     }
351 }
352
353
354
355 #-------------------------------------------------------------#
356
357 =head3 SearchBookFund
358
359 @results = SearchBookFund(
360         $bookfundid,$filter,$filter_bookfundid,
361         $filter_bookfundname,$filter_branchcode);
362
363 this function searchs among the bookfunds corresponding to our filtering rules.
364
365 =cut
366
367 sub SearchBookFund {
368     my $dbh = C4::Context->dbh;
369     my ($filter,
370         $filter_bookfundid,
371         $filter_bookfundname,
372         $filter_branchcode
373        ) = @_;
374
375     my @bindings;
376
377     my $query = "
378         SELECT  bookfundid,
379                 bookfundname,
380                 bookfundgroup,
381                 branchcode
382         FROM aqbookfund
383         WHERE 1 ";
384
385     if ($filter) {
386         if ($filter_bookfundid) {
387             $query.= "AND bookfundid = ?";
388             push @bindings, $filter_bookfundid;
389         }
390         if ($filter_bookfundname) {
391             $query.= "AND bookfundname like ?";
392             push @bindings, '%'.$filter_bookfundname.'%';
393         }
394         if ($filter_branchcode) {
395             $query.= "AND branchcode = ?";
396             push @bindings, $filter_branchcode;
397         }
398     }
399     $query.= "ORDER BY bookfundid";
400
401     my $sth = $dbh->prepare($query);
402     $sth->execute(@bindings);
403     my @results;
404     while (my $row = $sth->fetchrow_hashref) {
405         push @results, $row;
406     }
407     return @results;
408 }
409
410 #-------------------------------------------------------------#
411
412 =head3 ModCurrencies
413
414 &ModCurrencies($currency, $newrate);
415
416 Sets the exchange rate for C<$currency> to be C<$newrate>.
417
418 =cut
419
420 sub ModCurrencies {
421     my ( $currency, $rate ) = @_;
422     my $dbh = C4::Context->dbh;
423     my $query = "
424         UPDATE currency
425         SET    rate=?
426         WHERE  currency=?
427     ";
428     my $sth = $dbh->prepare($query);
429     $sth->execute( $rate, $currency );
430 }
431
432 #-------------------------------------------------------------#
433
434 =head3 Countbookfund
435
436 $number = Countbookfund($bookfundid);
437
438 this function count the number of bookfund with id given on input arg.
439 return :
440 the result of the SQL query as a number.
441
442 =cut
443
444 sub Countbookfund {
445     my $bookfundid = shift;
446     my $branchcode = shift;
447     my $dbh = C4::Context->dbh;
448     my $query ="
449         SELECT COUNT(*)
450         FROM  aqbookfund
451         WHERE bookfundid = ?
452         AND   branchcode = ?
453     ";
454     my $sth = $dbh->prepare($query);
455     $sth->execute($bookfundid,"$branchcode");
456     return $sth->fetchrow;
457 }
458
459
460 #-------------------------------------------------------------#
461
462 =head3 ConvertCurrency
463
464 $foreignprice = &ConvertCurrency($currency, $localprice);
465
466 Converts the price C<$localprice> to foreign currency C<$currency> by
467 dividing by the exchange rate, and returns the result.
468
469 If no exchange rate is found, C<&ConvertCurrency> assumes the rate is one
470 to one.
471
472 =cut
473
474 sub ConvertCurrency {
475     my ( $currency, $price ) = @_;
476     my $dbh = C4::Context->dbh;
477     my $query = "
478         SELECT rate
479         FROM   currency
480         WHERE  currency=?
481     ";
482     my $sth = $dbh->prepare($query);
483     $sth->execute($currency);
484     my $cur = ( $sth->fetchrow_array() )[0];
485     unless($cur) {
486         $cur = 1;
487     }
488     return ( $price / $cur );
489 }
490
491 #-------------------------------------------------------------#
492
493 =head3 DelBookFund
494
495 &DelBookFund($bookfundid);
496 this function delete a bookfund which has $bokfundid as parameter on aqbookfund table and delete the approriate budget.
497
498 =cut
499
500 sub DelBookFund {
501     my $bookfundid = shift;
502     my $branchcode=shift;
503     my $dbh = C4::Context->dbh;
504     my $query = "
505         DELETE FROM aqbookfund
506         WHERE bookfundid=?
507         AND branchcode=?
508     ";
509     my $sth=$dbh->prepare($query);
510     $sth->execute($bookfundid,$branchcode);
511     $sth->finish;
512     $query = "
513         DELETE FROM aqbudget where bookfundid=? and branchcode=?
514     ";
515     $sth=$dbh->prepare($query);
516     $sth->execute($bookfundid,$branchcode);
517     $sth->finish;
518 }
519
520 END { }    # module clean-up code here (global destructor)
521
522 1;
523
524 __END__
525
526 =head1 AUTHOR
527
528 Koha Developement team <info@koha.org>
529
530 =cut