Bug 9044: (follow-up) fix merge conflict typo that broke this script
[koha.git] / C4 / Budgets.pm
index e9732ac..334e610 100644 (file)
@@ -34,6 +34,7 @@ BEGIN {
        @EXPORT = qw(
 
         &GetBudget
+        &GetBudgetByOrderNumber
         &GetBudgets
         &GetBudgetHierarchy
            &AddBudget
@@ -41,6 +42,7 @@ BEGIN {
         &DelBudget
         &GetBudgetSpent
         &GetBudgetOrdered
+        &GetBudgetName
         &GetPeriodsCount
         &GetChildBudgetsSpent
 
@@ -314,9 +316,19 @@ sub GetBudgetSpent {
             quantityreceived > 0 AND
             datecancellationprinted IS NULL
     |);
-
        $sth->execute($budget_id);
        my $sum =  $sth->fetchrow_array;
+
+    $sth = $dbh->prepare(qq|
+        SELECT SUM(shipmentcost) AS sum
+        FROM aqinvoices
+        WHERE shipmentcost_budgetid = ?
+          AND closedate IS NOT NULL
+    |);
+    $sth->execute($budget_id);
+    my ($shipmentcost_sum) = $sth->fetchrow_array;
+    $sum += $shipmentcost_sum;
+
        return $sum;
 }
 
@@ -330,12 +342,44 @@ sub GetBudgetOrdered {
             quantityreceived = 0 AND
             datecancellationprinted IS NULL
     |);
-
        $sth->execute($budget_id);
        my $sum =  $sth->fetchrow_array;
+
+    $sth = $dbh->prepare(qq|
+        SELECT SUM(shipmentcost) AS sum
+        FROM aqinvoices
+        WHERE shipmentcost_budgetid = ?
+          AND closedate IS NULL
+    |);
+    $sth->execute($budget_id);
+    my ($shipmentcost_sum) = $sth->fetchrow_array;
+    $sum += $shipmentcost_sum;
+
        return $sum;
 }
 
+=head2 GetBudgetName
+
+  my $budget_name = &GetBudgetName($budget_id);
+
+get the budget_name for a given budget_id
+
+=cut
+
+sub GetBudgetName {
+    my ( $budget_id ) = @_;
+    my $dbh         = C4::Context->dbh;
+    my $sth         = $dbh->prepare(
+        qq|
+        SELECT budget_name
+        FROM aqbudgets
+        WHERE budget_id = ?
+    |);
+
+    $sth->execute($budget_id);
+    return $sth->fetchrow_array;
+}
+
 # -------------------------------------------------------------------
 sub GetBudgetAuthCats  {
     my ($budget_period_id) = shift;
@@ -359,19 +403,32 @@ sub GetBudgetAuthCats  {
 # -------------------------------------------------------------------
 sub GetAuthvalueDropbox {
     my ( $authcat, $default ) = @_;
+    my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
     my $dbh = C4::Context->dbh;
-    my $sth = $dbh->prepare(
-        'SELECT authorised_value,lib FROM authorised_values
-        WHERE category = ? ORDER BY lib'
-    );
-    $sth->execute( $authcat );
+
+    my $query = qq{
+        SELECT *
+        FROM authorised_values
+    };
+    $query .= qq{
+          LEFT JOIN authorised_values_branches ON ( id = av_id )
+    } if $branch_limit;
+    $query .= qq{
+        WHERE category = ?
+    };
+    $query .= " AND ( branchcode = ? OR branchcode IS NULL )" if $branch_limit;
+    $query .= " GROUP BY lib ORDER BY category, lib, lib_opac";
+    my $sth = $dbh->prepare($query);
+    $sth->execute( $authcat, $branch_limit ? $branch_limit : () );
+
+
     my $option_list = [];
     my @authorised_values = ( q{} );
-    while (my ($value, $lib) = $sth->fetchrow_array) {
+    while (my $av = $sth->fetchrow_hashref) {
         push @{$option_list}, {
-            value => $value,
-            label => $lib,
-            default => ($default eq $value),
+            value => $av->{authorised_value},
+            label => $av->{lib},
+            default => ($default eq $av->{authorised_value}),
         };
     }
 
@@ -616,6 +673,30 @@ sub GetBudget {
     return $result;
 }
 
+=head2 GetBudgetByOrderNumber
+
+  &GetBudgetByOrderNumber($ordernumber);
+
+get a specific budget by order number
+
+=cut
+
+# -------------------------------------------------------------------
+sub GetBudgetByOrderNumber {
+    my ( $ordernumber ) = @_;
+    my $dbh = C4::Context->dbh;
+    my $query = "
+        SELECT aqbudgets.*
+        FROM   aqbudgets, aqorders
+        WHERE  ordernumber=?
+        AND    aqorders.budget_id = aqbudgets.budget_id
+        ";
+    my $sth = $dbh->prepare($query);
+    $sth->execute( $ordernumber );
+    my $result = $sth->fetchrow_hashref;
+    return $result;
+}
+
 =head2 GetChildBudgetsSpent
 
   &GetChildBudgetsSpent($budget-id);
@@ -653,7 +734,8 @@ gets all budgets
 
 # -------------------------------------------------------------------
 sub GetBudgets {
-    my ($filters,$orderby) = @_;
+    my $filters = shift;
+    my $orderby = shift || 'budget_name';
     return SearchInTable("aqbudgets",$filters, $orderby, undef,undef, undef, "wide");
 }