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|
471 SELECT budget_period_id
472 , budget_period_startdate
473 , budget_period_enddate
474 , budget_period_amount
476 , budget_period_description
478 WHERE budget_period_id=? |
480 $sth->execute($budget_period_id);
481 my $data = $sth->fetchrow_hashref;
485 # -------------------------------------------------------------------
486 sub ModBudgetPeriod() {
487 my ($budget_period_id) = @_;
488 my $dbh = C4::Context->dbh
489 ; ## $total = number of records linked to the record that must be deleted my $total = 0;
491 ## get information about the record that will be deleted
492 my $sth = $dbh->prepare("
493 SELECT budget_period_id
494 , budget_period_startdate
495 , budget_period_enddate
496 , budget_period_amount
498 , budget_period_description
500 WHERE budget_period_id=?;"
502 $sth->execute($budget_period_id);
503 my $data = $sth->fetchrow_hashref;
507 # -------------------------------------------------------------------
508 sub GetBudgetHierarchy {
509 my ($budget_period_id, $branchcode, $owner) = @_;
511 my $dbh = C4::Context->dbh;
515 JOIN aqbudgetperiods USING (budget_period_id)
516 WHERE budget_period_active=1 |;
517 # show only period X if requested
518 if ($budget_period_id) {
519 $query .= "AND aqbudgets.budget_period_id = ?";
520 push @bind_params, $budget_period_id;
522 # show only budgets owned by me, my branch or everyone
525 $query .= " AND (budget_owner_id = ? OR budget_branchcode = ? OR (budget_branchcode IS NULL AND budget_owner_id IS NULL))";
526 push @bind_params, $owner;
527 push @bind_params, $branchcode;
529 $query .= ' AND budget_owner_id = ? OR budget_owner_id IS NULL';
530 push @bind_params, $owner;
534 $query .= " AND (budget_branchcode =? or budget_branchcode is NULL)";
535 push @bind_params, $branchcode;
538 my $sth = $dbh->prepare($query);
539 $sth->execute(@bind_params);
540 my $results = $sth->fetchall_arrayref({});
545 foreach my $r (@res) {
548 $r->{depth} = '0' if !defined $r->{budget_parent_id};
549 foreach my $r2 (@res) {
550 if (defined $r2->{budget_parent_id}
551 && $r2->{budget_parent_id} == $r->{budget_id}) {
552 push @child, $r2->{budget_id};
553 $r2->{depth} = ($r->{depth} + 1) if defined $r->{depth};
556 $r->{child} = \@child if scalar @child > 0; # add the child
557 $depth_cnt++ if !defined $r->{'depth'};
559 last if ($depth_cnt == 0 || $i == 100);
563 # look for top parents 1st
565 my ($i, $depth_count) = 0;
568 foreach my $r (@res) {
569 if ($r->{depth} == $depth_count) {
570 $children++ if (ref $r->{child} eq 'ARRAY');
572 # find the parent id element_id and insert it after
575 if ($depth_count > 0) {
578 my $depth = $r->{depth} * 2;
579 my $space = pack "A[$depth]";
580 $r->{budget_code_indent} = $space . $r->{budget_code};
581 $r->{budget_name_indent} = $space . $r->{budget_name};
582 foreach my $r3 (@sort) {
583 if ($r3->{budget_id} == $r->{budget_parent_id}) {
590 $r->{budget_code_indent} = $r->{budget_code};
591 $r->{budget_name_indent} = $r->{budget_name};
594 if (defined $parent) {
595 splice @sort, ($parent + 1), 0, $r;
602 } # --------------foreach
604 last if $children == 0;
607 # add budget-percent and allocation, and flags for html-template
608 foreach my $r (@sort) {
609 my $subs_href = $r->{'child'};
610 my @subs_arr = @$subs_href if defined $subs_href;
612 my $moo = $r->{'budget_code_indent'};
613 $moo =~ s/\ /\ \;/g;
614 $r->{'budget_code_indent'} = $moo;
616 my $moo = $r->{'budget_name_indent'};
617 $moo =~ s/\ /\ \;/g;
618 $r->{'budget_name_indent'} = $moo;
620 $r->{'budget_spent'} = GetBudgetSpent( $r->{'budget_id'} );
622 $r->{'budget_amount_total'} = $r->{'budget_amount'} + $r->{'budget_amount_sublevel'} ;
627 foreach my $sub (@subs_arr) {
628 my $sub_budget = GetBudget($sub);
630 $r->{budget_spent_sublevel} += GetBudgetSpent( $sub_budget->{'budget_id'} );
631 $unalloc_count += $sub_budget->{'budget_amount'} + $sub_budget->{'budget_amount_sublevel'};
634 $r->{budget_unalloc_sublevel} = $r->{'budget_amount_sublevel'} - $unalloc_count;
636 if ( scalar @subs_arr == 0 && $r->{budget_amount_sublevel} > 0 ) {
637 $r->{warn_no_subs} = 1;
643 # -------------------------------------------------------------------
646 my $dbh = C4::Context->dbh;
648 INSERT INTO aqbudgets
650 budget_period_id = ?,
651 budget_parent_id = ?,
653 budget_branchcode = ?,
655 budget_amount_sublevel = ?,
662 budget_permission = ?
664 my $sth = $dbh->prepare($query);
666 $budget->{'budget_code'} ? $budget->{'budget_code'} : undef,
667 $budget->{'budget_period_id'} ? $budget->{'budget_period_id'} : undef,
668 $budget->{'budget_parent_id'} ? $budget->{'budget_parent_id'} : undef,
669 $budget->{'budget_name'} ? $budget->{'budget_name'} : undef,
670 $budget->{'budget_branchcode'} ? $budget->{'budget_branchcode'} : undef,
671 $budget->{'budget_amount'} ? $budget->{'budget_amount'} : undef,
672 $budget->{'budget_amount_sublevel'} ? $budget->{'budget_amount_sublevel'} : undef,
673 $budget->{'budget_encumb'} ? $budget->{'budget_encumb'} : undef,
674 $budget->{'budget_expend'} ? $budget->{'budget_expend'} : undef,
675 $budget->{'budget_notes'} ? $budget->{'budget_notes'} : undef,
676 $budget->{'sort1_authcat'} ? $budget->{'sort1_authcat'} : undef,
677 $budget->{'sort2_authcat'} ? $budget->{'sort2_authcat'} : undef,
678 $budget->{'budget_owner_id'} ? $budget->{'budget_owner_id'} : undef,
679 $budget->{'budget_permission'} ? $budget->{'budget_permission'} : undef,
684 # -------------------------------------------------------------------
687 my $dbh = C4::Context->dbh;
691 budget_period_id = ?,
692 budget_parent_id = ?,
694 budget_branchcode = ?,
696 budget_amount_sublevel = ?,
703 budget_permission = ?
707 my $sth = $dbh->prepare($query);
709 $budget->{'budget_code'} ? $budget->{'budget_code'} : undef,
710 $budget->{'budget_period_id'} ? $budget->{'budget_period_id'} : undef,
711 $budget->{'budget_parent_id'} ? $budget->{'budget_parent_id'} : undef,
712 $budget->{'budget_name'} ? $budget->{'budget_name'} : undef,
713 $budget->{'budget_branchcode'} ? $budget->{'budget_branchcode'} : undef,
714 $budget->{'budget_amount'} ? $budget->{'budget_amount'} : undef,
715 $budget->{'budget_amount_sublevel'} ? $budget->{'budget_amount_sublevel'} : undef,
716 $budget->{'budget_encumb'} ? $budget->{'budget_encumb'} : undef,
717 $budget->{'budget_expend'} ? $budget->{'budget_expend'} : undef,
718 $budget->{'budget_notes'} ? $budget->{'budget_notes'} : undef,
719 $budget->{'sort1_authcat'} ? $budget->{'sort1_authcat'} : undef,
720 $budget->{'sort2_authcat'} ? $budget->{'sort2_authcat'} : undef,
721 $budget->{'budget_owner_id'} ? $budget->{'budget_owner_id'} : undef,
722 $budget->{'budget_permission'} ? $budget->{'budget_permission'} : undef,
723 $budget->{'budget_id'},
728 # -------------------------------------------------------------------
730 my ($budget_id) = @_;
731 my $dbh = C4::Context->dbh;
732 my $sth = $dbh->prepare("delete from aqbudgets where budget_id=?");
733 my $rc = $sth->execute($budget_id);
740 =head2 FUNCTIONS ABOUT BUDGETS
750 &GetBudget($budget_id);
752 get a specific budget
758 # -------------------------------------------------------------------
760 my ( $budget_id ) = @_;
761 my $dbh = C4::Context->dbh;
768 my $sth = $dbh->prepare($query);
769 $sth->execute( $budget_id );
770 my $result = $sth->fetchrow_hashref;
778 &GetBudget($budget_id);
786 # -------------------------------------------------------------------
789 my $dbh = C4::Context->dbh;
790 my $q = "SELECT * from aqbudgets";
794 $sth = $dbh->prepare($q);
797 $q = "select budget_period_id from aqbudgetperiods where budget_period_active = 1 ";
798 $sth = $dbh->prepare($q);
800 $row = $sth->fetchrow_hashref();
801 $q = "select * from aqbudgets WHERE budget_period_id =? ";
802 $sth = $dbh->prepare($q);
803 $sth->execute( $row->{'budget_period_id'} );
805 my $results = $sth->fetchall_arrayref( {} );
810 # -------------------------------------------------------------------
814 @currencies = &GetCurrencies;
816 Returns the list of all known currencies.
818 C<$currencies> is a array; its elements are references-to-hash, whose
819 keys are the fields from the currency table in the Koha database.
824 my $dbh = C4::Context->dbh;
829 my $sth = $dbh->prepare($query);
832 while ( my $data = $sth->fetchrow_hashref ) {
833 push( @results, $data );
839 # -------------------------------------------------------------------
842 my $dbh = C4::Context->dbh;
844 SELECT * FROM currency where active = '1' ";
845 my $sth = $dbh->prepare($query);
847 my $r = $sth->fetchrow_hashref;
854 &ModCurrencies($currency, $newrate);
856 Sets the exchange rate for C<$currency> to be C<$newrate>.
861 my ( $currency, $rate ) = @_;
862 my $dbh = C4::Context->dbh;
867 my $sth = $dbh->prepare($query);
868 $sth->execute( $rate, $currency );
871 # -------------------------------------------------------------------
873 =head3 ConvertCurrency
875 $foreignprice = &ConvertCurrency($currency, $localprice);
877 Converts the price C<$localprice> to foreign currency C<$currency> by
878 dividing by the exchange rate, and returns the result.
880 If no exchange rate is found,e is one
885 sub ConvertCurrency {
886 my ( $currency, $price ) = @_;
887 my $dbh = C4::Context->dbh;
893 my $sth = $dbh->prepare($query);
894 $sth->execute($currency);
895 my $cur = ( $sth->fetchrow_array() )[0];
899 return ( $price / $cur );
902 END { } # module clean-up code here (global destructor)
911 Koha Developement team <info@koha.org>