#use warnings; FIXME - Bug 2505
use C4::Context;
use Koha::Database;
+use Koha::Patrons;
use C4::Debug;
use vars qw(@ISA @EXPORT);
&GetBudgetByOrderNumber
&GetBudgetByCode
&GetBudgets
+ &BudgetsByActivity
+ &GetBudgetsReport
+ &GetBudgetReport
&GetBudgetHierarchy
&AddBudget
&ModBudget
# ----------------------------BUDGETS.PM-----------------------------";
-
=head1 FUNCTIONS ABOUT BUDGETS
=cut
# get the actual amount
$sth = $dbh->prepare( qq|
- SELECT SUM(ecost) AS actual FROM aqorders
+ SELECT SUM(ecost_tax_included) AS actual FROM aqorders
WHERE budget_id = ? AND
entrydate like "$cell->{'authvalue'}%" |
);
# get the actual amount
$sth = $dbh->prepare( qq|
- SELECT SUM(ecost) FROM aqorders
+ SELECT SUM(ecost_tax_included) FROM aqorders
LEFT JOIN aqorders_items
ON (aqorders.ordernumber = aqorders_items.ordernumber)
LEFT JOIN items
# get the actual amount
$sth = $dbh->prepare( qq|
- SELECT SUM( ecost * quantity) AS actual
+ SELECT SUM( ecost_tax_included * quantity) AS actual
FROM aqorders JOIN biblioitems
ON (biblioitems.biblionumber = aqorders.biblionumber )
WHERE aqorders.budget_id = ? and itemtype = ? |
# get the actual amount
$sth = $dbh->prepare( qq|
- SELECT SUM(ecost * quantity) AS actual
+ SELECT SUM(ecost_tax_included * quantity) AS actual
FROM aqorders
JOIN aqbudgets ON (aqbudgets.budget_id = aqorders.budget_id )
WHERE aqorders.budget_id = ? AND
# -------------------------------------------------------------------
sub GetBudgetSpent {
- my ($budget_id) = @_;
- my $dbh = C4::Context->dbh;
- my $sth = $dbh->prepare(qq|
- SELECT SUM( COALESCE(unitprice, ecost) * quantity ) AS sum FROM aqorders
+ my ($budget_id) = @_;
+ my $dbh = C4::Context->dbh;
+ # unitprice_tax_included should always been set here
+ # we should not need to retrieve ecost_tax_included
+ my $sth = $dbh->prepare(qq|
+ SELECT SUM( COALESCE(unitprice_tax_included, ecost_tax_included) * quantity ) AS sum FROM aqorders
WHERE budget_id = ? AND
quantityreceived > 0 AND
datecancellationprinted IS NULL
my ($budget_id) = @_;
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare(qq|
- SELECT SUM(ecost * quantity) AS sum FROM aqorders
+ SELECT SUM(ecost_tax_included * quantity) AS sum FROM aqorders
WHERE budget_id = ? AND
quantityreceived = 0 AND
datecancellationprinted IS NULL
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;
}
} else {
if ($branchcode) {
- push @where_strings," (budget_branchcode =? or budget_branchcode is NULL)";
+ push @where_strings," (budget_branchcode =? or budget_branchcode is NULL OR budget_branchcode='')";
push @bind_params, $branchcode;
}
}
my @sort = ();
foreach my $first_parent (@first_parents) {
- _add_budget_children(\@sort, $first_parent);
+ _add_budget_children(\@sort, $first_parent, 0);
}
foreach my $budget (@sort) {
sub _add_budget_children {
my $res = shift;
my $budget = shift;
+ $budget->{budget_level} = shift;
push @$res, $budget;
my $children = $budget->{'children'} || [];
return unless @$children; # break recursivity
foreach my $child (@$children) {
- _add_budget_children($res, $child);
+ _add_budget_children($res, $child, $budget->{budget_level} + 1);
}
}
}
+# -------------------------------------------------------------------
+
=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);
my ($borrower, $budget, $userflags) = @_;
if (not ref $borrower) {
- $borrower = C4::Members::GetMember(borrowernumber => $borrower);
+ $borrower = Koha::Patrons->find( $borrower );
+ return 0 unless $borrower;
+ $borrower = $borrower->unblessed;
}
if (not ref $budget) {
$budget = GetBudget($budget);
my ($borrower, $budget, $userflags) = @_;
if (not ref $borrower) {
- $borrower = C4::Members::GetMember(borrowernumber => $borrower);
+ $borrower = Koha::Patrons->find( $borrower );
+ return 0 unless $borrower;
+ $borrower = $borrower->unblessed;
}
if (not ref $budget) {
$budget = GetBudget($budget);