Acquisition module has been cut into 3 files :
[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
23 use strict;
24
25
26 use vars qw($VERSION @ISA @EXPORT);
27
28 # set the version for version checking
29 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v ); };
30
31 =head1 NAME
32
33 C4::Bookfund - Koha functions for dealing with bookfund, currency & money.
34
35 =head1 SYNOPSIS
36
37 use C4::Bookfund;
38
39 =head1 DESCRIPTION
40
41 the functions in this modules deal with bookfund, currency and money.
42 They allow to get and/or set some informations for a specific budget or currency.
43
44 =cut
45
46 @ISA    = qw(Exporter);
47 @EXPORT = qw(
48     &GetBookFund &GetBookFunds &GetBookFundBreakdown &GetCurrencies
49     &ModBookFund &ModCurrencies
50     &Countbookfund
51     &ConvertCurrency
52 );
53
54 =head1 FUNCTIONS
55
56 =over 2
57
58 =cut
59
60 #-------------------------------------------------------------#
61
62 =head3 GetBookFund
63
64 =over 4
65
66 $dataaqbookfund = &GetBookFund($bookfundid);
67
68 this function get the bookfundid, bookfundname, the bookfundgroup,  the branchcode
69 from aqbookfund table for bookfundid given on input arg.
70 return: 
71 C<$dataaqbookfund> is a hashref full of bookfundid, bookfundname, bookfundgroup,
72 and branchcode.
73
74 =back
75
76 =cut
77
78 sub GetBookFund {
79     my $bookfundid = @_;
80     my $dbh = C4::Context->dbh;
81     my $query = "
82         SELECT
83             bookfundid,
84             bookfundname,
85             bookfundgroup,
86             branchcode
87         FROM aqbookfund
88         WHERE bookfundid = ?
89     ";
90     my $sth=$dbh->prepare($query);
91     return $sth->fetchrow_hashref;
92 }
93
94 #-------------------------------------------------------------#
95
96 =head3 GetBookFunds
97
98 =over 4
99
100 @results = &GetBookFunds;
101
102 Returns a list of all book funds.
103
104 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
105 alphabetically by book fund name.
106
107 =back
108
109 =cut
110
111 sub GetBookFunds {
112     my ($branch) = @_;
113     my $dbh      = C4::Context->dbh;
114     my $userenv  = C4::Context->userenv;
115     my $branch   = $userenv->{branch};
116     my $strsth;
117
118     if ( $branch ne '' ) {
119         $strsth = "
120         SELECT *
121         FROM   aqbookfund,aqbudget
122         WHERE  aqbookfund.bookfundid=aqbudget.bookfundid
123             AND startdate<now()
124             AND enddate>now()
125             AND (aqbookfund.branchcode IS NULL OR aqbookfund.branchcode='' OR aqbookfund.branchcode= ? )
126       GROUP BY aqbookfund.bookfundid ORDER BY bookfundname";
127     }
128     else {
129         $strsth = "
130             SELECT *
131             FROM   aqbookfund,
132                    aqbudget
133             WHERE aqbookfund.bookfundid=aqbudget.bookfundid
134                 AND startdate<now()
135                 AND enddate>now()
136             GROUP BY aqbookfund.bookfundid ORDER BY bookfundname
137         ";
138     }
139     my $sth = $dbh->prepare($strsth);
140     if ( $branch ne '' ) {
141         $sth->execute($branch);
142     }
143     else {
144         $sth->execute;
145     }
146     my @results = ();
147     while ( my $data = $sth->fetchrow_hashref ) {
148         push( @results, $data );
149     }
150     $sth->finish;
151     return @results;
152 }
153
154 #-------------------------------------------------------------#
155
156 =head3 GetCurrencies
157
158 =over 4
159
160 @currencies = &GetCurrencies;
161
162 Returns the list of all known currencies.
163
164 C<$currencies> is a array; its elements are references-to-hash, whose
165 keys are the fields from the currency table in the Koha database.
166
167 =back
168
169 =cut
170
171 sub GetCurrencies {
172     my $dbh = C4::Context->dbh;
173     my $query = "
174         SELECT *
175         FROM   currency
176     ";
177     my $sth = $dbh->prepare($query);
178     $sth->execute;
179     my @results = ();
180     while ( my $data = $sth->fetchrow_hashref ) {
181         push( @results, $data );
182     }
183     $sth->finish;
184     return @results;
185 }
186
187 #-------------------------------------------------------------#
188
189 =head3 GetBookFundBreakdown
190
191 =over 4
192
193 ( $spent, $comtd ) = &GetBookFundBreakdown( $id, $year, $start, $end );
194
195 returns the total comtd & spent for a given bookfund, and a given year
196 used in acqui-home.pl
197
198 =back
199
200 =cut
201
202 sub GetBookFundBreakdown {
203     my ( $id, $year, $start, $end ) = @_;
204     my $dbh = C4::Context->dbh;
205
206     # if no start/end dates given defaut to everything
207     if ( !$start ) {
208         $start = '0000-00-00';
209         $end   = 'now()';
210     }
211
212     # do a query for spent totals.
213     my $query = "
214         SELECT quantity,datereceived,freight,unitprice,listprice,ecost,
215                quantityreceived,subscription
216         FROM   aqorders
217         LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
218         WHERE  bookfundid=?
219             AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
220             AND ((datereceived >= ? and datereceived < ?) OR (budgetdate >= ? and budgetdate < ?))
221     ";
222     my $sth = $dbh->prepare($query);
223     $sth->execute( $id, $start, $end, $start, $end );
224
225     my $spent = 0;
226     while ( my $data = $sth->fetchrow_hashref ) {
227         if ( $data->{'subscription'} == 1 ) {
228             $spent += $data->{'quantity'} * $data->{'unitprice'};
229         }
230         else {
231
232             my $leftover = $data->{'quantity'} - $data->{'quantityreceived'};
233             $spent += ( $data->{'unitprice'} ) * $data->{'quantityreceived'};
234
235         }
236     }
237
238     # then do a seperate query for commited totals, (pervious single query was
239     # returning incorrect comitted results.
240
241     my $query = "
242         SELECT  quantity,datereceived,freight,unitprice,
243                 listprice,ecost,quantityreceived AS qrev,
244                 subscription,title,itemtype,aqorders.biblionumber,
245                 aqorders.booksellerinvoicenumber,
246                 quantity-quantityreceived AS tleft,
247                 aqorders.ordernumber AS ordnum,entrydate,budgetdate,
248                 booksellerid,aqbasket.basketno
249         FROM    aqorderbreakdown,
250                 aqbasket,
251                 aqorders
252         LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=aqorders.biblioitemnumber
253         WHERE   bookfundid=?
254             AND aqorders.ordernumber=aqorderbreakdown.ordernumber
255             AND aqorders.basketno=aqbasket.basketno
256             AND (budgetdate >= ? AND budgetdate < ?)
257             AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
258     ";
259
260     my $sth = $dbh->prepare($query);
261     $sth->execute( $id, $start, $end );
262
263     my $comtd;
264
265     my $total = 0;
266     while ( my $data = $sth->fetchrow_hashref ) {
267         my $left = $data->{'tleft'};
268         if ( !$left || $left eq '' ) {
269             $left = $data->{'quantity'};
270         }
271         if ( $left && $left > 0 ) {
272             my $subtotal = $left * $data->{'ecost'};
273             $data->{subtotal} = $subtotal;
274             $data->{'left'} = $left;
275             $comtd += $subtotal;
276         }
277     }
278
279     $sth->finish;
280     return ( $spent, $comtd );
281 }
282
283 #-------------------------------------------------------------#
284
285 =head3 ModBookFund
286
287 =over 4
288
289 &ModBookFund($bookfundname,$branchcode,$bookfundid);
290 this function update the bookfundname and the branchcode on aqbookfund table
291 on database.
292
293 =back
294
295 =cut
296
297 sub ModBookFund {
298     my ($bookfundname,$branchcode,$bookfundid) = @_;
299     my $dbh = C4::Context->dbh;
300     my $query = "
301         UPDATE aqbookfund
302         SET    bookfundname = ?,
303                branchcode = ?
304         WHERE  bookfundid = ?
305     ";
306     my $sth=$dbh->prepare($query);
307     $sth->execute($bookfundname,$branchcode,$bookfundid);
308 # budgets depending on a bookfund must have the same branchcode
309 # if the bookfund branchcode is set
310     if (defined $branchcode) {
311         $query = "
312             UPDATE aqbudget
313             SET branchcode = ?
314         ";
315         $sth=$dbh->prepare($query);
316         $sth->execute($branchcode);
317     }
318 }
319
320 #-------------------------------------------------------------#
321
322 =head3 ModCurrencies
323
324 =over 4
325
326 &ModCurrencies($currency, $newrate);
327
328 Sets the exchange rate for C<$currency> to be C<$newrate>.
329
330 =back
331
332 =cut
333
334 sub ModCurrencies {
335     my ( $currency, $rate ) = @_;
336     my $dbh = C4::Context->dbh;
337     my $query = "
338         UPDATE currency
339         SET    rate=?
340         WHERE  currency=?
341     ";
342     my $sth = $dbh->prepare($query);
343     $sth->execute( $rate, $currency );
344 }
345
346 #-------------------------------------------------------------#
347
348 =head3 Countbookfund
349
350 =over 4
351
352 $data = Countbookfund($bookfundid);
353
354 this function count the number of bookfund with id given on input arg.
355 return :
356 the result of the SQL query as an hashref.
357
358 =back
359
360 =cut
361
362 sub Countbookfund {
363     my $bookfundid = @_;
364     my $dbh = C4::Context->dbh;
365     my $query ="
366         SELECT COUNT(*)
367         FROM   aqbookfund
368         WHERE bookfundid = ?
369     ";
370     my $sth = $dbh->prepare($query);
371     $sth->execute($bookfundid);
372     return $sth->fetchrow_hashref;
373 }
374
375
376 #-------------------------------------------------------------#
377
378 =head3 ConvertCurrency
379
380 =over 4
381
382 $foreignprice = &ConvertCurrency($currency, $localprice);
383
384 Converts the price C<$localprice> to foreign currency C<$currency> by
385 dividing by the exchange rate, and returns the result.
386
387 If no exchange rate is found, C<&ConvertCurrency> assumes the rate is one
388 to one.
389
390 =back
391
392 =cut
393
394 sub ConvertCurrency {
395     my ( $currency, $price ) = @_;
396     my $dbh = C4::Context->dbh;
397     my $query = "
398         SELECT rate
399         FROM   currency
400         WHERE  currency=?
401     ";
402     my $sth = $dbh->prepare($query);
403     $sth->execute($currency);
404     my $cur = ( $sth->fetchrow_array() )[0];
405     if ( $cur == 0 ) {
406         $cur = 1;
407     }
408     return ( $price / $cur );
409 }
410
411
412 END { }    # module clean-up code here (global destructor)
413
414 1;
415
416 __END__
417
418 =back
419
420 =head1 AUTHOR
421
422 Koha Developement team <info@koha.org>
423
424 =cut