Bug 18736: Unit tests for rounding
[koha.git] / t / db_dependent / Budgets.t
1 #!/usr/bin/perl
2 use Modern::Perl;
3 use Test::More tests => 146;
4
5 BEGIN {
6     use_ok('C4::Budgets')
7 }
8 use C4::Context;
9 use C4::Biblio;
10 use C4::Acquisition;
11
12 use Koha::Acquisition::Booksellers;
13 use Koha::Acquisition::Orders;
14 use Koha::Acquisition::Funds;
15 use Koha::Patrons;
16 use Koha::Number::Price;
17
18 use t::lib::TestBuilder;
19 use t::lib::Mocks;
20 use Koha::DateUtils;
21
22 use YAML;
23
24 use t::lib::Mocks;
25 t::lib::Mocks::mock_preference('OrderPriceRounding','');
26
27 my $schema  = Koha::Database->new->schema;
28 $schema->storage->txn_begin;
29 my $builder = t::lib::TestBuilder->new;
30 my $dbh = C4::Context->dbh;
31 $dbh->do(q|DELETE FROM aqbudgetperiods|);
32 $dbh->do(q|DELETE FROM aqbudgets|);
33
34 my $library = $builder->build({
35     source => 'Branch',
36 });
37
38 t::lib::Mocks::mock_userenv(
39     {
40         flags => 1,
41         userid => 'my_userid',
42         branch => $library->{branchcode},
43     }
44 );
45
46 #
47 # Budget Periods :
48 #
49
50 is( AddBudgetPeriod(), undef, 'AddBugetPeriod without argument returns undef' );
51 is( AddBudgetPeriod( { }  ), undef, 'AddBugetPeriod with an empty argument returns undef' );
52 my $bpid = AddBudgetPeriod({
53     budget_period_startdate => '2008-01-01',
54 });
55 is( $bpid, undef, 'AddBugetPeriod without end date returns undef' );
56 $bpid = AddBudgetPeriod({
57     budget_period_enddate => '2008-12-31',
58 });
59 is( $bpid, undef, 'AddBugetPeriod without start date returns undef' );
60 is( GetBudgetPeriod(0), undef ,'GetBudgetPeriod(0) returned undef : noactive BudgetPeriod' );
61 my $budgetperiods = GetBudgetPeriods();
62 is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
63
64 my $my_budgetperiod = {
65     budget_period_startdate   => '2008-01-01',
66     budget_period_enddate     => '2008-12-31',
67     budget_period_description => 'MAPERI',
68     budget_period_active      => 0,
69     budget_period_id          => '', # Bug 21604
70 };
71 $bpid = AddBudgetPeriod($my_budgetperiod);
72 isnt( $bpid, undef, 'AddBugetPeriod does not returns undef' );
73 my $budgetperiod = GetBudgetPeriod($bpid);
74 is( $budgetperiod->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'AddBudgetPeriod stores the start date correctly' );
75 is( $budgetperiod->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'AddBudgetPeriod stores the end date correctly' );
76 is( $budgetperiod->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'AddBudgetPeriod stores the description correctly' );
77 is( $budgetperiod->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'AddBudgetPeriod stores active correctly' );
78 is( GetBudgetPeriod(0), undef ,'GetBudgetPeriod(0) returned undef : noactive BudgetPeriod' );
79
80
81 $my_budgetperiod = {
82     budget_period_startdate   => '2009-01-01',
83     budget_period_enddate     => '2009-12-31',
84     budget_period_description => 'MODIF_MAPERI',
85     budget_period_active      => 1,
86 };
87 my $mod_status = ModBudgetPeriod($my_budgetperiod);
88 is( $mod_status, undef, 'ModBudgetPeriod without id returns undef' );
89
90 $my_budgetperiod->{budget_period_id} = $bpid;
91 $mod_status = ModBudgetPeriod($my_budgetperiod);
92 is( $mod_status, 1, 'ModBudgetPeriod returnis true' );
93 $budgetperiod = GetBudgetPeriod($bpid);
94 is( $budgetperiod->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'ModBudgetPeriod updates the start date correctly' );
95 is( $budgetperiod->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'ModBudgetPeriod updates the end date correctly' );
96 is( $budgetperiod->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'ModBudgetPeriod updates the description correctly' );
97 is( $budgetperiod->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'ModBudgetPeriod upates active correctly' );
98 isnt( GetBudgetPeriod(0), undef, 'GetBugetPeriods functions correctly' );
99
100
101 $budgetperiods = GetBudgetPeriods();
102 is( @$budgetperiods, 1, 'GetBudgetPeriods returns the correct number of budget periods' );
103 is( $budgetperiods->[0]->{budget_period_id}, $my_budgetperiod->{budget_period_id}, 'GetBudgetPeriods returns the id correctly' );
104 is( $budgetperiods->[0]->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'GetBudgetPeriods returns the start date correctly' );
105 is( $budgetperiods->[0]->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'GetBudgetPeriods returns the end date correctly' );
106 is( $budgetperiods->[0]->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'GetBudgetPeriods returns the description correctly' );
107 is( $budgetperiods->[0]->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'GetBudgetPeriods returns active correctly' );
108
109 is( DelBudgetPeriod($bpid), 1, 'DelBudgetPeriod returns true' );
110 $budgetperiods = GetBudgetPeriods();
111 is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
112
113
114 #
115 # Budget  :
116 #
117
118 # The budget hierarchy will be:
119 # budget_1
120 #   budget_11
121 #     budget_111
122 #   budget_12
123 # budget_2
124 #   budget_21
125
126 is( AddBudget(), undef, 'AddBuget without argument returns undef' );
127 my $budgets = GetBudgets();
128 is( @$budgets, 0, 'GetBudgets returns the correct number of budgets' );
129
130 $bpid = AddBudgetPeriod($my_budgetperiod); #this is an active budget
131
132 my $my_budget = {
133     budget_code      => 'ABCD',
134     budget_amount    => '123.132000',
135     budget_name      => 'Periodiques',
136     budget_notes     => 'This is a note',
137     budget_period_id => $bpid,
138     budget_encumb    => '', # Bug 21604
139 };
140 my $budget_id = AddBudget($my_budget);
141 isnt( $budget_id, undef, 'AddBudget does not returns undef' );
142 my $budget = GetBudget($budget_id);
143 is( $budget->{budget_code}, $my_budget->{budget_code}, 'AddBudget stores the budget code correctly' );
144 is( $budget->{budget_amount}, $my_budget->{budget_amount}, 'AddBudget stores the budget amount correctly' );
145 is( $budget->{budget_name}, $my_budget->{budget_name}, 'AddBudget stores the budget name correctly' );
146 is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'AddBudget stores the budget notes correctly' );
147 is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'AddBudget stores the budget period id correctly' );
148
149
150 $my_budget = {
151     budget_code      => 'EFG',
152     budget_amount    => '321.231000',
153     budget_name      => 'Modified name',
154     budget_notes     => 'This is a modified note',
155     budget_period_id => $bpid,
156 };
157 $mod_status = ModBudget($my_budget);
158 is( $mod_status, undef, 'ModBudget without id returns undef' );
159
160 $my_budget->{budget_id} = $budget_id;
161 $mod_status = ModBudget($my_budget);
162 is( $mod_status, 1, 'ModBudget returns true' );
163 $budget = GetBudget($budget_id);
164 is( $budget->{budget_code}, $my_budget->{budget_code}, 'ModBudget updates the budget code correctly' );
165 is( $budget->{budget_amount}, $my_budget->{budget_amount}, 'ModBudget updates the budget amount correctly' );
166 is( $budget->{budget_name}, $my_budget->{budget_name}, 'ModBudget updates the budget name correctly' );
167 is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'ModBudget updates the budget notes correctly' );
168 is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'ModBudget updates the budget period id correctly' );
169
170
171 $budgets = GetBudgets();
172 is( @$budgets, 1, 'GetBudgets returns the correct number of budgets' );
173 is( $budgets->[0]->{budget_id}, $my_budget->{budget_id}, 'GetBudgets returns the budget id correctly' );
174 is( $budgets->[0]->{budget_code}, $my_budget->{budget_code}, 'GetBudgets returns the budget code correctly' );
175 is( $budgets->[0]->{budget_amount}, $my_budget->{budget_amount}, 'GetBudgets returns the budget amount correctly' );
176 is( $budgets->[0]->{budget_name}, $my_budget->{budget_name}, 'GetBudgets returns the budget name correctly' );
177 is( $budgets->[0]->{budget_notes}, $my_budget->{budget_notes}, 'GetBudgets returns the budget notes correctly' );
178 is( $budgets->[0]->{budget_period_id}, $my_budget->{budget_period_id}, 'GetBudgets returns the budget period id correctly' );
179
180 $budgets = GetBudgets( {budget_period_id => $bpid} );
181 is( @$budgets, 1, 'GetBudgets With Filter OK' );
182 $budgets = GetBudgets( {budget_period_id => $bpid}, {-asc => "budget_name"} );
183 is( @$budgets, 1, 'GetBudgets With Order OK' );
184 $budgets = GetBudgets( {budget_period_id => GetBudgetPeriod($bpid)->{budget_period_id}}, {-asc => "budget_name"} );
185 is( @$budgets, 1, 'GetBudgets With Order Getting Active budgetPeriod OK');
186
187
188 my $budget_name = GetBudgetName( $budget_id );
189 is($budget_name, $my_budget->{budget_name}, "Test the GetBudgetName routine");
190
191 my $my_inactive_budgetperiod = { #let's add an inactive
192     budget_period_startdate   => '2010-01-01',
193     budget_period_enddate     => '2010-12-31',
194     budget_period_description => 'MODIF_MAPERI',
195     budget_period_active      => 0,
196 };
197 my $bpid_i = AddBudgetPeriod($my_inactive_budgetperiod); #this is an inactive budget
198
199 my $my_budget_inactive = {
200     budget_code      => 'EFG',
201     budget_amount    => '123.132000',
202     budget_name      => 'Periodiques',
203     budget_notes     => 'This is a note',
204     budget_period_id => $bpid_i,
205 };
206 my $budget_id_inactive = AddBudget($my_budget_inactive);
207
208 my $budget_code = $my_budget->{budget_code};
209 my $budget_by_code = GetBudgetByCode( $budget_code );
210 is($budget_by_code->{budget_id}, $budget_id, "GetBudgetByCode, check id"); #this should match the active budget, not the inactive
211 is($budget_by_code->{budget_notes}, $my_budget->{budget_notes}, "GetBudgetByCode, check notes");
212
213 my $second_budget_id = AddBudget({
214     budget_code      => "ZZZZ",
215     budget_amount    => "500.00",
216     budget_name      => "Art",
217     budget_notes     => "This is a note",
218     budget_period_id => $bpid,
219 });
220 isnt( $second_budget_id, undef, 'AddBudget does not returns undef' );
221
222 $budgets = GetBudgets( {budget_period_id => $bpid} );
223 ok( $budgets->[0]->{budget_name} lt $budgets->[1]->{budget_name}, 'default sort order for GetBudgets is by name' );
224
225 is( DelBudget($budget_id), 1, 'DelBudget returns true' );
226 $budgets = GetBudgets();
227 is( @$budgets, 2, 'GetBudgets returns the correct number of budget periods' );
228
229
230 # GetBudgetHierarchySpent and GetBudgetHierarchyOrdered
231 my $budget_period_total = 10_000;
232 my $budget_1_total = 1_000;
233 my $budget_11_total = 100;
234 my $budget_111_total = 50;
235 my $budget_12_total = 100;
236 my $budget_2_total = 2_000;
237
238 my $budget_period_id = AddBudgetPeriod(
239     {
240         budget_period_startdate   => '2013-01-01',
241         budget_period_enddate     => '2014-12-31',
242         budget_period_description => 'Budget Period',
243         budget_period_active      => 1,
244         budget_period_total       => $budget_period_total,
245     }
246 );
247 my $budget_id1 = AddBudget(
248     {
249         budget_code      => 'budget_1',
250         budget_name      => 'budget_1',
251         budget_period_id => $budget_period_id,
252         budget_parent_id => undef,
253         budget_amount    => $budget_1_total,
254     }
255 );
256 my $budget_id2 = AddBudget(
257     {
258         budget_code      => 'budget_2',
259         budget_name      => 'budget_2',
260         budget_period_id => $budget_period_id,
261         budget_parent_id => undef,
262         budget_amount    => $budget_2_total,
263     }
264 );
265 my $budget_id12 = AddBudget(
266     {
267         budget_code      => 'budget_12',
268         budget_name      => 'budget_12',
269         budget_period_id => $budget_period_id,
270         budget_parent_id => $budget_id1,
271         budget_amount    => $budget_12_total,
272     }
273 );
274 my $budget_id11 = AddBudget(
275     {
276         budget_code      => 'budget_11',
277         budget_name      => 'budget_11',
278         budget_period_id => $budget_period_id,
279         budget_parent_id => $budget_id1,
280         budget_amount    => $budget_11_total,
281     }
282 );
283 my $budget_id111 = AddBudget(
284     {
285         budget_code      => 'budget_111',
286         budget_name      => 'budget_111',
287         budget_period_id => $budget_period_id,
288         budget_parent_id => $budget_id11,
289         budget_owner_id  => 1,
290         budget_amount    => $budget_111_total,
291     }
292 );
293 my $budget_id21 = AddBudget(
294     {
295         budget_code      => 'budget_21',
296         budget_name      => 'budget_21',
297         budget_period_id => $budget_period_id,
298         budget_parent_id => $budget_id2,
299     }
300 );
301
302 my $bookseller = Koha::Acquisition::Bookseller->new(
303     {
304         name         => "my vendor",
305         address1     => "bookseller's address",
306         phone        => "0123456",
307         active       => 1,
308         deliverytime => 5,
309     }
310 )->store;
311 my $booksellerid = $bookseller->id;
312
313 my $basketno = C4::Acquisition::NewBasket( $booksellerid, 1 );
314 my ( $biblionumber, $biblioitemnumber ) =
315   C4::Biblio::AddBiblio( MARC::Record->new, '' );
316
317 my @order_infos = (
318     {
319         budget_id => $budget_id1,
320         pending_quantity  => 1,
321         spent_quantity  => 0,
322     },
323     {
324         budget_id => $budget_id2,
325         pending_quantity  => 2,
326         spent_quantity  => 1,
327     },
328     {
329         budget_id => $budget_id11,
330         pending_quantity  => 3,
331         spent_quantity  => 4,
332     },
333     {
334         budget_id => $budget_id12,
335         pending_quantity  => 4,
336         spent_quantity  => 3,
337     },
338     {
339         budget_id => $budget_id111,
340         pending_quantity  => 2,
341         spent_quantity  => 1,
342     },
343
344     # No order for budget_21
345
346 );
347
348 my %budgets;
349 my $invoiceid = AddInvoice(invoicenumber => 'invoice_test_clone', booksellerid => $booksellerid, unknown => "unknown");
350 my $invoice = GetInvoice( $invoiceid );
351 my $item_price = 10;
352 my $item_quantity = 2;
353 my $number_of_orders_to_move = 0;
354 for my $infos (@order_infos) {
355     for ( 1 .. $infos->{pending_quantity} ) {
356         my $order = Koha::Acquisition::Order->new(
357             {
358                 basketno           => $basketno,
359                 biblionumber       => $biblionumber,
360                 budget_id          => $infos->{budget_id},
361                 order_internalnote => "internal note",
362                 order_vendornote   => "vendor note",
363                 quantity           => 2,
364                 cost_tax_included  => $item_price,
365                 rrp_tax_included   => $item_price,
366                 listprice          => $item_price,
367                 ecost_tax_include  => $item_price,
368                 discount           => 0,
369                 uncertainprice     => 0,
370             }
371         )->store;
372         my $ordernumber = $order->ordernumber;
373         push @{ $budgets{$infos->{budget_id}} }, $ordernumber;
374         $number_of_orders_to_move++;
375     }
376     for ( 1 .. $infos->{spent_quantity} ) {
377         my $order = Koha::Acquisition::Order->new(
378             {
379                 basketno           => $basketno,
380                 biblionumber       => $biblionumber,
381                 budget_id          => $infos->{budget_id},
382                 order_internalnote => "internal note",
383                 order_vendornote   => "vendor note",
384                 quantity           => $item_quantity,
385                 cost               => $item_price,
386                 rrp_tax_included   => $item_price,
387                 listprice          => $item_price,
388                 ecost_tax_included => $item_price,
389                 discount           => 0,
390                 uncertainprice     => 0,
391             }
392         )->store;
393         my $ordernumber = $order->ordernumber;
394         ModReceiveOrder({
395               biblionumber     => $biblionumber,
396               order            => $order->unblessed,
397               budget_id        => $infos->{budget_id},
398               quantityreceived => $item_quantity,
399               invoice          => $invoice,
400               received_items   => [],
401         } );
402     }
403 }
404 is( GetBudgetHierarchySpent( $budget_id1 ), 160, "total spent for budget1 is 160" );
405 is( GetBudgetHierarchySpent( $budget_id11 ), 100, "total spent for budget11 is 100" );
406 is( GetBudgetHierarchySpent( $budget_id111 ), 20, "total spent for budget111 is 20" );
407
408 # GetBudgetSpent and GetBudgetOrdered
409 my $budget_period_amount = 100;
410 my $budget_amount = 50;
411
412 $budget = AddBudgetPeriod(
413     {
414         budget_period_startdate   => '2017-08-22',
415         budget_period_enddate     => '2018-08-22',
416         budget_period_description => 'Test budget',
417         budget_period_active      => 1,
418         budget_period_total       => $budget_period_amount,
419     }
420 );
421
422 my $fund = AddBudget(
423     {
424         budget_code       => 'Test fund',
425         budget_name       => 'Test fund',
426         budget_period_id  => $budget,
427         budget_parent_id  => undef,
428         budget_amount     => $budget_amount,
429     }
430 );
431
432 my $vendor = Koha::Acquisition::Bookseller->new(
433     {
434         name         => "test vendor",
435         address1     => "test address",
436         phone        => "0123456",
437         active       => 1,
438         deliverytime => 5,
439     }
440 )->store;
441
442 my $vendorid = $vendor->id;
443
444 my $basketnumber = C4::Acquisition::NewBasket( $vendorid, 1 );
445 my ( $biblio, $biblioitem ) = C4::Biblio::AddBiblio( MARC::Record->new, '' );
446
447 my @orders = (
448     {
449         budget_id  => $fund,
450         pending_quantity => 1,
451         spent_quantity => 0,
452     },
453 );
454
455 my $invoiceident = AddInvoice( invoicenumber => 'invoice_test_clone', booksellerid => $vendorid, shipmentdate => '2017-08-22', shipmentcost => 6, shipmentcost_budgetid => $fund );
456 my $test_invoice = GetInvoice( $invoiceident );
457 my $individual_item_price = 10;
458
459 my $order = Koha::Acquisition::Order->new(
460    {
461       basketno           => $basketnumber,
462       biblionumber       => $biblio,
463       budget_id          => $fund,
464       order_internalnote => "internalnote",
465       order_vendornote   => "vendor note",
466       quantity           => 2,
467       cost_tax_included  => $individual_item_price,
468       rrp_tax_included   => $individual_item_price,
469       listprice          => $individual_item_price,
470       ecost_tax_included => $individual_item_price,
471       discount           => 0,
472       uncertainprice     => 0,
473    }
474 )->store;
475
476 ModReceiveOrder({
477    bibionumber       => $biblio,
478    order             => $order->unblessed,
479    budget_id         => $fund,
480    quantityreceived  => 2,
481    invoice           => $test_invoice,
482    received_items    => [],
483 } );
484 t::lib::Mocks::mock_preference('OrderPriceRounding','');
485
486 is ( GetBudgetSpent( $fund ), 6, "total shipping cost is 6");
487 is ( GetBudgetOrdered( $fund ), '20', "total ordered price is 20");
488
489
490 # CloneBudgetPeriod
491 # Let's make sure our timestamp is old
492 my @orig_funds = Koha::Acquisition::Funds->search({ budget_period_id => $budget_period_id });
493 foreach my $fund (@orig_funds){
494     $fund->timestamp('1999-12-31 23:59:59')->store;
495 }
496
497 my $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
498     {
499         budget_period_id        => $budget_period_id,
500         budget_period_startdate => '2014-01-01',
501         budget_period_enddate   => '2014-12-31',
502         budget_period_description => 'Budget Period Cloned',
503     }
504 );
505
506 my $budget_period_cloned = C4::Budgets::GetBudgetPeriod($budget_period_id_cloned);
507 is($budget_period_cloned->{budget_period_description}, 'Budget Period Cloned', 'Cloned budget\'s description is updated.');
508
509 my $budget_cloned = C4::Budgets::GetBudgets({ budget_period_id => $budget_period_id_cloned });
510 my $budget_time =  $budget_cloned->[0]->{timestamp};
511
512 isnt($budget_time, '1999-12-31 23:59:59', "New budget has an updated timestamp");
513
514
515
516 my $budget_hierarchy        = GetBudgetHierarchy($budget_period_id);
517 my $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
518
519 is(
520     scalar(@$budget_hierarchy_cloned),
521     scalar(@$budget_hierarchy),
522     'CloneBudgetPeriod clones the same number of budgets (funds)'
523 );
524 is_deeply(
525     _get_dependencies($budget_hierarchy),
526     _get_dependencies($budget_hierarchy_cloned),
527     'CloneBudgetPeriod keeps the same dependencies order'
528 );
529
530 # CloneBudgetPeriod with param mark_original_budget_as_inactive
531 my $budget_period = C4::Budgets::GetBudgetPeriod($budget_period_id);
532 is( $budget_period->{budget_period_active}, 1,
533     'CloneBudgetPeriod does not mark as inactive the budgetperiod if not needed'
534 );
535
536 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
537 my $number_of_budgets_not_reset = 0;
538 for my $budget (@$budget_hierarchy_cloned) {
539     $number_of_budgets_not_reset++ if $budget->{budget_amount} > 0;
540 }
541 is( $number_of_budgets_not_reset, 5,
542     'CloneBudgetPeriod does not reset budgets (funds) if not needed' );
543
544 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
545     {
546         budget_period_id                 => $budget_period_id,
547         budget_period_startdate          => '2014-01-01',
548         budget_period_enddate            => '2014-12-31',
549         mark_original_budget_as_inactive => 1,
550     }
551 );
552
553 $budget_hierarchy        = GetBudgetHierarchy($budget_period_id);
554 is( $budget_hierarchy->[0]->{children}->[0]->{budget_name}, 'budget_11', 'GetBudgetHierarchy should return budgets ordered by name, first child is budget_11' );
555 is( $budget_hierarchy->[0]->{children}->[1]->{budget_name}, 'budget_12', 'GetBudgetHierarchy should return budgets ordered by name, second child is budget_12' );
556 is($budget_hierarchy->[0]->{budget_name},'budget_1','GetBudgetHierarchy should return budgets ordered by name, first budget is budget_1');
557 is($budget_hierarchy->[0]->{budget_level},'0','budget_level of budget (budget_1)  should be 0');
558 is($budget_hierarchy->[0]->{children}->[0]->{budget_level},'1','budget_level of first fund(budget_11)  should be 1');
559 is($budget_hierarchy->[0]->{children}->[1]->{budget_level},'1','budget_level of second fund(budget_12)  should be 1');
560 is($budget_hierarchy->[0]->{children}->[0]->{children}->[0]->{budget_level},'2','budget_level of  child fund budget_11 should be 2');
561 $budget_hierarchy        = GetBudgetHierarchy($budget_period_id);
562 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
563
564 is( scalar(@$budget_hierarchy_cloned), scalar(@$budget_hierarchy),
565 'CloneBudgetPeriod (with inactive param) clones the same number of budgets (funds)'
566 );
567 is_deeply(
568     _get_dependencies($budget_hierarchy),
569     _get_dependencies($budget_hierarchy_cloned),
570     'CloneBudgetPeriod (with inactive param) keeps the same dependencies order'
571 );
572 $budget_period = C4::Budgets::GetBudgetPeriod($budget_period_id);
573 is( $budget_period->{budget_period_active}, 0,
574     'CloneBudgetPeriod (with inactive param) marks as inactive the budgetperiod'
575 );
576
577 # CloneBudgetPeriod with param reset_all_budgets
578 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
579     {
580         budget_period_id        => $budget_period_id,
581         budget_period_startdate => '2014-01-01',
582         budget_period_enddate   => '2014-12-31',
583         reset_all_budgets         => 1,
584     }
585 );
586
587 $budget_hierarchy_cloned     = GetBudgetHierarchy($budget_period_id_cloned);
588 $number_of_budgets_not_reset = 0;
589 for my $budget (@$budget_hierarchy_cloned) {
590     $number_of_budgets_not_reset++ if $budget->{budget_amount} > 0;
591 }
592 is( $number_of_budgets_not_reset, 0,
593     'CloneBudgetPeriod has reset all budgets (funds)' );
594
595 #GetBudgetsByActivity
596 my $result=C4::Budgets::GetBudgetsByActivity(1);
597 isnt( $result, undef ,'GetBudgetsByActivity return correct value with parameter 1');
598 $result=C4::Budgets::GetBudgetsByActivity(0);
599  isnt( $result, undef ,'GetBudgetsByActivity return correct value with parameter 0');
600 $result=C4::Budgets::GetBudgetsByActivity();
601  is( $result, 0 , 'GetBudgetsByActivity return 0 with none parameter or other 0 or 1' );
602 DelBudget($budget_id);
603 DelBudgetPeriod($bpid);
604
605 # CloneBudgetPeriod with param amount_change_*
606 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
607     {
608         budget_period_id        => $budget_period_id,
609         budget_period_startdate => '2014-01-01',
610         budget_period_enddate   => '2014-12-31',
611         amount_change_percentage => 16,
612         amount_change_round_increment => 5,
613     }
614 );
615
616 $budget_period_cloned = C4::Budgets::GetBudgetPeriod($budget_period_id_cloned);
617 cmp_ok($budget_period_cloned->{budget_period_total}, '==', 11600, "CloneBudgetPeriod changed correctly budget amount");
618 $budget_hierarchy_cloned     = GetBudgetHierarchy($budget_period_id_cloned);
619 cmp_ok($budget_hierarchy_cloned->[0]->{budget_amount}, '==', 1160, "CloneBudgetPeriod changed correctly funds amounts");
620 cmp_ok($budget_hierarchy_cloned->[1]->{budget_amount}, '==', 115, "CloneBudgetPeriod changed correctly funds amounts");
621 cmp_ok($budget_hierarchy_cloned->[2]->{budget_amount}, '==', 55, "CloneBudgetPeriod changed correctly funds amounts");
622 cmp_ok($budget_hierarchy_cloned->[3]->{budget_amount}, '==', 115, "CloneBudgetPeriod changed correctly funds amounts");
623 cmp_ok($budget_hierarchy_cloned->[4]->{budget_amount}, '==', 2320, "CloneBudgetPeriod changed correctly funds amounts");
624 cmp_ok($budget_hierarchy_cloned->[5]->{budget_amount}, '==', 0, "CloneBudgetPeriod changed correctly funds amounts");
625
626 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
627     {
628         budget_period_id        => $budget_period_id,
629         budget_period_startdate => '2014-01-01',
630         budget_period_enddate   => '2014-12-31',
631         amount_change_percentage => 16,
632         amount_change_round_increment => 5,
633         reset_all_budgets => 1,
634     }
635 );
636 $budget_hierarchy_cloned     = GetBudgetHierarchy($budget_period_id_cloned);
637 cmp_ok($budget_hierarchy_cloned->[0]->{budget_amount}, '==', 0, "CloneBudgetPeriod reset all fund amounts");
638
639 # MoveOrders
640 my $number_orders_moved = C4::Budgets::MoveOrders();
641 is( $number_orders_moved, undef, 'MoveOrders return undef if no arg passed' );
642 $number_orders_moved =
643   C4::Budgets::MoveOrders( { from_budget_period_id => $budget_period_id } );
644 is( $number_orders_moved, undef,
645     'MoveOrders return undef if only 1 arg passed' );
646 $number_orders_moved =
647   C4::Budgets::MoveOrders( { to_budget_period_id => $budget_period_id } );
648 is( $number_orders_moved, undef,
649     'MoveOrders return undef if only 1 arg passed' );
650 $number_orders_moved = C4::Budgets::MoveOrders(
651     {
652         from_budget_period_id => $budget_period_id,
653         to_budget_period_id   => $budget_period_id
654     }
655 );
656 is( $number_orders_moved, undef,
657     'MoveOrders return undef if 2 budget period id are the same' );
658
659 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
660     {
661         budget_period_id        => $budget_period_id,
662         budget_period_startdate => '2014-01-01',
663         budget_period_enddate   => '2014-12-31',
664     }
665 );
666
667 my $report = C4::Budgets::MoveOrders(
668     {
669         from_budget_period_id  => $budget_period_id,
670         to_budget_period_id    => $budget_period_id_cloned,
671         move_remaining_unspent => 1,
672     }
673 );
674 is( scalar( @$report ), 6 , "MoveOrders has processed 6 funds" );
675
676 my $number_of_orders_moved = 0;
677 $number_of_orders_moved += scalar( @{ $_->{orders_moved} } ) for @$report;
678 is( $number_of_orders_moved, $number_of_orders_to_move, "MoveOrders has moved $number_of_orders_to_move orders" );
679
680 my @new_budget_ids = map { $_->{budget_id} }
681   @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
682 my @old_budget_ids = map { $_->{budget_id} }
683   @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
684 for my $budget_id ( keys %budgets ) {
685     for my $ordernumber ( @{ $budgets{$budget_id} } ) {
686         my $budget            = GetBudgetByOrderNumber($ordernumber);
687         my $is_in_new_budgets = grep /^$budget->{budget_id}$/, @new_budget_ids;
688         my $is_in_old_budgets = grep /^$budget->{budget_id}$/, @old_budget_ids;
689         is( $is_in_new_budgets, 1, "MoveOrders changed the budget_id for order $ordernumber" );
690         is( $is_in_old_budgets, 0, "MoveOrders changed the budget_id for order $ordernumber" );
691     }
692 }
693
694
695 # MoveOrders with param move_remaining_unspent
696 my @new_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
697 my @old_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
698
699 for my $new_budget ( @new_budgets ) {
700     my ( $old_budget ) = map { $_->{budget_code} eq $new_budget->{budget_code} ? $_ : () } @old_budgets;
701     my $new_budget_amount_should_be = $old_budget->{budget_amount} * 2 - $old_budget->{total_spent};
702     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})" );
703 }
704
705 # Test SetOwnerToFundHierarchy
706
707 my $patron_category = $builder->build({ source => 'Category' });
708 my $branchcode = $library->{branchcode};
709 my $john_doe = Koha::Patron->new({
710     cardnumber   => '123456',
711     firstname    => 'John',
712     surname      => 'Doe',
713     categorycode => $patron_category->{categorycode},
714     branchcode   => $branchcode,
715     dateofbirth  => '',
716     dateexpiry   => '9999-12-31',
717     userid       => 'john.doe'
718 })->store->borrowernumber;
719
720 C4::Budgets::SetOwnerToFundHierarchy( $budget_id1, $john_doe );
721 is( C4::Budgets::GetBudget($budget_id1)->{budget_owner_id},
722     $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 1 ($budget_id1)" );
723 is( C4::Budgets::GetBudget($budget_id11)->{budget_owner_id},
724     $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 11 ($budget_id11)" );
725 is( C4::Budgets::GetBudget($budget_id111)->{budget_owner_id},
726     $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 111 ($budget_id111)" );
727 is( C4::Budgets::GetBudget($budget_id12)->{budget_owner_id},
728     $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 12 ($budget_id12 )" );
729 is( C4::Budgets::GetBudget($budget_id2)->{budget_owner_id},
730     undef, "SetOwnerToFundHierarchy should not have set an owner for budget 2 ($budget_id2)" );
731 is( C4::Budgets::GetBudget($budget_id21)->{budget_owner_id},
732     undef, "SetOwnerToFundHierarchy should not have set an owner for budget 21 ($budget_id21)" );
733
734 my $jane_doe = Koha::Patron->new({
735     cardnumber   => '789012',
736     firstname    => 'Jane',
737     surname      => 'Doe',
738     categorycode => $patron_category->{categorycode},
739     branchcode   => $branchcode,
740     dateofbirth  => '',
741     dateexpiry   => '9999-12-31',
742     userid       => 'jane.doe'
743 })->store->borrowernumber;
744
745 C4::Budgets::SetOwnerToFundHierarchy( $budget_id11, $jane_doe );
746 is( C4::Budgets::GetBudget($budget_id1)->{budget_owner_id},
747     $john_doe, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 1 ($budget_id1)" );
748 is( C4::Budgets::GetBudget($budget_id11)->{budget_owner_id},
749     $jane_doe, "SetOwnerToFundHierarchy should have set John Doe $jane_doe for budget 11 ($budget_id11)" );
750 is( C4::Budgets::GetBudget($budget_id111)->{budget_owner_id},
751     $jane_doe, "SetOwnerToFundHierarchy should have set John Doe $jane_doe for budget 111 ($budget_id111)" );
752 is( C4::Budgets::GetBudget($budget_id12)->{budget_owner_id},
753     $john_doe, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 12 ($budget_id12 )" );
754 is( C4::Budgets::GetBudget($budget_id2)->{budget_owner_id},
755     undef, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 2 ($budget_id2)" );
756 is( C4::Budgets::GetBudget($budget_id21)->{budget_owner_id},
757     undef, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 21 ($budget_id21)" );
758
759 # Test GetBudgetAuthCats
760
761 my $budgetPeriodId = AddBudgetPeriod({
762     budget_period_startdate   => '2008-01-01',
763     budget_period_enddate     => '2008-12-31',
764     budget_period_description => 'just another budget',
765     budget_period_active      => 0,
766 });
767
768 $budgets = GetBudgets();
769 my $i = 0;
770 for my $budget ( @$budgets )
771 {
772     $budget->{sort1_authcat} = "sort1_authcat_$i";
773     $budget->{sort2_authcat} = "sort2_authcat_$i";
774     $budget->{budget_period_id} = $budgetPeriodId;
775     ModBudget( $budget );
776     $i++;
777 }
778
779 my $authCat = GetBudgetAuthCats($budgetPeriodId);
780
781 is( scalar @{$authCat}, $i * 2, "GetBudgetAuthCats returns only non-empty sorting categories (no empty authCat in db)" );
782
783 $i = 0;
784 for my $budget ( @$budgets )
785 {
786     $budget->{sort1_authcat} = "sort_authcat_$i";
787     $budget->{sort2_authcat} = "sort_authcat_$i";
788     $budget->{budget_period_id} = $budgetPeriodId;
789     ModBudget( $budget );
790     $i++;
791 }
792
793 $authCat = GetBudgetAuthCats($budgetPeriodId);
794 is( scalar @$authCat, scalar @$budgets, "GetBudgetAuthCats returns distinct authCat" );
795
796 $i = 0;
797 for my $budget ( @$budgets )
798 {
799     $budget->{sort1_authcat} = "sort1_authcat_$i";
800     $budget->{sort2_authcat} = "";
801     $budget->{budget_period_id} = $budgetPeriodId;
802     ModBudget( $budget );
803     $i++;
804 }
805
806 $authCat = GetBudgetAuthCats($budgetPeriodId);
807
808 is( scalar @{$authCat}, $i, "GetBudgetAuthCats returns only non-empty sorting categories (empty sort2_authcat on all records)" );
809
810 $i = 0;
811 for my $budget ( @$budgets )
812 {
813     $budget->{sort1_authcat} = "";
814     $budget->{sort2_authcat} = "";
815     $budget->{budget_period_id} = $budgetPeriodId;
816     ModBudget( $budget );
817     $i++;
818 }
819
820 $authCat = GetBudgetAuthCats($budgetPeriodId);
821
822 is( scalar @{$authCat}, 0, "GetBudgetAuthCats returns only non-empty sorting categories (all empty)" );
823
824 # /Test GetBudgetAuthCats
825
826 subtest 'GetBudgetSpent and GetBudgetOrdered' => sub {
827     plan tests => 20;
828
829     my $budget_period = $builder->build({
830         source => 'Aqbudgetperiod',
831         value  => {
832             budget_period_active => 1,
833             budget_total => 10000,
834         }
835     });
836     my $budget = $builder->build({
837         source => 'Aqbudget',
838         value  => {
839             budget_amount => 1000,
840             budget_encumb => undef,
841             budget_expend => undef,
842             budget_period_id => $budget_period->{budget_period_id},
843             budget_parent_id => undef,
844         }
845     });
846     my $invoice = $builder->build({
847         source => 'Aqinvoice',
848         value  => {
849             closedate => undef,
850         }
851     });
852
853     my $spent     = GetBudgetSpent( $budget->{budget_id} );
854     my $ordered   = GetBudgetOrdered( $budget->{budget_id} );
855     my $hierarchy = GetBudgetHierarchy($budget_period->{budget_period_id} );
856
857     is( $spent, 0, "New budget, no orders/invoices, should be nothing spent");
858     is( $ordered, 0, "New budget, no orders/invoices, should be nothing ordered");
859     is( @$hierarchy[0]->{total_spent},0,"New budgets, no orders/invoices, budget hierarchy shows 0 spent");
860     is( @$hierarchy[0]->{total_ordered},0,"New budgets, no orders/invoices, budget hierarchy shows 0 ordered");
861
862     my $inv_adj_1 = $builder->build({
863         source => 'AqinvoiceAdjustment',
864         value  => {
865             invoiceid     => $invoice->{invoiceid},
866             adjustment    => 3,
867             encumber_open => 0,
868             budget_id     => $budget->{budget_id},
869         }
870     });
871
872     $spent = GetBudgetSpent( $budget->{budget_id} );
873     $ordered = GetBudgetOrdered( $budget->{budget_id} );
874     $hierarchy = GetBudgetHierarchy($budget_period->{budget_period_id} );
875     is( $spent, 0, "After adding invoice adjustment on open invoice, should be nothing spent");
876     is( $ordered, 0, "After adding invoice adjustment on open invoice not encumbered, should be nothing ordered");
877     is( @$hierarchy[0]->{total_spent},0,"After adding invoice adjustment on open invoice, budget hierarchy shows 0 spent");
878     is( @$hierarchy[0]->{total_ordered},0,"After adding invoice adjustment on open invoice, budget hierarchy shows 0 ordered");
879
880     my $inv_adj_2 = $builder->build({
881         source => 'AqinvoiceAdjustment',
882         value  => {
883             invoiceid     => $invoice->{invoiceid},
884             adjustment    => 3,
885             encumber_open => 1,
886             budget_id     => $budget->{budget_id},
887         }
888     });
889
890     $spent = GetBudgetSpent( $budget->{budget_id} );
891     $ordered = GetBudgetOrdered( $budget->{budget_id} );
892     $hierarchy = GetBudgetHierarchy($budget_period->{budget_period_id} );
893     is( $spent, 0, "After adding invoice adjustment on open invoice, should be nothing spent");
894     is( $ordered, 3, "After adding invoice adjustment on open invoice encumbered, should be 3 ordered");
895     is( @$hierarchy[0]->{total_spent},0,"After adding invoice adjustment on open invoice encumbered, budget hierarchy shows 0 spent");
896     is( @$hierarchy[0]->{total_ordered},3,"After adding invoice adjustment on open invoice encumbered, budget hierarchy shows 3 ordered");
897
898     my $invoice_2 = $builder->build({
899         source => 'Aqinvoice',
900         value  => {
901             closedate => '2017-07-01',
902         }
903     });
904     my $inv_adj_3 = $builder->build({
905         source => 'AqinvoiceAdjustment',
906         value  => {
907             invoiceid     => $invoice_2->{invoiceid},
908             adjustment    => 3,
909             encumber_open => 0,
910             budget_id     => $budget->{budget_id},
911         }
912     });
913     my $inv_adj_4 = $builder->build({
914         source => 'AqinvoiceAdjustment',
915         value  => {
916             invoiceid     => $invoice_2->{invoiceid},
917             adjustment    => 3,
918             encumber_open => 1,
919             budget_id     => $budget->{budget_id},
920         }
921     });
922
923     $spent = GetBudgetSpent( $budget->{budget_id} );
924     $ordered = GetBudgetOrdered( $budget->{budget_id} );
925     $hierarchy = GetBudgetHierarchy($budget_period->{budget_period_id} );
926     is( $spent, 6, "After adding invoice adjustment on closed invoice, should be 6 spent, encumber has no affect once closed");
927     is( $ordered, 3, "After adding invoice adjustment on closed invoice, should still be 3 ordered");
928     is( @$hierarchy[0]->{total_spent},6,"After adding invoice adjustment on closed invoice, budget hierarchy shows 6 spent");
929     is( @$hierarchy[0]->{total_ordered},3,"After adding invoice adjustment on closed invoice, budget hierarchy still shows 3 ordered");
930
931     my $budget0 = $builder->build({
932         source => 'Aqbudget',
933         value  => {
934             budget_amount => 1000,
935             budget_encumb => undef,
936             budget_expend => undef,
937             budget_period_id => $budget_period->{budget_period_id},
938             budget_parent_id => $budget->{budget_id},
939         }
940     });
941     my $inv_adj_5 = $builder->build({
942         source => 'AqinvoiceAdjustment',
943         value  => {
944             invoiceid     => $invoice->{invoiceid},
945             adjustment    => 3,
946             encumber_open => 1,
947             budget_id     => $budget0->{budget_id},
948         }
949     });
950     my $inv_adj_6 = $builder->build({
951         source => 'AqinvoiceAdjustment',
952         value  => {
953             invoiceid     => $invoice_2->{invoiceid},
954             adjustment    => 3,
955             encumber_open => 1,
956             budget_id     => $budget0->{budget_id},
957         }
958     });
959
960     $spent = GetBudgetSpent( $budget->{budget_id} );
961     $ordered = GetBudgetOrdered( $budget->{budget_id} );
962     $hierarchy = GetBudgetHierarchy($budget_period->{budget_period_id} );
963     is( $spent, 6, "After adding invoice adjustment on a child budget should be 6 spent/budget unaffected");
964     is( $ordered, 3, "After adding invoice adjustment on a child budget, should still be 3 ordered/budget unaffected");
965     is( @$hierarchy[0]->{total_spent},9,"After adding invoice adjustment on child budget, budget hierarchy shows 9 spent");
966     is( @$hierarchy[0]->{total_ordered},6,"After adding invoice adjustment on child budget, budget hierarchy shows 6 ordered");
967 };
968
969 subtest 'OrderPriceRounding GetBudgetSpent GetBudgetOrdered tests' => sub {
970
971     plan tests => 8;
972
973 #Let's build an order, we need a couple things though
974
975     my $spent_biblio = $builder->build({ source => 'Biblio' });
976     my $spent_basket = $builder->build({ source => 'Aqbasket', value => { is_standing => 0 } });
977     my $spent_invoice = $builder->build({ source => 'Aqinvoice'});
978     my $spent_currency = $builder->build({ source => 'Currency', value => { active => 1, archived => 0, symbol => 'F', rate => 2, isocode => undef, currency => 'FOO' }  });
979     my $spent_vendor = $builder->build({ source => 'Aqbookseller',value => { listincgst => 0, listprice => $spent_currency->{currency}, invoiceprice => $spent_currency->{currency} } });
980     my $spent_orderinfo = {
981         basketno => $spent_basket->{basketno},
982         booksellerid => $spent_vendor->{id},
983         rrp => 16.99,
984         discount => .42,
985         ecost => 16.91,
986         biblionumber => $spent_biblio->{biblionumber},
987         currency => $spent_currency->{currency},
988         tax_rate_on_ordering => 0,
989         tax_value_on_ordering => 0,
990         tax_rate_on_receiving => 0,
991         tax_value_on_receiving => 0,
992         quantity => 8,
993         quantityreceived => 0,
994         datecancellationprinted => undef,
995         datereceived => undef,
996     };
997
998 #Okay we have basically what the user would enter, now we do some maths
999
1000     $spent_orderinfo = C4::Acquisition::populate_order_with_prices({
1001             order        => $spent_orderinfo,
1002             booksellerid => $spent_orderinfo->{booksellerid},
1003             ordering     => 1,
1004     });
1005
1006 #And let's place the order
1007
1008     my $spent_order = $builder->build({ source => 'Aqorder', value => $spent_orderinfo });
1009     t::lib::Mocks::mock_preference('OrderPriceRounding','');
1010     my $spent_ordered = GetBudgetOrdered( $spent_order->{budget_id} );
1011
1012     is($spent_orderinfo->{ecost_tax_excluded}, 9.854200,'We store extra precision in price calculation');
1013     is( Koha::Number::Price->new($spent_orderinfo->{ecost_tax_excluded})->format(), 9.85,'But the price as formatted is two digits');
1014     is($spent_ordered,'78.8336',"We expect the ordered amount to be equal to the estimated price times quantity with full precision");
1015
1016     t::lib::Mocks::mock_preference('OrderPriceRounding','nearest_cent');
1017     $spent_ordered = GetBudgetOrdered( $spent_order->{budget_id} );
1018     is($spent_ordered,'78.8',"We expect the ordered amount to be equal to the estimated price rounded times quantity");
1019
1020 #Okay, now we can receive the order, giving the price as the user would
1021
1022     $spent_orderinfo->{unitprice} = 9.85; #we are paying what we expected
1023
1024 #Do our maths
1025
1026     $spent_orderinfo = C4::Acquisition::populate_order_with_prices({
1027             order        => $spent_orderinfo,
1028             booksellerid => $spent_orderinfo->{booksellerid},
1029             receiving    => 1,
1030     });
1031     my $received_order = $builder->build({ source => 'Aqorder', value => $spent_orderinfo });
1032
1033 #And receive
1034
1035     ModReceiveOrder({
1036             biblionumber => $spent_order->{biblionumber},
1037             order => $received_order,
1038             invoice => $spent_invoice,
1039             quantityreceived => $spent_order->{quantity},
1040             budget_id => $spent_order->{budget_id},
1041             received_items => [],
1042     });
1043
1044     t::lib::Mocks::mock_preference('OrderPriceRounding','');
1045     my $spent_spent = GetBudgetSpent( $spent_order->{budget_id} );
1046     is($spent_orderinfo->{unitprice_tax_excluded}, 9.854200,'We store extra precision in price calculation');
1047     is( Koha::Number::Price->new($spent_orderinfo->{unitprice_tax_excluded})->format(), 9.85,'But the price as formatted is two digits');
1048     is($spent_spent,'78.8336',"We expect the spent amount to be equal to the estimated price times quantity with full precision");
1049
1050     t::lib::Mocks::mock_preference('OrderPriceRounding','nearest_cent');
1051     $spent_spent = GetBudgetSpent( $spent_order->{budget_id} );
1052     is($spent_spent,'78.8',"We expect the spent amount to be equal to the estimated price rounded times quantity");
1053
1054 };
1055
1056 sub _get_dependencies {
1057     my ($budget_hierarchy) = @_;
1058     my $graph;
1059     for my $budget (@$budget_hierarchy) {
1060         if ( $budget->{child} ) {
1061             my @sorted = sort @{ $budget->{child} };
1062             for my $child_id (@sorted) {
1063                 push @{ $graph->{ $budget->{budget_name} }{children} },
1064                   _get_budgetname_by_id( $budget_hierarchy, $child_id );
1065             }
1066         }
1067         push @{ $graph->{ $budget->{budget_name} }{parents} },
1068           $budget->{parent_id};
1069     }
1070     return $graph;
1071 }
1072
1073 sub _get_budgetname_by_id {
1074     my ( $budgets, $budget_id ) = @_;
1075     my ($budget_name) =
1076       map { ( $_->{budget_id} eq $budget_id ) ? $_->{budget_name} : () }
1077       @$budgets;
1078     return $budget_name;
1079 }