Bug 17672: Add damaged_on to items and deleteditems tables
[koha.git] / admin / aqplan.pl
index cb17f0e..fd3f2e6 100755 (executable)
@@ -3,30 +3,28 @@
 # Copyright 2008-2009 BibLibre SARL
 # 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., 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, see <http://www.gnu.org/licenses>.
 
 #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 CGI;
+use Modern::Perl;
+use CGI qw ( -utf8 );
 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 Text::CSV_XS;
 
 use C4::Acquisition;
@@ -35,21 +33,21 @@ use C4::Context;
 use C4::Output;
 use C4::Koha;
 use C4::Auth;
-use C4::Input;
 use C4::Debug;
+use Koha::Acquisition::Currencies;
 
-my $input = new CGI;
+our $input = new CGI;
 ####  $input
 
 my $dbh = C4::Context->dbh;
 
 my ( $template, $borrowernumber, $cookie, $staff_flags ) = get_template_and_user(
-    {   template_name   => "admin/aqplan.tmpl",
+    {   template_name   => "admin/aqplan.tt",
         query           => $input,
         type            => "intranet",
-        authnotrequired => 1,
+        authnotrequired => 0,
         flagsrequired   => { acquisition => 'planning_manage' },
-        debug           => 1,
+        debug           => 0,
     }
 );
 
@@ -58,77 +56,48 @@ my $budget_period_id = $input->param('budget_period_id');
 # IF PERIOD_ID IS DEFINED,  GET THE PERIOD - ELSE GET THE ACTIVE PERIOD BY DEFAULT
 my $period = GetBudgetPeriod($budget_period_id);
 my $count  = GetPeriodsCount();
-my $cur    = GetCurrency;
-
+my $active_currency = Koha::Acquisition::Currencies->get_active;
+if ( $active_currency ) {
+    $template->param( symbol => $active_currency->symbol,
+                      currency => $active_currency->currency,
+                   );
+}
 $template->param( period_button_only => 1 ) if $count == 0;
 
 
 
 # authcats_loop populates the YUI planning button
 my $auth_cats_loop            = GetBudgetAuthCats($budget_period_id);
-my $budget_period_id          = $period->{'budget_period_id'};
+$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,
 );
 
 # ------- 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'}->{'borrowernumber'};
+my $borrower_branchcode = $template->{VARS}->{'USER_INFO'}->{'branchcode'};
 
-my $periods;
 my $authcat      = $input->param('authcat');
 my $show_active  = $input->param('show_active');
 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");
+our $basename     = $input->param("basename");
+our $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;
-
-if ( $cur_format eq 'FR' ) {
-    $num = new Number::Format(
-        'decimal_fill'      => '2',
-        'decimal_point'     => ',',
-        'int_curr_symbol'   => '',
-        'mon_thousands_sep' => ' ',
-        'thousands_sep'     => ' ',
-        'mon_decimal_point' => ','
-    );
-} else {  # US by default..
-    $num = new Number::Format(
-        'int_curr_symbol'   => '',
-        'mon_thousands_sep' => ',',
-        'mon_decimal_point' => '.'
-    );
-}
+my @hide_cols      = $input->multi_param('hide_cols');
 
 if ( $budget_period_locked == 1  && not defined  $show_actual ) {
      $show_actual  = 1;
@@ -139,13 +108,7 @@ $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 $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'}->{'branchcode'}:'', $show_mine?$template->{VARS}->{'USER_INFO'}->{'borrowernumber'}:'' );
 
 # build categories list
 my $sth = $dbh->prepare("select distinct category from authorised_values where category like 'A%' ");
@@ -161,43 +124,27 @@ 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, $_ ) foreach @$auth_cats_loop;
 
 #reorder the list
 @category_list = sort { $a cmp $b } @category_list;
-my $tab_list = CGI::scrolling_list(
-    -name     => 'authcat',
-    -id       => 'authcat',
-    -values   => \@category_list,
-    -default  => $authcat,
-    -size     => 1,
-    -tabindex => '',
-    -multiple => 0,
-);
 
