- $sth->execute( $budget_id );
- my $result = $sth->fetchall_arrayref({});
- my $total_spent = GetBudgetSpent($budget_id);
- if ($result){
- $total_spent += GetChildBudgetsSpent($_->{"budget_id"}) foreach @$result;
+ $sth->execute( $ordernumber );
+ my $result = $sth->fetchrow_hashref;
+ return $result;
+}
+
+=head2 GetBudgetByCode
+
+ my $budget = &GetBudgetByCode($budget_code);
+
+Retrieve all aqbudgets fields as a hashref for the budget that has
+given budget_code
+
+=cut
+
+sub GetBudgetByCode {
+ my ( $budget_code ) = @_;
+
+ my $dbh = C4::Context->dbh;
+ my $query = qq{
+ SELECT *
+ FROM aqbudgets
+ WHERE budget_code = ?
+ ORDER BY budget_id DESC
+ LIMIT 1
+ };
+ my $sth = $dbh->prepare( $query );
+ $sth->execute( $budget_code );
+ return $sth->fetchrow_hashref;
+}
+
+=head2 GetBudgetHierarchySpent
+
+ my $spent = GetBudgetHierarchySpent( $budget_id );
+
+Gets the total spent of the level and sublevels of $budget_id
+
+=cut
+
+sub GetBudgetHierarchySpent {
+ my ( $budget_id ) = @_;
+ my $dbh = C4::Context->dbh;
+ my $children_ids = $dbh->selectcol_arrayref(q|
+ SELECT budget_id
+ FROM aqbudgets
+ WHERE budget_parent_id = ?
+ |, {}, $budget_id );
+
+ my $total_spent = GetBudgetSpent( $budget_id );
+ for my $child_id ( @$children_ids ) {
+ $total_spent += GetBudgetHierarchySpent( $child_id );