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