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