Merge remote-tracking branch 'origin/new/bug_7751'
[koha.git] / admin / aqplan.pl
index 8eb95e1..9cc8c59 100755 (executable)
 # 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., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# 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.
 
 #script to administer the aqbudgets0 table
 #written 20/02/2002 by paul.poulain@free.fr
 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
 
 use strict;
+#use warnings; FIXME - Bug 2505
 use CGI;
 use List::Util qw/min/;
 use Date::Calc qw/Delta_YMD Easter_Sunday Today Decode_Date_EU/;
 use Date::Manip qw/ ParseDate UnixDate DateCalc/;
 use C4::Dates qw/format_date format_date_in_iso/;
-use Number::Format qw(format_price);      
+use Number::Format qw(format_price);
 use Text::CSV_XS;
 
 use C4::Acquisition;
@@ -47,9 +48,9 @@ my ( $template, $borrowernumber, $cookie, $staff_flags ) = get_template_and_user
     {   template_name   => "admin/aqplan.tmpl",
         query           => $input,
         type            => "intranet",
-        authnotrequired => 1,
+        authnotrequired => 0,
         flagsrequired   => { acquisition => 'planning_manage' },
-        debug           => 1,
+        debug           => 0,
     }
 );
 
@@ -59,30 +60,33 @@ my $budget_period_id = $input->param('budget_period_id');
 my $period = GetBudgetPeriod($budget_period_id);
 my $count  = GetPeriodsCount();
 my $cur    = GetCurrency;
-
+$template->param( symbol => $cur->{symbol},
+                  currency => $cur->{currency}
+               );
 $template->param( period_button_only => 1 ) if $count == 0;
 
+
+
 # authcats_loop populates the YUI planning button
-my @auth_cats_loop            = GetBudgetAuthCats();
+my $auth_cats_loop            = GetBudgetAuthCats($budget_period_id);
 my $budget_period_id          = $period->{'budget_period_id'};
 my $budget_period_startdate   = $period->{'budget_period_startdate'};
 my $budget_period_enddate     = $period->{'budget_period_enddate'};
 my $budget_period_locked      = $period->{'budget_period_locked'};
 my $budget_period_description = $period->{'budget_period_description'};
-my $budget_period_dropbox     = GetBudgetPeriodsDropbox($budget_period_id );
+
 
 $template->param(
     budget_period_id          => $budget_period_id,
     budget_period_locked      => $budget_period_locked,
     budget_period_description => $budget_period_description,
-    budget_period_dropbox     => $budget_period_dropbox,
-    auth_cats_loop            => \@auth_cats_loop,
+    auth_cats_loop            => $auth_cats_loop,
 );
 
 # ------- get periods stuff ------------------
 
-my $borrower_id         = $template->{param_map}->{'USER_INFO'}[0]->{'borrowernumber'};
-my $borrower_branchcode = $template->{param_map}->{'USER_INFO'}[0]->{'branchcode'};
+my $borrower_id         = $template->{VARS}->{'USER_INFO'}[0]->{'borrowernumber'};
+my $borrower_branchcode = $template->{VARS}->{'USER_INFO'}[0]->{'branchcode'};
 
 my $periods;
 my $authcat      = $input->param('authcat');
@@ -91,12 +95,11 @@ my $show_actual  = $input->param('show_actual');
 my $show_percent = $input->param('show_percent');
 my $output       = $input->param("output");
 my $basename     = $input->param("basename");
-my $mime         = $input->param("MIME");
 my $del          = $input->param("sep");
 
-my $show_mine    = 1; #SHOW BY DEFAULT
-my $show         = $input->param('show'); # SET TO 1, BY A FORM SUMBIT
-$show_mine       = $input->param('show_mine') if $show == 1;
+my $show_mine       = $input->param('show_mine') ;
+
+my @hide_cols      = $input->param('hide_cols');
 
 my $cur_format = C4::Context->preference("CurrencyFormat");
 my $num;
@@ -118,7 +121,7 @@ if ( $cur_format eq 'FR' ) {
     );
 }
 
