#
# This file is part of Koha.
#
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
#
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY 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.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
use strict;
#use warnings; FIXME - Bug 2505
use C4::Context;
use Koha::Database;
use C4::Debug;
-use vars qw($VERSION @ISA @EXPORT);
+use vars qw(@ISA @EXPORT);
BEGIN {
- # set the version for version checking
- $VERSION = 3.07.00.049;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(
&GetBudgetByOrderNumber
&GetBudgetByCode
&GetBudgets
+ &BudgetsByActivity
+ &GetBudgetsReport
+ &GetBudgetReport
&GetBudgetHierarchy
&AddBudget
&ModBudget
&ModBudgetPlan
- &GetCurrency
- &GetCurrencies
- &ModCurrencies
- &ConvertCurrency
-
&GetBudgetsPlanCell
&AddBudgetPlanValue
&GetBudgetAuthCats
# ----------------------------BUDGETS.PM-----------------------------";
-
=head1 FUNCTIONS ABOUT BUDGETS
=cut
return $sth->fetchrow_array;
}
-# -------------------------------------------------------------------
+=head2 GetBudgetAuthCats
+
+ my $auth_cats = &GetBudgetAuthCats($budget_period_id);
+
+Return the list of authcat for a given budget_period_id
+
+=cut
+
sub GetBudgetAuthCats {
my ($budget_period_id) = shift;
# now, populate the auth_cats_loop used in the budget planning button
$sth->execute($budget_period_id);
my %authcats;
while (my ($sort1_authcat,$sort2_authcat) = $sth->fetchrow) {
- $authcats{$sort1_authcat}=1;
- $authcats{$sort2_authcat}=1;
- }
- my @auth_cats_loop;
- foreach (sort keys %authcats) {
- push @auth_cats_loop,{ authcat => $_ };
+ $authcats{$sort1_authcat}=1 if $sort1_authcat;
+ $authcats{$sort2_authcat}=1 if $sort2_authcat;
}
- return \@auth_cats_loop;
+ return [ sort keys %authcats ];
}
# -------------------------------------------------------------------
return $data;
}
-# -------------------------------------------------------------------
sub DelBudgetPeriod{
my ($budget_period_id) = @_;
my $dbh = C4::Context->dbh;
# link child to parent
my @first_parents;
- foreach ( sort keys %links ) {
- my $child = $links{$_};
+ foreach my $budget ( sort { $a->{budget_code} cmp $b->{budget_code} } values %links ) {
+ my $child = $links{$budget->{budget_id}};
if ( $child->{'budget_parent_id'} ) {
my $parent = $links{ $child->{'budget_parent_id'} };
if ($parent) {
}
+# -------------------------------------------------------------------
+
=head2 GetBudget
&GetBudget($budget_id);
=cut
-# -------------------------------------------------------------------
sub GetBudget {
my ( $budget_id ) = @_;
my $dbh = C4::Context->dbh;
return $result;
}
+# -------------------------------------------------------------------
+
=head2 GetBudgetByOrderNumber
&GetBudgetByOrderNumber($ordernumber);
=cut
-# -------------------------------------------------------------------
sub GetBudgetByOrderNumber {
my ( $ordernumber ) = @_;
my $dbh = C4::Context->dbh;
return $result;
}
+=head2 GetBudgetReport
+
+ &GetBudgetReport( [$budget_id] );
+
+Get all orders for a specific budget, without cancelled orders.
+
+Returns an array of hashrefs.
+
+=cut
+
+# --------------------------------------------------------------------
+sub GetBudgetReport {
+ my ( $budget_id ) = @_;
+ my $dbh = C4::Context->dbh;
+ my $query = '
+ SELECT o.*, b.budget_name
+ FROM aqbudgets b
+ INNER JOIN aqorders o
+ ON b.budget_id = o.budget_id
+ WHERE b.budget_id=?
+ AND (o.orderstatus != "cancelled")
+ ORDER BY b.budget_name';
+
+ my $sth = $dbh->prepare($query);
+ $sth->execute( $budget_id );
+
+ my @results = ();
+ while ( my $data = $sth->fetchrow_hashref ) {
+ push( @results, $data );
+ }
+ return @results;
+}
+
+=head2 GetBudgetsByActivity
+
+ &GetBudgetsByActivity( $budget_period_active );
+
+Get all active or inactive budgets, depending of the value
+of the parameter.
+
+1 = active
+0 = inactive
+
+=cut
+
+# --------------------------------------------------------------------
+sub GetBudgetsByActivity {
+ my ( $budget_period_active ) = @_;
+ my $dbh = C4::Context->dbh;
+ my $query = "
+ SELECT DISTINCT b.*
+ FROM aqbudgetperiods bp
+ INNER JOIN aqbudgets b
+ ON bp.budget_period_id = b.budget_period_id
+ WHERE bp.budget_period_active=?
+ ";
+ my $sth = $dbh->prepare($query);
+ $sth->execute( $budget_period_active );
+ my @results = ();
+ while ( my $data = $sth->fetchrow_hashref ) {
+ push( @results, $data );
+ }
+ return @results;
+}
+# --------------------------------------------------------------------
+
+=head2 GetBudgetsReport
+
+ &GetBudgetsReport( [$activity] );
+
+Get all but cancelled orders for all funds.
+
+If the optionnal activity parameter is passed, returns orders for active/inactive budgets only.
+
+active = 1
+inactive = 0
+
+Returns an array of hashrefs.
+
+=cut
+
+sub GetBudgetsReport {
+ my ($activity) = @_;
+ my $dbh = C4::Context->dbh;
+ my $query = '
+ SELECT o.*, b.budget_name
+ FROM aqbudgetperiods bp
+ INNER JOIN aqbudgets b
+ ON bp.budget_period_id = b.budget_period_id
+ INNER JOIN aqorders o
+ ON b.budget_id = o.budget_id ';
+ if($activity ne ''){
+ $query .= 'WHERE bp.budget_period_active=? ';
+ }
+ $query .= 'AND (o.orderstatus != "cancelled")
+ ORDER BY b.budget_name';
+
+ my $sth = $dbh->prepare($query);
+ if($activity ne ''){
+ $sth->execute($activity);
+ }
+ else{
+ $sth->execute;
+ }
+ my @results = ();
+ while ( my $data = $sth->fetchrow_hashref ) {
+ push( @results, $data );
+ }
+ return @results;
+}
+
=head2 GetBudgetByCode
my $budget = &GetBudgetByCode($budget_code);
return 1;
}
-# -------------------------------------------------------------------
-
-=head2 GetCurrencies
-
- @currencies = &GetCurrencies;
-
-Returns the list of all known currencies.
-
-C<$currencies> is a array; its elements are references-to-hash, whose
-keys are the fields from the currency table in the Koha database.
-
-=cut
+sub _round {
+ my ($value, $increment) = @_;
-sub GetCurrencies {
- my $dbh = C4::Context->dbh;
- my $query = "
- SELECT *
- FROM currency
- ";
- my $sth = $dbh->prepare($query);
- $sth->execute;
- my @results = ();
- while ( my $data = $sth->fetchrow_hashref ) {
- push( @results, $data );
+ if ($increment && $increment != 0) {
+ $value = int($value / $increment) * $increment;
}
- return @results;
-}
-
-# -------------------------------------------------------------------
-
-sub GetCurrency {
- my $dbh = C4::Context->dbh;
- my $query = "
- SELECT * FROM currency where active = '1' ";
- my $sth = $dbh->prepare($query);
- $sth->execute;
- my $r = $sth->fetchrow_hashref;
- return $r;
-}
-
-=head2 ModCurrencies
-
-&ModCurrencies($currency, $newrate);
-
-Sets the exchange rate for C<$currency> to be C<$newrate>.
-=cut
-
-sub ModCurrencies {
- my ( $currency, $rate ) = @_;
- my $dbh = C4::Context->dbh;
- my $query = qq|
- UPDATE currency
- SET rate=?
- WHERE currency=? |;
- my $sth = $dbh->prepare($query);
- $sth->execute( $rate, $currency );
+ return $value;
}
-# -------------------------------------------------------------------
-
-=head2 ConvertCurrency
-
- $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.
-
-=cut
-
-sub ConvertCurrency {
- my ( $currency, $price ) = @_;
- my $dbh = C4::Context->dbh;
- my $query = "
- SELECT rate
- FROM currency
- WHERE currency=?
- ";
- my $sth = $dbh->prepare($query);
- $sth->execute($currency);
- my $cur = ( $sth->fetchrow_array() )[0];
- unless ($cur) {
- $cur = 1;
- }
- return ( $price / $cur );
-}
-
-
=head2 CloneBudgetPeriod
my $new_budget_period_id = CloneBudgetPeriod({
my $budget_period_startdate = $params->{budget_period_startdate};
my $budget_period_enddate = $params->{budget_period_enddate};
my $budget_period_description = $params->{budget_period_description};
+ my $amount_change_percentage = $params->{amount_change_percentage};
+ my $amount_change_round_increment = $params->{amount_change_round_increment};
my $mark_original_budget_as_inactive =
$params->{mark_original_budget_as_inactive} || 0;
my $reset_all_budgets = $params->{reset_all_budgets} || 0;
$budget_period->{budget_period_description} = $budget_period_description;
# The new budget (budget_period) should be active by default
$budget_period->{budget_period_active} = 1;
+
+ if ($amount_change_percentage) {
+ my $total = $budget_period->{budget_period_total};
+ $total += $total * $amount_change_percentage / 100;
+ $total = _round($total, $amount_change_round_increment);
+ $budget_period->{budget_period_total} = $total;
+ }
+
my $original_budget_period_id = $budget_period->{budget_period_id};
delete $budget_period->{budget_period_id};
my $new_budget_period_id = AddBudgetPeriod( $budget_period );
$budget->{budget_amount} = 0;
ModBudget( $budget );
}
+ } elsif ($amount_change_percentage) {
+ my $budgets = GetBudgets({ budget_period_id => $new_budget_period_id });
+ for my $budget ( @$budgets ) {
+ my $amount = $budget->{budget_amount};
+ $amount += $amount * $amount_change_percentage / 100;
+ $amount = _round($amount, $amount_change_round_increment);
+ $budget->{budget_amount} = $amount;
+ ModBudget( $budget );
+ }
}
return $new_budget_period_id;