|
Lines 182-238
elsif ( $op eq 'duplicate_form'){
Link Here
|
| 182 |
# handle the actual duplication |
182 |
# handle the actual duplication |
| 183 |
elsif ( $op eq 'duplicate_budget' ){ |
183 |
elsif ( $op eq 'duplicate_budget' ){ |
| 184 |
die "please specify a budget period id\n" if( !defined $budget_period_id || $budget_period_id eq '' ); |
184 |
die "please specify a budget period id\n" if( !defined $budget_period_id || $budget_period_id eq '' ); |
| 185 |
my $startdate = $input->param('budget_period_startdate'); |
|
|
| 186 |
my $enddate = $input->param('budget_period_enddate'); |
| 187 |
|
185 |
|
| 188 |
my $data = GetBudgetPeriod( $budget_period_id); |
186 |
my $budget_period_startdate = $input->param('budget_period_startdate'); |
| 189 |
|
187 |
my $budget_period_enddate = $input->param('budget_period_enddate'); |
| 190 |
$data->{'budget_period_startdate'} = $startdate; |
|
|
| 191 |
$data->{'budget_period_enddate'} = $enddate; |
| 192 |
delete $data->{'budget_period_id'}; |
| 193 |
my $new_budget_period_id = C4::SQLHelper::InsertInTable('aqbudgetperiods', $data); |
| 194 |
|
| 195 |
my $tree = GetBudgetHierarchy( $budget_period_id ); |
| 196 |
|
| 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 |
|
188 |
|
| 222 |
# write it to db |
189 |
my $new_budget_period_id = C4::Budgets::CloneBudgetPeriod( |
| 223 |
my $new_id = C4::SQLHelper::InsertInTable('aqbudgets', $entry); |
190 |
{ |
| 224 |
$old_new{$old_id} = $new_id; |
191 |
budget_period_id => $budget_period_id, |
| 225 |
push @{$parent_children{$parent_id}}, $new_id if $orphan; |
192 |
budget_period_startdate => $budget_period_startdate, |
| 226 |
|
193 |
budget_period_enddate => $budget_period_enddate, |
| 227 |
# deal with any children |
|
|
| 228 |
if( defined $parent_children{$old_id} ){ |
| 229 |
# tell my children my new id |
| 230 |
for my $child ( @{$parent_children{$old_id}} ){ |
| 231 |
C4::SQLHelper::UpdateInTable('aqcudgets', [ 'budget_id' => $child, 'budget_parent_id' => $new_id ]); |
| 232 |
} |
| 233 |
delete $parent_children{$old_id}; |
| 234 |
} |
194 |
} |
| 235 |
} |
195 |
); |
| 236 |
|
196 |
|
| 237 |
# display the list of budgets |
197 |
# display the list of budgets |
| 238 |
$op = 'else'; |
198 |
$op = 'else'; |