2 use Test::More tests => 108;
14 my $dbh = C4::Context->dbh;
15 $dbh->{AutoCommit} = 0;
16 $dbh->{RaiseError} = 1;
18 $dbh->do(q|DELETE FROM aqbudgetperiods|);
19 $dbh->do(q|DELETE FROM aqbudgets|);
22 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
24 *C4::Context::userenv = \&Mock_userenv;
25 $userenv = { flags => 1, id => 'my_userid', branch => 'CPL' };
31 is( AddBudgetPeriod(), undef, 'AddBugetPeriod without argument returns undef' );
32 is( AddBudgetPeriod( { } ), undef, 'AddBugetPeriod with an empty argument returns undef' );
33 my $bpid = AddBudgetPeriod({
34 budget_period_startdate => '2008-01-01',
36 is( $bpid, undef, 'AddBugetPeriod without end date returns undef' );
37 $bpid = AddBudgetPeriod({
38 budget_period_enddate => '2008-12-31',
40 is( $bpid, undef, 'AddBugetPeriod without start date returns undef' );
41 is( GetBudgetPeriod(0), undef ,'GetBudgetPeriod(0) returned undef : noactive BudgetPeriod' );
42 my $budgetperiods = GetBudgetPeriods();
43 is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
45 my $my_budgetperiod = {
46 budget_period_startdate => '2008-01-01',
47 budget_period_enddate => '2008-12-31',
48 budget_period_description => 'MAPERI',
49 budget_period_active => 0,
51 $bpid = AddBudgetPeriod($my_budgetperiod);
52 isnt( $bpid, undef, 'AddBugetPeriod does not returns undef' );
53 my $budgetperiod = GetBudgetPeriod($bpid);
54 is( $budgetperiod->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'AddBudgetPeriod stores the start date correctly' );
55 is( $budgetperiod->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'AddBudgetPeriod stores the end date correctly' );
56 is( $budgetperiod->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'AddBudgetPeriod stores the description correctly' );
57 is( $budgetperiod->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'AddBudgetPeriod stores active correctly' );
58 is( GetBudgetPeriod(0), undef ,'GetBudgetPeriod(0) returned undef : noactive BudgetPeriod' );
62 budget_period_startdate => '2009-01-01',
63 budget_period_enddate => '2009-12-31',
64 budget_period_description => 'MODIF_MAPERI',
65 budget_period_active => 1,
67 my $mod_status = ModBudgetPeriod($my_budgetperiod);
68 is( $mod_status, undef, 'ModBudgetPeriod without id returns undef' );
70 $my_budgetperiod->{budget_period_id} = $bpid;
71 $mod_status = ModBudgetPeriod($my_budgetperiod);
72 is( $mod_status, 1, 'ModBudgetPeriod returnis true' );
73 $budgetperiod = GetBudgetPeriod($bpid);
74 is( $budgetperiod->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'ModBudgetPeriod updates the start date correctly' );
75 is( $budgetperiod->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'ModBudgetPeriod updates the end date correctly' );
76 is( $budgetperiod->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'ModBudgetPeriod updates the description correctly' );
77 is( $budgetperiod->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'ModBudgetPeriod upates active correctly' );
78 isnt( GetBudgetPeriod(0), undef, 'GetBugetPeriods functions correctly' );
81 $budgetperiods = GetBudgetPeriods();
82 is( @$budgetperiods, 1, 'GetBudgetPeriods returns the correct number of budget periods' );
83 is( $budgetperiods->[0]->{budget_period_id}, $my_budgetperiod->{budget_period_id}, 'GetBudgetPeriods returns the id correctly' );
84 is( $budgetperiods->[0]->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'GetBudgetPeriods returns the start date correctly' );
85 is( $budgetperiods->[0]->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'GetBudgetPeriods returns the end date correctly' );
86 is( $budgetperiods->[0]->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'GetBudgetPeriods returns the description correctly' );
87 is( $budgetperiods->[0]->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'GetBudgetPeriods returns active correctly' );
89 is( DelBudgetPeriod($bpid), 1, 'DelBudgetPeriod returns true' );
90 $budgetperiods = GetBudgetPeriods();
91 is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
98 is( AddBudget(), undef, 'AddBuget without argument returns undef' );
99 my $budgets = GetBudgets();
100 is( @$budgets, 0, 'GetBudgets returns the correct number of budgets' );
102 $bpid = AddBudgetPeriod($my_budgetperiod);
104 budget_code => 'ABCD',
105 budget_amount => '123.132000',
106 budget_name => 'Periodiques',
107 budget_notes => 'This is a note',
108 budget_period_id => $bpid,
110 my $budget_id = AddBudget($my_budget);
111 isnt( $budget_id, undef, 'AddBudget does not returns undef' );
112 my $budget = GetBudget($budget_id);
113 is( $budget->{budget_code}, $my_budget->{budget_code}, 'AddBudget stores the budget code correctly' );
114 is( $budget->{budget_amount}, $my_budget->{budget_amount}, 'AddBudget stores the budget amount correctly' );
115 is( $budget->{budget_name}, $my_budget->{budget_name}, 'AddBudget stores the budget name correctly' );
116 is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'AddBudget stores the budget notes correctly' );
117 is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'AddBudget stores the budget period id correctly' );
121 budget_code => 'EFG',
122 budget_amount => '321.231000',
123 budget_name => 'Modified name',
124 budget_notes => 'This is a modified note',
125 budget_period_id => $bpid,
127 $mod_status = ModBudget($my_budget);
128 is( $mod_status, undef, 'ModBudget without id returns undef' );
130 $my_budget->{budget_id} = $budget_id;
131 $mod_status = ModBudget($my_budget);
132 is( $mod_status, 1, 'ModBudget returns true' );
133 $budget = GetBudget($budget_id);
134 is( $budget->{budget_code}, $my_budget->{budget_code}, 'ModBudget updates the budget code correctly' );
135 is( $budget->{budget_amount}, $my_budget->{budget_amount}, 'ModBudget updates the budget amount correctly' );
136 is( $budget->{budget_name}, $my_budget->{budget_name}, 'ModBudget updates the budget name correctly' );
137 is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'ModBudget updates the budget notes correctly' );
138 is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'ModBudget updates the budget period id correctly' );
141 $budgets = GetBudgets();
142 is( @$budgets, 1, 'GetBudgets returns the correct number of budgets' );
143 is( $budgets->[0]->{budget_id}, $my_budget->{budget_id}, 'GetBudgets returns the budget id correctly' );
144 is( $budgets->[0]->{budget_code}, $my_budget->{budget_code}, 'GetBudgets returns the budget code correctly' );
145 is( $budgets->[0]->{budget_amount}, $my_budget->{budget_amount}, 'GetBudgets returns the budget amount correctly' );
146 is( $budgets->[0]->{budget_name}, $my_budget->{budget_name}, 'GetBudgets returns the budget name correctly' );
147 is( $budgets->[0]->{budget_notes}, $my_budget->{budget_notes}, 'GetBudgets returns the budget notes correctly' );
148 is( $budgets->[0]->{budget_period_id}, $my_budget->{budget_period_id}, 'GetBudgets returns the budget period id correctly' );
150 $budgets = GetBudgets( {budget_period_id => $bpid} );
151 is( @$budgets, 1, 'GetBudgets With Filter OK' );
152 $budgets = GetBudgets( {budget_period_id => $bpid}, {-asc => "budget_name"} );
153 is( @$budgets, 1, 'GetBudgets With Order OK' );
154 $budgets = GetBudgets( {budget_period_id => GetBudgetPeriod($bpid)->{budget_period_id}}, {-asc => "budget_name"} );
155 is( @$budgets, 1, 'GetBudgets With Order Getting Active budgetPeriod OK');
158 my $budget_name = GetBudgetName( $budget_id );
159 is($budget_name, $my_budget->{budget_name}, "Test the GetBudgetName routine");
161 my $budget_code = $my_budget->{budget_code};
162 my $budget_by_code = GetBudgetByCode( $budget_code );
163 is($budget_by_code->{budget_id}, $budget_id, "GetBudgetByCode, check id");
164 is($budget_by_code->{budget_notes}, $my_budget->{budget_notes}, "GetBudgetByCode, check notes");
166 my $second_budget_id = AddBudget({
167 budget_code => "ZZZZ",
168 budget_amount => "500.00",
169 budget_name => "Art",
170 budget_notes => "This is a note",
171 budget_period_id => $bpid,
173 isnt( $second_budget_id, undef, 'AddBudget does not returns undef' );
175 $budgets = GetBudgets( {budget_period_id => $bpid} );
176 ok( $budgets->[0]->{budget_name} lt $budgets->[1]->{budget_name}, 'default sort order for GetBudgets is by name' );
178 is( DelBudget($budget_id), 1, 'DelBudget returns true' );
179 $budgets = GetBudgets();
180 is( @$budgets, 1, 'GetBudgets returns the correct number of budget periods' );
183 # GetBudgetHierarchySpent and GetBudgetHierarchyOrdered
184 my $budget_period_total = 10_000;
185 my $budget_1_total = 1_000;
186 my $budget_11_total = 100;
187 my $budget_111_total = 50;
188 my $budget_12_total = 100;
189 my $budget_2_total = 2_000;
191 my $budget_period_id = AddBudgetPeriod(
193 budget_period_startdate => '2013-01-01',
194 budget_period_enddate => '2014-12-31',
195 budget_period_description => 'Budget Period',
196 budget_period_active => 1,
197 budget_period_total => $budget_period_total,
200 my $budget_id1 = AddBudget(
202 budget_code => 'budget_1',
203 budget_name => 'budget_1',
204 budget_period_id => $budget_period_id,
205 budget_parent_id => undef,
206 budget_amount => $budget_1_total,
209 my $budget_id2 = AddBudget(
211 budget_code => 'budget_2',
212 budget_name => 'budget_2',
213 budget_period_id => $budget_period_id,
214 budget_parent_id => undef,
215 budget_amount => $budget_2_total,
218 my $budget_id11 = AddBudget(
220 budget_code => 'budget_11',
221 budget_name => 'budget_11',
222 budget_period_id => $budget_period_id,
223 budget_parent_id => $budget_id1,
224 budget_amount => $budget_11_total,
227 my $budget_id12 = AddBudget(
229 budget_code => 'budget_12',
230 budget_name => 'budget_12',
231 budget_period_id => $budget_period_id,
232 budget_parent_id => $budget_id1,
233 budget_amount => $budget_12_total,
236 my $budget_id111 = AddBudget(
238 budget_code => 'budget_111',
239 budget_name => 'budget_111',
240 budget_period_id => $budget_period_id,
241 budget_parent_id => $budget_id11,
242 budget_owner_id => 1,
243 budget_amount => $budget_111_total,
246 my $budget_id21 = AddBudget(
248 budget_code => 'budget_21',
249 budget_name => 'budget_21',
250 budget_period_id => $budget_period_id,
251 budget_parent_id => $budget_id2,
255 my $booksellerid = C4::Bookseller::AddBookseller(
258 address1 => "bookseller's address",
265 my $basketno = C4::Acquisition::NewBasket( $booksellerid, 1 );
266 my ( $biblionumber, $biblioitemnumber ) =
267 C4::Biblio::AddBiblio( MARC::Record->new, '' );
271 budget_id => $budget_id1,
272 pending_quantity => 1,
276 budget_id => $budget_id2,
277 pending_quantity => 2,
281 budget_id => $budget_id11,
282 pending_quantity => 3,
286 budget_id => $budget_id12,
287 pending_quantity => 4,
291 budget_id => $budget_id111,
292 pending_quantity => 2,
296 # No order for budget_21
301 my $invoiceid = AddInvoice(invoicenumber => 'invoice_test_clone', booksellerid => $booksellerid, unknown => "unknown");
303 my $item_quantity = 2;
304 my $number_of_orders_to_move = 0;
305 for my $infos (@order_infos) {
306 for ( 1 .. $infos->{pending_quantity} ) {
307 my ( undef, $ordernumber ) = C4::Acquisition::NewOrder(
309 basketno => $basketno,
310 biblionumber => $biblionumber,
311 budget_id => $infos->{budget_id},
312 order_internalnote => "internal note",
313 order_vendornote => "vendor note",
317 listprice => $item_price,
318 ecost => $item_price,
325 push @{ $budgets{$infos->{budget_id}} }, $ordernumber;
326 $number_of_orders_to_move++;
328 for ( 1 .. $infos->{spent_quantity} ) {
329 my ( undef, $ordernumber ) = C4::Acquisition::NewOrder(
331 basketno => $basketno,
332 biblionumber => $biblionumber,
333 budget_id => $infos->{budget_id},
334 order_internalnote => "internal note",
335 order_vendornote => "vendor note",
336 quantity => $item_quantity,
339 listprice => $item_price,
340 ecost => $item_price,
348 biblionumber => $biblionumber,
349 ordernumber => $ordernumber,
350 budget_id => $infos->{budget_id},
351 quantityreceived => $item_quantity,
353 ecost => $item_price,
354 invoiceid => $invoiceid,
356 received_items => [],
360 is( GetBudgetHierarchySpent( $budget_id1 ), 160, "total spent for budget1 is 160" );
361 is( GetBudgetHierarchySpent( $budget_id11 ), 100, "total spent for budget11 is 100" );
362 is( GetBudgetHierarchySpent( $budget_id111 ), 20, "total spent for budget111 is 20" );
365 my $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
367 budget_period_id => $budget_period_id,
368 budget_period_startdate => '2014-01-01',
369 budget_period_enddate => '2014-12-31',
370 budget_period_description => 'Budget Period Cloned',
374 my $budget_period_cloned = C4::Budgets::GetBudgetPeriod($budget_period_id_cloned);
375 is($budget_period_cloned->{budget_period_description}, 'Budget Period Cloned', 'Cloned budget\'s description is updated.');
377 my $budget_hierarchy = GetBudgetHierarchy($budget_period_id);
378 my $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
381 scalar(@$budget_hierarchy_cloned),
382 scalar(@$budget_hierarchy),
383 'CloneBudgetPeriod clones the same number of budgets (funds)'
386 _get_dependencies($budget_hierarchy),
387 _get_dependencies($budget_hierarchy_cloned),
388 'CloneBudgetPeriod keeps the same dependencies order'
391 # CloneBudgetPeriod with param mark_original_budget_as_inactive
392 my $budget_period = C4::Budgets::GetBudgetPeriod($budget_period_id);
393 is( $budget_period->{budget_period_active}, 1,
394 'CloneBudgetPeriod does not mark as inactive the budgetperiod if not needed'
397 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
398 my $number_of_budgets_not_reset = 0;
399 for my $budget (@$budget_hierarchy_cloned) {
400 $number_of_budgets_not_reset++ if $budget->{budget_amount} > 0;
402 is( $number_of_budgets_not_reset, 5,
403 'CloneBudgetPeriod does not reset budgets (funds) if not needed' );
405 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
407 budget_period_id => $budget_period_id,
408 budget_period_startdate => '2014-01-01',
409 budget_period_enddate => '2014-12-31',
410 mark_original_budget_as_inactive => 1,
414 $budget_hierarchy = GetBudgetHierarchy($budget_period_id);
415 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
417 is( scalar(@$budget_hierarchy_cloned), scalar(@$budget_hierarchy),
418 'CloneBudgetPeriod (with inactive param) clones the same number of budgets (funds)'
421 _get_dependencies($budget_hierarchy),
422 _get_dependencies($budget_hierarchy_cloned),
423 'CloneBudgetPeriod (with inactive param) keeps the same dependencies order'
425 $budget_period = C4::Budgets::GetBudgetPeriod($budget_period_id);
426 is( $budget_period->{budget_period_active}, 0,
427 'CloneBudgetPeriod (with inactive param) marks as inactive the budgetperiod'
430 # CloneBudgetPeriod with param reset_all_budgets
431 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
433 budget_period_id => $budget_period_id,
434 budget_period_startdate => '2014-01-01',
435 budget_period_enddate => '2014-12-31',
436 reset_all_budgets => 1,
440 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
441 $number_of_budgets_not_reset = 0;
442 for my $budget (@$budget_hierarchy_cloned) {
443 $number_of_budgets_not_reset++ if $budget->{budget_amount} > 0;
445 is( $number_of_budgets_not_reset, 0,
446 'CloneBudgetPeriod has reset all budgets (funds)' );
450 my $number_orders_moved = C4::Budgets::MoveOrders();
451 is( $number_orders_moved, undef, 'MoveOrders return undef if no arg passed' );
452 $number_orders_moved =
453 C4::Budgets::MoveOrders( { from_budget_period_id => $budget_period_id } );
454 is( $number_orders_moved, undef,
455 'MoveOrders return undef if only 1 arg passed' );
456 $number_orders_moved =
457 C4::Budgets::MoveOrders( { to_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 = C4::Budgets::MoveOrders(
462 from_budget_period_id => $budget_period_id,
463 to_budget_period_id => $budget_period_id
466 is( $number_orders_moved, undef,
467 'MoveOrders return undef if 2 budget period id are the same' );
469 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
471 budget_period_id => $budget_period_id,
472 budget_period_startdate => '2014-01-01',
473 budget_period_enddate => '2014-12-31',
477 my $report = C4::Budgets::MoveOrders(
479 from_budget_period_id => $budget_period_id,
480 to_budget_period_id => $budget_period_id_cloned,
481 move_remaining_unspent => 1,
484 is( scalar( @$report ), 6 , "MoveOrders has processed 6 funds" );
486 my $number_of_orders_moved = 0;
487 $number_of_orders_moved += scalar( @{ $_->{orders_moved} } ) for @$report;
488 is( $number_of_orders_moved, $number_of_orders_to_move, "MoveOrders has moved $number_of_orders_to_move orders" );
490 my @new_budget_ids = map { $_->{budget_id} }
491 @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
492 my @old_budget_ids = map { $_->{budget_id} }
493 @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
494 for my $budget_id ( keys %budgets ) {
495 for my $ordernumber ( @{ $budgets{$budget_id} } ) {
496 my $budget = GetBudgetByOrderNumber($ordernumber);
497 my $is_in_new_budgets = grep /^$budget->{budget_id}$/, @new_budget_ids;
498 my $is_in_old_budgets = grep /^$budget->{budget_id}$/, @old_budget_ids;
499 is( $is_in_new_budgets, 1, "MoveOrders changed the budget_id for order $ordernumber" );
500 is( $is_in_old_budgets, 0, "MoveOrders changed the budget_id for order $ordernumber" );
505 # MoveOrders with param move_remaining_unspent
506 my @new_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
507 my @old_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
509 for my $new_budget ( @new_budgets ) {
510 my ( $old_budget ) = map { $_->{budget_code} eq $new_budget->{budget_code} ? $_ : () } @old_budgets;
511 my $new_budget_amount_should_be = $old_budget->{budget_amount} * 2 - $old_budget->{total_spent};
512 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})" );
515 sub _get_dependencies {
516 my ($budget_hierarchy) = @_;
518 for my $budget (@$budget_hierarchy) {
519 if ( $budget->{child} ) {
520 my @sorted = sort @{ $budget->{child} };
521 for my $child_id (@sorted) {
522 push @{ $graph->{ $budget->{budget_name} }{children} },
523 _get_budgetname_by_id( $budget_hierarchy, $child_id );
526 push @{ $graph->{ $budget->{budget_name} }{parents} },
527 $budget->{parent_id};
532 sub _get_budgetname_by_id {
533 my ( $budgets, $budget_id ) = @_;
535 map { ( $_->{budget_id} eq $budget_id ) ? $_->{budget_name} : () }
540 # C4::Context->userenv