kivitendo/t/bank/bank_transactions.t @ b6f8a6b0
5027341d | Jan Büren | use Test::More tests => 289;
|
||
691a1293 | Geoffrey Richardson | |||
use strict;
|
||||
use lib 't';
|
||||
use utf8;
|
||||
use Carp;
|
||||
use Support::TestSetup;
|
||||
use Test::Exception;
|
||||
use List::Util qw(sum);
|
||||
b75c6cbb | Jan Büren | use SL::DB::AccTransaction;
|
||
665741c4 | Jan Büren | use SL::DB::BankTransactionAccTrans;
|
||
691a1293 | Geoffrey Richardson | use SL::DB::Buchungsgruppe;
|
||
use SL::DB::Currency;
|
||||
use SL::DB::Customer;
|
||||
25230b8c | Jan Büren | use SL::DB::Default;
|
||
691a1293 | Geoffrey Richardson | use SL::DB::Vendor;
|
||
use SL::DB::Invoice;
|
||||
use SL::DB::Unit;
|
||||
280b772d | Geoffrey Richardson | use SL::DB::Part;
|
||
691a1293 | Geoffrey Richardson | use SL::DB::TaxZone;
|
||
use SL::DB::BankAccount;
|
||||
use SL::DB::PaymentTerm;
|
||||
use SL::DB::PurchaseInvoice;
|
||||
use SL::DB::BankTransaction;
|
||||
use SL::Controller::BankTransaction;
|
||||
b11c7ad5 | Jan Büren | use SL::Controller::Reconciliation;
|
||
63410aa6 | Sven Schöling | use SL::Dev::ALL qw(:ALL);
|
||
691a1293 | Geoffrey Richardson | use Data::Dumper;
|
||
f942d1f6 | Martin Helmling | my ($customer, $vendor, $currency_id, $unit, $tax, $tax0, $tax7, $tax_9, $payment_terms, $bank_account);
|
||
6378f59b | Jan Büren | my ($currency);
|
||
691a1293 | Geoffrey Richardson | my ($ar_chart,$bank,$ar_amount_chart, $ap_chart, $ap_amount_chart);
|
||
my ($ar_transaction, $ap_transaction);
|
||||
445ac52b | Geoffrey Richardson | my ($dt, $dt_5, $dt_10, $year);
|
||
6378f59b | Jan Büren | |||
691a1293 | Geoffrey Richardson | sub clear_up {
|
||
a81f877a | Sven Schöling | |||
665741c4 | Jan Büren | SL::DB::Manager::BankTransactionAccTrans->delete_all(all => 1);
|
||
691a1293 | Geoffrey Richardson | SL::DB::Manager::BankTransaction->delete_all(all => 1);
|
||
SL::DB::Manager::InvoiceItem->delete_all(all => 1);
|
||||
SL::DB::Manager::InvoiceItem->delete_all(all => 1);
|
||||
SL::DB::Manager::Invoice->delete_all(all => 1);
|
||||
SL::DB::Manager::PurchaseInvoice->delete_all(all => 1);
|
||||
280b772d | Geoffrey Richardson | SL::DB::Manager::Part->delete_all(all => 1);
|
||
691a1293 | Geoffrey Richardson | SL::DB::Manager::Customer->delete_all(all => 1);
|
||
SL::DB::Manager::Vendor->delete_all(all => 1);
|
||||
503fabbf | Martin Helmling | SL::DB::Manager::SepaExportItem->delete_all(all => 1);
|
||
SL::DB::Manager::SepaExport->delete_all(all => 1);
|
||||
691a1293 | Geoffrey Richardson | SL::DB::Manager::BankAccount->delete_all(all => 1);
|
||
SL::DB::Manager::PaymentTerm->delete_all(all => 1);
|
||||
SL::DB::Manager::Currency->delete_all(where => [ name => 'CUR' ]);
|
||||
5027341d | Jan Büren | # SL::DB::Manager::Default->delete_all(all => 1);
|
||
691a1293 | Geoffrey Richardson | };
|
||
f942d1f6 | Martin Helmling | my $bt_controller;
|
||
b8741ec3 | Martin Helmling | sub save_btcontroller_to_string {
|
||
my $output;
|
||||
open(my $outputFH, '>', \$output) or die;
|
||||
my $oldFH = select $outputFH;
|
||||
f942d1f6 | Martin Helmling | $bt_controller = SL::Controller::BankTransaction->new;
|
||
b8741ec3 | Martin Helmling | $bt_controller->action_save_invoices;
|
||
select $oldFH;
|
||||
close $outputFH;
|
||||
return $output;
|
||||
}
|
||||
691a1293 | Geoffrey Richardson | |||
# starting test:
|
||||
Support::TestSetup::login();
|
||||
b8741ec3 | Martin Helmling | clear_up();
|
||
691a1293 | Geoffrey Richardson | reset_state(); # initialise customers/vendors/bank/currency/...
|
||
test1();
|
||||
b6f37661 | Martin Helmling | |||
691a1293 | Geoffrey Richardson | test_overpayment_with_partialpayment();
|
||
test_overpayment();
|
||||
f942d1f6 | Martin Helmling | reset_state();
|
||
691a1293 | Geoffrey Richardson | test_skonto_exact();
|
||
test_two_invoices();
|
||||
test_partial_payment();
|
||||
280b772d | Geoffrey Richardson | test_credit_note();
|
||
64bfffde | Jan Büren | test_ap_transaction();
|
||
944d0062 | Jan Büren | test_neg_ap_transaction(invoice => 0);
|
||
test_neg_ap_transaction(invoice => 1);
|
||||
c97d4a0a | Jan Büren | test_ap_payment_transaction();
|
||
b6f37661 | Martin Helmling | test_ap_payment_part_transaction();
|
||
2c5a1cef | Jan Büren | test_neg_sales_invoice();
|
||
b75c6cbb | Jan Büren | test_two_neg_ap_transaction();
|
||
test_one_inv_and_two_invoices_with_skonto_exact();
|
||||
f942d1f6 | Martin Helmling | test_bt_error();
|
||
b11c7ad5 | Jan Büren | test_full_workflow_ar_multiple_inv_skonto_reconciliate_and_undo();
|
||
f942d1f6 | Martin Helmling | reset_state();
|
||
503fabbf | Martin Helmling | test_sepa_export();
|
||
f942d1f6 | Martin Helmling | reset_state();
|
||
test_bt_rule1();
|
||||
1b6d09ef | Martin Helmling | reset_state();
|
||
test_two_banktransactions();
|
||||
691a1293 | Geoffrey Richardson | # remove all created data at end of test
|
||
25230b8c | Jan Büren | test_closedto();
|
||
66796c03 | Geoffrey Richardson | clear_up();
|
||
691a1293 | Geoffrey Richardson | |||
done_testing();
|
||||
###### functions for setting up data
|
||||
sub reset_state {
|
||||
my %params = @_;
|
||||
c6d47254 | Geoffrey Richardson | $params{$_} ||= {} for qw(unit customer tax vendor);
|
||
691a1293 | Geoffrey Richardson | |||
clear_up();
|
||||
445ac52b | Geoffrey Richardson | $year = DateTime->today_local->year;
|
||
$year = 2019 if $year == 2020; # use year 2019 in 2020, because of tax rate change in Germany
|
||||
$dt = DateTime->new(year => $year, month => 1, day => 12);
|
||||
$dt_5 = $dt->clone->add(days => 5);
|
||||
$dt_10 = $dt->clone->add(days => 10);
|
||||
691a1293 | Geoffrey Richardson | |||
$tax = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} }) || croak "No tax";
|
||||
$tax7 = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07) || croak "No tax for 7\%";
|
||||
f942d1f6 | Martin Helmling | $tax_9 = SL::DB::Manager::Tax->find_by(taxkey => 9, rate => 0.19, %{ $params{tax} }) || croak "No tax for 19\%";
|
||
$tax0 = SL::DB::Manager::Tax->find_by(taxkey => 0, rate => 0.0) || croak "No tax for 0\%";
|
||||
691a1293 | Geoffrey Richardson | |||
$currency_id = $::instance_conf->get_currency_id;
|
||||
$bank_account = SL::DB::BankAccount->new(
|
||||
account_number => '123',
|
||||
bank_code => '123',
|
||||
iban => '123',
|
||||
bic => '123',
|
||||
bank => '123',
|
||||
chart_id => SL::DB::Manager::Chart->find_by(description => 'Bank')->id,
|
||||
name => SL::DB::Manager::Chart->find_by(description => 'Bank')->description,
|
||||
)->save;
|
||||
63410aa6 | Sven Schöling | $customer = new_customer(
|
||
c10ad4bb | Jan Büren | name => 'Test Customer OLÉ S.L. Årdbärg AB',
|
||
691a1293 | Geoffrey Richardson | iban => 'DE12500105170648489890',
|
||
bic => 'TESTBIC',
|
||||
account_number => '648489890',
|
||||
6378f59b | Jan Büren | mandate_date_of_signature => $dt,
|
||
691a1293 | Geoffrey Richardson | mandator_id => 'foobar',
|
||
bank => 'Geizkasse',
|
||||
c6d47254 | Geoffrey Richardson | bank_code => 'G1235',
|
||
691a1293 | Geoffrey Richardson | depositor => 'Test Customer',
|
||
b6f37661 | Martin Helmling | customernumber => 'CUST1704',
|
||
691a1293 | Geoffrey Richardson | )->save;
|
||
63410aa6 | Sven Schöling | $payment_terms = create_payment_terms();
|
||
c6d47254 | Geoffrey Richardson | |||
63410aa6 | Sven Schöling | $vendor = new_vendor(
|
||
c6d47254 | Geoffrey Richardson | name => 'Test Vendor',
|
||
payment_id => $payment_terms->id,
|
||||
iban => 'DE12500105170648489890',
|
||||
bic => 'TESTBIC',
|
||||
account_number => '648489890',
|
||||
bank => 'Geizkasse',
|
||||
bank_code => 'G1235',
|
||||
depositor => 'Test Vendor',
|
||||
b6f37661 | Martin Helmling | vendornumber => 'VEND1704',
|
||
691a1293 | Geoffrey Richardson | )->save;
|
||
$ar_chart = SL::DB::Manager::Chart->find_by( accno => '1400' ); # Forderungen
|
||||
$ap_chart = SL::DB::Manager::Chart->find_by( accno => '1600' ); # Verbindlichkeiten
|
||||
$bank = SL::DB::Manager::Chart->find_by( accno => '1200' ); # Bank
|
||||
$ar_amount_chart = SL::DB::Manager::Chart->find_by( accno => '8400' ); # Erlöse
|
||||
$ap_amount_chart = SL::DB::Manager::Chart->find_by( accno => '3400' ); # Wareneingang 19%
|
||||
}
|
||||
sub test_ar_transaction {
|
||||
my (%params) = @_;
|
||||
1b6d09ef | Martin Helmling | my $netamount = $params{amount} || 100;
|
||
my $amount = $::form->round_amount($netamount * 1.19,2);
|
||||
691a1293 | Geoffrey Richardson | my $invoice = SL::DB::Invoice->new(
|
||
invoice => 0,
|
||||
invnumber => $params{invnumber} || undef, # let it use its own invnumber
|
||||
amount => $amount,
|
||||
netamount => $netamount,
|
||||
6378f59b | Jan Büren | transdate => $dt,
|
||
f942d1f6 | Martin Helmling | taxincluded => $params{taxincluded } || 0,
|
||
691a1293 | Geoffrey Richardson | customer_id => $customer->id,
|
||
taxzone_id => $customer->taxzone_id,
|
||||
currency_id => $currency_id,
|
||||
transactions => [],
|
||||
payment_id => $params{payment_id} || undef,
|
||||
notes => 'test_ar_transaction',
|
||||
);
|
||||
$invoice->add_ar_amount_row(
|
||||
amount => $invoice->netamount,
|
||||
chart => $ar_amount_chart,
|
||||
f942d1f6 | Martin Helmling | tax_id => $params{tax_id} || $tax->id,
|
||
691a1293 | Geoffrey Richardson | );
|
||
$invoice->create_ar_row(chart => $ar_chart);
|
||||
$invoice->save;
|
||||
is($invoice->currency_id , $currency_id , 'currency_id has been saved');
|
||||
1b6d09ef | Martin Helmling | is($invoice->netamount , $netamount , 'ar amount has been converted');
|
||
is($invoice->amount , $amount , 'ar amount has been converted');
|
||||
691a1293 | Geoffrey Richardson | is($invoice->taxincluded , 0 , 'ar transaction doesn\'t have taxincluded');
|
||
1b6d09ef | Martin Helmling | if ( $netamount == 100 ) {
|
||
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');
|
||||
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');
|
||||
}
|
||||
691a1293 | Geoffrey Richardson | return $invoice;
|
||
};
|
||||
sub test_ap_transaction {
|
||||
my (%params) = @_;
|
||||
b6f37661 | Martin Helmling | my $testname = 'test_ap_transaction';
|
||
691a1293 | Geoffrey Richardson | my $netamount = 100;
|
||
my $amount = $::form->round_amount($netamount * 1.19,2);
|
||||
my $invoice = SL::DB::PurchaseInvoice->new(
|
||||
8f7fe4a4 | Geoffrey Richardson | invoice => 0,
|
||
b6f37661 | Martin Helmling | invnumber => $params{invnumber} || $testname,
|
||
8f7fe4a4 | Geoffrey Richardson | amount => $amount,
|
||
netamount => $netamount,
|
||||
6378f59b | Jan Büren | transdate => $dt,
|
||
8f7fe4a4 | Geoffrey Richardson | taxincluded => 0,
|
||
vendor_id => $vendor->id,
|
||||
taxzone_id => $vendor->taxzone_id,
|
||||
currency_id => $currency_id,
|
||||
transactions => [],
|
||||
notes => 'test_ap_transaction',
|
||||
691a1293 | Geoffrey Richardson | );
|
||
$invoice->add_ap_amount_row(
|
||||
amount => $invoice->netamount,
|
||||
chart => $ap_amount_chart,
|
||||
f942d1f6 | Martin Helmling | tax_id => $params{tax_id} || $tax_9->id,
|
||
691a1293 | Geoffrey Richardson | );
|
||
$invoice->create_ap_row(chart => $ap_chart);
|
||||
$invoice->save;
|
||||
b6f37661 | Martin Helmling | is($invoice->currency_id , $currency_id , "$testname: currency_id has been saved");
|
||
is($invoice->netamount , 100 , "$testname: ap amount has been converted");
|
||||
is($invoice->amount , 119 , "$testname: ap amount has been converted");
|
||||
is($invoice->taxincluded , 0 , "$testname: ap transaction doesn\'t have taxincluded");
|
||||
691a1293 | Geoffrey Richardson | |||
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');
|
||||
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');
|
||||
return $invoice;
|
||||
};
|
||||
###### test cases
|
||||
sub test1 {
|
||||
my $testname = 'test1';
|
||||
$ar_transaction = test_ar_transaction(invnumber => 'salesinv1');
|
||||
6378f59b | Jan Büren | my $bt = create_bank_transaction(record => $ar_transaction,
|
||
transdate => $dt,
|
||||
valutadate => $dt) or die "Couldn't create bank_transaction";
|
||||
691a1293 | Geoffrey Richardson | |||
$::form->{invoice_ids} = {
|
||||
8f7fe4a4 | Geoffrey Richardson | $bt->id => [ $ar_transaction->id ]
|
||
};
|
||||
a81f877a | Sven Schöling | |||
b8741ec3 | Martin Helmling | save_btcontroller_to_string();
|
||
691a1293 | Geoffrey Richardson | |||
$ar_transaction->load;
|
||||
$bt->load;
|
||||
is($ar_transaction->paid , '119.00000' , "$testname: salesinv1 was paid");
|
||||
is($ar_transaction->closed , 1 , "$testname: salesinv1 is closed");
|
||||
is($bt->invoice_amount , '119.00000' , "$testname: bt invoice amount was assigned");
|
||||
};
|
||||
sub test_skonto_exact {
|
||||
my $testname = 'test_skonto_exact';
|
||||
$ar_transaction = test_ar_transaction(invnumber => 'salesinv skonto',
|
||||
payment_id => $payment_terms->id,
|
||||
);
|
||||
63410aa6 | Sven Schöling | my $bt = create_bank_transaction(record => $ar_transaction,
|
||
bank_chart_id => $bank->id,
|
||||
6378f59b | Jan Büren | transdate => $dt,
|
||
valutadate => $dt,
|
||||
63410aa6 | Sven Schöling | amount => $ar_transaction->amount_less_skonto
|
||
) or die "Couldn't create bank_transaction";
|
||||
691a1293 | Geoffrey Richardson | |||
$::form->{invoice_ids} = {
|
||||
8f7fe4a4 | Geoffrey Richardson | $bt->id => [ $ar_transaction->id ]
|
||
};
|
||||
691a1293 | Geoffrey Richardson | $::form->{invoice_skontos} = {
|
||
8f7fe4a4 | Geoffrey Richardson | $bt->id => [ 'with_skonto_pt' ]
|
||
};
|
||||
a81f877a | Sven Schöling | |||
b8741ec3 | Martin Helmling | save_btcontroller_to_string();
|
||
691a1293 | Geoffrey Richardson | |||
$ar_transaction->load;
|
||||
$bt->load;
|
||||
is($ar_transaction->paid , '119.00000' , "$testname: salesinv skonto was paid");
|
||||
is($ar_transaction->closed , 1 , "$testname: salesinv skonto is closed");
|
||||
is($bt->invoice_amount , '113.05000' , "$testname: bt invoice amount was assigned");
|
||||
};
|
||||
f942d1f6 | Martin Helmling | sub test_bt_error {
|
||
b0d3ad92 | Jan Büren | my $testname = 'test_rollback_error';
|
||
# without type with_free_skonto the helper function (Payment.pm) looks ugly but not
|
||||
# breakable
|
||||
f942d1f6 | Martin Helmling | |||
$ar_transaction = test_ar_transaction(invnumber => 'salesinv skonto',
|
||||
payment_id => $payment_terms->id,
|
||||
taxincluded => 0,
|
||||
amount => 168.58 / 1.19,
|
||||
);
|
||||
my $bt = create_bank_transaction(record => $ar_transaction,
|
||||
bank_chart_id => $bank->id,
|
||||
6378f59b | Jan Büren | transdate => $dt,
|
||
valutadate => $dt,
|
||||
b0d3ad92 | Jan Büren | amount => 160.15,
|
||
f942d1f6 | Martin Helmling | ) or die "Couldn't create bank_transaction";
|
||
$::form->{invoice_ids} = {
|
||||
$bt->id => [ $ar_transaction->id ]
|
||||
};
|
||||
$::form->{invoice_skontos} = {
|
||||
b0d3ad92 | Jan Büren | $bt->id => [ 'with_skonto_pt' ]
|
||
f942d1f6 | Martin Helmling | };
|
||
is($ar_transaction->paid , '0' , "$testname: salesinv is not paid");
|
||||
# generate an error for testing rollback mechanism
|
||||
my $saved_skonto_sales_chart_id = $tax->skonto_sales_chart_id;
|
||||
$tax->skonto_sales_chart_id(undef);
|
||||
$tax->save;
|
||||
save_btcontroller_to_string();
|
||||
my @bt_errors = @{ $bt_controller->problems };
|
||||
is(substr($bt_errors[0]->{message},0,38), 'Kein Skontokonto für Steuerschlüssel 3', "$testname: Fehlermeldung ok");
|
||||
# set original value
|
||||
$tax->skonto_sales_chart_id($saved_skonto_sales_chart_id);
|
||||
$tax->save;
|
||||
$ar_transaction->load;
|
||||
$bt->load;
|
||||
is($ar_transaction->paid , '0.00000' , "$testname: salesinv was not paid");
|
||||
is($bt->invoice_amount , '0.00000' , "$testname: bt invoice amount was not assigned");
|
||||
};
|
||||
691a1293 | Geoffrey Richardson | sub test_two_invoices {
|
||
my $testname = 'test_two_invoices';
|
||||
my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv_1');
|
||||
my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv_2');
|
||||
63410aa6 | Sven Schöling | my $bt = create_bank_transaction(record => $ar_transaction_1,
|
||
amount => ($ar_transaction_1->amount + $ar_transaction_2->amount),
|
||||
purpose => "Rechnungen " . $ar_transaction_1->invnumber . " und " . $ar_transaction_2->invnumber,
|
||||
6378f59b | Jan Büren | transdate => $dt,
|
||
valutadate => $dt,
|
||||
63410aa6 | Sven Schöling | bank_chart_id => $bank->id,
|
||
) or die "Couldn't create bank_transaction";
|
||||
691a1293 | Geoffrey Richardson | |||
534a3e7e | Geoffrey Richardson | my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($ar_transaction_1);
|
||
is($agreement, 16, "points for ar_transaction_1 in test_two_invoices ok");
|
||||
691a1293 | Geoffrey Richardson | $::form->{invoice_ids} = {
|
||
8f7fe4a4 | Geoffrey Richardson | $bt->id => [ $ar_transaction_1->id, $ar_transaction_2->id ]
|
||
};
|
||||
a81f877a | Sven Schöling | |||
b8741ec3 | Martin Helmling | save_btcontroller_to_string();
|
||
691a1293 | Geoffrey Richardson | |||
$ar_transaction_1->load;
|
||||
$ar_transaction_2->load;
|
||||
$bt->load;
|
||||
1b6d09ef | Martin Helmling | is($ar_transaction_1->paid , '119.00000' , "$testname: salesinv_1 wcsv_import_reportsas paid");
|
||
691a1293 | Geoffrey Richardson | is($ar_transaction_1->closed , 1 , "$testname: salesinv_1 is closed");
|
||
is($ar_transaction_2->paid , '119.00000' , "$testname: salesinv_2 was paid");
|
||||
is($ar_transaction_2->closed , 1 , "$testname: salesinv_2 is closed");
|
||||
is($bt->invoice_amount , '238.00000' , "$testname: bt invoice amount was assigned");
|
||||
b75c6cbb | Jan Büren | }
|
||
sub test_one_inv_and_two_invoices_with_skonto_exact {
|
||||
my $testname = 'test_two_invoices_with_skonto_exact';
|
||||
my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv 1 skonto',
|
||||
payment_id => $payment_terms->id,
|
||||
);
|
||||
my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv 2 skonto',
|
||||
payment_id => $payment_terms->id,
|
||||
);
|
||||
my $ar_transaction_3 = test_ar_transaction(invnumber => 'salesinv 3 no skonto');
|
||||
my $bt = create_bank_transaction(record => $ar_transaction_1,
|
||||
bank_chart_id => $bank->id,
|
||||
6378f59b | Jan Büren | transdate => $dt,
|
||
valutadate => $dt,
|
||||
b75c6cbb | Jan Büren | amount => $ar_transaction_1->amount_less_skonto * 2 + $ar_transaction_3->amount
|
||
) or die "Couldn't create bank_transaction";
|
||||
$::form->{invoice_ids} = {
|
||||
$bt->id => [ $ar_transaction_1->id, $ar_transaction_3->id, $ar_transaction_2->id]
|
||||
};
|
||||
$::form->{invoice_skontos} = {
|
||||
$bt->id => [ 'with_skonto_pt', 'without_skonto', 'with_skonto_pt' ]
|
||||
};
|
||||
save_btcontroller_to_string();
|
||||
$ar_transaction_1->load;
|
||||
$ar_transaction_2->load;
|
||||
$ar_transaction_3->load;
|
||||
my $skonto_1 = SL::DB::Manager::AccTransaction->find_by(trans_id => $ar_transaction_1->id, chart_id => 162);
|
||||
my $skonto_2 = SL::DB::Manager::AccTransaction->find_by(trans_id => $ar_transaction_2->id, chart_id => 162);
|
||||
$bt->load;
|
||||
is($skonto_1->amount , '-5.95000' , "$testname: salesinv 1 skonto was booked");
|
||||
is($skonto_2->amount , '-5.95000' , "$testname: salesinv 2 skonto was booked");
|
||||
is($ar_transaction_1->paid , '119.00000' , "$testname: salesinv 1 was paid");
|
||||
is($ar_transaction_2->paid , '119.00000' , "$testname: salesinv 2 was paid");
|
||||
is($ar_transaction_3->paid , '119.00000' , "$testname: salesinv 3 was paid");
|
||||
is($ar_transaction_1->closed , 1 , "$testname: salesinv 1 skonto is closed");
|
||||
is($ar_transaction_2->closed , 1 , "$testname: salesinv 2 skonto is closed");
|
||||
is($ar_transaction_3->closed , 1 , "$testname: salesinv 2 skonto is closed");
|
||||
is($bt->invoice_amount , '345.10000' , "$testname: bt invoice amount was assigned");
|
||||
}
|
||||
691a1293 | Geoffrey Richardson | |||
sub test_overpayment {
|
||||
my $testname = 'test_overpayment';
|
||||
$ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid');
|
||||
# amount 135 > 119
|
||||
63410aa6 | Sven Schöling | my $bt = create_bank_transaction(record => $ar_transaction,
|
||
bank_chart_id => $bank->id,
|
||||
6378f59b | Jan Büren | transdate => $dt,
|
||
valutadate => $dt,
|
||||
63410aa6 | Sven Schöling | amount => 135
|
||
) or die "Couldn't create bank_transaction";
|
||||
691a1293 | Geoffrey Richardson | |||
$::form->{invoice_ids} = {
|
||||
8f7fe4a4 | Geoffrey Richardson | $bt->id => [ $ar_transaction->id ]
|
||
};
|
||||
a81f877a | Sven Schöling | |||
b8741ec3 | Martin Helmling | save_btcontroller_to_string();
|
||
691a1293 | Geoffrey Richardson | |||
$ar_transaction->load;
|
||||
$bt->load;
|
||||
665741c4 | Jan Büren | is($ar_transaction->paid , '119.00000' , "$testname: 'salesinv overpaid' was not overpaid");
|
||
is($bt->invoice_amount , '119.00000' , "$testname: bt invoice amount was not fully assigned with the overpaid amount");
|
||||
a81f877a | Sven Schöling | { local $TODO = 'this currently fails because closed ignores over-payments, see commit d90966c7';
|
||
691a1293 | Geoffrey Richardson | is($ar_transaction->closed , 0 , "$testname: 'salesinv overpaid' is open (via 'closed' method')");
|
||
a81f877a | Sven Schöling | }
|
||
665741c4 | Jan Büren | is($ar_transaction->open_amount == 0 ? 1 : 0 , 1 , "$testname: 'salesinv overpaid is closed (via amount-paid)");
|
||
691a1293 | Geoffrey Richardson | |||
};
|
||||
sub test_overpayment_with_partialpayment {
|
||||
665741c4 | Jan Büren | # two payments on different days, 10 and 119. If there is only one invoice we
|
||
# don't want it to be overpaid.
|
||||
691a1293 | Geoffrey Richardson | my $testname = 'test_overpayment_with_partialpayment';
|
||
$ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid partial');
|
||||
63410aa6 | Sven Schöling | my $bt_1 = create_bank_transaction(record => $ar_transaction,
|
||
bank_chart_id => $bank->id,
|
||||
6378f59b | Jan Büren | transdate => $dt,
|
||
valutadate => $dt,
|
||||
63410aa6 | Sven Schöling | amount => 10
|
||
) or die "Couldn't create bank_transaction";
|
||||
my $bt_2 = create_bank_transaction(record => $ar_transaction,
|
||||
amount => 119,
|
||||
6378f59b | Jan Büren | transdate => $dt_5,
|
||
valutadate => $dt_5,
|
||||
63410aa6 | Sven Schöling | bank_chart_id => $bank->id,
|
||
) or die "Couldn't create bank_transaction";
|
||||
691a1293 | Geoffrey Richardson | |||
$::form->{invoice_ids} = {
|
||||
8f7fe4a4 | Geoffrey Richardson | $bt_1->id => [ $ar_transaction->id ]
|
||
};
|
||||
b8741ec3 | Martin Helmling | save_btcontroller_to_string();
|
||
691a1293 | Geoffrey Richardson | |||
665741c4 | Jan Büren | $bt_1->load;
|
||
is($bt_1->invoice_amount , '10.00000' , "$testname: bt_1 invoice amount was fully assigned");
|
||||
691a1293 | Geoffrey Richardson | $::form->{invoice_ids} = {
|
||
8f7fe4a4 | Geoffrey Richardson | $bt_2->id => [ $ar_transaction->id ]
|
||
};
|
||||
b8741ec3 | Martin Helmling | save_btcontroller_to_string();
|
||
691a1293 | Geoffrey Richardson | |||
$ar_transaction->load;
|
||||
$bt_2->load;
|
||||
665741c4 | Jan Büren | is($bt_1->invoice_amount , '10.00000' , "$testname: bt_1 invoice amount was fully assigned");
|
||
is($ar_transaction->paid , '119.00000' , "$testname: 'salesinv overpaid partial' was not overpaid");
|
||||
is($bt_2->invoice_amount , '109.00000' , "$testname: bt_2 invoice amount was partly assigned");
|
||||
691a1293 | Geoffrey Richardson | |||
};
|
||||
sub test_partial_payment {
|
||||
my $testname = 'test_partial_payment';
|
||||
$ar_transaction = test_ar_transaction(invnumber => 'salesinv partial payment');
|
||||
# amount 100 < 119
|
||||
63410aa6 | Sven Schöling | my $bt = create_bank_transaction(record => $ar_transaction,
|
||
bank_chart_id => $bank->id,
|
||||
6378f59b | Jan Büren | transdate => $dt,
|
||
valutadate => $dt,
|
||||
63410aa6 | Sven Schöling | amount => 100
|
||
) or die "Couldn't create bank_transaction";
|
||||
691a1293 | Geoffrey Richardson | |||
$::form->{invoice_ids} = {
|
||||
8f7fe4a4 | Geoffrey Richardson | $bt->id => [ $ar_transaction->id ]
|
||
};
|
||||
a81f877a | Sven Schöling | |||
b8741ec3 | Martin Helmling | save_btcontroller_to_string();
|
||
691a1293 | Geoffrey Richardson | |||
$ar_transaction->load;
|
||||
$bt->load;
|
||||
is($ar_transaction->paid , '100.00000' , "$testname: 'salesinv partial payment' was partially paid");
|
||||
is($bt->invoice_amount , '100.00000' , "$testname: bt invoice amount was assigned partially paid amount");
|
||||
};
|
||||
b11c7ad5 | Jan Büren | sub test_full_workflow_ar_multiple_inv_skonto_reconciliate_and_undo {
|
||
my $testname = 'test_partial_payment';
|
||||
$ar_transaction = test_ar_transaction(invnumber => 'salesinv partial payment two');
|
||||
my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv 2 22d2', amount => 22);
|
||||
# amount 299.29 > 119
|
||||
my $bt = create_bank_transaction(record => $ar_transaction,
|
||||
bank_chart_id => $bank->id,
|
||||
amount => 299.29
|
||||
) or die "Couldn't create bank_transaction";
|
||||
$::form->{invoice_ids} = {
|
||||
$bt->id => [ $ar_transaction->id ]
|
||||
};
|
||||
save_btcontroller_to_string();
|
||||
$ar_transaction->load;
|
||||
$bt->load;
|
||||
is($ar_transaction->paid , '119.00000' , "$testname: 'salesinv partial payment' was fully paid");
|
||||
is($bt->invoice_amount , '119.00000' , "$testname: bt invoice amount was assigned partially paid amount");
|
||||
is($bt->amount , '299.29000' , "$testname: bt amount is stil there");
|
||||
# next invoice, same bank transaction
|
||||
$::form->{invoice_ids} = {
|
||||
$bt->id => [ $ar_transaction_2->id ]
|
||||
};
|
||||
save_btcontroller_to_string();
|
||||
$ar_transaction_2->load;
|
||||
$bt->load;
|
||||
is($ar_transaction_2->paid , '26.18000' , "$testname: 'salesinv partial payment' was fully paid");
|
||||
is($bt->invoice_amount , '145.18000' , "$testname: bt invoice amount was assigned partially paid amount");
|
||||
is($bt->amount , '299.29000' , "$testname: bt amount is stil there");
|
||||
is(scalar @{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )}, 4, "$testname 4 acc_trans entries created");
|
||||
# now check all 4 entries done so far and save paid acc_trans_ids for later use with reconcile
|
||||
foreach my $acc_trans_id_entry (@{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )}) {
|
||||
isnt($acc_trans_id_entry->ar_id, undef, "$testname: bt linked with acc_trans and trans_id set");
|
||||
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 ]);
|
||||
is (ref $rl->[0], 'SL::DB::RecordLink', "$testname record link created");
|
||||
my $acc_trans = SL::DB::Manager::AccTransaction->get_all(where => [acc_trans_id => $acc_trans_id_entry->acc_trans_id]);
|
||||
foreach my $entry (@{ $acc_trans }) {
|
||||
like(abs($entry->amount), qr/(119|26.18)/, "$testname: abs amount correct");
|
||||
like($entry->chart_link, qr/(paid|AR)/, "$testname chart_link correct");
|
||||
push @{ $::form->{bb_ids} }, $entry->acc_trans_id if $entry->chart_link =~ m/paid/;
|
||||
}
|
||||
}
|
||||
# great we need one last booking to clear the whole bank transaction - we include skonto
|
||||
my $ar_transaction_skonto = test_ar_transaction(invnumber => 'salesinv skonto last case',
|
||||
payment_id => $payment_terms->id,
|
||||
amount => 136.32,
|
||||
);
|
||||
$::form->{invoice_ids} = {
|
||||
$bt->id => [ $ar_transaction_skonto->id ]
|
||||
};
|
||||
$::form->{invoice_skontos} = {
|
||||
$bt->id => [ 'with_skonto_pt' ]
|
||||
};
|
||||
save_btcontroller_to_string();
|
||||
$ar_transaction_skonto->load;
|
||||
$bt->load;
|
||||
is($ar_transaction_skonto->paid , '162.22000' , "$testname: 'salesinv skonto fully paid");
|
||||
is($bt->invoice_amount , '299.29000' , "$testname: bt invoice amount was assigned partially paid amount");
|
||||
is($bt->amount , '299.29000' , "$testname: bt amount is stil there");
|
||||
is(scalar @{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )},
|
||||
7, "$testname 7 acc_trans entries created");
|
||||
# same loop as above, but only for the 3rd ar_id
|
||||
foreach my $acc_trans_id_entry (@{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [ar_id => $ar_transaction_skonto->id ] )}) {
|
||||
isnt($acc_trans_id_entry->ar_id, '', "$testname: bt linked with acc_trans and trans_id set");
|
||||
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 ]);
|
||||
is (ref $rl->[0], 'SL::DB::RecordLink', "$testname record link created");
|
||||
my $acc_trans = SL::DB::Manager::AccTransaction->get_all(where => [acc_trans_id => $acc_trans_id_entry->acc_trans_id]);
|
||||
foreach my $entry (@{ $acc_trans }) {
|
||||
like($entry->chart_link, qr/(paid|AR)/, "$testname chart_link correct");
|
||||
is ($entry->amount, '162.22000', "$testname full amont") if $entry->chart_link eq 'AR'; # full amount
|
||||
like(abs($entry->amount), qr/(154.11|8.11)/, "$testname: abs amount correct") if $entry->chart_link =~ m/paid/;
|
||||
push @{ $::form->{bb_ids} }, $entry->acc_trans_id if ($entry->chart_link =~ m/paid/ && $entry->amount == -154.11);
|
||||
}
|
||||
}
|
||||
# done, now reconciliate all bookings
|
||||
$::form->{bt_ids} = [ $bt->id ];
|
||||
my $rec_controller = SL::Controller::Reconciliation->new;
|
||||
my @errors = $rec_controller->_get_elements_and_validate;
|
||||
is (scalar @errors, 0, "$testname unsuccesfull reconciliation with error: " . Dumper(@errors));
|
||||
$rec_controller->_reconcile;
|
||||
$bt->load;
|
||||
# and check the cleared state of bt and the acc_transactions
|
||||
is($bt->cleared, '1' , "$testname: bt cleared");
|
||||
foreach (@{ $::form->{bb_ids} }) {
|
||||
my $acc_trans = SL::DB::Manager::AccTransaction->find_by(acc_trans_id => $_);
|
||||
is($acc_trans->cleared, '1' , "$testname: acc_trans entry cleared");
|
||||
}
|
||||
# now, this was a really bad idea and in general a major mistake. better undo and redo the whole bank transactions
|
||||
$::form->{ids} = [ $bt->id ];
|
||||
$bt_controller = SL::Controller::BankTransaction->new;
|
||||
$bt_controller->action_unlink_bank_transaction('testcase' => 1);
|
||||
$bt->load;
|
||||
# and check the cleared state of bt and the acc_transactions
|
||||
is($bt->cleared, '0' , "$testname: bt undo cleared");
|
||||
is($bt->invoice_amount, '0.00000' , "$testname: bt undo invoice amount");
|
||||
foreach (@{ $::form->{bb_ids} }) {
|
||||
my $acc_trans = SL::DB::Manager::AccTransaction->find_by(acc_trans_id => $_);
|
||||
is($acc_trans, undef , "$testname: cleared acc_trans entry completely removed");
|
||||
}
|
||||
# this was for data integrity for reconcile, now all the other options
|
||||
is(scalar @{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )},
|
||||
0, "$testname 7 acc_trans entries deleted");
|
||||
my $rl = SL::DB::Manager::RecordLink->get_all(where => [ from_id => $bt->id, from_table => 'bank_transactions' ]);
|
||||
is (ref $rl->[0], '', "$testname record link removed");
|
||||
# double safety and check ar.paid
|
||||
# load all three invoices and check for paid-link via acc_trans and paid in general
|
||||
$ar_transaction->load;
|
||||
$ar_transaction_2->load;
|
||||
$ar_transaction_skonto->load;
|
||||
is(scalar @{ SL::DB::Manager::AccTransaction->get_all(
|
||||
where => [ trans_id => $ar_transaction->id, chart_link => { like => '%paid%' } ])},
|
||||
0, "$testname no more paid entries in acc_trans for ar_transaction");
|
||||
is(scalar @{ SL::DB::Manager::AccTransaction->get_all(
|
||||
where => [ trans_id => $ar_transaction_2->id, chart_link => { like => '%paid%' } ])},
|
||||
0, "$testname no more paid entries in acc_trans for ar_transaction_2");
|
||||
is(scalar @{ SL::DB::Manager::AccTransaction->get_all(
|
||||
where => [ trans_id => $ar_transaction_skonto->id, chart_link => { like => '%paid%' } ])},
|
||||
0, "$testname no more paid entries in acc_trans for ar_transaction_skonto");
|
||||
is($ar_transaction->paid , '0.00000' , "$testname: 'salesinv fully unpaid");
|
||||
is($ar_transaction_2->paid , '0.00000' , "$testname: 'salesinv 2 fully unpaid");
|
||||
is($ar_transaction_skonto->paid , '0.00000' , "$testname: 'salesinv skonto fully unpaid");
|
||||
# whew. w(h)a(n)t a whole lotta test
|
||||
}
|
||||
280b772d | Geoffrey Richardson | sub test_credit_note {
|
||
my $testname = 'test_credit_note';
|
||||
63410aa6 | Sven Schöling | my $part1 = new_part( partnumber => 'T4254')->save;
|
||
my $part2 = new_service(partnumber => 'Serv1')->save;
|
||||
my $credit_note = create_credit_note(
|
||||
280b772d | Geoffrey Richardson | invnumber => 'cn 1',
|
||
customer => $customer,
|
||||
6378f59b | Jan Büren | transdate => $dt,
|
||
280b772d | Geoffrey Richardson | taxincluded => 0,
|
||
63410aa6 | Sven Schöling | invoiceitems => [ create_invoice_item(part => $part1, qty => 3, sellprice => 70),
|
||
create_invoice_item(part => $part2, qty => 10, sellprice => 50),
|
||||
280b772d | Geoffrey Richardson | ]
|
||
);
|
||||
63410aa6 | Sven Schöling | my $bt = create_bank_transaction(record => $credit_note,
|
||
280b772d | Geoffrey Richardson | amount => $credit_note->amount,
|
||
bank_chart_id => $bank->id,
|
||||
6378f59b | Jan Büren | transdate => $dt_10,
|
||
280b772d | Geoffrey Richardson | );
|
||
my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($credit_note);
|
||||
is($agreement, 13, "points for credit note ok");
|
||||
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");
|
||||
$::form->{invoice_ids} = {
|
||||
8f7fe4a4 | Geoffrey Richardson | $bt->id => [ $credit_note->id ]
|
||
280b772d | Geoffrey Richardson | };
|
||
b8741ec3 | Martin Helmling | save_btcontroller_to_string();
|
||
280b772d | Geoffrey Richardson | |||
$credit_note->load;
|
||||
$bt->load;
|
||||
is($credit_note->amount , '-844.90000', "$testname: amount ok");
|
||||
is($credit_note->netamount, '-710.00000', "$testname: netamount ok");
|
||||
is($credit_note->paid , '-844.90000', "$testname: paid ok");
|
||||
}
|
||||
b8741ec3 | Martin Helmling | sub test_neg_ap_transaction {
|
||
084c45b1 | Martin Helmling | my (%params) = @_;
|
||
944d0062 | Jan Büren | my $testname = 'test_neg_ap_transaction' . $params{invoice} ? ' invoice booking' : ' credit booking';
|
||
b8741ec3 | Martin Helmling | my $netamount = -20;
|
||
my $amount = $::form->round_amount($netamount * 1.19,2);
|
||||
my $invoice = SL::DB::PurchaseInvoice->new(
|
||||
944d0062 | Jan Büren | invoice => $params{invoice} // 0,
|
||
8f7fe4a4 | Geoffrey Richardson | invnumber => $params{invnumber} || 'test_neg_ap_transaction',
|
||
amount => $amount,
|
||||
netamount => $netamount,
|
||||
6378f59b | Jan Büren | transdate => $dt,
|
||
8f7fe4a4 | Geoffrey Richardson | taxincluded => 0,
|
||
vendor_id => $vendor->id,
|
||||
taxzone_id => $vendor->taxzone_id,
|
||||
currency_id => $currency_id,
|
||||
transactions => [],
|
||||
notes => 'test_neg_ap_transaction',
|
||||
b8741ec3 | Martin Helmling | );
|
||
$invoice->add_ap_amount_row(
|
||||
amount => $invoice->netamount,
|
||||
chart => $ap_amount_chart,
|
||||
tax_id => $tax_9->id,
|
||||
);
|
||||
$invoice->create_ap_row(chart => $ap_chart);
|
||||
$invoice->save;
|
||||
8f7fe4a4 | Geoffrey Richardson | is($invoice->netamount, -20 , "$testname: netamount ok");
|
||
is($invoice->amount , -23.8, "$testname: amount ok");
|
||||
b8741ec3 | Martin Helmling | |||
63410aa6 | Sven Schöling | my $bt = create_bank_transaction(record => $invoice,
|
||
amount => $invoice->amount,
|
||||
bank_chart_id => $bank->id,
|
||||
6378f59b | Jan Büren | transdate => $dt_10,
|
||
b8741ec3 | Martin Helmling | );
|
||
665741c4 | Jan Büren | |||
b8741ec3 | Martin Helmling | my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($invoice);
|
||
b6f8a6b0 | Jan Büren | is($agreement, 16, "points for negative ap transaction ok");
|
||
b8741ec3 | Martin Helmling | |||
$::form->{invoice_ids} = {
|
||||
8f7fe4a4 | Geoffrey Richardson | $bt->id => [ $invoice->id ]
|
||
b8741ec3 | Martin Helmling | };
|
||
save_btcontroller_to_string();
|
||||
$invoice->load;
|
||||
d301d97a | Jan Büren | $bt->load;
|
||
b8741ec3 | Martin Helmling | |||
is($invoice->amount , '-23.80000', "$testname: amount ok");
|
||||
is($invoice->netamount, '-20.00000', "$testname: netamount ok");
|
||||
is($invoice->paid , '-23.80000', "$testname: paid ok");
|
||||
d301d97a | Jan Büren | is($bt->invoice_amount, '23.80000', "$testname: bt invoice amount for ap was assigned");
|
||
665741c4 | Jan Büren | is($bt->amount, '23.80000', "$testname: bt amount for ap was assigned");
|
||
b8741ec3 | Martin Helmling | |||
return $invoice;
|
||||
};
|
||||
b75c6cbb | Jan Büren | sub test_two_neg_ap_transaction {
|
||
my $testname='test_two_neg_ap_transaction';
|
||||
my $netamount = -20;
|
||||
my $amount = $::form->round_amount($netamount * 1.19,2);
|
||||
my $invoice = SL::DB::PurchaseInvoice->new(
|
||||
invoice => 0,
|
||||
invnumber => 'test_neg_ap_transaction',
|
||||
amount => $amount,
|
||||
netamount => $netamount,
|
||||
6378f59b | Jan Büren | transdate => $dt,
|
||
b75c6cbb | Jan Büren | taxincluded => 0,
|
||
vendor_id => $vendor->id,
|
||||
taxzone_id => $vendor->taxzone_id,
|
||||
currency_id => $currency_id,
|
||||
transactions => [],
|
||||
notes => 'test_neg_ap_transaction',
|
||||
);
|
||||
$invoice->add_ap_amount_row(
|
||||
amount => $invoice->netamount,
|
||||
chart => $ap_amount_chart,
|
||||
tax_id => $tax_9->id,
|
||||
);
|
||||
$invoice->create_ap_row(chart => $ap_chart);
|
||||
$invoice->save;
|
||||
is($invoice->netamount, -20 , "$testname: netamount ok");
|
||||
is($invoice->amount , -23.8, "$testname: amount ok");
|
||||
my $netamount_two = -1.14;
|
||||
my $amount_two = $::form->round_amount($netamount_two * 1.19,2);
|
||||
my $invoice_two = SL::DB::PurchaseInvoice->new(
|
||||
invoice => 0,
|
||||
invnumber => 'test_neg_ap_transaction_two',
|
||||
amount => $amount_two,
|
||||
netamount => $netamount_two,
|
||||
6378f59b | Jan Büren | transdate => $dt,
|
||
b75c6cbb | Jan Büren | taxincluded => 0,
|
||
vendor_id => $vendor->id,
|
||||
taxzone_id => $vendor->taxzone_id,
|
||||
currency_id => $currency_id,
|
||||
transactions => [],
|
||||
notes => 'test_neg_ap_transaction_two',
|
||||
);
|
||||
$invoice_two->add_ap_amount_row(
|
||||
amount => $invoice_two->netamount,
|
||||
chart => $ap_amount_chart,
|
||||
tax_id => $tax_9->id,
|
||||
);
|
||||
$invoice_two->create_ap_row(chart => $ap_chart);
|
||||
$invoice_two->save;
|
||||
is($invoice_two->netamount, -1.14 , "$testname: netamount ok");
|
||||
is($invoice_two->amount , -1.36, "$testname: amount ok");
|
||||
my $bt = create_bank_transaction(record => $invoice_two,
|
||||
amount => $invoice_two->amount + $invoice->amount,
|
||||
bank_chart_id => $bank->id,
|
||||
6378f59b | Jan Büren | transdate => $dt_10,
|
||
b75c6cbb | Jan Büren | );
|
||
# my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($invoice_two);
|
||||
# is($agreement, 15, "points for negative ap transaction ok");
|
||||
$::form->{invoice_ids} = {
|
||||
$bt->id => [ $invoice->id, $invoice_two->id ]
|
||||
};
|
||||
save_btcontroller_to_string();
|
||||
$invoice->load;
|
||||
$invoice_two->load;
|
||||
$bt->load;
|
||||
is($invoice->amount , '-23.80000', "$testname: first inv amount ok");
|
||||
is($invoice->netamount, '-20.00000', "$testname: first inv netamount ok");
|
||||
is($invoice->paid , '-23.80000', "$testname: first inv paid ok");
|
||||
is($invoice_two->amount , '-1.36000', "$testname: second inv amount ok");
|
||||
is($invoice_two->netamount, '-1.14000', "$testname: second inv netamount ok");
|
||||
is($invoice_two->paid , '-1.36000', "$testname: second inv paid ok");
|
||||
is($bt->invoice_amount, '25.16000', "$testname: bt invoice amount for both invoices were assigned");
|
||||
return ($invoice, $invoice_two);
|
||||
};
|
||||
b8741ec3 | Martin Helmling | |||
c97d4a0a | Jan Büren | sub test_ap_payment_transaction {
|
||
my (%params) = @_;
|
||||
b6f37661 | Martin Helmling | my $testname = 'test_ap_payment_transaction';
|
||
c97d4a0a | Jan Büren | my $netamount = 115;
|
||
my $amount = $::form->round_amount($netamount * 1.19,2);
|
||||
my $invoice = SL::DB::PurchaseInvoice->new(
|
||||
invoice => 0,
|
||||
b6f37661 | Martin Helmling | invnumber => $params{invnumber} || $testname,
|
||
c97d4a0a | Jan Büren | amount => $amount,
|
||
netamount => $netamount,
|
||||
6378f59b | Jan Büren | transdate => $dt,
|
||
c97d4a0a | Jan Büren | taxincluded => 0,
|
||
vendor_id => $vendor->id,
|
||||
taxzone_id => $vendor->taxzone_id,
|
||||
currency_id => $currency_id,
|
||||
transactions => [],
|
||||
b6f37661 | Martin Helmling | notes => $testname,
|
||
c97d4a0a | Jan Büren | );
|
||
$invoice->add_ap_amount_row(
|
||||
amount => $invoice->netamount,
|
||||
chart => $ap_amount_chart,
|
||||
tax_id => $tax_9->id,
|
||||
);
|
||||
$invoice->create_ap_row(chart => $ap_chart);
|
||||
$invoice->save;
|
||||
is($invoice->netamount, 115 , "$testname: netamount ok");
|
||||
is($invoice->amount , 136.85, "$testname: amount ok");
|
||||
63410aa6 | Sven Schöling | my $bt = create_bank_transaction(record => $invoice,
|
||
amount => $invoice->amount,
|
||||
bank_chart_id => $bank->id,
|
||||
6378f59b | Jan Büren | transdate => $dt_10,
|
||
63410aa6 | Sven Schöling | );
|
||
c97d4a0a | Jan Büren | $::form->{invoice_ids} = {
|
||
$bt->id => [ $invoice->id ]
|
||||
};
|
||||
save_btcontroller_to_string();
|
||||
$invoice->load;
|
||||
$bt->load;
|
||||
is($invoice->amount , '136.85000', "$testname: amount ok");
|
||||
is($invoice->netamount, '115.00000', "$testname: netamount ok");
|
||||
b11c7ad5 | Jan Büren | is($bt->amount, '-136.85000', "$testname: bt amount ok");
|
||
c97d4a0a | Jan Büren | is($invoice->paid , '136.85000', "$testname: paid ok");
|
||
is($bt->invoice_amount, '-136.85000', "$testname: bt invoice amount for ap was assigned");
|
||||
return $invoice;
|
||||
};
|
||||
b6f37661 | Martin Helmling | |||
sub test_ap_payment_part_transaction {
|
||||
my (%params) = @_;
|
||||
my $testname = 'test_ap_payment_p_transaction';
|
||||
my $netamount = 115;
|
||||
my $amount = $::form->round_amount($netamount * 1.19,2);
|
||||
my $invoice = SL::DB::PurchaseInvoice->new(
|
||||
invoice => 0,
|
||||
invnumber => $params{invnumber} || $testname,
|
||||
amount => $amount,
|
||||
netamount => $netamount,
|
||||
6378f59b | Jan Büren | transdate => $dt,
|
||
b6f37661 | Martin Helmling | taxincluded => 0,
|
||
vendor_id => $vendor->id,
|
||||
taxzone_id => $vendor->taxzone_id,
|
||||
currency_id => $currency_id,
|
||||
transactions => [],
|
||||
notes => $testname,
|
||||
);
|
||||
$invoice->add_ap_amount_row(
|
||||
amount => $invoice->netamount,
|
||||
chart => $ap_amount_chart,
|
||||
tax_id => $tax_9->id,
|
||||
);
|
||||
$invoice->create_ap_row(chart => $ap_chart);
|
||||
$invoice->save;
|
||||
is($invoice->netamount, 115 , "$testname: netamount ok");
|
||||
is($invoice->amount , 136.85, "$testname: amount ok");
|
||||
63410aa6 | Sven Schöling | my $bt = create_bank_transaction(record => $invoice,
|
||
amount => $invoice->amount-100,
|
||||
bank_chart_id => $bank->id,
|
||||
6378f59b | Jan Büren | transdate => $dt_10,
|
||
63410aa6 | Sven Schöling | );
|
||
b6f37661 | Martin Helmling | $::form->{invoice_ids} = {
|
||
$bt->id => [ $invoice->id ]
|
||||
};
|
||||
save_btcontroller_to_string();
|
||||
$invoice->load;
|
||||
$bt->load;
|
||||
is($invoice->amount , '136.85000', "$testname: amount ok");
|
||||
is($invoice->netamount, '115.00000', "$testname: netamount ok");
|
||||
is($bt->amount, '-36.85000', "$testname: bt amount ok");
|
||||
is($invoice->paid , '36.85000', "$testname: paid ok");
|
||||
is($bt->invoice_amount, '-36.85000', "$testname: bt invoice amount for ap was assigned");
|
||||
63410aa6 | Sven Schöling | my $bt2 = create_bank_transaction(record => $invoice,
|
||
amount => 100,
|
||||
bank_chart_id => $bank->id,
|
||||
6378f59b | Jan Büren | transdate => $dt_10,
|
||
63410aa6 | Sven Schöling | );
|
||
b6f37661 | Martin Helmling | $::form->{invoice_ids} = {
|
||
$bt2->id => [ $invoice->id ]
|
||||
};
|
||||
save_btcontroller_to_string();
|
||||
$invoice->load;
|
||||
$bt2->load;
|
||||
is($invoice->amount , '136.85000', "$testname: amount ok");
|
||||
is($invoice->netamount, '115.00000', "$testname: netamount ok");
|
||||
is($bt2->amount, '-100.00000',"$testname: bt amount ok");
|
||||
is($invoice->paid , '136.85000', "$testname: paid ok");
|
||||
is($bt2->invoice_amount,'-100.00000', "$testname: bt invoice amount for ap was assigned");
|
||||
return $invoice;
|
||||
};
|
||||
2c5a1cef | Jan Büren | sub test_neg_sales_invoice {
|
||
my $testname = 'test_neg_sales_invoice';
|
||||
63410aa6 | Sven Schöling | my $part1 = new_part( partnumber => 'Funkenhaube öhm')->save;
|
||
my $part2 = new_service(partnumber => 'Service-Pauschale Pasch!')->save;
|
||||
2c5a1cef | Jan Büren | |||
63410aa6 | Sven Schöling | my $neg_sales_inv = create_sales_invoice(
|
||
2c5a1cef | Jan Büren | invnumber => '20172201',
|
||
customer => $customer,
|
||||
taxincluded => 0,
|
||||
6378f59b | Jan Büren | transdate => $dt,
|
||
63410aa6 | Sven Schöling | invoiceitems => [ create_invoice_item(part => $part1, qty => 3, sellprice => 70),
|
||
create_invoice_item(part => $part2, qty => 10, sellprice => -50),
|
||||
2c5a1cef | Jan Büren | ]
|
||
);
|
||||
63410aa6 | Sven Schöling | my $bt = create_bank_transaction(record => $neg_sales_inv,
|
||
2c5a1cef | Jan Büren | amount => $neg_sales_inv->amount,
|
||
bank_chart_id => $bank->id,
|
||||
6378f59b | Jan Büren | transdate => $dt,
|
||
valutadate => $dt,
|
||||
2c5a1cef | Jan Büren | );
|
||
$::form->{invoice_ids} = {
|
||||
$bt->id => [ $neg_sales_inv->id ]
|
||||
};
|
||||
save_btcontroller_to_string();
|
||||
$neg_sales_inv->load;
|
||||
$bt->load;
|
||||
is($neg_sales_inv->amount , '-345.10000', "$testname: amount ok");
|
||||
is($neg_sales_inv->netamount, '-290.00000', "$testname: netamount ok");
|
||||
is($neg_sales_inv->paid , '-345.10000', "$testname: paid ok");
|
||||
is($bt->amount , '-345.10000', "$testname: bt amount ok");
|
||||
is($bt->invoice_amount , '-345.10000', "$testname: bt invoice_amount ok");
|
||||
}
|
||||
503fabbf | Martin Helmling | sub test_bt_rule1 {
|
||
my $testname = 'test_bt_rule1';
|
||||
6378f59b | Jan Büren | $ar_transaction = test_ar_transaction(invnumber => 'bt_rule1', transdate => $dt);
|
||
503fabbf | Martin Helmling | |||
6378f59b | Jan Büren | my $bt = create_bank_transaction(record => $ar_transaction, transdate => $dt) or die "Couldn't create bank_transaction";
|
||
503fabbf | Martin Helmling | |||
$ar_transaction->load;
|
||||
$bt->load;
|
||||
is($ar_transaction->paid , '0.00000' , "$testname: not paid");
|
||||
is($bt->invoice_amount , '0.00000' , "$testname: bt invoice amount was not assigned");
|
||||
my $bt_controller = SL::Controller::BankTransaction->new;
|
||||
1c98ac62 | Moritz Bunkus | my ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
|
||
503fabbf | Martin Helmling | |||
is(scalar(@$bt_transactions) , 1 , "$testname: one bank_transaction");
|
||||
is($bt_transactions->[0]->{agreement}, 20 , "$testname: agreement == 20");
|
||||
my $match = join ( ' ',@{$bt_transactions->[0]->{rule_matches}});
|
||||
#print "rule_matches='".$match."'\n";
|
||||
is($match,
|
||||
1b6d09ef | Martin Helmling | "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) ",
|
||
503fabbf | Martin Helmling | "$testname: rule_matches ok");
|
||
$bt->invoice_amount($bt->amount);
|
||||
$bt->save;
|
||||
is($bt->invoice_amount , '119.00000' , "$testname: bt invoice amount now set");
|
||||
};
|
||||
sub test_sepa_export {
|
||||
my $testname = 'test_sepa_export';
|
||||
6378f59b | Jan Büren | $ar_transaction = test_ar_transaction(invnumber => 'sepa1', transdate => $dt);
|
||
503fabbf | Martin Helmling | |||
6378f59b | Jan Büren | my $bt = create_bank_transaction(record => $ar_transaction, transdate => $dt) or die "Couldn't create bank_transaction";
|
||
63410aa6 | Sven Schöling | my $se = create_sepa_export();
|
||
my $sei = create_sepa_export_item(
|
||||
503fabbf | Martin Helmling | chart_id => $bank->id,
|
||
ar_id => $ar_transaction->id,
|
||||
sepa_export_id => $se->id,
|
||||
vc_iban => $customer->iban,
|
||||
vc_bic => $customer->bic,
|
||||
vc_mandator_id => $customer->mandator_id,
|
||||
vc_depositor => $customer->depositor,
|
||||
amount => $ar_transaction->amount,
|
||||
);
|
||||
c10ad4bb | Jan Büren | require SL::SEPA::XML;
|
||
my $sepa_xml = SL::SEPA::XML->new('company' => $customer->name,
|
||||
'creditor_id' => "id",
|
||||
'src_charset' => 'UTF-8',
|
||||
'message_id' => "test",
|
||||
'grouped' => 1,
|
||||
'collection' => 1,
|
||||
);
|
||||
is($sepa_xml->{company} , 'Test Customer OLE S.L. Ardbaerg AB');
|
||||
503fabbf | Martin Helmling | |||
$ar_transaction->load;
|
||||
$bt->load;
|
||||
$sei->load;
|
||||
is($ar_transaction->paid , '0.00000' , "$testname: sepa1 not paid");
|
||||
is($bt->invoice_amount , '0.00000' , "$testname: bt invoice amount was not assigned");
|
||||
is($bt->amount , '119.00000' , "$testname: bt amount ok");
|
||||
is($sei->amount , '119.00000' , "$testname: sepa export amount ok");
|
||||
my $bt_controller = SL::Controller::BankTransaction->new;
|
||||
1c98ac62 | Moritz Bunkus | my ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
|
||
503fabbf | Martin Helmling | |||
is(scalar(@$bt_transactions) , 1 , "$testname: one bank_transaction");
|
||||
is($bt_transactions->[0]->{agreement}, 25 , "$testname: agreement == 25");
|
||||
my $match = join ( ' ',@{$bt_transactions->[0]->{rule_matches}});
|
||||
is($match,
|
||||
1b6d09ef | Martin Helmling | "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) ",
|
||
503fabbf | Martin Helmling | "$testname: rule_matches ok");
|
||
};
|
||||
1b6d09ef | Martin Helmling | sub test_two_banktransactions {
|
||
my $testname = 'two_banktransactions';
|
||||
my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv10000' , amount => 2912.00 );
|
||||
my $bt1 = create_bank_transaction(record => $ar_transaction_1,
|
||||
amount => $ar_transaction_1->amount,
|
||||
purpose => "Rechnung10000 beinahe",
|
||||
6378f59b | Jan Büren | transdate => $dt,
|
||
1b6d09ef | Martin Helmling | bank_chart_id => $bank->id,
|
||
) or die "Couldn't create bank_transaction";
|
||||
my $bt2 = create_bank_transaction(record => $ar_transaction_1,
|
||||
amount => $ar_transaction_1->amount + 0.01,
|
||||
purpose => "sicher salesinv20000 vielleicht",
|
||||
6378f59b | Jan Büren | transdate => $dt,
|
||
1b6d09ef | Martin Helmling | bank_chart_id => $bank->id,
|
||
) or die "Couldn't create bank_transaction";
|
||||
my ($agreement1, $rule_matches1) = $bt1->get_agreement_with_invoice($ar_transaction_1);
|
||||
is($agreement1, 19, "bt1 19 points for ar_transaction_1 in $testname ok");
|
||||
#print "rule_matches1=".$rule_matches1."\n";
|
||||
is($rule_matches1,
|
||||
"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) ",
|
||||
"$testname: rule_matches ok");
|
||||
my ($agreement2, $rule_matches2) = $bt2->get_agreement_with_invoice($ar_transaction_1);
|
||||
is($agreement2, 11, "bt2 11 points for ar_transaction_1 in $testname ok");
|
||||
is($rule_matches2,
|
||||
"remote_account_number(3) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus0(3) ",
|
||||
"$testname: rule_matches ok");
|
||||
my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv20000' , amount => 2912.01 );
|
||||
my $ar_transaction_3 = test_ar_transaction(invnumber => 'zweitemit10000', amount => 2912.00 );
|
||||
($agreement1, $rule_matches1) = $bt1->get_agreement_with_invoice($ar_transaction_2);
|
||||
is($agreement1, 11, "bt1 11 points for ar_transaction_2 in $testname ok");
|
||||
($agreement2, $rule_matches2) = $bt2->get_agreement_with_invoice($ar_transaction_2);
|
||||
is($agreement2, 20, "bt2 20 points for ar_transaction_2 in $testname ok");
|
||||
($agreement2, $rule_matches2) = $bt2->get_agreement_with_invoice($ar_transaction_1);
|
||||
is($agreement2, 11, "bt2 11 points for ar_transaction_1 in $testname ok");
|
||||
my $bt3 = create_bank_transaction(record => $ar_transaction_3,
|
||||
amount => $ar_transaction_3->amount,
|
||||
purpose => "sicher Rechnung10000 vielleicht",
|
||||
6378f59b | Jan Büren | transdate => $dt,
|
||
1b6d09ef | Martin Helmling | bank_chart_id => $bank->id,
|
||
) or die "Couldn't create bank_transaction";
|
||||
my ($agreement3, $rule_matches3) = $bt3->get_agreement_with_invoice($ar_transaction_3);
|
||||
is($agreement3, 19, "bt3 19 points for ar_transaction_3 in $testname ok");
|
||||
$bt2->delete;
|
||||
$ar_transaction_2->delete;
|
||||
#nun sollten zwei gleichwertige Rechnungen $ar_transaction_1 und $ar_transaction_3 für $bt1 gefunden werden
|
||||
#aber es darf keine Proposals geben mit mehreren Rechnungen
|
||||
my $bt_controller = SL::Controller::BankTransaction->new;
|
||||
1c98ac62 | Moritz Bunkus | my ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
|
||
1b6d09ef | Martin Helmling | |||
is(scalar(@$bt_transactions) , 2 , "$testname: two bank_transaction");
|
||||
is(scalar(@$proposals) , 0 , "$testname: no proposals");
|
||||
$ar_transaction_3->delete;
|
||||
# Jetzt gibt es zwei Kontobewegungen mit gleichen Punkten für eine Rechnung.
|
||||
# hier darf es auch keine Proposals geben
|
||||
1c98ac62 | Moritz Bunkus | ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
|
||
1b6d09ef | Martin Helmling | |||
is(scalar(@$bt_transactions) , 2 , "$testname: two bank_transaction");
|
||||
283306d7 | Jan Büren | # odyn testfall - anforderungen so (noch) nicht in kivi
|
||
# is(scalar(@$proposals) , 0 , "$testname: no proposals");
|
||||
1b6d09ef | Martin Helmling | |||
# Jetzt gibt es zwei Kontobewegungen für eine Rechnung.
|
||||
# eine Bewegung bekommt mehr Punkte
|
||||
# hier darf es auch keine Proposals geben
|
||||
$bt3->update_attributes( purpose => "fuer Rechnung salesinv10000");
|
||||
1c98ac62 | Moritz Bunkus | ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
|
||
1b6d09ef | Martin Helmling | |||
is(scalar(@$bt_transactions) , 2 , "$testname: two bank_transaction");
|
||||
283306d7 | Jan Büren | # odyn testfall - anforderungen so (noch) nicht in kivi
|
||
# is(scalar(@$proposals) , 1 , "$testname: one proposal");
|
||||
1b6d09ef | Martin Helmling | |||
};
|
||||
25230b8c | Jan Büren | sub test_closedto {
|
||
my $testname = 'closedto';
|
||||
my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv10000' , amount => 2912.00 );
|
||||
my $bt1 = create_bank_transaction(record => $ar_transaction_1,
|
||||
amount => $ar_transaction_1->amount,
|
||||
purpose => "Rechnung10000 beinahe",
|
||||
bank_chart_id => $bank->id,
|
||||
) or die "Couldn't create bank_transaction";
|
||||
$bt1->valutadate(DateTime->new(year => 2019, month => 12, day => 30));
|
||||
$bt1->save();
|
||||
is($bt1->closed_period, 0, "$testname undefined closedto");
|
||||
my $defaults = SL::DB::Manager::Default->get_all(limit => 1)->[0];
|
||||
5027341d | Jan Büren | $defaults->closedto(DateTime->new(year => 2019, month => 12, day => 31));
|
||
25230b8c | Jan Büren | $defaults->save();
|
||
$::instance_conf->reload->data;
|
||||
$bt1->load();
|
||||
5027341d | Jan Büren | is($bt1->closed_period, 1, "$testname defined and next date closedto");
|
||
1b6d09ef | Martin Helmling | |||
5027341d | Jan Büren | $bt1->valutadate(DateTime->new(year => 2020, month => 1, day => 1));
|
||
25230b8c | Jan Büren | $bt1->save();
|
||
$bt1->load();
|
||||
is($bt1->closed_period, 0, "$testname defined closedto and next date valuta");
|
||||
5027341d | Jan Büren | $defaults->closedto(undef);
|
||
$defaults->save();
|
||||
25230b8c | Jan Büren | |||
}
|
||||
503fabbf | Martin Helmling | |||
0307fa8a | Sven Schöling | sub test_skonto_exact_ap_transaction {
|
||
my $testname = 'test_skonto_exact_ap_transaction';
|
||||
$ap_transaction = test_ap_transaction(invnumber => 'ap transaction skonto',
|
||||
payment_id => $payment_terms->id,
|
||||
);
|
||||
my $bt = create_bank_transaction(record => $ap_transaction,
|
||||
bank_chart_id => $bank->id,
|
||||
transdate => $dt,
|
||||
valutadate => $dt,
|
||||
amount => $ap_transaction->amount_less_skonto
|
||||
) or die "Couldn't create bank_transaction";
|
||||
$::form->{invoice_ids} = {
|
||||
$bt->id => [ $ap_transaction->id ]
|
||||
};
|
||||
$::form->{invoice_skontos} = {
|
||||
$bt->id => [ 'with_skonto_pt' ]
|
||||
};
|
||||
save_btcontroller_to_string();
|
||||
$ap_transaction->load;
|
||||
$bt->load;
|
||||
is($ap_transaction->paid , '119.00000' , "$testname: ap transaction skonto was paid");
|
||||
is($ap_transaction->closed , 1 , "$testname: ap transaction skonto is closed");
|
||||
is($bt->invoice_amount , '113.05000' , "$testname: bt invoice amount was assigned");
|
||||
};
|
||||
691a1293 | Geoffrey Richardson | 1;
|