| Lines 186-241
          elsif ( $op eq 'duplicate_form'){
      
      
        Link Here | 
        
          | 186 | elsif ( $op eq 'duplicate_budget' ){ | 186 | elsif ( $op eq 'duplicate_budget' ){ | 
        
          | 187 |     die "please specify a budget period id\n" if( !defined $budget_period_id || $budget_period_id eq '' ); | 187 |     die "please specify a budget period id\n" if( !defined $budget_period_id || $budget_period_id eq '' ); | 
        
          | 188 |  | 188 |  | 
          
            
              | 189 |     my $data = GetBudgetPeriod( $budget_period_id); | 189 |     my $budget_period_startdate = dt_from_string $input->param('budget_period_startdate'); | 
            
              | 190 |     $data->{'budget_period_startdate'} = $budget_period_hashref->{budget_period_startdate}; | 190 |     my $budget_period_enddate   = dt_from_string $input->param('budget_period_enddate'); | 
            
              | 191 |     $data->{'budget_period_enddate'} = $budget_period_hashref->{budget_period_enddate}; | 191 |  | 
            
              | 192 |     delete $data->{'budget_period_id'}; | 192 |     my $new_budget_period_id = C4::Budgets::CloneBudgetPeriod( | 
            
              | 193 |     my $new_budget_period_id = AddBudgetPeriod($data); | 193 |         { | 
            
              | 194 |  | 194 |             budget_period_id        => $budget_period_id, | 
            
              | 195 |     my $tree = GetBudgetHierarchy( $budget_period_id ); | 195 |             budget_period_startdate => $budget_period_startdate, | 
            
              | 196 |  | 196 |             budget_period_enddate   => $budget_period_enddate, | 
            
              | 197 |     # hash mapping old ids to new |  |  | 
            
              | 198 |     my %old_new; | 
            
              | 199 |     # hash mapping old parent ids to list of new children ids | 
            
              | 200 |     # only store a child here if the parents old id isnt in the old_new map | 
            
              | 201 |     # when the parent is found, this map will be used, and then the entry removed and their id placed in old_new | 
            
              | 202 |     my %parent_children; | 
            
              | 203 |  | 
            
              | 204 |     for my $entry( @$tree ){ | 
            
              | 205 |         die "serious errors, parent period $budget_period_id doesnt match child ", $entry->{'budget_period_id'}, "\n" if( $entry->{'budget_period_id'} != $budget_period_id ); | 
            
              | 206 |         my $orphan = 0; # set to 1 if we need to make an entry in parent_children | 
            
              | 207 |         my $old_id = delete $entry->{'budget_id'}; | 
            
              | 208 |         my $parent_id = delete $entry->{'budget_parent_id'}; | 
            
              | 209 |         $entry->{'budget_period_id'} = $new_budget_period_id; | 
            
              | 210 |  | 
            
              | 211 |         if( !defined $parent_id ){ | 
            
              | 212 |         } elsif( defined $parent_id && $parent_id eq '' ){ | 
            
              | 213 |         } elsif( defined $old_new{$parent_id} ){ | 
            
              | 214 |             # set parent id now | 
            
              | 215 |             $entry->{'budget_parent_id'} = $old_new{$parent_id}; | 
            
              | 216 |         } else { | 
            
              | 217 |             # make an entry in parent_children | 
            
              | 218 |             $parent_children{$parent_id} = [] unless defined $parent_children{$parent_id}; | 
            
              | 219 |             $orphan = 1; | 
            
              | 220 |         } | 
            
              | 221 |  | 
            
              | 222 |         # get only the columns of aqbudgets | 
            
              | 223 |         my @columns = Koha::Database->new()->schema->source('Aqbudget')->columns; | 
            
              | 224 |         my $new_entry = { map { join(' ',@columns) =~ /$_/ ? ( $_ => $entry->{$_} )  : () } keys(%$entry) }; | 
            
              | 225 |         # write it to db | 
            
              | 226 |         my $new_id = AddBudget($new_entry); | 
            
              | 227 |         $old_new{$old_id} = $new_id; | 
            
              | 228 |         push @{$parent_children{$parent_id}}, $new_id if $orphan; | 
            
              | 229 |  | 
            
              | 230 |         # deal with any children | 
            
              | 231 |         if( defined $parent_children{$old_id} ){ | 
            
              | 232 |             # tell my children my new id | 
            
              | 233 |             for my $child ( @{$parent_children{$old_id}} ){ | 
            
              | 234 |                 ModBudget( { 'budget_id' => $child, 'budget_parent_id' => $new_id } ); | 
            
              | 235 |             } | 
            
              | 236 |             delete $parent_children{$old_id}; | 
        
          | 237 |         } | 197 |         } | 
          
            
              | 238 |     } | 198 |     ); | 
        
          | 239 |  | 199 |  | 
        
          | 240 |     # display the list of budgets | 200 |     # display the list of budgets | 
        
          | 241 |     $op = 'else'; | 201 |     $op = 'else'; |