3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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
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.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #use warnings; FIXME - Bug 2505
23 use C4::Dates qw(format_date format_date_in_iso);
24 use C4::SQLHelper qw<:all>;
27 use vars qw($VERSION @ISA @EXPORT);
30 # set the version for version checking
53 &GetBudgetPeriodsDropbox
70 &CheckBudgetParentPerm
77 # ----------------------------BUDGETS.PM-----------------------------";
80 =head1 FUNCTIONS ABOUT BUDGETS
85 my ( $authcat, @hide_cols ) = @_;
86 my $dbh = C4::Context->dbh;
88 my $sth1 = $dbh->prepare(
90 UPDATE aqbudgets_planning SET display = 0
94 foreach my $authvalue (@hide_cols) {
95 # $sth1->{TraceLevel} = 3;
96 $sth1->execute( $authcat, $authvalue );
101 my ( $authcat, $authvalue ) = @_;
103 my $dbh = C4::Context->dbh;
104 my $sth = $dbh->prepare(
106 SELECT count(display) as cnt from aqbudgets_planning
108 AND authvalue = ? and display = 0 |
111 # $sth->{TraceLevel} = 3;
112 $sth->execute( $authcat, $authvalue );
113 my $res = $sth->fetchrow_hashref;
115 return $res->{cnt} > 0 ? 0: 1
119 sub CheckBudgetParentPerm {
120 my ( $budget, $borrower_id ) = @_;
121 my $depth = $budget->{depth};
122 my $parent_id = $budget->{budget_parent_id};
124 my $parent = GetBudget($parent_id);
125 $parent_id = $parent->{budget_parent_id};
126 if ( $parent->{budget_owner_id} == $borrower_id ) {
134 sub AddBudgetPeriod {
135 my ($budgetperiod) = @_;
136 return InsertInTable("aqbudgetperiods",$budgetperiod);
138 # -------------------------------------------------------------------
139 sub GetPeriodsCount {
140 my $dbh = C4::Context->dbh;
141 my $sth = $dbh->prepare("
142 SELECT COUNT(*) AS sum FROM aqbudgetperiods ");
144 my $res = $sth->fetchrow_hashref;
145 return $res->{'sum'};
148 # -------------------------------------------------------------------
149 sub CheckBudgetParent {
150 my ( $new_parent, $budget ) = @_;
151 my $new_parent_id = $new_parent->{'budget_id'};
152 my $budget_id = $budget->{'budget_id'};
153 my $dbh = C4::Context->dbh;
154 my $parent_id_tmp = $new_parent_id;
156 # check new-parent is not a child (or a child's child ;)
157 my $sth = $dbh->prepare(qq|
158 SELECT budget_parent_id FROM
159 aqbudgets where budget_id = ? | );
161 $sth->execute($parent_id_tmp);
162 my $res = $sth->fetchrow_hashref;
163 if ( $res->{'budget_parent_id'} == $budget_id ) {
166 if ( not defined $res->{'budget_parent_id'} ) {
169 $parent_id_tmp = $res->{'budget_parent_id'};
173 # -------------------------------------------------------------------
174 sub BudgetHasChildren {
175 my ( $budget_id ) = @_;
176 my $dbh = C4::Context->dbh;
177 my $sth = $dbh->prepare(qq|
178 SELECT count(*) as sum FROM aqbudgets
179 WHERE budget_parent_id = ? | );
180 $sth->execute( $budget_id );
181 my $sum = $sth->fetchrow_hashref;
182 return $sum->{'sum'};
185 # -------------------------------------------------------------------
186 sub GetBudgetsPlanCell {
187 my ( $cell, $period, $budget ) = @_;
189 my $dbh = C4::Context->dbh;
190 if ( $cell->{'authcat'} eq 'MONTHS' ) {
191 # get the actual amount
192 $sth = $dbh->prepare( qq|
194 SELECT SUM(ecost) AS actual FROM aqorders
195 WHERE budget_id = ? AND
196 entrydate like "$cell->{'authvalue'}%" |
198 $sth->execute( $cell->{'budget_id'} );
199 } elsif ( $cell->{'authcat'} eq 'BRANCHES' ) {
200 # get the actual amount
201 $sth = $dbh->prepare( qq|
203 SELECT SUM(ecost) FROM aqorders
204 LEFT JOIN aqorders_items
205 ON (aqorders.ordernumber = aqorders_items.ordernumber)
207 ON (aqorders_items.itemnumber = items.itemnumber)
208 WHERE budget_id = ? AND homebranch = ? | );
210 $sth->execute( $cell->{'budget_id'}, $cell->{'authvalue'} );
211 } elsif ( $cell->{'authcat'} eq 'ITEMTYPES' ) {
212 # get the actual amount
213 $sth = $dbh->prepare( qq|
215 SELECT SUM( ecost * quantity) AS actual
216 FROM aqorders JOIN biblioitems
217 ON (biblioitems.biblionumber = aqorders.biblionumber )
218 WHERE aqorders.budget_id = ? and itemtype = ? |
220 $sth->execute( $cell->{'budget_id'},
221 $cell->{'authvalue'} );
223 # ELSE GENERIC ORDERS SORT1/SORT2 STAT COUNT.
225 # get the actual amount
226 $sth = $dbh->prepare( qq|
228 SELECT SUM(ecost * quantity) AS actual
230 JOIN aqbudgets ON (aqbudgets.budget_id = aqorders.budget_id )
231 WHERE aqorders.budget_id = ? AND
232 ((aqbudgets.sort1_authcat = ? AND sort1 =?) OR
233 (aqbudgets.sort2_authcat = ? AND sort2 =?)) |
235 $sth->execute( $cell->{'budget_id'},
236 $budget->{'sort1_authcat'},
237 $cell->{'authvalue'},
238 $budget->{'sort2_authcat'},
242 $actual = $sth->fetchrow_array;
244 # get the estimated amount
245 $sth = $dbh->prepare( qq|
247 SELECT estimated_amount AS estimated, display FROM aqbudgets_planning
248 WHERE budget_period_id = ? AND
253 $sth->execute( $cell->{'budget_period_id'},
254 $cell->{'budget_id'},
255 $cell->{'authvalue'},
260 my $res = $sth->fetchrow_hashref;
261 # my $display = $res->{'display'};
262 my $estimated = $res->{'estimated'};
265 return $actual, $estimated;
268 # -------------------------------------------------------------------
270 my ( $budget_plan, $budget_period_id, $authcat ) = @_;
271 my $dbh = C4::Context->dbh;
272 foreach my $buds (@$budget_plan) {
273 my $lines = $buds->{lines};
274 my $sth = $dbh->prepare( qq|
275 DELETE FROM aqbudgets_planning
276 WHERE budget_period_id = ? AND
280 #delete a aqplan line of cells, then insert new cells,
281 # these could be UPDATES rather than DEL/INSERTS...
282 $sth->execute( $budget_period_id, $lines->[0]{budget_id} , $authcat );
284 foreach my $cell (@$lines) {
285 my $sth = $dbh->prepare( qq|
287 INSERT INTO aqbudgets_planning
289 budget_period_id = ?,
291 estimated_amount = ?,
295 $cell->{'budget_id'},
296 $cell->{'budget_period_id'},
298 $cell->{'estimated_amount'},
299 $cell->{'authvalue'},
305 # -------------------------------------------------------------------
307 my ($budget_id) = @_;
308 my $dbh = C4::Context->dbh;
309 my $sth = $dbh->prepare(qq|
310 SELECT SUM(ecost * quantity) AS sum FROM aqorders
311 WHERE budget_id = ? AND
312 quantityreceived > 0 AND
313 datecancellationprinted IS NULL
316 $sth->execute($budget_id);
317 my $sum = $sth->fetchrow_array;
321 # -------------------------------------------------------------------
322 sub GetBudgetOrdered {
323 my ($budget_id) = @_;
324 my $dbh = C4::Context->dbh;
325 my $sth = $dbh->prepare(qq|
326 SELECT SUM(ecost * quantity) AS sum FROM aqorders
327 WHERE budget_id = ? AND
328 quantityreceived = 0 AND
329 datecancellationprinted IS NULL
332 $sth->execute($budget_id);
333 my $sum = $sth->fetchrow_array;
337 # -------------------------------------------------------------------
338 sub GetBudgetPermDropbox {
341 $labels{'0'} = 'None';
342 $labels{'1'} = 'Owner';
343 $labels{'2'} = 'Library';
344 my $radio = CGI::scrolling_list(
345 -id => 'budget_permission',
346 -name => 'budget_permission',
347 -values => [ '0', '1', '2' ],
355 # -------------------------------------------------------------------
356 sub GetBudgetAuthCats {
357 my ($budget_period_id) = shift;
358 # now, populate the auth_cats_loop used in the budget planning button
359 # we must retrieve all auth values used by at least one budget
360 my $dbh = C4::Context->dbh;
361 my $sth=$dbh->prepare("SELECT sort1_authcat,sort2_authcat FROM aqbudgets WHERE budget_period_id=?");
362 $sth->execute($budget_period_id);
364 while (my ($sort1_authcat,$sort2_authcat) = $sth->fetchrow) {
365 $authcats{$sort1_authcat}=1;
366 $authcats{$sort2_authcat}=1;
369 foreach (sort keys %authcats) {
370 push @auth_cats_loop,{ authcat => $_ };
372 return \@auth_cats_loop;
375 # -------------------------------------------------------------------
376 sub GetAuthvalueDropbox {
377 my ( $name, $authcat, $default ) = @_;
378 my @authorised_values;
381 my $dbh = C4::Context->dbh;
382 my $sth = $dbh->prepare(
383 "SELECT authorised_value,lib
384 FROM authorised_values
388 $sth->execute( $authcat );
390 push @authorised_values, '';
391 while (my ($value, $lib) = $sth->fetchrow_array) {
392 push @authorised_values, $value;
393 $authorised_lib{$value} = $lib;
396 return 0 if keys(%authorised_lib) == 0;
398 my $budget_authvalue_dropbox = CGI::scrolling_list(
399 -values => \@authorised_values,
400 -labels => \%authorised_lib,
401 -default => $default,
409 return $budget_authvalue_dropbox
412 # -------------------------------------------------------------------
413 sub GetBudgetPeriodsDropbox {
414 my ($budget_period_id) = @_;
417 my ($active, $periods) = GetBudgetPeriods();
418 foreach my $r (@$periods) {
419 $labels{"$r->{budget_period_id}"} = $r->{budget_period_description};
420 push @values, $r->{budget_period_id};
423 # if no buget_id is passed then its an add
424 my $budget_period_dropbox = CGI::scrolling_list(
425 -name => 'budget_period_id',
427 -default => $budget_period_id ? $budget_period_id : $active,
431 return $budget_period_dropbox;
434 # -------------------------------------------------------------------
435 sub GetBudgetPeriods {
436 my ($filters,$orderby) = @_;
437 return SearchInTable("aqbudgetperiods",$filters, $orderby, undef,undef, undef, "wide");
439 # -------------------------------------------------------------------
440 sub GetBudgetPeriod {
441 my ($budget_period_id) = @_;
442 my $dbh = C4::Context->dbh;
443 ## $total = number of records linked to the record that must be deleted
445 ## get information about the record that will be deleted
447 if ($budget_period_id) {
448 $sth = $dbh->prepare( qq|
451 WHERE budget_period_id=? |
453 $sth->execute($budget_period_id);
454 } else { # ACTIVE BUDGET
455 $sth = $dbh->prepare(qq|
458 WHERE budget_period_active=1 |
462 my $data = $sth->fetchrow_hashref;
466 # -------------------------------------------------------------------
468 my ($budget_period_id) = @_;
469 my $dbh = C4::Context->dbh;
470 ; ## $total = number of records linked to the record that must be deleted
473 ## get information about the record that will be deleted
474 my $sth = $dbh->prepare(qq|
477 WHERE budget_period_id=? |
479 return $sth->execute($budget_period_id);
482 # -------------------------------------------------------------------
483 sub ModBudgetPeriod {
484 my ($budget_period_information) = @_;
485 return UpdateInTable("aqbudgetperiods",$budget_period_information);
488 # -------------------------------------------------------------------
489 sub GetBudgetHierarchy {
490 my ($budget_period_id, $branchcode, $owner) = @_;
492 my $dbh = C4::Context->dbh;
496 LEFT JOIN aqbudgetperiods
497 ON aqbudgetperiods.budget_period_id=aqbudgets.budget_period_id |;
499 # Pick out the active ones
500 push @where_strings, 'aqbudgetperiods.budget_period_active=1';
501 # show only period X if requested
502 if ($budget_period_id) {
503 push @where_strings," aqbudgets.budget_period_id = ?";
504 push @bind_params, $budget_period_id;
506 # show only budgets owned by me, my branch or everyone
510 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="")))};
511 push @bind_params, ( $owner, $branchcode );
513 push @where_strings, ' (budget_owner_id = ? OR budget_owner_id IS NULL or budget_owner_id ="") ';
514 push @bind_params, $owner;
518 push @where_strings," (budget_branchcode =? or budget_branchcode is NULL)";
519 push @bind_params, $branchcode;
522 $query.=" WHERE ".join(' AND ', @where_strings) if @where_strings;
523 $debug && warn $query,join(",",@bind_params);
524 my $sth = $dbh->prepare($query);
525 $sth->execute(@bind_params);
526 my $results = $sth->fetchall_arrayref({});
531 foreach my $r (@res) {
534 $r->{depth} = '0' if !defined $r->{budget_parent_id};
535 foreach my $r2 (@res) {
536 if (defined $r2->{budget_parent_id}
537 && $r2->{budget_parent_id} == $r->{budget_id}) {
538 push @child, $r2->{budget_id};
539 $r2->{depth} = ($r->{depth} + 1) if defined $r->{depth};
542 $r->{child} = \@child if scalar @child > 0; # add the child
543 $depth_cnt++ if !defined $r->{'depth'};
545 last if ($depth_cnt == 0 || $i == 100);
549 # look for top parents 1st
550 my (@sort, $depth_count);
551 ($i, $depth_count) = 0;
554 foreach my $r (@res) {
555 if ($r->{depth} == $depth_count) {
556 $children++ if (ref $r->{child} eq 'ARRAY');
558 # find the parent id element_id and insert it after
561 if ($depth_count > 0) {
564 my $depth = $r->{depth} * 2;
565 $r->{budget_code_indent} = $r->{budget_code};
566 $r->{budget_name_indent} = $r->{budget_name};
567 foreach my $r3 (@sort) {
568 if ($r3->{budget_id} == $r->{budget_parent_id}) {
575 $r->{budget_code_indent} = $r->{budget_code};
576 $r->{budget_name_indent} = $r->{budget_name};
579 if (defined $parent) {
580 splice @sort, ($parent + 1), 0, $r;
587 } # --------------foreach
589 last if $children == 0;
592 # add budget-percent and allocation, and flags for html-template
593 foreach my $r (@sort) {
594 my $subs_href = $r->{'child'};
595 my @subs_arr = @$subs_href if defined $subs_href;
597 my $moo = $r->{'budget_code_indent'};
598 $moo =~ s/\ /\ \;/g;
599 $r->{'budget_code_indent'} = $moo;
601 $moo = $r->{'budget_name_indent'};
602 $moo =~ s/\ /\ \;/g;
603 $r->{'budget_name_indent'} = $moo;
605 $r->{'budget_spent'} = GetBudgetSpent( $r->{'budget_id'} );
607 $r->{'budget_amount_total'} = $r->{'budget_amount'};
612 foreach my $sub (@subs_arr) {
613 my $sub_budget = GetBudget($sub);
615 $r->{budget_spent_sublevel} += GetBudgetSpent( $sub_budget->{'budget_id'} );
616 $unalloc_count += $sub_budget->{'budget_amount'};
622 # -------------------------------------------------------------------
626 return InsertInTable("aqbudgets",$budget);
629 # -------------------------------------------------------------------
632 return UpdateInTable("aqbudgets",$budget);
635 # -------------------------------------------------------------------
637 my ($budget_id) = @_;
638 my $dbh = C4::Context->dbh;
639 my $sth = $dbh->prepare("delete from aqbudgets where budget_id=?");
640 my $rc = $sth->execute($budget_id);
647 &GetBudget($budget_id);
649 get a specific budget
653 # -------------------------------------------------------------------
655 my ( $budget_id ) = @_;
656 my $dbh = C4::Context->dbh;
662 my $sth = $dbh->prepare($query);
663 $sth->execute( $budget_id );
664 my $result = $sth->fetchrow_hashref;
670 &GetBudgets($filter, $order_by);
676 # -------------------------------------------------------------------
677 sub GetChildBudgetsSpent {
678 my ( $budget_id ) = @_;
679 my $dbh = C4::Context->dbh;
683 WHERE budget_parent_id=?
685 my $sth = $dbh->prepare($query);
686 $sth->execute( $budget_id );
687 my $result = $sth->fetchall_arrayref({});
688 my $total_spent = GetBudgetSpent($budget_id);
690 $total_spent += GetChildBudgetsSpent($_->{"budget_id"}) foreach @$result;
695 =head2 GetChildBudgetsSpent
697 &GetChildBudgetsSpent($budget-id);
699 gets the total spent of the level and sublevels of $budget_id
703 # -------------------------------------------------------------------
705 my ($filters,$orderby) = @_;
706 return SearchInTable("aqbudgets",$filters, $orderby, undef,undef, undef, "wide");
709 # -------------------------------------------------------------------
713 @currencies = &GetCurrencies;
715 Returns the list of all known currencies.
717 C<$currencies> is a array; its elements are references-to-hash, whose
718 keys are the fields from the currency table in the Koha database.
723 my $dbh = C4::Context->dbh;
728 my $sth = $dbh->prepare($query);
731 while ( my $data = $sth->fetchrow_hashref ) {
732 push( @results, $data );
737 # -------------------------------------------------------------------
740 my $dbh = C4::Context->dbh;
742 SELECT * FROM currency where active = '1' ";
743 my $sth = $dbh->prepare($query);
745 my $r = $sth->fetchrow_hashref;
751 &ModCurrencies($currency, $newrate);
753 Sets the exchange rate for C<$currency> to be C<$newrate>.
758 my ( $currency, $rate ) = @_;
759 my $dbh = C4::Context->dbh;
764 my $sth = $dbh->prepare($query);
765 $sth->execute( $rate, $currency );
768 # -------------------------------------------------------------------
770 =head2 ConvertCurrency
772 $foreignprice = &ConvertCurrency($currency, $localprice);
774 Converts the price C<$localprice> to foreign currency C<$currency> by
775 dividing by the exchange rate, and returns the result.
777 If no exchange rate is found, e is one to one.
781 sub ConvertCurrency {
782 my ( $currency, $price ) = @_;
783 my $dbh = C4::Context->dbh;
789 my $sth = $dbh->prepare($query);
790 $sth->execute($currency);
791 my $cur = ( $sth->fetchrow_array() )[0];
795 return ( $price / $cur );
800 returns an array containing fieldname followed by PRI as value if PRIMARY Key
805 my $tablename=shift||"aqbudgets";
806 return @{C4::Context->dbh->selectcol_arrayref("SHOW columns from $tablename",{Columns=>[1,4]})};
814 my %columns= _columns($tablename);
815 #Filter Primary Keys of table
816 my $elements=join "|",grep {$columns{$_} ne "PRI"} keys %columns;
817 foreach my $field (grep {/\b($elements)\b/} keys %$budget){
818 $$budget{$field}=format_date_in_iso($$budget{$field}) if ($field=~/date/ && $$budget{$field} !~C4::Dates->regexp("iso"));
819 my $strkeys= " $field = ? ";
820 if ($field=~/branch/){
821 $strkeys="( $strkeys OR $field='' OR $field IS NULL) ";
823 push @values, $$budget{$field};
824 push @keys, $strkeys;
826 return (\@keys,\@values);
829 END { } # module clean-up code here (global destructor)
836 Koha Development Team <http://koha-community.org/>