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