3 use Test::More tests => 146;
12 use Koha::Acquisition::Booksellers;
13 use Koha::Acquisition::Orders;
14 use Koha::Acquisition::Funds;
17 use t::lib::TestBuilder;
22 my $schema = Koha::Database->new->schema;
23 $schema->storage->txn_begin;
24 my $builder = t::lib::TestBuilder->new;
25 my $dbh = C4::Context->dbh;
26 $dbh->do(q|DELETE FROM aqbudgetperiods|);
27 $dbh->do(q|DELETE FROM aqbudgets|);
29 my $library = $builder->build({
34 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
36 *C4::Context::userenv = \&Mock_userenv;
37 $userenv = { flags => 1, id => 'my_userid', branch => $library->{branchcode} };
43 is( AddBudgetPeriod(), undef, 'AddBugetPeriod without argument returns undef' );
44 is( AddBudgetPeriod( { } ), undef, 'AddBugetPeriod with an empty argument returns undef' );
45 my $bpid = AddBudgetPeriod({
46 budget_period_startdate => '2008-01-01',
48 is( $bpid, undef, 'AddBugetPeriod without end date returns undef' );
49 $bpid = AddBudgetPeriod({
50 budget_period_enddate => '2008-12-31',
52 is( $bpid, undef, 'AddBugetPeriod without start date returns undef' );
53 is( GetBudgetPeriod(0), undef ,'GetBudgetPeriod(0) returned undef : noactive BudgetPeriod' );
54 my $budgetperiods = GetBudgetPeriods();
55 is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
57 my $my_budgetperiod = {
58 budget_period_startdate => '2008-01-01',
59 budget_period_enddate => '2008-12-31',
60 budget_period_description => 'MAPERI',
61 budget_period_active => 0,
63 $bpid = AddBudgetPeriod($my_budgetperiod);
64 isnt( $bpid, undef, 'AddBugetPeriod does not returns undef' );
65 my $budgetperiod = GetBudgetPeriod($bpid);
66 is( $budgetperiod->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'AddBudgetPeriod stores the start date correctly' );
67 is( $budgetperiod->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'AddBudgetPeriod stores the end date correctly' );
68 is( $budgetperiod->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'AddBudgetPeriod stores the description correctly' );
69 is( $budgetperiod->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'AddBudgetPeriod stores active correctly' );
70 is( GetBudgetPeriod(0), undef ,'GetBudgetPeriod(0) returned undef : noactive BudgetPeriod' );
74 budget_period_startdate => '2009-01-01',
75 budget_period_enddate => '2009-12-31',
76 budget_period_description => 'MODIF_MAPERI',
77 budget_period_active => 1,
79 my $mod_status = ModBudgetPeriod($my_budgetperiod);
80 is( $mod_status, undef, 'ModBudgetPeriod without id returns undef' );
82 $my_budgetperiod->{budget_period_id} = $bpid;
83 $mod_status = ModBudgetPeriod($my_budgetperiod);
84 is( $mod_status, 1, 'ModBudgetPeriod returnis true' );
85 $budgetperiod = GetBudgetPeriod($bpid);
86 is( $budgetperiod->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'ModBudgetPeriod updates the start date correctly' );
87 is( $budgetperiod->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'ModBudgetPeriod updates the end date correctly' );
88 is( $budgetperiod->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'ModBudgetPeriod updates the description correctly' );
89 is( $budgetperiod->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'ModBudgetPeriod upates active correctly' );
90 isnt( GetBudgetPeriod(0), undef, 'GetBugetPeriods functions correctly' );
93 $budgetperiods = GetBudgetPeriods();
94 is( @$budgetperiods, 1, 'GetBudgetPeriods returns the correct number of budget periods' );
95 is( $budgetperiods->[0]->{budget_period_id}, $my_budgetperiod->{budget_period_id}, 'GetBudgetPeriods returns the id correctly' );
96 is( $budgetperiods->[0]->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'GetBudgetPeriods returns the start date correctly' );
97 is( $budgetperiods->[0]->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'GetBudgetPeriods returns the end date correctly' );
98 is( $budgetperiods->[0]->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'GetBudgetPeriods returns the description correctly' );
99 is( $budgetperiods->[0]->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'GetBudgetPeriods returns active correctly' );
101 is( DelBudgetPeriod($bpid), 1, 'DelBudgetPeriod returns true' );
102 $budgetperiods = GetBudgetPeriods();
103 is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
110 # The budget hierarchy will be:
118 is( AddBudget(), undef, 'AddBuget without argument returns undef' );
119 my $budgets = GetBudgets();
120 is( @$budgets, 0, 'GetBudgets returns the correct number of budgets' );
122 $bpid = AddBudgetPeriod($my_budgetperiod); #this is an active budget
125 budget_code => 'ABCD',
126 budget_amount => '123.132000',
127 budget_name => 'Periodiques',
128 budget_notes => 'This is a note',
129 budget_period_id => $bpid,
131 my $budget_id = AddBudget($my_budget);
132 isnt( $budget_id, undef, 'AddBudget does not returns undef' );
133 my $budget = GetBudget($budget_id);
134 is( $budget->{budget_code}, $my_budget->{budget_code}, 'AddBudget stores the budget code correctly' );
135 is( $budget->{budget_amount}, $my_budget->{budget_amount}, 'AddBudget stores the budget amount correctly' );
136 is( $budget->{budget_name}, $my_budget->{budget_name}, 'AddBudget stores the budget name correctly' );
137 is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'AddBudget stores the budget notes correctly' );
138 is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'AddBudget stores the budget period id correctly' );
142 budget_code => 'EFG',
143 budget_amount => '321.231000',
144 budget_name => 'Modified name',
145 budget_notes => 'This is a modified note',
146 budget_period_id => $bpid,
148 $mod_status = ModBudget($my_budget);
149 is( $mod_status, undef, 'ModBudget without id returns undef' );
151 $my_budget->{budget_id} = $budget_id;
152 $mod_status = ModBudget($my_budget);
153 is( $mod_status, 1, 'ModBudget returns true' );
154 $budget = GetBudget($budget_id);
155 is( $budget->{budget_code}, $my_budget->{budget_code}, 'ModBudget updates the budget code correctly' );
156 is( $budget->{budget_amount}, $my_budget->{budget_amount}, 'ModBudget updates the budget amount correctly' );
157 is( $budget->{budget_name}, $my_budget->{budget_name}, 'ModBudget updates the budget name correctly' );
158 is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'ModBudget updates the budget notes correctly' );
159 is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'ModBudget updates the budget period id correctly' );
162 $budgets = GetBudgets();
163 is( @$budgets, 1, 'GetBudgets returns the correct number of budgets' );
164 is( $budgets->[0]->{budget_id}, $my_budget->{budget_id}, 'GetBudgets returns the budget id correctly' );
165 is( $budgets->[0]->{budget_code}, $my_budget->{budget_code}, 'GetBudgets returns the budget code correctly' );
166 is( $budgets->[0]->{budget_amount}, $my_budget->{budget_amount}, 'GetBudgets returns the budget amount correctly' );
167 is( $budgets->[0]->{budget_name}, $my_budget->{budget_name}, 'GetBudgets returns the budget name correctly' );
168 is( $budgets->[0]->{budget_notes}, $my_budget->{budget_notes}, 'GetBudgets returns the budget notes correctly' );
169 is( $budgets->[0]->{budget_period_id}, $my_budget->{budget_period_id}, 'GetBudgets returns the budget period id correctly' );
171 $budgets = GetBudgets( {budget_period_id => $bpid} );
172 is( @$budgets, 1, 'GetBudgets With Filter OK' );
173 $budgets = GetBudgets( {budget_period_id => $bpid}, {-asc => "budget_name"} );
174 is( @$budgets, 1, 'GetBudgets With Order OK' );
175 $budgets = GetBudgets( {budget_period_id => GetBudgetPeriod($bpid)->{budget_period_id}}, {-asc => "budget_name"} );
176 is( @$budgets, 1, 'GetBudgets With Order Getting Active budgetPeriod OK');
179 my $budget_name = GetBudgetName( $budget_id );
180 is($budget_name, $my_budget->{budget_name}, "Test the GetBudgetName routine");
182 my $my_inactive_budgetperiod = { #let's add an inactive
183 budget_period_startdate => '2010-01-01',
184 budget_period_enddate => '2010-12-31',
185 budget_period_description => 'MODIF_MAPERI',
186 budget_period_active => 0,
188 my $bpid_i = AddBudgetPeriod($my_inactive_budgetperiod); #this is an inactive budget
190 my $my_budget_inactive = {
191 budget_code => 'EFG',
192 budget_amount => '123.132000',
193 budget_name => 'Periodiques',
194 budget_notes => 'This is a note',
195 budget_period_id => $bpid_i,
197 my $budget_id_inactive = AddBudget($my_budget_inactive);
199 my $budget_code = $my_budget->{budget_code};
200 my $budget_by_code = GetBudgetByCode( $budget_code );
201 is($budget_by_code->{budget_id}, $budget_id, "GetBudgetByCode, check id"); #this should match the active budget, not the inactive
202 is($budget_by_code->{budget_notes}, $my_budget->{budget_notes}, "GetBudgetByCode, check notes");
204 my $second_budget_id = AddBudget({
205 budget_code => "ZZZZ",
206 budget_amount => "500.00",
207 budget_name => "Art",
208 budget_notes => "This is a note",
209 budget_period_id => $bpid,
211 isnt( $second_budget_id, undef, 'AddBudget does not returns undef' );
213 $budgets = GetBudgets( {budget_period_id => $bpid} );
214 ok( $budgets->[0]->{budget_name} lt $budgets->[1]->{budget_name}, 'default sort order for GetBudgets is by name' );
216 is( DelBudget($budget_id), 1, 'DelBudget returns true' );
217 $budgets = GetBudgets();
218 is( @$budgets, 2, 'GetBudgets returns the correct number of budget periods' );
221 # GetBudgetHierarchySpent and GetBudgetHierarchyOrdered
222 my $budget_period_total = 10_000;
223 my $budget_1_total = 1_000;
224 my $budget_11_total = 100;
225 my $budget_111_total = 50;
226 my $budget_12_total = 100;
227 my $budget_2_total = 2_000;
229 my $budget_period_id = AddBudgetPeriod(
231 budget_period_startdate => '2013-01-01',
232 budget_period_enddate => '2014-12-31',
233 budget_period_description => 'Budget Period',
234 budget_period_active => 1,
235 budget_period_total => $budget_period_total,
238 my $budget_id1 = AddBudget(
240 budget_code => 'budget_1',
241 budget_name => 'budget_1',
242 budget_period_id => $budget_period_id,
243 budget_parent_id => undef,
244 budget_amount => $budget_1_total,
247 my $budget_id2 = AddBudget(
249 budget_code => 'budget_2',
250 budget_name => 'budget_2',
251 budget_period_id => $budget_period_id,
252 budget_parent_id => undef,
253 budget_amount => $budget_2_total,
256 my $budget_id12 = AddBudget(
258 budget_code => 'budget_12',
259 budget_name => 'budget_12',
260 budget_period_id => $budget_period_id,
261 budget_parent_id => $budget_id1,
262 budget_amount => $budget_12_total,
265 my $budget_id11 = AddBudget(
267 budget_code => 'budget_11',
268 budget_name => 'budget_11',
269 budget_period_id => $budget_period_id,
270 budget_parent_id => $budget_id1,
271 budget_amount => $budget_11_total,
274 my $budget_id111 = AddBudget(
276 budget_code => 'budget_111',
277 budget_name => 'budget_111',
278 budget_period_id => $budget_period_id,
279 budget_parent_id => $budget_id11,
280 budget_owner_id => 1,
281 budget_amount => $budget_111_total,
284 my $budget_id21 = AddBudget(
286 budget_code => 'budget_21',
287 budget_name => 'budget_21',
288 budget_period_id => $budget_period_id,
289 budget_parent_id => $budget_id2,
293 my $bookseller = Koha::Acquisition::Bookseller->new(
296 address1 => "bookseller's address",
302 my $booksellerid = $bookseller->id;
304 my $basketno = C4::Acquisition::NewBasket( $booksellerid, 1 );
305 my ( $biblionumber, $biblioitemnumber ) =
306 C4::Biblio::AddBiblio( MARC::Record->new, '' );
310 budget_id => $budget_id1,
311 pending_quantity => 1,
315 budget_id => $budget_id2,
316 pending_quantity => 2,
320 budget_id => $budget_id11,
321 pending_quantity => 3,
325 budget_id => $budget_id12,
326 pending_quantity => 4,
330 budget_id => $budget_id111,
331 pending_quantity => 2,
335 # No order for budget_21
340 my $invoiceid = AddInvoice(invoicenumber => 'invoice_test_clone', booksellerid => $booksellerid, unknown => "unknown");
341 my $invoice = GetInvoice( $invoiceid );
343 my $item_quantity = 2;
344 my $number_of_orders_to_move = 0;
345 for my $infos (@order_infos) {
346 for ( 1 .. $infos->{pending_quantity} ) {
347 my $order = Koha::Acquisition::Order->new(
349 basketno => $basketno,
350 biblionumber => $biblionumber,
351 budget_id => $infos->{budget_id},
352 order_internalnote => "internal note",
353 order_vendornote => "vendor note",
355 cost_tax_included => $item_price,
356 rrp_tax_included => $item_price,
357 listprice => $item_price,
358 ecost_tax_include => $item_price,
363 my $ordernumber = $order->ordernumber;
364 push @{ $budgets{$infos->{budget_id}} }, $ordernumber;
365 $number_of_orders_to_move++;
367 for ( 1 .. $infos->{spent_quantity} ) {
368 my $order = Koha::Acquisition::Order->new(
370 basketno => $basketno,
371 biblionumber => $biblionumber,
372 budget_id => $infos->{budget_id},
373 order_internalnote => "internal note",
374 order_vendornote => "vendor note",
375 quantity => $item_quantity,
377 rrp_tax_included => $item_price,
378 listprice => $item_price,
379 ecost_tax_included => $item_price,
384 my $ordernumber = $order->ordernumber;
386 biblionumber => $biblionumber,
387 order => $order->unblessed,
388 budget_id => $infos->{budget_id},
389 quantityreceived => $item_quantity,
391 received_items => [],
395 is( GetBudgetHierarchySpent( $budget_id1 ), 160, "total spent for budget1 is 160" );
396 is( GetBudgetHierarchySpent( $budget_id11 ), 100, "total spent for budget11 is 100" );
397 is( GetBudgetHierarchySpent( $budget_id111 ), 20, "total spent for budget111 is 20" );
399 # GetBudgetSpent and GetBudgetOrdered
400 my $budget_period_amount = 100;
401 my $budget_amount = 50;
403 $budget = AddBudgetPeriod(
405 budget_period_startdate => '2017-08-22',
406 budget_period_enddate => '2018-08-22',
407 budget_period_description => 'Test budget',
408 budget_period_active => 1,
409 budget_period_total => $budget_period_amount,
413 my $fund = AddBudget(
415 budget_code => 'Test fund',
416 budget_name => 'Test fund',
417 budget_period_id => $budget,
418 budget_parent_id => undef,
419 budget_amount => $budget_amount,
423 my $vendor = Koha::Acquisition::Bookseller->new(
425 name => "test vendor",
426 address1 => "test address",
433 my $vendorid = $vendor->id;
435 my $basketnumber = C4::Acquisition::NewBasket( $vendorid, 1 );
436 my ( $biblio, $biblioitem ) = C4::Biblio::AddBiblio( MARC::Record->new, '' );
441 pending_quantity => 1,
446 my $invoiceident = AddInvoice( invoicenumber => 'invoice_test_clone', booksellerid => $vendorid, shipmentdate => '2017-08-22', shipmentcost => 6, shipmentcost_budgetid => $fund );
447 my $test_invoice = GetInvoice( $invoiceident );
448 my $individual_item_price = 10;
450 my $order = Koha::Acquisition::Order->new(
452 basketno => $basketnumber,
453 biblionumber => $biblio,
455 order_internalnote => "internalnote",
456 order_vendornote => "vendor note",
458 cost_tax_included => $individual_item_price,
459 rrp_tax_included => $individual_item_price,
460 listprice => $individual_item_price,
461 ecost_tax_included => $individual_item_price,
468 bibionumber => $biblio,
469 order => $order->unblessed,
471 quantityreceived => 2,
472 invoice => $test_invoice,
473 received_items => [],
476 is ( GetBudgetSpent( $fund ), 6, "total shipping cost is 6");
477 is ( GetBudgetOrdered( $fund ), '20', "total ordered price is 20");
481 # Let's make sure our timestamp is old
482 my @orig_funds = Koha::Acquisition::Funds->search({ budget_period_id => $budget_period_id });
483 foreach my $fund (@orig_funds){
484 $fund->timestamp('1999-12-31 23:59:59')->store;
487 my $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
489 budget_period_id => $budget_period_id,
490 budget_period_startdate => '2014-01-01',
491 budget_period_enddate => '2014-12-31',
492 budget_period_description => 'Budget Period Cloned',
496 my $budget_period_cloned = C4::Budgets::GetBudgetPeriod($budget_period_id_cloned);
497 is($budget_period_cloned->{budget_period_description}, 'Budget Period Cloned', 'Cloned budget\'s description is updated.');
499 my $budget_cloned = C4::Budgets::GetBudgets({ budget_period_id => $budget_period_id_cloned });
500 my $budget_time = $budget_cloned->[0]->{timestamp};
502 isnt($budget_time, '1999-12-31 23:59:59', "New budget has an updated timestamp");
506 my $budget_hierarchy = GetBudgetHierarchy($budget_period_id);
507 my $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
510 scalar(@$budget_hierarchy_cloned),
511 scalar(@$budget_hierarchy),
512 'CloneBudgetPeriod clones the same number of budgets (funds)'
515 _get_dependencies($budget_hierarchy),
516 _get_dependencies($budget_hierarchy_cloned),
517 'CloneBudgetPeriod keeps the same dependencies order'
520 # CloneBudgetPeriod with param mark_original_budget_as_inactive
521 my $budget_period = C4::Budgets::GetBudgetPeriod($budget_period_id);
522 is( $budget_period->{budget_period_active}, 1,
523 'CloneBudgetPeriod does not mark as inactive the budgetperiod if not needed'
526 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
527 my $number_of_budgets_not_reset = 0;
528 for my $budget (@$budget_hierarchy_cloned) {
529 $number_of_budgets_not_reset++ if $budget->{budget_amount} > 0;
531 is( $number_of_budgets_not_reset, 5,
532 'CloneBudgetPeriod does not reset budgets (funds) if not needed' );
534 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
536 budget_period_id => $budget_period_id,
537 budget_period_startdate => '2014-01-01',
538 budget_period_enddate => '2014-12-31',
539 mark_original_budget_as_inactive => 1,
543 $budget_hierarchy = GetBudgetHierarchy($budget_period_id);
544 is( $budget_hierarchy->[0]->{children}->[0]->{budget_name}, 'budget_11', 'GetBudgetHierarchy should return budgets ordered by name, first child is budget_11' );
545 is( $budget_hierarchy->[0]->{children}->[1]->{budget_name}, 'budget_12', 'GetBudgetHierarchy should return budgets ordered by name, second child is budget_12' );
546 is($budget_hierarchy->[0]->{budget_name},'budget_1','GetBudgetHierarchy should return budgets ordered by name, first budget is budget_1');
547 is($budget_hierarchy->[0]->{budget_level},'0','budget_level of budget (budget_1) should be 0');
548 is($budget_hierarchy->[0]->{children}->[0]->{budget_level},'1','budget_level of first fund(budget_11) should be 1');
549 is($budget_hierarchy->[0]->{children}->[1]->{budget_level},'1','budget_level of second fund(budget_12) should be 1');
550 is($budget_hierarchy->[0]->{children}->[0]->{children}->[0]->{budget_level},'2','budget_level of child fund budget_11 should be 2');
551 $budget_hierarchy = GetBudgetHierarchy($budget_period_id);
552 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
554 is( scalar(@$budget_hierarchy_cloned), scalar(@$budget_hierarchy),
555 'CloneBudgetPeriod (with inactive param) clones the same number of budgets (funds)'
558 _get_dependencies($budget_hierarchy),
559 _get_dependencies($budget_hierarchy_cloned),
560 'CloneBudgetPeriod (with inactive param) keeps the same dependencies order'
562 $budget_period = C4::Budgets::GetBudgetPeriod($budget_period_id);
563 is( $budget_period->{budget_period_active}, 0,
564 'CloneBudgetPeriod (with inactive param) marks as inactive the budgetperiod'
567 # CloneBudgetPeriod with param reset_all_budgets
568 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
570 budget_period_id => $budget_period_id,
571 budget_period_startdate => '2014-01-01',
572 budget_period_enddate => '2014-12-31',
573 reset_all_budgets => 1,
577 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
578 $number_of_budgets_not_reset = 0;
579 for my $budget (@$budget_hierarchy_cloned) {
580 $number_of_budgets_not_reset++ if $budget->{budget_amount} > 0;
582 is( $number_of_budgets_not_reset, 0,
583 'CloneBudgetPeriod has reset all budgets (funds)' );
585 #GetBudgetsByActivity
586 my $result=C4::Budgets::GetBudgetsByActivity(1);
587 isnt( $result, undef ,'GetBudgetsByActivity return correct value with parameter 1');
588 $result=C4::Budgets::GetBudgetsByActivity(0);
589 isnt( $result, undef ,'GetBudgetsByActivity return correct value with parameter 0');
590 $result=C4::Budgets::GetBudgetsByActivity();
591 is( $result, 0 , 'GetBudgetsByActivity return 0 with none parameter or other 0 or 1' );
592 DelBudget($budget_id);
593 DelBudgetPeriod($bpid);
595 # CloneBudgetPeriod with param amount_change_*
596 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
598 budget_period_id => $budget_period_id,
599 budget_period_startdate => '2014-01-01',
600 budget_period_enddate => '2014-12-31',
601 amount_change_percentage => 16,
602 amount_change_round_increment => 5,
606 $budget_period_cloned = C4::Budgets::GetBudgetPeriod($budget_period_id_cloned);
607 cmp_ok($budget_period_cloned->{budget_period_total}, '==', 11600, "CloneBudgetPeriod changed correctly budget amount");
608 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
609 cmp_ok($budget_hierarchy_cloned->[0]->{budget_amount}, '==', 1160, "CloneBudgetPeriod changed correctly funds amounts");
610 cmp_ok($budget_hierarchy_cloned->[1]->{budget_amount}, '==', 115, "CloneBudgetPeriod changed correctly funds amounts");
611 cmp_ok($budget_hierarchy_cloned->[2]->{budget_amount}, '==', 55, "CloneBudgetPeriod changed correctly funds amounts");
612 cmp_ok($budget_hierarchy_cloned->[3]->{budget_amount}, '==', 115, "CloneBudgetPeriod changed correctly funds amounts");
613 cmp_ok($budget_hierarchy_cloned->[4]->{budget_amount}, '==', 2320, "CloneBudgetPeriod changed correctly funds amounts");
614 cmp_ok($budget_hierarchy_cloned->[5]->{budget_amount}, '==', 0, "CloneBudgetPeriod changed correctly funds amounts");
616 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
618 budget_period_id => $budget_period_id,
619 budget_period_startdate => '2014-01-01',
620 budget_period_enddate => '2014-12-31',
621 amount_change_percentage => 16,
622 amount_change_round_increment => 5,
623 reset_all_budgets => 1,
626 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
627 cmp_ok($budget_hierarchy_cloned->[0]->{budget_amount}, '==', 0, "CloneBudgetPeriod reset all fund amounts");
630 my $number_orders_moved = C4::Budgets::MoveOrders();
631 is( $number_orders_moved, undef, 'MoveOrders return undef if no arg passed' );
632 $number_orders_moved =
633 C4::Budgets::MoveOrders( { from_budget_period_id => $budget_period_id } );
634 is( $number_orders_moved, undef,
635 'MoveOrders return undef if only 1 arg passed' );
636 $number_orders_moved =
637 C4::Budgets::MoveOrders( { to_budget_period_id => $budget_period_id } );
638 is( $number_orders_moved, undef,
639 'MoveOrders return undef if only 1 arg passed' );
640 $number_orders_moved = C4::Budgets::MoveOrders(
642 from_budget_period_id => $budget_period_id,
643 to_budget_period_id => $budget_period_id
646 is( $number_orders_moved, undef,
647 'MoveOrders return undef if 2 budget period id are the same' );
649 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
651 budget_period_id => $budget_period_id,
652 budget_period_startdate => '2014-01-01',
653 budget_period_enddate => '2014-12-31',
657 my $report = C4::Budgets::MoveOrders(
659 from_budget_period_id => $budget_period_id,
660 to_budget_period_id => $budget_period_id_cloned,
661 move_remaining_unspent => 1,
664 is( scalar( @$report ), 6 , "MoveOrders has processed 6 funds" );
666 my $number_of_orders_moved = 0;
667 $number_of_orders_moved += scalar( @{ $_->{orders_moved} } ) for @$report;
668 is( $number_of_orders_moved, $number_of_orders_to_move, "MoveOrders has moved $number_of_orders_to_move orders" );
670 my @new_budget_ids = map { $_->{budget_id} }
671 @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
672 my @old_budget_ids = map { $_->{budget_id} }
673 @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
674 for my $budget_id ( keys %budgets ) {
675 for my $ordernumber ( @{ $budgets{$budget_id} } ) {
676 my $budget = GetBudgetByOrderNumber($ordernumber);
677 my $is_in_new_budgets = grep /^$budget->{budget_id}$/, @new_budget_ids;
678 my $is_in_old_budgets = grep /^$budget->{budget_id}$/, @old_budget_ids;
679 is( $is_in_new_budgets, 1, "MoveOrders changed the budget_id for order $ordernumber" );
680 is( $is_in_old_budgets, 0, "MoveOrders changed the budget_id for order $ordernumber" );
685 # MoveOrders with param move_remaining_unspent
686 my @new_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
687 my @old_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
689 for my $new_budget ( @new_budgets ) {
690 my ( $old_budget ) = map { $_->{budget_code} eq $new_budget->{budget_code} ? $_ : () } @old_budgets;
691 my $new_budget_amount_should_be = $old_budget->{budget_amount} * 2 - $old_budget->{total_spent};
692 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})" );
695 # Test SetOwnerToFundHierarchy
697 my $patron_category = $builder->build({ source => 'Category' });
698 my $branchcode = $library->{branchcode};
699 my $john_doe = Koha::Patron->new({
700 cardnumber => '123456',
703 categorycode => $patron_category->{categorycode},
704 branchcode => $branchcode,
706 dateexpiry => '9999-12-31',
708 })->store->borrowernumber;
710 C4::Budgets::SetOwnerToFundHierarchy( $budget_id1, $john_doe );
711 is( C4::Budgets::GetBudget($budget_id1)->{budget_owner_id},
712 $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 1 ($budget_id1)" );
713 is( C4::Budgets::GetBudget($budget_id11)->{budget_owner_id},
714 $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 11 ($budget_id11)" );
715 is( C4::Budgets::GetBudget($budget_id111)->{budget_owner_id},
716 $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 111 ($budget_id111)" );
717 is( C4::Budgets::GetBudget($budget_id12)->{budget_owner_id},
718 $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 12 ($budget_id12 )" );
719 is( C4::Budgets::GetBudget($budget_id2)->{budget_owner_id},
720 undef, "SetOwnerToFundHierarchy should not have set an owner for budget 2 ($budget_id2)" );
721 is( C4::Budgets::GetBudget($budget_id21)->{budget_owner_id},
722 undef, "SetOwnerToFundHierarchy should not have set an owner for budget 21 ($budget_id21)" );
724 my $jane_doe = Koha::Patron->new({
725 cardnumber => '789012',
728 categorycode => $patron_category->{categorycode},
729 branchcode => $branchcode,
731 dateexpiry => '9999-12-31',
733 })->store->borrowernumber;
735 C4::Budgets::SetOwnerToFundHierarchy( $budget_id11, $jane_doe );
736 is( C4::Budgets::GetBudget($budget_id1)->{budget_owner_id},
737 $john_doe, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 1 ($budget_id1)" );
738 is( C4::Budgets::GetBudget($budget_id11)->{budget_owner_id},
739 $jane_doe, "SetOwnerToFundHierarchy should have set John Doe $jane_doe for budget 11 ($budget_id11)" );
740 is( C4::Budgets::GetBudget($budget_id111)->{budget_owner_id},
741 $jane_doe, "SetOwnerToFundHierarchy should have set John Doe $jane_doe for budget 111 ($budget_id111)" );
742 is( C4::Budgets::GetBudget($budget_id12)->{budget_owner_id},
743 $john_doe, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 12 ($budget_id12 )" );
744 is( C4::Budgets::GetBudget($budget_id2)->{budget_owner_id},
745 undef, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 2 ($budget_id2)" );
746 is( C4::Budgets::GetBudget($budget_id21)->{budget_owner_id},
747 undef, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 21 ($budget_id21)" );
749 # Test GetBudgetAuthCats
751 my $budgetPeriodId = AddBudgetPeriod({
752 budget_period_startdate => '2008-01-01',
753 budget_period_enddate => '2008-12-31',
754 budget_period_description => 'just another budget',
755 budget_period_active => 0,
758 $budgets = GetBudgets();
760 for my $budget ( @$budgets )
762 $budget->{sort1_authcat} = "sort1_authcat_$i";
763 $budget->{sort2_authcat} = "sort2_authcat_$i";
764 $budget->{budget_period_id} = $budgetPeriodId;
765 ModBudget( $budget );
769 my $authCat = GetBudgetAuthCats($budgetPeriodId);
771 is( scalar @{$authCat}, $i * 2, "GetBudgetAuthCats returns only non-empty sorting categories (no empty authCat in db)" );
774 for my $budget ( @$budgets )
776 $budget->{sort1_authcat} = "sort_authcat_$i";
777 $budget->{sort2_authcat} = "sort_authcat_$i";
778 $budget->{budget_period_id} = $budgetPeriodId;
779 ModBudget( $budget );
783 $authCat = GetBudgetAuthCats($budgetPeriodId);
784 is( scalar @$authCat, scalar @$budgets, "GetBudgetAuthCats returns distinct authCat" );
787 for my $budget ( @$budgets )
789 $budget->{sort1_authcat} = "sort1_authcat_$i";
790 $budget->{sort2_authcat} = "";
791 $budget->{budget_period_id} = $budgetPeriodId;
792 ModBudget( $budget );
796 $authCat = GetBudgetAuthCats($budgetPeriodId);
798 is( scalar @{$authCat}, $i, "GetBudgetAuthCats returns only non-empty sorting categories (empty sort2_authcat on all records)" );
801 for my $budget ( @$budgets )
803 $budget->{sort1_authcat} = "";
804 $budget->{sort2_authcat} = "";
805 $budget->{budget_period_id} = $budgetPeriodId;
806 ModBudget( $budget );
810 $authCat = GetBudgetAuthCats($budgetPeriodId);
812 is( scalar @{$authCat}, 0, "GetBudgetAuthCats returns only non-empty sorting categories (all empty)" );
814 # /Test GetBudgetAuthCats
816 subtest 'GetBudgetSpent and GetBudgetOrdered' => sub {
819 my $budget = $builder->build({
820 source => 'Aqbudget',
822 budget_amount => 1000,
825 my $invoice = $builder->build({
826 source => 'Aqinvoice',
832 my $spent = GetBudgetSpent( $budget->{budget_id} );
833 my $ordered = GetBudgetOrdered( $budget->{budget_id} );
835 is( $spent, 0, "New budget, no orders/invoices, should be nothing spent");
836 is( $ordered, 0, "New budget, no orders/invoices, should be nothing ordered");
838 my $inv_adj_1 = $builder->build({
839 source => 'AqinvoiceAdjustment',
841 invoiceid => $invoice->{invoiceid},
844 budget_id => $budget->{budget_id},
848 $spent = GetBudgetSpent( $budget->{budget_id} );
849 $ordered = GetBudgetOrdered( $budget->{budget_id} );
850 is( $spent, 0, "After adding invoice adjustment on open invoice, should be nothing spent");
851 is( $ordered, 0, "After adding invoice adjustment on open invoice not encumbered, should be nothing ordered");
853 my $inv_adj_2 = $builder->build({
854 source => 'AqinvoiceAdjustment',
856 invoiceid => $invoice->{invoiceid},
859 budget_id => $budget->{budget_id},
863 $spent = GetBudgetSpent( $budget->{budget_id} );
864 $ordered = GetBudgetOrdered( $budget->{budget_id} );
865 is( $spent, 0, "After adding invoice adjustment on open invoice, should be nothing spent");
866 is( $ordered, 3, "After adding invoice adjustment on open invoice encumbered, should be 3 ordered");
868 my $invoice_2 = $builder->build({
869 source => 'Aqinvoice',
871 closedate => '2017-07-01',
874 my $inv_adj_3 = $builder->build({
875 source => 'AqinvoiceAdjustment',
877 invoiceid => $invoice_2->{invoiceid},
880 budget_id => $budget->{budget_id},
883 my $inv_adj_4 = $builder->build({
884 source => 'AqinvoiceAdjustment',
886 invoiceid => $invoice_2->{invoiceid},
889 budget_id => $budget->{budget_id},
893 $spent = GetBudgetSpent( $budget->{budget_id} );
894 $ordered = GetBudgetOrdered( $budget->{budget_id} );
895 is( $spent, 6, "After adding invoice adjustment on closed invoice, should be 6 spent, encumber has no affect once closed");
896 is( $ordered, 3, "After adding invoice adjustment on closed invoice, should still be 3 ordered");
898 my $budget_2 = $builder->build({
899 source => 'Aqbudget',
901 budget_amount => 1000,
904 my $inv_adj_5 = $builder->build({
905 source => 'AqinvoiceAdjustment',
907 invoiceid => $invoice->{invoiceid},
910 budget_id => $budget_2->{budget_id},
913 my $inv_adj_6 = $builder->build({
914 source => 'AqinvoiceAdjustment',
916 invoiceid => $invoice_2->{invoiceid},
919 budget_id => $budget_2->{budget_id},
923 $spent = GetBudgetSpent( $budget->{budget_id} );
924 $ordered = GetBudgetOrdered( $budget->{budget_id} );
925 is( $spent, 6, "After adding invoice adjustment on a different budget should be 6 spent/budget unaffected");
926 is( $ordered, 3, "After adding invoice adjustment on a different budget, should still be 3 ordered/budget unaffected");
930 sub _get_dependencies {
931 my ($budget_hierarchy) = @_;
933 for my $budget (@$budget_hierarchy) {
934 if ( $budget->{child} ) {
935 my @sorted = sort @{ $budget->{child} };
936 for my $child_id (@sorted) {
937 push @{ $graph->{ $budget->{budget_name} }{children} },
938 _get_budgetname_by_id( $budget_hierarchy, $child_id );
941 push @{ $graph->{ $budget->{budget_name} }{parents} },
942 $budget->{parent_id};
947 sub _get_budgetname_by_id {
948 my ( $budgets, $budget_id ) = @_;
950 map { ( $_->{budget_id} eq $budget_id ) ? $_->{budget_name} : () }
955 # C4::Context->userenv