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