Bug 18887: Port max_holds rules to new CirculationRules system
[koha.git] / admin / aqbudgets.pl
index 0cf3c61..44f0d7f 100755 (executable)
@@ -6,18 +6,18 @@
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
 
@@ -26,16 +26,15 @@ use List::Util qw/min/;
 
 use Koha::Database;
 use C4::Auth qw/get_user_subpermissions/;
-use C4::Branch; # GetBranches
-use C4::Dates qw/format_date format_date_in_iso/;
 use C4::Auth;
 use C4::Acquisition;
 use C4::Budgets;
-use C4::Members;  # calls GetSortDetails()
 use C4::Context;
 use C4::Output;
 use C4::Koha;
 use C4::Debug;
+use Koha::Acquisition::Currencies;
+use Koha::Patrons;
 
 my $input = new CGI;
 my $dbh     = C4::Context->dbh;
@@ -50,17 +49,19 @@ my ($template, $borrowernumber, $cookie, $staffflags ) = get_template_and_user(
     }
 );
 
-my $cur = GetCurrency();
-$template->param( symbol => $cur->{symbol},
-                  currency => $cur->{currency}
-               );
+my $active_currency = Koha::Acquisition::Currencies->get_active;
+if ( $active_currency ) {
+    $template->param( symbol => $active_currency->symbol,
+                      currency => $active_currency->currency
+                   );
+}
 
 my $op = $input->param('op') || 'list';
 
 # see if the user want to see all budgets or only owned ones by default
 my $show_mine = $input->param('show_mine') // 0;
 
