2 use Test::More tests => 108;
13 use Koha::Acquisition::Order;
16 my $dbh = C4::Context->dbh;
17 $dbh->{AutoCommit} = 0;
18 $dbh->{RaiseError} = 1;
20 $dbh->do(q|DELETE FROM aqbudgetperiods|);
21 $dbh->do(q|DELETE FROM aqbudgets|);
24 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
26 *C4::Context::userenv = \&Mock_userenv;
27 $userenv = { flags => 1, id => 'my_userid', branch => 'CPL' };
33 is( AddBudgetPeriod(), undef, 'AddBugetPeriod without argument returns undef' );
34 is( AddBudgetPeriod( { } ), undef, 'AddBugetPeriod with an empty argument returns undef' );
35 my $bpid = AddBudgetPeriod({
36 budget_period_startdate => '2008-01-01',
38 is( $bpid, undef, 'AddBugetPeriod without end date returns undef' );
39 $bpid = AddBudgetPeriod({
40 budget_period_enddate => '2008-12-31',
42 is( $bpid, undef, 'AddBugetPeriod without start date returns undef' );
43 is( GetBudgetPeriod(0), undef ,'GetBudgetPeriod(0) returned undef : noactive BudgetPeriod' );
44 my $budgetperiods = GetBudgetPeriods();
45 is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
47 my $my_budgetperiod = {
48 budget_period_startdate => '2008-01-01',
49 budget_period_enddate => '2008-12-31',
50 budget_period_description => 'MAPERI',
51 budget_period_active => 0,
53 $bpid = AddBudgetPeriod($my_budgetperiod);
54 isnt( $bpid, undef, 'AddBugetPeriod does not returns undef' );
55 my $budgetperiod = GetBudgetPeriod($bpid);
56 is( $budgetperiod->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'AddBudgetPeriod stores the start date correctly' );
57 is( $budgetperiod->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'AddBudgetPeriod stores the end date correctly' );
58 is( $budgetperiod->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'AddBudgetPeriod stores the description correctly' );
59 is( $budgetperiod->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'AddBudgetPeriod stores active correctly' );
60 is( GetBudgetPeriod(0), undef ,'GetBudgetPeriod(0) returned undef : noactive BudgetPeriod' );
64 budget_period_startdate => '2009-01-01',
65 budget_period_enddate => '2009-12-31',
66 budget_period_description => 'MODIF_MAPERI',
67 budget_period_active => 1,
69 my $mod_status = ModBudgetPeriod($my_budgetperiod);
70 is( $mod_status, undef, 'ModBudgetPeriod without id returns undef' );
72 $my_budgetperiod->{budget_period_id} = $bpid;
73 $mod_status = ModBudgetPeriod($my_budgetperiod);
74 is( $mod_status, 1, 'ModBudgetPeriod returnis true' );
75 $budgetperiod = GetBudgetPeriod($bpid);
76 is( $budgetperiod->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'ModBudgetPeriod updates the start date correctly' );
77 is( $budgetperiod->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'ModBudgetPeriod updates the end date correctly' );
78 is( $budgetperiod->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'ModBudgetPeriod updates the description correctly' );
79 is( $budgetperiod->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'ModBudgetPeriod upates active correctly' );
80 isnt( GetBudgetPeriod(0), undef, 'GetBugetPeriods functions correctly' );
83 $budgetperiods = GetBudgetPeriods();
84 is( @$budgetperiods, 1, 'GetBudgetPeriods returns the correct number of budget periods' );
85 is( $budgetperiods->[0]->{budget_period_id}, $my_budgetperiod->{budget_period_id}, 'GetBudgetPeriods returns the id correctly' );
86 is( $budgetperiods->[0]->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'GetBudgetPeriods returns the start date correctly' );
87 is( $budgetperiods->[0]->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'GetBudgetPeriods returns the end date correctly' );
88 is( $budgetperiods->[0]->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'GetBudgetPeriods returns the description correctly' );
89 is( $budgetperiods->[0]->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'GetBudgetPeriods returns active correctly' );
91 is( DelBudgetPeriod($bpid), 1, 'DelBudgetPeriod returns true' );
92 $budgetperiods = GetBudgetPeriods();
93 is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
100 is( AddBudget(), undef, 'AddBuget without argument returns undef' );
101 my $budgets = GetBudgets();
102 is( @$budgets, 0, 'GetBudgets returns the correct number of budgets' );
104 $bpid = AddBudgetPeriod($my_budgetperiod);
106 budget_code => 'ABCD',
107 budget_amount => '123.132000',
108 budget_name => 'Periodiques',
109 budget_notes => 'This is a note',
110 budget_period_id => $bpid,
112 my $budget_id = AddBudget($my_budget);
113 isnt( $budget_id, undef, 'AddBudget does not returns undef' );
114 my $budget = GetBudget($budget_id);
115 is( $budget->{budget_code}, $my_budget->{budget_code}, 'AddBudget stores the budget code correctly' );
116 is( $budget->{budget_amount}, $my_budget->{budget_amount}, 'AddBudget stores the budget amount correctly' );
117 is( $budget->{budget_name}, $my_budget->{budget_name}, 'AddBudget stores the budget name correctly' );
118 is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'AddBudget stores the budget notes correctly' );
119 is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'AddBudget stores the budget period id correctly' );
123 budget_code => 'EFG',
124 budget_amount => '321.231000',
125 budget_name => 'Modified name',
126 budget_notes => 'This is a modified note',
127 budget_period_id => $bpid,
129 $mod_status = ModBudget($my_budget);
130 is( $mod_status, undef, 'ModBudget without id returns undef' );
132 $my_budget->{budget_id} = $budget_id;
133 $mod_status = ModBudget($my_budget);
134 is( $mod_status, 1, 'ModBudget returns true' );
135 $budget = GetBudget($budget_id);
136 is( $budget->{budget_code}, $my_budget->{budget_code}, 'ModBudget updates the budget code correctly' );
137 is( $budget->{budget_amount}, $my_budget->{budget_amount}, 'ModBudget updates the budget amount correctly' );
138 is( $budget->{budget_name}, $my_budget->{budget_name}, 'ModBudget updates the budget name correctly' );
139 is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'ModBudget updates the budget notes correctly' );
140 is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'ModBudget updates the budget period id correctly' );
143 $budgets = GetBudgets();
144 is( @$budgets, 1, 'GetBudgets returns the correct number of budgets' );
145 is( $budgets->[0]->{budget_id}, $my_budget->{budget_id}, 'GetBudgets returns the budget id correctly' );
146 is( $budgets->[0]->{budget_code}, $my_budget->{budget_code}, 'GetBudgets returns the budget code correctly' );
147 is( $budgets->[0]->{budget_amount}, $my_budget->{budget_amount}, 'GetBudgets returns the budget amount correctly' );
148 is( $budgets->[0]->{budget_name}, $my_budget->{budget_name}, 'GetBudgets returns the budget name correctly' );
149 is( $budgets->[0]->{budget_notes}, $my_budget->{budget_notes}, 'GetBudgets returns the budget notes correctly' );
150 is( $budgets->[0]->{budget_period_id}, $my_budget->{budget_period_id}, 'GetBudgets returns the budget period id correctly' );
152 $budgets = GetBudgets( {budget_period_id => $bpid} );
153 is( @$budgets, 1, 'GetBudgets With Filter OK' );
154 $budgets = GetBudgets( {budget_period_id => $bpid}, {-asc => "budget_name"} );
155 is( @$budgets, 1, 'GetBudgets With Order OK' );
156 $budgets = GetBudgets( {budget_period_id => GetBudgetPeriod($bpid)->{budget_period_id}}, {-asc => "budget_name"} );
157 is( @$budgets, 1, 'GetBudgets With Order Getting Active budgetPeriod OK');
160 my $budget_name = GetBudgetName( $budget_id );
161 is($budget_name, $my_budget->{budget_name}, "Test the GetBudgetName routine");
163 my $budget_code = $my_budget->{budget_code};
164 my $budget_by_code = GetBudgetByCode( $budget_code );
165 is($budget_by_code->{budget_id}, $budget_id, "GetBudgetByCode, check id");
166 is($budget_by_code->{budget_notes}, $my_budget->{budget_notes}, "GetBudgetByCode, check notes");
168 my $second_budget_id = AddBudget({
169 budget_code => "ZZZZ",
170 budget_amount => "500.00",
171 budget_name => "Art",
172 budget_notes => "This is a note",
173 budget_period_id => $bpid,
175 isnt( $second_budget_id, undef, 'AddBudget does not returns undef' );
177 $budgets = GetBudgets( {budget_period_id => $bpid} );
178 ok( $budgets->[0]->{budget_name} lt $budgets->[1]->{budget_name}, 'default sort order for GetBudgets is by name' );
180 is( DelBudget($budget_id), 1, 'DelBudget returns true' );
181 $budgets = GetBudgets();
182 is( @$budgets, 1, 'GetBudgets returns the correct number of budget periods' );
185 # GetBudgetHierarchySpent and GetBudgetHierarchyOrdered
186 my $budget_period_total = 10_000;
187 my $budget_1_total = 1_000;
188 my $budget_11_total = 100;
189 my $budget_111_total = 50;
190 my $budget_12_total = 100;
191 my $budget_2_total = 2_000;
193 my $budget_period_id = AddBudgetPeriod(
195 budget_period_startdate => '2013-01-01',
196 budget_period_enddate => '2014-12-31',
197 budget_period_description => 'Budget Period',
198 budget_period_active => 1,
199 budget_period_total => $budget_period_total,
202 my $budget_id1 = AddBudget(
204 budget_code => 'budget_1',
205 budget_name => 'budget_1',
206 budget_period_id => $budget_period_id,
207 budget_parent_id => undef,
208 budget_amount => $budget_1_total,
211 my $budget_id2 = AddBudget(
213 budget_code => 'budget_2',
214 budget_name => 'budget_2',
215 budget_period_id => $budget_period_id,
216 budget_parent_id => undef,
217 budget_amount => $budget_2_total,
220 my $budget_id11 = AddBudget(
222 budget_code => 'budget_11',
223 budget_name => 'budget_11',
224 budget_period_id => $budget_period_id,
225 budget_parent_id => $budget_id1,
226 budget_amount => $budget_11_total,
229 my $budget_id12 = AddBudget(
231 budget_code => 'budget_12',
232 budget_name => 'budget_12',
233 budget_period_id => $budget_period_id,
234 budget_parent_id => $budget_id1,
235 budget_amount => $budget_12_total,
238 my $budget_id111 = AddBudget(
240 budget_code => 'budget_111',
241 budget_name => 'budget_111',
242 budget_period_id => $budget_period_id,
243 budget_parent_id => $budget_id11,
244 budget_owner_id => 1,
245 budget_amount => $budget_111_total,
248 my $budget_id21 = AddBudget(
250 budget_code => 'budget_21',
251 budget_name => 'budget_21',
252 budget_period_id => $budget_period_id,
253 budget_parent_id => $budget_id2,
257 my $booksellerid = C4::Bookseller::AddBookseller(
260 address1 => "bookseller's address",
267 my $basketno = C4::Acquisition::NewBasket( $booksellerid, 1 );
268 my ( $biblionumber, $biblioitemnumber ) =
269 C4::Biblio::AddBiblio( MARC::Record->new, '' );
273 budget_id => $budget_id1,
274 pending_quantity => 1,
278 budget_id => $budget_id2,
279 pending_quantity => 2,
283 budget_id => $budget_id11,
284 pending_quantity => 3,
288 budget_id => $budget_id12,
289 pending_quantity => 4,
293 budget_id => $budget_id111,
294 pending_quantity => 2,
298 # No order for budget_21
303 my $invoiceid = AddInvoice(invoicenumber => 'invoice_test_clone', booksellerid => $booksellerid, unknown => "unknown");
305 my $item_quantity = 2;
306 my $number_of_orders_to_move = 0;
307 for my $infos (@order_infos) {
308 for ( 1 .. $infos->{pending_quantity} ) {
309 my $order = Koha::Acquisition::Order->new(
311 basketno => $basketno,
312 biblionumber => $biblionumber,
313 budget_id => $infos->{budget_id},
314 order_internalnote => "internal note",
315 order_vendornote => "vendor note",
319 listprice => $item_price,
320 ecost => $item_price,
327 my $ordernumber = $order->{ordernumber};
328 push @{ $budgets{$infos->{budget_id}} }, $ordernumber;
329 $number_of_orders_to_move++;
331 for ( 1 .. $infos->{spent_quantity} ) {
332 my $order = Koha::Acquisition::Order->new(
334 basketno => $basketno,
335 biblionumber => $biblionumber,
336 budget_id => $infos->{budget_id},
337 order_internalnote => "internal note",
338 order_vendornote => "vendor note",
339 quantity => $item_quantity,
342 listprice => $item_price,
343 ecost => $item_price,
350 my $ordernumber = $order->{ordernumber};
352 biblionumber => $biblionumber,
353 ordernumber => $ordernumber,
354 budget_id => $infos->{budget_id},
355 quantityreceived => $item_quantity,
357 ecost => $item_price,
358 invoiceid => $invoiceid,
360 received_items => [],
364 is( GetBudgetHierarchySpent( $budget_id1 ), 160, "total spent for budget1 is 160" );
365 is( GetBudgetHierarchySpent( $budget_id11 ), 100, "total spent for budget11 is 100" );
366 is( GetBudgetHierarchySpent( $budget_id111 ), 20, "total spent for budget111 is 20" );
369 my $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
371 budget_period_id => $budget_period_id,
372 budget_period_startdate => '2014-01-01',
373 budget_period_enddate => '2014-12-31',
374 budget_period_description => 'Budget Period Cloned',
378 my $budget_period_cloned = C4::Budgets::GetBudgetPeriod($budget_period_id_cloned);
379 is($budget_period_cloned->{budget_period_description}, 'Budget Period Cloned', 'Cloned budget\'s description is updated.');
381 my $budget_hierarchy = GetBudgetHierarchy($budget_period_id);
382 my $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
385 scalar(@$budget_hierarchy_cloned),
386 scalar(@$budget_hierarchy),
387 'CloneBudgetPeriod clones the same number of budgets (funds)'
390 _get_dependencies($budget_hierarchy),
391 _get_dependencies($budget_hierarchy_cloned),
392 'CloneBudgetPeriod keeps the same dependencies order'
395 # CloneBudgetPeriod with param mark_original_budget_as_inactive
396 my $budget_period = C4::Budgets::GetBudgetPeriod($budget_period_id);
397 is( $budget_period->{budget_period_active}, 1,
398 'CloneBudgetPeriod does not mark as inactive the budgetperiod if not needed'
401 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
402 my $number_of_budgets_not_reset = 0;
403 for my $budget (@$budget_hierarchy_cloned) {
404 $number_of_budgets_not_reset++ if $budget->{budget_amount} > 0;
406 is( $number_of_budgets_not_reset, 5,
407 'CloneBudgetPeriod does not reset budgets (funds) if not needed' );
409 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
411 budget_period_id => $budget_period_id,
412 budget_period_startdate => '2014-01-01',
413 budget_period_enddate => '2014-12-31',
414 mark_original_budget_as_inactive => 1,
418 $budget_hierarchy = GetBudgetHierarchy($budget_period_id);
419 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
421 is( scalar(@$budget_hierarchy_cloned), scalar(@$budget_hierarchy),
422 'CloneBudgetPeriod (with inactive param) clones the same number of budgets (funds)'
425 _get_dependencies($budget_hierarchy),
426 _get_dependencies($budget_hierarchy_cloned),
427 'CloneBudgetPeriod (with inactive param) keeps the same dependencies order'
429 $budget_period = C4::Budgets::GetBudgetPeriod($budget_period_id);
430 is( $budget_period->{budget_period_active}, 0,
431 'CloneBudgetPeriod (with inactive param) marks as inactive the budgetperiod'
434 # CloneBudgetPeriod with param reset_all_budgets
435 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
437 budget_period_id => $budget_period_id,
438 budget_period_startdate => '2014-01-01',
439 budget_period_enddate => '2014-12-31',
440 reset_all_budgets => 1,
444 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
445 $number_of_budgets_not_reset = 0;
446 for my $budget (@$budget_hierarchy_cloned) {
447 $number_of_budgets_not_reset++ if $budget->{budget_amount} > 0;
449 is( $number_of_budgets_not_reset, 0,
450 'CloneBudgetPeriod has reset all budgets (funds)' );
454 my $number_orders_moved = C4::Budgets::MoveOrders();
455 is( $number_orders_moved, undef, 'MoveOrders return undef if no arg passed' );
456 $number_orders_moved =
457 C4::Budgets::MoveOrders( { from_budget_period_id => $budget_period_id } );
458 is( $number_orders_moved, undef,
459 'MoveOrders return undef if only 1 arg passed' );
460 $number_orders_moved =
461 C4::Budgets::MoveOrders( { to_budget_period_id => $budget_period_id } );
462 is( $number_orders_moved, undef,
463 'MoveOrders return undef if only 1 arg passed' );
464 $number_orders_moved = C4::Budgets::MoveOrders(
466 from_budget_period_id => $budget_period_id,
467 to_budget_period_id => $budget_period_id
470 is( $number_orders_moved, undef,
471 'MoveOrders return undef if 2 budget period id are the same' );
473 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
475 budget_period_id => $budget_period_id,
476 budget_period_startdate => '2014-01-01',
477 budget_period_enddate => '2014-12-31',
481 my $report = C4::Budgets::MoveOrders(
483 from_budget_period_id => $budget_period_id,
484 to_budget_period_id => $budget_period_id_cloned,
485 move_remaining_unspent => 1,
488 is( scalar( @$report ), 6 , "MoveOrders has processed 6 funds" );
490 my $number_of_orders_moved = 0;
491 $number_of_orders_moved += scalar( @{ $_->{orders_moved} } ) for @$report;
492 is( $number_of_orders_moved, $number_of_orders_to_move, "MoveOrders has moved $number_of_orders_to_move orders" );
494 my @new_budget_ids = map { $_->{budget_id} }
495 @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
496 my @old_budget_ids = map { $_->{budget_id} }
497 @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
498 for my $budget_id ( keys %budgets ) {
499 for my $ordernumber ( @{ $budgets{$budget_id} } ) {
500 my $budget = GetBudgetByOrderNumber($ordernumber);
501 my $is_in_new_budgets = grep /^$budget->{budget_id}$/, @new_budget_ids;
502 my $is_in_old_budgets = grep /^$budget->{budget_id}$/, @old_budget_ids;
503 is( $is_in_new_budgets, 1, "MoveOrders changed the budget_id for order $ordernumber" );
504 is( $is_in_old_budgets, 0, "MoveOrders changed the budget_id for order $ordernumber" );
509 # MoveOrders with param move_remaining_unspent
510 my @new_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
511 my @old_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
513 for my $new_budget ( @new_budgets ) {
514 my ( $old_budget ) = map { $_->{budget_code} eq $new_budget->{budget_code} ? $_ : () } @old_budgets;
515 my $new_budget_amount_should_be = $old_budget->{budget_amount} * 2 - $old_budget->{total_spent};
516 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})" );
519 sub _get_dependencies {
520 my ($budget_hierarchy) = @_;
522 for my $budget (@$budget_hierarchy) {
523 if ( $budget->{child} ) {
524 my @sorted = sort @{ $budget->{child} };
525 for my $child_id (@sorted) {
526 push @{ $graph->{ $budget->{budget_name} }{children} },
527 _get_budgetname_by_id( $budget_hierarchy, $child_id );
530 push @{ $graph->{ $budget->{budget_name} }{parents} },
531 $budget->{parent_id};
536 sub _get_budgetname_by_id {
537 my ( $budgets, $budget_id ) = @_;
539 map { ( $_->{budget_id} eq $budget_id ) ? $_->{budget_name} : () }
544 # C4::Context->userenv