Bug 12164: Move the budget period clone logic into C4::Budgets
[koha.git] / t / db_dependent / Budgets.t
1 use Modern::Perl;
2 use Test::More tests => 65;
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::Dates;
12
13 use YAML;
14 my $dbh = C4::Context->dbh;
15 $dbh->{AutoCommit} = 0;
16 $dbh->{RaiseError} = 1;
17
18 $dbh->do(q|DELETE FROM aqbudgetperiods|);
19 $dbh->do(q|DELETE FROM aqbudgets|);
20
21 #
22 # Budget Periods :
23 #
24
25 is( AddBudgetPeriod(), undef, 'AddBugetPeriod without argument returns undef' );
26 is( AddBudgetPeriod( { }  ), undef, 'AddBugetPeriod with an empty argument returns undef' );
27 my $bpid = AddBudgetPeriod({
28     budget_period_startdate => '2008-01-01',
29 });
30 is( $bpid, undef, 'AddBugetPeriod without end date returns undef' );
31 $bpid = AddBudgetPeriod({
32     budget_period_enddate => '2008-12-31',
33 });
34 is( $bpid, undef, 'AddBugetPeriod without start date returns undef' );
35 is( GetBudgetPeriod(0), undef ,'GetBudgetPeriod(0) returned undef : noactive BudgetPeriod' );
36 my $budgetperiods = GetBudgetPeriods();
37 is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
38
39 my $my_budgetperiod = {
40     budget_period_startdate   => '2008-01-01',
41     budget_period_enddate     => '2008-12-31',
42     budget_period_description => 'MAPERI',
43     budget_period_active      => 0,
44 };
45 $bpid = AddBudgetPeriod($my_budgetperiod);
46 isnt( $bpid, undef, 'AddBugetPeriod does not returns undef' );
47 my $budgetperiod = GetBudgetPeriod($bpid);
48 is( $budgetperiod->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'AddBudgetPeriod stores the start date correctly' );
49 is( $budgetperiod->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'AddBudgetPeriod stores the end date correctly' );
50 is( $budgetperiod->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'AddBudgetPeriod stores the description correctly' );
51 is( $budgetperiod->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'AddBudgetPeriod stores active correctly' );
52 is( GetBudgetPeriod(0), undef ,'GetBudgetPeriod(0) returned undef : noactive BudgetPeriod' );
53
54
55 $my_budgetperiod = {
56     budget_period_startdate   => '2009-01-01',
57     budget_period_enddate     => '2009-12-31',
58     budget_period_description => 'MODIF_MAPERI',
59     budget_period_active      => 1,
60 };
61 my $mod_status = ModBudgetPeriod($my_budgetperiod);
62 is( $mod_status, undef, 'ModBudgetPeriod without id returns undef' );
63
64 $my_budgetperiod->{budget_period_id} = $bpid;
65 $mod_status = ModBudgetPeriod($my_budgetperiod);
66 is( $mod_status, 1, 'ModBudgetPeriod returnis true' );
67 $budgetperiod = GetBudgetPeriod($bpid);
68 is( $budgetperiod->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'ModBudgetPeriod updates the start date correctly' );
69 is( $budgetperiod->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'ModBudgetPeriod updates the end date correctly' );
70 is( $budgetperiod->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'ModBudgetPeriod updates the description correctly' );
71 is( $budgetperiod->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'ModBudgetPeriod upates active correctly' );
72 isnt( GetBudgetPeriod(0), undef, 'GetBugetPeriods functions correctly' );
73
74
75 $budgetperiods = GetBudgetPeriods();
76 is( @$budgetperiods, 1, 'GetBudgetPeriods returns the correct number of budget periods' );
77 is( $budgetperiods->[0]->{budget_period_id}, $my_budgetperiod->{budget_period_id}, 'GetBudgetPeriods returns the id correctly' );
78 is( $budgetperiods->[0]->{budget_period_startdate}, $my_budgetperiod->{budget_period_startdate}, 'GetBudgetPeriods returns the start date correctly' );
79 is( $budgetperiods->[0]->{budget_period_enddate}, $my_budgetperiod->{budget_period_enddate}, 'GetBudgetPeriods returns the end date correctly' );
80 is( $budgetperiods->[0]->{budget_period_description}, $my_budgetperiod->{budget_period_description}, 'GetBudgetPeriods returns the description correctly' );
81 is( $budgetperiods->[0]->{budget_period_active}, $my_budgetperiod->{budget_period_active}, 'GetBudgetPeriods returns active correctly' );
82
83 is( DelBudgetPeriod($bpid), 1, 'DelBudgetPeriod returns true' );
84 $budgetperiods = GetBudgetPeriods();
85 is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
86
87
88 #
89 # Budget  :
90 #
91
92 is( AddBudget(), undef, 'AddBuget without argument returns undef' );
93 my $budgets = GetBudgets();
94 is( @$budgets, 0, 'GetBudgets returns the correct number of budgets' );
95
96 $bpid = AddBudgetPeriod($my_budgetperiod);
97 my $my_budget = {
98     budget_code      => 'ABCD',
99     budget_amount    => '123.132000',
100     budget_name      => 'Periodiques',
101     budget_notes     => 'This is a note',
102     budget_period_id => $bpid,
103 };
104 my $budget_id = AddBudget($my_budget);
105 isnt( $budget_id, undef, 'AddBudget does not returns undef' );
106 my $budget = GetBudget($budget_id);
107 is( $budget->{budget_code}, $my_budget->{budget_code}, 'AddBudget stores the budget code correctly' );
108 is( $budget->{budget_amount}, $my_budget->{budget_amount}, 'AddBudget stores the budget amount correctly' );
109 is( $budget->{budget_name}, $my_budget->{budget_name}, 'AddBudget stores the budget name correctly' );
110 is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'AddBudget stores the budget notes correctly' );
111 is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'AddBudget stores the budget period id correctly' );
112
113
114 $my_budget = {
115     budget_code      => 'EFG',
116     budget_amount    => '321.231000',
117     budget_name      => 'Modified name',
118     budget_notes     => 'This is a modified note',
119     budget_period_id => $bpid,
120 };
121 $mod_status = ModBudget($my_budget);
122 is( $mod_status, undef, 'ModBudget without id returns undef' );
123
124 $my_budget->{budget_id} = $budget_id;
125 $mod_status = ModBudget($my_budget);
126 is( $mod_status, 1, 'ModBudget returns true' );
127 $budget = GetBudget($budget_id);
128 is( $budget->{budget_code}, $my_budget->{budget_code}, 'ModBudget updates the budget code correctly' );
129 is( $budget->{budget_amount}, $my_budget->{budget_amount}, 'ModBudget updates the budget amount correctly' );
130 is( $budget->{budget_name}, $my_budget->{budget_name}, 'ModBudget updates the budget name correctly' );
131 is( $budget->{budget_notes}, $my_budget->{budget_notes}, 'ModBudget updates the budget notes correctly' );
132 is( $budget->{budget_period_id}, $my_budget->{budget_period_id}, 'ModBudget updates the budget period id correctly' );
133
134
135 $budgets = GetBudgets();
136 is( @$budgets, 1, 'GetBudgets returns the correct number of budgets' );
137 is( $budgets->[0]->{budget_id}, $my_budget->{budget_id}, 'GetBudgets returns the budget id correctly' );
138 is( $budgets->[0]->{budget_code}, $my_budget->{budget_code}, 'GetBudgets returns the budget code correctly' );
139 is( $budgets->[0]->{budget_amount}, $my_budget->{budget_amount}, 'GetBudgets returns the budget amount correctly' );
140 is( $budgets->[0]->{budget_name}, $my_budget->{budget_name}, 'GetBudgets returns the budget name correctly' );
141 is( $budgets->[0]->{budget_notes}, $my_budget->{budget_notes}, 'GetBudgets returns the budget notes correctly' );
142 is( $budgets->[0]->{budget_period_id}, $my_budget->{budget_period_id}, 'GetBudgets returns the budget period id correctly' );
143
144 $budgets = GetBudgets( {budget_period_id => $bpid} );
145 is( @$budgets, 1, 'GetBudgets With Filter OK' );
146 $budgets = GetBudgets( {budget_period_id => $bpid}, {-asc => "budget_name"} );
147 is( @$budgets, 1, 'GetBudgets With Order OK' );
148 $budgets = GetBudgets( {budget_period_id => GetBudgetPeriod($bpid)->{budget_period_id}}, {-asc => "budget_name"} );
149 is( @$budgets, 1, 'GetBudgets With Order Getting Active budgetPeriod OK');
150
151
152 my $budget_name = GetBudgetName( $budget_id );
153 is($budget_name, $my_budget->{budget_name}, "Test the GetBudgetName routine");
154
155 my $budget_code = $my_budget->{budget_code};
156 my $budget_by_code = GetBudgetByCode( $budget_code );
157 is($budget_by_code->{budget_id}, $budget_id, "GetBudgetByCode, check id");
158 is($budget_by_code->{budget_notes}, $my_budget->{budget_notes}, "GetBudgetByCode, check notes");
159
160 my $second_budget_id = AddBudget({
161     budget_code      => "ZZZZ",
162     budget_amount    => "500.00",
163     budget_name      => "Art",
164     budget_notes     => "This is a note",
165     budget_period_id => $bpid,
166 });
167 isnt( $second_budget_id, undef, 'AddBudget does not returns undef' );
168
169 $budgets = GetBudgets( {budget_period_id => $bpid} );
170 ok( $budgets->[0]->{budget_name} lt $budgets->[1]->{budget_name}, 'default sort order for GetBudgets is by name' );
171
172 is( DelBudget($budget_id), 1, 'DelBudget returns true' );
173 $budgets = GetBudgets();
174 is( @$budgets, 1, 'GetBudgets returns the correct number of budget periods' );
175
176
177 # GetBudgetHierarchySpent and GetBudgetHierarchyOrdered
178 my $budget_period_total = 10_000;
179 my $budget_1_total = 1_000;
180 my $budget_11_total = 100;
181 my $budget_111_total = 50;
182 my $budget_12_total = 100;
183 my $budget_2_total = 2_000;
184
185 my $budget_period_id = AddBudgetPeriod(
186     {
187         budget_period_startdate   => '2013-01-01',
188         budget_period_enddate     => '2014-12-31',
189         budget_period_description => 'Budget Period',
190         budget_period_active      => 1,
191         budget_period_total       => $budget_period_total,
192     }
193 );
194 my $budget_id1 = AddBudget(
195     {
196         budget_code      => 'budget_1',
197         budget_name      => 'budget_1',
198         budget_period_id => $budget_period_id,
199         budget_parent_id => undef,
200         budget_amount    => $budget_1_total,
201     }
202 );
203 my $budget_id2 = AddBudget(
204     {
205         budget_code      => 'budget_2',
206         budget_name      => 'budget_2',
207         budget_period_id => $budget_period_id,
208         budget_parent_id => undef,
209         budget_amount    => $budget_2_total,
210     }
211 );
212 my $budget_id11 = AddBudget(
213     {
214         budget_code      => 'budget_11',
215         budget_name      => 'budget_11',
216         budget_period_id => $budget_period_id,
217         budget_parent_id => $budget_id1,
218         budget_amount    => $budget_11_total,
219     }
220 );
221 my $budget_id12 = AddBudget(
222     {
223         budget_code      => 'budget_12',
224         budget_name      => 'budget_12',
225         budget_period_id => $budget_period_id,
226         budget_parent_id => $budget_id1,
227         budget_amount    => $budget_12_total,
228     }
229 );
230 my $budget_id111 = AddBudget(
231     {
232         budget_code      => 'budget_111',
233         budget_name      => 'budget_111',
234         budget_period_id => $budget_period_id,
235         budget_parent_id => $budget_id11,
236         budget_owner_id  => 1,
237         budget_amount    => $budget_111_total,
238     }
239 );
240 my $budget_id21 = AddBudget(
241     {
242         budget_code      => 'budget_21',
243         budget_name      => 'budget_21',
244         budget_period_id => $budget_period_id,
245         budget_parent_id => $budget_id2,
246     }
247 );
248
249 my $booksellerid = C4::Bookseller::AddBookseller(
250     {
251         name         => "my vendor",
252         address1     => "bookseller's address",
253         phone        => "0123456",
254         active       => 1,
255         deliverytime => 5,
256     }
257 );
258
259 my $basketno = C4::Acquisition::NewBasket( $booksellerid, 1 );
260 my ( $biblionumber, $biblioitemnumber ) =
261   C4::Biblio::AddBiblio( MARC::Record->new, '' );
262
263 my @order_infos = (
264     {
265         budget_id => $budget_id1,
266         pending_quantity  => 1,
267         spent_quantity  => 0,
268     },
269     {
270         budget_id => $budget_id2,
271         pending_quantity  => 2,
272         spent_quantity  => 1,
273     },
274     {
275         budget_id => $budget_id11,
276         pending_quantity  => 3,
277         spent_quantity  => 4,
278     },
279     {
280         budget_id => $budget_id12,
281         pending_quantity  => 4,
282         spent_quantity  => 3,
283     },
284     {
285         budget_id => $budget_id111,
286         pending_quantity  => 2,
287         spent_quantity  => 1,
288     },
289
290     # No order for budget_21
291
292 );
293
294 my %budgets;
295 my $invoiceid = AddInvoice(invoicenumber => 'invoice_test_clone', booksellerid => $booksellerid, unknown => "unknown");
296 my $item_price = 10;
297 my $item_quantity = 2;
298 for my $infos (@order_infos) {
299     for ( 1 .. $infos->{pending_quantity} ) {
300         my ( undef, $ordernumber ) = C4::Acquisition::NewOrder(
301             {
302                 basketno           => $basketno,
303                 biblionumber       => $biblionumber,
304                 budget_id          => $infos->{budget_id},
305                 order_internalnote => "internal note",
306                 order_vendornote   => "vendor note",
307                 quantity           => 2,
308                 cost               => $item_price,
309                 rrp                => $item_price,
310                 listprice          => $item_price,
311                 ecost              => $item_price,
312                 rrp                => $item_price,
313                 discount           => 0,
314                 uncertainprice     => 0,
315                 gstrate            => 0,
316             }
317         );
318         push @{ $budgets{$infos->{budget_id}} }, $ordernumber;
319     }
320     for ( 1 .. $infos->{spent_quantity} ) {
321         my ( undef, $ordernumber ) = C4::Acquisition::NewOrder(
322             {
323                 basketno           => $basketno,
324                 biblionumber       => $biblionumber,
325                 budget_id          => $infos->{budget_id},
326                 order_internalnote => "internal note",
327                 order_vendornote   => "vendor note",
328                 quantity           => $item_quantity,
329                 cost               => $item_price,
330                 rrp                => $item_price,
331                 listprice          => $item_price,
332                 ecost              => $item_price,
333                 rrp                => $item_price,
334                 discount           => 0,
335                 uncertainprice     => 0,
336                 gstrate            => 0,
337             }
338         );
339         ModReceiveOrder({
340               biblionumber     => $biblionumber,
341               ordernumber      => $ordernumber,
342               budget_id        => $infos->{budget_id},
343               quantityreceived => $item_quantity,
344               cost             => $item_price,
345               ecost            => $item_price,
346               invoiceid        => $invoiceid,
347               rrp              => $item_price,
348               received_items   => [],
349         } );
350     }
351 }
352 is( GetBudgetHierarchySpent( $budget_id1 ), 160, "total spent for budget1 is 160" );
353 is( GetBudgetHierarchySpent( $budget_id11 ), 100, "total spent for budget11 is 100" );
354 is( GetBudgetHierarchySpent( $budget_id111 ), 20, "total spent for budget111 is 20" );
355
356 # CloneBudgetPeriod
357 my $budget_period_id_cloned = C4::Budgets::CloneBudgetPeriod(
358     {
359         budget_period_id        => $budget_period_id,
360         budget_period_startdate => '2014-01-01',
361         budget_period_enddate   => '2014-12-31',
362     }
363 );
364
365 my $budget_hierarchy        = GetBudgetHierarchy($budget_period_id);
366 my $budget_hierarchy_cloned = GetBudgetHierarchy($budget_period_id_cloned);
367
368 is(
369     scalar(@$budget_hierarchy_cloned),
370     scalar(@$budget_hierarchy),
371     'CloneBudgetPeriod clones the same number of budgets (funds)'
372 );
373 is_deeply(
374     _get_dependencies($budget_hierarchy),
375     _get_dependencies($budget_hierarchy_cloned),
376     'CloneBudgetPeriod keep the same dependencies order'
377 );
378
379 sub _get_dependencies {
380     my ($budget_hierarchy) = @_;
381     my $graph;
382     for my $budget (@$budget_hierarchy) {
383         if ( $budget->{child} ) {
384             my @sorted = sort @{ $budget->{child} };
385             for my $child_id (@sorted) {
386                 push @{ $graph->{ $budget->{budget_name} }{children} },
387                   _get_budgetname_by_id( $budget_hierarchy, $child_id );
388             }
389         }
390         push @{ $graph->{ $budget->{budget_name} }{parents} },
391           $budget->{parent_id};
392     }
393     return $graph;
394 }
395
396 sub _get_budgetname_by_id {
397     my ( $budgets, $budget_id ) = @_;
398     my ($budget_name) =
399       map { ( $_->{budget_id} eq $budget_id ) ? $_->{budget_name} : () }
400       @$budgets;
401     return $budget_name;
402 }