2 use Test::More tests => 25;
4 BEGIN {use_ok('C4::Budgets') }
12 my $dbh = C4::Context->dbh;
13 $dbh->{AutoCommit} = 0;
14 $dbh->{RaiseError} = 1;
16 $dbh->do(q|DELETE FROM aqbudgetperiods|);
17 $dbh->do(q|DELETE FROM aqbudgets|);
27 ok($bpid=AddBudgetPeriod(
28 { budget_period_startdate => '2008-01-01'
29 , budget_period_enddate => '2008-12-31'
30 , budget_description => "MAPERI"}),
31 "AddBudgetPeriod with iso dates OK");
33 ok($budgetperiod=GetBudgetPeriod($bpid),
34 "GetBudgetPeriod($bpid) returned ".Dump($budgetperiod));
35 ok(!GetBudgetPeriod(0) ,"GetBudgetPeriod(0) returned undef : noactive BudgetPeriod");
36 $$budgetperiod{budget_period_active}=1;
37 ok($mod_status=ModBudgetPeriod($budgetperiod),"ModBudgetPeriod OK");
38 ok($active_period=GetBudgetPeriod(0),"GetBudgetPeriod(0) returned".Dump($active_period));
39 ok(scalar(GetBudgetPeriods())>0,"GetBudgetPeriods OK");#Should at least return the Budget inserted
40 ok($del_status=DelBudgetPeriod($bpid),"DelBudgetPeriod returned $del_status");
47 if (C4::Context->preference('dateformat') eq "metric"){
48 ok($bpid=AddBudgetPeriod(
49 { budget_period_startdate =>'01-01-2008'
50 , budget_period_enddate =>'31-12-2008'
51 , budget_description =>"MAPERI"}),
52 "AddBudgetPeriod returned $bpid");
53 } elsif (C4::Context->preference('dateformat') eq "us"){
54 ok($bpid=AddBudgetPeriod(
55 { budget_period_startdate =>'01-01-2008'
56 , budget_period_enddate =>'12-31-2008'
57 , budget_description =>"MAPERI"}),
58 "AddBudgetPeriod returned $bpid");
61 ok($bpid=AddBudgetPeriod(
62 {budget_period_startdate=>'2008-01-01'
63 ,budget_period_enddate =>'2008-12-31'
64 ,budget_description =>"MAPERI"
66 "AddBudgetPeriod returned $bpid");
70 ok($budget_id=AddBudget(
71 { budget_code => "ABCD"
72 , budget_amount => "123.132"
73 , budget_name => "Périodiques"
74 , budget_notes => "This is a note"
75 , budget_description=> "Serials"
77 , budget_period_id => $bpid
80 "AddBudget returned $budget_id");
81 #budget_code | varchar(30) | YES | | NULL | |
82 #| budget_amount | decimal(28,6) | NO | | 0.000000 | |
83 #| budget_id | int(11) | NO | PRI | NULL | |
84 #| budget_branchcode | varchar(10) | YES | | NULL | |
85 #| budget_parent_id | int(11) | YES | | NULL | |
86 #| budget_name | varchar(80) | YES | | NULL | |
87 #| budget_encumb | decimal(28,6) | YES | | 0.000000 | |
88 #| budget_expend | decimal(28,6) | YES | | 0.000000 | |
89 #| budget_notes | mediumtext | YES | | NULL | |
90 #| timestamp | timestamp | NO | | CURRENT_TIMESTAMP | |
91 #| budget_period_id | int(11) | YES | MUL | NULL | |
92 #| sort1_authcat | varchar(80) | YES | | NULL | |
93 #| sort2_authcat | varchar(80) | YES | | NULL | |
94 #| budget_owner_id | int(11) | YES | | NULL | |
95 #| budget_permission | int(1) | YES | | 0 | |
98 ok($budget=GetBudget($budget_id) ,"GetBudget OK");
99 $budget_id = $budget->{budget_id};
100 $$budget{budget_permission}=1;
101 ok($mod_status=ModBudget($budget),"ModBudget OK");
104 ok(GetBudgets({budget_period_id=>$bpid})>0,
105 "GetBudgets With Filter OK");
106 ok(GetBudgets({budget_period_id=>$bpid},[{"budget_name"=>0}])>0,
107 "GetBudgets With Order OK");
108 ok(GetBudgets({budget_period_id=>GetBudgetPeriod($bpid)->{budget_period_id}},[{"budget_name"=>0}])>0,
109 "GetBudgets With Order
110 Getting Active budgetPeriod OK");
112 my $budget_name = GetBudgetName( $budget_id );
113 is($budget_name, $budget->{budget_name}, "Test the GetBudgetName routine");
115 my $budget_code = $budget->{budget_code};
116 my $budget_by_code = GetBudgetByCode( $budget_code );
117 is($budget_by_code->{budget_id}, $budget_id, "GetBudgetByCode, check id");
118 is($budget_by_code->{budget_notes}, 'This is a note', "GetBudgetByCode, check notes");
120 my $second_budget_id;
121 ok($second_budget_id=AddBudget(
122 { budget_code => "ZZZZ",
123 budget_amount => "500.00",
124 budget_name => "Art",
125 budget_notes => "This is a note",
126 budget_description=> "Art",
128 budget_period_id => $bpid,
131 "AddBudget returned $second_budget_id");
133 my $budgets = GetBudgets({ budget_period_id => $bpid});
134 ok($budgets->[0]->{budget_name} lt $budgets->[1]->{budget_name}, 'default sort order for GetBudgets is by name');
136 ok($del_status=DelBudget($budget_id),
137 "DelBudget returned $del_status");
139 # GetBudgetHierarchySpent and GetBudgetHierarchyOrdered
140 my $budget_period_total = 10_000;
141 my $budget_1_total = 1_000;
142 my $budget_11_total = 100;
143 my $budget_111_total = 50;
144 my $budget_12_total = 100;
145 my $budget_2_total = 2_000;
147 my $budget_period_id = AddBudgetPeriod(
149 budget_period_startdate => '2013-01-01',
150 budget_period_enddate => '2014-12-31',
151 budget_description => 'Budget Period',
152 budget_period_active => 1,
153 budget_period_total => $budget_period_total,
156 my $budget_id1 = AddBudget(
158 budget_code => 'budget_1',
159 budget_name => 'budget_1',
161 budget_period_id => $budget_period_id,
162 budget_parent_id => undef,
163 budget_amount => $budget_1_total,
166 my $budget_id2 = AddBudget(
168 budget_code => 'budget_2',
169 budget_name => 'budget_2',
171 budget_period_id => $budget_period_id,
172 budget_parent_id => undef,
173 budget_amount => $budget_2_total,
176 my $budget_id11 = AddBudget(
178 budget_code => 'budget_11',
179 budget_name => 'budget_11',
181 budget_period_id => $budget_period_id,
182 budget_parent_id => $budget_id1,
183 budget_amount => $budget_11_total,
186 my $budget_id12 = AddBudget(
188 budget_code => 'budget_12',
189 budget_name => 'budget_12',
191 budget_period_id => $budget_period_id,
192 budget_parent_id => $budget_id1,
193 budget_amount => $budget_12_total,
196 my $budget_id111 = AddBudget(
198 budget_code => 'budget_111',
199 budget_name => 'budget_111',
201 budget_period_id => $budget_period_id,
202 budget_parent_id => $budget_id11,
204 budget_amount => $budget_111_total,
207 my $budget_id21 = AddBudget(
209 budget_code => 'budget_21',
210 budget_name => 'budget_21',
212 budget_period_id => $budget_period_id,
213 budget_parent_id => $budget_id2,
217 my $booksellerid = C4::Bookseller::AddBookseller(
220 address1 => "bookseller's address",
227 my $basketno = C4::Acquisition::NewBasket( $booksellerid, 1 );
228 my ( $biblionumber, $biblioitemnumber ) =
229 C4::Biblio::AddBiblio( MARC::Record->new, '' );
233 budget_id => $budget_id1,
234 pending_quantity => 1,
238 budget_id => $budget_id2,
239 pending_quantity => 2,
243 budget_id => $budget_id11,
244 pending_quantity => 3,
248 budget_id => $budget_id12,
249 pending_quantity => 4,
253 budget_id => $budget_id111,
254 pending_quantity => 2,
258 # No order for budget_21
263 my $invoiceid = AddInvoice(invoicenumber => 'invoice_test_clone', booksellerid => $booksellerid, unknown => "unknown");
265 my $item_quantity = 2;
266 for my $infos (@order_infos) {
267 for ( 1 .. $infos->{pending_quantity} ) {
268 my ( undef, $ordernumber ) = C4::Acquisition::NewOrder(
270 basketno => $basketno,
271 biblionumber => $biblionumber,
272 budget_id => $infos->{budget_id},
273 order_internalnote => "internal note",
274 order_vendornote => "vendor note",
278 listprice => $item_price,
279 ecost => $item_price,
286 push @{ $budgets{$infos->{budget_id}} }, $ordernumber;
288 for ( 1 .. $infos->{spent_quantity} ) {
289 my ( undef, $ordernumber ) = C4::Acquisition::NewOrder(
291 basketno => $basketno,
292 biblionumber => $biblionumber,
293 budget_id => $infos->{budget_id},
294 order_internalnote => "internal note",
295 order_vendornote => "vendor note",
296 quantity => $item_quantity,
299 listprice => $item_price,
300 ecost => $item_price,
308 biblionumber => $biblionumber,
309 ordernumber => $ordernumber,
310 budget_id => $infos->{budget_id},
311 quantityreceived => $item_quantity,
313 ecost => $item_price,
314 invoiceid => $invoiceid,
316 received_items => [],
320 is( GetBudgetHierarchySpent( $budget_id1 ), 160, "total spent for budget1 is 160" );
321 is( GetBudgetHierarchySpent( $budget_id11 ), 100, "total spent for budget11 is 100" );
322 is( GetBudgetHierarchySpent( $budget_id111 ), 20, "total spent for budget111 is 20" );