478295315f1ba25b38b59ab242939a04ecdc7fe3
[kivitendo-erp.git] / t / bank / cb_ob_transactions.t
1 use Test::More;
2
3 use strict;
4
5 use lib 't';
6 use utf8;
7
8 use Carp;
9 use Support::TestSetup;
10 use Test::Exception;
11 use List::Util qw(sum);
12
13 use SL::DB::Buchungsgruppe;
14 use SL::DB::Currency;
15 use SL::DB::Exchangerate;
16 use SL::DB::Customer;
17 use SL::DB::Vendor;
18 use SL::DB::Employee;
19 use SL::DB::Invoice;
20 use SL::DB::Part;
21 use SL::DB::Unit;
22 use SL::DB::TaxZone;
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;
29 use Data::Dumper;
30
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);
35
36 sub clear_up {
37
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' ]);
50 };
51
52
53 # starting test:
54 Support::TestSetup::login();
55
56 reset_state(); # initialise customers/vendors/bank/currency/...
57
58 test1();
59
60 # remove all created data at end of test
61 clear_up();
62
63 done_testing();
64
65 ###### functions for setting up data
66
67 sub reset_state {
68   my %params = @_;
69
70   $params{$_} ||= {} for qw(unit customer part tax vendor);
71
72   clear_up();
73
74   $transdate1 = DateTime->today;
75   $transdate2 = DateTime->today->add(days => 5);
76
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";
82
83   $currency_id     = $::instance_conf->get_currency_id;
84
85   $bank_account     =  SL::DB::BankAccount->new(
86     account_number  => '123',
87     bank_code       => '123',
88     iban            => '123',
89     bic             => '123',
90     bank            => '123',
91     chart_id        => SL::DB::Manager::Chart->find_by(description => 'Bank')->id,
92     name            => SL::DB::Manager::Chart->find_by(description => 'Bank')->description,
93   )->save;
94
95   $customer     = SL::DB::Customer->new(
96     name                      => 'Test Customer',
97     currency_id               => $currency_id,
98     taxzone_id                => $taxzone->id,
99     iban                      => 'DE12500105170648489890',
100     bic                       => 'TESTBIC',
101     account_number            => '648489890',
102     mandate_date_of_signature => $transdate1,
103     mandator_id               => 'foobar',
104     bank                      => 'Geizkasse',
105     depositor                 => 'Test Customer',
106     %{ $params{customer} }
107   )->save;
108
109   $payment_terms     =  SL::DB::PaymentTerm->new(
110     description      => 'payment',
111     description_long => 'payment',
112     terms_netto      => '30',
113     terms_skonto     => '5',
114     percent_skonto   => '0.05',
115     auto_calculation => 1,
116   )->save;
117
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',
124     bic                       => 'TESTBIC',
125     account_number            => '648489890',
126     bank                      => 'Geizkasse',
127     depositor                 => 'Test Vendor',
128     %{ $params{vendor} }
129   )->save;
130
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
137
138 }
139
140 sub test_ar_transaction {
141   my (%params) = @_;
142   my $netamount = 100;
143   my $amount    = $params{amount} || $::form->round_amount(100 * 1.19,2);
144   my $invoice   = SL::DB::Invoice->new(
145       invoice      => 0,
146       invnumber    => $params{invnumber} || undef, # let it use its own invnumber
147       amount       => $amount,
148       netamount    => $netamount,
149       transdate    => $transdate1,
150       taxincluded  => 0,
151       customer_id  => $customer->id,
152       taxzone_id   => $customer->taxzone_id,
153       currency_id  => $currency_id,
154       transactions => [],
155       payment_id   => $params{payment_id} || undef,
156       notes        => 'test_ar_transaction',
157   );
158   $invoice->add_ar_amount_row(
159     amount => $invoice->netamount,
160     chart  => $ar_amount_chart,
161     tax_id => $tax->id,
162   );
163
164   $invoice->create_ar_row(chart => $ar_chart);
165   $invoice->save;
166
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');
171
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');
174
175   return $invoice;
176 };
177
178 sub test_ap_transaction {
179   my (%params) = @_;
180   my $netamount = 100;
181   my $amount    = $::form->round_amount($netamount * 1.19,2);
182   my $invoice   = SL::DB::PurchaseInvoice->new(
183       invoice      => 0,
184       invnumber    => $params{invnumber} || 'test_ap_transaction',
185       amount       => $amount,
186       netamount    => $netamount,
187       transdate    => $transdate1,
188       taxincluded  => 0,
189       vendor_id    => $vendor->id,
190       taxzone_id   => $vendor->taxzone_id,
191       currency_id  => $currency_id,
192       transactions => [],
193       notes        => 'test_ap_transaction',
194   );
195   $invoice->add_ap_amount_row(
196     amount     => $invoice->netamount,
197     chart      => $ap_amount_chart,
198     tax_id     => $tax_9->id,
199   );
200
201   $invoice->create_ap_row(chart => $ap_chart);
202   $invoice->save;
203
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');
208
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');
211
212   return $invoice;
213 };
214
215 ###### test cases
216
217 sub test1 {
218
219   my $testname = 'test1';
220
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');
224
225   my $yt_controller = SL::Controller::YearEndTransactions->new;
226   my $report     = SL::ReportGenerator->new(\%::myconfig, $::form);
227
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;
236
237   $yt_controller->prepare_report($report);
238
239   ## check balance of charts
240
241   my $idx = 1;
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;
253       $idx++ ;
254     }
255   }
256   $::form->{"rowcount"} = $idx-1;
257   #print "rowcount=". $::form->{"rowcount"}."\n";
258   $::form->{"login"}="unittests";
259
260   $yt_controller->make_booking;
261
262   ## no check cb ob booking :
263
264   my $sum_cb_p = 0;
265   my $sum_cb_m = 0;
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;
270   }
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');
274   my $sum_ob_p = 0;
275   my $sum_ob_m = 0;
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;
280   }
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');
284 }
285
286
287
288 1;