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

(-)a/C4/Budgets.pm (+62 lines)
Lines 921-926 sub CanUserModifyBudget { Link Here
921
921
922
# -------------------------------------------------------------------
922
# -------------------------------------------------------------------
923
923
924
sub DuplicateBudget {
925
    my ($budget_period_id, $startdate, $enddate, $budget_period_description, $budget_transfer_data) = @_;
926
    die "please specify a budget period id\n" if( !defined $budget_period_id || $budget_period_id eq '' );
927
928
    my $dbh   = C4::Context->dbh;
929
    my $data = GetBudgetPeriod( $budget_period_id);
930
931
    $data->{'budget_period_startdate'} = $startdate;
932
    $data->{'budget_period_enddate'} = $enddate;
933
    $data->{'budget_period_description'} = $budget_period_description;
934
    delete $data->{'budget_period_id'};
935
    my $new_budget_period_id = C4::SQLHelper::InsertInTable('aqbudgetperiods', $data);
936
937
    my $tree = GetBudgetHierarchy( $budget_period_id );
938
939
    # hash mapping old ids to new
940
    my %old_new;
941
    # hash mapping old parent ids to list of new children ids
942
    # only store a child here if the parents old id isnt in the old_new map
943
    # when the parent is found, this map will be used, and then the entry removed and their id placed in old_new
944
    my %parent_children;
945
946
    for my $entry( @$tree ){
947
        die "serious errors, parent period $budget_period_id doesnt match child ", $entry->{'budget_period_id'}, "\n" if( $entry->{'budget_period_id'} != $budget_period_id );
948
        my $orphan = 0; # set to 1 if we need to make an entry in parent_children
949
        my $old_id = delete $entry->{'budget_id'};
950
        my $parent_id = delete $entry->{'budget_parent_id'};
951
        $entry->{'budget_period_id'} = $new_budget_period_id;
952
953
        if( !defined $parent_id ){
954
        } elsif( defined $parent_id && $parent_id eq '' ){
955
        } elsif( defined $old_new{$parent_id} ){
956
            # set parent id now
957
            $entry->{'budget_parent_id'} = $old_new{$parent_id};
958
        } else {
959
            # make an entry in parent_children
960
            $parent_children{$parent_id} = [] unless defined $parent_children{$parent_id};
961
            $orphan = 1;
962
        }
963
964
        # write it to db
965
        my $new_id = C4::SQLHelper::InsertInTable('aqbudgets', $entry);
966
        $old_new{$old_id} = $new_id;
967
        push @{$parent_children{$parent_id}}, $new_id if $orphan;
968
969
        if ($budget_transfer_data == 1) {
970
            #Transfers the unreceived orders from the previous fund to the new
971
            my $query = "
972
            UPDATE aqorders SET budget_id = ?
973
                WHERE budget_id=? AND
974
                quantityreceived = 0 AND
975
                datecancellationprinted IS NULL";
976
            my $sth = $dbh->prepare($query);
977
            $sth->execute($new_id, $old_id );
978
        }
979
    }
980
981
    return $new_budget_period_id;
982
}
983
984
# -------------------------------------------------------------------
985
924
=head2 GetCurrencies
986
=head2 GetCurrencies
925
987
926
  @currencies = &GetCurrencies;
988
  @currencies = &GetCurrencies;
(-)a/admin/aqbudgetperiods.pl (+8 lines)
Lines 176-184 elsif ( $op eq 'delete_confirmed' ) { Link Here
176
176
177
# display the form for duplicating
177
# display the form for duplicating
178
elsif ( $op eq 'duplicate_form'){
178
elsif ( $op eq 'duplicate_form'){
179
    my $budgetperiod = GetBudgetPeriod($budget_period_id, $input);
179
    $template->param(
180
    $template->param(
180
        'duplicate_form' => '1',
181
        'duplicate_form' => '1',
181
        'budget_period_id' => $budget_period_id,
182
        'budget_period_id' => $budget_period_id,
183
        'budgetperiod' => $budgetperiod,
182
    );
184
    );
183
}
185
}
184
186
Lines 186-191 elsif ( $op eq 'duplicate_form'){ Link Here
186
elsif ( $op eq 'duplicate_budget' ){
188
elsif ( $op eq 'duplicate_budget' ){
187
    die "please specify a budget period id\n" if( !defined $budget_period_id || $budget_period_id eq '' );
189
    die "please specify a budget period id\n" if( !defined $budget_period_id || $budget_period_id eq '' );
188
190
191
    my $startdate = $input->param('budget_period_startdate');
192
    my $enddate = $input->param('budget_period_enddate');
193
189
    my $data = GetBudgetPeriod( $budget_period_id);
194
    my $data = GetBudgetPeriod( $budget_period_id);
190
    $data->{'budget_period_startdate'} = $budget_period_hashref->{budget_period_startdate};
195
    $data->{'budget_period_startdate'} = $budget_period_hashref->{budget_period_startdate};
191
    $data->{'budget_period_enddate'} = $budget_period_hashref->{budget_period_enddate};
196
    $data->{'budget_period_enddate'} = $budget_period_hashref->{budget_period_enddate};
Lines 236-241 elsif ( $op eq 'duplicate_budget' ){ Link Here
236
            delete $parent_children{$old_id};
241
            delete $parent_children{$old_id};
237
        }
242
        }
238
    }
243
    }
244
    my $budget_period_description = $input->param('budget_period_description');
245
    my $budget_transfer_data = $input->param('budget_transfer_data');
246
    C4::Budgets::DuplicateBudget($budget_period_id, $startdate, $enddate, $budget_period_description, $budget_transfer_data);
239
247
240
    # display the list of budgets
248
    # display the list of budgets
241
    $op = 'else';
249
    $op = 'else';
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgetperiods.tt (+10 lines)
Lines 185-190 Link Here
185
				<div class="hint">[% INCLUDE 'date-format.inc' %]</div>
185
				<div class="hint">[% INCLUDE 'date-format.inc' %]</div>
186
    </li>
186
    </li>
187
187
188
    <li>
189
        <label class="required" for="budget_period_description">Description</label>
190
        <input type="text" id="budget_period_description" name="budget_period_description" value="[% budgetperiod.budget_period_description %]" />
191
    </li>
192
193
    <li>
194
        <label for="budget_transfer_data">Transfer the ordered acquisitions</label>
195
        <input type="checkbox" id="budget_transfer_data" name="budget_transfer_data" value="1" />
196
    </li>
197
188
    </ol>
198
    </ol>
189
    </fieldset>
199
    </fieldset>
190
200
(-)a/t/db_dependent/Budgets.t (-8 / +45 lines)
Lines 1-5 Link Here
1
use Modern::Perl;
1
use Modern::Perl;
2
use Test::More tests => 63;
2
use Test::More tests => 75;
3
3
4
BEGIN {
4
BEGIN {
5
    use_ok('C4::Budgets')
5
    use_ok('C4::Budgets')
Lines 84-89 is( DelBudgetPeriod($bpid), 1, 'DelBudgetPeriod returns true' ); Link Here
84
$budgetperiods = GetBudgetPeriods();
84
$budgetperiods = GetBudgetPeriods();
85
is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
85
is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
86
86
87
if (C4::Context->preference('dateformat') eq "metric"){
88
 ok($bpid=AddBudgetPeriod(
89
                        { budget_period_startdate   =>'01-01-2008'
90
                        , budget_period_enddate     =>'31-12-2008'
91
                        , budget_period_description        =>"MAPERI"}),
92
    "AddBudgetPeriod returned $bpid");
93
 } elsif (C4::Context->preference('dateformat') eq "us"){
94
 ok($bpid=AddBudgetPeriod(
95
                        { budget_period_startdate   =>'01-01-2008'
96
                        , budget_period_enddate     =>'12-31-2008'
97
                        , budget_period_description        =>"MAPERI"}),
98
    "AddBudgetPeriod returned $bpid");
99
 }
100
 else{
101
 ok($bpid=AddBudgetPeriod(
102
                        {budget_period_startdate=>'2008-01-01'
103
                        ,budget_period_enddate  =>'2008-12-31'
104
                        ,budget_period_description        =>"MAPERI"
105
                        }),
106
    "AddBudgetPeriod returned $bpid");
87
107
88
#
108
#
89
# Budget  :
109
# Budget  :
Lines 110-116 is( $budget->{budget_name}, $my_budget->{budget_name}, 'AddBudget stores the bud Link Here
110
is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'AddBudget stores the budget notes correctly' );
130
is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'AddBudget stores the budget notes correctly' );
111
is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'AddBudget stores the budget period id correctly' );
131
is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'AddBudget stores the budget period id correctly' );
112
132
113
114
$my_budget = {
133
$my_budget = {
115
    budget_code      => 'EFG',
134
    budget_code      => 'EFG',
116
    budget_amount    => '321.231000',
135
    budget_amount    => '321.231000',
Lines 131-137 is( $budget->{budget_name}, $my_budget->{budget_name}, 'ModBudget updates the bu Link Here
131
is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'ModBudget updates the budget notes correctly' );
150
is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'ModBudget updates the budget notes correctly' );
132
is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'ModBudget updates the budget period id correctly' );
151
is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'ModBudget updates the budget period id correctly' );
133
152
134
135
$budgets = GetBudgets();
153
$budgets = GetBudgets();
136
is( @$budgets, 1, 'GetBudgets returns the correct number of budgets' );
154
is( @$budgets, 1, 'GetBudgets returns the correct number of budgets' );
137
is( $budgets->[0]->{budget_id}, $my_budget->{budget_id}, 'GetBudgets returns the budget id correctly' );
155
is( $budgets->[0]->{budget_id}, $my_budget->{budget_id}, 'GetBudgets returns the budget id correctly' );
Lines 148-154 is( @$budgets, 1, 'GetBudgets With Order OK' ); Link Here
148
$budgets = GetBudgets( {budget_period_id => GetBudgetPeriod($bpid)->{budget_period_id}}, {-asc => "budget_name"} );
166
$budgets = GetBudgets( {budget_period_id => GetBudgetPeriod($bpid)->{budget_period_id}}, {-asc => "budget_name"} );
149
is( @$budgets, 1, 'GetBudgets With Order Getting Active budgetPeriod OK');
167
is( @$budgets, 1, 'GetBudgets With Order Getting Active budgetPeriod OK');
150
168
151
152
my $budget_name = GetBudgetName( $budget_id );
169
my $budget_name = GetBudgetName( $budget_id );
153
is($budget_name, $my_budget->{budget_name}, "Test the GetBudgetName routine");
170
is($budget_name, $my_budget->{budget_name}, "Test the GetBudgetName routine");
154
171
Lines 169-178 isnt( $second_budget_id, undef, 'AddBudget does not returns undef' ); Link Here
169
$budgets = GetBudgets( {budget_period_id => $bpid} );
186
$budgets = GetBudgets( {budget_period_id => $bpid} );
170
ok( $budgets->[0]->{budget_name} lt $budgets->[1]->{budget_name}, 'default sort order for GetBudgets is by name' );
187
ok( $budgets->[0]->{budget_name} lt $budgets->[1]->{budget_name}, 'default sort order for GetBudgets is by name' );
171
188
172
is( DelBudget($budget_id), 1, 'DelBudget returns true' );
189
my $new_bpid;
173
$budgets = GetBudgets();
190
ok($new_bpid = C4::Budgets::DuplicateBudget($bpid, '2009-01-01', '2009-12-31', 'MAPERI COPY'),
174
is( @$budgets, 1, 'GetBudgets returns the correct number of budget periods' );
191
    "Duplicate budget returned $new_bpid");
192
193
my $budgetcpyp;
194
ok($budgetcpyp = GetBudgetPeriod($new_bpid) ,"GetBudgetPeriod OK");
175
195
196
is ($budgetcpyp->{'budget_period_startdate'}, '2009-01-01');
197
is ($budgetcpyp->{'budget_period_enddate'}, '2009-12-31');
198
is ($budgetcpyp->{'budget_period_description'}, 'MAPERI COPY');
199
200
my $budgetscpy;
201
ok($budgetscpy = GetBudgetHierarchy( $bpid ));
202
is (scalar(@$budgetscpy), 2, '2 budgets for that budget period : the original and his child');
203
is (@$budgetscpy[0]->{'budget_code'}, 'EFG');
204
is (@$budgetscpy[0]->{'budget_amount'}, '321.231000');
205
is (@$budgetscpy[0]->{'budget_notes'}, 'This is a modified note');
206
is (@$budgetscpy[0]->{'budget_name'}, 'Modified name');
207
208
my $budgetCount = @{GetBudgets()};
209
is( DelBudget($second_budget_id), 1, 'DelBudget returns true' );
210
$budgets = GetBudgets();
211
is( @$budgets, $budgetCount-1, 'GetBudgets returns the correct number of budget periods' );
176
212
177
# GetBudgetHierarchySpent and GetBudgetHierarchyOrdered
213
# GetBudgetHierarchySpent and GetBudgetHierarchyOrdered
178
my $budget_period_total = 10_000;
214
my $budget_period_total = 10_000;
Lines 352-354 for my $infos (@order_infos) { Link Here
352
is( GetBudgetHierarchySpent( $budget_id1 ), 160, "total spent for budget1 is 160" );
388
is( GetBudgetHierarchySpent( $budget_id1 ), 160, "total spent for budget1 is 160" );
353
is( GetBudgetHierarchySpent( $budget_id11 ), 100, "total spent for budget11 is 100" );
389
is( GetBudgetHierarchySpent( $budget_id11 ), 100, "total spent for budget11 is 100" );
354
is( GetBudgetHierarchySpent( $budget_id111 ), 20, "total spent for budget111 is 20" );
390
is( GetBudgetHierarchySpent( $budget_id111 ), 20, "total spent for budget111 is 20" );
355
- 
391
392
}

Return to bug 7498