# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
-# You should have received a copy of the GNU General Public License along with
-# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
-# Suite 330, Boston, MA 02111-1307 USA
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
use strict;
+#use warnings; FIXME - Bug 2505
use C4::Context;
use C4::Dates qw(format_date format_date_in_iso);
use C4::SQLHelper qw<:all>;
BEGIN {
# set the version for version checking
- $VERSION = 3.01;
+ $VERSION = 3.07.00.049;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(
&GetBudgetSpent
&GetBudgetOrdered
&GetPeriodsCount
+ &GetChildBudgetsSpent
+
+ &GetBudgetUsers
+ &ModBudgetUsers
+ &CanUserUseBudget
+ &CanUserModifyBudget
&GetBudgetPeriod
&GetBudgetPeriods
&AddBudgetPeriod
&DelBudgetPeriod
- &GetBudgetPeriodsDropbox
- &GetBudgetSortDropbox
&GetAuthvalueDropbox
- &GetBudgetPermDropbox
&ModBudgetPlan
# ----------------------------BUDGETS.PM-----------------------------";
+=head1 FUNCTIONS ABOUT BUDGETS
+
+=cut
+
sub HideCols {
my ( $authcat, @hide_cols ) = @_;
my $dbh = C4::Context->dbh;
my ($budget_id) = @_;
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare(qq|
- SELECT SUM(ecost * quantity) AS sum FROM aqorders
+ SELECT SUM( COALESCE(unitprice, ecost) * quantity ) AS sum FROM aqorders
WHERE budget_id = ? AND
quantityreceived > 0 AND
datecancellationprinted IS NULL
|);
-
$sth->execute($budget_id);
my $sum = $sth->fetchrow_array;
+
+ $sth = $dbh->prepare(qq|
+ SELECT SUM(shipmentcost) AS sum
+ FROM aqinvoices
+ WHERE shipmentcost_budgetid = ?
+ AND closedate IS NOT NULL
+ |);
+ $sth->execute($budget_id);
+ my ($shipmentcost_sum) = $sth->fetchrow_array;
+ $sum += $shipmentcost_sum;
+
return $sum;
}
quantityreceived = 0 AND
datecancellationprinted IS NULL
|);
-
$sth->execute($budget_id);
my $sum = $sth->fetchrow_array;
- return $sum;
-}
-# -------------------------------------------------------------------
-sub GetBudgetPermDropbox {
- my ($perm) = @_;
- my %labels;
- $labels{'0'} = 'None';
- $labels{'1'} = 'Owner';
- $labels{'2'} = 'Library';
- my $radio = CGI::scrolling_list(
- -id => 'budget_permission',
- -name => 'budget_permission',
- -values => [ '0', '1', '2' ],
- -default => $perm,
- -labels => \%labels,
- -size => 1,
- );
- return $radio;
+ $sth = $dbh->prepare(qq|
+ SELECT SUM(shipmentcost) AS sum
+ FROM aqinvoices
+ WHERE shipmentcost_budgetid = ?
+ AND closedate IS NULL
+ |);
+ $sth->execute($budget_id);
+ my ($shipmentcost_sum) = $sth->fetchrow_array;
+ $sum += $shipmentcost_sum;
+
+ return $sum;
}
# -------------------------------------------------------------------
# -------------------------------------------------------------------
sub GetAuthvalueDropbox {
- my ( $name, $authcat, $default ) = @_;
- my @authorised_values;
- my %authorised_lib;
- my $value;
- my $dbh = C4::Context->dbh;
- my $sth = $dbh->prepare(
- "SELECT authorised_value,lib
- FROM authorised_values
- WHERE category = ?
- ORDER BY lib"
- );
- $sth->execute( $authcat );
-
- push @authorised_values, '';
- while (my ($value, $lib) = $sth->fetchrow_array) {
- push @authorised_values, $value;
- $authorised_lib{$value} = $lib;
- }
-
- return 0 if keys(%authorised_lib) == 0;
-
- my $budget_authvalue_dropbox = CGI::scrolling_list(
- -values => \@authorised_values,
- -labels => \%authorised_lib,
- -default => $default,
- -override => 1,
- -size => 1,
- -multiple => 0,
- -name => $name,
- -id => $name,
+ my ( $authcat, $default ) = @_;
+ my $dbh = C4::Context->dbh;
+ my $sth = $dbh->prepare(
+ 'SELECT authorised_value,lib FROM authorised_values
+ WHERE category = ? ORDER BY lib'
);
+ $sth->execute( $authcat );
+ my $option_list = [];
+ my @authorised_values = ( q{} );
+ while (my ($value, $lib) = $sth->fetchrow_array) {
+ push @{$option_list}, {
+ value => $value,
+ label => $lib,
+ default => ($default eq $value),
+ };
+ }
- return $budget_authvalue_dropbox
-}
-
-# -------------------------------------------------------------------
-sub GetBudgetPeriodsDropbox {
- my ($budget_period_id) = @_;
- my %labels;
- my @values;
- my ($active, $periods) = GetBudgetPeriods();
- foreach my $r (@$periods) {
- $labels{"$r->{budget_period_id}"} = $r->{budget_period_description};
- push @values, $r->{budget_period_id};
- }
-
- # if no buget_id is passed then its an add
- my $budget_period_dropbox = CGI::scrolling_list(
- -name => 'budget_period_id',
- -values => \@values,
- -default => $budget_period_id ? $budget_period_id : $active,
- -size => 1,
- -labels => \%labels,
- );
- return $budget_period_dropbox;
+ if ( @{$option_list} ) {
+ return $option_list;
+ }
+ return;
}
# -------------------------------------------------------------------
# -------------------------------------------------------------------
sub GetBudgetHierarchy {
- my ($budget_period_id, $branchcode, $owner) = @_;
- my @bind_params;
- my $dbh = C4::Context->dbh;
- my $query = qq|
- SELECT aqbudgets.*
- FROM aqbudgets |;
- # show only period X if requested
+ my ( $budget_period_id, $branchcode, $owner ) = @_;
+ my @bind_params;
+ my $dbh = C4::Context->dbh;
+ my $query = qq|
+ SELECT aqbudgets.*, aqbudgetperiods.budget_period_active
+ FROM aqbudgets
+ JOIN aqbudgetperiods USING (budget_period_id)|;
+
my @where_strings;
+ # show only period X if requested
if ($budget_period_id) {
push @where_strings," aqbudgets.budget_period_id = ?";
push @bind_params, $budget_period_id;
# show only budgets owned by me, my branch or everyone
if ($owner) {
if ($branchcode) {
- 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="")))};
- push @bind_params, ($owner, $branchcode);
+ 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="")))};
+ push @bind_params, ( $owner, $branchcode );
} else {
push @where_strings, ' (budget_owner_id = ? OR budget_owner_id IS NULL or budget_owner_id ="") ';
push @bind_params, $owner;
# add budget-percent and allocation, and flags for html-template
foreach my $r (@sort) {
my $subs_href = $r->{'child'};
- my @subs_arr = @$subs_href if defined $subs_href;
+ my @subs_arr = ();
+ if ( defined $subs_href ) {
+ @subs_arr = @{$subs_href};
+ }
my $moo = $r->{'budget_code_indent'};
$moo =~ s/\ /\ \;/g;
return $rc;
}
-=head2 FUNCTIONS ABOUT BUDGETS
-
-=over 2
-
-=cut
-
-=back
-=head3 GetBudget
+=head2 GetBudget
-=over 4
-
-&GetBudget($budget_id);
+ &GetBudget($budget_id);
get a specific budget
-=back
-
=cut
# -------------------------------------------------------------------
return $result;
}
-=head3 GetBudgets
+=head2 GetChildBudgetsSpent
-=over 4
+ &GetChildBudgetsSpent($budget-id);
-&GetBudgets($filter, $order_by);
+gets the total spent of the level and sublevels of $budget_id
-gets all budgets
+=cut
+
+# -------------------------------------------------------------------
+sub GetChildBudgetsSpent {
+ my ( $budget_id ) = @_;
+ my $dbh = C4::Context->dbh;
+ my $query = "
+ SELECT *
+ FROM aqbudgets
+ WHERE budget_parent_id=?
+ ";
+ my $sth = $dbh->prepare($query);
+ $sth->execute( $budget_id );
+ my $result = $sth->fetchall_arrayref({});
+ my $total_spent = GetBudgetSpent($budget_id);
+ if ($result){
+ $total_spent += GetChildBudgetsSpent($_->{"budget_id"}) foreach @$result;
+ }
+ return $total_spent;
+}
+
+=head2 GetBudgets
+
+ &GetBudgets($filter, $order_by);
-=back
+gets all budgets
=cut
return SearchInTable("aqbudgets",$filters, $orderby, undef,undef, undef, "wide");
}
+=head2 GetBudgetUsers
+
+ my @borrowernumbers = &GetBudgetUsers($budget_id);
+
+Return the list of borrowernumbers linked to a budget
+
+=cut
+
+sub GetBudgetUsers {
+ my ($budget_id) = @_;
+
+ my $dbh = C4::Context->dbh;
+ my $query = qq{
+ SELECT borrowernumber
+ FROM aqbudgetborrowers
+ WHERE budget_id = ?
+ };
+ my $sth = $dbh->prepare($query);
+ $sth->execute($budget_id);
+
+ my @borrowernumbers;
+ while (my ($borrowernumber) = $sth->fetchrow_array) {
+ push @borrowernumbers, $borrowernumber
+ }
+
+ return @borrowernumbers;
+}
+
+=head2 ModBudgetUsers
+
+ &ModBudgetUsers($budget_id, @borrowernumbers);
+
+Modify the list of borrowernumbers linked to a budget
+
+=cut
+
+sub ModBudgetUsers {
+ my ($budget_id, @budget_users_id) = @_;
+
+ return unless $budget_id;
+
+ my $dbh = C4::Context->dbh;
+ my $query = "DELETE FROM aqbudgetborrowers WHERE budget_id = ?";
+ my $sth = $dbh->prepare($query);
+ $sth->execute($budget_id);
+
+ $query = qq{
+ INSERT INTO aqbudgetborrowers (budget_id, borrowernumber)
+ VALUES (?,?)
+ };
+ $sth = $dbh->prepare($query);
+ foreach my $borrowernumber (@budget_users_id) {
+ next unless $borrowernumber;
+ $sth->execute($budget_id, $borrowernumber);
+ }
+}
+
+sub CanUserUseBudget {
+ my ($borrower, $budget, $userflags) = @_;
+
+ if (not ref $borrower) {
+ $borrower = C4::Members::GetMember(borrowernumber => $borrower);
+ }
+ if (not ref $budget) {
+ $budget = GetBudget($budget);
+ }
+
+ return 0 unless ($borrower and $budget);
+
+ if (not defined $userflags) {
+ $userflags = C4::Auth::getuserflags($borrower->{flags},
+ $borrower->{userid});
+ }
+
+ unless ($userflags->{superlibrarian}
+ || (ref $userflags->{acquisition}
+ && $userflags->{acquisition}->{budget_manage_all})
+ || (!ref $userflags->{acquisition} && $userflags->{acquisition}))
+ {
+ if (not exists $userflags->{acquisition}) {
+ return 0;
+ }
+
+ if (!ref $userflags->{acquisition} && !$userflags->{acquisition}) {
+ return 0;
+ }
+
+ # Budget restricted to owner
+ if ($budget->{budget_permission} == 1
+ && $budget->{budget_owner_id}
+ && $budget->{budget_owner_id} != $borrower->{borrowernumber}) {
+ return 0;
+ }
+
+ my @budget_users = GetBudgetUsers($budget->{budget_id});
+
+ # Budget restricted to owner, users and library
+ if ($budget->{budget_permission} == 2
+ && $budget->{budget_owner_id}
+ && $budget->{budget_owner_id} != $borrower->{borrowernumber}
+ && (0 == grep {$borrower->{borrowernumber} == $_} @budget_users)
+ && defined $budget->{budget_branchcode}
+ && $budget->{budget_branchcode} ne C4::Context->userenv->{branch}) {
+ return 0;
+ }
+
+ # Budget restricted to owner and users
+ if ($budget->{budget_permission} == 3
+ && $budget->{budget_owner_id}
+ && $budget->{budget_owner_id} != $borrower->{borrowernumber}
+ && (0 == grep {$borrower->{borrowernumber} == $_} @budget_users)) {
+ return 0;
+ }
+ }
+
+ return 1;
+}
+
+sub CanUserModifyBudget {
+ my ($borrower, $budget, $userflags) = @_;
+
+ if (not ref $borrower) {
+ $borrower = C4::Members::GetMember(borrowernumber => $borrower);
+ }
+ if (not ref $budget) {
+ $budget = GetBudget($budget);
+ }
+
+ return 0 unless ($borrower and $budget);
+
+ if (not defined $userflags) {
+ $userflags = C4::Auth::getuserflags($borrower->{flags},
+ $borrower->{userid});
+ }
+
+ unless ($userflags->{superlibrarian}
+ || (ref $userflags->{acquisition}
+ && $userflags->{acquisition}->{budget_manage_all})
+ || (!ref $userflags->{acquisition} && $userflags->{acquisition}))
+ {
+ if (!CanUserUseBudget($borrower, $budget, $userflags)) {
+ return 0;
+ }
+
+ if (ref $userflags->{acquisition}
+ && !$userflags->{acquisition}->{budget_modify}) {
+ return 0;
+ }
+ }
+
+ return 1;
+}
+
# -------------------------------------------------------------------
-=head3 GetCurrencies
+=head2 GetCurrencies
-@currencies = &GetCurrencies;
+ @currencies = &GetCurrencies;
Returns the list of all known currencies.
return $r;
}
-=head3 ModCurrencies
+=head2 ModCurrencies
&ModCurrencies($currency, $newrate);
# -------------------------------------------------------------------
-=head3 ConvertCurrency
+=head2 ConvertCurrency
-$foreignprice = &ConvertCurrency($currency, $localprice);
+ $foreignprice = &ConvertCurrency($currency, $localprice);
Converts the price C<$localprice> to foreign currency C<$currency> by
dividing by the exchange rate, and returns the result.
-If no exchange rate is found,e is one
-to one.
+If no exchange rate is found, e is one to one.
=cut
return ( $price / $cur );
}
-=head3 _columns
+=head2 _columns
- returns an array containing fieldname followed by PRI as value if PRIMARY Key
+returns an array containing fieldname followed by PRI as value if PRIMARY Key
=cut
=head1 AUTHOR
-Koha Developement team <info@koha.org>
+Koha Development Team <http://koha-community.org/>
=cut