Bug 7162: (RM followup) fix the number of tests
[koha.git] / t / db_dependent / Budgets.t
1 use Modern::Perl;
2 use Test::More tests => 108;
3
4 BEGIN {
5     use_ok('C4::Budgets')
6 }
7 use C4::Context;
8 use C4::Biblio;
9 use C4::Bookseller;
10 use C4::Acquisition;
11 use C4::Dates;
12
13 use Koha::Acquisition::Order;
14
15 use YAML;
16 my $dbh = C4::Context->dbh;
17 $dbh->{AutoCommit} = 0;
18 $dbh->{RaiseError} = 1;
19
20 $dbh->do(q|DELETE FROM aqbudgetperiods|);
21 $dbh->do(q|DELETE FROM aqbudgets|);
22
23 # Mock userenv
24 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
25 my $userenv;
26 *C4::Context::userenv = \&Mock_userenv;
27 $userenv = { flags => 1, id => 'my_userid', branch => 'CPL' };
28
29 #
30 # Budget Periods :
31 #
32
33 is( AddBudgetPeriod(), undef, 'AddBugetPeriod without argument returns undef' );
34 is( AddBudgetPeriod( { }  ), undef, 'AddBugetPeriod with an empty argument returns undef' );
35 my $bpid = AddBudgetPeriod({
36     budget_period_startdate => '2008-01-01',
37 });
38 is( $bpid, undef, 'AddBugetPeriod without end date returns undef' );
39 $bpid = AddBudgetPeriod({
40     budget_period_enddate => '2008-12-31',
41 });
42 is( $bpid, undef, 'AddBugetPeriod without start date returns undef' );
43 is( GetBudgetPeriod(0), undef ,'GetBudgetPeriod(0) returned undef : noactive BudgetPeriod' );
44 my $budgetperiods = GetBudgetPeriods();
45 is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
46
47 my $my_budgetperiod = {
48     budget_period_startdate   => '2008-01-01',
49     budget_period_enddate     => '2008-12-31',
50     budget_period_description => 'MAPERI',
51     budget_period_active      => 0,
52 };
53 $bpid = AddBudgetPeriod($my_budgetperiod);
54 isnt( $bpid, undef, 'AddBugetPeriod does not returns undef' );
55 my $budgetperiod = GetBudgetPeriod($bpid);
56 is( $budgetperiod->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'AddBudgetPeriod stores the start date correctly' );
57 is( $budgetperiod->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'AddBudgetPeriod stores the end date correctly' );
58 is( $budgetperiod->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'AddBudgetPeriod stores the description correctly' );
59 is( $budgetperiod->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'AddBudgetPeriod stores active correctly' );
60 is( GetBudgetPeriod(0), undef ,'GetBudgetPeriod(0) returned undef : noactive BudgetPeriod' );
61
62
63 $my_budgetperiod = {
64     budget_period_startdate   => '2009-01-01',
65     budget_period_enddate     => '2009-12-31',
66     budget_period_description => 'MODIF_MAPERI',
67     budget_period_active      => 1,
68 };
69 my $mod_status = ModBudgetPeriod($my_budgetperiod);
70 is( $mod_status, undef, 'ModBudgetPeriod without id returns undef' );
71
72 $my_budgetperiod->{budget_period_id} = $bpid;
73 $mod_status = ModBudgetPeriod($my_budgetperiod);
74 is( $mod_status, 1, 'ModBudgetPeriod returnis true' );
75 $budgetperiod = GetBudgetPeriod($bpid);
76 is( $budgetperiod->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'ModBudgetPeriod updates the start date correctly' );
77 is( $budgetperiod->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'ModBudgetPeriod updates the end date correctly' );
78 is( $budgetperiod->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'ModBudgetPeriod updates the description correctly' );
79 is( $budgetperiod->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'ModBudgetPeriod upates active correctly' );
80 isnt( GetBudgetPeriod(0), undef, 'GetBugetPeriods functions correctly' );
81
82
83 $budgetperiods = GetBudgetPeriods();
84 is( @$budgetperiods, 1, 'GetBudgetPeriods returns the correct number of budget periods' );
85 is( $budgetperiods->[0]->{budget_period_id}, $my_budgetperiod->{budget_period_id}, 'GetBudgetPeriods returns the id correctly' );
86 is( $budgetperiods->[0]->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'GetBudgetPeriods returns the start date correctly' );
87 is( $budgetperiods->[0]->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'GetBudgetPeriods returns the end date correctly' );
88 is( $budgetperiods->[0]->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'GetBudgetPeriods returns the description correctly' );
89 is( $budgetperiods->[0]->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'GetBudgetPeriods returns active correctly' );
90
91 is( DelBudgetPeriod($bpid), 1, 'DelBudgetPeriod returns true' );
92 $budgetperiods = GetBudgetPeriods();
93 is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
94
95
96 #
97 # Budget  :
98 #
99
100 is( AddBudget(), undef, 'AddBuget without argument returns undef' );
101 my $budgets = GetBudgets();
102 is( @$budgets, 0, 'GetBudgets returns the correct number of budgets' );
103
104 $bpid = AddBudgetPeriod($my_budgetperiod);
105 my $my_budget = {
106     budget_code      => 'ABCD',
107     budget_amount    => '123.132000',
108     budget_name      => 'Periodiques',
109     budget_notes     => 'This is a note',
110     budget_period_id => $bpid,
111 };
112 my $budget_id = AddBudget($my_budget);
113 isnt( $budget_id, undef, 'AddBudget does not returns undef' );
114 my $budget = GetBudget($budget_id);
115 is( $budget->{budget_code}, $my_budget->{budget_code}, 'AddBudget stores the budget code correctly' );
116 is( $budget->{budget_amount}, $my_budget->{budget_amount}, 'AddBudget stores the budget amount correctly' );
117 is( $budget->{budget_name}, $my_budget->{budget_name}, 'AddBudget stores the budget name correctly' );
118 is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'AddBudget stores the budget notes correctly' );
119 is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'AddBudget stores the budget period id correctly' );
120
121
122 $my_budget = {
123     budget_code      => 'EFG',
124     budget_amount    => '321.231000',
125     budget_name      => 'Modified name',
126     budget_notes     => 'This is a modified note',
127     budget_period_id => $bpid,
128 };
129 $mod_status = ModBudget($my_budget);
130 is( $mod_status, undef, 'ModBudget without id returns undef' );
131
132 $my_budget->{budget_id} = $budget_id;
133 $mod_status = ModBudget($my_budget);
134 is( $mod_status, 1, 'ModBudget returns true' );
135 $budget = GetBudget($budget_id);
136 is( $budget->{budget_code}, $my_budget->{budget_code}, 'ModBudget updates the budget code correctly' );
137 is( $budget->{budget_amount}, $my_budget->{budget_amount}, 'ModBudget updates the budget amount correctly' );
138 is( $budget->{budget_name}, $my_budget->{budget_name}, 'ModBudget updates the budget name correctly' );
139 is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'ModBudget updates the budget notes correctly' );
140 is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'ModBudget updates the budget period id correctly' );
141
142
143 $budgets = GetBudgets();
144 is( @$budgets, 1, 'GetBudgets returns the correct number of budgets' );
145 is( $budgets->[0]->{budget_id}, $my_budget->{budget_id}, 'GetBudgets returns the budget id correctly' );
146 is( $budgets->[0]->{budget_code}, $my_budget->{budget_code}, 'GetBudgets returns the budget code correctly' );
147 is( $budgets->[0]->{budget_amount}, $my_budget->{budget_amount}, 'GetBudgets returns the budget amount correctly' );
148 is( $budgets->[0]->{budget_name}, $my_budget->{budget_name}, 'GetBudgets returns the budget name correctly' );
149 is( $budgets->[0]->{budget_notes}, $my_budget->{budget_notes}, 'GetBudgets returns the budget notes correctly' );
150 is( $budgets->[0]->{budget_period_id}, $my_budget->{budget_period_id}, 'GetBudgets returns the budget period id correctly' );
151
152 $budgets = GetBudgets( {budget_period_id => $bpid} );
153 is( @$budgets, 1, 'GetBudgets With Filter OK' );
154 $budgets = GetBudgets( {budget_period_id => $bpid}, {-asc => "budget_name"} );
155 is( @$budgets, 1, 'GetBudgets With Order OK' );
156 $budgets = GetBudgets( {budget_period_id => GetBudgetPeriod($bpid)->{budget_period_id}}, {-asc => "budget_name"} );
157 is( @$budgets, 1, 'GetBudgets With Order Getting Active budgetPeriod OK');
158
159
160 my $budget_name = GetBudgetName( $budget_id );
161 is($budget_name, $my_budget->{budget_name}, "Test the GetBudgetName routine");
162
163 my $budget_code = $my_budget->{budget_code};
164 my $budget_by_code = GetBudgetByCode( $budget_code );
165 is($budget_by_code->{budget_id}, $budget_id, "GetBudgetByCode, check id");
166 is($budget_by_code->{budget_notes}, $my_budget->{budget_notes}, "GetBudgetByCode, check notes");
167
168 my $second_budget_id = AddBudget({
169     budget_code      => "ZZZZ",
170     budget_amount    => "500.00",
171     budget_name      => "Art",
172     budget_notes     => "This is a note",
173     budget_period_id => $bpid,
174 });
175 isnt( $second_budget_id, undef, 'AddBudget does not returns undef' );
176
177 $budgets = GetBudgets( {budget_period_id => $bpid} );
178 ok( $budgets->[0]->{budget_name} lt $budgets->[1]->{budget_name}, 'default sort order for GetBudgets is by name' );
179
180 is( DelBudget($budget_id), 1, 'DelBudget returns true' );
181 $budgets = GetBudgets();
182 is( @$budgets, 1, 'GetBudgets returns the correct number of budget periods' );
183
184
185 # GetBudgetHierarchySpent and GetBudgetHierarchyOrdered
186 my $budget_period_total = 10_000;
187 my $budget_1_total = 1_000;
188 my $budget_11_total = 100;
189 my $budget_111_total = 50;
190 my $budget_12_total = 100;
191 my $budget_2_total = 2_000;
192
193 my $budget_period_id = AddBudgetPeriod(
194     {
195         budget_period_startdate   => '2013-01-01',
196         budget_period_enddate     => '2014-12-31',
197         budget_period_description => 'Budget Period',
198         budget_period_active      => 1,
199         budget_period_total       => $budget_period_total,
200     }
201 );
202 my $budget_id1 = AddBudget(
203     {
204         budget_code      => 'budget_1',
205         budget_name      => 'budget_1',
206         budget_period_id => $budget_period_id,
207         budget_parent_id => undef,
208         budget_amount    => $budget_1_total,
209     }
210 );
211 my $budget_id2 = AddBudget(
212     {
213         budget_code      => 'budget_2',
214         budget_name      => 'budget_2',
215         budget_period_id => $budget_period_id,
216         budget_parent_id => undef,
217         budget_amount    => $budget_2_total,
218     }
219 );
220 my $budget_id11 = AddBudget(
221     {
222         budget_code      => 'budget_11',
223         budget_name      => 'budget_11',
224         budget_period_id => $budget_period_id,
225         budget_parent_id => $budget_id1,
226         budget_amount    => $budget_11_total,
227     }
228 );
229 my $budget_id12 = AddBudget(
230     {
231         budget_code      => 'budget_12',
232         budget_name      => 'budget_12',
233         budget_period_id => $budget_period_id,
234         budget_parent_id => $budget_id1,
235         budget_amount    => $budget_12_total,
236     }
237 );
238 my $budget_id111 = AddBudget(
239     {
240         budget_code      => 'budget_111',
241         budget_name      => 'budget_111',
242         budget_period_id => $budget_period_id,
243         budget_parent_id => $budget_id11,
244         budget_owner_id  => 1,
245         budget_amount    => $budget_111_total,
246     }
247 );
248 my $budget_id21 = AddBudget(
249     {
250         budget_code      => 'budget_21',
251         budget_name      => 'budget_21',
252         budget_period_id => $budget_period_id,
253         budget_parent_id => $budget_id2,
254     }
255 );
256
257 my $booksellerid = C4::Bookseller::AddBookseller(
258     {
259         name         => "my vendor",
260         address1     => "bookseller's address",
261         phone        => "0123456",
262         active       => 1,
263         deliverytime => 5,
264     }
265 );
266
267 my $basketno = C4::Acquisition::NewBasket( $booksellerid, 1 );
268 my ( $biblionumber, $biblioitemnumber ) =
269   C4::Biblio::AddBiblio( MARC::Record->new, '' );
270
271 my @order_infos = (
272     {
273         budget_id => $budget_id1,
274         pending_quantity  => 1,
275         spent_quantity  => 0,
276     },
277     {
278         budget_id => $budget_id2,
279         pending_quantity  => 2,
280         spent_quantity  => 1,
281     },
282     {
283         budget_id => $budget_id11,
284         pending_quantity  => 3,
285         spent_quantity  => 4,
286     },
287     {
288         budget_id => $budget_id12,
289         pending_quantity  => 4,
290         spent_quantity  => 3,
291     },
292     {
293         budget_id => $budget_id111,
294         pending_quantity  => 2,
295         spent_quantity  => 1,
296     },
297
298     # No order for budget_21
299
300 );
301
302 my %budgets;
303 my $invoiceid = AddInvoice(invoicenumber => 'invoice_test_clone', booksellerid => $booksellerid, unknown => "unknown");
304 my $item_price = 10;
305 my $item_quantity = 2;
306 my $number_of_orders_to_move = 0;
307 for my $infos (@order_infos) {
308     for ( 1 .. $infos->{pending_quantity} ) {
309         my $order = Koha::Acquisition::Order->new(
310             {
311                 basketno           => $basketno,
312                 biblionumber       => $biblionumber,
313                 budget_id          => $infos->{budget_id},
314                 order_internalnote => "internal note",
315                 order_vendornote   => "vendor note",
316                 quantity           => 2,
317                 cost               => $item_price,
318                 rrp                => $item_price,
319                 listprice          => $item_price,
320                 ecost              => $item_price,
321                 rrp                => $item_price,
322                 discount           => 0,
323                 uncertainprice     => 0,
324                 gstrate            => 0,
325             }
326         )->insert;
327         my $ordernumber = $order->{ordernumber};
328         push @{ $budgets{$infos->{budget_id}} }, $ordernumber;
329         $number_of_orders_to_move++;
330     }
331     for ( 1 .. $infos->{spent_quantity} ) {
332         my $order = Koha::Acquisition::Order->new(
333             {
334                 basketno           => $basketno,
335                 biblionumber       => $biblionumber,
336                 budget_id          => $infos->{budget_id},
337                 order_internalnote => "internal note",
338                 order_vendornote   => "vendor note",
339                 quantity           => $item_quantity,
340                 cost               => $item_price,
341                 rrp                => $item_price,
342                 listprice          => $item_price,
343                 ecost              => $item_price,
344                 rrp                => $item_price,
345                 discount           => 0,
346                 uncertainprice     => 0,
347                 gstrate            => 0,
348             }
349         )->insert;
350         my $ordernumber = $order->{ordernumber};
351         ModReceiveOrder({
352               biblionumber     => $biblionumber,
353               ordernumber      => $ordernumber,
354               budget_id        => $infos->{budget_id},
355               quantityreceived => $item_quantity,
356               cost             => $item_price,
357               ecost            => $item_price,
358               invoiceid        => $invoiceid,
359               rrp              => $item_price,
360               received_items   => [],
361         } );
362     }
363 }
364 is( GetBudgetHierarchySpent( $budget_id1 ), 160, "total spent for budget1 is 160" );
365 is( GetBudgetHierarchySpent( $budget_id11 ), 100, "total spent for budget11 is 100" );
366 is( GetBudgetHierarchySpent( $budget_id111 ), 20, "total spent for budget111 is 20" );
367
368 # CloneBudgetPeriod
369 my $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
370     {
371         budget_period_id        => $budget_period_id,
372         budget_period_startdate => '2014-01-01',
373         budget_period_enddate   => '2014-12-31',
374         budget_period_description => 'Budget Period Cloned',
375     }
376 );
377
378 my $budget_period_cloned = C4::Budgets::GetBudgetPeriod($budget_period_id_cloned);
379 is($budget_period_cloned->{budget_period_description}, 'Budget Period Cloned', 'Cloned budget\'s description is updated.');
380
381 my $budget_hierarchy        = GetBudgetHierarchy($budget_period_id);
382 my $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
383
384 is(
385     scalar(@$budget_hierarchy_cloned),
386     scalar(@$budget_hierarchy),
387     'CloneBudgetPeriod clones the same number of budgets (funds)'
388 );
389 is_deeply(
390     _get_dependencies($budget_hierarchy),
391     _get_dependencies($budget_hierarchy_cloned),
392     'CloneBudgetPeriod keeps the same dependencies order'
393 );
394
395 # CloneBudgetPeriod with param mark_original_budget_as_inactive
396 my $budget_period = C4::Budgets::GetBudgetPeriod($budget_period_id);
397 is( $budget_period->{budget_period_active}, 1,
398     'CloneBudgetPeriod does not mark as inactive the budgetperiod if not needed'
399 );
400
401 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
402 my $number_of_budgets_not_reset = 0;
403 for my $budget (@$budget_hierarchy_cloned) {
404     $number_of_budgets_not_reset++ if $budget->{budget_amount} > 0;
405 }
406 is( $number_of_budgets_not_reset, 5,
407     'CloneBudgetPeriod does not reset budgets (funds) if not needed' );
408
409 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
410     {
411         budget_period_id                 => $budget_period_id,
412         budget_period_startdate          => '2014-01-01',
413         budget_period_enddate            => '2014-12-31',
414         mark_original_budget_as_inactive => 1,
415     }
416 );
417
418 $budget_hierarchy        = GetBudgetHierarchy($budget_period_id);
419 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
420
421 is( scalar(@$budget_hierarchy_cloned), scalar(@$budget_hierarchy),
422 'CloneBudgetPeriod (with inactive param) clones the same number of budgets (funds)'
423 );
424 is_deeply(
425     _get_dependencies($budget_hierarchy),
426     _get_dependencies($budget_hierarchy_cloned),
427     'CloneBudgetPeriod (with inactive param) keeps the same dependencies order'
428 );
429 $budget_period = C4::Budgets::GetBudgetPeriod($budget_period_id);
430 is( $budget_period->{budget_period_active}, 0,
431     'CloneBudgetPeriod (with inactive param) marks as inactive the budgetperiod'
432 );
433
434 # CloneBudgetPeriod with param reset_all_budgets
435 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
436     {
437         budget_period_id        => $budget_period_id,
438         budget_period_startdate => '2014-01-01',
439         budget_period_enddate   => '2014-12-31',
440         reset_all_budgets         => 1,
441     }
442 );
443
444 $budget_hierarchy_cloned     = GetBudgetHierarchy($budget_period_id_cloned);
445 $number_of_budgets_not_reset = 0;
446 for my $budget (@$budget_hierarchy_cloned) {
447     $number_of_budgets_not_reset++ if $budget->{budget_amount} > 0;
448 }
449 is( $number_of_budgets_not_reset, 0,
450     'CloneBudgetPeriod has reset all budgets (funds)' );
451
452
453 # MoveOrders
454 my $number_orders_moved = C4::Budgets::MoveOrders();
455 is( $number_orders_moved, undef, 'MoveOrders return undef if no arg passed' );
456 $number_orders_moved =
457   C4::Budgets::MoveOrders( { from_budget_period_id => $budget_period_id } );
458 is( $number_orders_moved, undef,
459     'MoveOrders return undef if only 1 arg passed' );
460 $number_orders_moved =
461   C4::Budgets::MoveOrders( { to_budget_period_id => $budget_period_id } );
462 is( $number_orders_moved, undef,
463     'MoveOrders return undef if only 1 arg passed' );
464 $number_orders_moved = C4::Budgets::MoveOrders(
465     {
466         from_budget_period_id => $budget_period_id,
467         to_budget_period_id   => $budget_period_id
468     }
469 );
470 is( $number_orders_moved, undef,
471     'MoveOrders return undef if 2 budget period id are the same' );
472
473 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
474     {
475         budget_period_id        => $budget_period_id,
476         budget_period_startdate => '2014-01-01',
477         budget_period_enddate   => '2014-12-31',
478     }
479 );
480
481 my $report = C4::Budgets::MoveOrders(
482     {
483         from_budget_period_id  => $budget_period_id,
484         to_budget_period_id    => $budget_period_id_cloned,
485         move_remaining_unspent => 1,
486     }
487 );
488 is( scalar( @$report ), 6 , "MoveOrders has processed 6 funds" );
489
490 my $number_of_orders_moved = 0;
491 $number_of_orders_moved += scalar( @{ $_->{orders_moved} } ) for @$report;
492 is( $number_of_orders_moved, $number_of_orders_to_move, "MoveOrders has moved $number_of_orders_to_move orders" );
493
494 my @new_budget_ids = map { $_->{budget_id} }
495   @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
496 my @old_budget_ids = map { $_->{budget_id} }
497   @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
498 for my $budget_id ( keys %budgets ) {
499     for my $ordernumber ( @{ $budgets{$budget_id} } ) {
500         my $budget            = GetBudgetByOrderNumber($ordernumber);
501         my $is_in_new_budgets = grep /^$budget->{budget_id}$/, @new_budget_ids;
502         my $is_in_old_budgets = grep /^$budget->{budget_id}$/, @old_budget_ids;
503         is( $is_in_new_budgets, 1, "MoveOrders changed the budget_id for order $ordernumber" );
504         is( $is_in_old_budgets, 0, "MoveOrders changed the budget_id for order $ordernumber" );
505     }
506 }
507
508
509 # MoveOrders with param move_remaining_unspent
510 my @new_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
511 my @old_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
512
513 for my $new_budget ( @new_budgets ) {
514     my ( $old_budget ) = map { $_->{budget_code} eq $new_budget->{budget_code} ? $_ : () } @old_budgets;
515     my $new_budget_amount_should_be = $old_budget->{budget_amount} * 2 - $old_budget->{total_spent};
516     is( $new_budget->{budget_amount} + 0, $new_budget_amount_should_be, "MoveOrders updated the budget amount with the previous unspent budget (for budget $new_budget->{budget_code})" );
517 }
518
519 sub _get_dependencies {
520     my ($budget_hierarchy) = @_;
521     my $graph;
522     for my $budget (@$budget_hierarchy) {
523         if ( $budget->{child} ) {
524             my @sorted = sort @{ $budget->{child} };
525             for my $child_id (@sorted) {
526                 push @{ $graph->{ $budget->{budget_name} }{children} },
527                   _get_budgetname_by_id( $budget_hierarchy, $child_id );
528             }
529         }
530         push @{ $graph->{ $budget->{budget_name} }{parents} },
531           $budget->{parent_id};
532     }
533     return $graph;
534 }
535
536 sub _get_budgetname_by_id {
537     my ( $budgets, $budget_id ) = @_;
538     my ($budget_name) =
539       map { ( $_->{budget_id} eq $budget_id ) ? $_->{budget_name} : () }
540       @$budgets;
541     return $budget_name;
542 }
543
544 # C4::Context->userenv
545 sub Mock_userenv {
546     return $userenv;
547 }