-$template->param( authcat_dropbox => $tab_list );
+$template->param( authcat_dropbox => {
+        values => \@category_list,
+        default => $authcat,
+    });
 
 my @budgets = @$budgets_ref;
-my $CGISort;
 my @authvals;
 my %labels;
 
-
-    my @names = $input->param();
-
+my @names = $input->multi_param();
 # ------------------------------------------------------------
 if ( $op eq 'save' ) {
-
-
-    ### ---------------------  save
-
-    ####  @names
-
-
     #get budgets
     my ( @buds, @auth_values );
     foreach my $n (@names) {
@@ -211,7 +158,6 @@ if ( $op eq 'save' ) {
     my %seen;
     @buds        = grep { !$seen{$_}++ } @buds;
     @auth_values = grep { !$seen{$_}++ } @auth_values;
-    my @budget_ids;
     my @budget_lines;
 
     foreach my $budget (@buds) {
@@ -241,69 +187,26 @@ 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' ) {
+elsif ( $authcat eq 'MONTHS' && $budget_period_startdate && $budget_period_enddate ) {
 
     # build months
     my @start_date = UnixDate( $budget_period_startdate, ( '%Y', '%m', '%d' ) );
@@ -313,7 +216,7 @@ elsif ( $authcat eq 'MONTHS' ) {
 
     #calc number of months between
     my $months      = ( $Dy * 12 ) + $Dm;
-    my $start_month = @start_date[1];
+    my $start_month = $start_date[1];
     my $end_month   = ( $Dy * 12 ) + $Dm;
 
     for my $mth ( 0 ... $months ) {
@@ -327,22 +230,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 +243,6 @@ elsif ( $authcat eq 'ITEMTYPES' ) {
     }
     $sth->finish;
 
-
-            ####  @authvals
-
-
-
-
-
-
 } elsif ( $authcat eq 'BRANCHES' ) {
 
     my $query = qq| SELECT branchcode, branchname FROM branches |;
@@ -376,13 +257,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,62 +284,33 @@ 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,
-    -size     => 1,
-    -multiple => 0
-);
-
-my @dels         = ( C4::Context->preference("delimiter") );
-my $CGIsepChoice = CGI::scrolling_list(
-    -name     => 'sep',
-    -id       => 'sep',
-    -values   => \@dels,
-    -size     => 1,
-    -multiple => 0
-);
+my $CGIextChoice = ( 'CSV' ); # FIXME translation
+my $CGIsepChoice = ( C4::Context->preference("delimiter") );
 
 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;
@@ -466,7 +325,7 @@ foreach my $budget (@budgets) {
         or  $authcat eq 'BRANCHES'
         or  $authcat eq 'MONTHS' ) {
 
-        # but skip budgets that dont match the current auth-category
+        # but skip budgets that don't match the current auth-category
         next if ( $budget->{'sort1_authcat'} ne $authcat
             && $budget->{'sort2_authcat'} ne $authcat );
     }
@@ -504,16 +363,12 @@ 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;
 
     %budget_line = (
         lines                   => \@cells_line,
-        budget_name_indent      => $budget->{budget_name_indent},
-        budget_amount_formatted => $num->format_price( $budget->{budget_amount} ),
+        budget_name             => $budget->{budget_name},
         budget_amount           => $budget->{budget_amount},
         budget_alloc            => $budget->{budget_alloc},
         budget_act_remain       => sprintf( "%.2f", $budget_act_remain ),
@@ -532,9 +387,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 +395,6 @@ if ( $output eq "file" ) {
     exit(1);
 }
 
-    ## ## @budget_lines
-
 $template->param(
     authvals_row              => \@authvals_row,
     budget_lines              => \@budget_lines,
@@ -556,38 +406,20 @@ $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,
 
     authvals              => \@authvals_row,
-    hide_cols_loop              => \@hide_cols
+    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',
@@ -621,4 +453,3 @@ sub _print_to_csv {
         print "$str\n";
     }
 }
-