View | Details | Raw Unified | Return to bug 12164
Collapse All | Expand All

(-)a/C4/Budgets.pm (+82 lines)
Lines 980-985 sub ConvertCurrency { Link Here
980
    return ( $price / $cur );
980
    return ( $price / $cur );
981
}
981
}
982
982
983
984
=head2 CloneBudgetPeriod
985
986
  my $new_budget_period_id = CloneBudgetPeriod({
987
    budget_period_id => $budget_period_id,
988
    budget_period_startdate => $budget_period_startdate;
989
    $budget_period_enddate   => $budget_period_enddate;
990
  });
991
992
Clone a budget period with all budgets.
993
994
=cut
995
996
sub CloneBudgetPeriod {
997
    my ($params)                = @_;
998
    my $budget_period_id        = $params->{budget_period_id};
999
    my $budget_period_startdate = $params->{budget_period_startdate};
1000
    my $budget_period_enddate   = $params->{budget_period_enddate};
1001
1002
    my $budget_period = GetBudgetPeriod($budget_period_id);
1003
1004
    $budget_period->{budget_period_startdate} = $budget_period_startdate;
1005
    $budget_period->{budget_period_enddate}   = $budget_period_enddate;
1006
    my $original_budget_period_id = $budget_period->{budget_period_id};
1007
    delete $budget_period->{budget_period_id};
1008
    my $new_budget_period_id = AddBudgetPeriod( $budget_period );
1009
1010
    my $budgets = GetBudgetHierarchy($budget_period_id);
1011
    CloneBudgetHierarchy(
1012
        { budgets => $budgets, new_budget_period_id => $new_budget_period_id }
1013
    );
1014
    return $new_budget_period_id;
1015
}
1016
1017
=head2 CloneBudgetHierarchy
1018
1019
  CloneBudgetHierarchy({
1020
    budgets => $budgets,
1021
    new_budget_period_id => $new_budget_period_id;
1022
  });
1023
1024
Clone a budget hierarchy.
1025
1026
=cut
1027
1028
sub CloneBudgetHierarchy {
1029
    my ($params)             = @_;
1030
    my $budgets              = $params->{budgets};
1031
    my $new_budget_period_id = $params->{new_budget_period_id};
1032
    next unless @$budgets or $new_budget_period_id;
1033
1034
    my $children_of   = $params->{children_of};
1035
    my $new_parent_id = $params->{new_parent_id};
1036
1037
    my @first_level_budgets =
1038
      ( not defined $children_of )
1039
      ? map { ( not $_->{budget_parent_id} )             ? $_ : () } @$budgets
1040
      : map { ( $_->{budget_parent_id} == $children_of ) ? $_ : () } @$budgets;
1041
1042
    for my $budget ( sort { $a->{budget_id} <=> $b->{budget_id} }
1043
        @first_level_budgets )
1044
    {
1045
        my $new_budget_id = AddBudget(
1046
            {
1047
                %$budget,
1048
                budget_id        => undef,
1049
                budget_parent_id => $new_parent_id,
1050
                budget_period_id => $new_budget_period_id
1051
            }
1052
        );
1053
        CloneBudgetHierarchy(
1054
            {
1055
                budgets              => $budgets,
1056
                new_budget_period_id => $new_budget_period_id,
1057
                children_of          => $budget->{budget_id},
1058
                new_parent_id        => $new_budget_id
1059
            }
1060
        );
1061
    }
1062
}
1063
1064
983
END { }    # module clean-up code here (global destructor)
1065
END { }    # module clean-up code here (global destructor)
984
1066
985
1;
1067
1;
(-)a/admin/aqbudgetperiods.pl (-48 / +8 lines)
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';
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgetperiods.tt (-2 / +4 lines)
Lines 339-344 Link Here
339
                <td>
339
                <td>
340
                  <a href="[% script_name %]?op=add_form&amp;budget_period_id=[% period_active.budget_period_id |html %]">Edit</a>
340
                  <a href="[% script_name %]?op=add_form&amp;budget_period_id=[% period_active.budget_period_id |html %]">Edit</a>
341
                  <a href="[% script_name %]?op=delete_confirm&amp;budget_period_id=[% period_active.budget_period_id %]">Delete</a>
341
                  <a href="[% script_name %]?op=delete_confirm&amp;budget_period_id=[% period_active.budget_period_id %]">Delete</a>
342
                  <a href="[% script_name %]?op=duplicate_form&amp;budget_period_id=[% period_active.budget_period_id %]">Duplicate</a>
342
                  <a href="/cgi-bin/koha/admin/aqbudgets.pl?op=add_form&amp;budget_period_id=[% period_active.budget_period_id %]">Add fund</a>
343
                  <a href="/cgi-bin/koha/admin/aqbudgets.pl?op=add_form&amp;budget_period_id=[% period_active.budget_period_id %]">Add fund</a>
