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 $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 -id => 'budget_permission',
322 -name => 'budget_permission',
323 -values => [ '0', '1', '2' ],
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);
340 while (my ($sort1_authcat,$sort2_authcat) = $sth->fetchrow) {
341 $authcats{$sort1_authcat}=1;
342 $authcats{$sort2_authcat}=1;
345 foreach (sort keys %authcats) {
346 push @auth_cats_loop,{ authcat => $_ };
348 return \@auth_cats_loop;
351 # -------------------------------------------------------------------
352 sub GetAuthvalueDropbox {
353 my ( $name, $authcat, $default ) = @_;
354 my @authorised_values;
357 my $dbh = C4::Context->dbh;
358 my $sth = $dbh->prepare(
359 "SELECT authorised_value,lib
360 FROM authorised_values
364 $sth->execute( $authcat );
366 push @authorised_values, '';
367 while (my ($value, $lib) = $sth->fetchrow_array) {
368 push @authorised_values, $value;
369 $authorised_lib{$value} = $lib;
372 return 0 if keys(%authorised_lib) == 0;
374 my $budget_authvalue_dropbox = CGI::scrolling_list(
375 -values => \@authorised_values,
376 -labels => \%authorised_lib,
377 -default => $default,
385 return $budget_authvalue_dropbox
388 # -------------------------------------------------------------------
389 sub GetBudgetPeriodsDropbox {
390 my ($budget_period_id) = @_;
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};
399 # if no buget_id is passed then its an add
400 my $budget_period_dropbox = CGI::scrolling_list(
401 -name => 'budget_period_id',
403 -default => $budget_period_id ? $budget_period_id : $active,
407 return $budget_period_dropbox;
410 # -------------------------------------------------------------------
411 sub GetBudgetPeriods {
412 my ($filters,$orderby) = @_;
413 return SearchInTable("aqbudgetperiods",$filters, $orderby, undef,undef, undef, "wide");
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
421 ## get information about the record that will be deleted
423 if ($budget_period_id) {
424 $sth = $dbh->prepare( qq|
427 WHERE budget_period_id=? |
429 $sth->execute($budget_period_id);
430 } else { # ACTIVE BUDGET
431 $sth = $dbh->prepare(qq|
434 WHERE budget_period_active=1 |
438 my $data = $sth->fetchrow_hashref;
442 # -------------------------------------------------------------------
444 my ($budget_period_id) = @_;
445 my $dbh = C4::Context->dbh;
446 ; ## $total = number of records linked to the record that must be deleted
449 ## get information about the record that will be deleted
450 my $sth = $dbh->prepare(qq|
453 WHERE budget_period_id=? |
455 return $sth->execute($budget_period_id);
458 # -------------------------------------------------------------------
459 sub ModBudgetPeriod {
460 my ($budget_period_information) = @_;
461 return UpdateInTable("aqbudgetperiods",$budget_period_information);
464 # -------------------------------------------------------------------
465 sub GetBudgetHierarchy {
466 my ($budget_period_id, $branchcode, $owner) = @_;
468 my $dbh = C4::Context->dbh;
472 # show only period X if requested
474 if ($budget_period_id) {
475 push @where_strings," aqbudgets.budget_period_id = ?";
476 push @bind_params, $budget_period_id;
478 # show only budgets owned by me, my branch or everyone
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);
484 push @where_strings, ' (budget_owner_id = ? OR budget_owner_id IS NULL or budget_owner_id ="") ';
485 push @bind_params, $owner;
489 push @where_strings," (budget_branchcode =? or budget_branchcode is NULL)";
490 push @bind_params, $branchcode;
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({});
502 foreach my $r (@res) {
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};
513 $r->{child} = \@child if scalar @child > 0; # add the child
514 $depth_cnt++ if !defined $r->{'depth'};
516 last if ($depth_cnt == 0 || $i == 100);
520 # look for top parents 1st
521 my (@sort, $depth_count);
522 ($i, $depth_count) = 0;
525 foreach my $r (@res) {
526 if ($r->{depth} == $depth_count) {
527 $children++ if (ref $r->{child} eq 'ARRAY');
529 # find the parent id element_id and insert it after
532 if ($depth_count > 0) {
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}) {
546 $r->{budget_code_indent} = $r->{budget_code};
547 $r->{budget_name_indent} = $r->{budget_name};
550 if (defined $parent) {
551 splice @sort, ($parent + 1), 0, $r;
558 } # --------------foreach
560 last if $children == 0;
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;
568 my $moo = $r->{'budget_code_indent'};
569 $moo =~ s/\ /\ \;/g;
570 $r->{'budget_code_indent'} = $moo;
572 $moo = $r->{'budget_name_indent'};
573 $moo =~ s/\ /\ \;/g;
574 $r->{'budget_name_indent'} = $moo;
576 $r->{'budget_spent'} = GetBudgetSpent( $r->{'budget_id'} );
578 $r->{'budget_amount_total'} = $r->{'budget_amount'};
583 foreach my $sub (@subs_arr) {
584 my $sub_budget = GetBudget($sub);
586 $r->{budget_spent_sublevel} += GetBudgetSpent( $sub_budget->{'budget_id'} );
587 $unalloc_count += $sub_budget->{'budget_amount'};
593 # -------------------------------------------------------------------
597 return InsertInTable("aqbudgets",$budget);
600 # -------------------------------------------------------------------
603 return UpdateInTable("aqbudgets",$budget);
606 # -------------------------------------------------------------------
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);
617 =head2 FUNCTIONS ABOUT BUDGETS
627 &GetBudget($budget_id);
629 get a specific budget
635 # -------------------------------------------------------------------
637 my ( $budget_id ) = @_;
638 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("aqbudgets",$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>