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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
22 use C4::Dates qw(format_date format_date_in_iso);
25 use vars qw($VERSION @ISA @EXPORT);
28 # set the version for version checking
48 &GetBudgetPeriodsDropbox
63 &CheckBudgetParentPerm
70 # ----------------------------BUDGETS.PM-----------------------------";
74 my ( $authcat, @hide_cols ) = @_;
75 my $dbh = C4::Context->dbh;
78 my $sth = $dbh->prepare(
80 UPDATE aqbudgets_planning
81 SET display = 1 where authcat = ? |
83 $sth->execute( $authcat );
86 my $sth1 = $dbh->prepare(
88 UPDATE aqbudgets_planning SET display = 0
92 foreach my $authvalue (@hide_cols) {
93 # $sth1->{TraceLevel} = 3;
94 $sth1->execute( $authcat, $authvalue );
99 my ( $authcat, $authvalue ) = @_;
101 my $dbh = C4::Context->dbh;
102 my $sth = $dbh->prepare(
104 SELECT count(display) as cnt from aqbudgets_planning
106 AND authvalue = ? and display = 0 |
109 # $sth->{TraceLevel} = 3;
110 $sth->execute( $authcat, $authvalue );
111 my $res = $sth->fetchrow_hashref;
113 return $res->{cnt} > 0 ? 0: 1
117 sub CheckBudgetParentPerm {
118 my ( $budget, $borrower_id ) = @_;
119 my $depth = $budget->{depth};
120 my $parent_id = $budget->{budget_parent_id};
122 my $parent = GetBudget($parent_id);
123 $parent_id = $parent->{budget_parent_id};
124 if ( $parent->{budget_owner_id} == $borrower_id ) {
132 # -------------------------------------------------------------------
133 sub GetPeriodsCount {
134 my $dbh = C4::Context->dbh;
135 my $sth = $dbh->prepare("
136 SELECT COUNT(*) AS sum FROM aqbudgetperiods ");
138 my $res = $sth->fetchrow_hashref;
139 return $res->{'sum'};
142 # -------------------------------------------------------------------
143 sub CheckBudgetParent {
144 my ( $new_parent, $budget ) = @_;
145 my $new_parent_id = $new_parent->{'budget_id'};
146 my $budget_id = $budget->{'budget_id'};
147 my $dbh = C4::Context->dbh;
148 my $parent_id_tmp = $new_parent_id;
150 # check new-parent is not a child (or a child's child ;)
151 my $sth = $dbh->prepare(qq|
152 SELECT budget_parent_id FROM
153 aqbudgets where budget_id = ? | );
155 $sth->execute($parent_id_tmp);
156 my $res = $sth->fetchrow_hashref;
157 if ( $res->{'budget_parent_id'} == $budget_id ) {
160 if ( not defined $res->{'budget_parent_id'} ) {
163 $parent_id_tmp = $res->{'budget_parent_id'};
167 # -------------------------------------------------------------------
168 sub BudgetHasChildren {
169 my ( $budget_id ) = @_;
170 my $dbh = C4::Context->dbh;
171 my $sth = $dbh->prepare(qq|
172 SELECT count(*) as sum FROM aqbudgets
173 WHERE budget_parent_id = ? | );
174 $sth->execute( $budget_id );
175 my $sum = $sth->fetchrow_hashref;
177 return $sum->{'sum'};
180 # -------------------------------------------------------------------
181 sub GetBudgetsPlanCell {
182 my ( $cell, $period, $budget ) = @_;
184 my $dbh = C4::Context->dbh;
185 if ( $cell->{'authcat'} eq 'MONTHS' ) {
186 # get the actual amount
187 $sth = $dbh->prepare( qq|
189 SELECT SUM(ecost) AS actual FROM aqorders
190 WHERE budget_id = ? AND
191 entrydate like "$cell->{'authvalue'}%" |
193 $sth->execute( $cell->{'budget_id'} );
194 } elsif ( $cell->{'authcat'} eq 'BRANCHES' ) {
195 # get the actual amount
196 $sth = $dbh->prepare( qq|
198 SELECT SUM(ecost) FROM aqorders
199 LEFT JOIN aqorders_items
200 ON (aqorders.ordernumber = aqorders_items.ordernumber)
202 ON (aqorders_items.itemnumber = items.itemnumber)
203 WHERE budget_id = ? AND homebranch = ? | );
205 $sth->execute( $cell->{'budget_id'}, $cell->{'authvalue'} );
206 } elsif ( $cell->{'authcat'} eq 'ITEMTYPES' ) {
207 # get the actual amount
208 $sth = $dbh->prepare( qq|
210 SELECT SUM( ecost * quantity) AS actual
211 FROM aqorders JOIN biblioitems
212 ON (biblioitems.biblionumber = aqorders.biblionumber )
213 WHERE aqorders.budget_id = ? and itemtype = ? |
215 $sth->execute( $cell->{'budget_id'},
216 $cell->{'authvalue'} );
218 # ELSE GENERIC ORDERS SORT1/SORT2 STAT COUNT.
220 # get the actual amount
221 $sth = $dbh->prepare( qq|
223 SELECT SUM(ecost * quantity) AS actual
225 JOIN aqbudgets ON (aqbudgets.budget_id = aqorders.budget_id )
226 WHERE aqorders.budget_id = ? AND
227 ((aqbudgets.sort1_authcat = ? AND sort1 =?) OR
228 (aqbudgets.sort2_authcat = ? AND sort2 =?)) |
230 $sth->{TraceLevel} = 2;
231 $sth->execute( $cell->{'budget_id'},
232 $budget->{'sort1_authcat'},
233 $cell->{'authvalue'},
234 $budget->{'sort2_authcat'},
238 $actual = $sth->fetchrow_array;
240 # get the estimated amount
241 my $sth = $dbh->prepare( qq|
243 SELECT estimated_amount AS estimated, display FROM aqbudgets_planning
244 WHERE budget_period_id = ? AND
249 $sth->execute( $cell->{'budget_period_id'},
250 $cell->{'budget_id'},
251 $cell->{'authvalue'},
256 my $res = $sth->fetchrow_hashref;
257 # my $display = $res->{'display'};
258 my $estimated = $res->{'estimated'};
261 return $actual, $estimated;
264 # -------------------------------------------------------------------
266 my ( $budget_plan, $budget_period_id, $authcat ) = @_;
267 my $dbh = C4::Context->dbh;
268 foreach my $buds (@$budget_plan) {
269 my $lines = $buds->{lines};
270 my $sth = $dbh->prepare( qq|
271 DELETE FROM aqbudgets_planning
272 WHERE budget_period_id = ? AND
276 #delete a aqplan line of cells, then insert new cells,
277 # these could be UPDATES rather than DEL/INSERTS...
278 $sth->execute( $budget_period_id, $lines->[0]{budget_id} , $authcat );
280 foreach my $cell (@$lines) {
281 my $sth = $dbh->prepare( qq|
283 INSERT INTO aqbudgets_planning
285 budget_period_id = ?,
287 estimated_amount = ?,
291 $cell->{'budget_id'},
292 $cell->{'budget_period_id'},
294 $cell->{'estimated_amount'},
295 $cell->{'authvalue'},
301 # -------------------------------------------------------------------
303 my ($budget_id) = @_;
304 my $dbh = C4::Context->dbh;
305 my $sth = $dbh->prepare(qq|
306 SELECT SUM(ecost * quantity ) AS sum FROM aqorders
307 WHERE budget_id = ? AND
308 datecancellationprinted IS NULL
311 $sth->execute($budget_id);
312 my $sum = $sth->fetchrow_array;
313 # $sum = sprintf "%.2f", $sum;
317 # -------------------------------------------------------------------
318 sub GetBudgetPermDropbox {
321 $labels{'0'} = 'None';
322 $labels{'1'} = 'Owner';
323 $labels{'2'} = 'Library';
324 my $radio = CGI::scrolling_list(
325 -name => 'budget_permission',
326 -values => [ '0', '1', '2' ],
334 # -------------------------------------------------------------------
335 sub GetBudgetAuthCats {
336 my ($budget_period_id) = shift;
337 # now, populate the auth_cats_loop used in the budget planning button
338 # we must retrieve all auth values used by at least one budget
339 my $dbh = C4::Context->dbh;
340 my $sth=$dbh->prepare("SELECT sort1_authcat,sort2_authcat FROM aqbudgets WHERE budget_period_id=?");
341 $sth->execute($budget_period_id);
343 while (my ($sort1_authcat,$sort2_authcat) = $sth->fetchrow) {
344 $authcats{$sort1_authcat}=1;
345 $authcats{$sort2_authcat}=1;
348 foreach (sort keys %authcats) {
349 push @auth_cats_loop,{ authcat => $_ };
351 return \@auth_cats_loop;
354 # -------------------------------------------------------------------
355 sub GetAuthvalueDropbox {
356 my ( $name, $authcat, $default ) = @_;
357 my @authorised_values;
360 my $dbh = C4::Context->dbh;
361 my $sth = $dbh->prepare(
362 "SELECT authorised_value,lib
363 FROM authorised_values
367 $sth->execute( $authcat );
369 push @authorised_values, '';
370 while (my ($value, $lib) = $sth->fetchrow_array) {
371 push @authorised_values, $value;
372 $authorised_lib{$value} = $lib;
375 return 0 if keys(%authorised_lib) == 0;
377 my $budget_authvalue_dropbox = CGI::scrolling_list(
378 -values => \@authorised_values,
379 -labels => \%authorised_lib,
380 -default => $default,
388 return $budget_authvalue_dropbox
391 # -------------------------------------------------------------------
392 sub GetBudgetPeriodsDropbox {
393 my ($budget_period_id) = @_;
396 my ($active, $periods) = GetBudgetPeriods();
397 foreach my $r (@$periods) {
398 $labels{"$r->{budget_period_id}"} = $r->{budget_period_description};
399 push @values, $r->{budget_period_id};
402 # if no buget_id is passed then its an add
403 my $budget_period_dropbox = CGI::scrolling_list(
404 -name => 'budget_period_id',
406 -default => $budget_period_id ? $budget_period_id : $active,
410 return $budget_period_dropbox;
413 # -------------------------------------------------------------------
414 sub GetBudgetPeriods {
415 my $dbh = C4::Context->dbh;
416 my $sth = $dbh->prepare(qq|
419 ORDER BY budget_period_startdate, budget_period_enddate |
424 while (my $data = $sth->fetchrow_hashref) {
425 if ($data->{'budget_period_active'} == 1) {
426 $active = $data->{'budget_period_id'};
428 push(@results, $data);
431 return ($active, \@results);
434 # -------------------------------------------------------------------
435 sub GetBudgetPeriod {
436 my ($budget_period_id) = @_;
437 my $dbh = C4::Context->dbh;
438 ## $total = number of records linked to the record that must be deleted
440 ## get information about the record that will be deleted
442 if ($budget_period_id gt 0) {
443 $sth = $dbh->prepare( qq|
446 WHERE budget_period_id=? |
448 $sth->execute($budget_period_id);
449 } else { # ACTIVE BUDGET
450 $sth = $dbh->prepare(qq|
453 WHERE budget_period_active=1 |
457 my $data = $sth->fetchrow_hashref;
462 # -------------------------------------------------------------------
463 sub DelBudgetPeriod() {
464 my ($budget_period_id) = @_;
465 my $dbh = C4::Context->dbh;
466 ; ## $total = number of records linked to the record that must be deleted
469 ## get information about the record that will be deleted
470 my $sth = $dbh->prepare(qq|
473 WHERE budget_period_id=? |
475 return $sth->execute($budget_period_id);
478 # -------------------------------------------------------------------
479 sub ModBudgetPeriod() {
480 my ($budget_period_information) = @_;
481 my $dbh = C4::Context->dbh ; ## $total = number of records linked to the record that must be deleted my $total = 0;
483 ## get information about the record that will be deleted
484 my $budget_period_id=$$budget_period_information{'budget_period_id'};
485 delete $$budget_period_information{'budget_period_id'};
489 while ( my ($k,$v) = each %$budget_period_information ) {
490 next if (not $v and $k!~/sort1|note/);
491 #next if any { $_ eq $k } qw( sort1 note);
496 my $query = do { local $"=',';
498 UPDATE aqbudgetperiods
500 WHERE budget_period_id=?
504 my $sth=$dbh->prepare($query);
505 my $data = $sth->execute(@values,$budget_period_id);
509 # -------------------------------------------------------------------
510 sub GetBudgetHierarchy {
511 my ($budget_period_id, $branchcode, $owner) = @_;
513 my $dbh = C4::Context->dbh;
517 JOIN aqbudgetperiods USING (budget_period_id)
518 WHERE budget_period_active=1 |;
519 # show only period X if requested
520 if ($budget_period_id) {
521 $query .= "AND aqbudgets.budget_period_id = ?";
522 push @bind_params, $budget_period_id;
524 # show only budgets owned by me, my branch or everyone
527 $query .= " AND (budget_owner_id = ? OR budget_branchcode = ? OR (budget_branchcode IS NULL AND budget_owner_id IS NULL))";
528 push @bind_params, $owner;
529 push @bind_params, $branchcode;
531 $query .= ' AND budget_owner_id = ? OR budget_owner_id IS NULL';
532 push @bind_params, $owner;
536 $query .= " AND (budget_branchcode =? or budget_branchcode is NULL)";
537 push @bind_params, $branchcode;
540 my $sth = $dbh->prepare($query);
541 $sth->execute(@bind_params);
542 my $results = $sth->fetchall_arrayref({});
547 foreach my $r (@res) {
550 $r->{depth} = '0' if !defined $r->{budget_parent_id};
551 foreach my $r2 (@res) {
552 if (defined $r2->{budget_parent_id}
553 && $r2->{budget_parent_id} == $r->{budget_id}) {
554 push @child, $r2->{budget_id};
555 $r2->{depth} = ($r->{depth} + 1) if defined $r->{depth};
558 $r->{child} = \@child if scalar @child > 0; # add the child
559 $depth_cnt++ if !defined $r->{'depth'};
561 last if ($depth_cnt == 0 || $i == 100);
565 # look for top parents 1st
567 my ($i, $depth_count) = 0;
570 foreach my $r (@res) {
571 if ($r->{depth} == $depth_count) {
572 $children++ if (ref $r->{child} eq 'ARRAY');
574 # find the parent id element_id and insert it after
577 if ($depth_count > 0) {
580 my $depth = $r->{depth} * 2;
581 my $space = pack "A[$depth]";
582 $r->{budget_code_indent} = $space . $r->{budget_code};
583 $r->{budget_name_indent} = $space . $r->{budget_name};
584 foreach my $r3 (@sort) {
585 if ($r3->{budget_id} == $r->{budget_parent_id}) {
592 $r->{budget_code_indent} = $r->{budget_code};
593 $r->{budget_name_indent} = $r->{budget_name};
596 if (defined $parent) {
597 splice @sort, ($parent + 1), 0, $r;
604 } # --------------foreach
606 last if $children == 0;
609 # add budget-percent and allocation, and flags for html-template
610 foreach my $r (@sort) {
611 my $subs_href = $r->{'child'};
612 my @subs_arr = @$subs_href if defined $subs_href;
614 my $moo = $r->{'budget_code_indent'};
615 $moo =~ s/\ /\ \;/g;
616 $r->{'budget_code_indent'} = $moo;
618 my $moo = $r->{'budget_name_indent'};
619 $moo =~ s/\ /\ \;/g;
620 $r->{'budget_name_indent'} = $moo;
622 $r->{'budget_spent'} = GetBudgetSpent( $r->{'budget_id'} );
624 $r->{'budget_amount_total'} = $r->{'budget_amount'} + $r->{'budget_amount_sublevel'} ;
629 foreach my $sub (@subs_arr) {
630 my $sub_budget = GetBudget($sub);
632 $r->{budget_spent_sublevel} += GetBudgetSpent( $sub_budget->{'budget_id'} );
633 $unalloc_count += $sub_budget->{'budget_amount'} + $sub_budget->{'budget_amount_sublevel'};
636 $r->{budget_unalloc_sublevel} = $r->{'budget_amount_sublevel'} - $unalloc_count;
638 if ( scalar @subs_arr == 0 && $r->{budget_amount_sublevel} > 0 ) {
639 $r->{warn_no_subs} = 1;
645 # -------------------------------------------------------------------
648 my $dbh = C4::Context->dbh;
649 my @keys; my @values;
651 while ( my ($k,$v) = each %$budget ) {
654 push @keys , "$k = ?";
660 INSERT INTO aqbudgets
666 my $sth = $dbh->prepare($query);
667 $sth->execute(@values);
668 return $dbh->{'mysql_insertid'};
671 # -------------------------------------------------------------------
674 my $dbh = C4::Context->dbh;
675 my $budgetid=$$budget{'budgetid'};
676 delete $$budget{'budget_id'};
681 while ( my ($k,$v) = each %$budget ) {
682 next if (not $v and $k!~/sort1|note/);
683 #next if any { $_ eq $k } qw( sort1 note);
688 my $query = do { local $"=',';
696 my $sth = $dbh->prepare($query);
697 $sth->execute( @values,$budgetid);
700 # -------------------------------------------------------------------
702 my ($budget_id) = @_;
703 my $dbh = C4::Context->dbh;
704 my $sth = $dbh->prepare("delete from aqbudgets where budget_id=?");
705 my $rc = $sth->execute($budget_id);
712 =head2 FUNCTIONS ABOUT BUDGETS
722 &GetBudget($budget_id);
724 get a specific budget
730 # -------------------------------------------------------------------
732 my ( $budget_id ) = @_;
733 my $dbh = C4::Context->dbh;
740 my $sth = $dbh->prepare($query);
741 $sth->execute( $budget_id );
742 my $result = $sth->fetchrow_hashref;
750 &GetBudget($budget_id);
758 # -------------------------------------------------------------------
761 my $dbh = C4::Context->dbh;
762 my $q = "SELECT * from aqbudgets";
766 $sth = $dbh->prepare($q);
769 $q = "select budget_period_id from aqbudgetperiods where budget_period_active = 1 ";
770 $sth = $dbh->prepare($q);
772 $row = $sth->fetchrow_hashref();
773 $q = "select * from aqbudgets WHERE budget_period_id =? ";
774 $sth = $dbh->prepare($q);
775 $sth->execute( $row->{'budget_period_id'} );
777 my $results = $sth->fetchall_arrayref( {} );
782 # -------------------------------------------------------------------
786 @currencies = &GetCurrencies;
788 Returns the list of all known currencies.
790 C<$currencies> is a array; its elements are references-to-hash, whose
791 keys are the fields from the currency table in the Koha database.
796 my $dbh = C4::Context->dbh;
801 my $sth = $dbh->prepare($query);
804 while ( my $data = $sth->fetchrow_hashref ) {
805 push( @results, $data );
811 # -------------------------------------------------------------------
814 my $dbh = C4::Context->dbh;
816 SELECT * FROM currency where active = '1' ";
817 my $sth = $dbh->prepare($query);
819 my $r = $sth->fetchrow_hashref;
826 &ModCurrencies($currency, $newrate);
828 Sets the exchange rate for C<$currency> to be C<$newrate>.
833 my ( $currency, $rate ) = @_;
834 my $dbh = C4::Context->dbh;
839 my $sth = $dbh->prepare($query);
840 $sth->execute( $rate, $currency );
843 # -------------------------------------------------------------------
845 =head3 ConvertCurrency
847 $foreignprice = &ConvertCurrency($currency, $localprice);
849 Converts the price C<$localprice> to foreign currency C<$currency> by
850 dividing by the exchange rate, and returns the result.
852 If no exchange rate is found,e is one
857 sub ConvertCurrency {
858 my ( $currency, $price ) = @_;
859 my $dbh = C4::Context->dbh;
865 my $sth = $dbh->prepare($query);
866 $sth->execute($currency);
867 my $cur = ( $sth->fetchrow_array() )[0];
871 return ( $price / $cur );
874 END { } # module clean-up code here (global destructor)
883 Koha Developement team <info@koha.org>