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::GLTransaction->delete_all(all => 1);
 
  49   SL::DB::Manager::PaymentTerm->delete_all(all => 1);
 
  50   SL::DB::Manager::Currency->delete_all(where => [ name => 'CUR' ]);
 
  55 Support::TestSetup::login();
 
  57 reset_state(); # initialise customers/vendors/bank/currency/...
 
  61 # remove all created data at end of test
 
  66 ###### functions for setting up data
 
  71   $params{$_} ||= {} for qw(unit customer part tax vendor);
 
  75   $transdate1 = DateTime->today;
 
  76   $transdate2 = DateTime->today->add(days => 5);
 
  78   $employee        = SL::DB::Manager::Employee->current                                          || croak "No employee";
 
  79   $tax             = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} }) || croak "No tax";
 
  80   $tax7            = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07)                    || croak "No tax for 7\%";
 
  81   $taxzone         = SL::DB::Manager::TaxZone->find_by( description => 'Inland')                 || croak "No taxzone";
 
  82   $tax_9           = SL::DB::Manager::Tax->find_by(taxkey => 9, rate => 0.19, %{ $params{tax} }) || croak "No tax";
 
  84   $currency_id     = $::instance_conf->get_currency_id;
 
  86   $bank_account     =  SL::DB::BankAccount->new(
 
  87     account_number  => '123',
 
  92     chart_id        => SL::DB::Manager::Chart->find_by(description => 'Bank')->id,
 
  93     name            => SL::DB::Manager::Chart->find_by(description => 'Bank')->description,
 
  96   $customer     = SL::DB::Customer->new(
 
  97     name                      => 'Test Customer',
 
  98     currency_id               => $currency_id,
 
  99     taxzone_id                => $taxzone->id,
 
 100     iban                      => 'DE12500105170648489890',
 
 102     account_number            => '648489890',
 
 103     mandate_date_of_signature => $transdate1,
 
 104     mandator_id               => 'foobar',
 
 106     depositor                 => 'Test Customer',
 
 107     %{ $params{customer} }
 
 110   $payment_terms     =  SL::DB::PaymentTerm->new(
 
 111     description      => 'payment',
 
 112     description_long => 'payment',
 
 115     percent_skonto   => '0.05',
 
 116     auto_calculation => 1,
 
 119   $vendor       = SL::DB::Vendor->new(
 
 120     name        => 'Test Vendor',
 
 121     currency_id => $currency_id,
 
 122     taxzone_id  => $taxzone->id,
 
 123     payment_id  => $payment_terms->id,
 
 124     iban                      => 'DE12500105170648489890',
 
 126     account_number            => '648489890',
 
 128     depositor                 => 'Test Vendor',
 
 132   $ar_chart        = SL::DB::Manager::Chart->find_by( accno => '1400' ); # Forderungen
 
 133   $ap_chart        = SL::DB::Manager::Chart->find_by( accno => '1600' ); # Verbindlichkeiten
 
 134   $bank            = SL::DB::Manager::Chart->find_by( accno => '1200' ); # Bank
 
 135   $ar_amount_chart = SL::DB::Manager::Chart->find_by( accno => '8400' ); # Erlöse
 
 136   $ap_amount_chart = SL::DB::Manager::Chart->find_by( accno => '3400' ); # Wareneingang 19%
 
 137   $saldo_chart     = SL::DB::Manager::Chart->find_by( accno => '9000' ); # Saldenvorträge
 
 141 sub test_ar_transaction {
 
 144   my $amount    = $params{amount} || $::form->round_amount(100 * 1.19,2);
 
 145   my $invoice   = SL::DB::Invoice->new(
 
 147       invnumber    => $params{invnumber} || undef, # let it use its own invnumber
 
 149       netamount    => $netamount,
 
 150       transdate    => $transdate1,
 
 152       customer_id  => $customer->id,
 
 153       taxzone_id   => $customer->taxzone_id,
 
 154       currency_id  => $currency_id,
 
 156       payment_id   => $params{payment_id} || undef,
 
 157       notes        => 'test_ar_transaction',
 
 159   $invoice->add_ar_amount_row(
 
 160     amount => $invoice->netamount,
 
 161     chart  => $ar_amount_chart,
 
 165   $invoice->create_ar_row(chart => $ar_chart);
 
 168   is($invoice->currency_id , $currency_id , 'currency_id has been saved');
 
 169   is($invoice->netamount   , 100          , 'ar amount has been converted');
 
 170   is($invoice->amount      , 119          , 'ar amount has been converted');
 
 171   is($invoice->taxincluded , 0            , 'ar transaction doesn\'t have taxincluded');
 
 173   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');
 
 174   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');
 
 179 sub test_ap_transaction {
 
 182   my $amount    = $::form->round_amount($netamount * 1.19,2);
 
 183   my $invoice   = SL::DB::PurchaseInvoice->new(
 
 185       invnumber    => $params{invnumber} || 'test_ap_transaction',
 
 187       netamount    => $netamount,
 
 188       transdate    => $transdate1,
 
 190       vendor_id    => $vendor->id,
 
 191       taxzone_id   => $vendor->taxzone_id,
 
 192       currency_id  => $currency_id,
 
 194       notes        => 'test_ap_transaction',
 
 196   $invoice->add_ap_amount_row(
 
 197     amount     => $invoice->netamount,
 
 198     chart      => $ap_amount_chart,
 
 199     tax_id     => $tax_9->id,
 
 202   $invoice->create_ap_row(chart => $ap_chart);
 
 205   is($invoice->currency_id , $currency_id , 'currency_id has been saved');
 
 206   is($invoice->netamount   , 100          , 'ap amount has been converted');
 
 207   is($invoice->amount      , 119          , 'ap amount has been converted');
 
 208   is($invoice->taxincluded , 0            , 'ap transaction doesn\'t have taxincluded');
 
 210   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');
 
 211   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');
 
 220   my $testname = 'test1';
 
 222   $ar_transaction = test_ar_transaction(invnumber => 'salesinv1');
 
 223   $ap_transaction = test_ap_transaction(invnumber => 'purchaseinv1');
 
 224   my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv_2');
 
 226   my $yt_controller = SL::Controller::YearEndTransactions->new;
 
 227   my $report     = SL::ReportGenerator->new(\%::myconfig, $::form);
 
 229   $::form->{"ob_date"} = DateTime->today->truncate(to => 'year')->add(years => 1)->to_kivitendo;
 
 230   $::form->{"cb_date"} = DateTime->today->truncate(to => 'year')->add(years => 1)->add(days => -1)->to_kivitendo;
 
 231   #print "ob_date=".$::form->{"ob_date"}." cb_date=".$::form->{"cb_date"}."\n";
 
 232   $::form->{"cb_reference"} = 'SB-Buchung';
 
 233   $::form->{"ob_reference"} = 'EB-Buchung';
 
 234   $::form->{"cb_description"} = 'SB-Buchung Beschreibung';
 
 235   $::form->{"ob_description"} = 'EB-Buchung Beschreibung';
 
 236   $::form->{"cbob_chart"} = $saldo_chart->id;
 
 238   $yt_controller->prepare_report($report);
 
 240   ## check balance of charts
 
 243   foreach my $chart (@{ $yt_controller->charts }) {
 
 244     my $balance = $yt_controller->get_balance($chart);
 
 245     if ( $balance != 0 ) {
 
 246       #print "chart_id=".$chart->id."balance=".$balance."\n";
 
 247       is($balance , '-238.00000' , $chart->accno.' has right balance') if $chart->accno eq '1400';
 
 248       is($balance ,  '-19.00000' , $chart->accno.' has right balance') if $chart->accno eq '1576';
 
 249       is($balance ,  '119.00000' , $chart->accno.' has right balance') if $chart->accno eq '1600';
 
 250       is($balance ,   '38.00000' , $chart->accno.' has right balance') if $chart->accno eq '1776';
 
 251       is($balance , '-100.00000' , $chart->accno.' has right balance') if $chart->accno eq '3400';
 
 252       is($balance ,  '200.00000' , $chart->accno.' has right balance') if $chart->accno eq '8400';
 
 253       $::form->{"multi_id_${idx}"} = $chart->id;
 
 257   $::form->{"rowcount"} = $idx-1;
 
 258   #print "rowcount=". $::form->{"rowcount"}."\n";
 
 259   $::form->{"login"}="unittests";
 
 261   $yt_controller->make_booking;
 
 263   ## no check cb ob booking :
 
 267   foreach my $acc ( @{ SL::DB::Manager::AccTransaction->get_all(where => [ chart_id => $saldo_chart->id, cb_transaction => 't' ]) }) {
 
 268     #print "cb amount=".$acc->amount."\n";
 
 269     $sum_cb_p +=  $acc->amount if $acc->amount > 0;
 
 270     $sum_cb_m += -$acc->amount if $acc->amount < 0;
 
 272   #print "chart_id=".$saldo_chart->id." sum_cb_p=".$sum_cb_p." sum_cb_m=".$sum_cb_m."\n";
 
 273   is($sum_cb_p ,  '357' , 'chart '.$saldo_chart->accno.' has right positive close saldo');
 
 274   is($sum_cb_m ,  '357' , 'chart '.$saldo_chart->accno.' has right negative close saldo');
 
 277   foreach my $acc ( @{ SL::DB::Manager::AccTransaction->get_all(where => [ chart_id => $saldo_chart->id, ob_transaction => 't' ]) }) {
 
 278     #print "ob amount=".$acc->amount."\n";
 
 279     $sum_ob_p +=  $acc->amount if $acc->amount > 0;
 
 280     $sum_ob_m += -$acc->amount if $acc->amount < 0;
 
 282   #print "chart_id=".$saldo_chart->id." sum_ob_p=".$sum_ob_p." sum_ob_m=".$sum_ob_m."\n";
 
 283   is($sum_ob_p ,  '357' , 'chart '.$saldo_chart->accno.' has right positive open saldo');
 
 284   is($sum_ob_m ,  '357' , 'chart '.$saldo_chart->accno.' has right negative open saldo');