Bug 7180: QA followup
[koha.git] / t / db_dependent / Budgets.t
1 use strict;
2 use warnings;
3 use Test::More tests => 22;
4
5 BEGIN {use_ok('C4::Budgets') }
6 use C4::Context;
7 use C4::Dates;
8 use C4::Context;
9
10 use YAML;
11 my $dbh = C4::Context->dbh;
12 $dbh->{AutoCommit} = 0;
13 $dbh->{RaiseError} = 1;
14
15 $dbh->do(q|DELETE FROM aqbudgetperiods|);
16 $dbh->do(q|DELETE FROM aqbudgets|);
17
18 #
19 # Budget Periods :
20 #
21 my $bpid;
22 my $budgetperiod;
23 my $active_period;
24 my $mod_status;
25 my $del_status;
26 ok($bpid=AddBudgetPeriod(
27                                                 { budget_period_startdate       => '2008-01-01'
28                                                 , budget_period_enddate         => '2008-12-31'
29                                                 , budget_description            => "MAPERI"}),
30         "AddBudgetPeriod with iso dates OK");
31
32 ok($budgetperiod=GetBudgetPeriod($bpid),
33         "GetBudgetPeriod($bpid) returned ".Dump($budgetperiod));
34 ok(!GetBudgetPeriod(0) ,"GetBudgetPeriod(0) returned undef : noactive BudgetPeriod");
35 $$budgetperiod{budget_period_active}=1;
36 ok($mod_status=ModBudgetPeriod($budgetperiod),"ModBudgetPeriod OK");
37 ok($active_period=GetBudgetPeriod(0),"GetBudgetPeriod(0) returned".Dump($active_period));
38 ok(scalar(GetBudgetPeriods())>0,"GetBudgetPeriods OK");#Should at least return the Budget inserted
39 ok($del_status=DelBudgetPeriod($bpid),"DelBudgetPeriod returned $del_status");
40
41 #
42 # Budget  :
43 #
44
45 # Add A budget Period
46 if (C4::Context->preference('dateformat') eq "metric"){
47 ok($bpid=AddBudgetPeriod(
48                                                 { budget_period_startdate       =>'01-01-2008'
49                                                 , budget_period_enddate         =>'31-12-2008'
50                                                 , budget_description            =>"MAPERI"}),
51         "AddBudgetPeriod returned $bpid");
52 } elsif (C4::Context->preference('dateformat') eq "us"){
53 ok($bpid=AddBudgetPeriod(
54                                                 { budget_period_startdate       =>'01-01-2008'
55                                                 , budget_period_enddate         =>'12-31-2008'
56                                                 , budget_description            =>"MAPERI"}),
57         "AddBudgetPeriod returned $bpid");
58 }
59 else{
60 ok($bpid=AddBudgetPeriod(
61                                                 {budget_period_startdate=>'2008-01-01'
62                                                 ,budget_period_enddate  =>'2008-12-31'
63                                                 ,budget_description             =>"MAPERI"
64                                                 }),
65         "AddBudgetPeriod returned $bpid");
66
67 }
68 my $budget_id;
69 ok($budget_id=AddBudget(
70                                                 {   budget_code                 => "ABCD"
71                                                         , budget_amount         => "123.132"
72                                                         , budget_name           => "Périodiques"
73                                                         , budget_notes          => "This is a note"
74                                                         , budget_description=> "Serials"
75                                                         , budget_active         => 1
76                                                         , budget_period_id      => $bpid
77                                                 }
78                                            ),
79         "AddBudget returned $budget_id");
80 #budget_code            | varchar(30)   | YES  |     | NULL              |       | 
81 #| budget_amount          | decimal(28,6) | NO   |     | 0.000000          |       | 
82 #| budget_id              | int(11)       | NO   | PRI | NULL              |       | 
83 #| budget_branchcode      | varchar(10)   | YES  |     | NULL              |       | 
84 #| budget_parent_id       | int(11)       | YES  |     | NULL              |       | 
85 #| budget_name            | varchar(80)   | YES  |     | NULL              |       | 
86 #| budget_encumb          | decimal(28,6) | YES  |     | 0.000000          |       | 
87 #| budget_expend          | decimal(28,6) | YES  |     | 0.000000          |       | 
88 #| budget_notes           | mediumtext    | YES  |     | NULL              |       | 
89 #| timestamp              | timestamp     | NO   |     | CURRENT_TIMESTAMP |       | 
90 #| budget_period_id       | int(11)       | YES  | MUL | NULL              |       | 
91 #| sort1_authcat          | varchar(80)   | YES  |     | NULL              |       | 
92 #| sort2_authcat          | varchar(80)   | YES  |     | NULL              |       | 
93 #| budget_owner_id        | int(11)       | YES  |     | NULL              |       | 
94 #| budget_permission      | int(1)        | YES  |     | 0                 |       | 
95
96 my $budget;
97 ok($budget=GetBudget($budget_id) ,"GetBudget OK");
98 $budget_id = $budget->{budget_id};
99 $$budget{budget_permission}=1;
100 ok($mod_status=ModBudget($budget),"ModBudget OK");
101 ok(GetBudgets()>0,
102         "GetBudgets OK");
103 ok(GetBudgets({budget_period_id=>$bpid})>0,
104         "GetBudgets With Filter OK");
105 ok(GetBudgets({budget_period_id=>$bpid},[{"budget_name"=>0}])>0,
106         "GetBudgets With Order OK");
107 ok(GetBudgets({budget_period_id=>GetBudgetPeriod($bpid)->{budget_period_id}},[{"budget_name"=>0}])>0,
108         "GetBudgets With Order 
109         Getting Active budgetPeriod OK");
110
111 my $budget_name = GetBudgetName( $budget_id );
112 is($budget_name, $budget->{budget_name}, "Test the GetBudgetName routine");
113
114 my $budget_code = $budget->{budget_code};
115 my $budget_by_code = GetBudgetByCode( $budget_code );
116 is($budget_by_code->{budget_id}, $budget_id, "GetBudgetByCode, check id");
117 is($budget_by_code->{budget_notes}, 'This is a note', "GetBudgetByCode, check notes");
118
119 my $second_budget_id;
120 ok($second_budget_id=AddBudget(
121                         {   budget_code         => "ZZZZ",
122                             budget_amount       => "500.00",
123                             budget_name     => "Art",
124                             budget_notes        => "This is a note",
125                             budget_description=> "Art",
126                             budget_active       => 1,
127                             budget_period_id    => $bpid,
128                         }
129                        ),
130     "AddBudget returned $second_budget_id");
131
132 my $budgets = GetBudgets({ budget_period_id => $bpid});
133 ok($budgets->[0]->{budget_name} lt $budgets->[1]->{budget_name}, 'default sort order for GetBudgets is by name');
134
135 ok($del_status=DelBudget($budget_id),
136     "DelBudget returned $del_status");
137
138 $dbh->rollback;