-if ( $budget_period_locked == 1  && not defined  $show_actual ) { 
+if ( $budget_period_locked == 1  && not defined  $show_actual ) {
      $show_actual  = 1;
 }
 
@@ -127,7 +130,9 @@ $authcat = 'Asort1' if  not defined $authcat; # defaults to Asort if no authcat
 my $budget_id = $input->param('budget_id');
 my $op        = $input->param("op");
 
-my $budgets_ref = GetBudgetHierarchy( $budget_period_id, $show_mine?$template->{param_map}->{'USER_INFO'}[0]->{'branchcode'}:'', $show_mine?$template->{param_map}->{'USER_INFO'}[0]->{'borrowernumber'}:'' );
+my $budget_branchcode;
+
+my $budgets_ref = GetBudgetHierarchy( $budget_period_id, $show_mine?$template->{VARS}->{'USER_INFO'}[0]->{'branchcode'}:'', $show_mine?$template->{VARS}->{'USER_INFO'}[0]->{'borrowernumber'}:'' );
 
 # build categories list
 my $sth = $dbh->prepare("select distinct category from authorised_values where category like 'A%' ");
@@ -143,11 +148,11 @@ while ( my ($category) = $sth->fetchrow_array ) {
     $categories{$category} = 1;
 }
 
+# push koha system categories
 push( @category_list, 'MONTHS' );
 push( @category_list, 'ITEMTYPES' );
 push( @category_list, 'BRANCHES' );
-
-# push koha system categories
+push( @category_list, $$_{'authcat'} ) foreach @$auth_cats_loop;
 
 #reorder the list
 @category_list = sort { $a cmp $b } @category_list;
@@ -168,13 +173,62 @@ my $CGISort;
 my @authvals;
 my %labels;
 
-if ( $authcat =~ m/^Asort/ ) {
+my @names = $input->param();
+# ------------------------------------------------------------
+if ( $op eq 'save' ) {
+    #get budgets
+    my ( @buds, @auth_values );
+    foreach my $n (@names) {
+        next if $n =~ m/^[^0-9]/;
+        my @moo = split( ',', $n );
+        push @buds, $moo[0];
+        push @auth_values, $moo[1];
+    }
+
+    #uniq buds and auth
+    my %seen;
+    @buds        = grep { !$seen{$_}++ } @buds;
+    @auth_values = grep { !$seen{$_}++ } @auth_values;
+    my @budget_ids;
+    my @budget_lines;
+
+    foreach my $budget (@buds) {
+        my %budget_line;
+        my @cells_line;
+        my %cell_hash;
+
+        foreach my $authvalue (@auth_values) {
+            # get actual stats
+            my $cell_name        = "$budget,$authvalue";
+            my $estimated_amount = $input->param("$cell_name");
+            my %cell_hash = (
+                estimated_amount => $estimated_amount,
+                authvalue        => $authvalue,
+                authcat          => $authcat,
+                budget_id        => $budget,
+                budget_period_id => $budget_period_id,
+            );
+            push( @cells_line, \%cell_hash );
+        }
+
+        %budget_line = (
+            lines => \@cells_line,
+        );
+        push( @budget_lines, \%budget_line );
+    }
+    my $plan = \@budget_lines;
+    ModBudgetPlan( $plan, $budget_period_id, $authcat );
+
+HideCols($authcat, @hide_cols);
 
+
+}
+# ------------------------------------------------------------
+if ( $authcat =~ m/^Asort/ ) {
     # ----------- copied from C4::Input::buildCGIsort()
-    my $query = qq{SELECT * FROM authorised_values WHERE category=? order by lib};
+   my $query = qq{ SELECT * FROM authorised_values WHERE category=? order by lib };
     my $sth   = $dbh->prepare($query);
-    $sth->execute($authcat);
-
+    $sth->execute($authcat  );
     if ( $sth->rows > 0 ) {
         for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
             my $results = $sth->fetchrow_hashref;
@@ -184,7 +238,6 @@ if ( $authcat =~ m/^Asort/ ) {
     }
     $sth->finish;
     @authvals = sort { $a <=> $b } @authvals;
-
 }
 elsif ( $authcat eq 'MONTHS' ) {
 
@@ -210,10 +263,9 @@ elsif ( $authcat eq 'MONTHS' ) {
 }
 
 elsif ( $authcat eq 'ITEMTYPES' ) {
-
     my $query = qq| SELECT itemtype, description FROM itemtypes |;
     my $sth   = $dbh->prepare($query);
-    $sth->execute();
+    $sth->execute(  );
 
     if ( $sth->rows > 0 ) {
         for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
@@ -238,73 +290,51 @@ elsif ( $authcat eq 'ITEMTYPES' ) {
         }
     }
     $sth->finish;
+} elsif ($authcat) {
+    my $query = qq{ SELECT * FROM authorised_values WHERE category=? order by lib };
+    my $sth   = $dbh->prepare($query);
+    $sth->execute($authcat);
+    if ( $sth->rows > 0 ) {
+        for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
+            my $results = $sth->fetchrow_hashref;
+            push @authvals, $results->{authorised_value};
+            $labels{ $results->{authorised_value} } = $results->{lib};
+        }
+    }
+    $sth->finish;
 }
 
 my @authvals_row;
+my $i=1;
 foreach my $val (@authvals) {
     my %auth_hash;
     $auth_hash{val} =   $labels{$val};
+    $auth_hash{code} =   $val;
+    $auth_hash{colnum} =   $i++;
+
+    # display lookup
+    $auth_hash{display} = GetCols( $authcat,  $auth_hash{code});
+
     push( @authvals_row, \%auth_hash );
 }
 
-# ------------------------------------------------------------
-if ( $op eq 'save' ) {
-    ### ---------------------  save
-    my @names = $input->param();
-
-    #get budgets
-    my ( @buds, @auth_values );
-    foreach my $n (@names) {
-        next if $n =~ m/^[^0-9]/;
-        $n =~ m/(\d*),(.*)/;
-        push @buds, $1;
-        push @auth_values, $2;
-    }
+#get budgets
+my ( @buds, @auth_values );
+foreach my $n (@names) {
+    next if $n =~ m/^[^0-9]/;
+    $n =~ m/(\d*),(.*)/;
+    push @buds, $1;
+    push @auth_values, $2;
+}
 
-    #uniq buds and auth
-    my %seen;
-    @buds        = grep { !$seen{$_}++ } @buds;
-    @auth_values = grep { !$seen{$_}++ } @auth_values;
-    my @budget_ids;
-    my @budget_lines;
 
-    foreach my $budget (@buds) {
-        my %budget_line;
-        my @cells_line;
-        my %cell_hash;
-        foreach my $authvalue (@auth_values) {
-            # get actual stats
-            my $cell_name        = "$budget,$authvalue";
-            my $estimated_amount = $input->param("$cell_name");
-            my %cell_hash = (
-                estimated_amount => $estimated_amount,
-                authvalue        => $authvalue,
-                authcat          => $authcat,
-                budget_id        => $budget,
-                budget_period_id => $budget_period_id,
-            );
-            push( @cells_line, \%cell_hash );
-        }
-        %budget_line = (
-            lines => \@cells_line,
-        );
-        push( @budget_lines, \%budget_line );
-    }
-    my $plan = \@budget_lines;
-    ModBudgetPlan( $plan, $budget_period_id, $authcat );
-}
 # ------------------------------------------------------------
 #         DEFAULT DISPLAY BEGINS
 
-my @mime = ( C4::Context->preference("MIME") );
-foreach my $mime (@mime) {
-    #               warn "".$mime;
-}
-
 my $CGIextChoice = CGI::scrolling_list(
     -name     => 'MIME',
     -id       => 'MIME',
-    -values   => \@mime,
+    -values   => ['CSV'], # FIXME translation
     -size     => 1,
     -multiple => 0
 );
@@ -320,9 +350,14 @@ my $CGIsepChoice = CGI::scrolling_list(
 
 my ( @budget_lines, %cell_hash );
 
+
 foreach my $budget (@budgets) {
     my $budget_lock;
 
+    unless (CanUserUseBudget($borrowernumber, $budget, $staff_flags)) {
+        $budget_lock = 1
+    }
+
     # check budget permission
     if ( $period->{budget_period_locked} == 1 ) {
         $budget_lock = 1;
@@ -342,10 +377,12 @@ foreach my $budget (@budgets) {
             && $budget->{'sort2_authcat'} ne $authcat );
     }
 
-    my %budget_line;
+    my %budget_line; # each row of the  table
     my @cells_line;
     my $actual_spent;
     my $estimated_spent;
+
+    my $i = 0;
     foreach my $authvalue (@authvals) {
 
         # get actual stats
@@ -359,15 +396,20 @@ foreach my $budget (@budgets) {
             budget_lock      => $budget_lock,
         );
 
-        my ( $actual, $estimated ) = GetBudgetsPlanCell( \%cell, $period, $budget );
+        my ( $actual, $estimated, $display ) = GetBudgetsPlanCell( \%cell, $period, $budget );
         $cell{actual_amount}    = sprintf( "%.2f", $actual );
         $cell{estimated_amount} = sprintf( "%.2f", $estimated );
+        $cell{display}          = $authvals_row[$i]{display};
+        $cell{colnum}           = $i;
+
         $actual_spent    += $cell{actual_amount};
         $estimated_spent += $cell{estimated_amount};
+
+
         push( @cells_line, \%cell );
+        $i++;
     }
 
-    #     lines => \@cells_line,
     my $budget_act_remain = $budget->{budget_amount} - $actual_spent;
     my $budget_est_remain = $budget->{budget_amount} - $estimated_spent;
 
@@ -379,9 +421,12 @@ foreach my $budget (@budgets) {
         budget_alloc            => $budget->{budget_alloc},
         budget_act_remain       => sprintf( "%.2f", $budget_act_remain ),
         budget_est_remain       => sprintf( "%.2f", $budget_est_remain ),
-        budget_id               => $budget->{budget_id}
+        budget_id               => $budget->{budget_id},
+        budget_lock             => $budget_lock,
     );
 
+
+
     $budget_line{est_negative} = '1' if $budget_est_remain < 0;
     $budget_line{est_positive} = '1' if $budget_est_remain > 0;
     $budget_line{act_negative} = '1' if $budget_act_remain < 0;
@@ -398,7 +443,6 @@ if ( $output eq "file" ) {
     exit(1);
 }
 
-    ## ## @budget_lines
 $template->param(
     authvals_row              => \@authvals_row,
     budget_lines              => \@budget_lines,
@@ -409,11 +453,13 @@ $template->param(
     show_active               => $show_active,
     show_actual               => $show_actual,
     show_percent              => $show_percent,
-    show_mine                  => $show_mine,
-    cur                       => $cur->{symbol},
+    show_mine                 => $show_mine,
     cur_format                => $cur_format,
     CGIextChoice              => $CGIextChoice,
     CGIsepChoice              => $CGIsepChoice,
+
+    authvals              => \@authvals_row,
+    hide_cols_loop              => \@hide_cols
 );
 
 output_html_with_http_headers $input, $cookie, $template->output;
@@ -421,6 +467,8 @@ output_html_with_http_headers $input, $cookie, $template->output;
 sub _print_to_csv {
     my ( $header, $results ) = @_;
 
+    binmode STDOUT, ':encoding(UTF-8)';
+
     my $csv = Text::CSV_XS->new(
         {   sep_char     => $del,
             always_quote => 'TRUE',
@@ -443,7 +491,8 @@ sub _print_to_csv {
     print "$str\n";
 
     foreach my $row (@$results) {
-        my @col = ( $row->{'budget_name'}, $row->{'budget_amount'} );
+        $row->{'budget_name_indent'} =~ s/&nbsp;/ /g;
+        my @col = ( $row->{'budget_name_indent'}, $row->{'budget_amount'} );
         my $l = $row->{'lines'};
         foreach my $line (@$l) {
             push @col, $line->{'estimated_amount'};
@@ -454,4 +503,3 @@ sub _print_to_csv {
         print "$str\n";
     }
 }
-