343
                </td>
344
                </td>
344
                </tr>
345
                </tr>
Lines 376-383 Link Here
376
                  <td> [% IF ( period_loo.budget_period_locked ) %]<span style="color:green;">Locked</span>&nbsp;[% ELSE %][% END %] </td>
377
                  <td> [% IF ( period_loo.budget_period_locked ) %]<span style="color:green;">Locked</span>&nbsp;[% ELSE %][% END %] </td>
377
                  <td class="data">[% period_loo.budget_period_total %]</td>
378
                  <td class="data">[% period_loo.budget_period_total %]</td>
378
                  <td>
379
                  <td>
379
                      <a href="[% period_loo.script_name %]?op=add_form&amp;budget_period_id=[% period_loo.budget_period_id |html %]">Edit</a>
380
                      <a href="[% script_name %]?op=add_form&amp;budget_period_id=[% period_loo.budget_period_id |html %]">Edit</a>
380
                      <a href="[% period_loo.script_name %]?op=delete_confirm&amp;budget_period_id=[% period_loo.budget_period_id %]">Delete</a>
381
                      <a href="[% script_name %]?op=delete_confirm&amp;budget_period_id=[% period_loo.budget_period_id %]">Delete</a>
382
                      <a href="[% script_name %]?op=duplicate_form&amp;budget_period_id=[% period_loo.budget_period_id %]">Duplicate</a>
381
                  <a href="/cgi-bin/koha/admin/aqbudgets.pl?op=add_form&amp;budget_period_id=[% period_loo.budget_period_id %]">Add fund</a>
383
                  <a href="/cgi-bin/koha/admin/aqbudgets.pl?op=add_form&amp;budget_period_id=[% period_loo.budget_period_id %]">Add fund</a>
382
                  </td>
384
                  </td>
383
                  </tr>
385
                  </tr>
(-)a/t/db_dependent/Budgets.t (-2 / +49 lines)
Lines 1-5 Link Here
1
use Modern::Perl;
1
use Modern::Perl;
2
use Test::More tests => 23;
2
use Test::More tests => 25;
3
3
4
BEGIN {use_ok('C4::Budgets') }
4
BEGIN {use_ok('C4::Budgets') }
5
use C4::Biblio;
5
use C4::Biblio;
Lines 315-317 for my $infos (@order_infos) { Link Here
315
is( GetBudgetHierarchySpent( $budget_id1 ), 160, "total spent for budget1 is 160" );
315
is( GetBudgetHierarchySpent( $budget_id1 ), 160, "total spent for budget1 is 160" );
316
is( GetBudgetHierarchySpent( $budget_id11 ), 100, "total spent for budget1 is 100" );
316
is( GetBudgetHierarchySpent( $budget_id11 ), 100, "total spent for budget1 is 100" );
317
is( GetBudgetHierarchySpent( $budget_id111 ), 20, "total spent for budget1 is 20" );
317
is( GetBudgetHierarchySpent( $budget_id111 ), 20, "total spent for budget1 is 20" );
318
- 
318
319
# CloneBudgetPeriod
320
my $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
321
    {
322
        budget_period_id        => $budget_period_id,
323
        budget_period_startdate => '2014-01-01',
324
        budget_period_enddate   => '2014-12-31',
325
    }
326
);
327
328
my $budget_hierarchy        = GetBudgetHierarchy($budget_period_id);
329
my $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
330
331
is(
332
    scalar(@$budget_hierarchy_cloned),
333
    scalar(@$budget_hierarchy),
334
    'CloneBudgetPeriod clones the same number of budgets (funds)'
335
);
336
is_deeply(
337
    _get_dependencies($budget_hierarchy),
338
    _get_dependencies($budget_hierarchy_cloned),
339
    'CloneBudgetPeriod keep the same dependencies order'
340
);
341
342
sub _get_dependencies {
343
    my ($budget_hierarchy) = @_;
344
    my $graph;
345
    for my $budget (@$budget_hierarchy) {
346
        if ( $budget->{child} ) {
347
            my @sorted = sort @{ $budget->{child} };
348
            for my $child_id (@sorted) {
349
                push @{ $graph->{ $budget->{budget_name} }{children} },
350
                  _get_budgetname_by_id( $budget_hierarchy, $child_id );
351
            }
352
        }
353
        push @{ $graph->{ $budget->{budget_name} }{parents} },
354
          $budget->{parent_id};
355
    }
356
    return $graph;
357
}
358
359
sub _get_budgetname_by_id {
360
    my ( $budgets, $budget_id ) = @_;
361
    my ($budget_name) =
362
      map { ( $_->{budget_id} eq $budget_id ) ? $_->{budget_name} : () }
363
      @$budgets;
364
    return $budget_name;
365
}

Return to bug 12164