Bug 18999: Add regression tests
[koha.git] / t / db_dependent / Budgets.t
1 #!/usr/bin/perl
2 use Modern::Perl;
3 use Test::More tests => 144;
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); #this is an active budget
121
122 my $my_budget = {
123     budget_code      => 'ABCD',
124     budget_amount    => '123.132000',
125     budget_name      => 'Periodiques',
126     budget_notes     => 'This is a note',
127     budget_period_id => $bpid,
128 };
129 my $budget_id = AddBudget($my_budget);
130 isnt( $budget_id, undef, 'AddBudget does not returns undef' );
131 my $budget = GetBudget($budget_id);
132 is( $budget->{budget_code}, $my_budget->{budget_code}, 'AddBudget stores the budget code correctly' );
133 is( $budget->{budget_amount}, $my_budget->{budget_amount}, 'AddBudget stores the budget amount correctly' );
134 is( $budget->{budget_name}, $my_budget->{budget_name}, 'AddBudget stores the budget name correctly' );
135 is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'AddBudget stores the budget notes correctly' );
136 is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'AddBudget stores the budget period id correctly' );
137
138
139 $my_budget = {
140     budget_code      => 'EFG',
141     budget_amount    => '321.231000',
142     budget_name      => 'Modified name',
143     budget_notes     => 'This is a modified note',
144     budget_period_id => $bpid,
145 };
146 $mod_status = ModBudget($my_budget);
147 is( $mod_status, undef, 'ModBudget without id returns undef' );
148
149 $my_budget->{budget_id} = $budget_id;
150 $mod_status = ModBudget($my_budget);
151 is( $mod_status, 1, 'ModBudget returns true' );
152 $budget = GetBudget($budget_id);
153 is( $budget->{budget_code}, $my_budget->{budget_code}, 'ModBudget updates the budget code correctly' );
154 is( $budget->{budget_amount}, $my_budget->{budget_amount}, 'ModBudget updates the budget amount correctly' );
155 is( $budget->{budget_name}, $my_budget->{budget_name}, 'ModBudget updates the budget name correctly' );
156 is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'ModBudget updates the budget notes correctly' );
157 is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'ModBudget updates the budget period id correctly' );
158
159
160 $budgets = GetBudgets();
161 is( @$budgets, 1, 'GetBudgets returns the correct number of budgets' );
162 is( $budgets->[0]->{budget_id}, $my_budget->{budget_id}, 'GetBudgets returns the budget id correctly' );
163 is( $budgets->[0]->{budget_code}, $my_budget->{budget_code}, 'GetBudgets returns the budget code correctly' );
164 is( $budgets->[0]->{budget_amount}, $my_budget->{budget_amount}, 'GetBudgets returns the budget amount correctly' );
165 is( $budgets->[0]->{budget_name}, $my_budget->{budget_name}, 'GetBudgets returns the budget name correctly' );
166 is( $budgets->[0]->{budget_notes}, $my_budget->{budget_notes}, 'GetBudgets returns the budget notes correctly' );
167 is( $budgets->[0]->{budget_period_id}, $my_budget->{budget_period_id}, 'GetBudgets returns the budget period id correctly' );
168
169 $budgets = GetBudgets( {budget_period_id => $bpid} );
170 is( @$budgets, 1, 'GetBudgets With Filter OK' );
171 $budgets = GetBudgets( {budget_period_id => $bpid}, {-asc => "budget_name"} );
172 is( @$budgets, 1, 'GetBudgets With Order OK' );
173 $budgets = GetBudgets( {budget_period_id => GetBudgetPeriod($bpid)->{budget_period_id}}, {-asc => "budget_name"} );
174 is( @$budgets, 1, 'GetBudgets With Order Getting Active budgetPeriod OK');
175
176
177 my $budget_name = GetBudgetName( $budget_id );
178 is($budget_name, $my_budget->{budget_name}, "Test the GetBudgetName routine");
179
180 my $my_inactive_budgetperiod = { #let's add an inactive
181     budget_period_startdate   => '2010-01-01',
182     budget_period_enddate     => '2010-12-31',
183     budget_period_description => 'MODIF_MAPERI',
184     budget_period_active      => 0,
185 };
186 my $bpid_i = AddBudgetPeriod($my_inactive_budgetperiod); #this is an inactive budget
187
188 my $my_budget_inactive = {
189     budget_code      => 'EFG',
190     budget_amount    => '123.132000',
191     budget_name      => 'Periodiques',
192     budget_notes     => 'This is a note',
193     budget_period_id => $bpid_i,
194 };
195 my $budget_id_inactive = AddBudget($my_budget_inactive);
196
197 my $budget_code = $my_budget->{budget_code};
198 my $budget_by_code = GetBudgetByCode( $budget_code );
199 is($budget_by_code->{budget_id}, $budget_id, "GetBudgetByCode, check id"); #this should match the active budget, not the inactive
200 is($budget_by_code->{budget_notes}, $my_budget->{budget_notes}, "GetBudgetByCode, check notes");
201
202 my $second_budget_id = AddBudget({
203     budget_code      => "ZZZZ",
204     budget_amount    => "500.00",
205     budget_name      => "Art",
206     budget_notes     => "This is a note",
207     budget_period_id => $bpid,
208 });
209 isnt( $second_budget_id, undef, 'AddBudget does not returns undef' );
210
211 $budgets = GetBudgets( {budget_period_id => $bpid} );
212 ok( $budgets->[0]->{budget_name} lt $budgets->[1]->{budget_name}, 'default sort order for GetBudgets is by name' );
213
214 is( DelBudget($budget_id), 1, 'DelBudget returns true' );
215 $budgets = GetBudgets();
216 is( @$budgets, 2, 'GetBudgets returns the correct number of budget periods' );
217
218
219 # GetBudgetHierarchySpent and GetBudgetHierarchyOrdered
220 my $budget_period_total = 10_000;
221 my $budget_1_total = 1_000;
222 my $budget_11_total = 100;
223 my $budget_111_total = 50;
224 my $budget_12_total = 100;
225 my $budget_2_total = 2_000;
226
227 my $budget_period_id = AddBudgetPeriod(
228     {
229         budget_period_startdate   => '2013-01-01',
230         budget_period_enddate     => '2014-12-31',
231         budget_period_description => 'Budget Period',
232         budget_period_active      => 1,
233         budget_period_total       => $budget_period_total,
234     }
235 );
236 my $budget_id1 = AddBudget(
237     {
238         budget_code      => 'budget_1',
239         budget_name      => 'budget_1',
240         budget_period_id => $budget_period_id,
241         budget_parent_id => undef,
242         budget_amount    => $budget_1_total,
243     }
244 );
245 my $budget_id2 = AddBudget(
246     {
247         budget_code      => 'budget_2',
248         budget_name      => 'budget_2',
249         budget_period_id => $budget_period_id,
250         budget_parent_id => undef,
251         budget_amount    => $budget_2_total,
252     }
253 );
254 my $budget_id12 = AddBudget(
255     {
256         budget_code      => 'budget_12',
257         budget_name      => 'budget_12',
258         budget_period_id => $budget_period_id,
259         budget_parent_id => $budget_id1,
260         budget_amount    => $budget_12_total,
261     }
262 );
263 my $budget_id11 = AddBudget(
264     {
265         budget_code      => 'budget_11',
266         budget_name      => 'budget_11',
267         budget_period_id => $budget_period_id,
268         budget_parent_id => $budget_id1,
269         budget_amount    => $budget_11_total,
270     }
271 );
272 my $budget_id111 = AddBudget(
273     {
274         budget_code      => 'budget_111',
275         budget_name      => 'budget_111',
276         budget_period_id => $budget_period_id,
277         budget_parent_id => $budget_id11,
278         budget_owner_id  => 1,
279         budget_amount    => $budget_111_total,
280     }
281 );
282 my $budget_id21 = AddBudget(
283     {
284         budget_code      => 'budget_21',
285         budget_name      => 'budget_21',
286         budget_period_id => $budget_period_id,
287         budget_parent_id => $budget_id2,
288     }
289 );
290
291 my $booksellerid = C4::Bookseller::AddBookseller(
292     {
293         name         => "my vendor",
294         address1     => "bookseller's address",
295         phone        => "0123456",
296         active       => 1,
297         deliverytime => 5,
298     }
299 );
300
301 my $basketno = C4::Acquisition::NewBasket( $booksellerid, 1 );
302 my ( $biblionumber, $biblioitemnumber ) =
303   C4::Biblio::AddBiblio( MARC::Record->new, '' );
304
305 my @order_infos = (
306     {
307         budget_id => $budget_id1,
308         pending_quantity  => 1,
309         spent_quantity  => 0,
310     },
311     {
312         budget_id => $budget_id2,
313         pending_quantity  => 2,
314         spent_quantity  => 1,
315     },
316     {
317         budget_id => $budget_id11,
318         pending_quantity  => 3,
319         spent_quantity  => 4,
320     },
321     {
322         budget_id => $budget_id12,
323         pending_quantity  => 4,
324         spent_quantity  => 3,
325     },
326     {
327         budget_id => $budget_id111,
328         pending_quantity  => 2,
329         spent_quantity  => 1,
330     },
331
332     # No order for budget_21
333
334 );
335
336 my %budgets;
337 my $invoiceid = AddInvoice(invoicenumber => 'invoice_test_clone', booksellerid => $booksellerid, unknown => "unknown");
338 my $invoice = GetInvoice( $invoiceid );
339 my $item_price = 10;
340 my $item_quantity = 2;
341 my $number_of_orders_to_move = 0;
342 for my $infos (@order_infos) {
343     for ( 1 .. $infos->{pending_quantity} ) {
344         my $order = Koha::Acquisition::Order->new(
345             {
346                 basketno           => $basketno,
347                 biblionumber       => $biblionumber,
348                 budget_id          => $infos->{budget_id},
349                 order_internalnote => "internal note",
350                 order_vendornote   => "vendor note",
351                 quantity           => 2,
352                 cost_tax_included  => $item_price,
353                 rrp_tax_included   => $item_price,
354                 listprice          => $item_price,
355                 ecost_tax_include  => $item_price,
356                 discount           => 0,
357                 uncertainprice     => 0,
358             }
359         )->insert;
360         my $ordernumber = $order->{ordernumber};
361         push @{ $budgets{$infos->{budget_id}} }, $ordernumber;
362         $number_of_orders_to_move++;
363     }
364     for ( 1 .. $infos->{spent_quantity} ) {
365         my $order = Koha::Acquisition::Order->new(
366             {
367                 basketno           => $basketno,
368                 biblionumber       => $biblionumber,
369                 budget_id          => $infos->{budget_id},
370                 order_internalnote => "internal note",
371                 order_vendornote   => "vendor note",
372                 quantity           => $item_quantity,
373                 cost               => $item_price,
374                 rrp_tax_included   => $item_price,
375                 listprice          => $item_price,
376                 ecost_tax_included => $item_price,
377                 discount           => 0,
378                 uncertainprice     => 0,
379             }
380         )->insert;
381         my $ordernumber = $order->{ordernumber};
382         ModReceiveOrder({
383               biblionumber     => $biblionumber,
384               order            => $order,
385               budget_id        => $infos->{budget_id},
386               quantityreceived => $item_quantity,
387               invoice          => $invoice,
388               received_items   => [],
389         } );
390     }
391 }
392 is( GetBudgetHierarchySpent( $budget_id1 ), 160, "total spent for budget1 is 160" );
393 is( GetBudgetHierarchySpent( $budget_id11 ), 100, "total spent for budget11 is 100" );
394 is( GetBudgetHierarchySpent( $budget_id111 ), 20, "total spent for budget111 is 20" );
395
396 # GetBudgetSpent and GetBudgetOrdered
397 my $budget_period_amount = 100;
398 my $budget_amount = 50;
399
400 my $budget = AddBudgetPeriod(
401     {
402         budget_period_startdate   => '2017-08-22',
403         budget_period_enddate     => '2018-08-22',
404         budget_period_description => 'Test budget',
405         budget_period_active      => 1,
406         budget_period_total       => $budget_period_amount,
407     }
408 );
409
410 my $fund = AddBudget(
411     {
412         budget_code       => 'Test fund',
413         budget_name       => 'Test fund',
414         budget_period_id  => $budget,
415         budget_parent_id  => undef,
416         budget_amount     => $budget_amount,
417     }
418 );
419
420 my $vendor = Koha::Acquisition::Bookseller->new(
421     {
422         name         => "test vendor",
423         address1     => "test address",
424         phone        => "0123456",
425         active       => 1,
426         deliverytime => 5,
427     }
428 )->store;
429
430 my $vendorid = $vendor->id;
431
432 my $basketnumber = C4::Acquisition::NewBasket( $vendorid, 1 );
433 my ( $biblio, $biblioitem ) = C4::Biblio::AddBiblio( MARC::Record->new, '' );
434
435 my @orders = (
436     {
437         budget_id  => $fund,
438         pending_quantity => 1,
439         spent_quantity => 0,
440     },
441 );
442
443 my $invoiceident = AddInvoice( invoicenumber => 'invoice_test_clone', booksellerid => $vendorid, shipmentdate => '2017-08-22', shipmentcost => 6, shipmentcost_budgetid => $fund );
444 my $test_invoice = GetInvoice( $invoiceident );
445 my $individual_item_price = 10;
446
447 my $order = Koha::Acquisition::Order->new(
448    {
449       basketno           => $basketnumber,
450       biblionumber       => $biblio,
451       budget_id          => $fund,
452       order_internalnote => "internalnote",
453       order_vendornote   => "vendor note",
454       quantity           => 2,
455       cost_tax_included  => $individual_item_price,
456       rrp_tax_included   => $individual_item_price,
457       listprice          => $individual_item_price,
458       ecost_tax_included => $individual_item_price,
459       discount           => 0,
460       uncertainprice     => 0,
461    }
462 )->insert;
463
464 ModReceiveOrder({
465    bibionumber       => $biblio,
466    order             => $order,
467    budget_id         => $fund,
468    quantityreceived  => 2,
469    invoice           => $test_invoice,
470    received_items    => [],
471 } );
472
473 is ( GetBudgetSpent( $fund ), 6, "total shipping cost is 6");
474 is ( GetBudgetOrdered( $fund ), '20.000000', "total ordered price is 20");
475
476
477 # CloneBudgetPeriod
478 my $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
479     {
480         budget_period_id        => $budget_period_id,
481         budget_period_startdate => '2014-01-01',
482         budget_period_enddate   => '2014-12-31',
483         budget_period_description => 'Budget Period Cloned',
484     }
485 );
486
487 my $budget_period_cloned = C4::Budgets::GetBudgetPeriod($budget_period_id_cloned);
488 is($budget_period_cloned->{budget_period_description}, 'Budget Period Cloned', 'Cloned budget\'s description is updated.');
489
490 my $budget_hierarchy        = GetBudgetHierarchy($budget_period_id);
491 my $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
492
493 is(
494     scalar(@$budget_hierarchy_cloned),
495     scalar(@$budget_hierarchy),
496     'CloneBudgetPeriod clones the same number of budgets (funds)'
497 );
498 is_deeply(
499     _get_dependencies($budget_hierarchy),
500     _get_dependencies($budget_hierarchy_cloned),
501     'CloneBudgetPeriod keeps the same dependencies order'
502 );
503
504 # CloneBudgetPeriod with param mark_original_budget_as_inactive
505 my $budget_period = C4::Budgets::GetBudgetPeriod($budget_period_id);
506 is( $budget_period->{budget_period_active}, 1,
507     'CloneBudgetPeriod does not mark as inactive the budgetperiod if not needed'
508 );
509
510 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
511 my $number_of_budgets_not_reset = 0;
512 for my $budget (@$budget_hierarchy_cloned) {
513     $number_of_budgets_not_reset++ if $budget->{budget_amount} > 0;
514 }
515 is( $number_of_budgets_not_reset, 5,
516     'CloneBudgetPeriod does not reset budgets (funds) if not needed' );
517
518 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
519     {
520         budget_period_id                 => $budget_period_id,
521         budget_period_startdate          => '2014-01-01',
522         budget_period_enddate            => '2014-12-31',
523         mark_original_budget_as_inactive => 1,
524     }
525 );
526
527 $budget_hierarchy        = GetBudgetHierarchy($budget_period_id);
528 is( $budget_hierarchy->[0]->{children}->[0]->{budget_name}, 'budget_11', 'GetBudgetHierarchy should return budgets ordered by name, first child is budget_11' );
529 is( $budget_hierarchy->[0]->{children}->[1]->{budget_name}, 'budget_12', 'GetBudgetHierarchy should return budgets ordered by name, second child is budget_12' );
530 is($budget_hierarchy->[0]->{budget_name},'budget_1','GetBudgetHierarchy should return budgets ordered by name, first budget is budget_1');
531 is($budget_hierarchy->[0]->{budget_level},'0','budget_level of budget (budget_1)  should be 0');
532 is($budget_hierarchy->[0]->{children}->[0]->{budget_level},'1','budget_level of first fund(budget_11)  should be 1');
533 is($budget_hierarchy->[0]->{children}->[1]->{budget_level},'1','budget_level of second fund(budget_12)  should be 1');
534 is($budget_hierarchy->[0]->{children}->[0]->{children}->[0]->{budget_level},'2','budget_level of  child fund budget_11 should be 2');
535 $budget_hierarchy        = GetBudgetHierarchy($budget_period_id);
536 $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
537
538 is( scalar(@$budget_hierarchy_cloned), scalar(@$budget_hierarchy),
539 'CloneBudgetPeriod (with inactive param) clones the same number of budgets (funds)'
540 );
541 is_deeply(
542     _get_dependencies($budget_hierarchy),
543     _get_dependencies($budget_hierarchy_cloned),
544     'CloneBudgetPeriod (with inactive param) keeps the same dependencies order'
545 );
546 $budget_period = C4::Budgets::GetBudgetPeriod($budget_period_id);
547 is( $budget_period->{budget_period_active}, 0,
548     'CloneBudgetPeriod (with inactive param) marks as inactive the budgetperiod'
549 );
550
551 # CloneBudgetPeriod with param reset_all_budgets
552 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
553     {
554         budget_period_id        => $budget_period_id,
555         budget_period_startdate => '2014-01-01',
556         budget_period_enddate   => '2014-12-31',
557         reset_all_budgets         => 1,
558     }
559 );
560
561 $budget_hierarchy_cloned     = GetBudgetHierarchy($budget_period_id_cloned);
562 $number_of_budgets_not_reset = 0;
563 for my $budget (@$budget_hierarchy_cloned) {
564     $number_of_budgets_not_reset++ if $budget->{budget_amount} > 0;
565 }
566 is( $number_of_budgets_not_reset, 0,
567     'CloneBudgetPeriod has reset all budgets (funds)' );
568
569 #GetBudgetsByActivity
570 my $result=C4::Budgets::GetBudgetsByActivity(1);
571 isnt( $result, undef ,'GetBudgetsByActivity return correct value with parameter 1');
572 $result=C4::Budgets::GetBudgetsByActivity(0);
573  isnt( $result, undef ,'GetBudgetsByActivity return correct value with parameter 0');
574 $result=C4::Budgets::GetBudgetsByActivity();
575  is( $result, 0 , 'GetBudgetsByActivity return 0 with none parameter or other 0 or 1' );
576 DelBudget($budget_id);
577 DelBudgetPeriod($bpid);
578
579 # CloneBudgetPeriod with param amount_change_*
580 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
581     {
582         budget_period_id        => $budget_period_id,
583         budget_period_startdate => '2014-01-01',
584         budget_period_enddate   => '2014-12-31',
585         amount_change_percentage => 16,
586         amount_change_round_increment => 5,
587     }
588 );
589
590 $budget_period_cloned = C4::Budgets::GetBudgetPeriod($budget_period_id_cloned);
591 cmp_ok($budget_period_cloned->{budget_period_total}, '==', 11600, "CloneBudgetPeriod changed correctly budget amount");
592 $budget_hierarchy_cloned     = GetBudgetHierarchy($budget_period_id_cloned);
593 cmp_ok($budget_hierarchy_cloned->[0]->{budget_amount}, '==', 1160, "CloneBudgetPeriod changed correctly funds amounts");
594 cmp_ok($budget_hierarchy_cloned->[1]->{budget_amount}, '==', 115, "CloneBudgetPeriod changed correctly funds amounts");
595 cmp_ok($budget_hierarchy_cloned->[2]->{budget_amount}, '==', 55, "CloneBudgetPeriod changed correctly funds amounts");
596 cmp_ok($budget_hierarchy_cloned->[3]->{budget_amount}, '==', 115, "CloneBudgetPeriod changed correctly funds amounts");
597 cmp_ok($budget_hierarchy_cloned->[4]->{budget_amount}, '==', 2320, "CloneBudgetPeriod changed correctly funds amounts");
598 cmp_ok($budget_hierarchy_cloned->[5]->{budget_amount}, '==', 0, "CloneBudgetPeriod changed correctly funds amounts");
599
600 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
601     {
602         budget_period_id        => $budget_period_id,
603         budget_period_startdate => '2014-01-01',
604         budget_period_enddate   => '2014-12-31',
605         amount_change_percentage => 16,
606         amount_change_round_increment => 5,
607         reset_all_budgets => 1,
608     }
609 );
610 $budget_hierarchy_cloned     = GetBudgetHierarchy($budget_period_id_cloned);
611 cmp_ok($budget_hierarchy_cloned->[0]->{budget_amount}, '==', 0, "CloneBudgetPeriod reset all fund amounts");
612
613 # MoveOrders
614 my $number_orders_moved = C4::Budgets::MoveOrders();
615 is( $number_orders_moved, undef, 'MoveOrders return undef if no arg passed' );
616 $number_orders_moved =
617   C4::Budgets::MoveOrders( { from_budget_period_id => $budget_period_id } );
618 is( $number_orders_moved, undef,
619     'MoveOrders return undef if only 1 arg passed' );
620 $number_orders_moved =
621   C4::Budgets::MoveOrders( { to_budget_period_id => $budget_period_id } );
622 is( $number_orders_moved, undef,
623     'MoveOrders return undef if only 1 arg passed' );
624 $number_orders_moved = C4::Budgets::MoveOrders(
625     {
626         from_budget_period_id => $budget_period_id,
627         to_budget_period_id   => $budget_period_id
628     }
629 );
630 is( $number_orders_moved, undef,
631     'MoveOrders return undef if 2 budget period id are the same' );
632
633 $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
634     {
635         budget_period_id        => $budget_period_id,
636         budget_period_startdate => '2014-01-01',
637         budget_period_enddate   => '2014-12-31',
638     }
639 );
640
641 my $report = C4::Budgets::MoveOrders(
642     {
643         from_budget_period_id  => $budget_period_id,
644         to_budget_period_id    => $budget_period_id_cloned,
645         move_remaining_unspent => 1,
646     }
647 );
648 is( scalar( @$report ), 6 , "MoveOrders has processed 6 funds" );
649
650 my $number_of_orders_moved = 0;
651 $number_of_orders_moved += scalar( @{ $_->{orders_moved} } ) for @$report;
652 is( $number_of_orders_moved, $number_of_orders_to_move, "MoveOrders has moved $number_of_orders_to_move orders" );
653
654 my @new_budget_ids = map { $_->{budget_id} }
655   @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
656 my @old_budget_ids = map { $_->{budget_id} }
657   @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
658 for my $budget_id ( keys %budgets ) {
659     for my $ordernumber ( @{ $budgets{$budget_id} } ) {
660         my $budget            = GetBudgetByOrderNumber($ordernumber);
661         my $is_in_new_budgets = grep /^$budget->{budget_id}$/, @new_budget_ids;
662         my $is_in_old_budgets = grep /^$budget->{budget_id}$/, @old_budget_ids;
663         is( $is_in_new_budgets, 1, "MoveOrders changed the budget_id for order $ordernumber" );
664         is( $is_in_old_budgets, 0, "MoveOrders changed the budget_id for order $ordernumber" );
665     }
666 }
667
668
669 # MoveOrders with param move_remaining_unspent
670 my @new_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id_cloned) };
671 my @old_budgets = @{ C4::Budgets::GetBudgetHierarchy($budget_period_id) };
672
673 for my $new_budget ( @new_budgets ) {
674     my ( $old_budget ) = map { $_->{budget_code} eq $new_budget->{budget_code} ? $_ : () } @old_budgets;
675     my $new_budget_amount_should_be = $old_budget->{budget_amount} * 2 - $old_budget->{total_spent};
676     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})" );
677 }
678
679 # Test SetOwnerToFundHierarchy
680
681 my $categorycode = 'S';
682 my $branchcode = $library->{branchcode};
683 my $john_doe = C4::Members::AddMember(
684     cardnumber   => '123456',
685     firstname    => 'John',
686     surname      => 'Doe',
687     categorycode => $categorycode,
688     branchcode   => $branchcode,
689     dateofbirth  => '',
690     dateexpiry   => '9999-12-31',
691     userid       => 'john.doe'
692 );
693
694 C4::Budgets::SetOwnerToFundHierarchy( $budget_id1, $john_doe );
695 is( C4::Budgets::GetBudget($budget_id1)->{budget_owner_id},
696     $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 1 ($budget_id1)" );
697 is( C4::Budgets::GetBudget($budget_id11)->{budget_owner_id},
698     $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 11 ($budget_id11)" );
699 is( C4::Budgets::GetBudget($budget_id111)->{budget_owner_id},
700     $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 111 ($budget_id111)" );
701 is( C4::Budgets::GetBudget($budget_id12)->{budget_owner_id},
702     $john_doe, "SetOwnerToFundHierarchy should have set John Doe for budget 12 ($budget_id12 )" );
703 is( C4::Budgets::GetBudget($budget_id2)->{budget_owner_id},
704     undef, "SetOwnerToFundHierarchy should not have set an owner for budget 2 ($budget_id2)" );
705 is( C4::Budgets::GetBudget($budget_id21)->{budget_owner_id},
706     undef, "SetOwnerToFundHierarchy should not have set an owner for budget 21 ($budget_id21)" );
707
708 my $jane_doe = C4::Members::AddMember(
709     cardnumber   => '789012',
710     firstname    => 'Jane',
711     surname      => 'Doe',
712     categorycode => $categorycode,
713     branchcode   => $branchcode,
714     dateofbirth  => '',
715     dateexpiry   => '9999-12-31',
716     userid       => 'jane.doe'
717 );
718
719 C4::Budgets::SetOwnerToFundHierarchy( $budget_id11, $jane_doe );
720 is( C4::Budgets::GetBudget($budget_id1)->{budget_owner_id},
721     $john_doe, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 1 ($budget_id1)" );
722 is( C4::Budgets::GetBudget($budget_id11)->{budget_owner_id},
723     $jane_doe, "SetOwnerToFundHierarchy should have set John Doe $jane_doe for budget 11 ($budget_id11)" );
724 is( C4::Budgets::GetBudget($budget_id111)->{budget_owner_id},
725     $jane_doe, "SetOwnerToFundHierarchy should have set John Doe $jane_doe for budget 111 ($budget_id111)" );
726 is( C4::Budgets::GetBudget($budget_id12)->{budget_owner_id},
727     $john_doe, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 12 ($budget_id12 )" );
728 is( C4::Budgets::GetBudget($budget_id2)->{budget_owner_id},
729     undef, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 2 ($budget_id2)" );
730 is( C4::Budgets::GetBudget($budget_id21)->{budget_owner_id},
731     undef, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 21 ($budget_id21)" );
732
733 # Test GetBudgetAuthCats
734
735 my $budgetPeriodId = AddBudgetPeriod({
736     budget_period_startdate   => '2008-01-01',
737     budget_period_enddate     => '2008-12-31',
738     budget_period_description => 'just another budget',
739     budget_period_active      => 0,
740 });
741
742 $budgets = GetBudgets();
743 my $i = 0;
744 for my $budget ( @$budgets )
745 {
746     $budget->{sort1_authcat} = "sort1_authcat_$i";
747     $budget->{sort2_authcat} = "sort2_authcat_$i";
748     $budget->{budget_period_id} = $budgetPeriodId;
749     ModBudget( $budget );
750     $i++;
751 }
752
753 my $authCat = GetBudgetAuthCats($budgetPeriodId);
754
755 is( scalar @{$authCat}, $i * 2, "GetBudgetAuthCats returns only non-empty sorting categories (no empty authCat in db)" );
756
757 $i = 0;
758 for my $budget ( @$budgets )
759 {
760     $budget->{sort1_authcat} = "sort_authcat_$i";
761     $budget->{sort2_authcat} = "sort_authcat_$i";
762     $budget->{budget_period_id} = $budgetPeriodId;
763     ModBudget( $budget );
764     $i++;
765 }
766
767 $authCat = GetBudgetAuthCats($budgetPeriodId);
768 is( scalar @$authCat, scalar @$budgets, "GetBudgetAuthCats returns distinct authCat" );
769
770 $i = 0;
771 for my $budget ( @$budgets )
772 {
773     $budget->{sort1_authcat} = "sort1_authcat_$i";
774     $budget->{sort2_authcat} = "";
775     $budget->{budget_period_id} = $budgetPeriodId;
776     ModBudget( $budget );
777     $i++;
778 }
779
780 $authCat = GetBudgetAuthCats($budgetPeriodId);
781
782 is( scalar @{$authCat}, $i, "GetBudgetAuthCats returns only non-empty sorting categories (empty sort2_authcat on all records)" );
783
784 $i = 0;
785 for my $budget ( @$budgets )
786 {
787     $budget->{sort1_authcat} = "";
788     $budget->{sort2_authcat} = "";
789     $budget->{budget_period_id} = $budgetPeriodId;
790     ModBudget( $budget );
791     $i++;
792 }
793
794 $authCat = GetBudgetAuthCats($budgetPeriodId);
795
796 is( scalar @{$authCat}, 0, "GetBudgetAuthCats returns only non-empty sorting categories (all empty)" );
797
798 # /Test GetBudgetAuthCats
799
800 sub _get_dependencies {
801     my ($budget_hierarchy) = @_;
802     my $graph;
803     for my $budget (@$budget_hierarchy) {
804         if ( $budget->{child} ) {
805             my @sorted = sort @{ $budget->{child} };
806             for my $child_id (@sorted) {
807                 push @{ $graph->{ $budget->{budget_name} }{children} },
808                   _get_budgetname_by_id( $budget_hierarchy, $child_id );
809             }
810         }
811         push @{ $graph->{ $budget->{budget_name} }{parents} },
812           $budget->{parent_id};
813     }
814     return $graph;
815 }
816
817 sub _get_budgetname_by_id {
818     my ( $budgets, $budget_id ) = @_;
819     my ($budget_name) =
820       map { ( $_->{budget_id} eq $budget_id ) ? $_->{budget_name} : () }
821       @$budgets;
822     return $budget_name;
823 }
824
825 # C4::Context->userenv
826 sub Mock_userenv {
827     return $userenv;
828 }