Merge commit 'biblibre/3.2_community' into to-push
[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                 -id       => 'budget_permission',
322                 -name      => 'budget_permission',
323                 -values    => [ '0', '1', '2' ],
324                 -default   => $perm,
325                 -labels    => \%labels,
326                 -size    => 1,
327         );
328         return $radio;
329 }
330
331 # -------------------------------------------------------------------
332 sub GetBudgetAuthCats  {
333     my ($budget_period_id) = shift;
334     # now, populate the auth_cats_loop used in the budget planning button
335     # we must retrieve all auth values used by at least one budget
336     my $dbh = C4::Context->dbh;
337     my $sth=$dbh->prepare("SELECT sort1_authcat,sort2_authcat FROM aqbudgets WHERE budget_period_id=?");
338     $sth->execute($budget_period_id);
339     my %authcats;
340     while (my ($sort1_authcat,$sort2_authcat) = $sth->fetchrow) {
341         $authcats{$sort1_authcat}=1;
342         $authcats{$sort2_authcat}=1;
343     }
344     my @auth_cats_loop;
345     foreach (sort keys %authcats) {
346         push @auth_cats_loop,{ authcat => $_ };
347     }
348     return \@auth_cats_loop;
349 }
350
351 # -------------------------------------------------------------------
352 sub GetAuthvalueDropbox {
353         my ( $name, $authcat, $default ) = @_;
354         my @authorised_values;
355         my %authorised_lib;
356         my $value;
357         my $dbh = C4::Context->dbh;
358         my $sth = $dbh->prepare(
359                 "SELECT authorised_value,lib
360             FROM authorised_values
361             WHERE category = ?
362             ORDER BY  lib"
363         );
364         $sth->execute( $authcat );
365
366         push @authorised_values, '';
367         while (my ($value, $lib) = $sth->fetchrow_array) {
368                 push @authorised_values, $value;
369                 $authorised_lib{$value} = $lib;
370         }
371
372     return 0 if keys(%authorised_lib) == 0;
373
374     my $budget_authvalue_dropbox = CGI::scrolling_list(
375         -values   => \@authorised_values,
376         -labels   => \%authorised_lib,
377         -default  => $default,
378         -override => 1,
379         -size     => 1,
380         -multiple => 0,
381         -name     => $name,
382         -id       => $name,
383     );
384
385     return $budget_authvalue_dropbox
386 }
387
388 # -------------------------------------------------------------------
389 sub GetBudgetPeriodsDropbox {
390     my ($budget_period_id) = @_;
391         my %labels;
392         my @values;
393         my ($active, $periods) = GetBudgetPeriods();
394         foreach my $r (@$periods) {
395                 $labels{"$r->{budget_period_id}"} = $r->{budget_period_description};
396                 push @values, $r->{budget_period_id};
397         }
398
399         # if no buget_id is passed then its an add
400         my $budget_period_dropbox = CGI::scrolling_list(
401                 -name    => 'budget_period_id',
402                 -values  => \@values,
403                 -default => $budget_period_id ? $budget_period_id :  $active,
404                 -size    => 1,
405                 -labels  => \%labels,
406         );
407         return $budget_period_dropbox;
408 }
409
410 # -------------------------------------------------------------------
411 sub GetBudgetPeriods {
412         my ($filters,$orderby) = @_;
413     return SearchInTable("aqbudgetperiods",$filters, $orderby, undef,undef, undef, "wide");
414 }
415 # -------------------------------------------------------------------
416 sub GetBudgetPeriod {
417         my ($budget_period_id) = @_;
418         my $dbh = C4::Context->dbh;
419         ## $total = number of records linked to the record that must be deleted
420         my $total = 0;
421         ## get information about the record that will be deleted
422         my $sth;
423         if ($budget_period_id) {
424                 $sth = $dbh->prepare( qq|
425               SELECT      *
426                 FROM aqbudgetperiods
427                 WHERE budget_period_id=? |
428                 );
429                 $sth->execute($budget_period_id);
430         } else {         # ACTIVE BUDGET
431                 $sth = $dbh->prepare(qq|
432                           SELECT      *
433                 FROM aqbudgetperiods
434                 WHERE budget_period_active=1 |
435                 );
436                 $sth->execute();
437         }
438         my $data = $sth->fetchrow_hashref;
439         return $data;
440 }
441
442 # -------------------------------------------------------------------
443 sub DelBudgetPeriod{
444         my ($budget_period_id) = @_;
445         my $dbh = C4::Context->dbh;
446           ; ## $total = number of records linked to the record that must be deleted
447     my $total = 0;
448
449         ## get information about the record that will be deleted
450         my $sth = $dbh->prepare(qq|
451                 DELETE 
452          FROM aqbudgetperiods
453          WHERE budget_period_id=? |
454         );
455         return $sth->execute($budget_period_id);
456 }
457
458 # -------------------------------------------------------------------
459 sub ModBudgetPeriod {
460         my ($budget_period_information) = @_;
461         return UpdateInTable("aqbudgetperiods",$budget_period_information);
462 }
463
464 # -------------------------------------------------------------------
465 sub GetBudgetHierarchy {
466         my ($budget_period_id, $branchcode, $owner) = @_;
467         my @bind_params;
468         my $dbh   = C4::Context->dbh;
469         my $query = qq|
470                     SELECT aqbudgets.*
471                     FROM aqbudgets |;
472     # show only period X if requested
473         my @where_strings;
474     if ($budget_period_id) {
475         push @where_strings," aqbudgets.budget_period_id = ?";
476         push @bind_params, $budget_period_id;
477     }
478         # show only budgets owned by me, my branch or everyone
479     if ($owner) {
480         if ($branchcode) {
481             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="")))};
482             push @bind_params, ($owner, $branchcode);
483         } else {
484             push @where_strings, ' (budget_owner_id = ? OR budget_owner_id IS NULL or budget_owner_id ="") ';
485             push @bind_params, $owner;
486         }
487     } else {
488         if ($branchcode) {
489             push @where_strings," (budget_branchcode =? or budget_branchcode is NULL)";
490             push @bind_params, $branchcode;
491         }
492     }
493         $query.=" WHERE ".join(' AND ', @where_strings) if @where_strings;
494         $debug && warn $query,join(",",@bind_params);
495         my $sth = $dbh->prepare($query);
496         $sth->execute(@bind_params);
497         my $results = $sth->fetchall_arrayref({});
498         my @res     = @$results;
499         my $i = 0;
500         while (1) {
501                 my $depth_cnt = 0;
502                 foreach my $r (@res) {
503                         my @child;
504                         # look for children
505                         $r->{depth} = '0' if !defined $r->{budget_parent_id};
506                         foreach my $r2 (@res) {
507                                 if (defined $r2->{budget_parent_id}
508                                         && $r2->{budget_parent_id} == $r->{budget_id}) {
509                                         push @child, $r2->{budget_id};
510                                         $r2->{depth} = ($r->{depth} + 1) if defined $r->{depth};
511                                 }
512                         }
513                         $r->{child} = \@child if scalar @child > 0;    # add the child
514                         $depth_cnt++ if !defined $r->{'depth'};
515                 }
516                 last if ($depth_cnt == 0 || $i == 100);
517                 $i++;
518         }
519
520         # look for top parents 1st
521         my (@sort, $depth_count);
522         ($i, $depth_count) = 0;
523         while (1) {
524                 my $children = 0;
525                 foreach my $r (@res) {
526                         if ($r->{depth} == $depth_count) {
527                                 $children++ if (ref $r->{child} eq 'ARRAY');
528
529                                 # find the parent id element_id and insert it after
530                                 my $i2 = 0;
531                                 my $parent;
532                                 if ($depth_count > 0) {
533
534                                         # add indent
535                                         my $depth = $r->{depth} * 2;
536                                         $r->{budget_code_indent} = $r->{budget_code};
537                                         $r->{budget_name_indent} = $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         $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'};
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'};
588                 }
589         }
590         return \@sort;
591 }
592
593 # -------------------------------------------------------------------
594
595 sub AddBudget {
596     my ($budget) = @_;
597         return InsertInTable("aqbudgets",$budget);
598 }
599
600 # -------------------------------------------------------------------
601 sub ModBudget {
602     my ($budget) = @_;
603         return UpdateInTable("aqbudgets",$budget);
604 }
605
606 # -------------------------------------------------------------------
607 sub DelBudget {
608         my ($budget_id) = @_;
609         my $dbh         = C4::Context->dbh;
610         my $sth         = $dbh->prepare("delete from aqbudgets where budget_id=?");
611         my $rc          = $sth->execute($budget_id);
612         return $rc;
613 }
614
615 =back
616
617 =head2 FUNCTIONS ABOUT BUDGETS
618
619 =over 2
620
621 =cut
622
623 =head3 GetBudget
624
625 =over 4
626
627 &GetBudget($budget_id);
628
629 get a specific budget
630
631 =back
632
633 =cut
634
635 # -------------------------------------------------------------------
636 sub GetBudget {
637     my ( $budget_id ) = @_;
638     my $dbh = C4::Context->dbh;
639     my $query = "
640         SELECT *
641         FROM   aqbudgets
642         WHERE  budget_id=?
643         ";
644     my $sth = $dbh->prepare($query);
645     $sth->execute( $budget_id );
646     my $result = $sth->fetchrow_hashref;
647     return $result;
648 }
649
650 =head3 GetBudgets
651
652 =over 4
653
654 &GetBudgets($filter, $order_by);
655
656 gets all budgets
657
658 =back
659
660 =cut
661
662 # -------------------------------------------------------------------
663 sub GetBudgets {
664     my ($filters,$orderby) = @_;
665     return SearchInTable("aqbudgets",$filters, $orderby, undef,undef, undef, "wide");
666 }
667
668 # -------------------------------------------------------------------
669
670 =head3 GetCurrencies
671
672 @currencies = &GetCurrencies;
673
674 Returns the list of all known currencies.
675
676 C<$currencies> is a array; its elements are references-to-hash, whose
677 keys are the fields from the currency table in the Koha database.
678
679 =cut
680
681 sub GetCurrencies {
682     my $dbh   = C4::Context->dbh;
683     my $query = "
684         SELECT *
685         FROM   currency
686     ";
687     my $sth = $dbh->prepare($query);
688     $sth->execute;
689     my @results = ();
690     while ( my $data = $sth->fetchrow_hashref ) {
691         push( @results, $data );
692     }
693     return @results;
694 }
695
696 # -------------------------------------------------------------------
697
698 sub GetCurrency {
699     my $dbh   = C4::Context->dbh;
700     my $query = "
701         SELECT * FROM currency where active = '1'    ";
702     my $sth = $dbh->prepare($query);
703     $sth->execute;
704     my $r = $sth->fetchrow_hashref;
705     return $r;
706 }
707
708 =head3 ModCurrencies
709
710 &ModCurrencies($currency, $newrate);
711
712 Sets the exchange rate for C<$currency> to be C<$newrate>.
713
714 =cut
715
716 sub ModCurrencies {
717     my ( $currency, $rate ) = @_;
718     my $dbh   = C4::Context->dbh;
719     my $query = qq|
720         UPDATE currency
721         SET    rate=?
722         WHERE  currency=? |;
723     my $sth = $dbh->prepare($query);
724     $sth->execute( $rate, $currency );
725 }
726
727 # -------------------------------------------------------------------
728
729 =head3 ConvertCurrency
730
731 $foreignprice = &ConvertCurrency($currency, $localprice);
732
733 Converts the price C<$localprice> to foreign currency C<$currency> by
734 dividing by the exchange rate, and returns the result.
735
736 If no exchange rate is found,e is one
737 to one.
738
739 =cut
740
741 sub ConvertCurrency {
742     my ( $currency, $price ) = @_;
743     my $dbh   = C4::Context->dbh;
744     my $query = "
745         SELECT rate
746         FROM   currency
747         WHERE  currency=?
748     ";
749     my $sth = $dbh->prepare($query);
750     $sth->execute($currency);
751     my $cur = ( $sth->fetchrow_array() )[0];
752     unless ($cur) {
753         $cur = 1;
754     }
755     return ( $price / $cur );
756 }
757
758 =item
759         returns an array containing fieldname followed by PRI as value if PRIMARY Key
760 =cut
761 sub _columns(;$) {
762         my $tablename=shift||"aqbudgets";
763     return @{C4::Context->dbh->selectcol_arrayref("SHOW columns from $tablename",{Columns=>[1,4]})};
764 }
765
766 sub _filter_fields{
767         my $budget=shift;
768         my $tablename=shift;
769     my @keys; 
770         my @values;
771         my %columns= _columns($tablename);
772         #Filter Primary Keys of table
773     my $elements=join "|",grep {$columns{$_} ne "PRI"} keys %columns;
774         foreach my $field (grep {/\b($elements)\b/} keys %$budget){
775                 $$budget{$field}=format_date_in_iso($$budget{$field}) if ($field=~/date/ && $$budget{$field} !~C4::Dates->regexp("iso"));
776                 my $strkeys= " $field = ? ";
777                 if ($field=~/branch/){
778                         $strkeys="( $strkeys OR $field='' OR $field IS NULL) ";
779                 }
780                 push @values, $$budget{$field};
781                 push @keys, $strkeys;
782         }
783         return (\@keys,\@values);
784 }
785
786 END { }    # module clean-up code here (global destructor)
787
788 1;
789 __END__
790
791 =back
792
793 =head1 AUTHOR
794
795 Koha Developement team <info@koha.org>
796
797 =cut