Bug 16123 - Add unit tests
[koha.git] / t / db_dependent / Budgets.t
1 #!/usr/bin/perl
2 use Modern::Perl;
3 use Test::More tests => 142;
4
5 BEGIN {
6     use_ok('C4::Budgets')
7 }
8 use C4::Context;
9 use C4::Biblio;
10 use C4::Bookseller;
11 use C4::Acquisition;
12 use C4::Members qw( AddMember );
13
14 use Koha::Acquisition::Order;
15
16 use t::lib::TestBuilder;
17
18 use YAML;
19
20 my $schema  = Koha::Database->new->schema;
21 $schema->storage->txn_begin;
22 my $builder = t::lib::TestBuilder->new;
23 my $dbh = C4::Context->dbh;
24 $dbh->do(q|DELETE FROM aqbudgetperiods|);
25 $dbh->do(q|DELETE FROM aqbudgets|);
26
27 my $library = $builder->build({
28     source => 'Branch',
29 });
30
31 # Mock userenv
32 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
33 my $userenv;
34 *C4::Context::userenv = \&Mock_userenv;
35 $userenv = { flags => 1, id => 'my_userid', branch => $library->{branchcode} };
36
37 #
38 # Budget Periods :
39 #
40
41 is( AddBudgetPeriod(), undef, 'AddBugetPeriod without argument returns undef' );
42 is( AddBudgetPeriod( { }  ), undef, 'AddBugetPeriod with an empty argument returns undef' );
43 my $bpid = AddBudgetPeriod({
44     budget_period_startdate => '2008-01-01',
45 });
46 is( $bpid, undef, 'AddBugetPeriod without end date returns undef' );
47 $bpid = AddBudgetPeriod({
48     budget_period_enddate => '2008-12-31',
49 });
50 is( $bpid, undef, 'AddBugetPeriod without start date returns undef' );
51 is( GetBudgetPeriod(0), undef ,'GetBudgetPeriod(0) returned undef : noactive BudgetPeriod' );
52 my $budgetperiods = GetBudgetPeriods();
53 is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
54
55 my $my_budgetperiod = {
56     budget_period_startdate   => '2008-01-01',
57     budget_period_enddate     => '2008-12-31',
58     budget_period_description => 'MAPERI',
59     budget_period_active      => 0,
60 };
61 $bpid = AddBudgetPeriod($my_budgetperiod);
62 isnt( $bpid, undef, 'AddBugetPeriod does not returns undef' );
63 my $budgetperiod = GetBudgetPeriod($bpid);
64 is( $budgetperiod->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'AddBudgetPeriod stores the start date correctly' );
65 is( $budgetperiod->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'AddBudgetPeriod stores the end date correctly' );
66 is( $budgetperiod->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'AddBudgetPeriod stores the description correctly' );
67 is( $budgetperiod->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'AddBudgetPeriod stores active correctly' );
68 is( GetBudgetPeriod(0), undef ,'GetBudgetPeriod(0) returned undef : noactive BudgetPeriod' );
69
70
71 $my_budgetperiod = {
72     budget_period_startdate   => '2009-01-01',
73     budget_period_enddate     => '2009-12-31',
74     budget_period_description => 'MODIF_MAPERI',
75     budget_period_active      => 1,
76 };
77 my $mod_status = ModBudgetPeriod($my_budgetperiod);
78 is( $mod_status, undef, 'ModBudgetPeriod without id returns undef' );
79
80 $my_budgetperiod->{budget_period_id} = $bpid;
81 $mod_status = ModBudgetPeriod($my_budgetperiod);
82 is( $mod_status, 1, 'ModBudgetPeriod returnis true' );
83 $budgetperiod = GetBudgetPeriod($bpid);
84 is( $budgetperiod->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'ModBudgetPeriod updates the start date correctly' );
85 is( $budgetperiod->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'ModBudgetPeriod updates the end date correctly' );
86 is( $budgetperiod->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'ModBudgetPeriod updates the description correctly' );
87 is( $budgetperiod->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'ModBudgetPeriod upates active correctly' );
88 isnt( GetBudgetPeriod(0), undef, 'GetBugetPeriods functions correctly' );
89
90
91 $budgetperiods = GetBudgetPeriods();
92 is( @$budgetperiods, 1, 'GetBudgetPeriods returns the correct number of budget periods' );
93 is( $budgetperiods->[0]->{budget_period_id}, $my_budgetperiod->{budget_period_id}, 'GetBudgetPeriods returns the id correctly' );
94 is( $budgetperiods->[0]->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'GetBudgetPeriods returns the start date correctly' );
95 is( $budgetperiods->[0]->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'GetBudgetPeriods returns the end date correctly' );
96 is( $budgetperiods->[0]->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'GetBudgetPeriods returns the description correctly' );
97 is( $budgetperiods->[0]->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'GetBudgetPeriods returns active correctly' );
98
99 is( DelBudgetPeriod($bpid), 1, 'DelBudgetPeriod returns true' );
100 $budgetperiods = GetBudgetPeriods();
101 is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
102
103
104 #
105 # Budget  :
106 #
107
108 # The budget hierarchy will be:
109 # budget_1
110 #   budget_11
111 #     budget_111
112 #   budget_12
113 # budget_2
114 #   budget_21
115
116 is( AddBudget(), undef, 'AddBuget without argument returns undef' );
117 my $budgets = GetBudgets();
118 is( @$budgets, 0, 'GetBudgets returns the correct number of budgets' );
119
120 $bpid = AddBudgetPeriod($my_budgetperiod);
121 my $my_budget = {
122     budget_code      => 'ABCD',
123     budget_amount    => '123.132000',
124     budget_name      => 'Periodiques',
125     budget_notes     => 'This is a note',
126     budget_period_id => $bpid,
127 };
128 my $budget_id = AddBudget($my_budget);
129 isnt( $budget_id, undef, 'AddBudget does not returns undef' );
130 my $budget = GetBudget($budget_id);
131 is( $budget->{budget_code}, $my_budget->{budget_code}, 'AddBudget stores the budget code correctly' );
132 is( $budget->{budget_amount}, $my_budget->{budget_amount}, 'AddBudget stores the budget amount correctly' );
133 is( $budget->{budget_name}, $my_budget->{budget_name}, 'AddBudget stores the budget name correctly' );
134 is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'AddBudget stores the budget notes correctly' );
135 is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'AddBudget stores the budget period id correctly' );
136
137
138 $my_budget = {
139     budget_code      => 'EFG',
140     budget_amount    => '321.231000',
141     budget_name      => 'Modified name',
142     budget_notes     => 'This is a modified note',
143     budget_period_id => $bpid,
144 };
145 $mod_status = ModBudget($my_budget);
146 is( $mod_status, undef, 'ModBudget without id returns undef' );
147
148 $my_budget->{budget_id} = $budget_id;
149 $mod_status = ModBudget($my_budget);
150 is( $mod_status, 1, 'ModBudget returns true' );
151 $budget = GetBudget($budget_id);
152 is( $budget->{budget_code}, $my_budget->{budget_code}, 'ModBudget updates the budget code correctly' );
153 is( $budget->{budget_amount}, $my_budget->{budget_amount}, 'ModBudget updates the budget amount correctly' );
154 is( $budget->{budget_name}, $my_budget->{budget_name}, 'ModBudget updates the budget name correctly' );
155 is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'ModBudget updates the budget notes correctly' );
156 is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'ModBudget updates the budget period id correctly' );
157
158
159 $budgets = GetBudgets();
160 is( @$budgets, 1, 'GetBudgets returns the correct number of budgets' );
161 is( $budgets->[0]->{budget_id}, $my_budget->{budget_id}, 'GetBudgets returns the budget id correctly' );
162 is( $budgets->[0]->{budget_code}, $my_budget->{budget_code}, 'GetBudgets returns the budget code correctly' );
163 is( $budgets->[0]->{budget_amount}, $my_budget->{budget_amount}, 'GetBudgets returns the budget amount correctly' );
164 is( $budgets->[0]->{budget_name}, $my_budget->{budget_name}, 'GetBudgets returns the budget name correctly' );
165 is( $budgets->[0]->{budget_notes}, $my_budget->{budget_notes}, 'GetBudgets returns the budget notes correctly' );
166 is( $budgets->[0]->{budget_period_id}, $my_budget->{budget_period_id}, 'GetBudgets returns the budget period id correctly' );
167
168 $budgets = GetBudgets( {budget_period_id => $bpid} );
169 is( @$budgets, 1, 'GetBudgets With Filter OK' );
170 $budgets = GetBudgets( {budget_period_id => $bpid}, {-asc => "budget_name"} );
171 is( @$budgets, 1, 'GetBudgets With Order OK' );
172 $budgets = GetBudgets( {budget_period_id => GetBudgetPeriod($bpid)->{budget_period_id}}, {-asc => "budget_name"} );
173 is( @$budgets, 1, 'GetBudgets With Order Getting Active budgetPeriod OK');
174
175
176 my $budget_name = GetBudgetName( $budget_id );
177 is($budget_name, $my_budget->{budget_name}, "Test the GetBudgetName routine");
178
179 my $budget_code = $my_budget->{budget_code};
180 my $budget_by_code = GetBudgetByCode( $budget_code );
181 is($budget_by_code->{budget_id}, $budget_id, "GetBudgetByCode, check id");
182 is($budget_by_code->{budget_notes}, $my_budget->{budget_notes}, "GetBudgetByCode, check notes");
183
184 my $second_budget_id = AddBudget({
185     budget_code      => "ZZZZ",
186     budget_amount    => "500.00",
187     budget_name      => "Art",
188     budget_notes     => "This is a note",
189     budget_period_id => $bpid,
190 });
191 isnt( $second_budget_id, undef, 'AddBudget does not returns undef' );
192
193 $budgets = GetBudgets( {budget_period_id => $bpid} );
194 ok( $budgets->[0]->{budget_name} lt $budgets->[1]->{budget_name}, 'default sort order for GetBudgets is by name' );
195
196 is( DelBudget($budget_id), 1, 'DelBudget returns true' );
197 $budgets = GetBudgets();
198 is( @$budgets, 1, 'GetBudgets returns the correct number of budget periods' );
199
200
201 # GetBudgetHierarchySpent and GetBudgetHierarchyOrdered
202 my $budget_period_total = 10_000;
203 my $budget_1_total = 1_000;
204 my $budget_11_total = 100;
205 my $budget_111_total = 50;
206 my $budget_12_total = 100;
207 my $budget_2_total = 2_000;
208
209 my $budget_period_id = AddBudgetPeriod(
210     {
211         budget_period_startdate   => '2013-01-01',
212         budget_period_enddate     => '2014-12-31',
213         budget_period_description => 'Budget Period',
214         budget_period_active      => 1,
215         budget_period_total       => $budget_period_total,
216     }
217 );
218 my $budget_id1 = AddBudget(
219     {
220         budget_code      => 'budget_1',
221         budget_name      => 'budget_1',
222         budget_period_id => $budget_period_id,
223         budget_parent_id => undef,
224         budget_amount    => $budget_1_total,
225     }
226 );
227 my $budget_id2 = AddBudget(
228     {
229         budget_code      => 'budget_2',
230         budget_name      => 'budget_2',
231         budget_period_id => $budget_period_id,
232         budget_parent_id => undef,
233         budget_amount    => $budget_2_total,
234     }
235 );
236 my $budget_id12 = AddBudget(
237     {
238         budget_code      => 'budget_12',
239         budget_name      => 'budget_12',
240         budget_period_id => $budget_period_id,
241         budget_parent_id => $budget_id1,
242         budget_amount    => $budget_12_total,
243     }
244 );
245 my $budget_id11 = AddBudget(
246     {
247         budget_code      => 'budget_11',
248         budget_name      => 'budget_11',
249         budget_period_id => $budget_period_id,
250         budget_parent_id => $budget_id1,
251         budget_amount    => $budget_11_total,
252     }
253 );
254 my $budget_id111 = AddBudget(
255     {
256         budget_code      => 'budget_111',
257         budget_name      => 'budget_111',
258         budget_period_id => $budget_period_id,
259         budget_parent_id => $budget_id11,
260         budget_owner_id  => 1,
261         budget_amount    => $budget_111_total,
262     }
263 );
264 my $budget_id21 = AddBudget(
265     {
266         budget_code      => 'budget_21',
267         budget_name      => 'budget_21',
268         budget_period_id => $budget_period_id,
269         budget_parent_id => $budget_id2,
270     }
271 );
272
273 my $booksellerid = C4::Bookseller::AddBookseller(
274     {
275         name         => "my vendor",
276         address1     => "bookseller's address",
277         phone        => "0123456",
278         active       => 1,
279         deliverytime => 5,
280     }
281 );
282
283 my $basketno = C4::Acquisition::NewBasket( $booksellerid, 1 );
284 my ( $biblionumber, $biblioitemnumber ) =
285   C4::Biblio::AddBiblio( MARC::Record->new, '' );
286
287 my @order_infos = (
288     {
289         budget_id => $budget_id1,
290         pending_quantity  => 1,
291         spent_quantity  => 0,
292     },
293     {
294         budget_id => $budget_id2,
295         pending_quantity  => 2,
296         spent_quantity  => 1,
297     },
298     {
299         budget_id => $budget_id11,
300         pending_quantity  => 3,
301         spent_quantity  => 4,
302     },
303     {
304         budget_id => $budget_id12,
305         pending_quantity  => 4,
306         spent_quantity  => 3,
307     },
308     {
309         budget_id => $budget_id111,
310         pending_quantity  => 2,
311         spent_quantity  => 1,
312     },
313
314     # No order for budget_21
315
316 );
317
318 my %budgets;
319 my $invoiceid = AddInvoice(invoicenumber => 'invoice_test_clone', booksellerid => $booksellerid, unknown => "unknown");
320 my $invoice = GetInvoice( $invoiceid );
321 my $item_price = 10;
322 my $item_quantity = 2;
323 my $number_of_orders_to_move = 0;
324 for my $infos (@order_infos) {
325     for ( 1 .. $infos->{pending_quantity} ) {
326         my $order = Koha::Acquisition::Order->new(
327             {
328                 basketno           => $basketno,
329                 biblionumber       => $biblionumber,
330                 budget_id          => $infos->{budget_id},
331                 order_internalnote => "internal note",
332                 order_vendornote   => "vendor note",
333                 quantity           => 2,
334                 cost_tax_included  => $item_price,
335                 rrp_tax_included   => $item_price,
336                 listprice          => $item_price,
337                 ecost_tax_include  => $item_price,
338                 discount           => 0,
339                 uncertainprice     => 0,
340             }
341         )->insert;
342         my $ordernumber = $order->{ordernumber};
343         push @{ $budgets{$infos->{budget_id}} }, $ordernumber;
344         $number_of_orders_to_move++;
345     }
346     for ( 1 .. $infos->{spent_quantity} ) {
347         my $order = Koha::Acquisition::Order->new(
348             {
349                 basketno           => $basketno,
350                 biblionumber       => $biblionumber,
351                 budget_id          => $infos->{budget_id},
352                 order_internalnote => "internal note",
353                 order_vendornote   => "vendor note",
354                 quantity           => $item_quantity,
355                 cost               => $item_price,
356                 rrp_tax_included   => $item_price,
357                 listprice          => $item_price,
358                 ecost_tax_included => $item_price,
359                 discount           => 0,
360                 uncertainprice     => 0,
361             }
362         )->insert;
363         my $ordernumber = $order->{ordernumber};
364         ModReceiveOrder({
365               biblionumber     => $biblionumber,
366               order            => $order,
367               budget_id        => $infos->{budget_id},
368               quantityreceived => $item_quantity,
369               invoice          => $invoice,
370               received_items   => [],
371         } );
372     }
373 }
374 is( GetBudgetHierarchySpent( $budget_id1 ), 160, "total spent for budget1 is 160" );
375 is( GetBudgetHierarchySpent( $budget_id11 ), 100, "total spent for budget11 is 100" );
376 is( GetBudgetHierarchySpent( $budget_id111 ), 20, "total spent for budget111 is 20" );
377
378 # CloneBudgetPeriod
379 my $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
380     {
381         budget_period_id        => $budget_period_id,
382         budget_period_startdate => '2014-01-01',
383         budget_period_enddate   => '2014-12-31',
384         budget_period_description => 'Budget Period Cloned',
385     }
386 );
387
388 my $budget_period_cloned = C4::Budgets::GetBudgetPeriod($budget_period_id_cloned);
389 is($budget_period_cloned->{budget_period_description}, 'Budget Period Cloned', 'Cloned budget\'s description is updated.');
390
391 my $budget_hierarchy        = GetBudgetHierarchy($budget_period_id);
392 my $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
393
394 is(
395     scalar(@$budget_hierarchy_cloned),
396     scalar(@$budget_hierarchy),
397     'CloneBudgetPeriod clones the same number of budgets (funds)'
398 );
399 is_deeply(
400     _get_dependencies($budget_hierarchy),
401     _get_dependencies($budget_hierarchy_cloned),
402     'CloneBudgetPeriod keeps the same dependencies order'
403 );
404
405 # CloneBudgetPeriod with param mark_original_budget_as_inactive
406 my $budget_period = C4::Budgets::GetBudgetPeriod($budget_period_id);
407 is( $budget_period->{budget_period_active}, 1,
408     'CloneBudgetPeriod does not mark as inactive the budgetperiod if not needed'
409 );
410
411 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
412 my $number_of_budgets_not_reset = 0;
413 for my $budget (@$budget_hierarchy_cloned) {
414     $number_of_budgets_not_reset++ if $budget->{budget_amount} > 0;
415 }
416 is( $number_of_budgets_not_reset, 5,
417     'CloneBudgetPeriod does not reset budgets (funds) if not needed' );
418
419 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
420     {
421         budget_period_id                 => $budget_period_id,
422         budget_period_startdate          => '2014-01-01',
423         budget_period_enddate            => '2014-12-31',
424         mark_original_budget_as_inactive => 1,
425     }
426 );
427
428 $budget_hierarchy        = GetBudgetHierarchy($budget_period_id);
429 is( $budget_hierarchy->[0]->{children}->[0]->{budget_name}, 'budget_11', 'GetBudgetHierarchy should return budgets ordered by name, first child is budget_11' );
430 is( $budget_hierarchy->[0]->{children}->[1]->{budget_name}, 'budget_12', 'GetBudgetHierarchy should return budgets ordered by name, second child is budget_12' );
431 is($budget_hierarchy->[0]->{budget_name},'budget_1','GetBudgetHierarchy should return budgets ordered by name, first budget is budget_1');
432 is($budget_hierarchy->[0]->{budget_level},'0','budget_level of budget (budget_1)  should be 0');
433 is($budget_hierarchy->[0]->{children}->[0]->{budget_level},'1','budget_level of first fund(budget_11)  should be 1');
434 is($budget_hierarchy->[0]->{children}->[1]->{budget_level},'1','budget_level of second fund(budget_12)  should be 1');
435 is($budget_hierarchy->[0]->{children}->[0]->{children}->[0]->{budget_level},'2','budget_level of  child fund budget_11 should be 2');
436 $budget_hierarchy        = GetBudgetHierarchy($budget_period_id);
437 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
438
439 is( scalar(@$budget_hierarchy_cloned), scalar(@$budget_hierarchy),
440 'CloneBudgetPeriod (with inactive param) clones the same number of budgets (funds)'
441 );
442 is_deeply(
443     _get_dependencies($budget_hierarchy),
444     _get_dependencies($budget_hierarchy_cloned),
445     'CloneBudgetPeriod (with inactive param) keeps the same dependencies order'
446 );
447 $budget_period = C4::Budgets::GetBudgetPeriod($budget_period_id);
448 is( $budget_period->{budget_period_active}, 0,
449     'CloneBudgetPeriod (with inactive param) marks as inactive the budgetperiod'
450 );
451
452 # CloneBudgetPeriod with param reset_all_budgets
453 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
454     {
455         budget_period_id        => $budget_period_id,
456         budget_period_startdate => '2014-01-01',
457         budget_period_enddate   => '2014-12-31',
458         reset_all_budgets         => 1,
459     }
460 );
461
462 $budget_hierarchy_cloned     = GetBudgetHierarchy($budget_period_id_cloned);
463 $number_of_budgets_not_reset = 0;
464 for my $budget (@$budget_hierarchy_cloned) {
465     $number_of_budgets_not_reset++ if $budget->{budget_amount} > 0;
466 }
467 is( $number_of_budgets_not_reset, 0,
468     'CloneBudgetPeriod has reset all budgets (funds)' );
469
470 #GetBudgetsByActivity
471 my $result=C4::Budgets::GetBudgetsByActivity(1);
472 isnt( $result, undef ,'GetBudgetsByActivity return correct value with parameter 1');
473 $result=C4::Budgets::GetBudgetsByActivity(0);
474  isnt( $result, undef ,'GetBudgetsByActivity return correct value with parameter 0');
475 $result=C4::Budgets::GetBudgetsByActivity();
476  is( $result, 0 , 'GetBudgetsByActivity return 0 with none parameter or other 0 or 1' );
477 DelBudget($budget_id);
478 DelBudgetPeriod($bpid);
479
480 # CloneBudgetPeriod with param amount_change_*
481 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
482     {
483         budget_period_id        => $budget_period_id,
484         budget_period_startdate => '2014-01-01',
485         budget_period_enddate   => '2014-12-31',
486         amount_change_percentage => 16,
487         amount_change_round_increment => 5,
488     }
489 );
490
491 $budget_period_cloned = C4::Budgets::GetBudgetPeriod($budget_period_id_cloned);
492 cmp_ok($budget_period_cloned->{budget_period_total}, '==', 11600, "CloneBudgetPeriod changed correctly budget amount");
493 $budget_hierarchy_cloned     = GetBudgetHierarchy($budget_period_id_cloned);
494 cmp_ok($budget_hierarchy_cloned->[0]->{budget_amount}, '==', 1160, "CloneBudgetPeriod changed correctly funds amounts");
495 cmp_ok($budget_hierarchy_cloned->[1]->{budget_amount}, '==', 115, "CloneBudgetPeriod changed correctly funds amounts");
496 cmp_ok($budget_hierarchy_cloned->[2]->{budget_amount}, '==', 55, "CloneBudgetPeriod changed correctly funds amounts");
497 cmp_ok($budget_hierarchy_cloned->[3]->{budget_amount}, '==', 115, "CloneBudgetPeriod changed correctly funds amounts");
498 cmp_ok($budget_hierarchy_cloned->[4]->{budget_amount}, '==', 2320, "CloneBudgetPeriod changed correctly funds amounts");
499 cmp_ok($budget_hierarchy_cloned->[5]->{budget_amount}, '==', 0, "CloneBudgetPeriod changed correctly funds amounts");
500
501 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
502     {
503         budget_period_id        => $budget_period_id,
504         budget_period_startdate => '2014-01-01',
505         budget_period_enddate   => '2014-12-31',
506         amount_change_percentage => 16,
507         amount_change_round_increment => 5,
508         reset_all_budgets => 1,
509     }
510 );
511 $budget_hierarchy_cloned     = GetBudgetHierarchy($budget_period_id_cloned);
512 cmp_ok($budget_hierarchy_cloned->[0]->{budget_amount}, '==', 0, "CloneBudgetPeriod reset all fund amounts");
513
514 # MoveOrders
515 my $number_orders_moved = C4::Budgets::MoveOrders();
516 is( $number_orders_moved, undef, 'MoveOrders return undef if no arg passed' );
517 $number_orders_moved =
518   C4::Budgets::MoveOrders( { from_budget_period_id => $budget_period_id } );
519 is( $number_orders_moved, undef,
520     'MoveOrders return undef if only 1 arg passed' );
521 $number_orders_moved =
522   C4::Budgets::MoveOrders( { to_budget_period_id => $budget_period_id } );
523 is( $number_orders_moved, undef,
524     'MoveOrders return undef if only 1 arg passed' );
525 $number_orders_moved = C4::Budgets::MoveOrders(
526     {
527         from_budget_period_id => $budget_period_id,
528         to_budget_period_id   => $budget_period_id
529     }
530 );
531 is( $number_orders_moved, undef,
532     'MoveOrders return undef if 2 budget period id are the same' );
533
534 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
535     {
536         budget_period_id        => $budget_period_id,
537         budget_period_startdate => '2014-01-01',
538         budget_period_enddate   => '2014-12-31',
539     }
540 );
541
542 my $report = C4::Budgets::MoveOrders(
543     {
544         from_budget_period_id  => $budget_period_id,
545         to_budget_period_id    => $budget_period_id_cloned,
546         move_remaining_unspent => 1,
547     }
548 );
549 is( scalar( @$report ), 6 , "MoveOrders has processed 6 funds" );
550
551 my $number_of_orders_moved = 0;
552 $number_of_orders_moved += scalar( @{ $_->{orders_moved} } ) for @$report;
553 is( $number_of_orders_moved, $number_of_orders_to_move, "MoveOrders has moved $number_of_orders_to_move orders" );
554
555 my @new_budget_ids = map { $_->{budget_id} }
556   @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
557 my @old_budget_ids = map { $_->{budget_id} }
558   @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
559 for my $budget_id ( keys %budgets ) {
560     for my $ordernumber ( @{ $budgets{$budget_id} } ) {
561         my $budget            = GetBudgetByOrderNumber($ordernumber);
562         my $is_in_new_budgets = grep /^$budget->{budget_id}$/, @new_budget_ids;
563         my $is_in_old_budgets = grep /^$budget->{budget_id}$/, @old_budget_ids;
564         is( $is_in_new_budgets, 1, "MoveOrders changed the budget_id for order $ordernumber" );
565         is( $is_in_old_budgets, 0, "MoveOrders changed the budget_id for order $ordernumber" );
566     }
567 }
568
569
570 # MoveOrders with param move_remaining_unspent
571 my @new_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
572 my @old_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
573
574 for my $new_budget ( @new_budgets ) {
575     my ( $old_budget ) = map { $_->{budget_code} eq $new_budget->{budget_code} ? $_ : () } @old_budgets;
576     my $new_budget_amount_should_be = $old_budget->{budget_amount} * 2 - $old_budget->{total_spent};
577     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})" );
578 }
579
580 # Test SetOwnerToFundHierarchy
581
582 my $categorycode = 'S';
583 my $branchcode = $library->{branchcode};
584 my $john_doe = C4::Members::AddMember(
585     cardnumber   => '123456',
586     firstname    => 'John',
587     surname      => 'Doe',
588     categorycode => $categorycode,
589     branchcode   => $branchcode,
590     dateofbirth  => '',
591     dateexpiry   => '9999-12-31',
592     userid       => 'john.doe'
593 );
594
595 C4::Budgets::SetOwnerToFundHierarchy( $budget_id1, $john_doe );
596 is( C4::Budgets::GetBudget($budget_id1)->{budget_owner_id},
597     $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 1 ($budget_id1)" );
598 is( C4::Budgets::GetBudget($budget_id11)->{budget_owner_id},
599     $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 11 ($budget_id11)" );
600 is( C4::Budgets::GetBudget($budget_id111)->{budget_owner_id},
601     $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 111 ($budget_id111)" );
602 is( C4::Budgets::GetBudget($budget_id12)->{budget_owner_id},
603     $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 12 ($budget_id12 )" );
604 is( C4::Budgets::GetBudget($budget_id2)->{budget_owner_id},
605     undef, "SetOwnerToFundHierarchy should not have set an owner for budget 2 ($budget_id2)" );
606 is( C4::Budgets::GetBudget($budget_id21)->{budget_owner_id},
607     undef, "SetOwnerToFundHierarchy should not have set an owner for budget 21 ($budget_id21)" );
608
609 my $jane_doe = C4::Members::AddMember(
610     cardnumber   => '789012',
611     firstname    => 'Jane',
612     surname      => 'Doe',
613     categorycode => $categorycode,
614     branchcode   => $branchcode,
615     dateofbirth  => '',
616     dateexpiry   => '9999-12-31',
617     userid       => 'jane.doe'
618 );
619
620 C4::Budgets::SetOwnerToFundHierarchy( $budget_id11, $jane_doe );
621 is( C4::Budgets::GetBudget($budget_id1)->{budget_owner_id},
622     $john_doe, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 1 ($budget_id1)" );
623 is( C4::Budgets::GetBudget($budget_id11)->{budget_owner_id},
624     $jane_doe, "SetOwnerToFundHierarchy should have set John Doe $jane_doe for budget 11 ($budget_id11)" );
625 is( C4::Budgets::GetBudget($budget_id111)->{budget_owner_id},
626     $jane_doe, "SetOwnerToFundHierarchy should have set John Doe $jane_doe for budget 111 ($budget_id111)" );
627 is( C4::Budgets::GetBudget($budget_id12)->{budget_owner_id},
628     $john_doe, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 12 ($budget_id12 )" );
629 is( C4::Budgets::GetBudget($budget_id2)->{budget_owner_id},
630     undef, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 2 ($budget_id2)" );
631 is( C4::Budgets::GetBudget($budget_id21)->{budget_owner_id},
632     undef, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 21 ($budget_id21)" );
633
634 # Test GetBudgetAuthCats
635
636 my $budgetPeriodId = AddBudgetPeriod({
637     budget_period_startdate   => '2008-01-01',
638     budget_period_enddate     => '2008-12-31',
639     budget_period_description => 'just another budget',
640     budget_period_active      => 0,
641 });
642
643 $budgets = GetBudgets();
644 my $i = 0;
645 for my $budget ( @$budgets )
646 {
647     $budget->{sort1_authcat} = "sort1_authcat_$i";
648     $budget->{sort2_authcat} = "sort2_authcat_$i";
649     $budget->{budget_period_id} = $budgetPeriodId;
650     ModBudget( $budget );
651     $i++;
652 }
653
654 my $authCat = GetBudgetAuthCats($budgetPeriodId);
655
656 is( scalar @{$authCat}, $i * 2, "GetBudgetAuthCats returns only non-empty sorting categories (no empty authCat in db)" );
657
658 $i = 0;
659 for my $budget ( @$budgets )
660 {
661     $budget->{sort1_authcat} = "sort_authcat_$i";
662     $budget->{sort2_authcat} = "sort_authcat_$i";
663     $budget->{budget_period_id} = $budgetPeriodId;
664     ModBudget( $budget );
665     $i++;
666 }
667
668 $authCat = GetBudgetAuthCats($budgetPeriodId);
669 is( scalar @$authCat, scalar @$budgets, "GetBudgetAuthCats returns distinct authCat" );
670
671 $i = 0;
672 for my $budget ( @$budgets )
673 {
674     $budget->{sort1_authcat} = "sort1_authcat_$i";
675     $budget->{sort2_authcat} = "";
676     $budget->{budget_period_id} = $budgetPeriodId;
677     ModBudget( $budget );
678     $i++;
679 }
680
681 $authCat = GetBudgetAuthCats($budgetPeriodId);
682
683 is( scalar @{$authCat}, $i, "GetBudgetAuthCats returns only non-empty sorting categories (empty sort2_authcat on all records)" );
684
685 $i = 0;
686 for my $budget ( @$budgets )
687 {
688     $budget->{sort1_authcat} = "";
689     $budget->{sort2_authcat} = "";
690     $budget->{budget_period_id} = $budgetPeriodId;
691     ModBudget( $budget );
692     $i++;
693 }
694
695 $authCat = GetBudgetAuthCats($budgetPeriodId);
696
697 is( scalar @{$authCat}, 0, "GetBudgetAuthCats returns only non-empty sorting categories (all empty)" );
698
699 # /Test GetBudgetAuthCats
700
701 sub _get_dependencies {
702     my ($budget_hierarchy) = @_;
703     my $graph;
704     for my $budget (@$budget_hierarchy) {
705         if ( $budget->{child} ) {
706             my @sorted = sort @{ $budget->{child} };
707             for my $child_id (@sorted) {
708                 push @{ $graph->{ $budget->{budget_name} }{children} },
709                   _get_budgetname_by_id( $budget_hierarchy, $child_id );
710             }
711         }
712         push @{ $graph->{ $budget->{budget_name} }{parents} },
713           $budget->{parent_id};
714     }
715     return $graph;
716 }
717
718 sub _get_budgetname_by_id {
719     my ( $budgets, $budget_id ) = @_;
720     my ($budget_name) =
721       map { ( $_->{budget_id} eq $budget_id ) ? $_->{budget_name} : () }
722       @$budgets;
723     return $budget_name;
724 }
725
726 # C4::Context->userenv
727 sub Mock_userenv {
728     return $userenv;
729 }