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