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