1 use Test::More tests => 289;
 
   9 use Support::TestSetup;
 
  11 use List::Util qw(sum);
 
  13 use SL::DB::AccTransaction;
 
  14 use SL::DB::BankTransactionAccTrans;
 
  15 use SL::DB::Buchungsgruppe;
 
  24 use SL::DB::BankAccount;
 
  25 use SL::DB::PaymentTerm;
 
  26 use SL::DB::PurchaseInvoice;
 
  27 use SL::DB::BankTransaction;
 
  28 use SL::Controller::BankTransaction;
 
  29 use SL::Controller::Reconciliation;
 
  30 use SL::Dev::ALL qw(:ALL);
 
  33 my ($customer, $vendor, $currency_id, $unit, $tax, $tax0, $tax7, $tax_9, $payment_terms, $bank_account);
 
  34 my ($transdate1, $transdate2, $currency);
 
  35 my ($ar_chart,$bank,$ar_amount_chart, $ap_chart, $ap_amount_chart);
 
  36 my ($ar_transaction, $ap_transaction);
 
  40   SL::DB::Manager::BankTransactionAccTrans->delete_all(all => 1);
 
  41   SL::DB::Manager::BankTransaction->delete_all(all => 1);
 
  42   SL::DB::Manager::InvoiceItem->delete_all(all => 1);
 
  43   SL::DB::Manager::InvoiceItem->delete_all(all => 1);
 
  44   SL::DB::Manager::Invoice->delete_all(all => 1);
 
  45   SL::DB::Manager::PurchaseInvoice->delete_all(all => 1);
 
  46   SL::DB::Manager::Part->delete_all(all => 1);
 
  47   SL::DB::Manager::Customer->delete_all(all => 1);
 
  48   SL::DB::Manager::Vendor->delete_all(all => 1);
 
  49   SL::DB::Manager::SepaExportItem->delete_all(all => 1);
 
  50   SL::DB::Manager::SepaExport->delete_all(all => 1);
 
  51   SL::DB::Manager::BankAccount->delete_all(all => 1);
 
  52   SL::DB::Manager::PaymentTerm->delete_all(all => 1);
 
  53   SL::DB::Manager::Currency->delete_all(where => [ name => 'CUR' ]);
 
  54   # SL::DB::Manager::Default->delete_all(all => 1);
 
  59 sub save_btcontroller_to_string {
 
  61   open(my $outputFH, '>', \$output) or die;
 
  62   my $oldFH = select $outputFH;
 
  64   $bt_controller = SL::Controller::BankTransaction->new;
 
  65   $bt_controller->action_save_invoices;
 
  73 Support::TestSetup::login();
 
  76 reset_state(); # initialise customers/vendors/bank/currency/...
 
  80 test_overpayment_with_partialpayment();
 
  85 test_partial_payment();
 
  87 test_ap_transaction();
 
  88 test_neg_ap_transaction(invoice => 0);
 
  89 test_neg_ap_transaction(invoice => 1);
 
  90 test_ap_payment_transaction();
 
  91 test_ap_payment_part_transaction();
 
  92 test_neg_sales_invoice();
 
  93 test_two_neg_ap_transaction();
 
  94 test_one_inv_and_two_invoices_with_skonto_exact();
 
  96 test_full_workflow_ar_multiple_inv_skonto_reconciliate_and_undo();
 
 103 test_two_banktransactions();
 
 104 # remove all created data at end of test
 
 110 ###### functions for setting up data
 
 115   $params{$_} ||= {} for qw(unit customer tax vendor);
 
 119   $transdate1 = DateTime->today;
 
 120   $transdate2 = DateTime->today->add(days => 5);
 
 122   $tax             = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} }) || croak "No tax";
 
 123   $tax7            = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07)                    || croak "No tax for 7\%";
 
 124   $tax_9           = SL::DB::Manager::Tax->find_by(taxkey => 9, rate => 0.19, %{ $params{tax} }) || croak "No tax for 19\%";
 
 125   $tax0            = SL::DB::Manager::Tax->find_by(taxkey => 0, rate => 0.0)                     || croak "No tax for 0\%";
 
 127   $currency_id     = $::instance_conf->get_currency_id;
 
 129   $bank_account     =  SL::DB::BankAccount->new(
 
 130     account_number  => '123',
 
 135     chart_id        => SL::DB::Manager::Chart->find_by(description => 'Bank')->id,
 
 136     name            => SL::DB::Manager::Chart->find_by(description => 'Bank')->description,
 
 139   $customer = new_customer(
 
 140     name                      => 'Test Customer OLÉ S.L. Årdbärg AB',
 
 141     iban                      => 'DE12500105170648489890',
 
 143     account_number            => '648489890',
 
 144     mandate_date_of_signature => $transdate1,
 
 145     mandator_id               => 'foobar',
 
 147     bank_code                 => 'G1235',
 
 148     depositor                 => 'Test Customer',
 
 149     customernumber            => 'CUST1704',
 
 152   $payment_terms = create_payment_terms();
 
 154   $vendor = new_vendor(
 
 155     name           => 'Test Vendor',
 
 156     payment_id     => $payment_terms->id,
 
 157     iban           => 'DE12500105170648489890',
 
 159     account_number => '648489890',
 
 161     bank_code      => 'G1235',
 
 162     depositor      => 'Test Vendor',
 
 163     vendornumber   => 'VEND1704',
 
 166   $ar_chart        = SL::DB::Manager::Chart->find_by( accno => '1400' ); # Forderungen
 
 167   $ap_chart        = SL::DB::Manager::Chart->find_by( accno => '1600' ); # Verbindlichkeiten
 
 168   $bank            = SL::DB::Manager::Chart->find_by( accno => '1200' ); # Bank
 
 169   $ar_amount_chart = SL::DB::Manager::Chart->find_by( accno => '8400' ); # Erlöse
 
 170   $ap_amount_chart = SL::DB::Manager::Chart->find_by( accno => '3400' ); # Wareneingang 19%
 
 174 sub test_ar_transaction {
 
 176   my $netamount = $params{amount} || 100;
 
 177   my $amount    = $::form->round_amount($netamount * 1.19,2);
 
 178   my $invoice   = SL::DB::Invoice->new(
 
 180       invnumber    => $params{invnumber} || undef, # let it use its own invnumber
 
 182       netamount    => $netamount,
 
 183       transdate    => $transdate1,
 
 184       taxincluded  => $params{taxincluded } || 0,
 
 185       customer_id  => $customer->id,
 
 186       taxzone_id   => $customer->taxzone_id,
 
 187       currency_id  => $currency_id,
 
 189       payment_id   => $params{payment_id} || undef,
 
 190       notes        => 'test_ar_transaction',
 
 192   $invoice->add_ar_amount_row(
 
 193     amount => $invoice->netamount,
 
 194     chart  => $ar_amount_chart,
 
 195     tax_id => $params{tax_id} || $tax->id,
 
 198   $invoice->create_ar_row(chart => $ar_chart);
 
 201   is($invoice->currency_id , $currency_id , 'currency_id has been saved');
 
 202   is($invoice->netamount   , $netamount   , 'ar amount has been converted');
 
 203   is($invoice->amount      , $amount      , 'ar amount has been converted');
 
 204   is($invoice->taxincluded , 0            , 'ar transaction doesn\'t have taxincluded');
 
 206   if ( $netamount == 100 ) {
 
 207     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');
 
 208     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');
 
 213 sub test_ap_transaction {
 
 215   my $testname = 'test_ap_transaction';
 
 218   my $amount    = $::form->round_amount($netamount * 1.19,2);
 
 219   my $invoice   = SL::DB::PurchaseInvoice->new(
 
 221     invnumber    => $params{invnumber} || $testname,
 
 223     netamount    => $netamount,
 
 224     transdate    => $transdate1,
 
 226     vendor_id    => $vendor->id,
 
 227     taxzone_id   => $vendor->taxzone_id,
 
 228     currency_id  => $currency_id,
 
 230     notes        => 'test_ap_transaction',
 
 232   $invoice->add_ap_amount_row(
 
 233     amount     => $invoice->netamount,
 
 234     chart      => $ap_amount_chart,
 
 235     tax_id     => $params{tax_id} || $tax_9->id,
 
 238   $invoice->create_ap_row(chart => $ap_chart);
 
 241   is($invoice->currency_id , $currency_id , "$testname: currency_id has been saved");
 
 242   is($invoice->netamount   , 100          , "$testname: ap amount has been converted");
 
 243   is($invoice->amount      , 119          , "$testname: ap amount has been converted");
 
 244   is($invoice->taxincluded , 0            , "$testname: ap transaction doesn\'t have taxincluded");
 
 246   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');
 
 247   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');
 
 256   my $testname = 'test1';
 
 258   $ar_transaction = test_ar_transaction(invnumber => 'salesinv1');
 
 260   my $bt = create_bank_transaction(record => $ar_transaction) or die "Couldn't create bank_transaction";
 
 262   $::form->{invoice_ids} = {
 
 263     $bt->id => [ $ar_transaction->id ]
 
 266   save_btcontroller_to_string();
 
 268   $ar_transaction->load;
 
 270   is($ar_transaction->paid   , '119.00000' , "$testname: salesinv1 was paid");
 
 271   is($ar_transaction->closed , 1           , "$testname: salesinv1 is closed");
 
 272   is($bt->invoice_amount     , '119.00000' , "$testname: bt invoice amount was assigned");
 
 276 sub test_skonto_exact {
 
 278   my $testname = 'test_skonto_exact';
 
 280   $ar_transaction = test_ar_transaction(invnumber => 'salesinv skonto',
 
 281                                         payment_id => $payment_terms->id,
 
 284   my $bt = create_bank_transaction(record        => $ar_transaction,
 
 285                                    bank_chart_id => $bank->id,
 
 286                                    amount        => $ar_transaction->amount_less_skonto
 
 287                                   ) or die "Couldn't create bank_transaction";
 
 289   $::form->{invoice_ids} = {
 
 290     $bt->id => [ $ar_transaction->id ]
 
 292   $::form->{invoice_skontos} = {
 
 293     $bt->id => [ 'with_skonto_pt' ]
 
 296   save_btcontroller_to_string();
 
 298   $ar_transaction->load;
 
 300   is($ar_transaction->paid   , '119.00000' , "$testname: salesinv skonto was paid");
 
 301   is($ar_transaction->closed , 1           , "$testname: salesinv skonto is closed");
 
 302   is($bt->invoice_amount     , '113.05000' , "$testname: bt invoice amount was assigned");
 
 308   my $testname = 'test_rollback_error';
 
 309   # without type with_free_skonto the helper function (Payment.pm) looks ugly but not
 
 312   $ar_transaction = test_ar_transaction(invnumber   => 'salesinv skonto',
 
 313                                         payment_id  => $payment_terms->id,
 
 315                                         amount      => 168.58 / 1.19,
 
 318   my $bt = create_bank_transaction(record        => $ar_transaction,
 
 319                                    bank_chart_id => $bank->id,
 
 321                                   ) or die "Couldn't create bank_transaction";
 
 322   $::form->{invoice_ids} = {
 
 323     $bt->id => [ $ar_transaction->id ]
 
 325   $::form->{invoice_skontos} = {
 
 326     $bt->id => [ 'with_skonto_pt' ]
 
 329   is($ar_transaction->paid   , '0' , "$testname: salesinv is not paid");
 
 331   # generate an error for testing rollback mechanism
 
 332   my $saved_skonto_sales_chart_id = $tax->skonto_sales_chart_id;
 
 333   $tax->skonto_sales_chart_id(undef);
 
 336   save_btcontroller_to_string();
 
 337   my @bt_errors = @{ $bt_controller->problems };
 
 338   is(substr($bt_errors[0]->{message},0,38), 'Kein Skontokonto für Steuerschlüssel 3', "$testname: Fehlermeldung ok");
 
 340   $tax->skonto_sales_chart_id($saved_skonto_sales_chart_id);
 
 343   $ar_transaction->load;
 
 345   is($ar_transaction->paid   , '0.00000' , "$testname: salesinv was not paid");
 
 346   is($bt->invoice_amount     , '0.00000' , "$testname: bt invoice amount was not assigned");
 
 350 sub test_two_invoices {
 
 352   my $testname = 'test_two_invoices';
 
 354   my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv_1');
 
 355   my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv_2');
 
 357   my $bt = create_bank_transaction(record        => $ar_transaction_1,
 
 358                                    amount        => ($ar_transaction_1->amount + $ar_transaction_2->amount),
 
 359                                    purpose       => "Rechnungen " . $ar_transaction_1->invnumber . " und " . $ar_transaction_2->invnumber,
 
 360                                    bank_chart_id => $bank->id,
 
 361                                   ) or die "Couldn't create bank_transaction";
 
 363   my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($ar_transaction_1);
 
 364   is($agreement, 16, "points for ar_transaction_1 in test_two_invoices ok");
 
 366   $::form->{invoice_ids} = {
 
 367     $bt->id => [ $ar_transaction_1->id, $ar_transaction_2->id ]
 
 370   save_btcontroller_to_string();
 
 372   $ar_transaction_1->load;
 
 373   $ar_transaction_2->load;
 
 376   is($ar_transaction_1->paid   , '119.00000' , "$testname: salesinv_1 wcsv_import_reportsas paid");
 
 377   is($ar_transaction_1->closed , 1           , "$testname: salesinv_1 is closed");
 
 378   is($ar_transaction_2->paid   , '119.00000' , "$testname: salesinv_2 was paid");
 
 379   is($ar_transaction_2->closed , 1           , "$testname: salesinv_2 is closed");
 
 380   is($bt->invoice_amount       , '238.00000' , "$testname: bt invoice amount was assigned");
 
 384 sub test_one_inv_and_two_invoices_with_skonto_exact {
 
 386   my $testname = 'test_two_invoices_with_skonto_exact';
 
 388   my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv 1 skonto',
 
 389                                              payment_id => $payment_terms->id,
 
 391   my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv 2 skonto',
 
 392                                              payment_id => $payment_terms->id,
 
 394   my $ar_transaction_3 = test_ar_transaction(invnumber => 'salesinv 3 no skonto');
 
 398   my $bt = create_bank_transaction(record        => $ar_transaction_1,
 
 399                                    bank_chart_id => $bank->id,
 
 400                                    amount        => $ar_transaction_1->amount_less_skonto * 2 + $ar_transaction_3->amount
 
 401                                   ) or die "Couldn't create bank_transaction";
 
 403   $::form->{invoice_ids} = {
 
 404     $bt->id => [ $ar_transaction_1->id, $ar_transaction_3->id, $ar_transaction_2->id]
 
 406   $::form->{invoice_skontos} = {
 
 407     $bt->id => [ 'with_skonto_pt', 'without_skonto', 'with_skonto_pt' ]
 
 410   save_btcontroller_to_string();
 
 412   $ar_transaction_1->load;
 
 413   $ar_transaction_2->load;
 
 414   $ar_transaction_3->load;
 
 415   my $skonto_1 = SL::DB::Manager::AccTransaction->find_by(trans_id => $ar_transaction_1->id, chart_id => 162);
 
 416   my $skonto_2 = SL::DB::Manager::AccTransaction->find_by(trans_id => $ar_transaction_2->id, chart_id => 162);
 
 418   is($skonto_1->amount   , '-5.95000' , "$testname: salesinv 1 skonto was booked");
 
 419   is($skonto_2->amount   , '-5.95000' , "$testname: salesinv 2 skonto was booked");
 
 420   is($ar_transaction_1->paid   , '119.00000' , "$testname: salesinv 1 was paid");
 
 421   is($ar_transaction_2->paid   , '119.00000' , "$testname: salesinv 2 was paid");
 
 422   is($ar_transaction_3->paid   , '119.00000' , "$testname: salesinv 3 was paid");
 
 423   is($ar_transaction_1->closed , 1           , "$testname: salesinv 1 skonto is closed");
 
 424   is($ar_transaction_2->closed , 1           , "$testname: salesinv 2 skonto is closed");
 
 425   is($ar_transaction_3->closed , 1           , "$testname: salesinv 2 skonto is closed");
 
 426   is($bt->invoice_amount     , '345.10000' , "$testname: bt invoice amount was assigned");
 
 430 sub test_overpayment {
 
 432   my $testname = 'test_overpayment';
 
 434   $ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid');
 
 437   my $bt = create_bank_transaction(record        => $ar_transaction,
 
 438                                    bank_chart_id => $bank->id,
 
 440                                   ) or die "Couldn't create bank_transaction";
 
 442   $::form->{invoice_ids} = {
 
 443     $bt->id => [ $ar_transaction->id ]
 
 446   save_btcontroller_to_string();
 
 448   $ar_transaction->load;
 
 451   is($ar_transaction->paid                     , '119.00000' , "$testname: 'salesinv overpaid' was not overpaid");
 
 452   is($bt->invoice_amount                       , '119.00000' , "$testname: bt invoice amount was not fully assigned with the overpaid amount");
 
 453 { local $TODO = 'this currently fails because closed ignores over-payments, see commit d90966c7';
 
 454   is($ar_transaction->closed                   , 0           , "$testname: 'salesinv overpaid' is open (via 'closed' method')");
 
 456   is($ar_transaction->open_amount == 0 ? 1 : 0 , 1           , "$testname: 'salesinv overpaid is closed (via amount-paid)");
 
 460 sub test_overpayment_with_partialpayment {
 
 462   # two payments on different days, 10 and 119. If there is only one invoice we
 
 463   # don't want it to be overpaid.
 
 464   my $testname = 'test_overpayment_with_partialpayment';
 
 466   $ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid partial');
 
 468   my $bt_1 = create_bank_transaction(record        => $ar_transaction,
 
 469                                      bank_chart_id => $bank->id,
 
 471                                     ) or die "Couldn't create bank_transaction";
 
 472   my $bt_2 = create_bank_transaction(record        => $ar_transaction,
 
 474                                      transdate     => DateTime->today->add(days => 5),
 
 475                                      bank_chart_id => $bank->id,
 
 476                                     ) or die "Couldn't create bank_transaction";
 
 478   $::form->{invoice_ids} = {
 
 479     $bt_1->id => [ $ar_transaction->id ]
 
 481   save_btcontroller_to_string();
 
 484   is($bt_1->invoice_amount ,  '10.00000' , "$testname: bt_1 invoice amount was fully assigned");
 
 485   $::form->{invoice_ids} = {
 
 486     $bt_2->id => [ $ar_transaction->id ]
 
 488   save_btcontroller_to_string();
 
 490   $ar_transaction->load;
 
 493   is($bt_1->invoice_amount ,  '10.00000' , "$testname: bt_1 invoice amount was fully assigned");
 
 494   is($ar_transaction->paid , '119.00000' , "$testname: 'salesinv overpaid partial' was not overpaid");
 
 495   is($bt_2->invoice_amount , '109.00000' , "$testname: bt_2 invoice amount was partly assigned");
 
 499 sub test_partial_payment {
 
 501   my $testname = 'test_partial_payment';
 
 503   $ar_transaction = test_ar_transaction(invnumber => 'salesinv partial payment');
 
 506   my $bt = create_bank_transaction(record        => $ar_transaction,
 
 507                                    bank_chart_id => $bank->id,
 
 509                                   ) or die "Couldn't create bank_transaction";
 
 511   $::form->{invoice_ids} = {
 
 512     $bt->id => [ $ar_transaction->id ]
 
 515   save_btcontroller_to_string();
 
 517   $ar_transaction->load;
 
 520   is($ar_transaction->paid , '100.00000' , "$testname: 'salesinv partial payment' was partially paid");
 
 521   is($bt->invoice_amount   , '100.00000' , "$testname: bt invoice amount was assigned partially paid amount");
 
 525 sub test_full_workflow_ar_multiple_inv_skonto_reconciliate_and_undo {
 
 527   my $testname = 'test_partial_payment';
 
 529   $ar_transaction = test_ar_transaction(invnumber => 'salesinv partial payment two');
 
 530   my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv 2 22d2', amount => 22);
 
 532   # amount 299.29 > 119
 
 533   my $bt = create_bank_transaction(record        => $ar_transaction,
 
 534                                    bank_chart_id => $bank->id,
 
 536                                   ) or die "Couldn't create bank_transaction";
 
 538   $::form->{invoice_ids} = {
 
 539     $bt->id => [ $ar_transaction->id ]
 
 542   save_btcontroller_to_string();
 
 544   $ar_transaction->load;
 
 547   is($ar_transaction->paid , '119.00000' , "$testname: 'salesinv partial payment' was fully paid");
 
 548   is($bt->invoice_amount   , '119.00000' , "$testname: bt invoice amount was assigned partially paid amount");
 
 549   is($bt->amount           , '299.29000' , "$testname: bt amount is stil there");
 
 550   # next invoice, same bank transaction
 
 551   $::form->{invoice_ids} = {
 
 552     $bt->id => [ $ar_transaction_2->id ]
 
 555   save_btcontroller_to_string();
 
 557   $ar_transaction_2->load;
 
 559   is($ar_transaction_2->paid , '26.18000' , "$testname: 'salesinv partial payment' was fully paid");
 
 560   is($bt->invoice_amount   , '145.18000' , "$testname: bt invoice amount was assigned partially paid amount");
 
 561   is($bt->amount           , '299.29000' , "$testname: bt amount is stil there");
 
 562   is(scalar @{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )}, 4, "$testname 4 acc_trans entries created");
 
 564   #  now check all 4 entries done so far and save paid acc_trans_ids for later use with reconcile
 
 565   foreach my $acc_trans_id_entry (@{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )}) {
 
 566     isnt($acc_trans_id_entry->ar_id, undef, "$testname: bt linked with acc_trans and trans_id set");
 
 567     my $rl = SL::DB::Manager::RecordLink->get_all(where => [ from_id => $bt->id, from_table => 'bank_transactions', to_id => $acc_trans_id_entry->ar_id ]);
 
 568     is (ref $rl->[0], 'SL::DB::RecordLink', "$testname record link created");
 
 569     my $acc_trans = SL::DB::Manager::AccTransaction->get_all(where => [acc_trans_id => $acc_trans_id_entry->acc_trans_id]);
 
 570     foreach my $entry (@{ $acc_trans }) {
 
 571       like(abs($entry->amount), qr/(119|26.18)/, "$testname: abs amount correct");
 
 572       like($entry->chart_link, qr/(paid|AR)/, "$testname chart_link correct");
 
 573       push @{ $::form->{bb_ids} }, $entry->acc_trans_id if $entry->chart_link =~ m/paid/;
 
 576   # great we need one last booking to clear the whole bank transaction - we include skonto
 
 577   my $ar_transaction_skonto = test_ar_transaction(invnumber  => 'salesinv skonto last case',
 
 578                                                   payment_id => $payment_terms->id,
 
 582   $::form->{invoice_ids} = {
 
 583     $bt->id => [ $ar_transaction_skonto->id ]
 
 585   $::form->{invoice_skontos} = {
 
 586     $bt->id => [ 'with_skonto_pt' ]
 
 589   save_btcontroller_to_string();
 
 591   $ar_transaction_skonto->load;
 
 593   is($ar_transaction_skonto->paid , '162.22000' , "$testname: 'salesinv skonto fully paid");
 
 594   is($bt->invoice_amount   , '299.29000' , "$testname: bt invoice amount was assigned partially paid amount");
 
 595   is($bt->amount           , '299.29000' , "$testname: bt amount is stil there");
 
 596   is(scalar @{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )},
 
 597        7, "$testname 7 acc_trans entries created");
 
 599   # same loop as above, but only for the 3rd ar_id
 
 600   foreach my $acc_trans_id_entry (@{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [ar_id => $ar_transaction_skonto->id ] )}) {
 
 601     isnt($acc_trans_id_entry->ar_id, '', "$testname: bt linked with acc_trans and trans_id set");
 
 602     my $rl = SL::DB::Manager::RecordLink->get_all(where => [ from_id => $bt->id, from_table => 'bank_transactions', to_id => $acc_trans_id_entry->ar_id ]);
 
 603     is (ref $rl->[0], 'SL::DB::RecordLink', "$testname record link created");
 
 604     my $acc_trans = SL::DB::Manager::AccTransaction->get_all(where => [acc_trans_id => $acc_trans_id_entry->acc_trans_id]);
 
 605     foreach my $entry (@{ $acc_trans }) {
 
 606       like($entry->chart_link, qr/(paid|AR)/, "$testname chart_link correct");
 
 607       is ($entry->amount, '162.22000', "$testname full amont") if $entry->chart_link eq 'AR'; # full amount
 
 608       like(abs($entry->amount), qr/(154.11|8.11)/, "$testname: abs amount correct") if $entry->chart_link =~ m/paid/;
 
 609       push @{ $::form->{bb_ids} }, $entry->acc_trans_id if ($entry->chart_link =~ m/paid/ && $entry->amount == -154.11);
 
 612   # done, now reconciliate all bookings
 
 613   $::form->{bt_ids} = [ $bt->id ];
 
 614   my $rec_controller = SL::Controller::Reconciliation->new;
 
 615   my @errors = $rec_controller->_get_elements_and_validate;
 
 617   is (scalar @errors, 0, "$testname unsuccesfull reconciliation with error: " . Dumper(@errors));
 
 618   $rec_controller->_reconcile;
 
 621   # and check the cleared state of bt and the acc_transactions
 
 622   is($bt->cleared, '1' , "$testname: bt cleared");
 
 623   foreach (@{ $::form->{bb_ids} }) {
 
 624     my $acc_trans = SL::DB::Manager::AccTransaction->find_by(acc_trans_id => $_);
 
 625     is($acc_trans->cleared, '1' , "$testname: acc_trans entry cleared");
 
 627   # now, this was a really bad idea and in general a major mistake. better undo and redo the whole bank transactions
 
 629   $::form->{ids} = [ $bt->id ];
 
 630   $bt_controller = SL::Controller::BankTransaction->new;
 
 631   $bt_controller->action_unlink_bank_transaction('testcase' => 1);
 
 635   # and check the cleared state of bt and the acc_transactions
 
 636   is($bt->cleared, '0' , "$testname: bt undo cleared");
 
 637   is($bt->invoice_amount, '0.00000' , "$testname: bt undo invoice amount");
 
 638   foreach (@{ $::form->{bb_ids} }) {
 
 639     my $acc_trans = SL::DB::Manager::AccTransaction->find_by(acc_trans_id => $_);
 
 640     is($acc_trans, undef , "$testname: cleared acc_trans entry completely removed");
 
 642   # this was for data integrity for reconcile, now all the other options
 
 643   is(scalar @{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )},
 
 644        0, "$testname 7 acc_trans entries deleted");
 
 645   my $rl = SL::DB::Manager::RecordLink->get_all(where => [ from_id => $bt->id, from_table => 'bank_transactions' ]);
 
 646   is (ref $rl->[0], '', "$testname record link removed");
 
 647   # double safety and check ar.paid
 
 648   # load all three invoices and check for paid-link via acc_trans and paid in general
 
 650   $ar_transaction->load;
 
 651   $ar_transaction_2->load;
 
 652   $ar_transaction_skonto->load;
 
 654   is(scalar @{ SL::DB::Manager::AccTransaction->get_all(
 
 655      where => [ trans_id => $ar_transaction->id, chart_link => { like => '%paid%' } ])},
 
 656      0, "$testname no more paid entries in acc_trans for ar_transaction");
 
 657   is(scalar @{ SL::DB::Manager::AccTransaction->get_all(
 
 658      where => [ trans_id => $ar_transaction_2->id, chart_link => { like => '%paid%' } ])},
 
 659      0, "$testname no more paid entries in acc_trans for ar_transaction_2");
 
 660   is(scalar @{ SL::DB::Manager::AccTransaction->get_all(
 
 661      where => [ trans_id => $ar_transaction_skonto->id, chart_link => { like => '%paid%' } ])},
 
 662      0, "$testname no more paid entries in acc_trans for ar_transaction_skonto");
 
 664   is($ar_transaction->paid , '0.00000' , "$testname: 'salesinv fully unpaid");
 
 665   is($ar_transaction_2->paid , '0.00000' , "$testname: 'salesinv 2 fully unpaid");
 
 666   is($ar_transaction_skonto->paid , '0.00000' , "$testname: 'salesinv skonto fully unpaid");
 
 668   # whew. w(h)a(n)t a whole lotta test
 
 672 sub test_credit_note {
 
 674   my $testname = 'test_credit_note';
 
 676   my $part1 = new_part(   partnumber => 'T4254')->save;
 
 677   my $part2 = new_service(partnumber => 'Serv1')->save;
 
 678   my $credit_note = create_credit_note(
 
 680     customer     => $customer,
 
 682     invoiceitems => [ create_invoice_item(part => $part1, qty =>  3, sellprice => 70),
 
 683                       create_invoice_item(part => $part2, qty => 10, sellprice => 50),
 
 686   my $bt            = create_bank_transaction(record        => $credit_note,
 
 687                                                                 amount        => $credit_note->amount,
 
 688                                                                 bank_chart_id => $bank->id,
 
 689                                                                 transdate     => DateTime->today->add(days => 10),
 
 691   my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($credit_note);
 
 692   is($agreement, 13, "points for credit note ok");
 
 693   is($rule_matches, 'remote_account_number(3) exact_amount(4) wrong_sign(-1) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus14(2) ', "rules_matches for credit note ok");
 
 695   $::form->{invoice_ids} = {
 
 696     $bt->id => [ $credit_note->id ]
 
 699   save_btcontroller_to_string();
 
 703   is($credit_note->amount   , '-844.90000', "$testname: amount ok");
 
 704   is($credit_note->netamount, '-710.00000', "$testname: netamount ok");
 
 705   is($credit_note->paid     , '-844.90000', "$testname: paid ok");
 
 708 sub test_neg_ap_transaction {
 
 710   my $testname = 'test_neg_ap_transaction' . $params{invoice} ? ' invoice booking' : ' credit booking';
 
 712   my $amount    = $::form->round_amount($netamount * 1.19,2);
 
 713   my $invoice   = SL::DB::PurchaseInvoice->new(
 
 714     invoice      => $params{invoice} // 0,
 
 715     invnumber    => $params{invnumber} || 'test_neg_ap_transaction',
 
 717     netamount    => $netamount,
 
 718     transdate    => $transdate1,
 
 720     vendor_id    => $vendor->id,
 
 721     taxzone_id   => $vendor->taxzone_id,
 
 722     currency_id  => $currency_id,
 
 724     notes        => 'test_neg_ap_transaction',
 
 726   $invoice->add_ap_amount_row(
 
 727     amount     => $invoice->netamount,
 
 728     chart      => $ap_amount_chart,
 
 729     tax_id     => $tax_9->id,
 
 732   $invoice->create_ap_row(chart => $ap_chart);
 
 735   is($invoice->netamount, -20  , "$testname: netamount ok");
 
 736   is($invoice->amount   , -23.8, "$testname: amount ok");
 
 738   my $bt            = create_bank_transaction(record        => $invoice,
 
 739                                               amount        => $invoice->amount,
 
 740                                               bank_chart_id => $bank->id,
 
 741                                               transdate     => DateTime->today->add(days => 10),
 
 744   my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($invoice);
 
 745   is($agreement, 15, "points for negative ap transaction ok");
 
 747   $::form->{invoice_ids} = {
 
 748     $bt->id => [ $invoice->id ]
 
 751   save_btcontroller_to_string();
 
 756   is($invoice->amount   , '-23.80000', "$testname: amount ok");
 
 757   is($invoice->netamount, '-20.00000', "$testname: netamount ok");
 
 758   is($invoice->paid     , '-23.80000', "$testname: paid ok");
 
 759   is($bt->invoice_amount, '23.80000', "$testname: bt invoice amount for ap was assigned");
 
 760   is($bt->amount,         '23.80000', "$testname: bt  amount for ap was assigned");
 
 764 sub test_two_neg_ap_transaction {
 
 765   my $testname='test_two_neg_ap_transaction';
 
 767   my $amount    = $::form->round_amount($netamount * 1.19,2);
 
 768   my $invoice   = SL::DB::PurchaseInvoice->new(
 
 770     invnumber    => 'test_neg_ap_transaction',
 
 772     netamount    => $netamount,
 
 773     transdate    => $transdate1,
 
 775     vendor_id    => $vendor->id,
 
 776     taxzone_id   => $vendor->taxzone_id,
 
 777     currency_id  => $currency_id,
 
 779     notes        => 'test_neg_ap_transaction',
 
 781   $invoice->add_ap_amount_row(
 
 782     amount     => $invoice->netamount,
 
 783     chart      => $ap_amount_chart,
 
 784     tax_id     => $tax_9->id,
 
 787   $invoice->create_ap_row(chart => $ap_chart);
 
 790   is($invoice->netamount, -20  , "$testname: netamount ok");
 
 791   is($invoice->amount   , -23.8, "$testname: amount ok");
 
 793   my $netamount_two = -1.14;
 
 794   my $amount_two    = $::form->round_amount($netamount_two * 1.19,2);
 
 795   my $invoice_two   = SL::DB::PurchaseInvoice->new(
 
 797     invnumber    => 'test_neg_ap_transaction_two',
 
 798     amount       => $amount_two,
 
 799     netamount    => $netamount_two,
 
 800     transdate    => $transdate1,
 
 802     vendor_id    => $vendor->id,
 
 803     taxzone_id   => $vendor->taxzone_id,
 
 804     currency_id  => $currency_id,
 
 806     notes        => 'test_neg_ap_transaction_two',
 
 808   $invoice_two->add_ap_amount_row(
 
 809     amount     => $invoice_two->netamount,
 
 810     chart      => $ap_amount_chart,
 
 811     tax_id     => $tax_9->id,
 
 814   $invoice_two->create_ap_row(chart => $ap_chart);
 
 817   is($invoice_two->netamount, -1.14  , "$testname: netamount ok");
 
 818   is($invoice_two->amount   , -1.36, "$testname: amount ok");
 
 821   my $bt            = create_bank_transaction(record        => $invoice_two,
 
 822                                               amount        => $invoice_two->amount + $invoice->amount,
 
 823                                               bank_chart_id => $bank->id,
 
 824                                               transdate     => DateTime->today->add(days => 10),
 
 826   # my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($invoice_two);
 
 827   # is($agreement, 15, "points for negative ap transaction ok");
 
 829   $::form->{invoice_ids} = {
 
 830     $bt->id => [ $invoice->id, $invoice_two->id ]
 
 833   save_btcontroller_to_string();
 
 839   is($invoice->amount   , '-23.80000', "$testname: first inv amount ok");
 
 840   is($invoice->netamount, '-20.00000', "$testname: first inv netamount ok");
 
 841   is($invoice->paid     , '-23.80000', "$testname: first inv paid ok");
 
 842   is($invoice_two->amount   , '-1.36000', "$testname: second inv amount ok");
 
 843   is($invoice_two->netamount, '-1.14000', "$testname: second inv netamount ok");
 
 844   is($invoice_two->paid     , '-1.36000', "$testname: second inv paid ok");
 
 845   is($bt->invoice_amount, '25.16000', "$testname: bt invoice amount for both invoices were assigned");
 
 848   return ($invoice, $invoice_two);
 
 851 sub test_ap_payment_transaction {
 
 853   my $testname = 'test_ap_payment_transaction';
 
 855   my $amount    = $::form->round_amount($netamount * 1.19,2);
 
 856   my $invoice   = SL::DB::PurchaseInvoice->new(
 
 858     invnumber    => $params{invnumber} || $testname,
 
 860     netamount    => $netamount,
 
 861     transdate    => $transdate1,
 
 863     vendor_id    => $vendor->id,
 
 864     taxzone_id   => $vendor->taxzone_id,
 
 865     currency_id  => $currency_id,
 
 869   $invoice->add_ap_amount_row(
 
 870     amount     => $invoice->netamount,
 
 871     chart      => $ap_amount_chart,
 
 872     tax_id     => $tax_9->id,
 
 875   $invoice->create_ap_row(chart => $ap_chart);
 
 878   is($invoice->netamount, 115  , "$testname: netamount ok");
 
 879   is($invoice->amount   , 136.85, "$testname: amount ok");
 
 881   my $bt            = create_bank_transaction(record        => $invoice,
 
 882                                               amount        => $invoice->amount,
 
 883                                               bank_chart_id => $bank->id,
 
 884                                               transdate     => DateTime->today->add(days => 10),
 
 886   $::form->{invoice_ids} = {
 
 887     $bt->id => [ $invoice->id ]
 
 890   save_btcontroller_to_string();
 
 895   is($invoice->amount   , '136.85000', "$testname: amount ok");
 
 896   is($invoice->netamount, '115.00000', "$testname: netamount ok");
 
 897   is($bt->amount,         '-136.85000', "$testname: bt amount ok");
 
 898   is($invoice->paid     , '136.85000', "$testname: paid ok");
 
 899   is($bt->invoice_amount, '-136.85000', "$testname: bt invoice amount for ap was assigned");
 
 904 sub test_ap_payment_part_transaction {
 
 906   my $testname = 'test_ap_payment_p_transaction';
 
 908   my $amount    = $::form->round_amount($netamount * 1.19,2);
 
 909   my $invoice   = SL::DB::PurchaseInvoice->new(
 
 911     invnumber    => $params{invnumber} || $testname,
 
 913     netamount    => $netamount,
 
 914     transdate    => $transdate1,
 
 916     vendor_id    => $vendor->id,
 
 917     taxzone_id   => $vendor->taxzone_id,
 
 918     currency_id  => $currency_id,
 
 922   $invoice->add_ap_amount_row(
 
 923     amount     => $invoice->netamount,
 
 924     chart      => $ap_amount_chart,
 
 925     tax_id     => $tax_9->id,
 
 928   $invoice->create_ap_row(chart => $ap_chart);
 
 931   is($invoice->netamount, 115  , "$testname: netamount ok");
 
 932   is($invoice->amount   , 136.85, "$testname: amount ok");
 
 934   my $bt            = create_bank_transaction(record        => $invoice,
 
 935                                               amount        => $invoice->amount-100,
 
 936                                               bank_chart_id => $bank->id,
 
 937                                               transdate     => DateTime->today->add(days => 10),
 
 939   $::form->{invoice_ids} = {
 
 940     $bt->id => [ $invoice->id ]
 
 943   save_btcontroller_to_string();
 
 948   is($invoice->amount   , '136.85000', "$testname: amount ok");
 
 949   is($invoice->netamount, '115.00000', "$testname: netamount ok");
 
 950   is($bt->amount,         '-36.85000', "$testname: bt amount ok");
 
 951   is($invoice->paid     ,  '36.85000', "$testname: paid ok");
 
 952   is($bt->invoice_amount, '-36.85000', "$testname: bt invoice amount for ap was assigned");
 
 954   my $bt2           = create_bank_transaction(record        => $invoice,
 
 956                                               bank_chart_id => $bank->id,
 
 957                                               transdate     => DateTime->today->add(days => 10),
 
 959   $::form->{invoice_ids} = {
 
 960     $bt2->id => [ $invoice->id ]
 
 963   save_btcontroller_to_string();
 
 967   is($invoice->amount   , '136.85000', "$testname: amount ok");
 
 968   is($invoice->netamount, '115.00000', "$testname: netamount ok");
 
 969   is($bt2->amount,        '-100.00000',"$testname: bt amount ok");
 
 970   is($invoice->paid     , '136.85000', "$testname: paid ok");
 
 971   is($bt2->invoice_amount,'-100.00000', "$testname: bt invoice amount for ap was assigned");
 
 976 sub test_neg_sales_invoice {
 
 978   my $testname = 'test_neg_sales_invoice';
 
 980   my $part1 = new_part(   partnumber => 'Funkenhaube öhm')->save;
 
 981   my $part2 = new_service(partnumber => 'Service-Pauschale Pasch!')->save;
 
 983   my $neg_sales_inv = create_sales_invoice(
 
 984     invnumber    => '20172201',
 
 985     customer     => $customer,
 
 987     invoiceitems => [ create_invoice_item(part => $part1, qty =>  3, sellprice => 70),
 
 988                       create_invoice_item(part => $part2, qty => 10, sellprice => -50),
 
 991   my $bt            = create_bank_transaction(record        => $neg_sales_inv,
 
 992                                                                 amount        => $neg_sales_inv->amount,
 
 993                                                                 bank_chart_id => $bank->id,
 
 994                                                                 transdate     => DateTime->today,
 
 996   $::form->{invoice_ids} = {
 
 997     $bt->id => [ $neg_sales_inv->id ]
 
1000   save_btcontroller_to_string();
 
1002   $neg_sales_inv->load;
 
1004   is($neg_sales_inv->amount   , '-345.10000', "$testname: amount ok");
 
1005   is($neg_sales_inv->netamount, '-290.00000', "$testname: netamount ok");
 
1006   is($neg_sales_inv->paid     , '-345.10000', "$testname: paid ok");
 
1007   is($bt->amount              , '-345.10000', "$testname: bt amount ok");
 
1008   is($bt->invoice_amount      , '-345.10000', "$testname: bt invoice_amount ok");
 
1013   my $testname = 'test_bt_rule1';
 
1015   $ar_transaction = test_ar_transaction(invnumber => 'bt_rule1');
 
1017   my $bt = create_bank_transaction(record => $ar_transaction) or die "Couldn't create bank_transaction";
 
1019   $ar_transaction->load;
 
1021   is($ar_transaction->paid   , '0.00000' , "$testname: not paid");
 
1022   is($bt->invoice_amount     , '0.00000' , "$testname: bt invoice amount was not assigned");
 
1024   my $bt_controller = SL::Controller::BankTransaction->new;
 
1025   my ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
 
1027   is(scalar(@$bt_transactions)         , 1  , "$testname: one bank_transaction");
 
1028   is($bt_transactions->[0]->{agreement}, 20 , "$testname: agreement == 20");
 
1029   my $match = join ( ' ',@{$bt_transactions->[0]->{rule_matches}});
 
1030   #print "rule_matches='".$match."'\n";
 
1032      "remote_account_number(3) exact_amount(4) own_invoice_in_purpose(5) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus0(3) ",
 
1033      "$testname: rule_matches ok");
 
1034   $bt->invoice_amount($bt->amount);
 
1036   is($bt->invoice_amount     , '119.00000' , "$testname: bt invoice amount now set");
 
1039 sub test_sepa_export {
 
1041   my $testname = 'test_sepa_export';
 
1043   $ar_transaction = test_ar_transaction(invnumber => 'sepa1');
 
1045   my $bt  = create_bank_transaction(record => $ar_transaction) or die "Couldn't create bank_transaction";
 
1046   my $se  = create_sepa_export();
 
1047   my $sei = create_sepa_export_item(
 
1048     chart_id       => $bank->id,
 
1049     ar_id          => $ar_transaction->id,
 
1050     sepa_export_id => $se->id,
 
1051     vc_iban        => $customer->iban,
 
1052     vc_bic         => $customer->bic,
 
1053     vc_mandator_id => $customer->mandator_id,
 
1054     vc_depositor   => $customer->depositor,
 
1055     amount         => $ar_transaction->amount,
 
1057   require SL::SEPA::XML;
 
1058   my $sepa_xml   = SL::SEPA::XML->new('company'     => $customer->name,
 
1059                                       'creditor_id' => "id",
 
1060                                       'src_charset' => 'UTF-8',
 
1061                                       'message_id'  => "test",
 
1065   is($sepa_xml->{company}    , 'Test Customer OLE S.L. Ardbaerg AB');
 
1067   $ar_transaction->load;
 
1070   is($ar_transaction->paid   , '0.00000' , "$testname: sepa1 not paid");
 
1071   is($bt->invoice_amount     , '0.00000' , "$testname: bt invoice amount was not assigned");
 
1072   is($bt->amount             , '119.00000' , "$testname: bt amount ok");
 
1073   is($sei->amount            , '119.00000' , "$testname: sepa export amount ok");
 
1075   my $bt_controller = SL::Controller::BankTransaction->new;
 
1076   my ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
 
1078   is(scalar(@$bt_transactions)         , 1  , "$testname: one bank_transaction");
 
1079   is($bt_transactions->[0]->{agreement}, 25 , "$testname: agreement == 25");
 
1080   my $match = join ( ' ',@{$bt_transactions->[0]->{rule_matches}});
 
1082      "remote_account_number(3) exact_amount(4) own_invoice_in_purpose(5) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus0(3) sepa_export_item(5) ",
 
1083      "$testname: rule_matches ok");
 
1086 sub test_two_banktransactions {
 
1088   my $testname = 'two_banktransactions';
 
1090   my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv10000' , amount => 2912.00 );
 
1091   my $bt1 = create_bank_transaction(record        => $ar_transaction_1,
 
1092                                     amount        => $ar_transaction_1->amount,
 
1093                                     purpose       => "Rechnung10000 beinahe",
 
1094                                     bank_chart_id => $bank->id,
 
1095                                   ) or die "Couldn't create bank_transaction";
 
1097   my $bt2 = create_bank_transaction(record        => $ar_transaction_1,
 
1098                                     amount        => $ar_transaction_1->amount + 0.01,
 
1099                                     purpose       => "sicher salesinv20000 vielleicht",
 
1100                                     bank_chart_id => $bank->id,
 
1101                                   ) or die "Couldn't create bank_transaction";
 
1103   my ($agreement1, $rule_matches1) = $bt1->get_agreement_with_invoice($ar_transaction_1);
 
1104   is($agreement1, 19, "bt1 19 points for ar_transaction_1 in $testname ok");
 
1105   #print "rule_matches1=".$rule_matches1."\n";
 
1107      "remote_account_number(3) exact_amount(4) own_invnumber_in_purpose(4) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus0(3) ",
 
1108      "$testname: rule_matches ok");
 
1109   my ($agreement2, $rule_matches2) = $bt2->get_agreement_with_invoice($ar_transaction_1);
 
1110   is($agreement2, 11, "bt2 11 points for ar_transaction_1 in $testname ok");
 
1112      "remote_account_number(3) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus0(3) ",
 
1113      "$testname: rule_matches ok");
 
1115   my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv20000' , amount => 2912.01 );
 
1116   my $ar_transaction_3 = test_ar_transaction(invnumber => 'zweitemit10000', amount => 2912.00 );
 
1117      ($agreement1, $rule_matches1) = $bt1->get_agreement_with_invoice($ar_transaction_2);
 
1119   is($agreement1, 11, "bt1 11 points for ar_transaction_2 in $testname ok");
 
1121      ($agreement2, $rule_matches2) = $bt2->get_agreement_with_invoice($ar_transaction_2);
 
1122   is($agreement2, 20, "bt2 20 points for ar_transaction_2 in $testname ok");
 
1124      ($agreement2, $rule_matches2) = $bt2->get_agreement_with_invoice($ar_transaction_1);
 
1125   is($agreement2, 11, "bt2 11 points for ar_transaction_1 in $testname ok");
 
1127   my $bt3 = create_bank_transaction(record        => $ar_transaction_3,
 
1128                                     amount        => $ar_transaction_3->amount,
 
1129                                     purpose       => "sicher Rechnung10000 vielleicht",
 
1130                                     bank_chart_id => $bank->id,
 
1131                                   ) or die "Couldn't create bank_transaction";
 
1133   my ($agreement3, $rule_matches3) = $bt3->get_agreement_with_invoice($ar_transaction_3);
 
1134   is($agreement3, 19, "bt3 19 points for ar_transaction_3 in $testname ok");
 
1137   $ar_transaction_2->delete;
 
1139   #nun sollten zwei gleichwertige Rechnungen $ar_transaction_1 und $ar_transaction_3 für $bt1 gefunden werden
 
1140   #aber es darf keine Proposals geben mit mehreren Rechnungen
 
1141   my $bt_controller = SL::Controller::BankTransaction->new;
 
1142   my ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
 
1144   is(scalar(@$bt_transactions)   , 2  , "$testname: two bank_transaction");
 
1145   is(scalar(@$proposals)         , 0  , "$testname: no proposals");
 
1147   $ar_transaction_3->delete;
 
1149   # Jetzt gibt es zwei Kontobewegungen mit gleichen Punkten für eine Rechnung.
 
1150   # hier darf es auch keine Proposals geben
 
1152   ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
 
1154   is(scalar(@$bt_transactions)   , 2  , "$testname: two bank_transaction");
 
1155   # odyn testfall - anforderungen so (noch) nicht in kivi
 
1156   # is(scalar(@$proposals)         , 0  , "$testname: no proposals");
 
1158   # Jetzt gibt es zwei Kontobewegungen für eine Rechnung.
 
1159   # eine Bewegung bekommt mehr Punkte
 
1160   # hier darf es auch keine Proposals geben
 
1161   $bt3->update_attributes( purpose => "fuer Rechnung salesinv10000");
 
1163   ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
 
1165   is(scalar(@$bt_transactions)   , 2  , "$testname: two bank_transaction");
 
1166   # odyn testfall - anforderungen so (noch) nicht in kivi
 
1167   # is(scalar(@$proposals)         , 1  , "$testname: one proposal");
 
1172   my $testname = 'closedto';
 
1174   my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv10000' , amount => 2912.00 );
 
1175   my $bt1 = create_bank_transaction(record        => $ar_transaction_1,
 
1176                                     amount        => $ar_transaction_1->amount,
 
1177                                     purpose       => "Rechnung10000 beinahe",
 
1178                                     bank_chart_id => $bank->id,
 
1179                                   ) or die "Couldn't create bank_transaction";
 
1181   $bt1->valutadate(DateTime->new(year => 2019, month => 12, day => 30));
 
1184   is($bt1->closed_period, 0, "$testname undefined closedto");
 
1186   my $defaults = SL::DB::Manager::Default->get_all(limit => 1)->[0];
 
1187   $defaults->closedto(DateTime->new(year => 2019, month => 12, day => 31));
 
1189   $::instance_conf->reload->data;
 
1191   is($bt1->closed_period, 1, "$testname defined and next date closedto");
 
1193   $bt1->valutadate(DateTime->new(year => 2020, month => 1, day => 1));
 
1197   is($bt1->closed_period, 0, "$testname defined closedto and next date valuta");
 
1198   $defaults->closedto(undef);