X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=admin%2Faqbudgetperiods.pl;h=d5657460e1bcec1cff81b726e67282eb37e6f44b;hb=5d6c092921919526ade501facb1220f8a108a08f;hp=e74935e7f389e583a659df2058d6c8f2f836f13f;hpb=a7a4d39697bf52ad805fadd53d388b4530568774;p=koha_fer diff --git a/admin/aqbudgetperiods.pl b/admin/aqbudgetperiods.pl index e74935e7f3..d5657460e1 100755 --- a/admin/aqbudgetperiods.pl +++ b/admin/aqbudgetperiods.pl @@ -37,16 +37,20 @@ script to administer the budget periods table - 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 -## modules -use strict; -#use warnings; FIXME - Bug 2505 +use Modern::Perl; + use Number::Format qw(format_price); use CGI; use List::Util qw/min/; -use C4::Dates qw/format_date format_date_in_iso/; +use Koha::DateUtils; +use Koha::Database; use C4::Koha; use C4::Context; use C4::Auth; @@ -59,15 +63,20 @@ my $dbh = C4::Context->dbh; my $input = new CGI; -my $searchfield = $input->param('searchfield'); +my $searchfield = $input->param('searchfield') // ''; my $budget_period_id = $input->param('budget_period_id'); my $op = $input->param('op')||"else"; - -my $budget_period_hashref= $input->Vars; #my $sort1_authcat = $input->param('sort1_authcat'); #my $sort2_authcat = $input->param('sort2_authcat'); -my $pagesize = 20; +# 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( @@ -111,11 +120,9 @@ 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 - FormatData($budgetperiod_hash); my $editnum = new Number::Format( 'int_curr_symbol' => '', @@ -129,13 +136,12 @@ if ( $op eq 'add_form' ) { %$budgetperiod_hash ); } # IF-MOD - $template->param( DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),); } elsif ( $op eq 'add_validate' ) { ## add or modify a budget period (confirmation) - ## update budget period data + ## 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); @@ -154,7 +160,6 @@ elsif ( $op eq 'delete_confirm' ) { my $total = 0; my $data = GetBudgetPeriod( $budget_period_id); - FormatData($data); $$data{'budget_period_total'}=$num->format_price( $data->{'budget_period_total'}); $template->param( %$data @@ -169,30 +174,125 @@ elsif ( $op eq 'delete_confirmed' ) { $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 $data = GetBudgetPeriod( $budget_period_id); + $data->{'budget_period_startdate'} = $budget_period_hashref->{budget_period_startdate}; + $data->{'budget_period_enddate'} = $budget_period_hashref->{budget_period_enddate}; + delete $data->{'budget_period_id'}; + my $new_budget_period_id = AddBudgetPeriod($data); + + my $tree = GetBudgetHierarchy( $budget_period_id ); + + # hash mapping old ids to new + my %old_new; + # hash mapping old parent ids to list of new children ids + # only store a child here if the parents old id isnt in the old_new map + # when the parent is found, this map will be used, and then the entry removed and their id placed in old_new + my %parent_children; + + for my $entry( @$tree ){ + die "serious errors, parent period $budget_period_id doesnt match child ", $entry->{'budget_period_id'}, "\n" if( $entry->{'budget_period_id'} != $budget_period_id ); + my $orphan = 0; # set to 1 if we need to make an entry in parent_children + my $old_id = delete $entry->{'budget_id'}; + my $parent_id = delete $entry->{'budget_parent_id'}; + $entry->{'budget_period_id'} = $new_budget_period_id; + + if( !defined $parent_id ){ + } elsif( defined $parent_id && $parent_id eq '' ){ + } elsif( defined $old_new{$parent_id} ){ + # set parent id now + $entry->{'budget_parent_id'} = $old_new{$parent_id}; + } else { + # make an entry in parent_children + $parent_children{$parent_id} = [] unless defined $parent_children{$parent_id}; + $orphan = 1; + } + + # get only the columns of aqbudgets + my @columns = Koha::Database->new()->schema->source('Aqbudget')->columns; + my $new_entry = { map { join(' ',@columns) =~ /$_/ ? ( $_ => $entry->{$_} ) : () } keys(%$entry) }; + # write it to db + my $new_id = AddBudget($new_entry); + $old_new{$old_id} = $new_id; + push @{$parent_children{$parent_id}}, $new_id if $orphan; + + # deal with any children + if( defined $parent_children{$old_id} ){ + # tell my children my new id + for my $child ( @{$parent_children{$old_id}} ){ + ModBudget( { 'budget_id' => $child, 'budget_parent_id' => $new_id } ); + } + delete $parent_children{$old_id}; + } + } + + # display the list of budgets + $op = 'else'; +} + # DEFAULT - DISPLAY AQPERIODS TABLE # ------------------------------------------------------------------- # display the list of budget periods - my $results = GetBudgetPeriods(); - $template->param( period_button_only => 1 ) unless (@$results) ; - my $page = $input->param('page') || 1; - my $first = ( $page - 1 ) * $pagesize; - # if we are on the last page, the number of the last word to display - # must not exceed the length of the results array - my $last = min( $first + $pagesize - 1, scalar @{$results} - 1, ); - my $toggle = 0; - my @period_loop; - foreach my $result ( @{$results}[ $first .. $last ] ) { - my $budgetperiod = $result; - FormatData($budgetperiod); - $budgetperiod->{'budget_period_total'} = $num->format_price( $budgetperiod->{'budget_period_total'} ); - $budgetperiod->{budget_active} = 1; - push( @period_loop, $budgetperiod ); - } - $template->param( - period_loop => \@period_loop, - pagination_bar => pagination_bar("aqbudgetperiods.pl",getnbpages(scalar(@$results),$pagesize),$page), - ); +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;