Bug 15062: Regression tests
[koha.git] / t / db_dependent / Budgets.t
1 use Modern::Perl;
2 use Test::More tests => 122;
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::Members qw( AddMember );
12
13 use Koha::Acquisition::Order;
14
15 use t::lib::TestBuilder;
16
17 use YAML;
18
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|);
25
26 my $library = $builder->build({
27     source => 'Branch',
28 });
29
30 # Mock userenv
31 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
32 my $userenv;
33 *C4::Context::userenv = \&Mock_userenv;
34 $userenv = { flags => 1, id => 'my_userid', branch => $library->{branchcode} };
35
36 #
37 # Budget Periods :
38 #
39
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',
44 });
45 is( $bpid, undef, 'AddBugetPeriod without end date returns undef' );
46 $bpid = AddBudgetPeriod({
47     budget_period_enddate => '2008-12-31',
48 });
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' );
53
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,
59 };
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' );
68
69
70 $my_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,
75 };
76 my $mod_status = ModBudgetPeriod($my_budgetperiod);
77 is( $mod_status, undef, 'ModBudgetPeriod without id returns undef' );
78
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' );
88
89
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' );
97
98 is( DelBudgetPeriod($bpid), 1, 'DelBudgetPeriod returns true' );
99 $budgetperiods = GetBudgetPeriods();
100 is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
101
102
103 #
104 # Budget  :
105 #
106
107 # The budget hierarchy will be:
108 # budget_1
109 #   budget_11
110 #     budget_111
111 #   budget_12
112 # budget_2
113 #   budget_21
114
115 is( AddBudget(), undef, 'AddBuget without argument returns undef' );
116 my $budgets = GetBudgets();
117 is( @$budgets, 0, 'GetBudgets returns the correct number of budgets' );
118
119 $bpid = AddBudgetPeriod($my_budgetperiod);
120 my $my_budget = {
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,
126 };
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' );
135
136
137 $my_budget = {
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,
143 };
144 $mod_status = ModBudget($my_budget);
145 is( $mod_status, undef, 'ModBudget without id returns undef' );
146
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' );
156
157
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' );
166
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');
173
174
175 my $budget_name = GetBudgetName( $budget_id );
176 is($budget_name, $my_budget->{budget_name}, "Test the GetBudgetName routine");
177
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");
182
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,
189 });
190 isnt( $second_budget_id, undef, 'AddBudget does not returns undef' );
191
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' );
194
195 is( DelBudget($budget_id), 1, 'DelBudget returns true' );
196 $budgets = GetBudgets();
197 is( @$budgets, 1, 'GetBudgets returns the correct number of budget periods' );
198
199
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;
207
208 my $budget_period_id = AddBudgetPeriod(
209     {
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,
215     }
216 );
217 my $budget_id1 = AddBudget(
218     {
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,
224     }
225 );
226 my $budget_id2 = AddBudget(
227     {
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,
233     }
234 );
235 my $budget_id12 = AddBudget(
236     {
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,
242     }
243 );
244 my $budget_id11 = AddBudget(
245     {
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,
251     }
252 );
253 my $budget_id111 = AddBudget(
254     {
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,
261     }
262 );
263 my $budget_id21 = AddBudget(
264     {
265         budget_code      => 'budget_21',
266         budget_name      => 'budget_21',
267         budget_period_id => $budget_period_id,
268         budget_parent_id => $budget_id2,
269     }
270 );
271
272 my $booksellerid = C4::Bookseller::AddBookseller(
273     {
274         name         => "my vendor",
275         address1     => "bookseller's address",
276         phone        => "0123456",
277         active       => 1,
278         deliverytime => 5,
279     }
280 );
281
282 my $basketno = C4::Acquisition::NewBasket( $booksellerid, 1 );
283 my ( $biblionumber, $biblioitemnumber ) =
284   C4::Biblio::AddBiblio( MARC::Record->new, '' );
285
286 my @order_infos = (
287     {
288         budget_id => $budget_id1,
289         pending_quantity  => 1,
290         spent_quantity  => 0,
291     },
292     {
293         budget_id => $budget_id2,
294         pending_quantity  => 2,
295         spent_quantity  => 1,
296     },
297     {
298         budget_id => $budget_id11,
299         pending_quantity  => 3,
300         spent_quantity  => 4,
301     },
302     {
303         budget_id => $budget_id12,
304         pending_quantity  => 4,
305         spent_quantity  => 3,
306     },
307     {
308         budget_id => $budget_id111,
309         pending_quantity  => 2,
310         spent_quantity  => 1,
311     },
312
313     # No order for budget_21
314
315 );
316
317 my %budgets;
318 my $invoiceid = AddInvoice(invoicenumber => 'invoice_test_clone', booksellerid => $booksellerid, unknown => "unknown");
319 my $item_price = 10;
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(
325             {
326                 basketno           => $basketno,
327                 biblionumber       => $biblionumber,
328                 budget_id          => $infos->{budget_id},
329                 order_internalnote => "internal note",
330                 order_vendornote   => "vendor note",
331                 quantity           => 2,
332                 cost               => $item_price,
333                 rrp                => $item_price,
334                 listprice          => $item_price,
335                 ecost              => $item_price,
336                 rrp                => $item_price,
337                 discount           => 0,
338                 uncertainprice     => 0,
339                 gstrate            => 0,
340             }
341         )->insert;
342         my $ordernumber = $order->{ordernumber};
343         push @{ $budgets{$infos->{budget_id}} }, $ordernumber;
344         $number_of_orders_to_move++;
345     }
346     for ( 1 .. $infos->{spent_quantity} ) {
347         my $order = Koha::Acquisition::Order->new(
348             {
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,
355                 cost               => $item_price,
356                 rrp                => $item_price,
357                 listprice          => $item_price,
358                 ecost              => $item_price,
359                 rrp                => $item_price,
360                 discount           => 0,
361                 uncertainprice     => 0,
362                 gstrate            => 0,
363             }
364         )->insert;
365         my $ordernumber = $order->{ordernumber};
366         ModReceiveOrder({
367               biblionumber     => $biblionumber,
368               ordernumber      => $ordernumber,
369               budget_id        => $infos->{budget_id},
370               quantityreceived => $item_quantity,
371               cost             => $item_price,
372               ecost            => $item_price,
373               invoiceid        => $invoiceid,
374               rrp              => $item_price,
375               received_items   => [],
376         } );
377     }
378 }
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" );
382
383 # CloneBudgetPeriod
384 my $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
385     {
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',
390     }
391 );
392
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.');
395
396 my $budget_hierarchy        = GetBudgetHierarchy($budget_period_id);
397 my $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
398
399 is(
400     scalar(@$budget_hierarchy_cloned),
401     scalar(@$budget_hierarchy),
402     'CloneBudgetPeriod clones the same number of budgets (funds)'
403 );
404 is_deeply(
405     _get_dependencies($budget_hierarchy),
406     _get_dependencies($budget_hierarchy_cloned),
407     'CloneBudgetPeriod keeps the same dependencies order'
408 );
409
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'
414 );
415
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;
420 }
421 is( $number_of_budgets_not_reset, 5,
422     'CloneBudgetPeriod does not reset budgets (funds) if not needed' );
423
424 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
425     {
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,
430     }
431 );
432
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' );
436
437 $budget_hierarchy        = GetBudgetHierarchy($budget_period_id);
438 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
439
440 is( scalar(@$budget_hierarchy_cloned), scalar(@$budget_hierarchy),
441 'CloneBudgetPeriod (with inactive param) clones the same number of budgets (funds)'
442 );
443 is_deeply(
444     _get_dependencies($budget_hierarchy),
445     _get_dependencies($budget_hierarchy_cloned),
446     'CloneBudgetPeriod (with inactive param) keeps the same dependencies order'
447 );
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'
451 );
452
453 # CloneBudgetPeriod with param reset_all_budgets
454 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
455     {
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,
460     }
461 );
462
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;
467 }
468 is( $number_of_budgets_not_reset, 0,
469     'CloneBudgetPeriod has reset all budgets (funds)' );
470
471
472 # MoveOrders
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(
484     {
485         from_budget_period_id => $budget_period_id,
486         to_budget_period_id   => $budget_period_id
487     }
488 );
489 is( $number_orders_moved, undef,
490     'MoveOrders return undef if 2 budget period id are the same' );
491
492 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
493     {
494         budget_period_id        => $budget_period_id,
495         budget_period_startdate => '2014-01-01',
496         budget_period_enddate   => '2014-12-31',
497     }
498 );
499
500 my $report = C4::Budgets::MoveOrders(
501     {
502         from_budget_period_id  => $budget_period_id,
503         to_budget_period_id    => $budget_period_id_cloned,
504         move_remaining_unspent => 1,
505     }
506 );
507 is( scalar( @$report ), 6 , "MoveOrders has processed 6 funds" );
508
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" );
512
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" );
524     }
525 }
526
527
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) };
531
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})" );
536 }
537
538 # Test SetOwnerToFundHierarchy
539
540 my $categorycode = 'S';
541 my $branchcode = $library->{branchcode};
542 my $john_doe = C4::Members::AddMember(
543     cardnumber   => '123456',
544     firstname    => 'John',
545     surname      => 'Doe',
546     categorycode => $categorycode,
547     branchcode   => $branchcode,
548     dateofbirth  => '',
549     dateexpiry   => '9999-12-31',
550     userid       => 'john.doe'
551 );
552
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)" );
566
567 my $jane_doe = C4::Members::AddMember(
568     cardnumber   => '789012',
569     firstname    => 'Jane',
570     surname      => 'Doe',
571     categorycode => $categorycode,
572     branchcode   => $branchcode,
573     dateofbirth  => '',
574     dateexpiry   => '9999-12-31',
575     userid       => 'jane.doe'
576 );
577
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)" );
591
592 sub _get_dependencies {
593     my ($budget_hierarchy) = @_;
594     my $graph;
595     for my $budget (@$budget_hierarchy) {
596         if ( $budget->{child} ) {
597             my @sorted = sort @{ $budget->{child} };
598             for my $child_id (@sorted) {
599                 push @{ $graph->{ $budget->{budget_name} }{children} },
600                   _get_budgetname_by_id( $budget_hierarchy, $child_id );
601             }
602         }
603         push @{ $graph->{ $budget->{budget_name} }{parents} },
604           $budget->{parent_id};
605     }
606     return $graph;
607 }
608
609 sub _get_budgetname_by_id {
610     my ( $budgets, $budget_id ) = @_;
611     my ($budget_name) =
612       map { ( $_->{budget_id} eq $budget_id ) ? $_->{budget_name} : () }
613       @$budgets;
614     return $budget_name;
615 }
616
617 # C4::Context->userenv
618 sub Mock_userenv {
619     return $userenv;
620 }