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