+=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;
+}
+