2 use Test::More tests => 125;
11 use C4::Members qw( AddMember );
13 use Koha::Acquisition::Order;
15 use t::lib::TestBuilder;
19 my $schema = Koha::Database->new->schema;
20 $schema->storage->txn_begin;
21 my $builder = t::lib::TestBuilder->new;
22 my $dbh = C4::Context->dbh;
23 $dbh->do(q|DELETE FROM aqbudgetperiods|);
24 $dbh->do(q|DELETE FROM aqbudgets|);
26 my $library = $builder->build({
31 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
33 *C4::Context::userenv = \&Mock_userenv;
34 $userenv = { flags => 1, id => 'my_userid', branch => $library->{branchcode} };
40 is( AddBudgetPeriod(), undef, 'AddBugetPeriod without argument returns undef' );
41 is( AddBudgetPeriod( { } ), undef, 'AddBugetPeriod with an empty argument returns undef' );
42 my $bpid = AddBudgetPeriod({
43 budget_period_startdate => '2008-01-01',
45 is( $bpid, undef, 'AddBugetPeriod without end date returns undef' );
46 $bpid = AddBudgetPeriod({
47 budget_period_enddate => '2008-12-31',
49 is( $bpid, undef, 'AddBugetPeriod without start date returns undef' );
50 is( GetBudgetPeriod(0), undef ,'GetBudgetPeriod(0) returned undef : noactive BudgetPeriod' );
51 my $budgetperiods = GetBudgetPeriods();
52 is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
54 my $my_budgetperiod = {
55 budget_period_startdate => '2008-01-01',
56 budget_period_enddate => '2008-12-31',
57 budget_period_description => 'MAPERI',
58 budget_period_active => 0,
60 $bpid = AddBudgetPeriod($my_budgetperiod);
61 isnt( $bpid, undef, 'AddBugetPeriod does not returns undef' );
62 my $budgetperiod = GetBudgetPeriod($bpid);
63 is( $budgetperiod->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'AddBudgetPeriod stores the start date correctly' );
64 is( $budgetperiod->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'AddBudgetPeriod stores the end date correctly' );
65 is( $budgetperiod->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'AddBudgetPeriod stores the description correctly' );
66 is( $budgetperiod->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'AddBudgetPeriod stores active correctly' );
67 is( GetBudgetPeriod(0), undef ,'GetBudgetPeriod(0) returned undef : noactive BudgetPeriod' );
71 budget_period_startdate => '2009-01-01',
72 budget_period_enddate => '2009-12-31',
73 budget_period_description => 'MODIF_MAPERI',
74 budget_period_active => 1,
76 my $mod_status = ModBudgetPeriod($my_budgetperiod);
77 is( $mod_status, undef, 'ModBudgetPeriod without id returns undef' );
79 $my_budgetperiod->{budget_period_id} = $bpid;
80 $mod_status = ModBudgetPeriod($my_budgetperiod);
81 is( $mod_status, 1, 'ModBudgetPeriod returnis true' );
82 $budgetperiod = GetBudgetPeriod($bpid);
83 is( $budgetperiod->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'ModBudgetPeriod updates the start date correctly' );
84 is( $budgetperiod->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'ModBudgetPeriod updates the end date correctly' );
85 is( $budgetperiod->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'ModBudgetPeriod updates the description correctly' );
86 is( $budgetperiod->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'ModBudgetPeriod upates active correctly' );
87 isnt( GetBudgetPeriod(0), undef, 'GetBugetPeriods functions correctly' );
90 $budgetperiods = GetBudgetPeriods();
91 is( @$budgetperiods, 1, 'GetBudgetPeriods returns the correct number of budget periods' );
92 is( $budgetperiods->[0]->{budget_period_id}, $my_budgetperiod->{budget_period_id}, 'GetBudgetPeriods returns the id correctly' );
93 is( $budgetperiods->[0]->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'GetBudgetPeriods returns the start date correctly' );
94 is( $budgetperiods->[0]->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'GetBudgetPeriods returns the end date correctly' );
95 is( $budgetperiods->[0]->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'GetBudgetPeriods returns the description correctly' );
96 is( $budgetperiods->[0]->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'GetBudgetPeriods returns active correctly' );
98 is( DelBudgetPeriod($bpid), 1, 'DelBudgetPeriod returns true' );
99 $budgetperiods = GetBudgetPeriods();
100 is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
107 # The budget hierarchy will be:
115 is( AddBudget(), undef, 'AddBuget without argument returns undef' );
116 my $budgets = GetBudgets();
117 is( @$budgets, 0, 'GetBudgets returns the correct number of budgets' );
119 $bpid = AddBudgetPeriod($my_budgetperiod);
121 budget_code => 'ABCD',
122 budget_amount => '123.132000',
123 budget_name => 'Periodiques',
124 budget_notes => 'This is a note',
125 budget_period_id => $bpid,
127 my $budget_id = AddBudget($my_budget);
128 isnt( $budget_id, undef, 'AddBudget does not returns undef' );
129 my $budget = GetBudget($budget_id);
130 is( $budget->{budget_code}, $my_budget->{budget_code}, 'AddBudget stores the budget code correctly' );
131 is( $budget->{budget_amount}, $my_budget->{budget_amount}, 'AddBudget stores the budget amount correctly' );
132 is( $budget->{budget_name}, $my_budget->{budget_name}, 'AddBudget stores the budget name correctly' );
133 is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'AddBudget stores the budget notes correctly' );
134 is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'AddBudget stores the budget period id correctly' );
138 budget_code => 'EFG',
139 budget_amount => '321.231000',
140 budget_name => 'Modified name',
141 budget_notes => 'This is a modified note',
142 budget_period_id => $bpid,
144 $mod_status = ModBudget($my_budget);
145 is( $mod_status, undef, 'ModBudget without id returns undef' );
147 $my_budget->{budget_id} = $budget_id;
148 $mod_status = ModBudget($my_budget);
149 is( $mod_status, 1, 'ModBudget returns true' );
150 $budget = GetBudget($budget_id);
151 is( $budget->{budget_code}, $my_budget->{budget_code}, 'ModBudget updates the budget code correctly' );
152 is( $budget->{budget_amount}, $my_budget->{budget_amount}, 'ModBudget updates the budget amount correctly' );
153 is( $budget->{budget_name}, $my_budget->{budget_name}, 'ModBudget updates the budget name correctly' );
154 is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'ModBudget updates the budget notes correctly' );
155 is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'ModBudget updates the budget period id correctly' );
158 $budgets = GetBudgets();
159 is( @$budgets, 1, 'GetBudgets returns the correct number of budgets' );
160 is( $budgets->[0]->{budget_id}, $my_budget->{budget_id}, 'GetBudgets returns the budget id correctly' );
161 is( $budgets->[0]->{budget_code}, $my_budget->{budget_code}, 'GetBudgets returns the budget code correctly' );
162 is( $budgets->[0]->{budget_amount}, $my_budget->{budget_amount}, 'GetBudgets returns the budget amount correctly' );
163 is( $budgets->[0]->{budget_name}, $my_budget->{budget_name}, 'GetBudgets returns the budget name correctly' );
164 is( $budgets->[0]->{budget_notes}, $my_budget->{budget_notes}, 'GetBudgets returns the budget notes correctly' );
165 is( $budgets->[0]->{budget_period_id}, $my_budget->{budget_period_id}, 'GetBudgets returns the budget period id correctly' );
167 $budgets = GetBudgets( {budget_period_id => $bpid} );
168 is( @$budgets, 1, 'GetBudgets With Filter OK' );
169 $budgets = GetBudgets( {budget_period_id => $bpid}, {-asc => "budget_name"} );
170 is( @$budgets, 1, 'GetBudgets With Order OK' );
171 $budgets = GetBudgets( {budget_period_id => GetBudgetPeriod($bpid)->{budget_period_id}}, {-asc => "budget_name"} );
172 is( @$budgets, 1, 'GetBudgets With Order Getting Active budgetPeriod OK');
175 my $budget_name = GetBudgetName( $budget_id );
176 is($budget_name, $my_budget->{budget_name}, "Test the GetBudgetName routine");
178 my $budget_code = $my_budget->{budget_code};
179 my $budget_by_code = GetBudgetByCode( $budget_code );
180 is($budget_by_code->{budget_id}, $budget_id, "GetBudgetByCode, check id");
181 is($budget_by_code->{budget_notes}, $my_budget->{budget_notes}, "GetBudgetByCode, check notes");
183 my $second_budget_id = AddBudget({
184 budget_code => "ZZZZ",
185 budget_amount => "500.00",
186 budget_name => "Art",
187 budget_notes => "This is a note",
188 budget_period_id => $bpid,
190 isnt( $second_budget_id, undef, 'AddBudget does not returns undef' );
192 $budgets = GetBudgets( {budget_period_id => $bpid} );
193 ok( $budgets->[0]->{budget_name} lt $budgets->[1]->{budget_name}, 'default sort order for GetBudgets is by name' );
195 is( DelBudget($budget_id), 1, 'DelBudget returns true' );
196 $budgets = GetBudgets();
197 is( @$budgets, 1, 'GetBudgets returns the correct number of budget periods' );
200 # GetBudgetHierarchySpent and GetBudgetHierarchyOrdered
201 my $budget_period_total = 10_000;
202 my $budget_1_total = 1_000;
203 my $budget_11_total = 100;
204 my $budget_111_total = 50;
205 my $budget_12_total = 100;
206 my $budget_2_total = 2_000;
208 my $budget_period_id = AddBudgetPeriod(
210 budget_period_startdate => '2013-01-01',
211 budget_period_enddate => '2014-12-31',
212 budget_period_description => 'Budget Period',
213 budget_period_active => 1,
214 budget_period_total => $budget_period_total,
217 my $budget_id1 = AddBudget(
219 budget_code => 'budget_1',
220 budget_name => 'budget_1',
221 budget_period_id => $budget_period_id,
222 budget_parent_id => undef,
223 budget_amount => $budget_1_total,
226 my $budget_id2 = AddBudget(
228 budget_code => 'budget_2',
229 budget_name => 'budget_2',
230 budget_period_id => $budget_period_id,
231 budget_parent_id => undef,
232 budget_amount => $budget_2_total,
235 my $budget_id12 = AddBudget(
237 budget_code => 'budget_12',
238 budget_name => 'budget_12',
239 budget_period_id => $budget_period_id,
240 budget_parent_id => $budget_id1,
241 budget_amount => $budget_12_total,
244 my $budget_id11 = AddBudget(
246 budget_code => 'budget_11',
247 budget_name => 'budget_11',
248 budget_period_id => $budget_period_id,
249 budget_parent_id => $budget_id1,
250 budget_amount => $budget_11_total,
253 my $budget_id111 = AddBudget(
255 budget_code => 'budget_111',
256 budget_name => 'budget_111',
257 budget_period_id => $budget_period_id,
258 budget_parent_id => $budget_id11,
259 budget_owner_id => 1,
260 budget_amount => $budget_111_total,
263 my $budget_id21 = AddBudget(
265 budget_code => 'budget_21',
266 budget_name => 'budget_21',
267 budget_period_id => $budget_period_id,
268 budget_parent_id => $budget_id2,
272 my $booksellerid = C4::Bookseller::AddBookseller(
275 address1 => "bookseller's address",
282 my $basketno = C4::Acquisition::NewBasket( $booksellerid, 1 );
283 my ( $biblionumber, $biblioitemnumber ) =
284 C4::Biblio::AddBiblio( MARC::Record->new, '' );
288 budget_id => $budget_id1,
289 pending_quantity => 1,
293 budget_id => $budget_id2,
294 pending_quantity => 2,
298 budget_id => $budget_id11,
299 pending_quantity => 3,
303 budget_id => $budget_id12,
304 pending_quantity => 4,
308 budget_id => $budget_id111,
309 pending_quantity => 2,
313 # No order for budget_21
318 my $invoiceid = AddInvoice(invoicenumber => 'invoice_test_clone', booksellerid => $booksellerid, unknown => "unknown");
320 my $item_quantity = 2;
321 my $number_of_orders_to_move = 0;
322 for my $infos (@order_infos) {
323 for ( 1 .. $infos->{pending_quantity} ) {
324 my $order = Koha::Acquisition::Order->new(
326 basketno => $basketno,
327 biblionumber => $biblionumber,
328 budget_id => $infos->{budget_id},
329 order_internalnote => "internal note",
330 order_vendornote => "vendor note",
334 listprice => $item_price,
335 ecost => $item_price,
342 my $ordernumber = $order->{ordernumber};
343 push @{ $budgets{$infos->{budget_id}} }, $ordernumber;
344 $number_of_orders_to_move++;
346 for ( 1 .. $infos->{spent_quantity} ) {
347 my $order = Koha::Acquisition::Order->new(
349 basketno => $basketno,
350 biblionumber => $biblionumber,
351 budget_id => $infos->{budget_id},
352 order_internalnote => "internal note",
353 order_vendornote => "vendor note",
354 quantity => $item_quantity,
357 listprice => $item_price,
358 ecost => $item_price,
365 my $ordernumber = $order->{ordernumber};
367 biblionumber => $biblionumber,
368 ordernumber => $ordernumber,
369 budget_id => $infos->{budget_id},
370 quantityreceived => $item_quantity,
372 ecost => $item_price,
373 invoiceid => $invoiceid,
375 received_items => [],
379 is( GetBudgetHierarchySpent( $budget_id1 ), 160, "total spent for budget1 is 160" );
380 is( GetBudgetHierarchySpent( $budget_id11 ), 100, "total spent for budget11 is 100" );
381 is( GetBudgetHierarchySpent( $budget_id111 ), 20, "total spent for budget111 is 20" );
384 my $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
386 budget_period_id => $budget_period_id,
387 budget_period_startdate => '2014-01-01',
388 budget_period_enddate => '2014-12-31',
389 budget_period_description => 'Budget Period Cloned',
393 my $budget_period_cloned = C4::Budgets::GetBudgetPeriod($budget_period_id_cloned);
394 is($budget_period_cloned->{budget_period_description}, 'Budget Period Cloned', 'Cloned budget\'s description is updated.');
396 my $budget_hierarchy = GetBudgetHierarchy($budget_period_id);
397 my $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
400 scalar(@$budget_hierarchy_cloned),
401 scalar(@$budget_hierarchy),
402 'CloneBudgetPeriod clones the same number of budgets (funds)'
405 _get_dependencies($budget_hierarchy),
406 _get_dependencies($budget_hierarchy_cloned),
407 'CloneBudgetPeriod keeps the same dependencies order'
410 # CloneBudgetPeriod with param mark_original_budget_as_inactive
411 my $budget_period = C4::Budgets::GetBudgetPeriod($budget_period_id);
412 is( $budget_period->{budget_period_active}, 1,
413 'CloneBudgetPeriod does not mark as inactive the budgetperiod if not needed'
416 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
417 my $number_of_budgets_not_reset = 0;
418 for my $budget (@$budget_hierarchy_cloned) {
419 $number_of_budgets_not_reset++ if $budget->{budget_amount} > 0;
421 is( $number_of_budgets_not_reset, 5,
422 'CloneBudgetPeriod does not reset budgets (funds) if not needed' );
424 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
426 budget_period_id => $budget_period_id,
427 budget_period_startdate => '2014-01-01',
428 budget_period_enddate => '2014-12-31',
429 mark_original_budget_as_inactive => 1,
433 $budget_hierarchy = GetBudgetHierarchy($budget_period_id);
434 is( $budget_hierarchy->[0]->{children}->[0]->{budget_name}, 'budget_11', 'GetBudgetHierarchy should return budgets ordered by name, first child is budget_11' );
435 is( $budget_hierarchy->[0]->{children}->[1]->{budget_name}, 'budget_12', 'GetBudgetHierarchy should return budgets ordered by name, second child is budget_12' );
437 $budget_hierarchy = GetBudgetHierarchy($budget_period_id);
438 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
440 is( scalar(@$budget_hierarchy_cloned), scalar(@$budget_hierarchy),
441 'CloneBudgetPeriod (with inactive param) clones the same number of budgets (funds)'
444 _get_dependencies($budget_hierarchy),
445 _get_dependencies($budget_hierarchy_cloned),
446 'CloneBudgetPeriod (with inactive param) keeps the same dependencies order'
448 $budget_period = C4::Budgets::GetBudgetPeriod($budget_period_id);
449 is( $budget_period->{budget_period_active}, 0,
450 'CloneBudgetPeriod (with inactive param) marks as inactive the budgetperiod'
453 # CloneBudgetPeriod with param reset_all_budgets
454 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
456 budget_period_id => $budget_period_id,
457 budget_period_startdate => '2014-01-01',
458 budget_period_enddate => '2014-12-31',
459 reset_all_budgets => 1,
463 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
464 $number_of_budgets_not_reset = 0;
465 for my $budget (@$budget_hierarchy_cloned) {
466 $number_of_budgets_not_reset++ if $budget->{budget_amount} > 0;
468 is( $number_of_budgets_not_reset, 0,
469 'CloneBudgetPeriod has reset all budgets (funds)' );
473 my $number_orders_moved = C4::Budgets::MoveOrders();
474 is( $number_orders_moved, undef, 'MoveOrders return undef if no arg passed' );
475 $number_orders_moved =
476 C4::Budgets::MoveOrders( { from_budget_period_id => $budget_period_id } );
477 is( $number_orders_moved, undef,
478 'MoveOrders return undef if only 1 arg passed' );
479 $number_orders_moved =
480 C4::Budgets::MoveOrders( { to_budget_period_id => $budget_period_id } );
481 is( $number_orders_moved, undef,
482 'MoveOrders return undef if only 1 arg passed' );
483 $number_orders_moved = C4::Budgets::MoveOrders(
485 from_budget_period_id => $budget_period_id,
486 to_budget_period_id => $budget_period_id
489 is( $number_orders_moved, undef,
490 'MoveOrders return undef if 2 budget period id are the same' );
492 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
494 budget_period_id => $budget_period_id,
495 budget_period_startdate => '2014-01-01',
496 budget_period_enddate => '2014-12-31',
500 my $report = C4::Budgets::MoveOrders(
502 from_budget_period_id => $budget_period_id,
503 to_budget_period_id => $budget_period_id_cloned,
504 move_remaining_unspent => 1,
507 is( scalar( @$report ), 6 , "MoveOrders has processed 6 funds" );
509 my $number_of_orders_moved = 0;
510 $number_of_orders_moved += scalar( @{ $_->{orders_moved} } ) for @$report;
511 is( $number_of_orders_moved, $number_of_orders_to_move, "MoveOrders has moved $number_of_orders_to_move orders" );
513 my @new_budget_ids = map { $_->{budget_id} }
514 @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
515 my @old_budget_ids = map { $_->{budget_id} }
516 @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
517 for my $budget_id ( keys %budgets ) {
518 for my $ordernumber ( @{ $budgets{$budget_id} } ) {
519 my $budget = GetBudgetByOrderNumber($ordernumber);
520 my $is_in_new_budgets = grep /^$budget->{budget_id}$/, @new_budget_ids;
521 my $is_in_old_budgets = grep /^$budget->{budget_id}$/, @old_budget_ids;
522 is( $is_in_new_budgets, 1, "MoveOrders changed the budget_id for order $ordernumber" );
523 is( $is_in_old_budgets, 0, "MoveOrders changed the budget_id for order $ordernumber" );
528 # MoveOrders with param move_remaining_unspent
529 my @new_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
530 my @old_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
532 for my $new_budget ( @new_budgets ) {
533 my ( $old_budget ) = map { $_->{budget_code} eq $new_budget->{budget_code} ? $_ : () } @old_budgets;
534 my $new_budget_amount_should_be = $old_budget->{budget_amount} * 2 - $old_budget->{total_spent};
535 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})" );
538 # Test SetOwnerToFundHierarchy
540 my $categorycode = 'S';
541 my $branchcode = $library->{branchcode};
542 my $john_doe = C4::Members::AddMember(
543 cardnumber => '123456',
546 categorycode => $categorycode,
547 branchcode => $branchcode,
549 dateexpiry => '9999-12-31',
553 C4::Budgets::SetOwnerToFundHierarchy( $budget_id1, $john_doe );
554 is( C4::Budgets::GetBudget($budget_id1)->{budget_owner_id},
555 $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 1 ($budget_id1)" );
556 is( C4::Budgets::GetBudget($budget_id11)->{budget_owner_id},
557 $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 11 ($budget_id11)" );
558 is( C4::Budgets::GetBudget($budget_id111)->{budget_owner_id},
559 $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 111 ($budget_id111)" );
560 is( C4::Budgets::GetBudget($budget_id12)->{budget_owner_id},
561 $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 12 ($budget_id12 )" );
562 is( C4::Budgets::GetBudget($budget_id2)->{budget_owner_id},
563 undef, "SetOwnerToFundHierarchy should not have set an owner for budget 2 ($budget_id2)" );
564 is( C4::Budgets::GetBudget($budget_id21)->{budget_owner_id},
565 undef, "SetOwnerToFundHierarchy should not have set an owner for budget 21 ($budget_id21)" );
567 my $jane_doe = C4::Members::AddMember(
568 cardnumber => '789012',
571 categorycode => $categorycode,
572 branchcode => $branchcode,
574 dateexpiry => '9999-12-31',
578 C4::Budgets::SetOwnerToFundHierarchy( $budget_id11, $jane_doe );
579 is( C4::Budgets::GetBudget($budget_id1)->{budget_owner_id},
580 $john_doe, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 1 ($budget_id1)" );
581 is( C4::Budgets::GetBudget($budget_id11)->{budget_owner_id},
582 $jane_doe, "SetOwnerToFundHierarchy should have set John Doe $jane_doe for budget 11 ($budget_id11)" );
583 is( C4::Budgets::GetBudget($budget_id111)->{budget_owner_id},
584 $jane_doe, "SetOwnerToFundHierarchy should have set John Doe $jane_doe for budget 111 ($budget_id111)" );
585 is( C4::Budgets::GetBudget($budget_id12)->{budget_owner_id},
586 $john_doe, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 12 ($budget_id12 )" );
587 is( C4::Budgets::GetBudget($budget_id2)->{budget_owner_id},
588 undef, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 2 ($budget_id2)" );
589 is( C4::Budgets::GetBudget($budget_id21)->{budget_owner_id},
590 undef, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 21 ($budget_id21)" );
592 # Test GetBudgetAuthCats
594 my $budgetPeriodId = 1;
596 my $bdgts = GetBudgets();
598 for my $budget ( @{$bdgts} )
600 $budget->{sort1_authcat} = "sort1_authcat_$i";
601 $budget->{sort2_authcat} = "sort2_authcat_$i";
602 $budget->{budget_period_id} = $budgetPeriodId;
603 ModBudget( $budget );
607 my $authCat = GetBudgetAuthCats($budgetPeriodId);
609 is( scalar @{$authCat}, $i * 2, "GetBudgetAuthCats returns only non-empty sorting categories (no empty authCat in db)" );
612 for my $budget ( @{$bdgts} )
614 $budget->{sort1_authcat} = "sort1_authcat_$i";
615 $budget->{sort2_authcat} = "";
616 $budget->{budget_period_id} = $budgetPeriodId;
617 ModBudget( $budget );
621 $authCat = GetBudgetAuthCats($budgetPeriodId);
623 is( scalar @{$authCat}, $i, "GetBudgetAuthCats returns only non-empty sorting categories (empty sort2_authcat on all records)" );
626 for my $budget ( @{$bdgts} )
628 $budget->{sort1_authcat} = "";
629 $budget->{sort2_authcat} = "";
630 $budget->{budget_period_id} = $budgetPeriodId;
631 ModBudget( $budget );
635 $authCat = GetBudgetAuthCats($budgetPeriodId);
637 is( scalar @{$authCat}, 0, "GetBudgetAuthCats returns only non-empty sorting categories (all empty)" );
639 # /Test GetBudgetAuthCats
641 sub _get_dependencies {
642 my ($budget_hierarchy) = @_;
644 for my $budget (@$budget_hierarchy) {
645 if ( $budget->{child} ) {
646 my @sorted = sort @{ $budget->{child} };
647 for my $child_id (@sorted) {
648 push @{ $graph->{ $budget->{budget_name} }{children} },
649 _get_budgetname_by_id( $budget_hierarchy, $child_id );
652 push @{ $graph->{ $budget->{budget_name} }{parents} },
653 $budget->{parent_id};
658 sub _get_budgetname_by_id {
659 my ( $budgets, $budget_id ) = @_;
661 map { ( $_->{budget_id} eq $budget_id ) ? $_->{budget_name} : () }
666 # C4::Context->userenv