Merge fixes and removing warnings
[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     $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, undef,undef, undef, "wide");
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, $depth_count);
521         ($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                                         $r->{budget_code_indent} = $r->{budget_code};
536                                         $r->{budget_name_indent} = $r->{budget_name};
537                                         foreach my $r3 (@sort) {
538                                                 if ($r3->{budget_id} == $r->{budget_parent_id}) {
539                                                         $parent = $i2;
540                                                         last;
541                                                 }
542                                                 $i2++;
543                                         }
544                                 } else {
545                                         $r->{budget_code_indent} = $r->{budget_code};
546                                         $r->{budget_name_indent} = $r->{budget_name};
547                                 }
548                 
549                                 if (defined $parent) {
550                                         splice @sort, ($parent + 1), 0, $r;
551                                 } else {
552                                         push @sort, $r;
553                                 }
554                         }
555
556                         $i++;
557                 }    # --------------foreach
558                 $depth_count++;
559                 last if $children == 0;
560         }
561
562 # add budget-percent and allocation, and flags for html-template
563         foreach my $r (@sort) {
564                 my $subs_href = $r->{'child'};
565         my @subs_arr = @$subs_href if defined $subs_href;
566
567         my $moo = $r->{'budget_code_indent'};
568         $moo =~ s/\ /\&nbsp\;/g;
569         $r->{'budget_code_indent'} =  $moo;
570
571         $moo = $r->{'budget_name_indent'};
572         $moo =~ s/\ /\&nbsp\;/g;
573         $r->{'budget_name_indent'} = $moo;
574
575         $r->{'budget_spent'}       = GetBudgetSpent( $r->{'budget_id'} );
576
577         $r->{'budget_amount_total'} =  $r->{'budget_amount'};
578
579         # foreach sub-levels
580         my $unalloc_count ;
581
582                 foreach my $sub (@subs_arr) {
583                         my $sub_budget = GetBudget($sub);
584
585                         $r->{budget_spent_sublevel} +=    GetBudgetSpent( $sub_budget->{'budget_id'} );
586                         $unalloc_count +=   $sub_budget->{'budget_amount'};
587                 }
588         }
589         return \@sort;
590 }
591
592 # -------------------------------------------------------------------
593
594 sub AddBudget {
595     my ($budget) = @_;
596         return InsertInTable("aqbudgets",$budget);
597 }
598
599 # -------------------------------------------------------------------
600 sub ModBudget {
601     my ($budget) = @_;
602         return UpdateInTable("aqbudgets",$budget);
603 }
604
605 # -------------------------------------------------------------------
606 sub DelBudget {
607         my ($budget_id) = @_;
608         my $dbh         = C4::Context->dbh;
609         my $sth         = $dbh->prepare("delete from aqbudgets where budget_id=?");
610         my $rc          = $sth->execute($budget_id);
611         return $rc;
612 }
613
614 =back
615
616 =head2 FUNCTIONS ABOUT BUDGETS
617
618 =over 2
619
620 =cut
621
622 =head3 GetBudget
623
624 =over 4
625
626 &GetBudget($budget_id);
627
628 get a specific budget
629
630 =back
631
632 =cut
633
634 # -------------------------------------------------------------------
635 sub GetBudget {
636     my ( $budget_id ) = @_;
637     my $dbh = C4::Context->dbh;
638     my $query = "
639         SELECT *
640         FROM   aqbudgets
641         WHERE  budget_id=?
642         ";
643     my $sth = $dbh->prepare($query);
644     $sth->execute( $budget_id );
645     my $result = $sth->fetchrow_hashref;
646     return $result;
647 }
648
649 =head3 GetBudgets
650
651 =over 4
652
653 &GetBudgets($filter, $order_by);
654
655 gets all budgets
656
657 =back
658
659 =cut
660
661 # -------------------------------------------------------------------
662 sub GetBudgets {
663     my ($filters,$orderby) = @_;
664     return SearchInTable("aqbudgetperiods",$filters, $orderby, undef,undef, undef, "wide");
665 }
666
667 # -------------------------------------------------------------------
668
669 =head3 GetCurrencies
670
671 @currencies = &GetCurrencies;
672
673 Returns the list of all known currencies.
674
675 C<$currencies> is a array; its elements are references-to-hash, whose
676 keys are the fields from the currency table in the Koha database.
677
678 =cut
679
680 sub GetCurrencies {
681     my $dbh   = C4::Context->dbh;
682     my $query = "
683         SELECT *
684         FROM   currency
685     ";
686     my $sth = $dbh->prepare($query);
687     $sth->execute;
688     my @results = ();
689     while ( my $data = $sth->fetchrow_hashref ) {
690         push( @results, $data );
691     }
692     return @results;
693 }
694
695 # -------------------------------------------------------------------
696
697 sub GetCurrency {
698     my $dbh   = C4::Context->dbh;
699     my $query = "
700         SELECT * FROM currency where active = '1'    ";
701     my $sth = $dbh->prepare($query);
702     $sth->execute;
703     my $r = $sth->fetchrow_hashref;
704     return $r;
705 }
706
707 =head3 ModCurrencies
708
709 &ModCurrencies($currency, $newrate);
710
711 Sets the exchange rate for C<$currency> to be C<$newrate>.
712
713 =cut
714
715 sub ModCurrencies {
716     my ( $currency, $rate ) = @_;
717     my $dbh   = C4::Context->dbh;
718     my $query = qq|
719         UPDATE currency
720         SET    rate=?
721         WHERE  currency=? |;
722     my $sth = $dbh->prepare($query);
723     $sth->execute( $rate, $currency );
724 }
725
726 # -------------------------------------------------------------------
727
728 =head3 ConvertCurrency
729
730 $foreignprice = &ConvertCurrency($currency, $localprice);
731
732 Converts the price C<$localprice> to foreign currency C<$currency> by
733 dividing by the exchange rate, and returns the result.
734
735 If no exchange rate is found,e is one
736 to one.
737
738 =cut
739
740 sub ConvertCurrency {
741     my ( $currency, $price ) = @_;
742     my $dbh   = C4::Context->dbh;
743     my $query = "
744         SELECT rate
745         FROM   currency
746         WHERE  currency=?
747     ";
748     my $sth = $dbh->prepare($query);
749     $sth->execute($currency);
750     my $cur = ( $sth->fetchrow_array() )[0];
751     unless ($cur) {
752         $cur = 1;
753     }
754     return ( $price / $cur );
755 }
756
757 =item
758         returns an array containing fieldname followed by PRI as value if PRIMARY Key
759 =cut
760 sub _columns(;$) {
761         my $tablename=shift||"aqbudgets";
762     return @{C4::Context->dbh->selectcol_arrayref("SHOW columns from $tablename",{Columns=>[1,4]})};
763 }
764
765 sub _filter_fields{
766         my $budget=shift;
767         my $tablename=shift;
768     my @keys; 
769         my @values;
770         my %columns= _columns($tablename);
771         #Filter Primary Keys of table
772     my $elements=join "|",grep {$columns{$_} ne "PRI"} keys %columns;
773         foreach my $field (grep {/\b($elements)\b/} keys %$budget){
774                 $$budget{$field}=format_date_in_iso($$budget{$field}) if ($field=~/date/ && $$budget{$field} !~C4::Dates->regexp("iso"));
775                 my $strkeys= " $field = ? ";
776                 if ($field=~/branch/){
777                         $strkeys="( $strkeys OR $field='' OR $field IS NULL) ";
778                 }
779                 push @values, $$budget{$field};
780                 push @keys, $strkeys;
781         }
782         return (\@keys,\@values);
783 }
784
785 END { }    # module clean-up code here (global destructor)
786
787 1;
788 __END__
789
790 =back
791
792 =head1 AUTHOR
793
794 Koha Developement team <info@koha.org>
795
796 =cut