Bug 11742: (QA followup) wrong number of tests
[koha.git] / t / db_dependent / Budgets.t
1 use Modern::Perl;
2 use Test::More tests => 25;
3
4 BEGIN {use_ok('C4::Budgets') }
5 use C4::Context;
6 use C4::Biblio;
7 use C4::Bookseller;
8 use C4::Acquisition;
9 use C4::Dates;
10
11 use YAML;
12 my $dbh = C4::Context->dbh;
13 $dbh->{AutoCommit} = 0;
14 $dbh->{RaiseError} = 1;
15
16 $dbh->do(q|DELETE FROM aqbudgetperiods|);
17 $dbh->do(q|DELETE FROM aqbudgets|);
18
19 #
20 # Budget Periods :
21 #
22 my $bpid;
23 my $budgetperiod;
24 my $active_period;
25 my $mod_status;
26 my $del_status;
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");
32
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");
41
42 #
43 # Budget  :
44 #
45
46 # Add A budget Period
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");
59 }
60 else{
61 ok($bpid=AddBudgetPeriod(
62                                                 {budget_period_startdate=>'2008-01-01'
63                                                 ,budget_period_enddate  =>'2008-12-31'
64                                                 ,budget_description             =>"MAPERI"
65                                                 }),
66         "AddBudgetPeriod returned $bpid");
67
68 }
69 my $budget_id;
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"
76                                                         , budget_active         => 1
77                                                         , budget_period_id      => $bpid
78                                                 }
79                                            ),
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                 |       | 
96
97 my $budget;
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");
102 ok(GetBudgets()>0,
103         "GetBudgets 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");
111
112 my $budget_name = GetBudgetName( $budget_id );
113 is($budget_name, $budget->{budget_name}, "Test the GetBudgetName routine");
114
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");
119
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",
127                             budget_active       => 1,
128                             budget_period_id    => $bpid,
129                         }
130                        ),
131     "AddBudget returned $second_budget_id");
132
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');
135
136 ok($del_status=DelBudget($budget_id),
137     "DelBudget returned $del_status");
138
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;
146
147 my $budget_period_id = AddBudgetPeriod(
148     {
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,
154     }
155 );
156 my $budget_id1 = AddBudget(
157     {
158         budget_code      => 'budget_1',
159         budget_name      => 'budget_1',
160         budget_active    => 1,
161         budget_period_id => $budget_period_id,
162         budget_parent_id => undef,
163         budget_amount    => $budget_1_total,
164     }
165 );
166 my $budget_id2 = AddBudget(
167     {
168         budget_code      => 'budget_2',
169         budget_name      => 'budget_2',
170         budget_active    => 1,
171         budget_period_id => $budget_period_id,
172         budget_parent_id => undef,
173         budget_amount    => $budget_2_total,
174     }
175 );
176 my $budget_id11 = AddBudget(
177     {
178         budget_code      => 'budget_11',
179         budget_name      => 'budget_11',
180         budget_active    => 1,
181         budget_period_id => $budget_period_id,
182         budget_parent_id => $budget_id1,
183         budget_amount    => $budget_11_total,
184     }
185 );
186 my $budget_id12 = AddBudget(
187     {
188         budget_code      => 'budget_12',
189         budget_name      => 'budget_12',
190         budget_active    => 1,
191         budget_period_id => $budget_period_id,
192         budget_parent_id => $budget_id1,
193         budget_amount    => $budget_12_total,
194     }
195 );
196 my $budget_id111 = AddBudget(
197     {
198         budget_code      => 'budget_111',
199         budget_name      => 'budget_111',
200         budget_active    => 1,
201         budget_period_id => $budget_period_id,
202         budget_parent_id => $budget_id11,
203         owner_id         => 1,
204         budget_amount    => $budget_111_total,
205     }
206 );
207 my $budget_id21 = AddBudget(
208     {
209         budget_code      => 'budget_21',
210         budget_name      => 'budget_21',
211         budget_active    => 1,
212         budget_period_id => $budget_period_id,
213         budget_parent_id => $budget_id2,
214     }
215 );
216
217 my $booksellerid = C4::Bookseller::AddBookseller(
218     {
219         name         => "my vendor",
220         address1     => "bookseller's address",
221         phone        => "0123456",
222         active       => 1,
223         deliverytime => 5,
224     }
225 );
226
227 my $basketno = C4::Acquisition::NewBasket( $booksellerid, 1 );
228 my ( $biblionumber, $biblioitemnumber ) =
229   C4::Biblio::AddBiblio( MARC::Record->new, '' );
230
231 my @order_infos = (
232     {
233         budget_id => $budget_id1,
234         pending_quantity  => 1,
235         spent_quantity  => 0,
236     },
237     {
238         budget_id => $budget_id2,
239         pending_quantity  => 2,
240         spent_quantity  => 1,
241     },
242     {
243         budget_id => $budget_id11,
244         pending_quantity  => 3,
245         spent_quantity  => 4,
246     },
247     {
248         budget_id => $budget_id12,
249         pending_quantity  => 4,
250         spent_quantity  => 3,
251     },
252     {
253         budget_id => $budget_id111,
254         pending_quantity  => 2,
255         spent_quantity  => 1,
256     },
257
258     # No order for budget_21
259
260 );
261
262 my %budgets;
263 my $invoiceid = AddInvoice(invoicenumber => 'invoice_test_clone', booksellerid => $booksellerid, unknown => "unknown");
264 my $item_price = 10;
265 my $item_quantity = 2;
266 for my $infos (@order_infos) {
267     for ( 1 .. $infos->{pending_quantity} ) {
268         my ( undef, $ordernumber ) = C4::Acquisition::NewOrder(
269             {
270                 basketno           => $basketno,
271                 biblionumber       => $biblionumber,
272                 budget_id          => $infos->{budget_id},
273                 order_internalnote => "internal note",
274                 order_vendornote   => "vendor note",
275                 quantity           => 2,
276                 cost               => $item_price,
277                 rrp                => $item_price,
278                 listprice          => $item_price,
279                 ecost              => $item_price,
280                 rrp                => $item_price,
281                 discount           => 0,
282                 uncertainprice     => 0,
283                 gstrate            => 0,
284             }
285         );
286         push @{ $budgets{$infos->{budget_id}} }, $ordernumber;
287     }
288     for ( 1 .. $infos->{spent_quantity} ) {
289         my ( undef, $ordernumber ) = C4::Acquisition::NewOrder(
290             {
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,
297                 cost               => $item_price,
298                 rrp                => $item_price,
299                 listprice          => $item_price,
300                 ecost              => $item_price,
301                 rrp                => $item_price,
302                 discount           => 0,
303                 uncertainprice     => 0,
304                 gstrate            => 0,
305             }
306         );
307         ModReceiveOrder({
308               biblionumber     => $biblionumber,
309               ordernumber      => $ordernumber,
310               budget_id        => $infos->{budget_id},
311               quantityreceived => $item_quantity,
312               cost             => $item_price,
313               ecost            => $item_price,
314               invoiceid        => $invoiceid,
315               rrp              => $item_price,
316               received_items   => [],
317         } );
318     }
319 }
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" );