code cleaning
[koha.git] / C4 / Budgets.pm
1 package C4::Budgets;
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 use strict;
21 use C4::Context;
22 use C4::Dates qw(format_date format_date_in_iso);
23 use C4::Debug;
24
25 use vars qw($VERSION @ISA @EXPORT);
26
27 BEGIN {
28         # set the version for version checking
29         $VERSION = 3.01;
30         require Exporter;
31         @ISA    = qw(Exporter);
32         @EXPORT = qw(
33
34         &GetBudget
35         &GetBudgets
36         &GetBudgetHierarchy
37             &AddBudget
38         &ModBudget
39         &DelBudget
40         &GetBudgetSpent
41         &GetPeriodsCount
42
43             &GetBudgetPeriod
44         &GetBudgetPeriods
45         &ModBudgetPeriod
46             &DelBudgetPeriod
47
48             &GetBudgetPeriodsDropbox
49         &GetBudgetSortDropbox
50         &GetAuthvalueDropbox
51         &GetBudgetPermDropbox
52
53         &ModBudgetPlan
54         &GetCurrency
55         &GetCurrencies
56         &ModCurrencies
57         &ConvertCurrency
58         &GetBudgetsPlanCell
59         &AddBudgetPlanValue
60         &GetBudgetAuthCats
61         &BudgetHasChildren
62         &CheckBudgetParent
63         &CheckBudgetParentPerm
64
65         &HideCols
66         &GetCols
67         );
68 }
69
70 # ----------------------------BUDGETS.PM-----------------------------";
71
72
73 sub HideCols {
74     my ( $authcat, @hide_cols ) = @_;
75     my $dbh = C4::Context->dbh;
76
77 =c
78     my $sth = $dbh->prepare(
79         qq|
80         UPDATE aqbudgets_planning
81         SET display = 1 where authcat =  ? |
82     );
83     $sth->execute( $authcat );
84 =cut
85
86     my $sth1 = $dbh->prepare(
87         qq|
88         UPDATE aqbudgets_planning SET display = 0 
89         WHERE authcat = ? 
90         AND  authvalue = ? |
91     );
92     foreach my $authvalue (@hide_cols) {
93 #        $sth1->{TraceLevel} = 3;
94         $sth1->execute(  $authcat, $authvalue );
95     }
96 }
97
98 sub GetCols {
99     my ( $authcat, $authvalue ) = @_;
100
101     my $dbh = C4::Context->dbh;
102     my $sth = $dbh->prepare(
103         qq|
104         SELECT count(display) as cnt from aqbudgets_planning
105         WHERE  authcat = ? 
106         AND authvalue = ? and display  = 0   |
107     );
108
109 #    $sth->{TraceLevel} = 3;
110     $sth->execute( $authcat, $authvalue );
111     my $res  = $sth->fetchrow_hashref;
112
113     return  $res->{cnt} > 0 ? 0: 1
114
115 }
116
117 sub CheckBudgetParentPerm {
118     my ( $budget, $borrower_id ) = @_;
119     my $depth = $budget->{depth};
120     my $parent_id = $budget->{budget_parent_id};
121     while ($depth) {
122         my $parent = GetBudget($parent_id);
123         $parent_id = $parent->{budget_parent_id};
124         if ( $parent->{budget_owner_id} == $borrower_id ) {
125             return 1;
126         }
127         $depth--
128     }
129     return 0;
130 }
131
132 # -------------------------------------------------------------------
133 sub GetPeriodsCount {
134     my $dbh = C4::Context->dbh;
135     my $sth = $dbh->prepare("
136         SELECT COUNT(*) AS sum FROM aqbudgetperiods ");
137     $sth->execute();
138     my $res = $sth->fetchrow_hashref;
139     return $res->{'sum'};
140 }
141
142 # -------------------------------------------------------------------
143 sub CheckBudgetParent {
144     my ( $new_parent, $budget ) = @_;
145     my $new_parent_id = $new_parent->{'budget_id'};
146     my $budget_id     = $budget->{'budget_id'};
147     my $dbh           = C4::Context->dbh;
148     my $parent_id_tmp = $new_parent_id;
149
150     # check new-parent is not a child (or a child's child ;)
151     my $sth = $dbh->prepare(qq|
152         SELECT budget_parent_id FROM
153             aqbudgets where budget_id = ? | );
154     while (1) {
155         $sth->execute($parent_id_tmp);
156         my $res = $sth->fetchrow_hashref;
157         if ( $res->{'budget_parent_id'} == $budget_id ) {
158             return 1;
159         }
160         if ( not defined $res->{'budget_parent_id'} ) {
161             return 0;
162         }
163         $parent_id_tmp = $res->{'budget_parent_id'};
164     }
165 }
166
167 # -------------------------------------------------------------------
168 sub BudgetHasChildren {
169     my ( $budget_id  ) = @_;
170     my $dbh = C4::Context->dbh;
171     my $sth = $dbh->prepare(qq|
172        SELECT count(*) as sum FROM  aqbudgets
173         WHERE budget_parent_id = ?   | );
174     $sth->execute( $budget_id );
175     my $sum = $sth->fetchrow_hashref;
176     $sth->finish;
177     return $sum->{'sum'};
178 }
179
180 # -------------------------------------------------------------------
181 sub GetBudgetsPlanCell {
182     my ( $cell, $period, $budget ) = @_;
183     my ($actual, $sth);
184     my $dbh = C4::Context->dbh;
185     if ( $cell->{'authcat'} eq 'MONTHS' ) {
186         # get the actual amount
187         $sth = $dbh->prepare( qq|
188
189             SELECT SUM(ecost) AS actual FROM aqorders
190                 WHERE    budget_id = ? AND
191                 entrydate like "$cell->{'authvalue'}%"  |
192         );
193         $sth->execute( $cell->{'budget_id'} );
194     } elsif ( $cell->{'authcat'} eq 'BRANCHES' ) {
195         # get the actual amount
196         $sth = $dbh->prepare( qq|
197
198             SELECT SUM(ecost) FROM aqorders
199                 LEFT JOIN aqorders_items
200                 ON (aqorders.ordernumber = aqorders_items.ordernumber)
201                 LEFT JOIN items
202                 ON (aqorders_items.itemnumber = items.itemnumber)
203                 WHERE budget_id = ? AND homebranch = ? |          );
204
205         $sth->execute( $cell->{'budget_id'}, $cell->{'authvalue'} );
206     } elsif ( $cell->{'authcat'} eq 'ITEMTYPES' ) {
207         # get the actual amount
208         $sth = $dbh->prepare(  qq|
209
210             SELECT SUM( ecost *  quantity) AS actual
211                 FROM aqorders JOIN biblioitems
212                 ON (biblioitems.biblionumber = aqorders.biblionumber )
213                 WHERE aqorders.budget_id = ? and itemtype  = ? |
214         );
215         $sth->execute(  $cell->{'budget_id'},
216                         $cell->{'authvalue'} );
217     }
218     # ELSE GENERIC ORDERS SORT1/SORT2 STAT COUNT.
219     else {
220         # get the actual amount
221         $sth = $dbh->prepare( qq|
222
223         SELECT  SUM(ecost * quantity) AS actual
224             FROM aqorders
225             JOIN aqbudgets ON (aqbudgets.budget_id = aqorders.budget_id )
226             WHERE  aqorders.budget_id = ? AND
227                 ((aqbudgets.sort1_authcat = ? AND sort1 =?) OR
228                 (aqbudgets.sort2_authcat = ? AND sort2 =?))    |
229         );
230         $sth->{TraceLevel} = 2;
231         $sth->execute(  $cell->{'budget_id'},
232                         $budget->{'sort1_authcat'},
233                         $cell->{'authvalue'},
234                         $budget->{'sort2_authcat'},
235                         $cell->{'authvalue'}
236         );
237     }
238     $actual = $sth->fetchrow_array;
239
240     # get the estimated amount
241     my $sth = $dbh->prepare( qq|
242
243         SELECT estimated_amount AS estimated, display FROM aqbudgets_planning
244             WHERE budget_period_id = ? AND
245                 budget_id = ? AND
246                 authvalue = ? AND
247                 authcat = ?         |
248     );
249     $sth->execute(  $cell->{'budget_period_id'},
250                     $cell->{'budget_id'},
251                     $cell->{'authvalue'},
252                     $cell->{'authcat'},
253     );
254
255
256     my $res  = $sth->fetchrow_hashref;
257   #  my $display = $res->{'display'};
258     my $estimated = $res->{'estimated'};
259
260
261     return $actual, $estimated;
262 }
263
264 # -------------------------------------------------------------------
265 sub ModBudgetPlan {
266     my ( $budget_plan, $budget_period_id, $authcat ) = @_;
267     my $dbh = C4::Context->dbh;
268     foreach my $buds (@$budget_plan) {
269         my $lines = $buds->{lines};
270         my $sth = $dbh->prepare( qq|
271                 DELETE FROM aqbudgets_planning
272                     WHERE   budget_period_id   = ? AND
273                             budget_id   = ? AND
274                             authcat            = ? |
275         );
276     #delete a aqplan line of cells, then insert new cells, 
277     # these could be UPDATES rather than DEL/INSERTS...
278         $sth->execute( $budget_period_id,  $lines->[0]{budget_id}   , $authcat );
279
280         foreach my $cell (@$lines) {
281             my $sth = $dbh->prepare( qq|
282
283                 INSERT INTO aqbudgets_planning
284                      SET   budget_id     = ?,
285                      budget_period_id  = ?,
286                      authcat          = ?,
287                      estimated_amount  = ?,
288                      authvalue       = ?  |
289             );
290             $sth->execute(
291                             $cell->{'budget_id'},
292                             $cell->{'budget_period_id'},
293                             $cell->{'authcat'},
294                             $cell->{'estimated_amount'},
295                             $cell->{'authvalue'},
296             );
297         }
298     }
299 }
300
301 # -------------------------------------------------------------------
302 sub GetBudgetSpent {
303         my ($budget_id) = @_;
304         my $dbh = C4::Context->dbh;
305         my $sth = $dbh->prepare(qq|
306         SELECT SUM(ecost *  quantity  ) AS sum FROM aqorders
307             WHERE budget_id = ? AND
308             datecancellationprinted IS NULL 
309     |);
310
311         $sth->execute($budget_id);
312         my $sum =  $sth->fetchrow_array;
313 #       $sum =  sprintf  "%.2f", $sum;
314         return $sum;
315 }
316
317 # -------------------------------------------------------------------
318 sub GetBudgetPermDropbox {
319         my ($perm) = @_;
320         my %labels;
321         $labels{'0'} = 'None';
322         $labels{'1'} = 'Owner';
323         $labels{'2'} = 'Library';
324         my $radio = CGI::scrolling_list(
325                 -name      => 'budget_permission',
326                 -values    => [ '0', '1', '2' ],
327                 -default   => $perm,
328                 -labels    => \%labels,
329                 -size    => 1,
330         );
331         return $radio;
332 }
333
334 # -------------------------------------------------------------------
335 sub GetBudgetAuthCats  {
336     my ($budget_period_id) = shift;
337     # now, populate the auth_cats_loop used in the budget planning button
338     # we must retrieve all auth values used by at least one budget
339     my $dbh = C4::Context->dbh;
340     my $sth=$dbh->prepare("SELECT sort1_authcat,sort2_authcat FROM aqbudgets WHERE budget_period_id=?");
341     $sth->execute($budget_period_id);
342     my %authcats;
343     while (my ($sort1_authcat,$sort2_authcat) = $sth->fetchrow) {
344         $authcats{$sort1_authcat}=1;
345         $authcats{$sort2_authcat}=1;
346     }
347     my @auth_cats_loop;
348     foreach (sort keys %authcats) {
349         push @auth_cats_loop,{ authcat => $_ };
350     }
351     return \@auth_cats_loop;
352 }
353
354 # -------------------------------------------------------------------
355 sub GetAuthvalueDropbox {
356         my ( $name, $authcat, $default ) = @_;
357         my @authorised_values;
358         my %authorised_lib;
359         my $value;
360         my $dbh = C4::Context->dbh;
361         my $sth = $dbh->prepare(
362                 "SELECT authorised_value,lib
363             FROM authorised_values
364             WHERE category = ?
365             ORDER BY  lib"
366         );
367         $sth->execute( $authcat );
368
369         push @authorised_values, '';
370         while (my ($value, $lib) = $sth->fetchrow_array) {
371                 push @authorised_values, $value;
372                 $authorised_lib{$value} = $lib;
373         }
374
375     return 0 if keys(%authorised_lib) == 0;
376
377     my $budget_authvalue_dropbox = CGI::scrolling_list(
378         -values   => \@authorised_values,
379         -labels   => \%authorised_lib,
380         -default  => $default,
381         -override => 1,
382         -size     => 1,
383         -multiple => 0,
384         -name     => $name,
385         -id       => $name,
386     );
387
388     return $budget_authvalue_dropbox
389 }
390
391 # -------------------------------------------------------------------
392 sub GetBudgetPeriodsDropbox {
393     my ($budget_period_id) = @_;
394         my %labels;
395         my @values;
396         my ($active, $periods) = GetBudgetPeriods();
397         foreach my $r (@$periods) {
398                 $labels{"$r->{budget_period_id}"} = $r->{budget_period_description};
399                 push @values, $r->{budget_period_id};
400         }
401
402         # if no buget_id is passed then its an add
403         my $budget_period_dropbox = CGI::scrolling_list(
404                 -name    => 'budget_period_id',
405                 -values  => \@values,
406                 -default => $budget_period_id ? $budget_period_id :  $active,
407                 -size    => 1,
408                 -labels  => \%labels,
409         );
410         return $budget_period_dropbox;
411 }
412
413 # -------------------------------------------------------------------
414 sub GetBudgetPeriods {
415         my $dbh = C4::Context->dbh;
416         my $sth = $dbh->prepare(qq|
417         SELECT *
418          FROM aqbudgetperiods
419          ORDER BY budget_period_startdate, budget_period_enddate |
420         );
421         $sth->execute();
422         my @results;
423         my $active;
424         while (my $data = $sth->fetchrow_hashref) {
425                 if ($data->{'budget_period_active'} == 1) {
426                         $active = $data->{'budget_period_id'};
427                 }
428                 push(@results, $data);
429         }
430         $sth->finish;
431         return ($active, \@results);
432 }
433
434 # -------------------------------------------------------------------
435 sub GetBudgetPeriod {
436         my ($budget_period_id) = @_;
437         my $dbh = C4::Context->dbh;
438         ## $total = number of records linked to the record that must be deleted
439         my $total = 0;
440         ## get information about the record that will be deleted
441         my $sth;
442         if ($budget_period_id gt 0) {
443                 $sth = $dbh->prepare( qq|
444               SELECT      *
445                 FROM aqbudgetperiods
446                 WHERE budget_period_id=? |
447                 );
448                 $sth->execute($budget_period_id);
449         } else {         # ACTIVE BUDGET
450                 $sth = $dbh->prepare(qq|
451                           SELECT      *
452                 FROM aqbudgetperiods
453                 WHERE budget_period_active=1 |
454                 );
455                 $sth->execute();
456         }
457         my $data = $sth->fetchrow_hashref;
458         $sth->finish;
459         return $data;
460 }
461
462 # -------------------------------------------------------------------
463 sub DelBudgetPeriod() {
464         my ($budget_period_id) = @_;
465         my $dbh = C4::Context->dbh;
466           ; ## $total = number of records linked to the record that must be deleted
467     my $total = 0;
468
469         ## get information about the record that will be deleted
470         my $sth = $dbh->prepare(qq|
471                 SELECT     budget_period_id
472                  , budget_period_startdate
473                  , budget_period_enddate
474                  , budget_period_amount
475                  , budget_period_ref
476                  , budget_period_description
477          FROM aqbudgetperiods
478          WHERE budget_period_id=? |
479         );
480         $sth->execute($budget_period_id);
481         my $data = $sth->fetchrow_hashref;
482         $sth->finish;
483 }
484
485 # -------------------------------------------------------------------
486 sub ModBudgetPeriod() {
487         my ($budget_period_id) = @_;
488         my $dbh = C4::Context->dbh
489           ; ## $total = number of records linked to the record that must be deleted       my $total = 0;
490
491         ## get information about the record that will be deleted
492         my $sth = $dbh->prepare("
493             SELECT     budget_period_id
494                  , budget_period_startdate
495                  , budget_period_enddate
496                  , budget_period_amount
497                  , budget_period_ref
498                  , budget_period_description
499         FROM aqbudgetperiods
500         WHERE budget_period_id=?;"
501         );
502         $sth->execute($budget_period_id);
503         my $data = $sth->fetchrow_hashref;
504         $sth->finish;
505 }
506
507 # -------------------------------------------------------------------
508 sub GetBudgetHierarchy {
509         my ($budget_period_id, $branchcode, $owner) = @_;
510         my @bind_params;
511         my $dbh   = C4::Context->dbh;
512         my $query = qq|
513                     SELECT aqbudgets.*
514                     FROM aqbudgets
515                     JOIN aqbudgetperiods USING (budget_period_id)
516                     WHERE budget_period_active=1 |;
517     # show only period X if requested
518     if ($budget_period_id) {
519         $query .= "AND aqbudgets.budget_period_id = ?";
520         push @bind_params, $budget_period_id;
521     }
522         # show only budgets owned by me, my branch or everyone
523     if ($owner) {
524         if ($branchcode) {
525             $query .= " AND (budget_owner_id = ? OR budget_branchcode = ? OR (budget_branchcode IS NULL AND budget_owner_id IS NULL))";
526             push @bind_params, $owner;
527             push @bind_params, $branchcode;
528         } else {
529             $query .= ' AND budget_owner_id = ? OR budget_owner_id IS NULL';
530             push @bind_params, $owner;
531         }
532     } else {
533         if ($branchcode) {
534             $query .= " AND (budget_branchcode =? or budget_branchcode is NULL)";
535             push @bind_params, $branchcode;
536         }
537     }
538         my $sth = $dbh->prepare($query);
539         $sth->execute(@bind_params);
540         my $results = $sth->fetchall_arrayref({});
541         my @res     = @$results;
542         my $i = 0;
543         while (1) {
544                 my $depth_cnt = 0;
545                 foreach my $r (@res) {
546                         my @child;
547                         # look for children
548                         $r->{depth} = '0' if !defined $r->{budget_parent_id};
549                         foreach my $r2 (@res) {
550                                 if (defined $r2->{budget_parent_id}
551                                         && $r2->{budget_parent_id} == $r->{budget_id}) {
552                                         push @child, $r2->{budget_id};
553                                         $r2->{depth} = ($r->{depth} + 1) if defined $r->{depth};
554                                 }
555                         }
556                         $r->{child} = \@child if scalar @child > 0;    # add the child
557                         $depth_cnt++ if !defined $r->{'depth'};
558                 }
559                 last if ($depth_cnt == 0 || $i == 100);
560                 $i++;
561         }
562
563         # look for top parents 1st
564         my @sort;
565         my ($i, $depth_count) = 0;
566         while (1) {
567                 my $children = 0;
568                 foreach my $r (@res) {
569                         if ($r->{depth} == $depth_count) {
570                                 $children++ if (ref $r->{child} eq 'ARRAY');
571
572                                 # find the parent id element_id and insert it after
573                                 my $i2 = 0;
574                                 my $parent;
575                                 if ($depth_count > 0) {
576
577                                         # add indent
578                                         my $depth = $r->{depth} * 2;
579                                         my $space = pack "A[$depth]";
580                                         $r->{budget_code_indent} = $space . $r->{budget_code};
581                                         $r->{budget_name_indent} = $space . $r->{budget_name};
582                                         foreach my $r3 (@sort) {
583                                                 if ($r3->{budget_id} == $r->{budget_parent_id}) {
584                                                         $parent = $i2;
585                                                         last;
586                                                 }
587                                                 $i2++;
588                                         }
589                                 } else {
590                                         $r->{budget_code_indent} = $r->{budget_code};
591                                         $r->{budget_name_indent} = $r->{budget_name};
592                                 }
593
594                                 if (defined $parent) {
595                                         splice @sort, ($parent + 1), 0, $r;
596                                 } else {
597                                         push @sort, $r;
598                                 }
599                         }
600
601                         $i++;
602                 }    # --------------foreach
603                 $depth_count++;
604                 last if $children == 0;
605         }
606
607 # add budget-percent and allocation, and flags for html-template
608         foreach my $r (@sort) {
609                 my $subs_href = $r->{'child'};
610         my @subs_arr = @$subs_href if defined $subs_href;
611
612         my $moo = $r->{'budget_code_indent'};
613         $moo =~ s/\ /\&nbsp\;/g;
614         $r->{'budget_code_indent'} =  $moo;
615
616         my $moo = $r->{'budget_name_indent'};
617         $moo =~ s/\ /\&nbsp\;/g;
618         $r->{'budget_name_indent'} = $moo;
619
620         $r->{'budget_spent'}       = GetBudgetSpent( $r->{'budget_id'} );
621
622         $r->{'budget_amount_total'} =  $r->{'budget_amount'} + $r->{'budget_amount_sublevel'}  ;
623
624         # foreach sub-levels
625         my $unalloc_count ;
626
627                 foreach my $sub (@subs_arr) {
628                         my $sub_budget = GetBudget($sub);
629
630                         $r->{budget_spent_sublevel} +=    GetBudgetSpent( $sub_budget->{'budget_id'} );
631                         $unalloc_count +=   $sub_budget->{'budget_amount'} + $sub_budget->{'budget_amount_sublevel'};
632                 }
633
634             $r->{budget_unalloc_sublevel} =  $r->{'budget_amount_sublevel'}   -   $unalloc_count;
635
636         if ( scalar  @subs_arr == 0  && $r->{budget_amount_sublevel} > 0 ) {
637             $r->{warn_no_subs} = 1;
638         }
639         }
640         return \@sort;
641 }
642
643 # -------------------------------------------------------------------
644 sub AddBudget {
645 my ($budget) = @_;
646 my $dbh        = C4::Context->dbh;
647         my $query = qq|
648     INSERT INTO aqbudgets
649     SET budget_code         = ?,
650         budget_period_id    = ?,
651         budget_parent_id    = ?,
652         budget_name         = ?,
653         budget_branchcode   = ?,
654         budget_amount       = ?,
655         budget_amount_sublevel       = ?,
656         budget_encumb       = ?,
657         budget_expend       = ?,
658         budget_notes        = ?,
659         sort1_authcat       = ?,
660         sort2_authcat       = ?,
661         budget_owner_id     = ?,
662         budget_permission   = ?
663     |;
664         my $sth = $dbh->prepare($query);
665         $sth->execute(
666         $budget->{'budget_code'}        ? $budget->{'budget_code'} : undef,
667         $budget->{'budget_period_id'}   ? $budget->{'budget_period_id'} : undef,
668         $budget->{'budget_parent_id'}   ? $budget->{'budget_parent_id'} : undef,
669         $budget->{'budget_name'}        ? $budget->{'budget_name'} : undef,
670         $budget->{'budget_branchcode'}  ? $budget->{'budget_branchcode'} : undef,
671         $budget->{'budget_amount'}      ? $budget->{'budget_amount'} : undef,
672         $budget->{'budget_amount_sublevel'}      ? $budget->{'budget_amount_sublevel'} : undef,
673         $budget->{'budget_encumb'}      ? $budget->{'budget_encumb'} : undef,
674         $budget->{'budget_expend'}      ? $budget->{'budget_expend'} : undef,
675         $budget->{'budget_notes'}       ? $budget->{'budget_notes'} : undef,
676         $budget->{'sort1_authcat'}      ? $budget->{'sort1_authcat'} : undef,
677         $budget->{'sort2_authcat'}      ? $budget->{'sort2_authcat'} : undef,
678         $budget->{'budget_owner_id'}    ? $budget->{'budget_owner_id'} : undef,
679         $budget->{'budget_permission'}  ? $budget->{'budget_permission'} : undef,
680         );
681         $sth->finish;
682 }
683
684 # -------------------------------------------------------------------
685 sub ModBudget {
686     my ($budget) = @_;
687     my $dbh      = C4::Context->dbh;
688         my $query = qq|
689     UPDATE aqbudgets
690     SET budget_code         = ?,
691         budget_period_id    = ?,
692         budget_parent_id    = ?,
693         budget_name         = ?,
694         budget_branchcode   = ?,
695         budget_amount       = ?,
696         budget_amount_sublevel       = ?,
697         budget_encumb       = ?,
698         budget_expend       = ?,
699         budget_notes        = ?,
700         sort1_authcat       = ?,
701         sort2_authcat       = ?,
702         budget_owner_id     = ?,
703         budget_permission   = ?
704     WHERE budget_id = ?
705     |;
706
707         my $sth = $dbh->prepare($query);
708     $sth->execute(
709         $budget->{'budget_code'}        ? $budget->{'budget_code'} : undef,
710         $budget->{'budget_period_id'}   ? $budget->{'budget_period_id'} : undef,
711         $budget->{'budget_parent_id'}   ? $budget->{'budget_parent_id'} : undef,
712         $budget->{'budget_name'}        ? $budget->{'budget_name'} : undef,
713         $budget->{'budget_branchcode'}  ? $budget->{'budget_branchcode'} : undef,
714         $budget->{'budget_amount'}      ? $budget->{'budget_amount'} : undef,
715         $budget->{'budget_amount_sublevel'}      ? $budget->{'budget_amount_sublevel'} : undef,
716         $budget->{'budget_encumb'}      ? $budget->{'budget_encumb'} : undef,
717         $budget->{'budget_expend'}      ? $budget->{'budget_expend'} : undef,
718         $budget->{'budget_notes'}       ? $budget->{'budget_notes'} : undef,
719         $budget->{'sort1_authcat'}      ? $budget->{'sort1_authcat'} : undef,
720         $budget->{'sort2_authcat'}      ? $budget->{'sort2_authcat'} : undef,
721         $budget->{'budget_owner_id'}    ? $budget->{'budget_owner_id'} : undef,
722         $budget->{'budget_permission'}  ? $budget->{'budget_permission'} : undef,
723         $budget->{'budget_id'},
724     );
725     $sth->finish;
726 }
727
728 # -------------------------------------------------------------------
729 sub DelBudget {
730         my ($budget_id) = @_;
731         my $dbh         = C4::Context->dbh;
732         my $sth         = $dbh->prepare("delete from aqbudgets where budget_id=?");
733         my $rc          = $sth->execute($budget_id);
734         $sth->finish;
735         return $rc;
736 }
737
738 =back
739
740 =head2 FUNCTIONS ABOUT BUDGETS
741
742 =over 2
743
744 =cut
745
746 =head3 GetBudget
747
748 =over 4
749
750 &GetBudget($budget_id);
751
752 get a specific budget
753
754 =back
755
756 =cut
757
758 # -------------------------------------------------------------------
759 sub GetBudget {
760     my ( $budget_id ) = @_;
761     my $dbh = C4::Context->dbh;
762     my $query;
763     my $query = "
764         SELECT *
765         FROM   aqbudgets
766         WHERE  budget_id=?
767         ";
768     my $sth = $dbh->prepare($query);
769     $sth->execute( $budget_id );
770     my $result = $sth->fetchrow_hashref;
771     return $result;
772 }
773
774 =head3 GetBudgets
775
776 =over 4
777
778 &GetBudget($budget_id);
779
780 gets all budgets
781
782 =back
783
784 =cut
785
786 # -------------------------------------------------------------------
787 sub GetBudgets {
788     my ($active) = @_;
789     my $dbh      = C4::Context->dbh;
790     my $q        = "SELECT * from aqbudgets";
791     my $row;
792     my $sth;
793     unless ($active) {
794         $sth = $dbh->prepare($q);
795         $sth->execute();
796     } else {
797         $q   = "select budget_period_id from aqbudgetperiods where budget_period_active = 1 ";
798         $sth = $dbh->prepare($q);
799         $sth->execute();
800         $row = $sth->fetchrow_hashref();
801         $q   = "select * from aqbudgets  WHERE budget_period_id =? ";
802         $sth = $dbh->prepare($q);
803         $sth->execute( $row->{'budget_period_id'} );
804     }
805     my $results = $sth->fetchall_arrayref( {} );
806     $sth->finish;
807     return $results;
808 }
809
810 # -------------------------------------------------------------------
811
812 =head3 GetCurrencies
813
814 @currencies = &GetCurrencies;
815
816 Returns the list of all known currencies.
817
818 C<$currencies> is a array; its elements are references-to-hash, whose
819 keys are the fields from the currency table in the Koha database.
820
821 =cut
822
823 sub GetCurrencies {
824     my $dbh   = C4::Context->dbh;
825     my $query = "
826         SELECT *
827         FROM   currency
828     ";
829     my $sth = $dbh->prepare($query);
830     $sth->execute;
831     my @results = ();
832     while ( my $data = $sth->fetchrow_hashref ) {
833         push( @results, $data );
834     }
835     $sth->finish;
836     return @results;
837 }
838
839 # -------------------------------------------------------------------
840
841 sub GetCurrency {
842     my $dbh   = C4::Context->dbh;
843     my $query = "
844         SELECT * FROM currency where active = '1'    ";
845     my $sth = $dbh->prepare($query);
846     $sth->execute;
847     my $r = $sth->fetchrow_hashref;
848     $sth->finish;
849     return $r;
850 }
851
852 =head3 ModCurrencies
853
854 &ModCurrencies($currency, $newrate);
855
856 Sets the exchange rate for C<$currency> to be C<$newrate>.
857
858 =cut
859
860 sub ModCurrencies {
861     my ( $currency, $rate ) = @_;
862     my $dbh   = C4::Context->dbh;
863     my $query = qq|
864         UPDATE currency
865         SET    rate=?
866         WHERE  currency=? |;
867     my $sth = $dbh->prepare($query);
868     $sth->execute( $rate, $currency );
869 }
870
871 # -------------------------------------------------------------------
872
873 =head3 ConvertCurrency
874
875 $foreignprice = &ConvertCurrency($currency, $localprice);
876
877 Converts the price C<$localprice> to foreign currency C<$currency> by
878 dividing by the exchange rate, and returns the result.
879
880 If no exchange rate is found,e is one
881 to one.
882
883 =cut
884
885 sub ConvertCurrency {
886     my ( $currency, $price ) = @_;
887     my $dbh   = C4::Context->dbh;
888     my $query = "
889         SELECT rate
890         FROM   currency
891         WHERE  currency=?
892     ";
893     my $sth = $dbh->prepare($query);
894     $sth->execute($currency);
895     my $cur = ( $sth->fetchrow_array() )[0];
896     unless ($cur) {
897         $cur = 1;
898     }
899     return ( $price / $cur );
900 }
901
902 END { }    # module clean-up code here (global destructor)
903
904 1;
905 __END__
906
907 =back
908
909 =head1 AUTHOR
910
911 Koha Developement team <info@koha.org>
912
913 =cut