1 use Test::More tests => 291;
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);
35 my ($ar_chart,$bank,$ar_amount_chart, $ap_chart, $ap_amount_chart);
36 my ($ar_transaction, $ap_transaction);
37 my ($dt, $dt_5, $dt_10, $year);
41 SL::DB::Manager::BankTransactionAccTrans->delete_all(all => 1);
42 SL::DB::Manager::BankTransaction->delete_all(all => 1);
43 SL::DB::Manager::InvoiceItem->delete_all(all => 1);
44 SL::DB::Manager::InvoiceItem->delete_all(all => 1);
45 SL::DB::Manager::Invoice->delete_all(all => 1);
46 SL::DB::Manager::PurchaseInvoice->delete_all(all => 1);
47 SL::DB::Manager::Part->delete_all(all => 1);
48 SL::DB::Manager::Customer->delete_all(all => 1);
49 SL::DB::Manager::Vendor->delete_all(all => 1);
50 SL::DB::Manager::SepaExportItem->delete_all(all => 1);
51 SL::DB::Manager::SepaExport->delete_all(all => 1);
52 SL::DB::Manager::BankAccount->delete_all(all => 1);
53 SL::DB::Manager::PaymentTerm->delete_all(all => 1);
54 SL::DB::Manager::Currency->delete_all(where => [ name => 'CUR' ]);
55 # SL::DB::Manager::Default->delete_all(all => 1);
60 sub save_btcontroller_to_string {
62 open(my $outputFH, '>', \$output) or die;
63 my $oldFH = select $outputFH;
65 $bt_controller = SL::Controller::BankTransaction->new;
66 $bt_controller->action_save_invoices;
74 Support::TestSetup::login();
77 reset_state(); # initialise customers/vendors/bank/currency/...
81 test_overpayment_with_partialpayment();
86 test_partial_payment();
88 test_ap_transaction();
89 test_neg_ap_transaction(invoice => 0);
90 test_neg_ap_transaction(invoice => 1);
91 test_ap_payment_transaction();
92 test_ap_payment_part_transaction();
93 test_neg_sales_invoice();
94 test_two_neg_ap_transaction();
95 test_one_inv_and_two_invoices_with_skonto_exact();
97 test_full_workflow_ar_multiple_inv_skonto_reconciliate_and_undo();
104 test_two_banktransactions();
105 # remove all created data at end of test
111 ###### functions for setting up data
116 $params{$_} ||= {} for qw(unit customer tax vendor);
120 $year = DateTime->today_local->year;
121 $year = 2019 if $year == 2020; # use year 2019 in 2020, because of tax rate change in Germany
122 $dt = DateTime->new(year => $year, month => 1, day => 12);
123 $dt_5 = $dt->clone->add(days => 5);
124 $dt_10 = $dt->clone->add(days => 10);
126 $tax = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} }) || croak "No tax";
127 $tax7 = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07) || croak "No tax for 7\%";
128 $tax_9 = SL::DB::Manager::Tax->find_by(taxkey => 9, rate => 0.19, %{ $params{tax} }) || croak "No tax for 19\%";
129 $tax0 = SL::DB::Manager::Tax->find_by(taxkey => 0, rate => 0.0) || croak "No tax for 0\%";
131 $currency_id = $::instance_conf->get_currency_id;
133 $bank_account = SL::DB::BankAccount->new(
134 account_number => '123',
139 chart_id => SL::DB::Manager::Chart->find_by(description => 'Bank')->id,
140 name => SL::DB::Manager::Chart->find_by(description => 'Bank')->description,
143 $customer = new_customer(
144 name => 'Test Customer OLÉ S.L. Årdbärg AB',
145 iban => 'DE12500105170648489890',
147 account_number => '648489890',
148 mandate_date_of_signature => $dt,
149 mandator_id => 'foobar',
151 bank_code => 'G1235',
152 depositor => 'Test Customer',
153 customernumber => 'CUST1704',
156 $payment_terms = create_payment_terms();
158 $vendor = new_vendor(
159 name => 'Test Vendor',
160 payment_id => $payment_terms->id,
161 iban => 'DE12500105170648489890',
163 account_number => '648489890',
165 bank_code => 'G1235',
166 depositor => 'Test Vendor',
167 vendornumber => 'VEND1704',
170 $ar_chart = SL::DB::Manager::Chart->find_by( accno => '1400' ); # Forderungen
171 $ap_chart = SL::DB::Manager::Chart->find_by( accno => '1600' ); # Verbindlichkeiten
172 $bank = SL::DB::Manager::Chart->find_by( accno => '1200' ); # Bank
173 $ar_amount_chart = SL::DB::Manager::Chart->find_by( accno => '8400' ); # Erlöse
174 $ap_amount_chart = SL::DB::Manager::Chart->find_by( accno => '3400' ); # Wareneingang 19%
178 sub test_ar_transaction {
180 my $netamount = $params{amount} || 100;
181 my $amount = $::form->round_amount($netamount * 1.19,2);
182 my $invoice = SL::DB::Invoice->new(
184 invnumber => $params{invnumber} || undef, # let it use its own invnumber
186 netamount => $netamount,
188 taxincluded => $params{taxincluded } || 0,
189 customer_id => $customer->id,
190 taxzone_id => $customer->taxzone_id,
191 currency_id => $currency_id,
193 payment_id => $params{payment_id} || undef,
194 notes => 'test_ar_transaction',
196 $invoice->add_ar_amount_row(
197 amount => $invoice->netamount,
198 chart => $ar_amount_chart,
199 tax_id => $params{tax_id} || $tax->id,
202 $invoice->create_ar_row(chart => $ar_chart);
205 is($invoice->currency_id , $currency_id , 'currency_id has been saved');
206 is($invoice->netamount , $netamount , 'ar amount has been converted');
207 is($invoice->amount , $amount , 'ar amount has been converted');
208 is($invoice->taxincluded , 0 , 'ar transaction doesn\'t have taxincluded');
210 if ( $netamount == 100 ) {
211 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');
212 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');
217 sub test_ap_transaction {
219 my $testname = 'test_ap_transaction';
222 my $amount = $::form->round_amount($netamount * 1.19,2);
223 my $invoice = SL::DB::PurchaseInvoice->new(
225 invnumber => $params{invnumber} || $testname,
227 netamount => $netamount,
230 vendor_id => $vendor->id,
231 taxzone_id => $vendor->taxzone_id,
232 currency_id => $currency_id,
234 notes => 'test_ap_transaction',
236 $invoice->add_ap_amount_row(
237 amount => $invoice->netamount,
238 chart => $ap_amount_chart,
239 tax_id => $params{tax_id} || $tax_9->id,
242 $invoice->create_ap_row(chart => $ap_chart);
245 is($invoice->currency_id , $currency_id , "$testname: currency_id has been saved");
246 is($invoice->netamount , 100 , "$testname: ap amount has been converted");
247 is($invoice->amount , 119 , "$testname: ap amount has been converted");
248 is($invoice->taxincluded , 0 , "$testname: ap transaction doesn\'t have taxincluded");
250 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');
251 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');
260 my $testname = 'test1';
262 $ar_transaction = test_ar_transaction(invnumber => 'salesinv1');
264 my $bt = create_bank_transaction(record => $ar_transaction,
266 valutadate => $dt) or die "Couldn't create bank_transaction";
268 $::form->{invoice_ids} = {
269 $bt->id => [ $ar_transaction->id ]
272 save_btcontroller_to_string();
274 $ar_transaction->load;
276 is($ar_transaction->paid , '119.00000' , "$testname: salesinv1 was paid");
277 is($ar_transaction->closed , 1 , "$testname: salesinv1 is closed");
278 is($bt->invoice_amount , '119.00000' , "$testname: bt invoice amount was assigned");
282 sub test_skonto_exact {
284 my $testname = 'test_skonto_exact';
286 $ar_transaction = test_ar_transaction(invnumber => 'salesinv skonto',
287 payment_id => $payment_terms->id,
290 my $bt = create_bank_transaction(record => $ar_transaction,
291 bank_chart_id => $bank->id,
294 amount => $ar_transaction->amount_less_skonto
295 ) or die "Couldn't create bank_transaction";
297 $::form->{invoice_ids} = {
298 $bt->id => [ $ar_transaction->id ]
300 $::form->{invoice_skontos} = {
301 $bt->id => [ 'with_skonto_pt' ]
304 save_btcontroller_to_string();
306 $ar_transaction->load;
308 is($ar_transaction->paid , '119.00000' , "$testname: salesinv skonto was paid");
309 is($ar_transaction->closed , 1 , "$testname: salesinv skonto is closed");
310 is($bt->invoice_amount , '113.05000' , "$testname: bt invoice amount was assigned");
316 my $testname = 'test_rollback_error';
317 # without type with_free_skonto the helper function (Payment.pm) looks ugly but not
320 $ar_transaction = test_ar_transaction(invnumber => 'salesinv skonto',
321 payment_id => $payment_terms->id,
323 amount => 168.58 / 1.19,
326 my $bt = create_bank_transaction(record => $ar_transaction,
327 bank_chart_id => $bank->id,
331 ) or die "Couldn't create bank_transaction";
332 $::form->{invoice_ids} = {
333 $bt->id => [ $ar_transaction->id ]
335 $::form->{invoice_skontos} = {
336 $bt->id => [ 'with_skonto_pt' ]
339 is($ar_transaction->paid , '0' , "$testname: salesinv is not paid");
341 # generate an error for testing rollback mechanism
342 my $saved_skonto_sales_chart_id = $tax->skonto_sales_chart_id;
343 $tax->skonto_sales_chart_id(undef);
346 save_btcontroller_to_string();
347 my @bt_errors = @{ $bt_controller->problems };
348 is(substr($bt_errors[0]->{message},0,38), 'Kein Skontokonto für Steuerschlüssel 3', "$testname: Fehlermeldung ok");
350 $tax->skonto_sales_chart_id($saved_skonto_sales_chart_id);
353 $ar_transaction->load;
355 is($ar_transaction->paid , '0.00000' , "$testname: salesinv was not paid");
356 is($bt->invoice_amount , '0.00000' , "$testname: bt invoice amount was not assigned");
360 sub test_two_invoices {
362 my $testname = 'test_two_invoices';
364 my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv_1');
365 my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv_2');
367 my $bt = create_bank_transaction(record => $ar_transaction_1,
368 amount => ($ar_transaction_1->amount + $ar_transaction_2->amount),
369 purpose => "Rechnungen " . $ar_transaction_1->invnumber . " und " . $ar_transaction_2->invnumber,
372 bank_chart_id => $bank->id,
373 ) or die "Couldn't create bank_transaction";
375 my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($ar_transaction_1);
376 is($agreement, 16, "points for ar_transaction_1 in test_two_invoices ok");
378 $::form->{invoice_ids} = {
379 $bt->id => [ $ar_transaction_1->id, $ar_transaction_2->id ]
382 save_btcontroller_to_string();
384 $ar_transaction_1->load;
385 $ar_transaction_2->load;
388 is($ar_transaction_1->paid , '119.00000' , "$testname: salesinv_1 wcsv_import_reportsas paid");
389 is($ar_transaction_1->closed , 1 , "$testname: salesinv_1 is closed");
390 is($ar_transaction_2->paid , '119.00000' , "$testname: salesinv_2 was paid");
391 is($ar_transaction_2->closed , 1 , "$testname: salesinv_2 is closed");
392 is($bt->invoice_amount , '238.00000' , "$testname: bt invoice amount was assigned");
396 sub test_one_inv_and_two_invoices_with_skonto_exact {
398 my $testname = 'test_two_invoices_with_skonto_exact';
400 my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv 1 skonto',
401 payment_id => $payment_terms->id,
403 my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv 2 skonto',
404 payment_id => $payment_terms->id,
406 my $ar_transaction_3 = test_ar_transaction(invnumber => 'salesinv 3 no skonto');
410 my $bt = create_bank_transaction(record => $ar_transaction_1,
411 bank_chart_id => $bank->id,
414 amount => $ar_transaction_1->amount_less_skonto * 2 + $ar_transaction_3->amount
415 ) or die "Couldn't create bank_transaction";
417 $::form->{invoice_ids} = {
418 $bt->id => [ $ar_transaction_1->id, $ar_transaction_3->id, $ar_transaction_2->id]
420 $::form->{invoice_skontos} = {
421 $bt->id => [ 'with_skonto_pt', 'without_skonto', 'with_skonto_pt' ]
424 save_btcontroller_to_string();
426 $ar_transaction_1->load;
427 $ar_transaction_2->load;
428 $ar_transaction_3->load;
429 my $skonto_1 = SL::DB::Manager::AccTransaction->find_by(trans_id => $ar_transaction_1->id, chart_id => 162);
430 my $skonto_2 = SL::DB::Manager::AccTransaction->find_by(trans_id => $ar_transaction_2->id, chart_id => 162);
432 is($skonto_1->amount , '-5.95000' , "$testname: salesinv 1 skonto was booked");
433 is($skonto_2->amount , '-5.95000' , "$testname: salesinv 2 skonto was booked");
434 is($ar_transaction_1->paid , '119.00000' , "$testname: salesinv 1 was paid");
435 is($ar_transaction_2->paid , '119.00000' , "$testname: salesinv 2 was paid");
436 is($ar_transaction_3->paid , '119.00000' , "$testname: salesinv 3 was paid");
437 is($ar_transaction_1->closed , 1 , "$testname: salesinv 1 skonto is closed");
438 is($ar_transaction_2->closed , 1 , "$testname: salesinv 2 skonto is closed");
439 is($ar_transaction_3->closed , 1 , "$testname: salesinv 2 skonto is closed");
440 is($bt->invoice_amount , '345.10000' , "$testname: bt invoice amount was assigned");
444 sub test_overpayment {
446 my $testname = 'test_overpayment';
448 $ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid');
451 my $bt = create_bank_transaction(record => $ar_transaction,
452 bank_chart_id => $bank->id,
456 ) or die "Couldn't create bank_transaction";
458 $::form->{invoice_ids} = {
459 $bt->id => [ $ar_transaction->id ]
462 save_btcontroller_to_string();
464 $ar_transaction->load;
467 is($ar_transaction->paid , '119.00000' , "$testname: 'salesinv overpaid' was not overpaid");
468 is($bt->invoice_amount , '119.00000' , "$testname: bt invoice amount was not fully assigned with the overpaid amount");
469 { local $TODO = 'this currently fails because closed ignores over-payments, see commit d90966c7';
470 is($ar_transaction->closed , 0 , "$testname: 'salesinv overpaid' is open (via 'closed' method')");
472 is($ar_transaction->open_amount == 0 ? 1 : 0 , 1 , "$testname: 'salesinv overpaid is closed (via amount-paid)");
476 sub test_overpayment_with_partialpayment {
478 # two payments on different days, 10 and 119. If there is only one invoice we
479 # don't want it to be overpaid.
480 my $testname = 'test_overpayment_with_partialpayment';
482 $ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid partial');
484 my $bt_1 = create_bank_transaction(record => $ar_transaction,
485 bank_chart_id => $bank->id,
489 ) or die "Couldn't create bank_transaction";
490 my $bt_2 = create_bank_transaction(record => $ar_transaction,
494 bank_chart_id => $bank->id,
495 ) or die "Couldn't create bank_transaction";
497 $::form->{invoice_ids} = {
498 $bt_1->id => [ $ar_transaction->id ]
500 save_btcontroller_to_string();
503 is($bt_1->invoice_amount , '10.00000' , "$testname: bt_1 invoice amount was fully assigned");
504 $::form->{invoice_ids} = {
505 $bt_2->id => [ $ar_transaction->id ]
507 save_btcontroller_to_string();
509 $ar_transaction->load;
512 is($bt_1->invoice_amount , '10.00000' , "$testname: bt_1 invoice amount was fully assigned");
513 is($ar_transaction->paid , '119.00000' , "$testname: 'salesinv overpaid partial' was not overpaid");
514 is($bt_2->invoice_amount , '109.00000' , "$testname: bt_2 invoice amount was partly assigned");
518 sub test_partial_payment {
520 my $testname = 'test_partial_payment';
522 $ar_transaction = test_ar_transaction(invnumber => 'salesinv partial payment');
525 my $bt = create_bank_transaction(record => $ar_transaction,
526 bank_chart_id => $bank->id,
530 ) or die "Couldn't create bank_transaction";
532 $::form->{invoice_ids} = {
533 $bt->id => [ $ar_transaction->id ]
536 save_btcontroller_to_string();
538 $ar_transaction->load;
541 is($ar_transaction->paid , '100.00000' , "$testname: 'salesinv partial payment' was partially paid");
542 is($bt->invoice_amount , '100.00000' , "$testname: bt invoice amount was assigned partially paid amount");
544 # addon test partial payment full point match
545 my $bt2 = create_bank_transaction(record => $ar_transaction,
546 bank_chart_id => $bank->id,
550 ) or die "Couldn't create bank_transaction";
552 my ($agreement, $rule_matches) = $bt2->get_agreement_with_invoice($ar_transaction);
553 is($agreement, 15, "points for exact partial payment ok");
554 is($rule_matches, 'remote_account_number(3) exact_open_amount(4) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus0(3) ', "rules_matches for exact partial payment ok");
557 sub test_full_workflow_ar_multiple_inv_skonto_reconciliate_and_undo {
559 my $testname = 'test_partial_payment';
561 $ar_transaction = test_ar_transaction(invnumber => 'salesinv partial payment two');
562 my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv 2 22d2', amount => 22);
564 # amount 299.29 > 119
565 my $bt = create_bank_transaction(record => $ar_transaction,
566 bank_chart_id => $bank->id,
568 ) or die "Couldn't create bank_transaction";
570 $::form->{invoice_ids} = {
571 $bt->id => [ $ar_transaction->id ]
574 save_btcontroller_to_string();
576 $ar_transaction->load;
579 is($ar_transaction->paid , '119.00000' , "$testname: 'salesinv partial payment' was fully paid");
580 is($bt->invoice_amount , '119.00000' , "$testname: bt invoice amount was assigned partially paid amount");
581 is($bt->amount , '299.29000' , "$testname: bt amount is stil there");
582 # next invoice, same bank transaction
583 $::form->{invoice_ids} = {
584 $bt->id => [ $ar_transaction_2->id ]
587 save_btcontroller_to_string();
589 $ar_transaction_2->load;
591 is($ar_transaction_2->paid , '26.18000' , "$testname: 'salesinv partial payment' was fully paid");
592 is($bt->invoice_amount , '145.18000' , "$testname: bt invoice amount was assigned partially paid amount");
593 is($bt->amount , '299.29000' , "$testname: bt amount is stil there");
594 is(scalar @{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )}, 4, "$testname 4 acc_trans entries created");
596 # now check all 4 entries done so far and save paid acc_trans_ids for later use with reconcile
597 foreach my $acc_trans_id_entry (@{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )}) {
598 isnt($acc_trans_id_entry->ar_id, undef, "$testname: bt linked with acc_trans and trans_id set");
599 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 ]);
600 is (ref $rl->[0], 'SL::DB::RecordLink', "$testname record link created");
601 my $acc_trans = SL::DB::Manager::AccTransaction->get_all(where => [acc_trans_id => $acc_trans_id_entry->acc_trans_id]);
602 foreach my $entry (@{ $acc_trans }) {
603 like(abs($entry->amount), qr/(119|26.18)/, "$testname: abs amount correct");
604 like($entry->chart_link, qr/(paid|AR)/, "$testname chart_link correct");
605 push @{ $::form->{bb_ids} }, $entry->acc_trans_id if $entry->chart_link =~ m/paid/;
608 # great we need one last booking to clear the whole bank transaction - we include skonto
609 my $ar_transaction_skonto = test_ar_transaction(invnumber => 'salesinv skonto last case',
610 payment_id => $payment_terms->id,
614 $::form->{invoice_ids} = {
615 $bt->id => [ $ar_transaction_skonto->id ]
617 $::form->{invoice_skontos} = {
618 $bt->id => [ 'with_skonto_pt' ]
621 save_btcontroller_to_string();
623 $ar_transaction_skonto->load;
625 is($ar_transaction_skonto->paid , '162.22000' , "$testname: 'salesinv skonto fully paid");
626 is($bt->invoice_amount , '299.29000' , "$testname: bt invoice amount was assigned partially paid amount");
627 is($bt->amount , '299.29000' , "$testname: bt amount is stil there");
628 is(scalar @{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )},
629 7, "$testname 7 acc_trans entries created");
631 # same loop as above, but only for the 3rd ar_id
632 foreach my $acc_trans_id_entry (@{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [ar_id => $ar_transaction_skonto->id ] )}) {
633 isnt($acc_trans_id_entry->ar_id, '', "$testname: bt linked with acc_trans and trans_id set");
634 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 ]);
635 is (ref $rl->[0], 'SL::DB::RecordLink', "$testname record link created");
636 my $acc_trans = SL::DB::Manager::AccTransaction->get_all(where => [acc_trans_id => $acc_trans_id_entry->acc_trans_id]);
637 foreach my $entry (@{ $acc_trans }) {
638 like($entry->chart_link, qr/(paid|AR)/, "$testname chart_link correct");
639 is ($entry->amount, '162.22000', "$testname full amont") if $entry->chart_link eq 'AR'; # full amount
640 like(abs($entry->amount), qr/(154.11|8.11)/, "$testname: abs amount correct") if $entry->chart_link =~ m/paid/;
641 push @{ $::form->{bb_ids} }, $entry->acc_trans_id if ($entry->chart_link =~ m/paid/ && $entry->amount == -154.11);
644 # done, now reconciliate all bookings
645 $::form->{bt_ids} = [ $bt->id ];
646 my $rec_controller = SL::Controller::Reconciliation->new;
647 my @errors = $rec_controller->_get_elements_and_validate;
649 is (scalar @errors, 0, "$testname unsuccesfull reconciliation with error: " . Dumper(@errors));
650 $rec_controller->_reconcile;
653 # and check the cleared state of bt and the acc_transactions
654 is($bt->cleared, '1' , "$testname: bt cleared");
655 foreach (@{ $::form->{bb_ids} }) {
656 my $acc_trans = SL::DB::Manager::AccTransaction->find_by(acc_trans_id => $_);
657 is($acc_trans->cleared, '1' , "$testname: acc_trans entry cleared");
659 # now, this was a really bad idea and in general a major mistake. better undo and redo the whole bank transactions
661 $::form->{ids} = [ $bt->id ];
662 $bt_controller = SL::Controller::BankTransaction->new;
663 $bt_controller->action_unlink_bank_transaction('testcase' => 1);
667 # and check the cleared state of bt and the acc_transactions
668 is($bt->cleared, '0' , "$testname: bt undo cleared");
669 is($bt->invoice_amount, '0.00000' , "$testname: bt undo invoice amount");
670 foreach (@{ $::form->{bb_ids} }) {
671 my $acc_trans = SL::DB::Manager::AccTransaction->find_by(acc_trans_id => $_);
672 is($acc_trans, undef , "$testname: cleared acc_trans entry completely removed");
674 # this was for data integrity for reconcile, now all the other options
675 is(scalar @{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )},
676 0, "$testname 7 acc_trans entries deleted");
677 my $rl = SL::DB::Manager::RecordLink->get_all(where => [ from_id => $bt->id, from_table => 'bank_transactions' ]);
678 is (ref $rl->[0], '', "$testname record link removed");
679 # double safety and check ar.paid
680 # load all three invoices and check for paid-link via acc_trans and paid in general
682 $ar_transaction->load;
683 $ar_transaction_2->load;
684 $ar_transaction_skonto->load;
686 is(scalar @{ SL::DB::Manager::AccTransaction->get_all(
687 where => [ trans_id => $ar_transaction->id, chart_link => { like => '%paid%' } ])},
688 0, "$testname no more paid entries in acc_trans for ar_transaction");
689 is(scalar @{ SL::DB::Manager::AccTransaction->get_all(
690 where => [ trans_id => $ar_transaction_2->id, chart_link => { like => '%paid%' } ])},
691 0, "$testname no more paid entries in acc_trans for ar_transaction_2");
692 is(scalar @{ SL::DB::Manager::AccTransaction->get_all(
693 where => [ trans_id => $ar_transaction_skonto->id, chart_link => { like => '%paid%' } ])},
694 0, "$testname no more paid entries in acc_trans for ar_transaction_skonto");
696 is($ar_transaction->paid , '0.00000' , "$testname: 'salesinv fully unpaid");
697 is($ar_transaction_2->paid , '0.00000' , "$testname: 'salesinv 2 fully unpaid");
698 is($ar_transaction_skonto->paid , '0.00000' , "$testname: 'salesinv skonto fully unpaid");
700 # whew. w(h)a(n)t a whole lotta test
704 sub test_credit_note {
706 my $testname = 'test_credit_note';
708 my $part1 = new_part( partnumber => 'T4254')->save;
709 my $part2 = new_service(partnumber => 'Serv1')->save;
710 my $credit_note = create_credit_note(
712 customer => $customer,
715 invoiceitems => [ create_invoice_item(part => $part1, qty => 3, sellprice => 70),
716 create_invoice_item(part => $part2, qty => 10, sellprice => 50),
719 my $bt = create_bank_transaction(record => $credit_note,
720 amount => $credit_note->amount,
721 bank_chart_id => $bank->id,
724 my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($credit_note);
725 is($agreement, 14, "points for credit note ok");
726 is($rule_matches, 'remote_account_number(3) exact_amount(4) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus14(2) ', "rules_matches for credit note ok");
728 $::form->{invoice_ids} = {
729 $bt->id => [ $credit_note->id ]
732 save_btcontroller_to_string();
736 is($credit_note->amount , '-844.90000', "$testname: amount ok");
737 is($credit_note->netamount, '-710.00000', "$testname: netamount ok");
738 is($credit_note->paid , '-844.90000', "$testname: paid ok");
741 sub test_neg_ap_transaction {
743 my $testname = 'test_neg_ap_transaction' . $params{invoice} ? ' invoice booking' : ' credit booking';
745 my $amount = $::form->round_amount($netamount * 1.19,2);
746 my $invoice = SL::DB::PurchaseInvoice->new(
747 invoice => $params{invoice} // 0,
748 invnumber => $params{invnumber} || 'test_neg_ap_transaction',
750 netamount => $netamount,
753 vendor_id => $vendor->id,
754 taxzone_id => $vendor->taxzone_id,
755 currency_id => $currency_id,
757 notes => 'test_neg_ap_transaction',
759 $invoice->add_ap_amount_row(
760 amount => $invoice->netamount,
761 chart => $ap_amount_chart,
762 tax_id => $tax_9->id,
765 $invoice->create_ap_row(chart => $ap_chart);
768 is($invoice->netamount, -20 , "$testname: netamount ok");
769 is($invoice->amount , -23.8, "$testname: amount ok");
771 my $bt = create_bank_transaction(record => $invoice,
772 amount => $invoice->amount,
773 bank_chart_id => $bank->id,
777 my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($invoice);
778 is($agreement, 16, "points for negative ap transaction ok");
780 $::form->{invoice_ids} = {
781 $bt->id => [ $invoice->id ]
784 save_btcontroller_to_string();
789 is($invoice->amount , '-23.80000', "$testname: amount ok");
790 is($invoice->netamount, '-20.00000', "$testname: netamount ok");
791 is($invoice->paid , '-23.80000', "$testname: paid ok");
792 is($bt->invoice_amount, '23.80000', "$testname: bt invoice amount for ap was assigned");
793 is($bt->amount, '23.80000', "$testname: bt amount for ap was assigned");
797 sub test_two_neg_ap_transaction {
798 my $testname='test_two_neg_ap_transaction';
800 my $amount = $::form->round_amount($netamount * 1.19,2);
801 my $invoice = SL::DB::PurchaseInvoice->new(
803 invnumber => 'test_neg_ap_transaction',
805 netamount => $netamount,
808 vendor_id => $vendor->id,
809 taxzone_id => $vendor->taxzone_id,
810 currency_id => $currency_id,
812 notes => 'test_neg_ap_transaction',
814 $invoice->add_ap_amount_row(
815 amount => $invoice->netamount,
816 chart => $ap_amount_chart,
817 tax_id => $tax_9->id,
820 $invoice->create_ap_row(chart => $ap_chart);
823 is($invoice->netamount, -20 , "$testname: netamount ok");
824 is($invoice->amount , -23.8, "$testname: amount ok");
826 my $netamount_two = -1.14;
827 my $amount_two = $::form->round_amount($netamount_two * 1.19,2);
828 my $invoice_two = SL::DB::PurchaseInvoice->new(
830 invnumber => 'test_neg_ap_transaction_two',
831 amount => $amount_two,
832 netamount => $netamount_two,
835 vendor_id => $vendor->id,
836 taxzone_id => $vendor->taxzone_id,
837 currency_id => $currency_id,
839 notes => 'test_neg_ap_transaction_two',
841 $invoice_two->add_ap_amount_row(
842 amount => $invoice_two->netamount,
843 chart => $ap_amount_chart,
844 tax_id => $tax_9->id,
847 $invoice_two->create_ap_row(chart => $ap_chart);
850 is($invoice_two->netamount, -1.14 , "$testname: netamount ok");
851 is($invoice_two->amount , -1.36, "$testname: amount ok");
854 my $bt = create_bank_transaction(record => $invoice_two,
855 amount => $invoice_two->amount + $invoice->amount,
856 bank_chart_id => $bank->id,
859 # my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($invoice_two);
860 # is($agreement, 15, "points for negative ap transaction ok");
862 $::form->{invoice_ids} = {
863 $bt->id => [ $invoice->id, $invoice_two->id ]
866 save_btcontroller_to_string();
872 is($invoice->amount , '-23.80000', "$testname: first inv amount ok");
873 is($invoice->netamount, '-20.00000', "$testname: first inv netamount ok");
874 is($invoice->paid , '-23.80000', "$testname: first inv paid ok");
875 is($invoice_two->amount , '-1.36000', "$testname: second inv amount ok");
876 is($invoice_two->netamount, '-1.14000', "$testname: second inv netamount ok");
877 is($invoice_two->paid , '-1.36000', "$testname: second inv paid ok");
878 is($bt->invoice_amount, '25.16000', "$testname: bt invoice amount for both invoices were assigned");
881 return ($invoice, $invoice_two);
884 sub test_ap_payment_transaction {
886 my $testname = 'test_ap_payment_transaction';
888 my $amount = $::form->round_amount($netamount * 1.19,2);
889 my $invoice = SL::DB::PurchaseInvoice->new(
891 invnumber => $params{invnumber} || $testname,
893 netamount => $netamount,
896 vendor_id => $vendor->id,
897 taxzone_id => $vendor->taxzone_id,
898 currency_id => $currency_id,
902 $invoice->add_ap_amount_row(
903 amount => $invoice->netamount,
904 chart => $ap_amount_chart,
905 tax_id => $tax_9->id,
908 $invoice->create_ap_row(chart => $ap_chart);
911 is($invoice->netamount, 115 , "$testname: netamount ok");
912 is($invoice->amount , 136.85, "$testname: amount ok");
914 my $bt = create_bank_transaction(record => $invoice,
915 amount => $invoice->amount,
916 bank_chart_id => $bank->id,
919 $::form->{invoice_ids} = {
920 $bt->id => [ $invoice->id ]
923 save_btcontroller_to_string();
928 is($invoice->amount , '136.85000', "$testname: amount ok");
929 is($invoice->netamount, '115.00000', "$testname: netamount ok");
930 is($bt->amount, '-136.85000', "$testname: bt amount ok");
931 is($invoice->paid , '136.85000', "$testname: paid ok");
932 is($bt->invoice_amount, '-136.85000', "$testname: bt invoice amount for ap was assigned");
937 sub test_ap_payment_part_transaction {
939 my $testname = 'test_ap_payment_p_transaction';
941 my $amount = $::form->round_amount($netamount * 1.19,2);
942 my $invoice = SL::DB::PurchaseInvoice->new(
944 invnumber => $params{invnumber} || $testname,
946 netamount => $netamount,
949 vendor_id => $vendor->id,
950 taxzone_id => $vendor->taxzone_id,
951 currency_id => $currency_id,
955 $invoice->add_ap_amount_row(
956 amount => $invoice->netamount,
957 chart => $ap_amount_chart,
958 tax_id => $tax_9->id,
961 $invoice->create_ap_row(chart => $ap_chart);
964 is($invoice->netamount, 115 , "$testname: netamount ok");
965 is($invoice->amount , 136.85, "$testname: amount ok");
967 my $bt = create_bank_transaction(record => $invoice,
968 amount => $invoice->amount-100,
969 bank_chart_id => $bank->id,
972 $::form->{invoice_ids} = {
973 $bt->id => [ $invoice->id ]
976 save_btcontroller_to_string();
981 is($invoice->amount , '136.85000', "$testname: amount ok");
982 is($invoice->netamount, '115.00000', "$testname: netamount ok");
983 is($bt->amount, '-36.85000', "$testname: bt amount ok");
984 is($invoice->paid , '36.85000', "$testname: paid ok");
985 is($bt->invoice_amount, '-36.85000', "$testname: bt invoice amount for ap was assigned");
987 my $bt2 = create_bank_transaction(record => $invoice,
989 bank_chart_id => $bank->id,
992 $::form->{invoice_ids} = {
993 $bt2->id => [ $invoice->id ]
996 save_btcontroller_to_string();
1000 is($invoice->amount , '136.85000', "$testname: amount ok");
1001 is($invoice->netamount, '115.00000', "$testname: netamount ok");
1002 is($bt2->amount, '-100.00000',"$testname: bt amount ok");
1003 is($invoice->paid , '136.85000', "$testname: paid ok");
1004 is($bt2->invoice_amount,'-100.00000', "$testname: bt invoice amount for ap was assigned");
1009 sub test_neg_sales_invoice {
1011 my $testname = 'test_neg_sales_invoice';
1013 my $part1 = new_part( partnumber => 'Funkenhaube öhm')->save;
1014 my $part2 = new_service(partnumber => 'Service-Pauschale Pasch!')->save;
1016 my $neg_sales_inv = create_sales_invoice(
1017 invnumber => '20172201',
1018 customer => $customer,
1021 invoiceitems => [ create_invoice_item(part => $part1, qty => 3, sellprice => 70),
1022 create_invoice_item(part => $part2, qty => 10, sellprice => -50),
1025 my $bt = create_bank_transaction(record => $neg_sales_inv,
1026 amount => $neg_sales_inv->amount,
1027 bank_chart_id => $bank->id,
1031 $::form->{invoice_ids} = {
1032 $bt->id => [ $neg_sales_inv->id ]
1035 save_btcontroller_to_string();
1037 $neg_sales_inv->load;
1039 is($neg_sales_inv->amount , '-345.10000', "$testname: amount ok");
1040 is($neg_sales_inv->netamount, '-290.00000', "$testname: netamount ok");
1041 is($neg_sales_inv->paid , '-345.10000', "$testname: paid ok");
1042 is($bt->amount , '-345.10000', "$testname: bt amount ok");
1043 is($bt->invoice_amount , '-345.10000', "$testname: bt invoice_amount ok");
1048 my $testname = 'test_bt_rule1';
1050 $ar_transaction = test_ar_transaction(invnumber => 'bt_rule1', transdate => $dt);
1052 my $bt = create_bank_transaction(record => $ar_transaction, transdate => $dt) or die "Couldn't create bank_transaction";
1054 $ar_transaction->load;
1056 is($ar_transaction->paid , '0.00000' , "$testname: not paid");
1057 is($bt->invoice_amount , '0.00000' , "$testname: bt invoice amount was not assigned");
1059 my $bt_controller = SL::Controller::BankTransaction->new;
1060 my ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
1062 is(scalar(@$bt_transactions) , 1 , "$testname: one bank_transaction");
1063 is($bt_transactions->[0]->{agreement}, 20 , "$testname: agreement == 20");
1064 my $match = join ( ' ',@{$bt_transactions->[0]->{rule_matches}});
1065 #print "rule_matches='".$match."'\n";
1067 "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) ",
1068 "$testname: rule_matches ok");
1069 $bt->invoice_amount($bt->amount);
1071 is($bt->invoice_amount , '119.00000' , "$testname: bt invoice amount now set");
1074 sub test_sepa_export {
1076 my $testname = 'test_sepa_export';
1078 $ar_transaction = test_ar_transaction(invnumber => 'sepa1', transdate => $dt);
1080 my $bt = create_bank_transaction(record => $ar_transaction, transdate => $dt) or die "Couldn't create bank_transaction";
1081 my $se = create_sepa_export();
1082 my $sei = create_sepa_export_item(
1083 chart_id => $bank->id,
1084 ar_id => $ar_transaction->id,
1085 sepa_export_id => $se->id,
1086 vc_iban => $customer->iban,
1087 vc_bic => $customer->bic,
1088 vc_mandator_id => $customer->mandator_id,
1089 vc_depositor => $customer->depositor,
1090 amount => $ar_transaction->amount,
1092 require SL::SEPA::XML;
1093 my $sepa_xml = SL::SEPA::XML->new('company' => $customer->name,
1094 'creditor_id' => "id",
1095 'src_charset' => 'UTF-8',
1096 'message_id' => "test",
1100 is($sepa_xml->{company} , 'Test Customer OLE S.L. Ardbaerg AB');
1102 $ar_transaction->load;
1105 is($ar_transaction->paid , '0.00000' , "$testname: sepa1 not paid");
1106 is($bt->invoice_amount , '0.00000' , "$testname: bt invoice amount was not assigned");
1107 is($bt->amount , '119.00000' , "$testname: bt amount ok");
1108 is($sei->amount , '119.00000' , "$testname: sepa export amount ok");
1110 my $bt_controller = SL::Controller::BankTransaction->new;
1111 my ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
1113 is(scalar(@$bt_transactions) , 1 , "$testname: one bank_transaction");
1114 is($bt_transactions->[0]->{agreement}, 25 , "$testname: agreement == 25");
1115 my $match = join ( ' ',@{$bt_transactions->[0]->{rule_matches}});
1117 "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) ",
1118 "$testname: rule_matches ok");
1121 sub test_two_banktransactions {
1123 my $testname = 'two_banktransactions';
1125 my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv10000' , amount => 2912.00 );
1126 my $bt1 = create_bank_transaction(record => $ar_transaction_1,
1127 amount => $ar_transaction_1->amount,
1128 purpose => "Rechnung10000 beinahe",
1130 bank_chart_id => $bank->id,
1131 ) or die "Couldn't create bank_transaction";
1133 my $bt2 = create_bank_transaction(record => $ar_transaction_1,
1134 amount => $ar_transaction_1->amount + 0.01,
1135 purpose => "sicher salesinv20000 vielleicht",
1137 bank_chart_id => $bank->id,
1138 ) or die "Couldn't create bank_transaction";
1140 my ($agreement1, $rule_matches1) = $bt1->get_agreement_with_invoice($ar_transaction_1);
1141 is($agreement1, 19, "bt1 19 points for ar_transaction_1 in $testname ok");
1142 #print "rule_matches1=".$rule_matches1."\n";
1144 "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) ",
1145 "$testname: rule_matches ok");
1146 my ($agreement2, $rule_matches2) = $bt2->get_agreement_with_invoice($ar_transaction_1);
1147 is($agreement2, 11, "bt2 11 points for ar_transaction_1 in $testname ok");
1149 "remote_account_number(3) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus0(3) ",
1150 "$testname: rule_matches ok");
1152 my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv20000' , amount => 2912.01 );
1153 my $ar_transaction_3 = test_ar_transaction(invnumber => 'zweitemit10000', amount => 2912.00 );
1154 ($agreement1, $rule_matches1) = $bt1->get_agreement_with_invoice($ar_transaction_2);
1156 is($agreement1, 11, "bt1 11 points for ar_transaction_2 in $testname ok");
1158 ($agreement2, $rule_matches2) = $bt2->get_agreement_with_invoice($ar_transaction_2);
1159 is($agreement2, 20, "bt2 20 points for ar_transaction_2 in $testname ok");
1161 ($agreement2, $rule_matches2) = $bt2->get_agreement_with_invoice($ar_transaction_1);
1162 is($agreement2, 11, "bt2 11 points for ar_transaction_1 in $testname ok");
1164 my $bt3 = create_bank_transaction(record => $ar_transaction_3,
1165 amount => $ar_transaction_3->amount,
1166 purpose => "sicher Rechnung10000 vielleicht",
1168 bank_chart_id => $bank->id,
1169 ) or die "Couldn't create bank_transaction";
1171 my ($agreement3, $rule_matches3) = $bt3->get_agreement_with_invoice($ar_transaction_3);
1172 is($agreement3, 19, "bt3 19 points for ar_transaction_3 in $testname ok");
1175 $ar_transaction_2->delete;
1177 #nun sollten zwei gleichwertige Rechnungen $ar_transaction_1 und $ar_transaction_3 für $bt1 gefunden werden
1178 #aber es darf keine Proposals geben mit mehreren Rechnungen
1179 my $bt_controller = SL::Controller::BankTransaction->new;
1180 my ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
1182 is(scalar(@$bt_transactions) , 2 , "$testname: two bank_transaction");
1183 is(scalar(@$proposals) , 0 , "$testname: no proposals");
1185 $ar_transaction_3->delete;
1187 # Jetzt gibt es zwei Kontobewegungen mit gleichen Punkten für eine Rechnung.
1188 # hier darf es auch keine Proposals geben
1190 ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
1192 is(scalar(@$bt_transactions) , 2 , "$testname: two bank_transaction");
1193 # odyn testfall - anforderungen so (noch) nicht in kivi
1194 # is(scalar(@$proposals) , 0 , "$testname: no proposals");
1196 # Jetzt gibt es zwei Kontobewegungen für eine Rechnung.
1197 # eine Bewegung bekommt mehr Punkte
1198 # hier darf es auch keine Proposals geben
1199 $bt3->update_attributes( purpose => "fuer Rechnung salesinv10000");
1201 ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
1203 is(scalar(@$bt_transactions) , 2 , "$testname: two bank_transaction");
1204 # odyn testfall - anforderungen so (noch) nicht in kivi
1205 # is(scalar(@$proposals) , 1 , "$testname: one proposal");
1210 my $testname = 'closedto';
1212 my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv10000' , amount => 2912.00 );
1213 my $bt1 = create_bank_transaction(record => $ar_transaction_1,
1214 amount => $ar_transaction_1->amount,
1215 purpose => "Rechnung10000 beinahe",
1216 bank_chart_id => $bank->id,
1217 ) or die "Couldn't create bank_transaction";
1219 $bt1->valutadate(DateTime->new(year => 2019, month => 12, day => 30));
1222 is($bt1->closed_period, 0, "$testname undefined closedto");
1224 my $defaults = SL::DB::Manager::Default->get_all(limit => 1)->[0];
1225 $defaults->closedto(DateTime->new(year => 2019, month => 12, day => 31));
1227 $::instance_conf->reload->data;
1229 is($bt1->closed_period, 1, "$testname defined and next date closedto");
1231 $bt1->valutadate(DateTime->new(year => 2020, month => 1, day => 1));
1235 is($bt1->closed_period, 0, "$testname defined closedto and next date valuta");
1236 $defaults->closedto(undef);
1241 sub test_skonto_exact_ap_transaction {
1243 my $testname = 'test_skonto_exact_ap_transaction';
1245 $ap_transaction = test_ap_transaction(invnumber => 'ap transaction skonto',
1246 payment_id => $payment_terms->id,
1249 my $bt = create_bank_transaction(record => $ap_transaction,
1250 bank_chart_id => $bank->id,
1253 amount => $ap_transaction->amount_less_skonto
1254 ) or die "Couldn't create bank_transaction";
1256 $::form->{invoice_ids} = {
1257 $bt->id => [ $ap_transaction->id ]
1259 $::form->{invoice_skontos} = {
1260 $bt->id => [ 'with_skonto_pt' ]
1263 save_btcontroller_to_string();
1265 $ap_transaction->load;
1267 is($ap_transaction->paid , '119.00000' , "$testname: ap transaction skonto was paid");
1268 is($ap_transaction->closed , 1 , "$testname: ap transaction skonto is closed");
1269 is($bt->invoice_amount , '113.05000' , "$testname: bt invoice amount was assigned");