Bug 17969: (QA followup) Add POD
[koha.git] / C4 / Budgets.pm
index d0c4400..1c0c201 100644 (file)
@@ -21,6 +21,7 @@ use strict;
 #use warnings; FIXME - Bug 2505
 use C4::Context;
 use Koha::Database;
+use Koha::Patrons;
 use C4::Debug;
 use vars qw(@ISA @EXPORT);
 
@@ -212,7 +213,7 @@ sub GetBudgetsPlanCell {
         # 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'}%"  |
         );
@@ -221,7 +222,7 @@ sub GetBudgetsPlanCell {
         # 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
@@ -233,7 +234,7 @@ sub GetBudgetsPlanCell {
         # 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  = ? |
@@ -246,7 +247,7 @@ sub GetBudgetsPlanCell {
         # 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
@@ -325,10 +326,12 @@ sub ModBudgetPlan {
 
 # -------------------------------------------------------------------
 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
@@ -354,7 +357,7 @@ sub GetBudgetOrdered {
        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
@@ -509,7 +512,7 @@ sub GetBudgetHierarchy {
         }
     } 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;
         }
     }
@@ -545,7 +548,7 @@ sub GetBudgetHierarchy {
 
     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) {
@@ -561,11 +564,12 @@ sub GetBudgetHierarchy {
 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);
     }
 }
 
@@ -773,10 +777,11 @@ sub GetBudgetByCode {
 
     my $dbh = C4::Context->dbh;
     my $query = qq{
-        SELECT *
+        SELECT aqbudgets.*
         FROM aqbudgets
+        JOIN aqbudgetperiods USING (budget_period_id)
         WHERE budget_code = ?
-        ORDER BY budget_id DESC
+        ORDER BY budget_period_active DESC, budget_id DESC
         LIMIT 1
     };
     my $sth = $dbh->prepare( $query );
@@ -912,7 +917,9 @@ sub CanUserUseBudget {
     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);
@@ -995,7 +1002,9 @@ sub CanUserModifyBudget {
     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);