Revision 63517dfe
Von Kivitendo Admin vor mehr als 1 Jahr hinzugefügt
t/clearing/clearing.t | ||
---|---|---|
1 |
use strict; |
|
2 |
use Test::More tests => 5; |
|
3 |
|
|
4 |
use lib 't'; |
|
5 |
use Support::TestSetup; |
|
6 |
use Test::Exception; |
|
7 |
|
|
8 |
use SL::DB::Chart; |
|
9 |
use SL::DB::TaxKey; |
|
10 |
use SL::DB::GLTransaction; |
|
11 |
use SL::DB::Cleared; |
|
12 |
use SL::DB::ClearedGroup; |
|
13 |
|
|
14 |
use SL::DBUtils qw(selectall_hashref_query selectall_array_query); |
|
15 |
use SL::Clearing; |
|
16 |
|
|
17 |
use Data::Dumper; |
|
18 |
|
|
19 |
Support::TestSetup::login(); |
|
20 |
|
|
21 |
my $durchlaufende_posten_orig_clearing; |
|
22 |
clear_up(); |
|
23 |
|
|
24 |
my $cash = SL::DB::Manager::Chart->find_by( description => 'Kasse'); |
|
25 |
my $bank = SL::DB::Manager::Chart->find_by( description => 'Bank' ); |
|
26 |
my $durchlaufende_posten = SL::DB::Manager::Chart->find_by( description => 'Durchlaufende Posten' ) // die "no chart durchlaufende Posten"; |
|
27 |
|
|
28 |
$durchlaufende_posten_orig_clearing = $durchlaufende_posten->clearing; |
|
29 |
$durchlaufende_posten->clearing(1); |
|
30 |
$durchlaufende_posten->save(changes_only => 1); |
|
31 |
my $tax_0 = SL::DB::Manager::Tax->find_by(taxkey => 0, rate => 0.00); |
|
32 |
|
|
33 |
my $dbh = SL::DB->client->dbh; |
|
34 |
|
|
35 |
my $start_date = DateTime->today_local->subtract(days => 20); |
|
36 |
|
|
37 |
my $expected_cleared_entries = 0; |
|
38 |
|
|
39 |
quick_gl($durchlaufende_posten, 550, $bank, 550, "abc"); |
|
40 |
quick_gl($cash, 100, $durchlaufende_posten, 100, "abc"); |
|
41 |
quick_gl($cash, 450, $durchlaufende_posten, 450, "abc"); |
|
42 |
|
|
43 |
my ($cg_abc); |
|
44 |
my ($entries); |
|
45 |
($cg_abc, $entries) = create_cleared_for_chart_and_reference($durchlaufende_posten, "abc"); |
|
46 |
$expected_cleared_entries += $entries; |
|
47 |
|
|
48 |
is(SL::DB::Manager::Cleared->get_all_count(), $expected_cleared_entries, "$expected_cleared_entries cleared entries after creating cleared_group abc ok"); |
|
49 |
|
|
50 |
eval { |
|
51 |
create_cleared_for_chart_and_reference($durchlaufende_posten, "abc"); |
|
52 |
} or do { |
|
53 |
ok(1 == 1, "caught error when trying to clear already cleared transaction abc: ok"); |
|
54 |
}; |
|
55 |
|
|
56 |
is(SL::DB::Manager::Cleared->get_all_count(), $expected_cleared_entries, "$expected_cleared_entries cleared entries after failing cleared_group abc ok"); |
|
57 |
|
|
58 |
quick_gl($durchlaufende_posten, 55, $bank, 55, "xyz"); |
|
59 |
quick_gl($cash, 55, $durchlaufende_posten, 55, "xyz"); |
|
60 |
my $cg_xyz; |
|
61 |
($cg_xyz, $entries) = create_cleared_for_chart_and_reference($durchlaufende_posten, "xyz"); |
|
62 |
$expected_cleared_entries += $entries; |
|
63 |
is(SL::DB::Manager::Cleared->get_all_count(), $expected_cleared_entries, "$expected_cleared_entries cleared entries after creating cleared_group xyz"); |
|
64 |
|
|
65 |
SL::Clearing::remove_cleared_group($cg_abc->id); |
|
66 |
$expected_cleared_entries -= 3; |
|
67 |
|
|
68 |
is(SL::DB::Manager::Cleared->get_all_count(), $expected_cleared_entries, "$expected_cleared_entries cleared entries after unclearing group abc ok"); |
|
69 |
|
|
70 |
# for my $i ( 2000..2005 ) { |
|
71 |
# quick_gl($cash, 77.77, $durchlaufende_posten, 77.77, "$i"); |
|
72 |
# quick_gl($durchlaufende_posten, 77.77, $bank, 77.77, "$i"); |
|
73 |
# } |
|
74 |
|
|
75 |
# my $randnum = 30; |
|
76 |
# create_rand_entries($durchlaufende_posten, $randnum); |
|
77 |
# die |
|
78 |
|
|
79 |
done_testing; |
|
80 |
clear_up(); |
|
81 |
|
|
82 |
1; |
|
83 |
|
|
84 |
sub clear_up { |
|
85 |
"SL::DB::Manager::${_}"->delete_all(all => 1) for qw( ClearedGroup AccTransaction GLTransaction); |
|
86 |
if (defined $durchlaufende_posten_orig_clearing) { |
|
87 |
$durchlaufende_posten->clearing($durchlaufende_posten_orig_clearing); |
|
88 |
$durchlaufende_posten->save(changes_only => 1); |
|
89 |
} |
|
90 |
} |
|
91 |
|
|
92 |
sub quick_gl { |
|
93 |
my ($chart_credit, $amount_credit, $chart_debit, $amount_debit, $reference) = @_; |
|
94 |
|
|
95 |
my $gl_transaction = SL::DB::GLTransaction->new( |
|
96 |
taxincluded => 1, |
|
97 |
reference => $reference, |
|
98 |
description => '1', |
|
99 |
transdate => get_random_date(), |
|
100 |
)->add_chart_booking( |
|
101 |
chart => $chart_credit, |
|
102 |
credit => $amount_credit, |
|
103 |
tax_id => $tax_0->id, |
|
104 |
)->add_chart_booking( |
|
105 |
chart => $chart_debit, |
|
106 |
debit => $amount_debit, |
|
107 |
tax_id => $tax_0->id, |
|
108 |
)->post; |
|
109 |
} |
|
110 |
|
|
111 |
sub quick_gl_multi { |
|
112 |
my ($chart_credit, $amount_credit, $chart_debit, $amount_debit, $reference) = @_; |
|
113 |
|
|
114 |
my $gl_transaction = SL::DB::GLTransaction->new( |
|
115 |
taxincluded => 1, |
|
116 |
reference => $reference, |
|
117 |
description => '1', |
|
118 |
transdate => get_random_date(), |
|
119 |
); |
|
120 |
|
|
121 |
my $rand_diff = int(rand($amount_credit)*100)/100; |
|
122 |
$rand_diff = 1 unless $rand_diff > 0; |
|
123 |
# printf("credit = %s debit = %s rand_diff = %s\n", $amount_credit, $amount_debit, $rand_diff); |
|
124 |
if ( rand(1) > 0.5 ) { |
|
125 |
# several credit |
|
126 |
$gl_transaction->add_chart_booking( |
|
127 |
chart => $chart_credit, |
|
128 |
credit => $amount_credit-$rand_diff, |
|
129 |
tax_id => $tax_0->id, |
|
130 |
); |
|
131 |
|
|
132 |
$gl_transaction->add_chart_booking( |
|
133 |
chart => $chart_credit, |
|
134 |
credit => $rand_diff, |
|
135 |
tax_id => $tax_0->id, |
|
136 |
); |
|
137 |
|
|
138 |
$gl_transaction->add_chart_booking( |
|
139 |
chart => $chart_debit, |
|
140 |
debit => $amount_debit, |
|
141 |
tax_id => $tax_0->id, |
|
142 |
); |
|
143 |
|
|
144 |
|
|
145 |
} else { |
|
146 |
# several debit |
|
147 |
$gl_transaction->add_chart_booking( |
|
148 |
chart => $chart_credit, |
|
149 |
credit => $amount_credit, |
|
150 |
tax_id => $tax_0->id, |
|
151 |
); |
|
152 |
$gl_transaction->add_chart_booking( |
|
153 |
chart => $chart_debit, |
|
154 |
debit => $amount_debit-$rand_diff, |
|
155 |
tax_id => $tax_0->id, |
|
156 |
); |
|
157 |
$gl_transaction->add_chart_booking( |
|
158 |
chart => $chart_debit, |
|
159 |
debit => $rand_diff, |
|
160 |
tax_id => $tax_0->id, |
|
161 |
); |
|
162 |
} |
|
163 |
|
|
164 |
$gl_transaction->post; |
|
165 |
} |
|
166 |
|
|
167 |
sub create_cleared_for_chart_and_reference { |
|
168 |
my ($chart, $reference) = @_; |
|
169 |
|
|
170 |
my $query = 'select acc_trans_id from acc_trans where chart_id = ? and trans_id in (select id from gl where reference = ?)'; |
|
171 |
my @acc_trans_ids = selectall_array_query($::form, $dbh, $query, $chart->id, $reference); |
|
172 |
my $cg = SL::Clearing::create_cleared_group(\@acc_trans_ids); |
|
173 |
($cg, scalar @acc_trans_ids); # return cleared_group and expected number of acc_trans_ids (for testing) |
|
174 |
} |
|
175 |
|
|
176 |
sub get_next_date { |
|
177 |
return $start_date->add(days => 1); |
|
178 |
} |
|
179 |
|
|
180 |
sub get_random_date { |
|
181 |
my ($max_subtract_days, $max_add_days) = @_; |
|
182 |
|
|
183 |
$max_subtract_days //= 30; |
|
184 |
$max_add_days //= 0; |
|
185 |
|
|
186 |
my $span = $max_subtract_days + $max_add_days; |
|
187 |
my $rand = int(rand($span)); |
|
188 |
|
|
189 |
return DateTime->today_local->subtract( days => $max_subtract_days )->add( days => $rand ); |
|
190 |
} |
|
191 |
|
|
192 |
sub create_rand_entries { |
|
193 |
my $chart = shift; |
|
194 |
my $i = shift // 40; |
|
195 |
|
|
196 |
my $single_prob = 0.3; |
|
197 |
my $mult_prob = 0.3; |
|
198 |
# create lots of random entries |
|
199 |
for my $i ( 1000 .. (1000+$i) ) { |
|
200 |
my $amount = int(rand(99))+1 + (int(rand(99))+1)/100; |
|
201 |
|
|
202 |
quick_gl($bank, $amount, $chart, $amount, $i); |
|
203 |
my $rand = rand(1); |
|
204 |
if ( $rand < 0.3 ) { |
|
205 |
# most entries will be clearable |
|
206 |
quick_gl($chart, $amount, $bank, $amount, $i); |
|
207 |
if ( rand(1) > 0.6 ) { |
|
208 |
# some of the entries will already be set as cleared |
|
209 |
$expected_cleared_entries += create_cleared_for_chart_and_reference($chart, "$i"); |
|
210 |
} |
|
211 |
} elsif ( $rand < ($single_prob + $mult_prob) ) { |
|
212 |
quick_gl_multi($chart, $amount, $bank, $amount, $i); |
|
213 |
if ( rand(1) > 0.6 ) { |
|
214 |
# some of the entries will already be set as cleared |
|
215 |
$expected_cleared_entries += create_cleared_for_chart_and_reference($chart, "$i"); |
|
216 |
} |
|
217 |
} else { |
|
218 |
# no matching booking |
|
219 |
} |
|
220 |
} |
|
221 |
} |
Auch abrufbar als: Unified diff
Konten ausziffern - Tests