9 use Support::TestSetup;
11 use List::Util qw(sum);
13 use SL::DB::Buchungsgruppe;
15 use SL::DB::Exchangerate;
23 use SL::DB::BankAccount;
24 use SL::DB::PaymentTerm;
25 use SL::DB::PurchaseInvoice;
26 use SL::DB::BankTransaction;
27 use SL::DB::AccTransaction;
28 use SL::Controller::YearEndTransactions;
31 my ($customer, $vendor, $currency_id, @parts, $unit, $employee, $tax, $tax7, $tax_9, $taxzone, $payment_terms, $bank_account);
32 my ($transdate1, $transdate2, $currency);
33 my ($ar_chart,$bank,$ar_amount_chart, $ap_chart, $ap_amount_chart, $saldo_chart);
34 my ($ar_transaction, $ap_transaction);
38 SL::DB::Manager::BankTransaction->delete_all(all => 1);
39 SL::DB::Manager::InvoiceItem->delete_all(all => 1);
40 SL::DB::Manager::InvoiceItem->delete_all(all => 1);
41 SL::DB::Manager::Invoice->delete_all(all => 1);
42 SL::DB::Manager::PurchaseInvoice->delete_all(all => 1);
43 SL::DB::Manager::Part->delete_all(all => 1);
44 SL::DB::Manager::Customer->delete_all(all => 1);
45 SL::DB::Manager::Vendor->delete_all(all => 1);
46 SL::DB::Manager::BankAccount->delete_all(all => 1);
47 SL::DB::Manager::AccTransaction->delete_all(all => 1);
48 SL::DB::Manager::PaymentTerm->delete_all(all => 1);
49 SL::DB::Manager::Currency->delete_all(where => [ name => 'CUR' ]);
54 Support::TestSetup::login();
56 reset_state(); # initialise customers/vendors/bank/currency/...
60 # remove all created data at end of test
65 ###### functions for setting up data
70 $params{$_} ||= {} for qw(unit customer part tax vendor);
74 $transdate1 = DateTime->today;
75 $transdate2 = DateTime->today->add(days => 5);
77 $employee = SL::DB::Manager::Employee->current || croak "No employee";
78 $tax = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} }) || croak "No tax";
79 $tax7 = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07) || croak "No tax for 7\%";
80 $taxzone = SL::DB::Manager::TaxZone->find_by( description => 'Inland') || croak "No taxzone";
81 $tax_9 = SL::DB::Manager::Tax->find_by(taxkey => 9, rate => 0.19, %{ $params{tax} }) || croak "No tax";
83 $currency_id = $::instance_conf->get_currency_id;
85 $bank_account = SL::DB::BankAccount->new(
86 account_number => '123',
91 chart_id => SL::DB::Manager::Chart->find_by(description => 'Bank')->id,
92 name => SL::DB::Manager::Chart->find_by(description => 'Bank')->description,
95 $customer = SL::DB::Customer->new(
96 name => 'Test Customer',
97 currency_id => $currency_id,
98 taxzone_id => $taxzone->id,
99 iban => 'DE12500105170648489890',
101 account_number => '648489890',
102 mandate_date_of_signature => $transdate1,
103 mandator_id => 'foobar',
105 depositor => 'Test Customer',
106 %{ $params{customer} }
109 $payment_terms = SL::DB::PaymentTerm->new(
110 description => 'payment',
111 description_long => 'payment',
114 percent_skonto => '0.05',
115 auto_calculation => 1,
118 $vendor = SL::DB::Vendor->new(
119 name => 'Test Vendor',
120 currency_id => $currency_id,
121 taxzone_id => $taxzone->id,
122 payment_id => $payment_terms->id,
123 iban => 'DE12500105170648489890',
125 account_number => '648489890',
127 depositor => 'Test Vendor',
131 $ar_chart = SL::DB::Manager::Chart->find_by( accno => '1400' ); # Forderungen
132 $ap_chart = SL::DB::Manager::Chart->find_by( accno => '1600' ); # Verbindlichkeiten
133 $bank = SL::DB::Manager::Chart->find_by( accno => '1200' ); # Bank
134 $ar_amount_chart = SL::DB::Manager::Chart->find_by( accno => '8400' ); # Erlöse
135 $ap_amount_chart = SL::DB::Manager::Chart->find_by( accno => '3400' ); # Wareneingang 19%
136 $saldo_chart = SL::DB::Manager::Chart->find_by( accno => '9000' ); # Saldenvorträge
140 sub test_ar_transaction {
143 my $amount = $params{amount} || $::form->round_amount(100 * 1.19,2);
144 my $invoice = SL::DB::Invoice->new(
146 invnumber => $params{invnumber} || undef, # let it use its own invnumber
148 netamount => $netamount,
149 transdate => $transdate1,
151 customer_id => $customer->id,
152 taxzone_id => $customer->taxzone_id,
153 currency_id => $currency_id,
155 payment_id => $params{payment_id} || undef,
156 notes => 'test_ar_transaction',
158 $invoice->add_ar_amount_row(
159 amount => $invoice->netamount,
160 chart => $ar_amount_chart,
164 $invoice->create_ar_row(chart => $ar_chart);
167 is($invoice->currency_id , $currency_id , 'currency_id has been saved');
168 is($invoice->netamount , 100 , 'ar amount has been converted');
169 is($invoice->amount , 119 , 'ar amount has been converted');
170 is($invoice->taxincluded , 0 , 'ar transaction doesn\'t have taxincluded');
172 is(SL::DB::Manager::AccTransaction->find_by(chart_id => $ar_amount_chart->id , trans_id => $invoice->id)->amount , '100.00000' , $ar_amount_chart->accno . ': has been converted for currency');
173 is(SL::DB::Manager::AccTransaction->find_by(chart_id => $ar_chart->id , trans_id => $invoice->id)->amount , '-119.00000' , $ar_chart->accno . ': has been converted for currency');
178 sub test_ap_transaction {
181 my $amount = $::form->round_amount($netamount * 1.19,2);
182 my $invoice = SL::DB::PurchaseInvoice->new(
184 invnumber => $params{invnumber} || 'test_ap_transaction',
186 netamount => $netamount,
187 transdate => $transdate1,
189 vendor_id => $vendor->id,
190 taxzone_id => $vendor->taxzone_id,
191 currency_id => $currency_id,
193 notes => 'test_ap_transaction',
195 $invoice->add_ap_amount_row(
196 amount => $invoice->netamount,
197 chart => $ap_amount_chart,
198 tax_id => $tax_9->id,
201 $invoice->create_ap_row(chart => $ap_chart);
204 is($invoice->currency_id , $currency_id , 'currency_id has been saved');
205 is($invoice->netamount , 100 , 'ap amount has been converted');
206 is($invoice->amount , 119 , 'ap amount has been converted');
207 is($invoice->taxincluded , 0 , 'ap transaction doesn\'t have taxincluded');
209 is(SL::DB::Manager::AccTransaction->find_by(chart_id => $ap_amount_chart->id , trans_id => $invoice->id)->amount , '-100.00000' , $ap_amount_chart->accno . ': has been converted for currency');
210 is(SL::DB::Manager::AccTransaction->find_by(chart_id => $ap_chart->id , trans_id => $invoice->id)->amount , '119.00000' , $ap_chart->accno . ': has been converted for currency');
219 my $testname = 'test1';
221 $ar_transaction = test_ar_transaction(invnumber => 'salesinv1');
222 $ap_transaction = test_ap_transaction(invnumber => 'purchaseinv1');
223 my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv_2');
225 my $yt_controller = SL::Controller::YearEndTransactions->new;
226 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
228 $::form->{"ob_date"} = DateTime->today->truncate(to => 'year')->add(years => 1)->to_kivitendo;
229 $::form->{"cb_date"} = DateTime->today->truncate(to => 'year')->add(years => 1)->add(days => -1)->to_kivitendo;
230 #print "ob_date=".$::form->{"ob_date"}." cb_date=".$::form->{"cb_date"}."\n";
231 $::form->{"cb_reference"} = 'SB-Buchung';
232 $::form->{"ob_reference"} = 'EB-Buchung';
233 $::form->{"cb_description"} = 'SB-Buchung Beschreibung';
234 $::form->{"ob_description"} = 'EB-Buchung Beschreibung';
235 $::form->{"cbob_chart"} = $saldo_chart->id;
237 $yt_controller->prepare_report($report);
239 ## check balance of charts
242 foreach my $chart (@{ $yt_controller->charts }) {
243 my $balance = $yt_controller->get_balance($chart);
244 if ( $balance != 0 ) {
245 #print "chart_id=".$chart->id."balance=".$balance."\n";
246 is($balance , '-238.00000' , $chart->accno.' has right balance') if $chart->accno eq '1400';
247 is($balance , '-19.00000' , $chart->accno.' has right balance') if $chart->accno eq '1576';
248 is($balance , '119.00000' , $chart->accno.' has right balance') if $chart->accno eq '1600';
249 is($balance , '38.00000' , $chart->accno.' has right balance') if $chart->accno eq '1776';
250 is($balance , '-100.00000' , $chart->accno.' has right balance') if $chart->accno eq '3400';
251 is($balance , '200.00000' , $chart->accno.' has right balance') if $chart->accno eq '8400';
252 $::form->{"multi_id_${idx}"} = $chart->id;
256 $::form->{"rowcount"} = $idx-1;
257 #print "rowcount=". $::form->{"rowcount"}."\n";
258 $::form->{"login"}="unittests";
260 $yt_controller->make_booking;
262 ## no check cb ob booking :
266 foreach my $acc ( @{ SL::DB::Manager::AccTransaction->get_all(where => [ chart_id => $saldo_chart->id, cb_transaction => 't' ]) }) {
267 #print "cb amount=".$acc->amount."\n";
268 $sum_cb_p += $acc->amount if $acc->amount > 0;
269 $sum_cb_m += -$acc->amount if $acc->amount < 0;
271 #print "chart_id=".$saldo_chart->id." sum_cb_p=".$sum_cb_p." sum_cb_m=".$sum_cb_m."\n";
272 is($sum_cb_p , '357' , 'chart '.$saldo_chart->accno.' has right positive close saldo');
273 is($sum_cb_m , '357' , 'chart '.$saldo_chart->accno.' has right negative close saldo');
276 foreach my $acc ( @{ SL::DB::Manager::AccTransaction->get_all(where => [ chart_id => $saldo_chart->id, ob_transaction => 't' ]) }) {
277 #print "ob amount=".$acc->amount."\n";
278 $sum_ob_p += $acc->amount if $acc->amount > 0;
279 $sum_ob_m += -$acc->amount if $acc->amount < 0;
281 #print "chart_id=".$saldo_chart->id." sum_ob_p=".$sum_ob_p." sum_ob_m=".$sum_ob_m."\n";
282 is($sum_ob_p , '357' , 'chart '.$saldo_chart->accno.' has right positive open saldo');
283 is($sum_ob_m , '357' , 'chart '.$saldo_chart->accno.' has right negative open saldo');