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.
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
51 &GetBudgetPeriodsDropbox
68 &CheckBudgetParentPerm
75 # ----------------------------BUDGETS.PM-----------------------------";
79 my ( $authcat, @hide_cols ) = @_;
80 my $dbh = C4
::Context
->dbh;
82 my $sth1 = $dbh->prepare(
84 UPDATE aqbudgets_planning SET display
= 0
88 foreach my $authvalue (@hide_cols) {
89 # $sth1->{TraceLevel} = 3;
90 $sth1->execute( $authcat, $authvalue );
95 my ( $authcat, $authvalue ) = @_;
97 my $dbh = C4
::Context
->dbh;
98 my $sth = $dbh->prepare(
100 SELECT count
(display
) as cnt from aqbudgets_planning
102 AND authvalue
= ?
and display
= 0 |
105 # $sth->{TraceLevel} = 3;
106 $sth->execute( $authcat, $authvalue );
107 my $res = $sth->fetchrow_hashref;
109 return $res->{cnt
} > 0 ?
0: 1
113 sub CheckBudgetParentPerm
{
114 my ( $budget, $borrower_id ) = @_;
115 my $depth = $budget->{depth
};
116 my $parent_id = $budget->{budget_parent_id
};
118 my $parent = GetBudget
($parent_id);
119 $parent_id = $parent->{budget_parent_id
};
120 if ( $parent->{budget_owner_id
} == $borrower_id ) {
128 sub AddBudgetPeriod
{
129 my ($budgetperiod) = @_;
130 return InsertInTable
("aqbudgetperiods",$budgetperiod);
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;
176 return $sum->{'sum'};
179 # -------------------------------------------------------------------
180 sub GetBudgetsPlanCell
{
181 my ( $cell, $period, $budget ) = @_;
183 my $dbh = C4
::Context
->dbh;
184 if ( $cell->{'authcat'} eq 'MONTHS' ) {
185 # get the actual amount
186 $sth = $dbh->prepare( qq|
188 SELECT SUM
(ecost
) AS actual FROM aqorders
189 WHERE budget_id
= ? AND
190 entrydate like
"$cell->{'authvalue'}%" |
192 $sth->execute( $cell->{'budget_id'} );
193 } elsif ( $cell->{'authcat'} eq 'BRANCHES' ) {
194 # get the actual amount
195 $sth = $dbh->prepare( qq|
197 SELECT SUM
(ecost
) FROM aqorders
198 LEFT JOIN aqorders_items
199 ON
(aqorders
.ordernumber
= aqorders_items
.ordernumber
)
201 ON
(aqorders_items
.itemnumber
= items
.itemnumber
)
202 WHERE budget_id
= ? AND homebranch
= ?
| );
204 $sth->execute( $cell->{'budget_id'}, $cell->{'authvalue'} );
205 } elsif ( $cell->{'authcat'} eq 'ITEMTYPES' ) {
206 # get the actual amount
207 $sth = $dbh->prepare( qq|
209 SELECT SUM
( ecost
* quantity
) AS actual
210 FROM aqorders JOIN biblioitems
211 ON
(biblioitems
.biblionumber
= aqorders
.biblionumber
)
212 WHERE aqorders
.budget_id
= ?
and itemtype
= ?
|
214 $sth->execute( $cell->{'budget_id'},
215 $cell->{'authvalue'} );
217 # ELSE GENERIC ORDERS SORT1/SORT2 STAT COUNT.
219 # get the actual amount
220 $sth = $dbh->prepare( qq|
222 SELECT SUM
(ecost
* quantity
) AS actual
224 JOIN aqbudgets ON
(aqbudgets
.budget_id
= aqorders
.budget_id
)
225 WHERE aqorders
.budget_id
= ? AND
226 ((aqbudgets
.sort1_authcat
= ? AND sort1
=?
) OR
227 (aqbudgets
.sort2_authcat
= ? AND sort2
=?
)) |
229 $sth->execute( $cell->{'budget_id'},
230 $budget->{'sort1_authcat'},
231 $cell->{'authvalue'},
232 $budget->{'sort2_authcat'},
236 $actual = $sth->fetchrow_array;
238 # get the estimated amount
239 $sth = $dbh->prepare( qq|
241 SELECT estimated_amount AS estimated
, display FROM aqbudgets_planning
242 WHERE budget_period_id
= ? AND
247 $sth->execute( $cell->{'budget_period_id'},
248 $cell->{'budget_id'},
249 $cell->{'authvalue'},
254 my $res = $sth->fetchrow_hashref;
255 # my $display = $res->{'display'};
256 my $estimated = $res->{'estimated'};
259 return $actual, $estimated;
262 # -------------------------------------------------------------------
264 my ( $budget_plan, $budget_period_id, $authcat ) = @_;
265 my $dbh = C4
::Context
->dbh;
266 foreach my $buds (@
$budget_plan) {
267 my $lines = $buds->{lines
};
268 my $sth = $dbh->prepare( qq|
269 DELETE FROM aqbudgets_planning
270 WHERE budget_period_id
= ? AND
274 #delete a aqplan line of cells, then insert new cells,
275 # these could be UPDATES rather than DEL/INSERTS...
276 $sth->execute( $budget_period_id, $lines->[0]{budget_id
} , $authcat );
278 foreach my $cell (@
$lines) {
279 my $sth = $dbh->prepare( qq|
281 INSERT INTO aqbudgets_planning
283 budget_period_id
= ?
,
285 estimated_amount
= ?
,
289 $cell->{'budget_id'},
290 $cell->{'budget_period_id'},
292 $cell->{'estimated_amount'},
293 $cell->{'authvalue'},
299 # -------------------------------------------------------------------
301 my ($budget_id) = @_;
302 my $dbh = C4
::Context
->dbh;
303 my $sth = $dbh->prepare(qq|
304 SELECT SUM
(ecost
* quantity
) AS sum FROM aqorders
305 WHERE budget_id
= ? AND
306 quantityreceived
> 0 AND
307 datecancellationprinted IS NULL
310 $sth->execute($budget_id);
311 my $sum = $sth->fetchrow_array;
315 # -------------------------------------------------------------------
316 sub GetBudgetOrdered
{
317 my ($budget_id) = @_;
318 my $dbh = C4
::Context
->dbh;
319 my $sth = $dbh->prepare(qq|
320 SELECT SUM
(ecost
* quantity
) AS sum FROM aqorders
321 WHERE budget_id
= ? AND
322 quantityreceived
= 0 AND
323 datecancellationprinted IS NULL
326 $sth->execute($budget_id);
327 my $sum = $sth->fetchrow_array;
331 # -------------------------------------------------------------------
332 sub GetBudgetPermDropbox
{
335 $labels{'0'} = 'None';
336 $labels{'1'} = 'Owner';
337 $labels{'2'} = 'Library';
338 my $radio = CGI
::scrolling_list
(
339 -id
=> 'budget_permission',
340 -name
=> 'budget_permission',
341 -values => [ '0', '1', '2' ],
349 # -------------------------------------------------------------------
350 sub GetBudgetAuthCats
{
351 my ($budget_period_id) = shift;
352 # now, populate the auth_cats_loop used in the budget planning button
353 # we must retrieve all auth values used by at least one budget
354 my $dbh = C4
::Context
->dbh;
355 my $sth=$dbh->prepare("SELECT sort1_authcat,sort2_authcat FROM aqbudgets WHERE budget_period_id=?");
356 $sth->execute($budget_period_id);
358 while (my ($sort1_authcat,$sort2_authcat) = $sth->fetchrow) {
359 $authcats{$sort1_authcat}=1;
360 $authcats{$sort2_authcat}=1;
363 foreach (sort keys %authcats) {
364 push @auth_cats_loop,{ authcat
=> $_ };
366 return \
@auth_cats_loop;
369 # -------------------------------------------------------------------
370 sub GetAuthvalueDropbox
{
371 my ( $name, $authcat, $default ) = @_;
372 my @authorised_values;
375 my $dbh = C4
::Context
->dbh;
376 my $sth = $dbh->prepare(
377 "SELECT authorised_value,lib
378 FROM authorised_values
382 $sth->execute( $authcat );
384 push @authorised_values, '';
385 while (my ($value, $lib) = $sth->fetchrow_array) {
386 push @authorised_values, $value;
387 $authorised_lib{$value} = $lib;
390 return 0 if keys(%authorised_lib) == 0;
392 my $budget_authvalue_dropbox = CGI
::scrolling_list
(
393 -values => \
@authorised_values,
394 -labels
=> \
%authorised_lib,
395 -default => $default,
403 return $budget_authvalue_dropbox
406 # -------------------------------------------------------------------
407 sub GetBudgetPeriodsDropbox
{
408 my ($budget_period_id) = @_;
411 my ($active, $periods) = GetBudgetPeriods
();
412 foreach my $r (@
$periods) {
413 $labels{"$r->{budget_period_id}"} = $r->{budget_period_description
};
414 push @values, $r->{budget_period_id
};
417 # if no buget_id is passed then its an add
418 my $budget_period_dropbox = CGI
::scrolling_list
(
419 -name
=> 'budget_period_id',
421 -default => $budget_period_id ?
$budget_period_id : $active,
425 return $budget_period_dropbox;
428 # -------------------------------------------------------------------
429 sub GetBudgetPeriods
{
430 my ($filters,$orderby) = @_;
431 return SearchInTable
("aqbudgetperiods",$filters, $orderby, undef,undef, undef, "wide");
433 # -------------------------------------------------------------------
434 sub GetBudgetPeriod
{
435 my ($budget_period_id) = @_;
436 my $dbh = C4
::Context
->dbh;
437 ## $total = number of records linked to the record that must be deleted
439 ## get information about the record that will be deleted
441 if ($budget_period_id) {
442 $sth = $dbh->prepare( qq|
445 WHERE budget_period_id
=?
|
447 $sth->execute($budget_period_id);
448 } else { # ACTIVE BUDGET
449 $sth = $dbh->prepare(qq|
452 WHERE budget_period_active
=1 |
456 my $data = $sth->fetchrow_hashref;
460 # -------------------------------------------------------------------
462 my ($budget_period_id) = @_;
463 my $dbh = C4
::Context
->dbh;
464 ; ## $total = number of records linked to the record that must be deleted
467 ## get information about the record that will be deleted
468 my $sth = $dbh->prepare(qq|
471 WHERE budget_period_id
=?
|
473 return $sth->execute($budget_period_id);
476 # -------------------------------------------------------------------
477 sub ModBudgetPeriod
{
478 my ($budget_period_information) = @_;
479 return UpdateInTable
("aqbudgetperiods",$budget_period_information);
482 # -------------------------------------------------------------------
483 sub GetBudgetHierarchy
{
484 my ($budget_period_id, $branchcode, $owner) = @_;
486 my $dbh = C4
::Context
->dbh;
490 # show only period X if requested
492 if ($budget_period_id) {
493 push @where_strings," aqbudgets.budget_period_id = ?";
494 push @bind_params, $budget_period_id;
496 # show only budgets owned by me, my branch or everyone
499 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
="")))};
500 push @bind_params, ($owner, $branchcode);
502 push @where_strings, ' (budget_owner_id = ? OR budget_owner_id IS NULL or budget_owner_id ="") ';
503 push @bind_params, $owner;
507 push @where_strings," (budget_branchcode =? or budget_branchcode is NULL)";
508 push @bind_params, $branchcode;
511 $query.=" WHERE ".join(' AND ', @where_strings) if @where_strings;
512 $debug && warn $query,join(",",@bind_params);
513 my $sth = $dbh->prepare($query);
514 $sth->execute(@bind_params);
515 my $results = $sth->fetchall_arrayref({});
520 foreach my $r (@res) {
523 $r->{depth
} = '0' if !defined $r->{budget_parent_id
};
524 foreach my $r2 (@res) {
525 if (defined $r2->{budget_parent_id
}
526 && $r2->{budget_parent_id
} == $r->{budget_id
}) {
527 push @child, $r2->{budget_id
};
528 $r2->{depth
} = ($r->{depth
} + 1) if defined $r->{depth
};
531 $r->{child
} = \
@child if scalar @child > 0; # add the child
532 $depth_cnt++ if !defined $r->{'depth'};
534 last if ($depth_cnt == 0 || $i == 100);
538 # look for top parents 1st
539 my (@sort, $depth_count);
540 ($i, $depth_count) = 0;
543 foreach my $r (@res) {
544 if ($r->{depth
} == $depth_count) {
545 $children++ if (ref $r->{child
} eq 'ARRAY');
547 # find the parent id element_id and insert it after
550 if ($depth_count > 0) {
553 my $depth = $r->{depth
} * 2;
554 $r->{budget_code_indent
} = $r->{budget_code
};
555 $r->{budget_name_indent
} = $r->{budget_name
};
556 foreach my $r3 (@sort) {
557 if ($r3->{budget_id
} == $r->{budget_parent_id
}) {
564 $r->{budget_code_indent
} = $r->{budget_code
};
565 $r->{budget_name_indent
} = $r->{budget_name
};
568 if (defined $parent) {
569 splice @sort, ($parent + 1), 0, $r;
576 } # --------------foreach
578 last if $children == 0;
581 # add budget-percent and allocation, and flags for html-template
582 foreach my $r (@sort) {
583 my $subs_href = $r->{'child'};
584 my @subs_arr = @
$subs_href if defined $subs_href;
586 my $moo = $r->{'budget_code_indent'};
587 $moo =~ s/\ /\ \;/g;
588 $r->{'budget_code_indent'} = $moo;
590 $moo = $r->{'budget_name_indent'};
591 $moo =~ s/\ /\ \;/g;
592 $r->{'budget_name_indent'} = $moo;
594 $r->{'budget_spent'} = GetBudgetSpent
( $r->{'budget_id'} );
596 $r->{'budget_amount_total'} = $r->{'budget_amount'};
601 foreach my $sub (@subs_arr) {
602 my $sub_budget = GetBudget
($sub);
604 $r->{budget_spent_sublevel
} += GetBudgetSpent
( $sub_budget->{'budget_id'} );
605 $unalloc_count += $sub_budget->{'budget_amount'};
611 # -------------------------------------------------------------------
615 return InsertInTable
("aqbudgets",$budget);
618 # -------------------------------------------------------------------
621 return UpdateInTable
("aqbudgets",$budget);
624 # -------------------------------------------------------------------
626 my ($budget_id) = @_;
627 my $dbh = C4
::Context
->dbh;
628 my $sth = $dbh->prepare("delete from aqbudgets where budget_id=?");
629 my $rc = $sth->execute($budget_id);
633 =head2 FUNCTIONS ABOUT BUDGETS
645 &GetBudget($budget_id);
647 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;
672 &GetBudgets($filter, $order_by);
680 # -------------------------------------------------------------------
682 my ($filters,$orderby) = @_;
683 return SearchInTable
("aqbudgets",$filters, $orderby, undef,undef, undef, "wide");
686 # -------------------------------------------------------------------
690 @currencies = &GetCurrencies;
692 Returns the list of all known currencies.
694 C<$currencies> is a array; its elements are references-to-hash, whose
695 keys are the fields from the currency table in the Koha database.
700 my $dbh = C4
::Context
->dbh;
705 my $sth = $dbh->prepare($query);
708 while ( my $data = $sth->fetchrow_hashref ) {
709 push( @results, $data );
714 # -------------------------------------------------------------------
717 my $dbh = C4
::Context
->dbh;
719 SELECT * FROM currency where active = '1' ";
720 my $sth = $dbh->prepare($query);
722 my $r = $sth->fetchrow_hashref;
728 &ModCurrencies($currency, $newrate);
730 Sets the exchange rate for C<$currency> to be C<$newrate>.
735 my ( $currency, $rate ) = @_;
736 my $dbh = C4
::Context
->dbh;
741 my $sth = $dbh->prepare($query);
742 $sth->execute( $rate, $currency );
745 # -------------------------------------------------------------------
747 =head3 ConvertCurrency
749 $foreignprice = &ConvertCurrency($currency, $localprice);
751 Converts the price C<$localprice> to foreign currency C<$currency> by
752 dividing by the exchange rate, and returns the result.
754 If no exchange rate is found,e is one
759 sub ConvertCurrency
{
760 my ( $currency, $price ) = @_;
761 my $dbh = C4
::Context
->dbh;
767 my $sth = $dbh->prepare($query);
768 $sth->execute($currency);
769 my $cur = ( $sth->fetchrow_array() )[0];
773 return ( $price / $cur );
778 returns an array containing fieldname followed by PRI as value if PRIMARY Key
783 my $tablename=shift||"aqbudgets";
784 return @
{C4
::Context
->dbh->selectcol_arrayref("SHOW columns from $tablename",{Columns
=>[1,4]})};
792 my %columns= _columns
($tablename);
793 #Filter Primary Keys of table
794 my $elements=join "|",grep {$columns{$_} ne "PRI"} keys %columns;
795 foreach my $field (grep {/\b($elements)\b/} keys %$budget){
796 $$budget{$field}=format_date_in_iso
($$budget{$field}) if ($field=~/date/ && $$budget{$field} !~C4
::Dates
->regexp("iso"));
797 my $strkeys= " $field = ? ";
798 if ($field=~/branch/){
799 $strkeys="( $strkeys OR $field='' OR $field IS NULL) ";
801 push @values, $$budget{$field};
802 push @keys, $strkeys;
804 return (\
@keys,\
@values);
807 END { } # module clean-up code here (global destructor)
814 Koha Developement team <info@koha.org>