3 use Test::More tests => 146;
12 use Koha::Acquisition::Booksellers;
13 use Koha::Acquisition::Orders;
14 use Koha::Acquisition::Funds;
16 use Koha::Number::Price;
18 use t::lib::TestBuilder;
25 t::lib::Mocks::mock_preference('OrderPriceRounding','');
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|);
34 my $library = $builder->build({
38 t::lib::Mocks::mock_userenv(
41 userid => 'my_userid',
42 branch => $library->{branchcode},
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',
55 is( $bpid, undef, 'AddBugetPeriod without end date returns undef' );
56 $bpid = AddBudgetPeriod({
57 budget_period_enddate => '2008-12-31',
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' );
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' );
78 is( GetBudgetPeriod(0), undef ,'GetBudgetPeriod(0) returned undef : noactive 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,
87 my $mod_status = ModBudgetPeriod($my_budgetperiod);
88 is( $mod_status, undef, 'ModBudgetPeriod without id returns undef' );
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' );
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' );
109 is( DelBudgetPeriod($bpid), 1, 'DelBudgetPeriod returns true' );
110 $budgetperiods = GetBudgetPeriods();
111 is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
118 # The budget hierarchy will be:
126 is( AddBudget(), undef, 'AddBuget without argument returns undef' );
127 my $budgets = GetBudgets();
128 is( @$budgets, 0, 'GetBudgets returns the correct number of budgets' );
130 $bpid = AddBudgetPeriod($my_budgetperiod); #this is an active 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
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' );
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,
157 $mod_status = ModBudget($my_budget);
158 is( $mod_status, undef, 'ModBudget without id returns undef' );
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' );
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' );
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');
188 my $budget_name = GetBudgetName( $budget_id );
189 is($budget_name, $my_budget->{budget_name}, "Test the GetBudgetName routine");
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,
197 my $bpid_i = AddBudgetPeriod($my_inactive_budgetperiod); #this is an inactive budget
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,
206 my $budget_id_inactive = AddBudget($my_budget_inactive);
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");
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,
220 isnt( $second_budget_id, undef, 'AddBudget does not returns undef' );
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' );
225 is( DelBudget($budget_id), 1, 'DelBudget returns true' );
226 $budgets = GetBudgets();
227 is( @$budgets, 2, 'GetBudgets returns the correct number of budget periods' );
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;
238 my $budget_period_id = AddBudgetPeriod(
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,
247 my $budget_id1 = AddBudget(
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,
256 my $budget_id2 = AddBudget(
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,
265 my $budget_id12 = AddBudget(
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,
274 my $budget_id11 = AddBudget(
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,
283 my $budget_id111 = AddBudget(
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,
293 my $budget_id21 = AddBudget(
295 budget_code => 'budget_21',
296 budget_name => 'budget_21',
297 budget_period_id => $budget_period_id,
298 budget_parent_id => $budget_id2,
302 my $bookseller = Koha::Acquisition::Bookseller->new(
305 address1 => "bookseller's address",
311 my $booksellerid = $bookseller->id;
313 my $basketno = C4::Acquisition::NewBasket( $booksellerid, 1 );
314 my ( $biblionumber, $biblioitemnumber ) =
315 C4::Biblio::AddBiblio( MARC::Record->new, '' );
319 budget_id => $budget_id1,
320 pending_quantity => 1,
324 budget_id => $budget_id2,
325 pending_quantity => 2,
329 budget_id => $budget_id11,
330 pending_quantity => 3,
334 budget_id => $budget_id12,
335 pending_quantity => 4,
339 budget_id => $budget_id111,
340 pending_quantity => 2,
344 # No order for budget_21
349 my $invoiceid = AddInvoice(invoicenumber => 'invoice_test_clone', booksellerid => $booksellerid, unknown => "unknown");
350 my $invoice = GetInvoice( $invoiceid );
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(
358 basketno => $basketno,
359 biblionumber => $biblionumber,
360 budget_id => $infos->{budget_id},
361 order_internalnote => "internal note",
362 order_vendornote => "vendor note",
364 cost_tax_included => $item_price,
365 rrp_tax_included => $item_price,
366 listprice => $item_price,
367 ecost_tax_include => $item_price,
372 my $ordernumber = $order->ordernumber;
373 push @{ $budgets{$infos->{budget_id}} }, $ordernumber;
374 $number_of_orders_to_move++;
376 for ( 1 .. $infos->{spent_quantity} ) {
377 my $order = Koha::Acquisition::Order->new(
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,
386 rrp_tax_included => $item_price,
387 listprice => $item_price,
388 ecost_tax_included => $item_price,
393 my $ordernumber = $order->ordernumber;
395 biblionumber => $biblionumber,
396 order => $order->unblessed,
397 budget_id => $infos->{budget_id},
398 quantityreceived => $item_quantity,
400 received_items => [],
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" );
408 # GetBudgetSpent and GetBudgetOrdered
409 my $budget_period_amount = 100;
410 my $budget_amount = 50;
412 $budget = AddBudgetPeriod(
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,
422 my $fund = AddBudget(
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,
432 my $vendor = Koha::Acquisition::Bookseller->new(
434 name => "test vendor",
435 address1 => "test address",
442 my $vendorid = $vendor->id;
444 my $basketnumber = C4::Acquisition::NewBasket( $vendorid, 1 );
445 my ( $biblio, $biblioitem ) = C4::Biblio::AddBiblio( MARC::Record->new, '' );
450 pending_quantity => 1,
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;
459 my $order = Koha::Acquisition::Order->new(
461 basketno => $basketnumber,
462 biblionumber => $biblio,
464 order_internalnote => "internalnote",
465 order_vendornote => "vendor note",
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,
477 bibionumber => $biblio,
478 order => $order->unblessed,
480 quantityreceived => 2,
481 invoice => $test_invoice,
482 received_items => [],
484 t::lib::Mocks::mock_preference('OrderPriceRounding','');
486 is ( GetBudgetSpent( $fund ), 6, "total shipping cost is 6");
487 is ( GetBudgetOrdered( $fund ), '20', "total ordered price is 20");
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;
497 my $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
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',
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.');
509 my $budget_cloned = C4::Budgets::GetBudgets({ budget_period_id => $budget_period_id_cloned });
510 my $budget_time = $budget_cloned->[0]->{timestamp};
512 isnt($budget_time, '1999-12-31 23:59:59', "New budget has an updated timestamp");
516 my $budget_hierarchy = GetBudgetHierarchy($budget_period_id);
517 my $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
520 scalar(@$budget_hierarchy_cloned),
521 scalar(@$budget_hierarchy),
522 'CloneBudgetPeriod clones the same number of budgets (funds)'
525 _get_dependencies($budget_hierarchy),
526 _get_dependencies($budget_hierarchy_cloned),
527 'CloneBudgetPeriod keeps the same dependencies order'
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'
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;
541 is( $number_of_budgets_not_reset, 5,
542 'CloneBudgetPeriod does not reset budgets (funds) if not needed' );
544 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
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,
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);
564 is( scalar(@$budget_hierarchy_cloned), scalar(@$budget_hierarchy),
565 'CloneBudgetPeriod (with inactive param) clones the same number of budgets (funds)'
568 _get_dependencies($budget_hierarchy),
569 _get_dependencies($budget_hierarchy_cloned),
570 'CloneBudgetPeriod (with inactive param) keeps the same dependencies order'
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'
577 # CloneBudgetPeriod with param reset_all_budgets
578 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
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,
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;
592 is( $number_of_budgets_not_reset, 0,
593 'CloneBudgetPeriod has reset all budgets (funds)' );
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);
605 # CloneBudgetPeriod with param amount_change_*
606 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
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,
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");
626 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
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,
636 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
637 cmp_ok($budget_hierarchy_cloned->[0]->{budget_amount}, '==', 0, "CloneBudgetPeriod reset all fund amounts");
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(
652 from_budget_period_id => $budget_period_id,
653 to_budget_period_id => $budget_period_id
656 is( $number_orders_moved, undef,
657 'MoveOrders return undef if 2 budget period id are the same' );
659 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
661 budget_period_id => $budget_period_id,
662 budget_period_startdate => '2014-01-01',
663 budget_period_enddate => '2014-12-31',
667 my $report = C4::Budgets::MoveOrders(
669 from_budget_period_id => $budget_period_id,
670 to_budget_period_id => $budget_period_id_cloned,
671 move_remaining_unspent => 1,
674 is( scalar( @$report ), 6 , "MoveOrders has processed 6 funds" );
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" );
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" );
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) };
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})" );
705 # Test SetOwnerToFundHierarchy
707 my $patron_category = $builder->build({ source => 'Category' });
708 my $branchcode = $library->{branchcode};
709 my $john_doe = Koha::Patron->new({
710 cardnumber => '123456',
713 categorycode => $patron_category->{categorycode},
714 branchcode => $branchcode,
716 dateexpiry => '9999-12-31',
718 })->store->borrowernumber;
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)" );
734 my $jane_doe = Koha::Patron->new({
735 cardnumber => '789012',
738 categorycode => $patron_category->{categorycode},
739 branchcode => $branchcode,
741 dateexpiry => '9999-12-31',
743 })->store->borrowernumber;
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)" );
759 # Test GetBudgetAuthCats
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,
768 $budgets = GetBudgets();
770 for my $budget ( @$budgets )
772 $budget->{sort1_authcat} = "sort1_authcat_$i";
773 $budget->{sort2_authcat} = "sort2_authcat_$i";
774 $budget->{budget_period_id} = $budgetPeriodId;
775 ModBudget( $budget );
779 my $authCat = GetBudgetAuthCats($budgetPeriodId);
781 is( scalar @{$authCat}, $i * 2, "GetBudgetAuthCats returns only non-empty sorting categories (no empty authCat in db)" );
784 for my $budget ( @$budgets )
786 $budget->{sort1_authcat} = "sort_authcat_$i";
787 $budget->{sort2_authcat} = "sort_authcat_$i";
788 $budget->{budget_period_id} = $budgetPeriodId;
789 ModBudget( $budget );
793 $authCat = GetBudgetAuthCats($budgetPeriodId);
794 is( scalar @$authCat, scalar @$budgets, "GetBudgetAuthCats returns distinct authCat" );
797 for my $budget ( @$budgets )
799 $budget->{sort1_authcat} = "sort1_authcat_$i";
800 $budget->{sort2_authcat} = "";
801 $budget->{budget_period_id} = $budgetPeriodId;
802 ModBudget( $budget );
806 $authCat = GetBudgetAuthCats($budgetPeriodId);
808 is( scalar @{$authCat}, $i, "GetBudgetAuthCats returns only non-empty sorting categories (empty sort2_authcat on all records)" );
811 for my $budget ( @$budgets )
813 $budget->{sort1_authcat} = "";
814 $budget->{sort2_authcat} = "";
815 $budget->{budget_period_id} = $budgetPeriodId;
816 ModBudget( $budget );
820 $authCat = GetBudgetAuthCats($budgetPeriodId);
822 is( scalar @{$authCat}, 0, "GetBudgetAuthCats returns only non-empty sorting categories (all empty)" );
824 # /Test GetBudgetAuthCats
826 subtest 'GetBudgetSpent and GetBudgetOrdered' => sub {
829 my $budget_period = $builder->build({
830 source => 'Aqbudgetperiod',
832 budget_period_active => 1,
833 budget_total => 10000,
836 my $budget = $builder->build({
837 source => 'Aqbudget',
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,
846 my $invoice = $builder->build({
847 source => 'Aqinvoice',
853 my $spent = GetBudgetSpent( $budget->{budget_id} );
854 my $ordered = GetBudgetOrdered( $budget->{budget_id} );
855 my $hierarchy = GetBudgetHierarchy($budget_period->{budget_period_id} );
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");
862 my $inv_adj_1 = $builder->build({
863 source => 'AqinvoiceAdjustment',
865 invoiceid => $invoice->{invoiceid},
868 budget_id => $budget->{budget_id},
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");
880 my $inv_adj_2 = $builder->build({
881 source => 'AqinvoiceAdjustment',
883 invoiceid => $invoice->{invoiceid},
886 budget_id => $budget->{budget_id},
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");
898 my $invoice_2 = $builder->build({
899 source => 'Aqinvoice',
901 closedate => '2017-07-01',
904 my $inv_adj_3 = $builder->build({
905 source => 'AqinvoiceAdjustment',
907 invoiceid => $invoice_2->{invoiceid},
910 budget_id => $budget->{budget_id},
913 my $inv_adj_4 = $builder->build({
914 source => 'AqinvoiceAdjustment',
916 invoiceid => $invoice_2->{invoiceid},
919 budget_id => $budget->{budget_id},
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");
931 my $budget0 = $builder->build({
932 source => 'Aqbudget',
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},
941 my $inv_adj_5 = $builder->build({
942 source => 'AqinvoiceAdjustment',
944 invoiceid => $invoice->{invoiceid},
947 budget_id => $budget0->{budget_id},
950 my $inv_adj_6 = $builder->build({
951 source => 'AqinvoiceAdjustment',
953 invoiceid => $invoice_2->{invoiceid},
956 budget_id => $budget0->{budget_id},
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");
969 subtest 'OrderPriceRounding GetBudgetSpent GetBudgetOrdered tests' => sub {
973 #Let's build an order, we need a couple things though
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},
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,
993 quantityreceived => 0,
994 datecancellationprinted => undef,
995 datereceived => undef,
998 #Okay we have basically what the user would enter, now we do some maths
1000 $spent_orderinfo = C4::Acquisition::populate_order_with_prices({
1001 order => $spent_orderinfo,
1002 booksellerid => $spent_orderinfo->{booksellerid},
1006 #And let's place the order
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} );
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");
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");
1020 #Okay, now we can receive the order, giving the price as the user would
1022 $spent_orderinfo->{unitprice} = 9.85; #we are paying what we expected
1026 $spent_orderinfo = C4::Acquisition::populate_order_with_prices({
1027 order => $spent_orderinfo,
1028 booksellerid => $spent_orderinfo->{booksellerid},
1031 my $received_order = $builder->build({ source => 'Aqorder', value => $spent_orderinfo });
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 => [],
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");
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");
1056 sub _get_dependencies {
1057 my ($budget_hierarchy) = @_;
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 );
1067 push @{ $graph->{ $budget->{budget_name} }{parents} },
1068 $budget->{parent_id};
1073 sub _get_budgetname_by_id {
1074 my ( $budgets, $budget_id ) = @_;
1076 map { ( $_->{budget_id} eq $budget_id ) ? $_->{budget_name} : () }
1078 return $budget_name;