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