2 use Test::More tests => 8;
5 use Support::TestSetup;
10 use SL::DB::GLTransaction;
12 use SL::DBUtils qw(selectall_hashref_query);
14 Support::TestSetup::login();
18 my $cash = SL::DB::Manager::Chart->find_by( description => 'Kasse' );
19 my $bank = SL::DB::Manager::Chart->find_by( description => 'Bank' );
20 my $betriebsbedarf = SL::DB::Manager::Chart->find_by( description => 'Betriebsbedarf' );
22 my $tax_9 = SL::DB::Manager::Tax->find_by(taxkey => 9, rate => 0.19);
23 my $tax_8 = SL::DB::Manager::Tax->find_by(taxkey => 8, rate => 0.07);
24 my $tax_0 = SL::DB::Manager::Tax->find_by(taxkey => 0, rate => 0.00);
26 my $dbh = SL::DB->client->dbh;
28 # example with chaining of add_chart_booking
29 my $gl_transaction = SL::DB::GLTransaction->new(
31 reference => 'bank/cash',
32 description => 'bank/cash',
33 transdate => DateTime->today_local,
44 # example where bookings is prepared separately as an arrayref
45 my $gl_transaction_2 = SL::DB::GLTransaction->new(
46 reference => 'betriebsbedarf several rows',
47 description => 'betriebsbedarf',
49 transdate => DateTime->today_local,
54 chart => $betriebsbedarf,
61 chart => $betriebsbedarf,
75 $gl_transaction_2->add_chart_booking(%{$_}) foreach @{ $bookings };
76 $gl_transaction_2->post;
79 # example where add_chart_booking is called via a foreach
80 my $gl_transaction_3 = SL::DB::GLTransaction->new(
81 reference => 'betriebsbedarf tax included',
84 transdate => DateTime->today_local,
86 $gl_transaction_3->add_chart_booking(%{$_}) foreach (
88 chart => $betriebsbedarf,
93 chart => $betriebsbedarf,
98 chart => $betriebsbedarf,
100 tax_id => $tax_0->id,
105 tax_id => $tax_0->id,
108 $gl_transaction_3->post;
110 my $gl_transaction_4 = SL::DB::GLTransaction->new(
111 reference => 'betriebsbedarf tax not included',
112 description => 'bar',
114 transdate => DateTime->today_local,
116 $gl_transaction_4->add_chart_booking(%{$_}) foreach (
118 chart => $betriebsbedarf,
120 tax_id => $tax_9->id,
123 chart => $betriebsbedarf,
125 tax_id => $tax_8->id,
128 chart => $betriebsbedarf,
130 tax_id => $tax_0->id,
135 tax_id => $tax_0->id,
138 $gl_transaction_4->post;
140 is(SL::DB::Manager::GLTransaction->get_all_count(), 4, "gl transactions created ok");
142 is_deeply(&get_account_balances,
150 'sum' => '-100.00000'
162 'sum' => '-800.00000'
169 note('testing subcent');
171 my $gl_transaction_5_taxinc = SL::DB::GLTransaction->new(
173 reference => 'subcent tax included',
174 description => 'subcent tax included',
175 transdate => DateTime->today_local,
176 )->add_chart_booking(
177 chart => $betriebsbedarf,
179 tax_id => $tax_9->id,
180 )->add_chart_booking(
183 tax_id => $tax_0->id,
186 my $gl_transaction_5_taxnoinc = SL::DB::GLTransaction->new(
188 reference => 'subcent tax not included',
189 description => 'subcent tax not included',
190 transdate => DateTime->today_local,
191 )->add_chart_booking(
192 chart => $betriebsbedarf,
194 tax_id => $tax_9->id,
195 )->add_chart_booking(
198 tax_id => $tax_0->id,
201 my $gl_transaction_6_taxinc = SL::DB::GLTransaction->new(
203 reference => 'cent tax included',
204 description => 'cent tax included',
205 transdate => DateTime->today_local,
206 )->add_chart_booking(
207 chart => $betriebsbedarf,
209 tax_id => $tax_9->id,
210 )->add_chart_booking(
213 tax_id => $tax_0->id,
216 my $gl_transaction_6_taxnoinc = SL::DB::GLTransaction->new(
218 reference => 'cent tax included',
219 description => 'cent tax included',
220 transdate => DateTime->today_local,
221 )->add_chart_booking(
222 chart => $betriebsbedarf,
224 tax_id => $tax_9->id,
225 )->add_chart_booking(
228 tax_id => $tax_0->id,
231 is(SL::DB::Manager::GLTransaction->get_all_count(), 8, "gl transactions created ok");
234 is_deeply(&get_account_balances,
242 'sum' => '-100.00000'
254 'sum' => '-800.12000'
260 note "testing automatic tax 19%";
262 my $gl_transaction_7 = SL::DB::GLTransaction->new(
263 reference => 'betriebsbedarf tax not included',
264 description => 'bar',
266 transdate => DateTime->new(year => 2019, month => 12, day => 30),
269 $gl_transaction_7->add_chart_booking(%{$_}) foreach (
271 chart => $betriebsbedarf,
275 chart => $betriebsbedarf,
279 chart => $betriebsbedarf,
281 tax_id => $tax_0->id,
288 $gl_transaction_7->post;
290 is(SL::DB::Manager::GLTransaction->get_all_count(), 9, "gl transactions created ok");
291 is_deeply(&get_account_balances,
295 'sum' => '1328.14000'
299 'sum' => '-100.00000'
307 'sum' => '-114.02000'
311 'sum' => '-1100.12000'
317 note "testing automatic tax 16%";
319 my $gl_transaction_8 = SL::DB::GLTransaction->new(
320 reference => 'betriebsbedarf tax not included',
321 description => 'bar',
323 transdate => DateTime->new(year => 2020, month => 12, day => 31),
326 $gl_transaction_8->add_chart_booking(%{$_}) foreach (
328 chart => $betriebsbedarf,
332 chart => $betriebsbedarf,
336 chart => $betriebsbedarf,
338 tax_id => $tax_0->id,
345 $gl_transaction_8->post;
347 is(SL::DB::Manager::GLTransaction->get_all_count(), 10, "gl transactions created ok");
348 is_deeply(&get_account_balances,
352 'sum' => '1660.14000'
356 'sum' => '-100.00000'
368 'sum' => '-114.02000'
372 'sum' => '-1400.12000'
384 "SL::DB::Manager::${_}"->delete_all(all => 1) for qw(
390 sub get_account_balances {
395 left join chart c on (c.id = a.chart_id)
400 my $result = selectall_hashref_query($::form, $dbh, $query);