Bug 25417: Add unit test
[koha.git] / t / db_dependent / Budgets.t
blob339d5bd6e4b0504c5a7fcf69d5915d9001bbbbed
1 #!/usr/bin/perl
2 use Modern::Perl;
3 use Test::More tests => 144;
5 BEGIN {
6 use_ok('C4::Budgets')
8 use C4::Context;
9 use C4::Biblio;
10 use C4::Acquisition;
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 use Koha::Items;
19 use t::lib::TestBuilder;
20 use t::lib::Mocks;
21 use Koha::DateUtils;
23 use YAML;
25 use t::lib::Mocks;
26 t::lib::Mocks::mock_preference('OrderPriceRounding','');
28 my $schema = Koha::Database->new->schema;
29 $schema->storage->txn_begin;
30 my $builder = t::lib::TestBuilder->new;
31 my $dbh = C4::Context->dbh;
32 $dbh->do(q|DELETE FROM aqbudgetperiods|);
33 $dbh->do(q|DELETE FROM aqbudgets|);
35 my $library = $builder->build({
36 source => 'Branch',
37 });
39 t::lib::Mocks::mock_userenv(
41 flags => 1,
42 userid => 'my_userid',
43 branch => $library->{branchcode},
48 # Budget Periods :
51 is( AddBudgetPeriod(), undef, 'AddBugetPeriod without argument returns undef' );
52 is( AddBudgetPeriod( { } ), undef, 'AddBugetPeriod with an empty argument returns undef' );
53 my $bpid = AddBudgetPeriod({
54 budget_period_startdate => '2008-01-01',
55 });
56 is( $bpid, undef, 'AddBugetPeriod without end date returns undef' );
57 $bpid = AddBudgetPeriod({
58 budget_period_enddate => '2008-12-31',
59 });
60 is( $bpid, undef, 'AddBugetPeriod without start date returns undef' );
61 my $budgetperiods = GetBudgetPeriods();
62 is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
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
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' );
79 $my_budgetperiod = {
80 budget_period_startdate => '2009-01-01',
81 budget_period_enddate => '2009-12-31',
82 budget_period_description => 'MODIF_MAPERI',
83 budget_period_active => 1,
85 my $mod_status = ModBudgetPeriod($my_budgetperiod);
86 is( $mod_status, undef, 'ModBudgetPeriod without id returns undef' );
88 $my_budgetperiod->{budget_period_id} = $bpid;
89 $mod_status = ModBudgetPeriod($my_budgetperiod);
90 is( $mod_status, 1, 'ModBudgetPeriod returnis true' );
91 $budgetperiod = GetBudgetPeriod($bpid);
92 is( $budgetperiod->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'ModBudgetPeriod updates the start date correctly' );
93 is( $budgetperiod->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'ModBudgetPeriod updates the end date correctly' );
94 is( $budgetperiod->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'ModBudgetPeriod updates the description correctly' );
95 is( $budgetperiod->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'ModBudgetPeriod upates active correctly' );
97 $budgetperiods = GetBudgetPeriods();
98 is( @$budgetperiods, 1, 'GetBudgetPeriods returns the correct number of budget periods' );
99 is( $budgetperiods->[0]->{budget_period_id}, $my_budgetperiod->{budget_period_id}, 'GetBudgetPeriods returns the id correctly' );
100 is( $budgetperiods->[0]->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'GetBudgetPeriods returns the start date correctly' );
101 is( $budgetperiods->[0]->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'GetBudgetPeriods returns the end date correctly' );
102 is( $budgetperiods->[0]->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'GetBudgetPeriods returns the description correctly' );
103 is( $budgetperiods->[0]->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'GetBudgetPeriods returns active correctly' );
105 is( DelBudgetPeriod($bpid), 1, 'DelBudgetPeriod returns true' );
106 $budgetperiods = GetBudgetPeriods();
107 is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
110 # Budget :
113 # The budget hierarchy will be:
114 # budget_1
115 # budget_11
116 # budget_111
117 # budget_12
118 # budget_2
119 # budget_21
121 is( AddBudget(), undef, 'AddBuget without argument returns undef' );
122 my $budgets = GetBudgets();
123 is( @$budgets, 0, 'GetBudgets returns the correct number of budgets' );
125 $bpid = AddBudgetPeriod($my_budgetperiod); #this is an active budget
127 my $my_budget = {
128 budget_code => 'ABCD',
129 budget_amount => '123.132000',
130 budget_name => 'Periodiques',
131 budget_notes => 'This is a note',
132 budget_period_id => $bpid,
133 budget_encumb => '', # Bug 21604
135 my $budget_id = AddBudget($my_budget);
136 isnt( $budget_id, undef, 'AddBudget does not returns undef' );
137 my $budget = GetBudget($budget_id);
138 is( $budget->{budget_code}, $my_budget->{budget_code}, 'AddBudget stores the budget code correctly' );
139 is( $budget->{budget_amount}, $my_budget->{budget_amount}, 'AddBudget stores the budget amount correctly' );
140 is( $budget->{budget_name}, $my_budget->{budget_name}, 'AddBudget stores the budget name correctly' );
141 is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'AddBudget stores the budget notes correctly' );
142 is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'AddBudget stores the budget period id correctly' );
145 $my_budget = {
146 budget_code => 'EFG',
147 budget_amount => '321.231000',
148 budget_name => 'Modified name',
149 budget_notes => 'This is a modified note',
150 budget_period_id => $bpid,
152 $mod_status = ModBudget($my_budget);
153 is( $mod_status, undef, 'ModBudget without id returns undef' );
155 $my_budget->{budget_id} = $budget_id;
156 $mod_status = ModBudget($my_budget);
157 is( $mod_status, 1, 'ModBudget returns true' );
158 $budget = GetBudget($budget_id);
159 is( $budget->{budget_code}, $my_budget->{budget_code}, 'ModBudget updates the budget code correctly' );
160 is( $budget->{budget_amount}, $my_budget->{budget_amount}, 'ModBudget updates the budget amount correctly' );
161 is( $budget->{budget_name}, $my_budget->{budget_name}, 'ModBudget updates the budget name correctly' );
162 is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'ModBudget updates the budget notes correctly' );
163 is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'ModBudget updates the budget period id correctly' );
166 $budgets = GetBudgets();
167 is( @$budgets, 1, 'GetBudgets returns the correct number of budgets' );
168 is( $budgets->[0]->{budget_id}, $my_budget->{budget_id}, 'GetBudgets returns the budget id correctly' );
169 is( $budgets->[0]->{budget_code}, $my_budget->{budget_code}, 'GetBudgets returns the budget code correctly' );
170 is( $budgets->[0]->{budget_amount}, $my_budget->{budget_amount}, 'GetBudgets returns the budget amount correctly' );
171 is( $budgets->[0]->{budget_name}, $my_budget->{budget_name}, 'GetBudgets returns the budget name correctly' );
172 is( $budgets->[0]->{budget_notes}, $my_budget->{budget_notes}, 'GetBudgets returns the budget notes correctly' );
173 is( $budgets->[0]->{budget_period_id}, $my_budget->{budget_period_id}, 'GetBudgets returns the budget period id correctly' );
175 $budgets = GetBudgets( {budget_period_id => $bpid} );
176 is( @$budgets, 1, 'GetBudgets With Filter OK' );
177 $budgets = GetBudgets( {budget_period_id => $bpid}, {-asc => "budget_name"} );
178 is( @$budgets, 1, 'GetBudgets With Order OK' );
179 $budgets = GetBudgets( {budget_period_id => GetBudgetPeriod($bpid)->{budget_period_id}}, {-asc => "budget_name"} );
180 is( @$budgets, 1, 'GetBudgets With Order Getting Active budgetPeriod OK');
183 my $budget_name = GetBudgetName( $budget_id );
184 is($budget_name, $my_budget->{budget_name}, "Test the GetBudgetName routine");
186 my $my_inactive_budgetperiod = { #let's add an inactive
187 budget_period_startdate => '2010-01-01',
188 budget_period_enddate => '2010-12-31',
189 budget_period_description => 'MODIF_MAPERI',
190 budget_period_active => 0,
192 my $bpid_i = AddBudgetPeriod($my_inactive_budgetperiod); #this is an inactive budget
194 my $my_budget_inactive = {
195 budget_code => 'EFG',
196 budget_amount => '123.132000',
197 budget_name => 'Periodiques',
198 budget_notes => 'This is a note',
199 budget_period_id => $bpid_i,
201 my $budget_id_inactive = AddBudget($my_budget_inactive);
203 my $budget_code = $my_budget->{budget_code};
204 my $budget_by_code = GetBudgetByCode( $budget_code );
205 is($budget_by_code->{budget_id}, $budget_id, "GetBudgetByCode, check id"); #this should match the active budget, not the inactive
206 is($budget_by_code->{budget_notes}, $my_budget->{budget_notes}, "GetBudgetByCode, check notes");
208 my $second_budget_id = AddBudget({
209 budget_code => "ZZZZ",
210 budget_amount => "500.00",
211 budget_name => "Art",
212 budget_notes => "This is a note",
213 budget_period_id => $bpid,
215 isnt( $second_budget_id, undef, 'AddBudget does not returns undef' );
217 $budgets = GetBudgets( {budget_period_id => $bpid} );
218 ok( $budgets->[0]->{budget_name} lt $budgets->[1]->{budget_name}, 'default sort order for GetBudgets is by name' );
220 is( DelBudget($budget_id), 1, 'DelBudget returns true' );
221 $budgets = GetBudgets();
222 is( @$budgets, 2, 'GetBudgets returns the correct number of budget periods' );
225 # GetBudgetHierarchySpent and GetBudgetHierarchyOrdered
226 my $budget_period_total = 10_000;
227 my $budget_1_total = 1_000;
228 my $budget_11_total = 100;
229 my $budget_111_total = 50;
230 my $budget_12_total = 100;
231 my $budget_2_total = 2_000;
233 my $budget_period_id = AddBudgetPeriod(
235 budget_period_startdate => '2013-01-01',
236 budget_period_enddate => '2014-12-31',
237 budget_period_description => 'Budget Period',
238 budget_period_active => 1,
239 budget_period_total => $budget_period_total,
242 my $budget_id1 = AddBudget(
244 budget_code => 'budget_1',
245 budget_name => 'budget_1',
246 budget_period_id => $budget_period_id,
247 budget_parent_id => undef,
248 budget_amount => $budget_1_total,
251 my $budget_id2 = AddBudget(
253 budget_code => 'budget_2',
254 budget_name => 'budget_2',
255 budget_period_id => $budget_period_id,
256 budget_parent_id => undef,
257 budget_amount => $budget_2_total,
260 my $budget_id12 = AddBudget(
262 budget_code => 'budget_12',
263 budget_name => 'budget_12',
264 budget_period_id => $budget_period_id,
265 budget_parent_id => $budget_id1,
266 budget_amount => $budget_12_total,
269 my $budget_id11 = AddBudget(
271 budget_code => 'budget_11',
272 budget_name => 'budget_11',
273 budget_period_id => $budget_period_id,
274 budget_parent_id => $budget_id1,
275 budget_amount => $budget_11_total,
278 my $budget_id111 = AddBudget(
280 budget_code => 'budget_111',
281 budget_name => 'budget_111',
282 budget_period_id => $budget_period_id,
283 budget_parent_id => $budget_id11,
284 budget_owner_id => 1,
285 budget_amount => $budget_111_total,
288 my $budget_id21 = AddBudget(
290 budget_code => 'budget_21',
291 budget_name => 'budget_21',
292 budget_period_id => $budget_period_id,
293 budget_parent_id => $budget_id2,
297 my $bookseller = Koha::Acquisition::Bookseller->new(
299 name => "my vendor",
300 address1 => "bookseller's address",
301 phone => "0123456",
302 active => 1,
303 deliverytime => 5,
305 )->store;
306 my $booksellerid = $bookseller->id;
308 my $basketno = C4::Acquisition::NewBasket( $booksellerid, 1 );
309 my ( $biblionumber, $biblioitemnumber ) =
310 C4::Biblio::AddBiblio( MARC::Record->new, '' );
312 my @order_infos = (
314 budget_id => $budget_id1,
315 pending_quantity => 1,
316 spent_quantity => 0,
319 budget_id => $budget_id2,
320 pending_quantity => 2,
321 spent_quantity => 1,
324 budget_id => $budget_id11,
325 pending_quantity => 3,
326 spent_quantity => 4,
329 budget_id => $budget_id12,
330 pending_quantity => 4,
331 spent_quantity => 3,
334 budget_id => $budget_id111,
335 pending_quantity => 2,
336 spent_quantity => 1,
339 # No order for budget_21
343 my %budgets;
344 my $invoiceid = AddInvoice(invoicenumber => 'invoice_test_clone', booksellerid => $booksellerid, unknown => "unknown");
345 my $invoice = GetInvoice( $invoiceid );
346 my $item_price = 10;
347 my $item_quantity = 2;
348 my $number_of_orders_to_move = 0;
349 for my $infos (@order_infos) {
350 for ( 1 .. $infos->{pending_quantity} ) {
351 my $order = Koha::Acquisition::Order->new(
353 basketno => $basketno,
354 biblionumber => $biblionumber,
355 budget_id => $infos->{budget_id},
356 order_internalnote => "internal note",
357 order_vendornote => "vendor note",
358 quantity => 2,
359 cost_tax_included => $item_price,
360 rrp_tax_included => $item_price,
361 listprice => $item_price,
362 ecost_tax_include => $item_price,
363 discount => 0,
364 uncertainprice => 0,
366 )->store;
367 my $ordernumber = $order->ordernumber;
368 push @{ $budgets{$infos->{budget_id}} }, $ordernumber;
369 $number_of_orders_to_move++;
371 for ( 1 .. $infos->{spent_quantity} ) {
372 my $order = Koha::Acquisition::Order->new(
374 basketno => $basketno,
375 biblionumber => $biblionumber,
376 budget_id => $infos->{budget_id},
377 order_internalnote => "internal note",
378 order_vendornote => "vendor note",
379 quantity => $item_quantity,
380 cost => $item_price,
381 rrp_tax_included => $item_price,
382 listprice => $item_price,
383 ecost_tax_included => $item_price,
384 discount => 0,
385 uncertainprice => 0,
387 )->store;
388 my $ordernumber = $order->ordernumber;
389 ModReceiveOrder({
390 biblionumber => $biblionumber,
391 order => $order->unblessed,
392 budget_id => $infos->{budget_id},
393 quantityreceived => $item_quantity,
394 invoice => $invoice,
395 received_items => [],
396 } );
399 is( GetBudgetHierarchySpent( $budget_id1 ), 160, "total spent for budget1 is 160" );
400 is( GetBudgetHierarchySpent( $budget_id11 ), 100, "total spent for budget11 is 100" );
401 is( GetBudgetHierarchySpent( $budget_id111 ), 20, "total spent for budget111 is 20" );
403 # GetBudgetSpent and GetBudgetOrdered
404 my $budget_period_amount = 100;
405 my $budget_amount = 50;
407 $budget = AddBudgetPeriod(
409 budget_period_startdate => '2017-08-22',
410 budget_period_enddate => '2018-08-22',
411 budget_period_description => 'Test budget',
412 budget_period_active => 1,
413 budget_period_total => $budget_period_amount,
417 my $fund = AddBudget(
419 budget_code => 'Test fund',
420 budget_name => 'Test fund',
421 budget_period_id => $budget,
422 budget_parent_id => undef,
423 budget_amount => $budget_amount,
427 my $vendor = Koha::Acquisition::Bookseller->new(
429 name => "test vendor",
430 address1 => "test address",
431 phone => "0123456",
432 active => 1,
433 deliverytime => 5,
435 )->store;
437 my $vendorid = $vendor->id;
439 my $basketnumber = C4::Acquisition::NewBasket( $vendorid, 1 );
440 my ( $biblio, $biblioitem ) = C4::Biblio::AddBiblio( MARC::Record->new, '' );
442 my @orders = (
444 budget_id => $fund,
445 pending_quantity => 1,
446 spent_quantity => 0,
450 my $invoiceident = AddInvoice( invoicenumber => 'invoice_test_clone', booksellerid => $vendorid, shipmentdate => '2017-08-22', shipmentcost => 6, shipmentcost_budgetid => $fund );
451 my $test_invoice = GetInvoice( $invoiceident );
452 my $individual_item_price = 10;
454 my $order = Koha::Acquisition::Order->new(
456 basketno => $basketnumber,
457 biblionumber => $biblio,
458 budget_id => $fund,
459 order_internalnote => "internalnote",
460 order_vendornote => "vendor note",
461 quantity => 2,
462 cost_tax_included => $individual_item_price,
463 rrp_tax_included => $individual_item_price,
464 listprice => $individual_item_price,
465 ecost_tax_included => $individual_item_price,
466 discount => 0,
467 uncertainprice => 0,
469 )->store;
471 ModReceiveOrder({
472 bibionumber => $biblio,
473 order => $order->unblessed,
474 budget_id => $fund,
475 quantityreceived => 2,
476 invoice => $test_invoice,
477 received_items => [],
478 } );
479 t::lib::Mocks::mock_preference('OrderPriceRounding','');
481 is ( GetBudgetSpent( $fund ), 6, "total shipping cost is 6");
482 is ( GetBudgetOrdered( $fund ), '20', "total ordered price is 20");
485 # CloneBudgetPeriod
486 # Let's make sure our timestamp is old
487 my @orig_funds = Koha::Acquisition::Funds->search({ budget_period_id => $budget_period_id });
488 foreach my $fund (@orig_funds){
489 $fund->timestamp('1999-12-31 23:59:59')->store;
492 my $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
494 budget_period_id => $budget_period_id,
495 budget_period_startdate => '2014-01-01',
496 budget_period_enddate => '2014-12-31',
497 budget_period_description => 'Budget Period Cloned',
501 my $budget_period_cloned = C4::Budgets::GetBudgetPeriod($budget_period_id_cloned);
502 is($budget_period_cloned->{budget_period_description}, 'Budget Period Cloned', 'Cloned budget\'s description is updated.');
504 my $budget_cloned = C4::Budgets::GetBudgets({ budget_period_id => $budget_period_id_cloned });
505 my $budget_time = $budget_cloned->[0]->{timestamp};
507 isnt($budget_time, '1999-12-31 23:59:59', "New budget has an updated timestamp");
511 my $budget_hierarchy = GetBudgetHierarchy($budget_period_id);
512 my $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
515 scalar(@$budget_hierarchy_cloned),
516 scalar(@$budget_hierarchy),
517 'CloneBudgetPeriod clones the same number of budgets (funds)'
519 is_deeply(
520 _get_dependencies($budget_hierarchy),
521 _get_dependencies($budget_hierarchy_cloned),
522 'CloneBudgetPeriod keeps the same dependencies order'
525 # CloneBudgetPeriod with param mark_original_budget_as_inactive
526 my $budget_period = C4::Budgets::GetBudgetPeriod($budget_period_id);
527 is( $budget_period->{budget_period_active}, 1,
528 'CloneBudgetPeriod does not mark as inactive the budgetperiod if not needed'
531 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
532 my $number_of_budgets_not_reset = 0;
533 for my $budget (@$budget_hierarchy_cloned) {
534 $number_of_budgets_not_reset++ if $budget->{budget_amount} > 0;
536 is( $number_of_budgets_not_reset, 5,
537 'CloneBudgetPeriod does not reset budgets (funds) if not needed' );
539 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
541 budget_period_id => $budget_period_id,
542 budget_period_startdate => '2014-01-01',
543 budget_period_enddate => '2014-12-31',
544 mark_original_budget_as_inactive => 1,
548 $budget_hierarchy = GetBudgetHierarchy($budget_period_id);
549 is( $budget_hierarchy->[0]->{children}->[0]->{budget_name}, 'budget_11', 'GetBudgetHierarchy should return budgets ordered by name, first child is budget_11' );
550 is( $budget_hierarchy->[0]->{children}->[1]->{budget_name}, 'budget_12', 'GetBudgetHierarchy should return budgets ordered by name, second child is budget_12' );
551 is($budget_hierarchy->[0]->{budget_name},'budget_1','GetBudgetHierarchy should return budgets ordered by name, first budget is budget_1');
552 is($budget_hierarchy->[0]->{budget_level},'0','budget_level of budget (budget_1) should be 0');
553 is($budget_hierarchy->[0]->{children}->[0]->{budget_level},'1','budget_level of first fund(budget_11) should be 1');
554 is($budget_hierarchy->[0]->{children}->[1]->{budget_level},'1','budget_level of second fund(budget_12) should be 1');
555 is($budget_hierarchy->[0]->{children}->[0]->{children}->[0]->{budget_level},'2','budget_level of child fund budget_11 should be 2');
556 $budget_hierarchy = GetBudgetHierarchy($budget_period_id);
557 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
559 is( scalar(@$budget_hierarchy_cloned), scalar(@$budget_hierarchy),
560 'CloneBudgetPeriod (with inactive param) clones the same number of budgets (funds)'
562 is_deeply(
563 _get_dependencies($budget_hierarchy),
564 _get_dependencies($budget_hierarchy_cloned),
565 'CloneBudgetPeriod (with inactive param) keeps the same dependencies order'
567 $budget_period = C4::Budgets::GetBudgetPeriod($budget_period_id);
568 is( $budget_period->{budget_period_active}, 0,
569 'CloneBudgetPeriod (with inactive param) marks as inactive the budgetperiod'
572 # CloneBudgetPeriod with param reset_all_budgets
573 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
575 budget_period_id => $budget_period_id,
576 budget_period_startdate => '2014-01-01',
577 budget_period_enddate => '2014-12-31',
578 reset_all_budgets => 1,
582 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
583 $number_of_budgets_not_reset = 0;
584 for my $budget (@$budget_hierarchy_cloned) {
585 $number_of_budgets_not_reset++ if $budget->{budget_amount} > 0;
587 is( $number_of_budgets_not_reset, 0,
588 'CloneBudgetPeriod has reset all budgets (funds)' );
590 #GetBudgetsByActivity
591 my $result=C4::Budgets::GetBudgetsByActivity(1);
592 isnt( $result, undef ,'GetBudgetsByActivity return correct value with parameter 1');
593 $result=C4::Budgets::GetBudgetsByActivity(0);
594 isnt( $result, undef ,'GetBudgetsByActivity return correct value with parameter 0');
595 $result=C4::Budgets::GetBudgetsByActivity();
596 is( $result, 0 , 'GetBudgetsByActivity return 0 with none parameter or other 0 or 1' );
597 DelBudget($budget_id);
598 DelBudgetPeriod($bpid);
600 # CloneBudgetPeriod with param amount_change_*
601 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
603 budget_period_id => $budget_period_id,
604 budget_period_startdate => '2014-01-01',
605 budget_period_enddate => '2014-12-31',
606 amount_change_percentage => 16,
607 amount_change_round_increment => 5,
611 $budget_period_cloned = C4::Budgets::GetBudgetPeriod($budget_period_id_cloned);
612 cmp_ok($budget_period_cloned->{budget_period_total}, '==', 11600, "CloneBudgetPeriod changed correctly budget amount");
613 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
614 cmp_ok($budget_hierarchy_cloned->[0]->{budget_amount}, '==', 1160, "CloneBudgetPeriod changed correctly funds amounts");
615 cmp_ok($budget_hierarchy_cloned->[1]->{budget_amount}, '==', 115, "CloneBudgetPeriod changed correctly funds amounts");
616 cmp_ok($budget_hierarchy_cloned->[2]->{budget_amount}, '==', 55, "CloneBudgetPeriod changed correctly funds amounts");
617 cmp_ok($budget_hierarchy_cloned->[3]->{budget_amount}, '==', 115, "CloneBudgetPeriod changed correctly funds amounts");
618 cmp_ok($budget_hierarchy_cloned->[4]->{budget_amount}, '==', 2320, "CloneBudgetPeriod changed correctly funds amounts");
619 cmp_ok($budget_hierarchy_cloned->[5]->{budget_amount}, '==', 0, "CloneBudgetPeriod changed correctly funds amounts");
621 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
623 budget_period_id => $budget_period_id,
624 budget_period_startdate => '2014-01-01',
625 budget_period_enddate => '2014-12-31',
626 amount_change_percentage => 16,
627 amount_change_round_increment => 5,
628 reset_all_budgets => 1,
631 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
632 cmp_ok($budget_hierarchy_cloned->[0]->{budget_amount}, '==', 0, "CloneBudgetPeriod reset all fund amounts");
634 # MoveOrders
635 my $number_orders_moved = C4::Budgets::MoveOrders();
636 is( $number_orders_moved, undef, 'MoveOrders return undef if no arg passed' );
637 $number_orders_moved =
638 C4::Budgets::MoveOrders( { from_budget_period_id => $budget_period_id } );
639 is( $number_orders_moved, undef,
640 'MoveOrders return undef if only 1 arg passed' );
641 $number_orders_moved =
642 C4::Budgets::MoveOrders( { to_budget_period_id => $budget_period_id } );
643 is( $number_orders_moved, undef,
644 'MoveOrders return undef if only 1 arg passed' );
645 $number_orders_moved = C4::Budgets::MoveOrders(
647 from_budget_period_id => $budget_period_id,
648 to_budget_period_id => $budget_period_id
651 is( $number_orders_moved, undef,
652 'MoveOrders return undef if 2 budget period id are the same' );
654 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
656 budget_period_id => $budget_period_id,
657 budget_period_startdate => '2014-01-01',
658 budget_period_enddate => '2014-12-31',
662 my $report = C4::Budgets::MoveOrders(
664 from_budget_period_id => $budget_period_id,
665 to_budget_period_id => $budget_period_id_cloned,
666 move_remaining_unspent => 1,
669 is( scalar( @$report ), 6 , "MoveOrders has processed 6 funds" );
671 my $number_of_orders_moved = 0;
672 $number_of_orders_moved += scalar( @{ $_->{orders_moved} } ) for @$report;
673 is( $number_of_orders_moved, $number_of_orders_to_move, "MoveOrders has moved $number_of_orders_to_move orders" );
675 my @new_budget_ids = map { $_->{budget_id} }
676 @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
677 my @old_budget_ids = map { $_->{budget_id} }
678 @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
679 for my $budget_id ( keys %budgets ) {
680 for my $ordernumber ( @{ $budgets{$budget_id} } ) {
681 my $budget = GetBudgetByOrderNumber($ordernumber);
682 my $is_in_new_budgets = grep /^$budget->{budget_id}$/, @new_budget_ids;
683 my $is_in_old_budgets = grep /^$budget->{budget_id}$/, @old_budget_ids;
684 is( $is_in_new_budgets, 1, "MoveOrders changed the budget_id for order $ordernumber" );
685 is( $is_in_old_budgets, 0, "MoveOrders changed the budget_id for order $ordernumber" );
690 # MoveOrders with param move_remaining_unspent
691 my @new_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
692 my @old_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
694 for my $new_budget ( @new_budgets ) {
695 my ( $old_budget ) = map { $_->{budget_code} eq $new_budget->{budget_code} ? $_ : () } @old_budgets;
696 my $new_budget_amount_should_be = $old_budget->{budget_amount} * 2 - $old_budget->{total_spent};
697 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})" );
700 # Test SetOwnerToFundHierarchy
702 my $patron_category = $builder->build({ source => 'Category' });
703 my $branchcode = $library->{branchcode};
704 my $john_doe = Koha::Patron->new({
705 cardnumber => '123456',
706 firstname => 'John',
707 surname => 'Doe',
708 categorycode => $patron_category->{categorycode},
709 branchcode => $branchcode,
710 dateofbirth => '',
711 dateexpiry => '9999-12-31',
712 userid => 'john.doe'
713 })->store->borrowernumber;
715 C4::Budgets::SetOwnerToFundHierarchy( $budget_id1, $john_doe );
716 is( C4::Budgets::GetBudget($budget_id1)->{budget_owner_id},
717 $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 1 ($budget_id1)" );
718 is( C4::Budgets::GetBudget($budget_id11)->{budget_owner_id},
719 $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 11 ($budget_id11)" );
720 is( C4::Budgets::GetBudget($budget_id111)->{budget_owner_id},
721 $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 111 ($budget_id111)" );
722 is( C4::Budgets::GetBudget($budget_id12)->{budget_owner_id},
723 $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 12 ($budget_id12 )" );
724 is( C4::Budgets::GetBudget($budget_id2)->{budget_owner_id},
725 undef, "SetOwnerToFundHierarchy should not have set an owner for budget 2 ($budget_id2)" );
726 is( C4::Budgets::GetBudget($budget_id21)->{budget_owner_id},
727 undef, "SetOwnerToFundHierarchy should not have set an owner for budget 21 ($budget_id21)" );
729 my $jane_doe = Koha::Patron->new({
730 cardnumber => '789012',
731 firstname => 'Jane',
732 surname => 'Doe',
733 categorycode => $patron_category->{categorycode},
734 branchcode => $branchcode,
735 dateofbirth => '',
736 dateexpiry => '9999-12-31',
737 userid => 'jane.doe'
738 })->store->borrowernumber;
740 C4::Budgets::SetOwnerToFundHierarchy( $budget_id11, $jane_doe );
741 is( C4::Budgets::GetBudget($budget_id1)->{budget_owner_id},
742 $john_doe, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 1 ($budget_id1)" );
743 is( C4::Budgets::GetBudget($budget_id11)->{budget_owner_id},
744 $jane_doe, "SetOwnerToFundHierarchy should have set John Doe $jane_doe for budget 11 ($budget_id11)" );
745 is( C4::Budgets::GetBudget($budget_id111)->{budget_owner_id},
746 $jane_doe, "SetOwnerToFundHierarchy should have set John Doe $jane_doe for budget 111 ($budget_id111)" );
747 is( C4::Budgets::GetBudget($budget_id12)->{budget_owner_id},
748 $john_doe, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 12 ($budget_id12 )" );
749 is( C4::Budgets::GetBudget($budget_id2)->{budget_owner_id},
750 undef, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 2 ($budget_id2)" );
751 is( C4::Budgets::GetBudget($budget_id21)->{budget_owner_id},
752 undef, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 21 ($budget_id21)" );
754 # Test GetBudgetAuthCats
756 my $budgetPeriodId = AddBudgetPeriod({
757 budget_period_startdate => '2008-01-01',
758 budget_period_enddate => '2008-12-31',
759 budget_period_description => 'just another budget',
760 budget_period_active => 0,
763 $budgets = GetBudgets();
764 my $i = 0;
765 for my $budget ( @$budgets )
767 $budget->{sort1_authcat} = "sort1_authcat_$i";
768 $budget->{sort2_authcat} = "sort2_authcat_$i";
769 $budget->{budget_period_id} = $budgetPeriodId;
770 ModBudget( $budget );
771 $i++;
774 my $authCat = GetBudgetAuthCats($budgetPeriodId);
776 is( scalar @{$authCat}, $i * 2, "GetBudgetAuthCats returns only non-empty sorting categories (no empty authCat in db)" );
778 $i = 0;
779 for my $budget ( @$budgets )
781 $budget->{sort1_authcat} = "sort_authcat_$i";
782 $budget->{sort2_authcat} = "sort_authcat_$i";
783 $budget->{budget_period_id} = $budgetPeriodId;
784 ModBudget( $budget );
785 $i++;
788 $authCat = GetBudgetAuthCats($budgetPeriodId);
789 is( scalar @$authCat, scalar @$budgets, "GetBudgetAuthCats returns distinct authCat" );
791 $i = 0;
792 for my $budget ( @$budgets )
794 $budget->{sort1_authcat} = "sort1_authcat_$i";
795 $budget->{sort2_authcat} = "";
796 $budget->{budget_period_id} = $budgetPeriodId;
797 ModBudget( $budget );
798 $i++;
801 $authCat = GetBudgetAuthCats($budgetPeriodId);
803 is( scalar @{$authCat}, $i, "GetBudgetAuthCats returns only non-empty sorting categories (empty sort2_authcat on all records)" );
805 $i = 0;
806 for my $budget ( @$budgets )
808 $budget->{sort1_authcat} = "";
809 $budget->{sort2_authcat} = "";
810 $budget->{budget_period_id} = $budgetPeriodId;
811 ModBudget( $budget );
812 $i++;
815 $authCat = GetBudgetAuthCats($budgetPeriodId);
817 is( scalar @{$authCat}, 0, "GetBudgetAuthCats returns only non-empty sorting categories (all empty)" );
819 # /Test GetBudgetAuthCats
821 subtest 'GetBudgetSpent and GetBudgetOrdered' => sub {
822 plan tests => 20;
824 my $budget_period = $builder->build({
825 source => 'Aqbudgetperiod',
826 value => {
827 budget_period_active => 1,
828 budget_total => 10000,
831 my $budget = $builder->build({
832 source => 'Aqbudget',
833 value => {
834 budget_amount => 1000,
835 budget_encumb => undef,
836 budget_expend => undef,
837 budget_period_id => $budget_period->{budget_period_id},
838 budget_parent_id => undef,
841 my $invoice = $builder->build({
842 source => 'Aqinvoice',
843 value => {
844 closedate => undef,
848 my $spent = GetBudgetSpent( $budget->{budget_id} );
849 my $ordered = GetBudgetOrdered( $budget->{budget_id} );
850 my $hierarchy = GetBudgetHierarchy($budget_period->{budget_period_id} );
852 is( $spent, 0, "New budget, no orders/invoices, should be nothing spent");
853 is( $ordered, 0, "New budget, no orders/invoices, should be nothing ordered");
854 is( @$hierarchy[0]->{total_spent},0,"New budgets, no orders/invoices, budget hierarchy shows 0 spent");
855 is( @$hierarchy[0]->{total_ordered},0,"New budgets, no orders/invoices, budget hierarchy shows 0 ordered");
857 my $inv_adj_1 = $builder->build({
858 source => 'AqinvoiceAdjustment',
859 value => {
860 invoiceid => $invoice->{invoiceid},
861 adjustment => 3,
862 encumber_open => 0,
863 budget_id => $budget->{budget_id},
867 $spent = GetBudgetSpent( $budget->{budget_id} );
868 $ordered = GetBudgetOrdered( $budget->{budget_id} );
869 $hierarchy = GetBudgetHierarchy($budget_period->{budget_period_id} );
870 is( $spent, 0, "After adding invoice adjustment on open invoice, should be nothing spent");
871 is( $ordered, 0, "After adding invoice adjustment on open invoice not encumbered, should be nothing ordered");
872 is( @$hierarchy[0]->{total_spent},0,"After adding invoice adjustment on open invoice, budget hierarchy shows 0 spent");
873 is( @$hierarchy[0]->{total_ordered},0,"After adding invoice adjustment on open invoice, budget hierarchy shows 0 ordered");
875 my $inv_adj_2 = $builder->build({
876 source => 'AqinvoiceAdjustment',
877 value => {
878 invoiceid => $invoice->{invoiceid},
879 adjustment => 3,
880 encumber_open => 1,
881 budget_id => $budget->{budget_id},
885 $spent = GetBudgetSpent( $budget->{budget_id} );
886 $ordered = GetBudgetOrdered( $budget->{budget_id} );
887 $hierarchy = GetBudgetHierarchy($budget_period->{budget_period_id} );
888 is( $spent, 0, "After adding invoice adjustment on open invoice, should be nothing spent");
889 is( $ordered, 3, "After adding invoice adjustment on open invoice encumbered, should be 3 ordered");
890 is( @$hierarchy[0]->{total_spent},0,"After adding invoice adjustment on open invoice encumbered, budget hierarchy shows 0 spent");
891 is( @$hierarchy[0]->{total_ordered},3,"After adding invoice adjustment on open invoice encumbered, budget hierarchy shows 3 ordered");
893 my $invoice_2 = $builder->build({
894 source => 'Aqinvoice',
895 value => {
896 closedate => '2017-07-01',
899 my $inv_adj_3 = $builder->build({
900 source => 'AqinvoiceAdjustment',
901 value => {
902 invoiceid => $invoice_2->{invoiceid},
903 adjustment => 3,
904 encumber_open => 0,
905 budget_id => $budget->{budget_id},
908 my $inv_adj_4 = $builder->build({
909 source => 'AqinvoiceAdjustment',
910 value => {
911 invoiceid => $invoice_2->{invoiceid},
912 adjustment => 3,
913 encumber_open => 1,
914 budget_id => $budget->{budget_id},
918 $spent = GetBudgetSpent( $budget->{budget_id} );
919 $ordered = GetBudgetOrdered( $budget->{budget_id} );
920 $hierarchy = GetBudgetHierarchy($budget_period->{budget_period_id} );
921 is( $spent, 6, "After adding invoice adjustment on closed invoice, should be 6 spent, encumber has no affect once closed");
922 is( $ordered, 3, "After adding invoice adjustment on closed invoice, should still be 3 ordered");
923 is( @$hierarchy[0]->{total_spent},6,"After adding invoice adjustment on closed invoice, budget hierarchy shows 6 spent");
924 is( @$hierarchy[0]->{total_ordered},3,"After adding invoice adjustment on closed invoice, budget hierarchy still shows 3 ordered");
926 my $budget0 = $builder->build({
927 source => 'Aqbudget',
928 value => {
929 budget_amount => 1000,
930 budget_encumb => undef,
931 budget_expend => undef,
932 budget_period_id => $budget_period->{budget_period_id},
933 budget_parent_id => $budget->{budget_id},
936 my $inv_adj_5 = $builder->build({
937 source => 'AqinvoiceAdjustment',
938 value => {
939 invoiceid => $invoice->{invoiceid},
940 adjustment => 3,
941 encumber_open => 1,
942 budget_id => $budget0->{budget_id},
945 my $inv_adj_6 = $builder->build({
946 source => 'AqinvoiceAdjustment',
947 value => {
948 invoiceid => $invoice_2->{invoiceid},
949 adjustment => 3,
950 encumber_open => 1,
951 budget_id => $budget0->{budget_id},
955 $spent = GetBudgetSpent( $budget->{budget_id} );
956 $ordered = GetBudgetOrdered( $budget->{budget_id} );
957 $hierarchy = GetBudgetHierarchy($budget_period->{budget_period_id} );
958 is( $spent, 6, "After adding invoice adjustment on a child budget should be 6 spent/budget unaffected");
959 is( $ordered, 3, "After adding invoice adjustment on a child budget, should still be 3 ordered/budget unaffected");
960 is( @$hierarchy[0]->{total_spent},9,"After adding invoice adjustment on child budget, budget hierarchy shows 9 spent");
961 is( @$hierarchy[0]->{total_ordered},6,"After adding invoice adjustment on child budget, budget hierarchy shows 6 ordered");
964 subtest 'GetBudgetSpent GetBudgetOrdered GetBudgetsPlanCell tests' => sub {
966 plan tests => 24;
968 #Let's build an order, we need a couple things though
969 t::lib::Mocks::mock_preference('OrderPriceRounding','nearest_cent');
971 my $spent_biblio = $builder->build({ source => 'Biblio' });
972 my $item_1 = $builder->build({ source => 'Item', value => { biblionumber => $spent_biblio->{biblionumber} } });
973 my $biblioitem_1 = $builder->build({ source => 'Biblioitem', value => { biblionumber => $spent_biblio->{biblionumber}, itemnumber => $item_1->{itemnumber} } });
974 my $spent_basket = $builder->build({ source => 'Aqbasket', value => { is_standing => 0 } });
975 my $spent_invoice = $builder->build({ source => 'Aqinvoice'});
976 my $spent_currency = $builder->build({ source => 'Currency', value => { active => 1, archived => 0, symbol => 'F', rate => 2, isocode => undef, currency => 'FOO' } });
977 my $spent_vendor = $builder->build({ source => 'Aqbookseller',value => { listincgst => 0, listprice => $spent_currency->{currency}, invoiceprice => $spent_currency->{currency} } });
978 my $budget_authcat = $builder->build({ source => 'AuthorisedValueCategory', value => {} });
979 my $spent_sort1 = $builder->build({ source => 'AuthorisedValue', value => {
980 category => $budget_authcat->{category_name},
981 authorised_value => 'PICKLE',
984 my $spent_budget_period = $builder->build({ source => 'Aqbudgetperiod', value => {
987 my $spent_budget = $builder->build({ source => 'Aqbudget', value => {
988 sort1_authcat => $budget_authcat->{category_name},
989 budget_period_id => $spent_budget_period->{budget_period_id},
990 budget_parent_id => undef,
993 my $spent_orderinfo = {
994 basketno => $spent_basket->{basketno},
995 booksellerid => $spent_vendor->{id},
996 rrp => 16.99,
997 discount => .42,
998 ecost => 16.91,
999 biblionumber => $spent_biblio->{biblionumber},
1000 currency => $spent_currency->{currency},
1001 tax_rate_on_ordering => 0,
1002 tax_value_on_ordering => 0,
1003 tax_rate_on_receiving => 0,
1004 tax_value_on_receiving => 0,
1005 quantity => 8,
1006 quantityreceived => 0,
1007 datecancellationprinted => undef,
1008 datereceived => undef,
1009 budget_id => $spent_budget->{budget_id},
1010 sort1 => $spent_sort1->{authorised_value},
1013 #Okay we have basically what the user would enter, now we do some maths
1015 $spent_orderinfo = C4::Acquisition::populate_order_with_prices({
1016 order => $spent_orderinfo,
1017 booksellerid => $spent_orderinfo->{booksellerid},
1018 ordering => 1,
1021 #And let's place the order
1023 my $spent_order = $builder->build({ source => 'Aqorder', value => $spent_orderinfo });
1024 t::lib::Mocks::mock_preference('OrderPriceRounding','');
1025 my $spent_ordered = GetBudgetOrdered( $spent_order->{budget_id} );
1027 is($spent_orderinfo->{ecost_tax_excluded}, 9.854200,'We store extra precision in price calculation');
1028 is( Koha::Number::Price->new($spent_orderinfo->{ecost_tax_excluded})->format(), 9.85,'But the price as formatted is two digits');
1029 is($spent_ordered,'78.8336',"We expect the ordered amount to be equal to the estimated price times quantity with full precision");
1031 t::lib::Mocks::mock_preference('OrderPriceRounding','nearest_cent');
1032 $spent_ordered = GetBudgetOrdered( $spent_order->{budget_id} );
1033 is($spent_ordered,'78.8',"We expect the ordered amount to be equal to the estimated price rounded times quantity");
1035 #Test GetBudgetHierarchy for rounding
1036 t::lib::Mocks::mock_preference('OrderPriceRounding','');
1037 my $gbh = GetBudgetHierarchy($spent_budget->{budget_period_id});
1038 is ( @$gbh[0]->{budget_spent}+0, 0, "We expect this to be an exact order cost * quantity");
1039 is ( @$gbh[0]->{budget_ordered}+0, 78.8336, "We expect this to be an exact order cost * quantity");
1040 t::lib::Mocks::mock_preference('OrderPriceRounding','nearest_cent');
1041 $gbh = GetBudgetHierarchy($spent_budget->{budget_period_id});
1042 is ( @$gbh[0]->{budget_spent}+0, 0, "We expect this to be an rounded order cost * quantity");
1043 is ( @$gbh[0]->{budget_ordered}+0, 78.8, "We expect this to be an exact order cost * quantity");
1045 #Let's test some budget planning
1046 #Regression tests for bug 18736
1047 #We need an item to test by BRANCHES
1048 my $order_item_1 = $builder->build({ source => 'AqordersItem', value => { ordernumber => $spent_order->{ordernumber}, itemnumber => $item_1->{itemnumber} } });
1049 my $spent_fund = Koha::Acquisition::Funds->find( $spent_order->{budget_id} );
1050 my $cell = {
1051 authcat => 'MONTHS',
1052 cell_authvalue => $spent_order->{entrydate}, #normally this is just the year/month but full won't hurt us here
1053 budget_id => $spent_order->{budget_id},
1054 budget_period_id => $spent_fund->budget_period_id,
1055 sort1_authcat => $spent_order->{sort1_authcat},
1056 sort2_authcat => $spent_order->{sort1_authcat},
1058 my $test_values = {
1059 'MONTHS' => {
1060 authvalue => $spent_order->{entrydate},
1061 expected_rounded => 9.85,
1062 expected_exact => 9.8542,
1064 'BRANCHES' => {
1065 authvalue => $item_1->{homebranch},
1066 expected_rounded => 9.85,
1067 expected_exact => 9.8542,
1069 'ITEMTYPES' => {
1070 authvalue => $biblioitem_1->{itemtype},
1071 expected_rounded => 78.80,
1072 expected_exact => 78.8336,
1074 'ELSE' => {
1075 authvalue => $spent_sort1->{authorised_value},
1076 expected_rounded => 78.8,
1077 expected_exact => 78.8336,
1081 for my $authcat ( keys %$test_values ) {
1082 my $test_val = $test_values->{$authcat};
1083 my $authvalue = $test_val->{authvalue};
1084 my $expected_rounded = $test_val->{expected_rounded};
1085 my $expected_exact = $test_val->{expected_exact};
1086 $cell->{authcat} = $authcat;
1087 $cell->{authvalue} = $authvalue;
1088 t::lib::Mocks::mock_preference('OrderPriceRounding','');
1089 my ( $actual ) = GetBudgetsPlanCell( $cell, undef, $spent_budget); #we are only testing the actual for now
1090 is ( $actual+0, $expected_exact, "We expect this to be an exact order cost ($authcat)"); #really we should expect cost*quantity but we don't
1091 t::lib::Mocks::mock_preference('OrderPriceRounding','nearest_cent');
1092 ( $actual ) = GetBudgetsPlanCell( $cell, undef, $spent_budget); #we are only testing the actual for now
1093 is ( $actual+0, $expected_rounded, "We expect this to be a rounded order cost ($authcat)"); #really we should expect cost*quantity but we don't
1096 #Okay, now we can receive the order, giving the price as the user would
1098 $spent_orderinfo->{unitprice} = 9.85; #we are paying what we expected
1100 #Do our maths
1102 $spent_orderinfo = C4::Acquisition::populate_order_with_prices({
1103 order => $spent_orderinfo,
1104 booksellerid => $spent_orderinfo->{booksellerid},
1105 receiving => 1,
1107 my $received_order = $builder->build({ source => 'Aqorder', value => $spent_orderinfo });
1109 #And receive a copy of the order so we have both spent and ordered values
1111 ModReceiveOrder({
1112 biblionumber => $spent_order->{biblionumber},
1113 order => $received_order,
1114 invoice => $spent_invoice,
1115 quantityreceived => $spent_order->{quantity},
1116 budget_id => $spent_order->{budget_id},
1117 received_items => [],
1120 t::lib::Mocks::mock_preference('OrderPriceRounding','');
1121 my $spent_spent = GetBudgetSpent( $spent_order->{budget_id} );
1122 is($spent_orderinfo->{unitprice_tax_excluded}, 9.854200,'We store extra precision in price calculation');
1123 is( Koha::Number::Price->new($spent_orderinfo->{unitprice_tax_excluded})->format(), 9.85,'But the price as formatted is two digits');
1124 is($spent_spent,'78.8336',"We expect the spent amount to be equal to the estimated price times quantity with full precision");
1126 t::lib::Mocks::mock_preference('OrderPriceRounding','nearest_cent');
1127 $spent_spent = GetBudgetSpent( $spent_order->{budget_id} );
1128 is($spent_spent,'78.8',"We expect the spent amount to be equal to the estimated price rounded times quantity");
1130 #Test GetBudgetHierarchy for rounding
1131 t::lib::Mocks::mock_preference('OrderPriceRounding','');
1132 $gbh = GetBudgetHierarchy($spent_budget->{budget_period_id});
1133 is ( @$gbh[0]->{budget_spent}, 78.8336, "We expect this to be an exact order cost * quantity");
1134 is ( @$gbh[0]->{budget_ordered}, 78.8336, "We expect this to be an exact order cost * quantity");
1135 t::lib::Mocks::mock_preference('OrderPriceRounding','nearest_cent');
1136 $gbh = GetBudgetHierarchy($spent_budget->{budget_period_id});
1137 is ( @$gbh[0]->{budget_spent}+0, 78.8, "We expect this to be a rounded order cost * quantity");
1138 is ( @$gbh[0]->{budget_ordered}, 78.8, "We expect this to be a rounded order cost * quantity");
1142 sub _get_dependencies {
1143 my ($budget_hierarchy) = @_;
1144 my $graph;
1145 for my $budget (@$budget_hierarchy) {
1146 if ( $budget->{child} ) {
1147 my @sorted = sort @{ $budget->{child} };
1148 for my $child_id (@sorted) {
1149 push @{ $graph->{ $budget->{budget_name} }{children} },
1150 _get_budgetname_by_id( $budget_hierarchy, $child_id );
1153 push @{ $graph->{ $budget->{budget_name} }{parents} },
1154 $budget->{parent_id};
1156 return $graph;
1159 sub _get_budgetname_by_id {
1160 my ( $budgets, $budget_id ) = @_;
1161 my ($budget_name) =
1162 map { ( $_->{budget_id} eq $budget_id ) ? $_->{budget_name} : () }
1163 @$budgets;
1164 return $budget_name;