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);
23 use C4::SQLHelper qw<:all>;
26 use vars qw($VERSION @ISA @EXPORT);
29 # set the version for version checking
50 &GetBudgetPeriodsDropbox
67 &CheckBudgetParentPerm
74 # ----------------------------BUDGETS.PM-----------------------------";
78 my ( $authcat, @hide_cols ) = @_;
79 my $dbh = C4::Context->dbh;
81 my $sth1 = $dbh->prepare(
83 UPDATE aqbudgets_planning SET display = 0
87 foreach my $authvalue (@hide_cols) {
88 # $sth1->{TraceLevel} = 3;
89 $sth1->execute( $authcat, $authvalue );
94 my ( $authcat, $authvalue ) = @_;
96 my $dbh = C4::Context->dbh;
97 my $sth = $dbh->prepare(
99 SELECT count(display) as cnt from aqbudgets_planning
101 AND authvalue = ? and display = 0 |
104 # $sth->{TraceLevel} = 3;
105 $sth->execute( $authcat, $authvalue );
106 my $res = $sth->fetchrow_hashref;
108 return $res->{cnt} > 0 ? 0: 1
112 sub CheckBudgetParentPerm {
113 my ( $budget, $borrower_id ) = @_;
114 my $depth = $budget->{depth};
115 my $parent_id = $budget->{budget_parent_id};
117 my $parent = GetBudget($parent_id);
118 $parent_id = $parent->{budget_parent_id};
119 if ( $parent->{budget_owner_id} == $borrower_id ) {
127 sub AddBudgetPeriod {
128 my ($budgetperiod) = @_;
129 return InsertInTable("aqbudgetperiods",$budgetperiod);
131 # -------------------------------------------------------------------
132 sub GetPeriodsCount {
133 my $dbh = C4::Context->dbh;
134 my $sth = $dbh->prepare("
135 SELECT COUNT(*) AS sum FROM aqbudgetperiods ");
137 my $res = $sth->fetchrow_hashref;
138 return $res->{'sum'};
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;
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 = ? | );
154 $sth->execute($parent_id_tmp);
155 my $res = $sth->fetchrow_hashref;
156 if ( $res->{'budget_parent_id'} == $budget_id ) {
159 if ( not defined $res->{'budget_parent_id'} ) {
162 $parent_id_tmp = $res->{'budget_parent_id'};
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'};
178 # -------------------------------------------------------------------
179 sub GetBudgetsPlanCell {
180 my ( $cell, $period, $budget ) = @_;
182 my $dbh = C4::Context->dbh;
183 if ( $cell->{'authcat'} eq 'MONTHS' ) {
184 # get the actual amount
185 $sth = $dbh->prepare( qq|
187 SELECT SUM(ecost) AS actual FROM aqorders
188 WHERE budget_id = ? AND
189 entrydate like "$cell->{'authvalue'}%" |
191 $sth->execute( $cell->{'budget_id'} );
192 } elsif ( $cell->{'authcat'} eq 'BRANCHES' ) {
193 # get the actual amount
194 $sth = $dbh->prepare( qq|
196 SELECT SUM(ecost) FROM aqorders
197 LEFT JOIN aqorders_items
198 ON (aqorders.ordernumber = aqorders_items.ordernumber)
200 ON (aqorders_items.itemnumber = items.itemnumber)
201 WHERE budget_id = ? AND homebranch = ? | );
203 $sth->execute( $cell->{'budget_id'}, $cell->{'authvalue'} );
204 } elsif ( $cell->{'authcat'} eq 'ITEMTYPES' ) {
205 # get the actual amount
206 $sth = $dbh->prepare( qq|
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 = ? |
213 $sth->execute( $cell->{'budget_id'},
214 $cell->{'authvalue'} );
216 # ELSE GENERIC ORDERS SORT1/SORT2 STAT COUNT.
218 # get the actual amount
219 $sth = $dbh->prepare( qq|
221 SELECT SUM(ecost * quantity) AS actual
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 =?)) |
228 $sth->execute( $cell->{'budget_id'},
229 $budget->{'sort1_authcat'},
230 $cell->{'authvalue'},
231 $budget->{'sort2_authcat'},
235 $actual = $sth->fetchrow_array;
237 # get the estimated amount
238 my $sth = $dbh->prepare( qq|
240 SELECT estimated_amount AS estimated, display FROM aqbudgets_planning
241 WHERE budget_period_id = ? AND
246 $sth->execute( $cell->{'budget_period_id'},
247 $cell->{'budget_id'},
248 $cell->{'authvalue'},
253 my $res = $sth->fetchrow_hashref;
254 # my $display = $res->{'display'};
255 my $estimated = $res->{'estimated'};
258 return $actual, $estimated;
261 # -------------------------------------------------------------------
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
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 );
277 foreach my $cell (@$lines) {
278 my $sth = $dbh->prepare( qq|
280 INSERT INTO aqbudgets_planning
282 budget_period_id = ?,
284 estimated_amount = ?,
288 $cell->{'budget_id'},
289 $cell->{'budget_period_id'},
291 $cell->{'estimated_amount'},
292 $cell->{'authvalue'},
298 # -------------------------------------------------------------------
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
308 $sth->execute($budget_id);
309 my $sum = $sth->fetchrow_array;
313 # -------------------------------------------------------------------
314 sub GetBudgetPermDropbox {
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' ],
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);
339 while (my ($sort1_authcat,$sort2_authcat) = $sth->fetchrow) {
340 $authcats{$sort1_authcat}=1;
341 $authcats{$sort2_authcat}=1;
344 foreach (sort keys %authcats) {
345 push @auth_cats_loop,{ authcat => $_ };
347 return \@auth_cats_loop;
350 # -------------------------------------------------------------------
351 sub GetAuthvalueDropbox {
352 my ( $name, $authcat, $default ) = @_;
353 my @authorised_values;
356 my $dbh = C4::Context->dbh;
357 my $sth = $dbh->prepare(
358 "SELECT authorised_value,lib
359 FROM authorised_values
363 $sth->execute( $authcat );
365 push @authorised_values, '';
366 while (my ($value, $lib) = $sth->fetchrow_array) {
367 push @authorised_values, $value;
368 $authorised_lib{$value} = $lib;
371 return 0 if keys(%authorised_lib) == 0;
373 my $budget_authvalue_dropbox = CGI::scrolling_list(
374 -values => \@authorised_values,
375 -labels => \%authorised_lib,
376 -default => $default,
384 return $budget_authvalue_dropbox
387 # -------------------------------------------------------------------
388 sub GetBudgetPeriodsDropbox {
389 my ($budget_period_id) = @_;
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};
398 # if no buget_id is passed then its an add
399 my $budget_period_dropbox = CGI::scrolling_list(
400 -name => 'budget_period_id',
402 -default => $budget_period_id ? $budget_period_id : $active,
406 return $budget_period_dropbox;
409 # -------------------------------------------------------------------
410 sub GetBudgetPeriods {
411 my ($filters,$orderby) = @_;
412 return SearchInTable("aqbudgetperiods",$filters, $orderby, undef,undef, undef, "wide");
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
420 ## get information about the record that will be deleted
422 if ($budget_period_id) {
423 $sth = $dbh->prepare( qq|
426 WHERE budget_period_id=? |
428 $sth->execute($budget_period_id);
429 } else { # ACTIVE BUDGET
430 $sth = $dbh->prepare(qq|
433 WHERE budget_period_active=1 |
437 my $data = $sth->fetchrow_hashref;
441 # -------------------------------------------------------------------
443 my ($budget_period_id) = @_;
444 my $dbh = C4::Context->dbh;
445 ; ## $total = number of records linked to the record that must be deleted
448 ## get information about the record that will be deleted
449 my $sth = $dbh->prepare(qq|
452 WHERE budget_period_id=? |
454 return $sth->execute($budget_period_id);
457 # -------------------------------------------------------------------
458 sub ModBudgetPeriod {
459 my ($budget_period_information) = @_;
460 return UpdateInTable("aqbudgetperiods",$budget_period_information);
463 # -------------------------------------------------------------------
464 sub GetBudgetHierarchy {
465 my ($budget_period_id, $branchcode, $owner) = @_;
467 my $dbh = C4::Context->dbh;
471 # show only period X if requested
473 if ($budget_period_id) {
474 push @where_strings," aqbudgets.budget_period_id = ?";
475 push @bind_params, $budget_period_id;
477 # show only budgets owned by me, my branch or everyone
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);
483 push @where_strings, ' (budget_owner_id = ? OR budget_owner_id IS NULL or budget_owner_id ="") ';
484 push @bind_params, $owner;
488 push @where_strings," (budget_branchcode =? or budget_branchcode is NULL)";
489 push @bind_params, $branchcode;
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({});
501 foreach my $r (@res) {
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};
512 $r->{child} = \@child if scalar @child > 0; # add the child
513 $depth_cnt++ if !defined $r->{'depth'};
515 last if ($depth_cnt == 0 || $i == 100);
519 # look for top parents 1st
521 my ($i, $depth_count) = 0;
524 foreach my $r (@res) {
525 if ($r->{depth} == $depth_count) {
526 $children++ if (ref $r->{child} eq 'ARRAY');
528 # find the parent id element_id and insert it after
531 if ($depth_count > 0) {
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}) {
545 $r->{budget_code_indent} = $r->{budget_code};
546 $r->{budget_name_indent} = $r->{budget_name};
549 if (defined $parent) {
550 splice @sort, ($parent + 1), 0, $r;
557 } # --------------foreach
559 last if $children == 0;
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;
567 my $moo = $r->{'budget_code_indent'};
568 $moo =~ s/\ /\ \;/g;
569 $r->{'budget_code_indent'} = $moo;
571 my $moo = $r->{'budget_name_indent'};
572 $moo =~ s/\ /\ \;/g;
573 $r->{'budget_name_indent'} = $moo;
575 $r->{'budget_spent'} = GetBudgetSpent( $r->{'budget_id'} );
577 $r->{'budget_amount_total'} = $r->{'budget_amount'};
582 foreach my $sub (@subs_arr) {
583 my $sub_budget = GetBudget($sub);
585 $r->{budget_spent_sublevel} += GetBudgetSpent( $sub_budget->{'budget_id'} );
586 $unalloc_count += $sub_budget->{'budget_amount'};
592 # -------------------------------------------------------------------
596 return InsertInTable("aqbudgets",$budget);
599 # -------------------------------------------------------------------
602 return UpdateInTable("aqbudgets",$budget);
605 # -------------------------------------------------------------------
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);
616 =head2 FUNCTIONS ABOUT BUDGETS
626 &GetBudget($budget_id);
628 get a specific budget
634 # -------------------------------------------------------------------
636 my ( $budget_id ) = @_;
637 my $dbh = C4::Context->dbh;
644 my $sth = $dbh->prepare($query);
645 $sth->execute( $budget_id );
646 my $result = $sth->fetchrow_hashref;
654 &GetBudgets($filter, $order_by);
662 # -------------------------------------------------------------------
664 my ($filters,$orderby) = @_;
665 return SearchInTable("aqbudgetperiods",$filters, $orderby, undef,undef, undef, "wide");
668 # -------------------------------------------------------------------
672 @currencies = &GetCurrencies;
674 Returns the list of all known currencies.
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.
682 my $dbh = C4::Context->dbh;
687 my $sth = $dbh->prepare($query);
690 while ( my $data = $sth->fetchrow_hashref ) {
691 push( @results, $data );
696 # -------------------------------------------------------------------
699 my $dbh = C4::Context->dbh;
701 SELECT * FROM currency where active = '1' ";
702 my $sth = $dbh->prepare($query);
704 my $r = $sth->fetchrow_hashref;
710 &ModCurrencies($currency, $newrate);
712 Sets the exchange rate for C<$currency> to be C<$newrate>.
717 my ( $currency, $rate ) = @_;
718 my $dbh = C4::Context->dbh;
723 my $sth = $dbh->prepare($query);
724 $sth->execute( $rate, $currency );
727 # -------------------------------------------------------------------
729 =head3 ConvertCurrency
731 $foreignprice = &ConvertCurrency($currency, $localprice);
733 Converts the price C<$localprice> to foreign currency C<$currency> by
734 dividing by the exchange rate, and returns the result.
736 If no exchange rate is found,e is one
741 sub ConvertCurrency {
742 my ( $currency, $price ) = @_;
743 my $dbh = C4::Context->dbh;
749 my $sth = $dbh->prepare($query);
750 $sth->execute($currency);
751 my $cur = ( $sth->fetchrow_array() )[0];
755 return ( $price / $cur );
759 returns an array containing fieldname followed by PRI as value if PRIMARY Key
762 my $tablename=shift||"aqbudgets";
763 return @{C4::Context->dbh->selectcol_arrayref("SHOW columns from $tablename",{Columns=>[1,4]})};
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) ";
780 push @values, $$budget{$field};
781 push @keys, $strkeys;
783 return (\@keys,\@values);
786 END { } # module clean-up code here (global destructor)
795 Koha Developement team <info@koha.org>