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.
21 #use warnings; FIXME - Bug 2505
23 use C4
::Dates
qw(format_date format_date_in_iso);
24 use C4
::SQLHelper qw
<:all
>;
27 use vars
qw($VERSION @ISA @EXPORT);
30 # set the version for version checking
31 $VERSION = 3.07.00.049;
72 &CheckBudgetParentPerm
79 # ----------------------------BUDGETS.PM-----------------------------";
82 =head1 FUNCTIONS ABOUT BUDGETS
87 my ( $authcat, @hide_cols ) = @_;
88 my $dbh = C4
::Context
->dbh;
90 my $sth1 = $dbh->prepare(
92 UPDATE aqbudgets_planning SET display
= 0
96 foreach my $authvalue (@hide_cols) {
97 # $sth1->{TraceLevel} = 3;
98 $sth1->execute( $authcat, $authvalue );
103 my ( $authcat, $authvalue ) = @_;
105 my $dbh = C4
::Context
->dbh;
106 my $sth = $dbh->prepare(
108 SELECT count
(display
) as cnt from aqbudgets_planning
110 AND authvalue
= ?
and display
= 0 |
113 # $sth->{TraceLevel} = 3;
114 $sth->execute( $authcat, $authvalue );
115 my $res = $sth->fetchrow_hashref;
117 return $res->{cnt
} > 0 ?
0: 1
121 sub CheckBudgetParentPerm
{
122 my ( $budget, $borrower_id ) = @_;
123 my $depth = $budget->{depth
};
124 my $parent_id = $budget->{budget_parent_id
};
126 my $parent = GetBudget
($parent_id);
127 $parent_id = $parent->{budget_parent_id
};
128 if ( $parent->{budget_owner_id
} == $borrower_id ) {
136 sub AddBudgetPeriod
{
137 my ($budgetperiod) = @_;
138 return InsertInTable
("aqbudgetperiods",$budgetperiod);
140 # -------------------------------------------------------------------
141 sub GetPeriodsCount
{
142 my $dbh = C4
::Context
->dbh;
143 my $sth = $dbh->prepare("
144 SELECT COUNT(*) AS sum FROM aqbudgetperiods ");
146 my $res = $sth->fetchrow_hashref;
147 return $res->{'sum'};
150 # -------------------------------------------------------------------
151 sub CheckBudgetParent
{
152 my ( $new_parent, $budget ) = @_;
153 my $new_parent_id = $new_parent->{'budget_id'};
154 my $budget_id = $budget->{'budget_id'};
155 my $dbh = C4
::Context
->dbh;
156 my $parent_id_tmp = $new_parent_id;
158 # check new-parent is not a child (or a child's child ;)
159 my $sth = $dbh->prepare(qq|
160 SELECT budget_parent_id FROM
161 aqbudgets where budget_id
= ?
| );
163 $sth->execute($parent_id_tmp);
164 my $res = $sth->fetchrow_hashref;
165 if ( $res->{'budget_parent_id'} == $budget_id ) {
168 if ( not defined $res->{'budget_parent_id'} ) {
171 $parent_id_tmp = $res->{'budget_parent_id'};
175 # -------------------------------------------------------------------
176 sub BudgetHasChildren
{
177 my ( $budget_id ) = @_;
178 my $dbh = C4
::Context
->dbh;
179 my $sth = $dbh->prepare(qq|
180 SELECT count
(*) as sum FROM aqbudgets
181 WHERE budget_parent_id
= ?
| );
182 $sth->execute( $budget_id );
183 my $sum = $sth->fetchrow_hashref;
184 return $sum->{'sum'};
187 # -------------------------------------------------------------------
188 sub GetBudgetsPlanCell
{
189 my ( $cell, $period, $budget ) = @_;
191 my $dbh = C4
::Context
->dbh;
192 if ( $cell->{'authcat'} eq 'MONTHS' ) {
193 # get the actual amount
194 $sth = $dbh->prepare( qq|
196 SELECT SUM
(ecost
) AS actual FROM aqorders
197 WHERE budget_id
= ? AND
198 entrydate like
"$cell->{'authvalue'}%" |
200 $sth->execute( $cell->{'budget_id'} );
201 } elsif ( $cell->{'authcat'} eq 'BRANCHES' ) {
202 # get the actual amount
203 $sth = $dbh->prepare( qq|
205 SELECT SUM
(ecost
) FROM aqorders
206 LEFT JOIN aqorders_items
207 ON
(aqorders
.ordernumber
= aqorders_items
.ordernumber
)
209 ON
(aqorders_items
.itemnumber
= items
.itemnumber
)
210 WHERE budget_id
= ? AND homebranch
= ?
| );
212 $sth->execute( $cell->{'budget_id'}, $cell->{'authvalue'} );
213 } elsif ( $cell->{'authcat'} eq 'ITEMTYPES' ) {
214 # get the actual amount
215 $sth = $dbh->prepare( qq|
217 SELECT SUM
( ecost
* quantity
) AS actual
218 FROM aqorders JOIN biblioitems
219 ON
(biblioitems
.biblionumber
= aqorders
.biblionumber
)
220 WHERE aqorders
.budget_id
= ?
and itemtype
= ?
|
222 $sth->execute( $cell->{'budget_id'},
223 $cell->{'authvalue'} );
225 # ELSE GENERIC ORDERS SORT1/SORT2 STAT COUNT.
227 # get the actual amount
228 $sth = $dbh->prepare( qq|
230 SELECT SUM
(ecost
* quantity
) AS actual
232 JOIN aqbudgets ON
(aqbudgets
.budget_id
= aqorders
.budget_id
)
233 WHERE aqorders
.budget_id
= ? AND
234 ((aqbudgets
.sort1_authcat
= ? AND sort1
=?
) OR
235 (aqbudgets
.sort2_authcat
= ? AND sort2
=?
)) |
237 $sth->execute( $cell->{'budget_id'},
238 $budget->{'sort1_authcat'},
239 $cell->{'authvalue'},
240 $budget->{'sort2_authcat'},
244 $actual = $sth->fetchrow_array;
246 # get the estimated amount
247 $sth = $dbh->prepare( qq|
249 SELECT estimated_amount AS estimated
, display FROM aqbudgets_planning
250 WHERE budget_period_id
= ? AND
255 $sth->execute( $cell->{'budget_period_id'},
256 $cell->{'budget_id'},
257 $cell->{'authvalue'},
262 my $res = $sth->fetchrow_hashref;
263 # my $display = $res->{'display'};
264 my $estimated = $res->{'estimated'};
267 return $actual, $estimated;
270 # -------------------------------------------------------------------
272 my ( $budget_plan, $budget_period_id, $authcat ) = @_;
273 my $dbh = C4
::Context
->dbh;
274 foreach my $buds (@
$budget_plan) {
275 my $lines = $buds->{lines
};
276 my $sth = $dbh->prepare( qq|
277 DELETE FROM aqbudgets_planning
278 WHERE budget_period_id
= ? AND
282 #delete a aqplan line of cells, then insert new cells,
283 # these could be UPDATES rather than DEL/INSERTS...
284 $sth->execute( $budget_period_id, $lines->[0]{budget_id
} , $authcat );
286 foreach my $cell (@
$lines) {
287 my $sth = $dbh->prepare( qq|
289 INSERT INTO aqbudgets_planning
291 budget_period_id
= ?
,
293 estimated_amount
= ?
,
297 $cell->{'budget_id'},
298 $cell->{'budget_period_id'},
300 $cell->{'estimated_amount'},
301 $cell->{'authvalue'},
307 # -------------------------------------------------------------------
309 my ($budget_id) = @_;
310 my $dbh = C4
::Context
->dbh;
311 my $sth = $dbh->prepare(qq|
312 SELECT SUM
( COALESCE
(unitprice
, ecost
) * quantity
) AS sum FROM aqorders
313 WHERE budget_id
= ? AND
314 quantityreceived
> 0 AND
315 datecancellationprinted IS NULL
318 $sth->execute($budget_id);
319 my $sum = $sth->fetchrow_array;
323 # -------------------------------------------------------------------
324 sub GetBudgetOrdered
{
325 my ($budget_id) = @_;
326 my $dbh = C4
::Context
->dbh;
327 my $sth = $dbh->prepare(qq|
328 SELECT SUM
(ecost
* quantity
) AS sum FROM aqorders
329 WHERE budget_id
= ? AND
330 quantityreceived
= 0 AND
331 datecancellationprinted IS NULL
334 $sth->execute($budget_id);
335 my $sum = $sth->fetchrow_array;
339 # -------------------------------------------------------------------
340 sub GetBudgetAuthCats
{
341 my ($budget_period_id) = shift;
342 # now, populate the auth_cats_loop used in the budget planning button
343 # we must retrieve all auth values used by at least one budget
344 my $dbh = C4
::Context
->dbh;
345 my $sth=$dbh->prepare("SELECT sort1_authcat,sort2_authcat FROM aqbudgets WHERE budget_period_id=?");
346 $sth->execute($budget_period_id);
348 while (my ($sort1_authcat,$sort2_authcat) = $sth->fetchrow) {
349 $authcats{$sort1_authcat}=1;
350 $authcats{$sort2_authcat}=1;
353 foreach (sort keys %authcats) {
354 push @auth_cats_loop,{ authcat
=> $_ };
356 return \
@auth_cats_loop;
359 # -------------------------------------------------------------------
360 sub GetAuthvalueDropbox
{
361 my ( $authcat, $default ) = @_;
362 my $dbh = C4
::Context
->dbh;
363 my $sth = $dbh->prepare(
364 'SELECT authorised_value,lib FROM authorised_values
365 WHERE category = ? ORDER BY lib'
367 $sth->execute( $authcat );
368 my $option_list = [];
369 my @authorised_values = ( q{} );
370 while (my ($value, $lib) = $sth->fetchrow_array) {
371 push @
{$option_list}, {
374 default => ($default eq $value),
378 if ( @
{$option_list} ) {
384 # -------------------------------------------------------------------
385 sub GetBudgetPeriods
{
386 my ($filters,$orderby) = @_;
387 return SearchInTable
("aqbudgetperiods",$filters, $orderby, undef,undef, undef, "wide");
389 # -------------------------------------------------------------------
390 sub GetBudgetPeriod
{
391 my ($budget_period_id) = @_;
392 my $dbh = C4
::Context
->dbh;
393 ## $total = number of records linked to the record that must be deleted
395 ## get information about the record that will be deleted
397 if ($budget_period_id) {
398 $sth = $dbh->prepare( qq|
401 WHERE budget_period_id
=?
|
403 $sth->execute($budget_period_id);
404 } else { # ACTIVE BUDGET
405 $sth = $dbh->prepare(qq|
408 WHERE budget_period_active
=1 |
412 my $data = $sth->fetchrow_hashref;
416 # -------------------------------------------------------------------
418 my ($budget_period_id) = @_;
419 my $dbh = C4
::Context
->dbh;
420 ; ## $total = number of records linked to the record that must be deleted
423 ## get information about the record that will be deleted
424 my $sth = $dbh->prepare(qq|
427 WHERE budget_period_id
=?
|
429 return $sth->execute($budget_period_id);
432 # -------------------------------------------------------------------
433 sub ModBudgetPeriod
{
434 my ($budget_period_information) = @_;
435 return UpdateInTable
("aqbudgetperiods",$budget_period_information);
438 # -------------------------------------------------------------------
439 sub GetBudgetHierarchy
{
440 my ( $budget_period_id, $branchcode, $owner ) = @_;
442 my $dbh = C4
::Context
->dbh;
444 SELECT aqbudgets
.*, aqbudgetperiods
.budget_period_active
446 JOIN aqbudgetperiods USING
(budget_period_id
)|;
449 # show only period X if requested
450 if ($budget_period_id) {
451 push @where_strings," aqbudgets.budget_period_id = ?";
452 push @bind_params, $budget_period_id;
454 # show only budgets owned by me, my branch or everyone
458 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
="")))};
459 push @bind_params, ( $owner, $branchcode );
461 push @where_strings, ' (budget_owner_id = ? OR budget_owner_id IS NULL or budget_owner_id ="") ';
462 push @bind_params, $owner;
466 push @where_strings," (budget_branchcode =? or budget_branchcode is NULL)";
467 push @bind_params, $branchcode;
470 $query.=" WHERE ".join(' AND ', @where_strings) if @where_strings;
471 $debug && warn $query,join(",",@bind_params);
472 my $sth = $dbh->prepare($query);
473 $sth->execute(@bind_params);
474 my $results = $sth->fetchall_arrayref({});
479 foreach my $r (@res) {
482 $r->{depth
} = '0' if !defined $r->{budget_parent_id
};
483 foreach my $r2 (@res) {
484 if (defined $r2->{budget_parent_id
}
485 && $r2->{budget_parent_id
} == $r->{budget_id
}) {
486 push @child, $r2->{budget_id
};
487 $r2->{depth
} = ($r->{depth
} + 1) if defined $r->{depth
};
490 $r->{child
} = \
@child if scalar @child > 0; # add the child
491 $depth_cnt++ if !defined $r->{'depth'};
493 last if ($depth_cnt == 0 || $i == 100);
497 # look for top parents 1st
498 my (@sort, $depth_count);
499 ($i, $depth_count) = 0;
502 foreach my $r (@res) {
503 if ($r->{depth
} == $depth_count) {
504 $children++ if (ref $r->{child
} eq 'ARRAY');
506 # find the parent id element_id and insert it after
509 if ($depth_count > 0) {
512 my $depth = $r->{depth
} * 2;
513 $r->{budget_code_indent
} = $r->{budget_code
};
514 $r->{budget_name_indent
} = $r->{budget_name
};
515 foreach my $r3 (@sort) {
516 if ($r3->{budget_id
} == $r->{budget_parent_id
}) {
523 $r->{budget_code_indent
} = $r->{budget_code
};
524 $r->{budget_name_indent
} = $r->{budget_name
};
527 if (defined $parent) {
528 splice @sort, ($parent + 1), 0, $r;
535 } # --------------foreach
537 last if $children == 0;
540 # add budget-percent and allocation, and flags for html-template
541 foreach my $r (@sort) {
542 my $subs_href = $r->{'child'};
544 if ( defined $subs_href ) {
545 @subs_arr = @
{$subs_href};
548 my $moo = $r->{'budget_code_indent'};
549 $moo =~ s/\ /\ \;/g;
550 $r->{'budget_code_indent'} = $moo;
552 $moo = $r->{'budget_name_indent'};
553 $moo =~ s/\ /\ \;/g;
554 $r->{'budget_name_indent'} = $moo;
556 $r->{'budget_spent'} = GetBudgetSpent
( $r->{'budget_id'} );
558 $r->{'budget_amount_total'} = $r->{'budget_amount'};
563 foreach my $sub (@subs_arr) {
564 my $sub_budget = GetBudget
($sub);
566 $r->{budget_spent_sublevel
} += GetBudgetSpent
( $sub_budget->{'budget_id'} );
567 $unalloc_count += $sub_budget->{'budget_amount'};
573 # -------------------------------------------------------------------
577 return InsertInTable
("aqbudgets",$budget);
580 # -------------------------------------------------------------------
583 return UpdateInTable
("aqbudgets",$budget);
586 # -------------------------------------------------------------------
588 my ($budget_id) = @_;
589 my $dbh = C4
::Context
->dbh;
590 my $sth = $dbh->prepare("delete from aqbudgets where budget_id=?");
591 my $rc = $sth->execute($budget_id);
598 &GetBudget($budget_id);
600 get a specific budget
604 # -------------------------------------------------------------------
606 my ( $budget_id ) = @_;
607 my $dbh = C4
::Context
->dbh;
613 my $sth = $dbh->prepare($query);
614 $sth->execute( $budget_id );
615 my $result = $sth->fetchrow_hashref;
619 =head2 GetChildBudgetsSpent
621 &GetChildBudgetsSpent($budget-id);
623 gets the total spent of the level and sublevels of $budget_id
627 # -------------------------------------------------------------------
628 sub GetChildBudgetsSpent
{
629 my ( $budget_id ) = @_;
630 my $dbh = C4
::Context
->dbh;
634 WHERE budget_parent_id=?
636 my $sth = $dbh->prepare($query);
637 $sth->execute( $budget_id );
638 my $result = $sth->fetchall_arrayref({});
639 my $total_spent = GetBudgetSpent
($budget_id);
641 $total_spent += GetChildBudgetsSpent
($_->{"budget_id"}) foreach @
$result;
648 &GetBudgets($filter, $order_by);
654 # -------------------------------------------------------------------
656 my ($filters,$orderby) = @_;
657 return SearchInTable
("aqbudgets",$filters, $orderby, undef,undef, undef, "wide");
660 =head2 GetBudgetUsers
662 my @borrowernumbers = &GetBudgetUsers($budget_id);
664 Return the list of borrowernumbers linked to a budget
669 my ($budget_id) = @_;
671 my $dbh = C4
::Context
->dbh;
673 SELECT borrowernumber
674 FROM aqbudgetborrowers
677 my $sth = $dbh->prepare($query);
678 $sth->execute($budget_id);
681 while (my ($borrowernumber) = $sth->fetchrow_array) {
682 push @borrowernumbers, $borrowernumber
685 return @borrowernumbers;
688 =head2 ModBudgetUsers
690 &ModBudgetUsers($budget_id, @borrowernumbers);
692 Modify the list of borrowernumbers linked to a budget
697 my ($budget_id, @budget_users_id) = @_;
699 return unless $budget_id;
701 my $dbh = C4
::Context
->dbh;
702 my $query = "DELETE FROM aqbudgetborrowers WHERE budget_id = ?";
703 my $sth = $dbh->prepare($query);
704 $sth->execute($budget_id);
707 INSERT INTO aqbudgetborrowers
(budget_id
, borrowernumber
)
710 $sth = $dbh->prepare($query);
711 foreach my $borrowernumber (@budget_users_id) {
712 next unless $borrowernumber;
713 $sth->execute($budget_id, $borrowernumber);
717 sub CanUserUseBudget
{
718 my ($borrower, $budget, $userflags) = @_;
720 if (not ref $borrower) {
721 $borrower = C4
::Members
::GetMember
(borrowernumber
=> $borrower);
723 if (not ref $budget) {
724 $budget = GetBudget
($budget);
727 return 0 unless ($borrower and $budget);
729 if (not defined $userflags) {
730 $userflags = C4
::Auth
::getuserflags
($borrower->{flags
},
731 $borrower->{userid
});
734 unless ($userflags->{superlibrarian
}
735 || (ref $userflags->{acquisition
}
736 && $userflags->{acquisition
}->{budget_manage_all
})
737 || (!ref $userflags->{acquisition
} && $userflags->{acquisition
}))
739 if (not exists $userflags->{acquisition
}) {
743 if (!ref $userflags->{acquisition
} && !$userflags->{acquisition
}) {
747 # Budget restricted to owner
748 if ($budget->{budget_permission
} == 1
749 && $budget->{budget_owner_id
}
750 && $budget->{budget_owner_id
} != $borrower->{borrowernumber
}) {
754 my @budget_users = GetBudgetUsers
($budget->{budget_id
});
756 # Budget restricted to owner, users and library
757 if ($budget->{budget_permission
} == 2
758 && $budget->{budget_owner_id
}
759 && $budget->{budget_owner_id
} != $borrower->{borrowernumber
}
760 && (0 == grep {$borrower->{borrowernumber
} == $_} @budget_users)
761 && defined $budget->{budget_branchcode
}
762 && $budget->{budget_branchcode
} ne C4
::Context
->userenv->{branch
}) {
766 # Budget restricted to owner and users
767 if ($budget->{budget_permission
} == 3
768 && $budget->{budget_owner_id
}
769 && $budget->{budget_owner_id
} != $borrower->{borrowernumber
}
770 && (0 == grep {$borrower->{borrowernumber
} == $_} @budget_users)) {
778 sub CanUserModifyBudget
{
779 my ($borrower, $budget, $userflags) = @_;
781 if (not ref $borrower) {
782 $borrower = C4
::Members
::GetMember
(borrowernumber
=> $borrower);
784 if (not ref $budget) {
785 $budget = GetBudget
($budget);
788 return 0 unless ($borrower and $budget);
790 if (not defined $userflags) {
791 $userflags = C4
::Auth
::getuserflags
($borrower->{flags
},
792 $borrower->{userid
});
795 unless ($userflags->{superlibrarian
}
796 || (ref $userflags->{acquisition
}
797 && $userflags->{acquisition
}->{budget_manage_all
})
798 || (!ref $userflags->{acquisition
} && $userflags->{acquisition
}))
800 if (!CanUserUseBudget
($borrower, $budget, $userflags)) {
804 if (ref $userflags->{acquisition
}
805 && !$userflags->{acquisition
}->{budget_modify
}) {
813 # -------------------------------------------------------------------
817 @currencies = &GetCurrencies;
819 Returns the list of all known currencies.
821 C<$currencies> is a array; its elements are references-to-hash, whose
822 keys are the fields from the currency table in the Koha database.
827 my $dbh = C4
::Context
->dbh;
832 my $sth = $dbh->prepare($query);
835 while ( my $data = $sth->fetchrow_hashref ) {
836 push( @results, $data );
841 # -------------------------------------------------------------------
844 my $dbh = C4
::Context
->dbh;
846 SELECT * FROM currency where active = '1' ";
847 my $sth = $dbh->prepare($query);
849 my $r = $sth->fetchrow_hashref;
855 &ModCurrencies($currency, $newrate);
857 Sets the exchange rate for C<$currency> to be C<$newrate>.
862 my ( $currency, $rate ) = @_;
863 my $dbh = C4
::Context
->dbh;
868 my $sth = $dbh->prepare($query);
869 $sth->execute( $rate, $currency );
872 # -------------------------------------------------------------------
874 =head2 ConvertCurrency
876 $foreignprice = &ConvertCurrency($currency, $localprice);
878 Converts the price C<$localprice> to foreign currency C<$currency> by
879 dividing by the exchange rate, and returns the result.
881 If no exchange rate is found, e is one to 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 );
904 returns an array containing fieldname followed by PRI as value if PRIMARY Key
909 my $tablename=shift||"aqbudgets";
910 return @
{C4
::Context
->dbh->selectcol_arrayref("SHOW columns from $tablename",{Columns
=>[1,4]})};
918 my %columns= _columns
($tablename);
919 #Filter Primary Keys of table
920 my $elements=join "|",grep {$columns{$_} ne "PRI"} keys %columns;
921 foreach my $field (grep {/\b($elements)\b/} keys %$budget){
922 $$budget{$field}=format_date_in_iso
($$budget{$field}) if ($field=~/date/ && $$budget{$field} !~C4
::Dates
->regexp("iso"));
923 my $strkeys= " $field = ? ";
924 if ($field=~/branch/){
925 $strkeys="( $strkeys OR $field='' OR $field IS NULL) ";
927 push @values, $$budget{$field};
928 push @keys, $strkeys;
930 return (\
@keys,\
@values);
933 END { } # module clean-up code here (global destructor)
940 Koha Development Team <http://koha-community.org/>