Bug 9044: DBRev 3.13.00.036
[koha.git] / admin / aqplan.pl
index cb17f0e..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/;
@@ -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,7 +60,9 @@ 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;
 
 
@@ -71,21 +74,19 @@ 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,
 );
 
 # ------- 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');
@@ -94,21 +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       = $input->param('show_mine') ;
 
 my @hide_cols      = $input->param('hide_cols');
-####  @hide_cols
-
-
-
-=c
-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;
-=cut
 
 my $cur_format = C4::Context->preference("CurrencyFormat");
 my $num;
@@ -141,11 +132,7 @@ my $op        = $input->param("op");
 
 my $budget_branchcode;
 
-
-#my $budgets_ref = GetBudgetHierarchy( $budget_period_id, $budget_branchcode );
-## ##  $budgets_ref
-
-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 $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%' ");
@@ -161,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;
@@ -186,18 +173,9 @@ my $CGISort;
 my @authvals;
 my %labels;
 
-
-    my @names = $input->param();
-
+my @names = $input->param();
 # ------------------------------------------------------------
 if ( $op eq 'save' ) {
-
-
-    ### ---------------------  save
-
-    ####  @names
-
-
     #get budgets
     my ( @buds, @auth_values );
     foreach my $n (@names) {
@@ -241,67 +219,25 @@ if ( $op eq 'save' ) {
     my $plan = \@budget_lines;
     ModBudgetPlan( $plan, $budget_period_id, $authcat );
 
-#### iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
 HideCols($authcat, @hide_cols);
 
 
 }
-
-
 # ------------------------------------------------------------
-
-
 if ( $authcat =~ m/^Asort/ ) {
-
-#### eeeee
     # ----------- copied from C4::Input::buildCGIsort()
    my $query = qq{ SELECT * FROM authorised_values WHERE category=? order by lib };
     my $sth   = $dbh->prepare($query);
-#### $query
-#    $sth->{TraceLevel} = 2;
     $sth->execute($authcat  );
-
-=c
-    my $query = qq{ 
-
-SELECT * FROM authorised_values
-JOIN aqbudgets_planning  ON
-    (aqbudgets_planning.authvalue = authorised_values.authorised_value )
-WHERE (authcat = ?
-AND category = ?
-AND budget_period_id = ?
-AND display = 1 ) 
-ORDER BY lib   };
-
-
-    my $sth   = $dbh->prepare($query);
-#### $query
-    $sth->{TraceLevel} = 2;
-
-    $sth->execute($authcat, $authcat, $budget_period_id  );
-=cut
-
-
-#### qq
-
     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};
- #          $labels{ $results->{display} } = 1 ;
-
         }
     }
     $sth->finish;
     @authvals = sort { $a <=> $b } @authvals;
-
-
-####  @authvals
-
-
-
-
 }
 elsif ( $authcat eq 'MONTHS' ) {
 
@@ -327,22 +263,8 @@ elsif ( $authcat eq 'MONTHS' ) {
 }
 
 elsif ( $authcat eq 'ITEMTYPES' ) {
-#### aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
-
     my $query = qq| SELECT itemtype, description FROM itemtypes |;
-=c
-    my $query = qq| SELECT * FROM aqbudgets_planning
-                    JOIN itemtypes ON (aqbudgets_planning.authvalue = itemtypes.itemtype)
-                    WHERE (authcat = 'itemtypes'
-                            AND budget_period_id = ? )     |;
-=cut
-
-
     my $sth   = $dbh->prepare($query);
-    $sth->{TraceLevel} = 3;
-
-
-    #$sth->execute( $budget_period_id  );
     $sth->execute(  );
 
     if ( $sth->rows > 0 ) {
@@ -354,14 +276,6 @@ elsif ( $authcat eq 'ITEMTYPES' ) {
     }
     $sth->finish;
 
-
-            ####  @authvals
-
-
-
-
-
-
 } elsif ( $authcat eq 'BRANCHES' ) {
 
     my $query = qq| SELECT branchcode, branchname FROM branches |;
@@ -376,13 +290,20 @@ 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;
 }
 
-####     @authvals
-####     %labels
-
-
-
 my @authvals_row;
 my $i=1;
 foreach my $val (@authvals) {
@@ -396,40 +317,24 @@ foreach my $val (@authvals) {
 
     push( @authvals_row, \%auth_hash );
 }
-#### ddddddddddddd
-#### @authvals_row
-
-
-
-
-
-
-
 
-
-
-    #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;
+}
 
 
 # ------------------------------------------------------------
 #         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
 );
@@ -446,12 +351,13 @@ 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;
@@ -504,9 +410,6 @@ foreach my $budget (@budgets) {
         $i++;
     }
 
-
-
-    #     lines => \@cells_line,
     my $budget_act_remain = $budget->{budget_amount} - $actual_spent;
     my $budget_est_remain = $budget->{budget_amount} - $estimated_spent;
 
@@ -532,9 +435,6 @@ foreach my $budget (@budgets) {
     # skip if active set , and spent == 0
     next if ( $show_active == '1' && ( $actual_spent == 0 ) );
 
-
-    ### %budget_lin
-
     push( @budget_lines, \%budget_line );
 }
 
@@ -543,8 +443,6 @@ if ( $output eq "file" ) {
     exit(1);
 }
 
-    ## ## @budget_lines
-
 $template->param(
     authvals_row              => \@authvals_row,
     budget_lines              => \@budget_lines,
@@ -556,7 +454,6 @@ $template->param(
     show_actual               => $show_actual,
     show_percent              => $show_percent,
     show_mine                 => $show_mine,
-    cur                       => $cur->{symbol},
     cur_format                => $cur_format,
     CGIextChoice              => $CGIextChoice,
     CGIsepChoice              => $CGIsepChoice,
@@ -564,30 +461,14 @@ $template->param(
     authvals              => \@authvals_row,
     hide_cols_loop              => \@hide_cols
 );
-#### uuuuuuuuuuu
-
-
 
 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',
@@ -610,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'};
@@ -621,4 +503,3 @@ sub _print_to_csv {
         print "$str\n";
     }
 }
-