Bug 7180: Order from staged file improvements
[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     };
717     my $sth = $dbh->prepare( $query );
718     $sth->execute( $budget_code );
719     return $sth->fetchrow_hashref;
720 }
721
722 =head2 GetChildBudgetsSpent
723
724   &GetChildBudgetsSpent($budget-id);
725
726 gets the total spent of the level and sublevels of $budget_id
727
728 =cut
729
730 # -------------------------------------------------------------------
731 sub GetChildBudgetsSpent {
732     my ( $budget_id ) = @_;
733     my $dbh = C4::Context->dbh;
734     my $query = "
735         SELECT *
736         FROM   aqbudgets
737         WHERE  budget_parent_id=?
738         ";
739     my $sth = $dbh->prepare($query);
740     $sth->execute( $budget_id );
741     my $result = $sth->fetchall_arrayref({});
742     my $total_spent = GetBudgetSpent($budget_id);
743     if ($result){
744         $total_spent += GetChildBudgetsSpent($_->{"budget_id"}) foreach @$result;    
745     }
746     return $total_spent;
747 }
748
749 =head2 GetBudgets
750
751   &GetBudgets($filter, $order_by);
752
753 gets all budgets
754
755 =cut
756
757 # -------------------------------------------------------------------
758 sub GetBudgets {
759     my $filters = shift;
760     my $orderby = shift || 'budget_name';
761     return SearchInTable("aqbudgets",$filters, $orderby, undef,undef, undef, "wide");
762 }
763
764 =head2 GetBudgetUsers
765
766     my @borrowernumbers = &GetBudgetUsers($budget_id);
767
768 Return the list of borrowernumbers linked to a budget
769
770 =cut
771
772 sub GetBudgetUsers {
773     my ($budget_id) = @_;
774
775     my $dbh = C4::Context->dbh;
776     my $query = qq{
777         SELECT borrowernumber
778         FROM aqbudgetborrowers
779         WHERE budget_id = ?
780     };
781     my $sth = $dbh->prepare($query);
782     $sth->execute($budget_id);
783
784     my @borrowernumbers;
785     while (my ($borrowernumber) = $sth->fetchrow_array) {
786         push @borrowernumbers, $borrowernumber
787     }
788
789     return @borrowernumbers;
790 }
791
792 =head2 ModBudgetUsers
793
794     &ModBudgetUsers($budget_id, @borrowernumbers);
795
796 Modify the list of borrowernumbers linked to a budget
797
798 =cut
799
800 sub ModBudgetUsers {
801     my ($budget_id, @budget_users_id) = @_;
802
803     return unless $budget_id;
804
805     my $dbh = C4::Context->dbh;
806     my $query = "DELETE FROM aqbudgetborrowers WHERE budget_id = ?";
807     my $sth = $dbh->prepare($query);
808     $sth->execute($budget_id);
809
810     $query = qq{
811         INSERT INTO aqbudgetborrowers (budget_id, borrowernumber)
812         VALUES (?,?)
813     };
814     $sth = $dbh->prepare($query);
815     foreach my $borrowernumber (@budget_users_id) {
816         next unless $borrowernumber;
817         $sth->execute($budget_id, $borrowernumber);
818     }
819 }
820
821 sub CanUserUseBudget {
822     my ($borrower, $budget, $userflags) = @_;
823
824     if (not ref $borrower) {
825         $borrower = C4::Members::GetMember(borrowernumber => $borrower);
826     }
827     if (not ref $budget) {
828         $budget = GetBudget($budget);
829     }
830
831     return 0 unless ($borrower and $budget);
832
833     if (not defined $userflags) {
834         $userflags = C4::Auth::getuserflags($borrower->{flags},
835             $borrower->{userid});
836     }
837
838     unless ($userflags->{superlibrarian}
839     || (ref $userflags->{acquisition}
840         && $userflags->{acquisition}->{budget_manage_all})
841     || (!ref $userflags->{acquisition} && $userflags->{acquisition}))
842     {
843         if (not exists $userflags->{acquisition}) {
844             return 0;
845         }
846
847         if (!ref $userflags->{acquisition} && !$userflags->{acquisition}) {
848             return 0;
849         }
850
851         # Budget restricted to owner
852         if ( $budget->{budget_permission} == 1 ) {
853             if (    $budget->{budget_owner_id}
854                 and $budget->{budget_owner_id} != $borrower->{borrowernumber} )
855             {
856                 return 0;
857             }
858         }
859
860         # Budget restricted to owner, users and library
861         elsif ( $budget->{budget_permission} == 2 ) {
862             my @budget_users = GetBudgetUsers( $budget->{budget_id} );
863
864             if (
865                 (
866                         $budget->{budget_owner_id}
867                     and $budget->{budget_owner_id} !=
868                     $borrower->{borrowernumber}
869                     or not $budget->{budget_owner_id}
870                 )
871                 and ( 0 == grep { $borrower->{borrowernumber} == $_ }
872                     @budget_users )
873                 and defined $budget->{budget_branchcode}
874                 and $budget->{budget_branchcode} ne
875                 C4::Context->userenv->{branch}
876               )
877             {
878                 return 0;
879             }
880         }
881
882         # Budget restricted to owner and users
883         elsif ( $budget->{budget_permission} == 3 ) {
884             my @budget_users = GetBudgetUsers( $budget->{budget_id} );
885             if (
886                 (
887                         $budget->{budget_owner_id}
888                     and $budget->{budget_owner_id} !=
889                     $borrower->{borrowernumber}
890                     or not $budget->{budget_owner_id}
891                 )
892                 and ( 0 == grep { $borrower->{borrowernumber} == $_ }
893                     @budget_users )
894               )
895             {
896                 return 0;
897             }
898         }
899     }
900
901     return 1;
902 }
903
904 sub CanUserModifyBudget {
905     my ($borrower, $budget, $userflags) = @_;
906
907     if (not ref $borrower) {
908         $borrower = C4::Members::GetMember(borrowernumber => $borrower);
909     }
910     if (not ref $budget) {
911         $budget = GetBudget($budget);
912     }
913
914     return 0 unless ($borrower and $budget);
915
916     if (not defined $userflags) {
917         $userflags = C4::Auth::getuserflags($borrower->{flags},
918             $borrower->{userid});
919     }
920
921     unless ($userflags->{superlibrarian}
922     || (ref $userflags->{acquisition}
923         && $userflags->{acquisition}->{budget_manage_all})
924     || (!ref $userflags->{acquisition} && $userflags->{acquisition}))
925     {
926         if (!CanUserUseBudget($borrower, $budget, $userflags)) {
927             return 0;
928         }
929
930         if (ref $userflags->{acquisition}
931         && !$userflags->{acquisition}->{budget_modify}) {
932             return 0;
933         }
934     }
935
936     return 1;
937 }
938
939 # -------------------------------------------------------------------
940
941 =head2 GetCurrencies
942
943   @currencies = &GetCurrencies;
944
945 Returns the list of all known currencies.
946
947 C<$currencies> is a array; its elements are references-to-hash, whose
948 keys are the fields from the currency table in the Koha database.
949
950 =cut
951
952 sub GetCurrencies {
953     my $dbh   = C4::Context->dbh;
954     my $query = "
955         SELECT *
956         FROM   currency
957     ";
958     my $sth = $dbh->prepare($query);
959     $sth->execute;
960     my @results = ();
961     while ( my $data = $sth->fetchrow_hashref ) {
962         push( @results, $data );
963     }
964     return @results;
965 }
966
967 # -------------------------------------------------------------------
968
969 sub GetCurrency {
970     my $dbh   = C4::Context->dbh;
971     my $query = "
972         SELECT * FROM currency where active = '1'    ";
973     my $sth = $dbh->prepare($query);
974     $sth->execute;
975     my $r = $sth->fetchrow_hashref;
976     return $r;
977 }
978
979 =head2 ModCurrencies
980
981 &ModCurrencies($currency, $newrate);
982
983 Sets the exchange rate for C<$currency> to be C<$newrate>.
984
985 =cut
986
987 sub ModCurrencies {
988     my ( $currency, $rate ) = @_;
989     my $dbh   = C4::Context->dbh;
990     my $query = qq|
991         UPDATE currency
992         SET    rate=?
993         WHERE  currency=? |;
994     my $sth = $dbh->prepare($query);
995     $sth->execute( $rate, $currency );
996 }
997
998 # -------------------------------------------------------------------
999
1000 =head2 ConvertCurrency
1001
1002   $foreignprice = &ConvertCurrency($currency, $localprice);
1003
1004 Converts the price C<$localprice> to foreign currency C<$currency> by
1005 dividing by the exchange rate, and returns the result.
1006
1007 If no exchange rate is found, e is one to one.
1008
1009 =cut
1010
1011 sub ConvertCurrency {
1012     my ( $currency, $price ) = @_;
1013     my $dbh   = C4::Context->dbh;
1014     my $query = "
1015         SELECT rate
1016         FROM   currency
1017         WHERE  currency=?
1018     ";
1019     my $sth = $dbh->prepare($query);
1020     $sth->execute($currency);
1021     my $cur = ( $sth->fetchrow_array() )[0];
1022     unless ($cur) {
1023         $cur = 1;
1024     }
1025     return ( $price / $cur );
1026 }
1027
1028 END { }    # module clean-up code here (global destructor)
1029
1030 1;
1031 __END__
1032
1033 =head1 AUTHOR
1034
1035 Koha Development Team <http://koha-community.org/>
1036
1037 =cut