-# IF USER DOESNT HAVE PERM FOR AN 'ADD', THEN REDIRECT TO THE DEFAULT VIEW...
+# IF USER DOESN'T HAVE PERM FOR AN 'ADD', THEN REDIRECT TO THE DEFAULT VIEW...
 if (not defined $template->{VARS}->{'CAN_user_acquisition_budget_add_del'}
     and $op eq 'add_form')
 {
@@ -69,7 +70,7 @@ if (not defined $template->{VARS}->{'CAN_user_acquisition_budget_add_del'}
 
 # get only the columns of aqbudgets in budget_hash
 my @columns = Koha::Database->new()->schema->source('Aqbudget')->columns;
-my $budget_hash = { map { join(' ',@columns) =~ /$_/ ? ( $_ => $input->param($_) )  : () } keys( %{$input->Vars()}) } ;
+my $budget_hash = { map { join(' ',@columns) =~ /$_/ ? ( $_ => scalar $input->param($_) )  : () } keys( %{$input->Vars()}) } ;
 
 my $budget_id                 = $input->param('budget_id');
 my $budget_period_id          = $input->param('budget_period_id');
@@ -88,20 +89,14 @@ if ( $budget_period_id ) {
 
 # ------- get periods stuff ------------------
 
-# USED FOR PERMISSION COMPARISON LATER
-my $borrower_id         = $template->{VARS}->{'USER_INFO'}[0]->{'borrowernumber'};
-my $user                = GetMemberDetails($borrower_id);
-my $user_branchcode     = $user->{'branchcode'};
-
 $template->param(
     show_mine   => $show_mine,
     op  => $op,
+    selected_branchcode => $filter_budgetbranch,
 );
 
 my $budget;
 
-my $branchloop = C4::Branch::GetBranchesLoop($filter_budgetbranch);
-
 $template->param(auth_cats_loop => GetBudgetAuthCats( $budget_period_id ))
     if $budget_period_id;
 
@@ -119,8 +114,7 @@ if ($op eq 'add_form') {
             exit;
         }
         $dropbox_disabled = BudgetHasChildren($budget_id);
-        my $borrower = &GetMember( borrowernumber=>$budget->{budget_owner_id} );
-        $budget->{budget_owner_name} = ( $borrower ? $borrower->{'firstname'} . ' ' . $borrower->{'surname'} : '' );
+        $budget->{budget_owner} = Koha::Patrons->find( $budget->{budget_owner_id} );
     }
 
     # build budget hierarchy
@@ -142,36 +136,11 @@ if ($op eq 'add_form') {
     }
     $budget_parent = GetBudget($budget_parent_id);
 
-    # build branches select
-    my $branches = GetBranches;
-    my @branchloop_select;
-    foreach my $thisbranch ( sort keys %$branches ) {
-        my %row = (
-            value      => $thisbranch,
-            branchname => $branches->{$thisbranch}->{'branchname'},
-        );
-        $row{selected} = 1 if $budget and $thisbranch eq $budget->{'budget_branchcode'};
-        push @branchloop_select, \%row;
-    }
-    
-    # populates the YUI planning button
-    my $categories = GetAuthorisedValueCategories();
-    my @auth_cats_loop1 = ();
-    foreach my $category (@$categories) {
-        my $entry = { category => $category,
-                        selected => ( $budget and $budget->{sort1_authcat} eq $category ? 1 : 0 ),
-                    };
-        push @auth_cats_loop1, $entry;
-    }
-    my @auth_cats_loop2 = ();
-    foreach my $category (@$categories) {
-        my $entry = { category => $category,
-                        selected => ( $budget and $budget->{sort2_authcat} eq $category ? 1 : 0 ),
-                    };
-        push @auth_cats_loop2, $entry;
-    }
-    $template->param(authorised_value_categories1 => \@auth_cats_loop1);
-    $template->param(authorised_value_categories2 => \@auth_cats_loop2);
+    # populates the planning button
+    $template->param(
+        sort1_auth => $budget->{sort1_authcat},
+        sort2_auth => $budget->{sort2_authcat},
+    );
 
     if($budget->{'budget_permission'}){
         my $budget_permission = "budget_perm_".$budget->{'budget_permission'};
@@ -182,11 +151,10 @@ if ($op eq 'add_form') {
         my @budgetusers = GetBudgetUsers($budget->{budget_id});
         my @budgetusers_loop;
         foreach my $borrowernumber (@budgetusers) {
-            my $member = C4::Members::GetMember(
-                borrowernumber => $borrowernumber);
+            my $patron = Koha::Patrons->find( $borrowernumber );
             push @budgetusers_loop, {
-                firstname => $member->{firstname},
-                surname => $member->{surname},
+                firstname => $patron->firstname, # FIXME Should pass the patron object
+                surname => $patron->surname,
                 borrowernumber => $borrowernumber
             };
         }
@@ -201,7 +169,6 @@ if ($op eq 'add_form') {
         budget_has_children => BudgetHasChildren( $budget->{budget_id} ),
         budget_parent_id                 => $budget_parent->{'budget_id'},
         budget_parent_name               => $budget_parent->{'budget_name'},
-        branchloop_select         => \@branchloop_select,
                %$period,
                %$budget,
     );
@@ -259,15 +226,13 @@ if ($op eq 'add_form') {
 }
 
 if ( $op eq 'list' ) {
-    my $branches = GetBranches();
     $template->param(
         budget_id => $budget_id,
         %$period,
     );
 
     my @budgets = @{
-        GetBudgetHierarchy($$period{budget_period_id},
-            C4::Context->userenv->{branchcode}, $show_mine ? $borrower_id : '')
+        GetBudgetHierarchy( $$period{budget_period_id}, undef, ( $show_mine ? $borrowernumber : 0 ))
     };
 
     my $period_total = 0;
@@ -278,11 +243,12 @@ if ( $op eq 'list' ) {
     my @budgets_to_display;
     foreach my $budget (@budgets) {
         # PERMISSIONS
+        next unless CanUserUseBudget($borrowernumber, $budget, $staffflags);
         unless(CanUserModifyBudget($borrowernumber, $budget, $staffflags)) {
             $budget->{'budget_lock'} = 1;
         }
 
-        # if a budget search doesnt match, next
+        # if a budget search doesn't match, next
         if ($filter_budgetname) {
             next
               unless $budget->{budget_code} =~ m/$filter_budgetname/i
@@ -303,7 +269,7 @@ if ( $op eq 'list' ) {
             $available_total += $budget->{total_remaining};
         }
 
-# if amount == 0 dont display...
+# if amount == 0 don't display...
         delete $budget->{'budget_unalloc_sublevel'}
             if (!defined $budget->{'budget_unalloc_sublevel'}
             or $budget->{'budget_unalloc_sublevel'} == 0);
@@ -343,7 +309,6 @@ if ( $op eq 'list' ) {
         spent_total            => $spent_total,
         ordered_total          => $ordered_total,
         available_total        => $available_total,
-        branchloop             => $branchloop,
         filter_budgetname      => $filter_budgetname,
     );