Koha/admin/aqbudgetperiods.pl
Jonathan Druart 110c665a4b Bug 12164: Close a budget period (budget)
This is the main patch.

On closing a budget period, all unreceived orders are moved from the
old/previous fiscal year into the new fiscal year.

You can rollover funds unused in the previous fiscal year to the new
fiscal year.

This patch set is based on bug 12168 (bugfix) and can be tested on top
of bug 11578 (easier to see the fund structure).

The patch set is cut in 6 main patches:

- Move the budget period clone logic into C4::Budgets
  The code is moved from the pl to Budgets.pm and unit tests are provided.
  The original code should certainly be buggy since a typo existed.
- On cloning budget period, mark original budget as inactive
  Cloning a budget period is already possible in Koha, this patch adds a
  checkbox to mark as inactive the original budget. That avoids to edit
  the budget and click the "inactive" checkbox. Both do the same action.
- On cloning budget periods, add a "reset all funds" option
  Same as before, a new checkbox is added on cloning a budget period. If
  you check it, all fund amounts will be set to 0. Otherwise, no change
  compared to the existing behavior.
- Close a budget period (budget)
  The goal of this patch set is to move unreceived orders from a budget to
  another. This patch adds a C4::Budgets::MoveOrders routine which does
  this job.
  This action is only possible if the fund structure is the same for both
  budgets, the budget_code field should be the same.
- On closing budget period, move unspent amount
  Unspent amount will be move from the previous budget structure to the
  new one.
- Add UI report
  This patch only adds a report when closing a budget is done.

Test plan:
Wording: below, budget is a "budget period" and fund is a "budget".
Prerequisite: Having 1 active budget with some funds (with different
levels and different amounts). Order and receive some orders (not all).
1/ Go on the budgets administration page (admin/aqbudgetperiods.pl) and
duplicate the structure of this budget ("Duplicate" link in the
"Actions" column).
2/ Enter start and end date for this budget and mark the original budget
as inactive.
3/ Note that a new budget is created, with the same fund structures (and
same value) and that the old one is marked as inactive (see
admin/aqbudgets.pl page with patches from bug 11578).
4/ Try to close the new budget: it is not possible, there is no
unreceived orders for this budget.
5/ You can close the inactive budget ("Close" link in the "Actions"
column).
6/ Verify the number of "Unreceived orders" is correct and select the
new budget in the budget list. Click on the "Move remaining unspent
funds" if you want to move unspent amounts.
7/ A report view is displayed and show you the ordernumber which have
been impacted (grouped by fund).
8/ Try different configuration, depending on the selected checkboxes.

Signed-off-by: Paola Rossi <paola.rossi@cineca.it>
Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
2014-07-24 14:17:15 -03:00

310 lines
10 KiB
Perl
Executable file

