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

(-)a/C4/Budgets.pm (+61 lines)
Lines 867-872 sub CanUserModifyBudget { Link Here
867
}
867
}
868
868
869
# -------------------------------------------------------------------
869
# -------------------------------------------------------------------
870
sub DuplicateBudget {
871
    my ($budget_period_id, $startdate, $enddate, $budget_period_description, $budget_transfer_data) = @_;
872
    die "please specify a budget period id\n" if( !defined $budget_period_id || $budget_period_id eq '' );
873
874
    my $dbh   = C4::Context->dbh;
875
    my $data = GetBudgetPeriod( $budget_period_id);
876
877
    $data->{'budget_period_startdate'} = $startdate;
878
    $data->{'budget_period_enddate'} = $enddate;
879
    $data->{'budget_period_description'} = $budget_period_description;
880
    delete $data->{'budget_period_id'};
881
    my $new_budget_period_id = C4::SQLHelper::InsertInTable('aqbudgetperiods', $data);
882
883
    my $tree = GetBudgetHierarchy( $budget_period_id );
884
885
    # hash mapping old ids to new
886
    my %old_new;
887
    # hash mapping old parent ids to list of new children ids
888
    # only store a child here if the parents old id isnt in the old_new map
889
    # when the parent is found, this map will be used, and then the entry removed and their id placed in old_new
890
    my %parent_children;
891
892
    for my $entry( @$tree ){
893
        die "serious errors, parent period $budget_period_id doesnt match child ", $entry->{'budget_period_id'}, "\n" if( $entry->{'budget_period_id'} != $budget_period_id );
894
        my $orphan = 0; # set to 1 if we need to make an entry in parent_children
895
        my $old_id = delete $entry->{'budget_id'};
896
        my $parent_id = delete $entry->{'budget_parent_id'};
897
        $entry->{'budget_period_id'} = $new_budget_period_id;
898
899
        if( !defined $parent_id ){
900
        } elsif( defined $parent_id && $parent_id eq '' ){
901
        } elsif( defined $old_new{$parent_id} ){
902
            # set parent id now
903
            $entry->{'budget_parent_id'} = $old_new{$parent_id};
904
        } else {
905
            # make an entry in parent_children
906
            $parent_children{$parent_id} = [] unless defined $parent_children{$parent_id};
907
            $orphan = 1;
908
        }
909
910
        # write it to db
911
        my $new_id = C4::SQLHelper::InsertInTable('aqbudgets', $entry);
912
        $old_new{$old_id} = $new_id;
913
        push @{$parent_children{$parent_id}}, $new_id if $orphan;
914
915
        if ($budget_transfer_data == 1) {
916
            #Transfers the unreceived orders from the previous fund to the new
917
            my $query = "
918
            UPDATE aqorders SET budget_id = ?
919
                WHERE budget_id=? AND
920
                quantityreceived = 0 AND
921
                datecancellationprinted IS NULL";
922
            my $sth = $dbh->prepare($query);
923
            $sth->execute($new_id, $old_id );
924
        }
925
    }
926
927
    return $new_budget_period_id;
928
}
929
930
# -------------------------------------------------------------------
870
931
871
=head2 GetCurrencies
932
=head2 GetCurrencies
872
933
(-)a/admin/aqbudgetperiods.pl (-49 / +6 lines)
Lines 173-238 elsif ( $op eq 'delete_confirmed' ) { Link Here
173
173
174
# display the form for duplicating
174
# display the form for duplicating
175
elsif ( $op eq 'duplicate_form'){
175
elsif ( $op eq 'duplicate_form'){
176
    my $budgetperiod = GetBudgetPeriod($budget_period_id, $input);
176
    $template->param(
177
    $template->param(
177
        'duplicate_form' => '1',
178
        'duplicate_form' => '1',
178
        'budget_period_id' => $budget_period_id,
179
        'budget_period_id' => $budget_period_id,
180
        'budgetperiod' => $budgetperiod,
179
    );
181
    );
180
}
182
}
181
183
182
# handle the actual duplication
184
# handle the actual duplication
183
elsif ( $op eq 'duplicate_budget' ){
185
elsif ( $op eq 'duplicate_budget' ){
184
    die "please specify a budget period id\n" if( !defined $budget_period_id || $budget_period_id eq '' );
186
185
    my $startdate = $input->param('budget_period_startdate');
187
    my $startdate = $input->param('budget_period_startdate');
186
    my $enddate = $input->param('budget_period_enddate');
188
    my $enddate = $input->param('budget_period_enddate');
187
189
188
    my $data = GetBudgetPeriod( $budget_period_id);
190
    my $budget_period_description = $input->param('budget_period_description');
189
191
    my $budget_transfer_data = $input->param('budget_transfer_data');
190
    $data->{'budget_period_startdate'} = $startdate;
192
    C4::Budgets::DuplicateBudget($budget_period_id, $startdate, $enddate, $budget_period_description, $budget_transfer_data);
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
222
        # write it to db
223
        my $new_id = C4::SQLHelper::InsertInTable('aqbudgets', $entry);
224
        $old_new{$old_id} = $new_id;
225
        push @{$parent_children{$parent_id}}, $new_id if $orphan;
226
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
        }
235
    }
236
193
237
    # display the list of budgets
194
    # display the list of budgets
238
    $op = 'else';
195
    $op = 'else';
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgetperiods.tt (+10 lines)
Lines 198-203 Link Here
198
				<div class="hint">[% INCLUDE 'date-format.inc' %]</div>
198
				<div class="hint">[% INCLUDE 'date-format.inc' %]</div>
199
    </li>
199
    </li>
200
200
201
    <li>
202
        <label class="required" for="budget_period_description">Description</label>
203
        <input type="text" id="budget_period_description" name="budget_period_description" value="[% budgetperiod.budget_period_description %]" />
204
    </li>
205
206
    <li>
207
        <label for="budget_transfer_data">Transfer the ordered acquisitions</label>
208
        <input type="checkbox" id="budget_transfer_data" name="budget_transfer_data" value="1" />
209
    </li>
210
201
    </ol>
211
    </ol>
202
    </fieldset>
212
    </fieldset>
203
213
(-)a/t/db_dependent/Budgets.t (-10 / +29 lines)
Lines 1-6 Link Here
1
use strict;
1
use strict;
2
use warnings;
2
use warnings;
3
use Test::More tests=>18;
3
use Test::More tests=>29;
4
4
5
BEGIN {use_ok('C4::Budgets') }
5
BEGIN {use_ok('C4::Budgets') }
6
use C4::Dates;
6
use C4::Dates;
Lines 16-28 my $active_period; Link Here
16
my $mod_status;
16
my $mod_status;
17
my $del_status;
17
my $del_status;
18
ok($bpid=AddBudgetPeriod(
18
ok($bpid=AddBudgetPeriod(
19
						{ budget_period_startdate	=> '2008-01-01'
19
                        { budget_period_startdate   => '2008-01-01'
20
						, budget_period_enddate		=> '2008-12-31'
20
                        , budget_period_enddate     => '2008-12-31'
21
						, budget_description		=> "MAPERI"}),
21
                        , budget_period_description     => "MAPERI"}),
22
	"AddBudgetPeriod with iso dates OK");
22
    "AddBudgetPeriod with iso dates OK");
23
23
24
ok($budgetperiod=GetBudgetPeriod($bpid),
24
ok($budgetperiod=GetBudgetPeriod($bpid),
25
	"GetBudgetPeriod($bpid) returned ".Dump($budgetperiod));
25
    "GetBudgetPeriod($bpid) returned ".Dump($budgetperiod));
26
ok(!GetBudgetPeriod(0) ,"GetBudgetPeriod(0) returned undef : noactive BudgetPeriod");
26
ok(!GetBudgetPeriod(0) ,"GetBudgetPeriod(0) returned undef : noactive BudgetPeriod");
27
$$budgetperiod{budget_period_active}=1;
27
$$budgetperiod{budget_period_active}=1;
28
ok($mod_status=ModBudgetPeriod($budgetperiod),"ModBudgetPeriod OK");
28
ok($mod_status=ModBudgetPeriod($budgetperiod),"ModBudgetPeriod OK");
Lines 39-58 if (C4::Context->preference('dateformat') eq "metric"){ Link Here
39
ok($bpid=AddBudgetPeriod(
39
ok($bpid=AddBudgetPeriod(
40
						{ budget_period_startdate	=>'01-01-2008'
40
						{ budget_period_startdate	=>'01-01-2008'
41
						, budget_period_enddate		=>'31-12-2008'
41
						, budget_period_enddate		=>'31-12-2008'
42
						, budget_description		=>"MAPERI"}),
42
						, budget_period_description		=>"MAPERI"}),
43
	"AddBudgetPeriod returned $bpid");
43
	"AddBudgetPeriod returned $bpid");
44
} elsif (C4::Context->preference('dateformat') eq "us"){
44
} elsif (C4::Context->preference('dateformat') eq "us"){
45
ok($bpid=AddBudgetPeriod(
45
ok($bpid=AddBudgetPeriod(
46
						{ budget_period_startdate	=>'01-01-2008'
46
						{ budget_period_startdate	=>'01-01-2008'
47
						, budget_period_enddate		=>'12-31-2008'
47
						, budget_period_enddate		=>'12-31-2008'
48
						, budget_description		=>"MAPERI"}),
48
						, budget_period_description		=>"MAPERI"}),
49
	"AddBudgetPeriod returned $bpid");
49
	"AddBudgetPeriod returned $bpid");
50
}
50
}
51
else{
51
else{
52
ok($bpid=AddBudgetPeriod(
52
ok($bpid=AddBudgetPeriod(
53
						{budget_period_startdate=>'2008-01-01'
53
						{budget_period_startdate=>'2008-01-01'
54
						,budget_period_enddate	=>'2008-12-31'
54
						,budget_period_enddate	=>'2008-12-31'
55
						,budget_description		=>"MAPERI"
55
						,budget_period_description		=>"MAPERI"
56
						}),
56
						}),
57
	"AddBudgetPeriod returned $bpid");
57
	"AddBudgetPeriod returned $bpid");
58
58
Lines 102-106 ok(GetBudgets({budget_period_id=>GetBudgetPeriod($bpid)->{budget_period_id}},[{" Link Here
102
my $budget_name = GetBudgetName( $budget_id );
102
my $budget_name = GetBudgetName( $budget_id );
103
is($budget_name, $budget->{budget_name}, "Test the GetBudgetName routine");
103
is($budget_name, $budget->{budget_name}, "Test the GetBudgetName routine");
104
104
105
my $new_bpid;
106
ok($new_bpid = C4::Budgets::DuplicateBudget($bpid, '2012-01-01', '2012-08-01', 'MAPERI COPY'),
107
    "Duplicate budget returned $new_bpid");
108
109
my $budgetcpyp;
110
ok($budgetcpyp = GetBudgetPeriod($new_bpid) ,"GetBudgetPeriod OK");
111
112
#Verify data of the copy
113
is ($budgetcpyp->{'budget_period_startdate'}, '2012-01-01');
114
is ($budgetcpyp->{'budget_period_enddate'}, '2012-08-01');
115
is ($budgetcpyp->{'budget_period_description'}, 'MAPERI COPY');
116
117
my $budgetscpy;
118
ok($budgetscpy = GetBudgetHierarchy( $new_bpid ));
119
is (scalar(@$budgetscpy), 2, '2 budgets for that budget period : the original and his child');
120
is (@$budgetscpy[0]->{'budget_code'}, 'ABCD');
121
is (@$budgetscpy[0]->{'budget_amount'}, '123.132000');
122
is (@$budgetscpy[0]->{'budget_notes'}, 'This is a note');
123
is (@$budgetscpy[0]->{'budget_name'}, 'Serials');
124
105
ok($del_status=DelBudget($budget_id),
125
ok($del_status=DelBudget($budget_id),
106
    "DelBudget returned $del_status");
126
    "DelBudget returned $del_status");
107
- 

Return to bug 7498