2 use Test::More tests => 63;
14 my $dbh = C4::Context->dbh;
15 $dbh->{AutoCommit} = 0;
16 $dbh->{RaiseError} = 1;
18 $dbh->do(q|DELETE FROM aqbudgetperiods|);
19 $dbh->do(q|DELETE FROM aqbudgets|);
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',
30 is( $bpid, undef, 'AddBugetPeriod without end date returns undef' );
31 $bpid = AddBudgetPeriod({
32 budget_period_enddate => '2008-12-31',
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' );
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,
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' );
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,
61 my $mod_status = ModBudgetPeriod($my_budgetperiod);
62 is( $mod_status, undef, 'ModBudgetPeriod without id returns undef' );
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' );
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' );
83 is( DelBudgetPeriod($bpid), 1, 'DelBudgetPeriod returns true' );
84 $budgetperiods = GetBudgetPeriods();
85 is( @$budgetperiods, 0, 'GetBudgetPeriods returns the correct number of budget periods' );
92 is( AddBudget(), undef, 'AddBuget without argument returns undef' );
93 my $budgets = GetBudgets();
94 is( @$budgets, 0, 'GetBudgets returns the correct number of budgets' );
96 $bpid = AddBudgetPeriod($my_budgetperiod);
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,
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' );
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,
121 $mod_status = ModBudget($my_budget);
122 is( $mod_status, undef, 'ModBudget without id returns undef' );
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' );
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' );
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');
152 my $budget_name = GetBudgetName( $budget_id );
153 is($budget_name, $my_budget->{budget_name}, "Test the GetBudgetName routine");
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");
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,
167 isnt( $second_budget_id, undef, 'AddBudget does not returns undef' );
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' );
172 is( DelBudget($budget_id), 1, 'DelBudget returns true' );
173 $budgets = GetBudgets();
174 is( @$budgets, 1, 'GetBudgets returns the correct number of budget periods' );
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;
185 my $budget_period_id = AddBudgetPeriod(
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,
194 my $budget_id1 = AddBudget(
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,
203 my $budget_id2 = AddBudget(
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,
212 my $budget_id11 = AddBudget(
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,
221 my $budget_id12 = AddBudget(
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,
230 my $budget_id111 = AddBudget(
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,
240 my $budget_id21 = AddBudget(
242 budget_code => 'budget_21',
243 budget_name => 'budget_21',
244 budget_period_id => $budget_period_id,
245 budget_parent_id => $budget_id2,
249 my $booksellerid = C4::Bookseller::AddBookseller(
252 address1 => "bookseller's address",
259 my $basketno = C4::Acquisition::NewBasket( $booksellerid, 1 );
260 my ( $biblionumber, $biblioitemnumber ) =
261 C4::Biblio::AddBiblio( MARC::Record->new, '' );
265 budget_id => $budget_id1,
266 pending_quantity => 1,
270 budget_id => $budget_id2,
271 pending_quantity => 2,
275 budget_id => $budget_id11,
276 pending_quantity => 3,
280 budget_id => $budget_id12,
281 pending_quantity => 4,
285 budget_id => $budget_id111,
286 pending_quantity => 2,
290 # No order for budget_21
295 my $invoiceid = AddInvoice(invoicenumber => 'invoice_test_clone', booksellerid => $booksellerid, unknown => "unknown");
297 my $item_quantity = 2;
298 for my $infos (@order_infos) {
299 for ( 1 .. $infos->{pending_quantity} ) {
300 my ( undef, $ordernumber ) = C4::Acquisition::NewOrder(
302 basketno => $basketno,
303 biblionumber => $biblionumber,
304 budget_id => $infos->{budget_id},
305 order_internalnote => "internal note",
306 order_vendornote => "vendor note",
310 listprice => $item_price,
311 ecost => $item_price,
318 push @{ $budgets{$infos->{budget_id}} }, $ordernumber;
320 for ( 1 .. $infos->{spent_quantity} ) {
321 my ( undef, $ordernumber ) = C4::Acquisition::NewOrder(
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,
331 listprice => $item_price,
332 ecost => $item_price,
340 biblionumber => $biblionumber,
341 ordernumber => $ordernumber,
342 budget_id => $infos->{budget_id},
343 quantityreceived => $item_quantity,
345 ecost => $item_price,
346 invoiceid => $invoiceid,
348 received_items => [],
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" );