#!/usr/bin/perl
# Copyright 2008 BibLibre, BibLibre, Paul POULAIN
# SAN Ouest Provence
#
# 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 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.
=head1 admin/aqbudgetperiods.pl
script to administer the budget periods table
This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
ALGO :
this script use an $op to know what to do.
if $op is empty or none of the above values,
- the default screen is build (with all records, or filtered datas).
- the user can clic on add, modify or delete record.
if $op=add_form
- if primkey exists, this is a modification,so we read the $primkey record
- builds the add/modify form
if $op=add_validate
- the user has just send datas, so we create/modify the record
if $op=delete_confirm
- we show the record having primkey=$primkey and ask for deletion validation form
if $op=delete_confirmed
- we delete the record having primkey=$primkey
if $op=duplicate_form
- displays the duplication of budget period form (allowing specification of dates)
if $op=duplicate_budget
- we perform the duplication of the budget period specified as budget_period_id
=cut
use Modern::Perl;
use Number::Format qw(format_price);
use CGI;
use List::Util qw/min/;
use Koha::DateUtils;
use Koha::Database;
use C4::Koha;
use C4::Context;
use C4::Auth;
use C4::Output;
use C4::Acquisition;
use C4::Budgets;
use C4::Debug;
my $dbh = C4::Context->dbh;
my $input = new CGI;
my $searchfield = $input->param('searchfield') // '';
my $budget_period_id = $input->param('budget_period_id');
my $op = $input->param('op')||"else";
#my $sort1_authcat = $input->param('sort1_authcat');
#my $sort2_authcat = $input->param('sort2_authcat');
# get only the columns of aqbudgetperiods in budget_period_hashref
my @columns = Koha::Database->new()->schema->source('Aqbudgetperiod')->columns;
my $budget_period_hashref = { map { join(' ',@columns) =~ /$_/ ? ( $_ => $input->param($_) ) : () } keys( %{$input->Vars()} ) } ;
$budget_period_hashref->{budget_period_startdate} = dt_from_string( $input->param('budget_period_startdate') );
$budget_period_hashref->{budget_period_enddate} = dt_from_string( $input->param('budget_period_enddate') );
my $activepagesize = 20;
my $inactivepagesize = 20;
$searchfield =~ s/\,//g;
my ($template, $borrowernumber, $cookie, $staff_flags ) = get_template_and_user(
{
template_name => "admin/aqbudgetperiods.tt",
query => $input,
type => "intranet",
authnotrequired => 0,
flagsrequired => { acquisition => 'period_manage' },
debug => 1,
}
);
my $cur = GetCurrency();
$template->param( symbol => $cur->{symbol},
currency => $cur->{currency}
);
my $cur_format = C4::Context->preference("CurrencyFormat");
my $num;
if ( $cur_format eq 'US' ) {
$num = new Number::Format(
'int_curr_symbol' => '',
'mon_thousands_sep' => ',',
'mon_decimal_point' => '.'
);
} elsif ( $cur_format eq 'FR' ) {
$num = new Number::Format(
'decimal_fill' => '2',
'decimal_point' => ',',
'int_curr_symbol' => '',
'mon_thousands_sep' => ' ',
'thousands_sep' => ' ',
'mon_decimal_point' => ','
);
}
# ADD OR MODIFY A BUDGET PERIOD - BUILD SCREEN
if ( $op eq 'add_form' ) {
## add or modify a budget period (preparation)
## get information about the budget period that must be modified
if ($budget_period_id) { # MOD
my $budgetperiod_hash=GetBudgetPeriod($budget_period_id);
# get dropboxes
my $editnum = new Number::Format(
'int_curr_symbol' => '',
'thousands_sep' => '',
'mon_thousands_sep' => '',
'mon_decimal_point' => '.'
);
$$budgetperiod_hash{budget_period_total}= $editnum->format_price($$budgetperiod_hash{'budget_period_total'});
$template->param(
%$budgetperiod_hash
);
} # IF-MOD
}
elsif ( $op eq 'add_validate' ) {
## add or modify a budget period (confirmation)
## update budget period data
if ( $budget_period_id ne '' ) {
$$budget_period_hashref{$_}||=0 for qw(budget_period_active budget_period_locked);
my $status=ModBudgetPeriod($budget_period_hashref);
}
else { # ELSE ITS AN ADD
my $budget_period_id=AddBudgetPeriod($budget_period_hashref);
}
$op='else';
}
#--------------------------------------------------
elsif ( $op eq 'delete_confirm' ) {
## delete a budget period (preparation)
my $dbh = C4::Context->dbh;
## $total = number of records linked to the record that must be deleted
my $total = 0;
my $data = GetBudgetPeriod( $budget_period_id);
$$data{'budget_period_total'}=$num->format_price( $data->{'budget_period_total'});
$template->param(
%$data
);
}
elsif ( $op eq 'delete_confirmed' ) {
## delete the budget period record
my $data = GetBudgetPeriod( $budget_period_id);
DelBudgetPeriod($budget_period_id);
$op='else';
}
# display the form for duplicating
elsif ( $op eq 'duplicate_form'){
$template->param(
'duplicate_form' => '1',
'budget_period_id' => $budget_period_id,
);
}
# handle the actual duplication
elsif ( $op eq 'duplicate_budget' ){
die "please specify a budget period id\n" if( !defined $budget_period_id || $budget_period_id eq '' );
my $budget_period_startdate = dt_from_string $input->param('budget_period_startdate');
my $budget_period_enddate = dt_from_string $input->param('budget_period_enddate');
my $mark_original_budget_as_inactive = $input->param('mark_original_budget_as_inactive');
my $reset_all_budgets = $input->param('reset_all_budgets');
my $new_budget_period_id = C4::Budgets::CloneBudgetPeriod(
{
budget_period_id => $budget_period_id,
budget_period_startdate => $budget_period_startdate,
budget_period_enddate => $budget_period_enddate,
mark_original_budget_as_inactive => $mark_original_budget_as_inactive,
reset_all_budgets => $reset_all_budgets,
}
);
# display the list of budgets
$op = 'else';
}
elsif ( $op eq 'close_form' ) {
my $budget_period = GetBudgetPeriod($budget_period_id);
my $active_budget_periods =
C4::Budgets::GetBudgetPeriods( { budget_period_active => 1 } );
# Remove the budget period from the list
$active_budget_periods =
[ map { ( $_->{budget_period_id} == $budget_period_id ) ? () : $_ }
@$active_budget_periods ];
my $budgets_to_move = GetBudgetHierarchy($budget_period_id);
# C4::Context->userenv->{branchcode}, $show_mine ? $borrower_id : '')
my $number_of_unreceived_orders = 0;
for my $budget (@$budgets_to_move) {
# We want to move funds from this budget
my $unreceived_orders = C4::Acquisition::SearchOrders(
{ budget_id => $budget->{budget_id}, } );
$budget->{unreceived_orders} = $unreceived_orders;
$number_of_unreceived_orders += scalar(@$unreceived_orders);
}
$template->param(
close_form => 1,
budget_period_id => $budget_period_id,
budget_period_description =>
$budget_period->{budget_period_description},
budget_periods => $active_budget_periods,
budgets_to_move => $budgets_to_move,
number_of_unreceived_orders => $number_of_unreceived_orders,
);
}
elsif ( $op eq 'close_confirmed' ) {
my $to_budget_period_id = $input->param('to_budget_period_id');
my $report = C4::Budgets::MoveOrders(
{
from_budget_period_id => $budget_period_id,
to_budget_period_id => $to_budget_period_id,
}
);
}
# DEFAULT - DISPLAY AQPERIODS TABLE
# -------------------------------------------------------------------
# display the list of budget periods
my $activepage = $input->param('apage') || 1;
my $inactivepage = $input->param('ipage') || 1;
# Get active budget periods
my $results = GetBudgetPeriods(
{ budget_period_active => 1 },
{ -asc => 'budget_period_description' },
);
my $first = ( $activepage - 1 ) * $activepagesize;
my $last = min( $first + $activepagesize - 1, scalar @{$results} - 1, );
my @period_active_loop;
foreach my $result ( @{$results}[ $first .. $last ] ) {
my $budgetperiod = $result;
$budgetperiod->{'budget_period_total'} = $num->format_price( $budgetperiod->{'budget_period_total'} );
$budgetperiod->{budget_active} = 1;
push( @period_active_loop, $budgetperiod );
}
my $url = "aqbudgetperiods.pl";
$url .= "?ipage=$inactivepage" if($inactivepage != 1);
my $active_pagination_bar = pagination_bar ($url, getnbpages( scalar(@$results), $activepagesize), $activepage, "apage");
# Get inactive budget periods
$results = GetBudgetPeriods(
{ budget_period_active => 0 },
{ -desc => 'budget_period_enddate' },
);
$first = ( $inactivepage - 1 ) * $inactivepagesize;
$last = min( $first + $inactivepagesize - 1, scalar @{$results} - 1, );
my @period_inactive_loop;
foreach my $result ( @{$results}[ $first .. $last ] ) {
my $budgetperiod = $result;
$budgetperiod->{'budget_period_total'} = $num->format_price( $budgetperiod->{'budget_period_total'} );
$budgetperiod->{budget_active} = 1;
push( @period_inactive_loop, $budgetperiod );
}
$url = "aqbudgetperiods.pl?tab=2";
$url .= "&apage=$activepage" if($activepage != 1);
my $inactive_pagination_bar = pagination_bar ($url, getnbpages( scalar(@$results), $inactivepagesize), $inactivepage, "ipage");
my $tab = $input->param('tab') ? $input->param('tab') - 1 : 0;
$template->param(
period_active_loop => \@period_active_loop,
period_inactive_loop => \@period_inactive_loop,
active_pagination_bar => $active_pagination_bar,
inactive_pagination_bar => $inactive_pagination_bar,
tab => $tab,
);
$template->param($op=>1);
output_html_with_http_headers $input, $cookie, $template->output;