1 use Test::More tests => 293;
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 = $::form->round_amount($params{amount}, 2) || 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' ]
338 is($ar_transaction->netamount, $::form->round_amount(168.58/1.19, 2), "$testname: Net Amount assigned");
339 is($ar_transaction->amount, 168.58, "$testname: Amount assigned");
340 is($ar_transaction->paid , '0' , "$testname: salesinv is not paid");
342 # generate an error for testing rollback mechanism
343 my $saved_skonto_sales_chart_id = $tax->skonto_sales_chart_id;
344 $tax->skonto_sales_chart_id(undef);
347 save_btcontroller_to_string();
348 my @bt_errors = @{ $bt_controller->problems };
349 is(substr($bt_errors[0]->{message},0,38), 'Kein Skontokonto für Steuerschlüssel 3', "$testname: Fehlermeldung ok");
351 $tax->skonto_sales_chart_id($saved_skonto_sales_chart_id);
354 $ar_transaction->load;
356 is($ar_transaction->paid , '0.00000' , "$testname: salesinv was not paid");
357 is($bt->invoice_amount , '0.00000' , "$testname: bt invoice amount was not assigned");
361 sub test_two_invoices {
363 my $testname = 'test_two_invoices';
365 my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv_1');
366 my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv_2');
368 my $bt = create_bank_transaction(record => $ar_transaction_1,
369 amount => ($ar_transaction_1->amount + $ar_transaction_2->amount),
370 purpose => "Rechnungen " . $ar_transaction_1->invnumber . " und " . $ar_transaction_2->invnumber,
373 bank_chart_id => $bank->id,
374 ) or die "Couldn't create bank_transaction";
376 my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($ar_transaction_1);
377 is($agreement, 16, "points for ar_transaction_1 in test_two_invoices ok");
379 $::form->{invoice_ids} = {
380 $bt->id => [ $ar_transaction_1->id, $ar_transaction_2->id ]
383 save_btcontroller_to_string();
385 $ar_transaction_1->load;
386 $ar_transaction_2->load;
389 is($ar_transaction_1->paid , '119.00000' , "$testname: salesinv_1 wcsv_import_reportsas paid");
390 is($ar_transaction_1->closed , 1 , "$testname: salesinv_1 is closed");
391 is($ar_transaction_2->paid , '119.00000' , "$testname: salesinv_2 was paid");
392 is($ar_transaction_2->closed , 1 , "$testname: salesinv_2 is closed");
393 is($bt->invoice_amount , '238.00000' , "$testname: bt invoice amount was assigned");
397 sub test_one_inv_and_two_invoices_with_skonto_exact {
399 my $testname = 'test_two_invoices_with_skonto_exact';
401 my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv 1 skonto',
402 payment_id => $payment_terms->id,
404 my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv 2 skonto',
405 payment_id => $payment_terms->id,
407 my $ar_transaction_3 = test_ar_transaction(invnumber => 'salesinv 3 no skonto');
411 my $bt = create_bank_transaction(record => $ar_transaction_1,
412 bank_chart_id => $bank->id,
415 amount => $ar_transaction_1->amount_less_skonto * 2 + $ar_transaction_3->amount
416 ) or die "Couldn't create bank_transaction";
418 $::form->{invoice_ids} = {
419 $bt->id => [ $ar_transaction_1->id, $ar_transaction_3->id, $ar_transaction_2->id]
421 $::form->{invoice_skontos} = {
422 $bt->id => [ 'with_skonto_pt', 'without_skonto', 'with_skonto_pt' ]
425 save_btcontroller_to_string();
427 $ar_transaction_1->load;
428 $ar_transaction_2->load;
429 $ar_transaction_3->load;
430 my $skonto_1 = SL::DB::Manager::AccTransaction->find_by(trans_id => $ar_transaction_1->id, chart_id => 162);
431 my $skonto_2 = SL::DB::Manager::AccTransaction->find_by(trans_id => $ar_transaction_2->id, chart_id => 162);
433 is($skonto_1->amount , '-5.95000' , "$testname: salesinv 1 skonto was booked");
434 is($skonto_2->amount , '-5.95000' , "$testname: salesinv 2 skonto was booked");
435 is($ar_transaction_1->paid , '119.00000' , "$testname: salesinv 1 was paid");
436 is($ar_transaction_2->paid , '119.00000' , "$testname: salesinv 2 was paid");
437 is($ar_transaction_3->paid , '119.00000' , "$testname: salesinv 3 was paid");
438 is($ar_transaction_1->closed , 1 , "$testname: salesinv 1 skonto is closed");
439 is($ar_transaction_2->closed , 1 , "$testname: salesinv 2 skonto is closed");
440 is($ar_transaction_3->closed , 1 , "$testname: salesinv 2 skonto is closed");
441 is($bt->invoice_amount , '345.10000' , "$testname: bt invoice amount was assigned");
445 sub test_overpayment {
447 my $testname = 'test_overpayment';
449 $ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid');
452 my $bt = create_bank_transaction(record => $ar_transaction,
453 bank_chart_id => $bank->id,
457 ) or die "Couldn't create bank_transaction";
459 $::form->{invoice_ids} = {
460 $bt->id => [ $ar_transaction->id ]
463 save_btcontroller_to_string();
465 $ar_transaction->load;
468 is($ar_transaction->paid , '119.00000' , "$testname: 'salesinv overpaid' was not overpaid");
469 is($bt->invoice_amount , '119.00000' , "$testname: bt invoice amount was not fully assigned with the overpaid amount");
470 { local $TODO = 'this currently fails because closed ignores over-payments, see commit d90966c7';
471 is($ar_transaction->closed , 0 , "$testname: 'salesinv overpaid' is open (via 'closed' method')");
473 is($ar_transaction->open_amount == 0 ? 1 : 0 , 1 , "$testname: 'salesinv overpaid is closed (via amount-paid)");
477 sub test_overpayment_with_partialpayment {
479 # two payments on different days, 10 and 119. If there is only one invoice we
480 # don't want it to be overpaid.
481 my $testname = 'test_overpayment_with_partialpayment';
483 $ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid partial');
485 my $bt_1 = create_bank_transaction(record => $ar_transaction,
486 bank_chart_id => $bank->id,
490 ) or die "Couldn't create bank_transaction";
491 my $bt_2 = create_bank_transaction(record => $ar_transaction,
495 bank_chart_id => $bank->id,
496 ) or die "Couldn't create bank_transaction";
498 $::form->{invoice_ids} = {
499 $bt_1->id => [ $ar_transaction->id ]
501 save_btcontroller_to_string();
504 is($bt_1->invoice_amount , '10.00000' , "$testname: bt_1 invoice amount was fully assigned");
505 $::form->{invoice_ids} = {
506 $bt_2->id => [ $ar_transaction->id ]
508 save_btcontroller_to_string();
510 $ar_transaction->load;
513 is($bt_1->invoice_amount , '10.00000' , "$testname: bt_1 invoice amount was fully assigned");
514 is($ar_transaction->paid , '119.00000' , "$testname: 'salesinv overpaid partial' was not overpaid");
515 is($bt_2->invoice_amount , '109.00000' , "$testname: bt_2 invoice amount was partly assigned");
519 sub test_partial_payment {
521 my $testname = 'test_partial_payment';
523 $ar_transaction = test_ar_transaction(invnumber => 'salesinv partial payment');
526 my $bt = create_bank_transaction(record => $ar_transaction,
527 bank_chart_id => $bank->id,
531 ) or die "Couldn't create bank_transaction";
533 $::form->{invoice_ids} = {
534 $bt->id => [ $ar_transaction->id ]
537 save_btcontroller_to_string();
539 $ar_transaction->load;
542 is($ar_transaction->paid , '100.00000' , "$testname: 'salesinv partial payment' was partially paid");
543 is($bt->invoice_amount , '100.00000' , "$testname: bt invoice amount was assigned partially paid amount");
545 # addon test partial payment full point match
546 my $bt2 = create_bank_transaction(record => $ar_transaction,
547 bank_chart_id => $bank->id,
551 ) or die "Couldn't create bank_transaction";
553 my ($agreement, $rule_matches) = $bt2->get_agreement_with_invoice($ar_transaction);
554 is($agreement, 15, "points for exact partial payment ok");
555 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");
558 sub test_full_workflow_ar_multiple_inv_skonto_reconciliate_and_undo {
560 my $testname = 'test_partial_payment';
562 $ar_transaction = test_ar_transaction(invnumber => 'salesinv partial payment two');
563 my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv 2 22d2', amount => 22);
565 # amount 299.29 > 119
566 my $bt = create_bank_transaction(record => $ar_transaction,
567 bank_chart_id => $bank->id,
569 ) or die "Couldn't create bank_transaction";
571 $::form->{invoice_ids} = {
572 $bt->id => [ $ar_transaction->id ]
575 save_btcontroller_to_string();
577 $ar_transaction->load;
580 is($ar_transaction->paid , '119.00000' , "$testname: 'salesinv partial payment' was fully paid");
581 is($bt->invoice_amount , '119.00000' , "$testname: bt invoice amount was assigned partially paid amount");
582 is($bt->amount , '299.29000' , "$testname: bt amount is stil there");
583 # next invoice, same bank transaction
584 $::form->{invoice_ids} = {
585 $bt->id => [ $ar_transaction_2->id ]
588 save_btcontroller_to_string();
590 $ar_transaction_2->load;
592 is($ar_transaction_2->paid , '26.18000' , "$testname: 'salesinv partial payment' was fully paid");
593 is($bt->invoice_amount , '145.18000' , "$testname: bt invoice amount was assigned partially paid amount");
594 is($bt->amount , '299.29000' , "$testname: bt amount is stil there");
595 is(scalar @{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )}, 4, "$testname 4 acc_trans entries created");
597 # now check all 4 entries done so far and save paid acc_trans_ids for later use with reconcile
598 foreach my $acc_trans_id_entry (@{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )}) {
599 isnt($acc_trans_id_entry->ar_id, undef, "$testname: bt linked with acc_trans and trans_id set");
600 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 ]);
601 is (ref $rl->[0], 'SL::DB::RecordLink', "$testname record link created");
602 my $acc_trans = SL::DB::Manager::AccTransaction->get_all(where => [acc_trans_id => $acc_trans_id_entry->acc_trans_id]);
603 foreach my $entry (@{ $acc_trans }) {
604 like(abs($entry->amount), qr/(119|26.18)/, "$testname: abs amount correct");
605 like($entry->chart_link, qr/(paid|AR)/, "$testname chart_link correct");
606 push @{ $::form->{bb_ids} }, $entry->acc_trans_id if $entry->chart_link =~ m/paid/;
609 # great we need one last booking to clear the whole bank transaction - we include skonto
610 my $ar_transaction_skonto = test_ar_transaction(invnumber => 'salesinv skonto last case',
611 payment_id => $payment_terms->id,
615 $::form->{invoice_ids} = {
616 $bt->id => [ $ar_transaction_skonto->id ]
618 $::form->{invoice_skontos} = {
619 $bt->id => [ 'with_skonto_pt' ]
622 save_btcontroller_to_string();
624 $ar_transaction_skonto->load;
626 is($ar_transaction_skonto->paid , '162.22000' , "$testname: 'salesinv skonto fully paid");
627 is($bt->invoice_amount , '299.29000' , "$testname: bt invoice amount was assigned partially paid amount");
628 is($bt->amount , '299.29000' , "$testname: bt amount is stil there");
629 is(scalar @{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )},
630 9, "$testname 9 acc_trans entries created");
632 # same loop as above, but only for the 3rd ar_id
633 foreach my $acc_trans_id_entry (@{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [ar_id => $ar_transaction_skonto->id ] )}) {
634 isnt($acc_trans_id_entry->ar_id, '', "$testname: bt linked with acc_trans and trans_id set");
635 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 ]);
636 is (ref $rl->[0], 'SL::DB::RecordLink', "$testname record link created");
637 my $acc_trans = SL::DB::Manager::AccTransaction->get_all(where => [acc_trans_id => $acc_trans_id_entry->acc_trans_id]);
638 foreach my $entry (@{ $acc_trans }) {
639 like($entry->chart_link, qr/(paid|AR)/, "$testname chart_link correct");
640 is ($entry->amount, '162.22000', "$testname full amont") if $entry->chart_link eq 'AR'; # full amount
641 like(abs($entry->amount), qr/(154.11|8.11)/, "$testname: abs amount correct") if $entry->chart_link =~ m/paid/;
642 push @{ $::form->{bb_ids} }, $entry->acc_trans_id if ($entry->chart_link =~ m/paid/ && $entry->amount == -154.11);
645 # done, now reconciliate all bookings
646 $::form->{bt_ids} = [ $bt->id ];
647 my $rec_controller = SL::Controller::Reconciliation->new;
648 my @errors = $rec_controller->_get_elements_and_validate;
650 is (scalar @errors, 0, "$testname unsuccesfull reconciliation with error: " . Dumper(@errors));
651 $rec_controller->_reconcile;
654 # and check the cleared state of bt and the acc_transactions
655 is($bt->cleared, '1' , "$testname: bt cleared");
656 foreach (@{ $::form->{bb_ids} }) {
657 my $acc_trans = SL::DB::Manager::AccTransaction->find_by(acc_trans_id => $_);
658 is($acc_trans->cleared, '1' , "$testname: acc_trans entry cleared");
660 # now, this was a really bad idea and in general a major mistake. better undo and redo the whole bank transactions
662 $::form->{ids} = [ $bt->id ];
663 $bt_controller = SL::Controller::BankTransaction->new;
664 $bt_controller->action_unlink_bank_transaction('testcase' => 1);
668 # and check the cleared state of bt and the acc_transactions
669 is($bt->cleared, '0' , "$testname: bt undo cleared");
670 is($bt->invoice_amount, '0.00000' , "$testname: bt undo invoice amount");
671 foreach (@{ $::form->{bb_ids} }) {
672 my $acc_trans = SL::DB::Manager::AccTransaction->find_by(acc_trans_id => $_);
673 is($acc_trans, undef , "$testname: cleared acc_trans entry completely removed");
675 # this was for data integrity for reconcile, now all the other options
676 is(scalar @{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )},
677 0, "$testname 7 acc_trans entries deleted");
678 my $rl = SL::DB::Manager::RecordLink->get_all(where => [ from_id => $bt->id, from_table => 'bank_transactions' ]);
679 is (ref $rl->[0], '', "$testname record link removed");
680 # double safety and check ar.paid
681 # load all three invoices and check for paid-link via acc_trans and paid in general
683 $ar_transaction->load;
684 $ar_transaction_2->load;
685 $ar_transaction_skonto->load;
687 is(scalar @{ SL::DB::Manager::AccTransaction->get_all(
688 where => [ trans_id => $ar_transaction->id, chart_link => { like => '%paid%' } ])},
689 0, "$testname no more paid entries in acc_trans for ar_transaction");
690 is(scalar @{ SL::DB::Manager::AccTransaction->get_all(
691 where => [ trans_id => $ar_transaction_2->id, chart_link => { like => '%paid%' } ])},
692 0, "$testname no more paid entries in acc_trans for ar_transaction_2");
693 is(scalar @{ SL::DB::Manager::AccTransaction->get_all(
694 where => [ trans_id => $ar_transaction_skonto->id, chart_link => { like => '%paid%' } ])},
695 0, "$testname no more paid entries in acc_trans for ar_transaction_skonto");
697 is($ar_transaction->paid , '0.00000' , "$testname: 'salesinv fully unpaid");
698 is($ar_transaction_2->paid , '0.00000' , "$testname: 'salesinv 2 fully unpaid");
699 is($ar_transaction_skonto->paid , '0.00000' , "$testname: 'salesinv skonto fully unpaid");
701 # whew. w(h)a(n)t a whole lotta test
705 sub test_credit_note {
707 my $testname = 'test_credit_note';
709 my $part1 = new_part( partnumber => 'T4254')->save;
710 my $part2 = new_service(partnumber => 'Serv1')->save;
711 my $credit_note = create_credit_note(
713 customer => $customer,
716 invoiceitems => [ create_invoice_item(part => $part1, qty => 3, sellprice => 70),
717 create_invoice_item(part => $part2, qty => 10, sellprice => 50),
720 my $bt = create_bank_transaction(record => $credit_note,
721 amount => $credit_note->amount,
722 bank_chart_id => $bank->id,
725 my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($credit_note);
726 is($agreement, 14, "points for credit note ok");
727 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");
729 $::form->{invoice_ids} = {
730 $bt->id => [ $credit_note->id ]
733 save_btcontroller_to_string();
737 is($credit_note->amount , '-844.90000', "$testname: amount ok");
738 is($credit_note->netamount, '-710.00000', "$testname: netamount ok");
739 is($credit_note->paid , '-844.90000', "$testname: paid ok");
742 sub test_neg_ap_transaction {
744 my $testname = 'test_neg_ap_transaction' . $params{invoice} ? ' invoice booking' : ' credit booking';
746 my $amount = $::form->round_amount($netamount * 1.19,2);
747 my $invoice = SL::DB::PurchaseInvoice->new(
748 invoice => $params{invoice} // 0,
749 invnumber => $params{invnumber} || 'test_neg_ap_transaction',
751 netamount => $netamount,
754 vendor_id => $vendor->id,
755 taxzone_id => $vendor->taxzone_id,
756 currency_id => $currency_id,
758 notes => 'test_neg_ap_transaction',
760 $invoice->add_ap_amount_row(
761 amount => $invoice->netamount,
762 chart => $ap_amount_chart,
763 tax_id => $tax_9->id,
766 $invoice->create_ap_row(chart => $ap_chart);
769 is($invoice->netamount, -20 , "$testname: netamount ok");
770 is($invoice->amount , -23.8, "$testname: amount ok");
772 my $bt = create_bank_transaction(record => $invoice,
773 amount => $invoice->amount,
774 bank_chart_id => $bank->id,
778 my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($invoice);
779 is($agreement, 16, "points for negative ap transaction ok");
781 $::form->{invoice_ids} = {
782 $bt->id => [ $invoice->id ]
785 save_btcontroller_to_string();
790 is($invoice->amount , '-23.80000', "$testname: amount ok");
791 is($invoice->netamount, '-20.00000', "$testname: netamount ok");
792 is($invoice->paid , '-23.80000', "$testname: paid ok");
793 is($bt->invoice_amount, '23.80000', "$testname: bt invoice amount for ap was assigned");
794 is($bt->amount, '23.80000', "$testname: bt amount for ap was assigned");
798 sub test_two_neg_ap_transaction {
799 my $testname='test_two_neg_ap_transaction';
801 my $amount = $::form->round_amount($netamount * 1.19,2);
802 my $invoice = SL::DB::PurchaseInvoice->new(
804 invnumber => 'test_neg_ap_transaction',
806 netamount => $netamount,
809 vendor_id => $vendor->id,
810 taxzone_id => $vendor->taxzone_id,
811 currency_id => $currency_id,
813 notes => 'test_neg_ap_transaction',
815 $invoice->add_ap_amount_row(
816 amount => $invoice->netamount,
817 chart => $ap_amount_chart,
818 tax_id => $tax_9->id,
821 $invoice->create_ap_row(chart => $ap_chart);
824 is($invoice->netamount, -20 , "$testname: netamount ok");
825 is($invoice->amount , -23.8, "$testname: amount ok");
827 my $netamount_two = -1.14;
828 my $amount_two = $::form->round_amount($netamount_two * 1.19,2);
829 my $invoice_two = SL::DB::PurchaseInvoice->new(
831 invnumber => 'test_neg_ap_transaction_two',
832 amount => $amount_two,
833 netamount => $netamount_two,
836 vendor_id => $vendor->id,
837 taxzone_id => $vendor->taxzone_id,
838 currency_id => $currency_id,
840 notes => 'test_neg_ap_transaction_two',
842 $invoice_two->add_ap_amount_row(
843 amount => $invoice_two->netamount,
844 chart => $ap_amount_chart,
845 tax_id => $tax_9->id,
848 $invoice_two->create_ap_row(chart => $ap_chart);
851 is($invoice_two->netamount, -1.14 , "$testname: netamount ok");
852 is($invoice_two->amount , -1.36, "$testname: amount ok");
855 my $bt = create_bank_transaction(record => $invoice_two,
856 amount => $invoice_two->amount + $invoice->amount,
857 bank_chart_id => $bank->id,
860 # my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($invoice_two);
861 # is($agreement, 15, "points for negative ap transaction ok");
863 $::form->{invoice_ids} = {
864 $bt->id => [ $invoice->id, $invoice_two->id ]
867 save_btcontroller_to_string();
873 is($invoice->amount , '-23.80000', "$testname: first inv amount ok");
874 is($invoice->netamount, '-20.00000', "$testname: first inv netamount ok");
875 is($invoice->paid , '-23.80000', "$testname: first inv paid ok");
876 is($invoice_two->amount , '-1.36000', "$testname: second inv amount ok");
877 is($invoice_two->netamount, '-1.14000', "$testname: second inv netamount ok");
878 is($invoice_two->paid , '-1.36000', "$testname: second inv paid ok");
879 is($bt->invoice_amount, '25.16000', "$testname: bt invoice amount for both invoices were assigned");
882 return ($invoice, $invoice_two);
885 sub test_ap_payment_transaction {
887 my $testname = 'test_ap_payment_transaction';
889 my $amount = $::form->round_amount($netamount * 1.19,2);
890 my $invoice = SL::DB::PurchaseInvoice->new(
892 invnumber => $params{invnumber} || $testname,
894 netamount => $netamount,
897 vendor_id => $vendor->id,
898 taxzone_id => $vendor->taxzone_id,
899 currency_id => $currency_id,
903 $invoice->add_ap_amount_row(
904 amount => $invoice->netamount,
905 chart => $ap_amount_chart,
906 tax_id => $tax_9->id,
909 $invoice->create_ap_row(chart => $ap_chart);
912 is($invoice->netamount, 115 , "$testname: netamount ok");
913 is($invoice->amount , 136.85, "$testname: amount ok");
915 my $bt = create_bank_transaction(record => $invoice,
916 amount => $invoice->amount,
917 bank_chart_id => $bank->id,
920 $::form->{invoice_ids} = {
921 $bt->id => [ $invoice->id ]
924 save_btcontroller_to_string();
929 is($invoice->amount , '136.85000', "$testname: amount ok");
930 is($invoice->netamount, '115.00000', "$testname: netamount ok");
931 is($bt->amount, '-136.85000', "$testname: bt amount ok");
932 is($invoice->paid , '136.85000', "$testname: paid ok");
933 is($bt->invoice_amount, '-136.85000', "$testname: bt invoice amount for ap was assigned");
938 sub test_ap_payment_part_transaction {
940 my $testname = 'test_ap_payment_p_transaction';
942 my $amount = $::form->round_amount($netamount * 1.19,2);
943 my $invoice = SL::DB::PurchaseInvoice->new(
945 invnumber => $params{invnumber} || $testname,
947 netamount => $netamount,
950 vendor_id => $vendor->id,
951 taxzone_id => $vendor->taxzone_id,
952 currency_id => $currency_id,
956 $invoice->add_ap_amount_row(
957 amount => $invoice->netamount,
958 chart => $ap_amount_chart,
959 tax_id => $tax_9->id,
962 $invoice->create_ap_row(chart => $ap_chart);
965 is($invoice->netamount, 115 , "$testname: netamount ok");
966 is($invoice->amount , 136.85, "$testname: amount ok");
968 my $bt = create_bank_transaction(record => $invoice,
969 amount => $invoice->amount-100,
970 bank_chart_id => $bank->id,
973 $::form->{invoice_ids} = {
974 $bt->id => [ $invoice->id ]
977 save_btcontroller_to_string();
982 is($invoice->amount , '136.85000', "$testname: amount ok");
983 is($invoice->netamount, '115.00000', "$testname: netamount ok");
984 is($bt->amount, '-36.85000', "$testname: bt amount ok");
985 is($invoice->paid , '36.85000', "$testname: paid ok");
986 is($bt->invoice_amount, '-36.85000', "$testname: bt invoice amount for ap was assigned");
988 my $bt2 = create_bank_transaction(record => $invoice,
990 bank_chart_id => $bank->id,
993 $::form->{invoice_ids} = {
994 $bt2->id => [ $invoice->id ]
997 save_btcontroller_to_string();
1001 is($invoice->amount , '136.85000', "$testname: amount ok");
1002 is($invoice->netamount, '115.00000', "$testname: netamount ok");
1003 is($bt2->amount, '-100.00000',"$testname: bt amount ok");
1004 is($invoice->paid , '136.85000', "$testname: paid ok");
1005 is($bt2->invoice_amount,'-100.00000', "$testname: bt invoice amount for ap was assigned");
1010 sub test_neg_sales_invoice {
1012 my $testname = 'test_neg_sales_invoice';
1014 my $part1 = new_part( partnumber => 'Funkenhaube öhm')->save;
1015 my $part2 = new_service(partnumber => 'Service-Pauschale Pasch!')->save;
1017 my $neg_sales_inv = create_sales_invoice(
1018 invnumber => '20172201',
1019 customer => $customer,
1022 invoiceitems => [ create_invoice_item(part => $part1, qty => 3, sellprice => 70),
1023 create_invoice_item(part => $part2, qty => 10, sellprice => -50),
1026 my $bt = create_bank_transaction(record => $neg_sales_inv,
1027 amount => $neg_sales_inv->amount,
1028 bank_chart_id => $bank->id,
1032 $::form->{invoice_ids} = {
1033 $bt->id => [ $neg_sales_inv->id ]
1036 save_btcontroller_to_string();
1038 $neg_sales_inv->load;
1040 is($neg_sales_inv->amount , '-345.10000', "$testname: amount ok");
1041 is($neg_sales_inv->netamount, '-290.00000', "$testname: netamount ok");
1042 is($neg_sales_inv->paid , '-345.10000', "$testname: paid ok");
1043 is($bt->amount , '-345.10000', "$testname: bt amount ok");
1044 is($bt->invoice_amount , '-345.10000', "$testname: bt invoice_amount ok");
1049 my $testname = 'test_bt_rule1';
1051 $ar_transaction = test_ar_transaction(invnumber => 'bt_rule1', transdate => $dt);
1053 my $bt = create_bank_transaction(record => $ar_transaction, transdate => $dt) or die "Couldn't create bank_transaction";
1055 $ar_transaction->load;
1057 is($ar_transaction->paid , '0.00000' , "$testname: not paid");
1058 is($bt->invoice_amount , '0.00000' , "$testname: bt invoice amount was not assigned");
1060 my $bt_controller = SL::Controller::BankTransaction->new;
1061 my ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
1063 is(scalar(@$bt_transactions) , 1 , "$testname: one bank_transaction");
1064 is($bt_transactions->[0]->{agreement}, 20 , "$testname: agreement == 20");
1065 my $match = join ( ' ',@{$bt_transactions->[0]->{rule_matches}});
1066 #print "rule_matches='".$match."'\n";
1068 "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) ",
1069 "$testname: rule_matches ok");
1070 $bt->invoice_amount($bt->amount);
1072 is($bt->invoice_amount , '119.00000' , "$testname: bt invoice amount now set");
1075 sub test_sepa_export {
1077 my $testname = 'test_sepa_export';
1079 $ar_transaction = test_ar_transaction(invnumber => 'sepa1', transdate => $dt);
1081 my $bt = create_bank_transaction(record => $ar_transaction, transdate => $dt) or die "Couldn't create bank_transaction";
1082 my $se = create_sepa_export();
1083 my $sei = create_sepa_export_item(
1084 chart_id => $bank->id,
1085 ar_id => $ar_transaction->id,
1086 sepa_export_id => $se->id,
1087 vc_iban => $customer->iban,
1088 vc_bic => $customer->bic,
1089 vc_mandator_id => $customer->mandator_id,
1090 vc_depositor => $customer->depositor,
1091 amount => $ar_transaction->amount,
1093 require SL::SEPA::XML;
1094 my $sepa_xml = SL::SEPA::XML->new('company' => $customer->name,
1095 'creditor_id' => "id",
1096 'src_charset' => 'UTF-8',
1097 'message_id' => "test",
1101 is($sepa_xml->{company} , 'Test Customer OLE S.L. Ardbaerg AB');
1103 $ar_transaction->load;
1106 is($ar_transaction->paid , '0.00000' , "$testname: sepa1 not paid");
1107 is($bt->invoice_amount , '0.00000' , "$testname: bt invoice amount was not assigned");
1108 is($bt->amount , '119.00000' , "$testname: bt amount ok");
1109 is($sei->amount , '119.00000' , "$testname: sepa export amount ok");
1111 my $bt_controller = SL::Controller::BankTransaction->new;
1112 my ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
1114 is(scalar(@$bt_transactions) , 1 , "$testname: one bank_transaction");
1115 is($bt_transactions->[0]->{agreement}, 25 , "$testname: agreement == 25");
1116 my $match = join ( ' ',@{$bt_transactions->[0]->{rule_matches}});
1118 "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) ",
1119 "$testname: rule_matches ok");
1122 sub test_two_banktransactions {
1124 my $testname = 'two_banktransactions';
1126 my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv10000' , amount => 2912.00 );
1127 my $bt1 = create_bank_transaction(record => $ar_transaction_1,
1128 amount => $ar_transaction_1->amount,
1129 purpose => "Rechnung10000 beinahe",
1131 bank_chart_id => $bank->id,
1132 ) or die "Couldn't create bank_transaction";
1134 my $bt2 = create_bank_transaction(record => $ar_transaction_1,
1135 amount => $ar_transaction_1->amount + 0.01,
1136 purpose => "sicher salesinv20000 vielleicht",
1138 bank_chart_id => $bank->id,
1139 ) or die "Couldn't create bank_transaction";
1141 my ($agreement1, $rule_matches1) = $bt1->get_agreement_with_invoice($ar_transaction_1);
1142 is($agreement1, 19, "bt1 19 points for ar_transaction_1 in $testname ok");
1143 #print "rule_matches1=".$rule_matches1."\n";
1145 "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) ",
1146 "$testname: rule_matches ok");
1147 my ($agreement2, $rule_matches2) = $bt2->get_agreement_with_invoice($ar_transaction_1);
1148 is($agreement2, 11, "bt2 11 points for ar_transaction_1 in $testname ok");
1150 "remote_account_number(3) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus0(3) ",
1151 "$testname: rule_matches ok");
1153 my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv20000' , amount => 2912.01 );
1154 my $ar_transaction_3 = test_ar_transaction(invnumber => 'zweitemit10000', amount => 2912.00 );
1155 ($agreement1, $rule_matches1) = $bt1->get_agreement_with_invoice($ar_transaction_2);
1157 is($agreement1, 11, "bt1 11 points for ar_transaction_2 in $testname ok");
1159 ($agreement2, $rule_matches2) = $bt2->get_agreement_with_invoice($ar_transaction_2);
1160 is($agreement2, 20, "bt2 20 points for ar_transaction_2 in $testname ok");
1162 ($agreement2, $rule_matches2) = $bt2->get_agreement_with_invoice($ar_transaction_1);
1163 is($agreement2, 11, "bt2 11 points for ar_transaction_1 in $testname ok");
1165 my $bt3 = create_bank_transaction(record => $ar_transaction_3,
1166 amount => $ar_transaction_3->amount,
1167 purpose => "sicher Rechnung10000 vielleicht",
1169 bank_chart_id => $bank->id,
1170 ) or die "Couldn't create bank_transaction";
1172 my ($agreement3, $rule_matches3) = $bt3->get_agreement_with_invoice($ar_transaction_3);
1173 is($agreement3, 19, "bt3 19 points for ar_transaction_3 in $testname ok");
1176 $ar_transaction_2->delete;
1178 #nun sollten zwei gleichwertige Rechnungen $ar_transaction_1 und $ar_transaction_3 für $bt1 gefunden werden
1179 #aber es darf keine Proposals geben mit mehreren Rechnungen
1180 my $bt_controller = SL::Controller::BankTransaction->new;
1181 my ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
1183 is(scalar(@$bt_transactions) , 2 , "$testname: two bank_transaction");
1184 is(scalar(@$proposals) , 0 , "$testname: no proposals");
1186 $ar_transaction_3->delete;
1188 # Jetzt gibt es zwei Kontobewegungen mit gleichen Punkten für eine Rechnung.
1189 # hier darf es auch keine Proposals geben
1191 ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
1193 is(scalar(@$bt_transactions) , 2 , "$testname: two bank_transaction");
1194 # odyn testfall - anforderungen so (noch) nicht in kivi
1195 # is(scalar(@$proposals) , 0 , "$testname: no proposals");
1197 # Jetzt gibt es zwei Kontobewegungen für eine Rechnung.
1198 # eine Bewegung bekommt mehr Punkte
1199 # hier darf es auch keine Proposals geben
1200 $bt3->update_attributes( purpose => "fuer Rechnung salesinv10000");
1202 ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
1204 is(scalar(@$bt_transactions) , 2 , "$testname: two bank_transaction");
1205 # odyn testfall - anforderungen so (noch) nicht in kivi
1206 # is(scalar(@$proposals) , 1 , "$testname: one proposal");
1211 my $testname = 'closedto';
1213 my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv10000' , amount => 2912.00 );
1214 my $bt1 = create_bank_transaction(record => $ar_transaction_1,
1215 amount => $ar_transaction_1->amount,
1216 purpose => "Rechnung10000 beinahe",
1217 bank_chart_id => $bank->id,
1218 ) or die "Couldn't create bank_transaction";
1220 $bt1->valutadate(DateTime->new(year => 2019, month => 12, day => 30));
1223 is($bt1->closed_period, 0, "$testname undefined closedto");
1225 my $defaults = SL::DB::Manager::Default->get_all(limit => 1)->[0];
1226 $defaults->closedto(DateTime->new(year => 2019, month => 12, day => 31));
1228 $::instance_conf->reload->data;
1230 is($bt1->closed_period, 1, "$testname defined and next date closedto");
1232 $bt1->valutadate(DateTime->new(year => 2020, month => 1, day => 1));
1236 is($bt1->closed_period, 0, "$testname defined closedto and next date valuta");
1237 $defaults->closedto(undef);
1242 sub test_skonto_exact_ap_transaction {
1244 my $testname = 'test_skonto_exact_ap_transaction';
1246 $ap_transaction = test_ap_transaction(invnumber => 'ap transaction skonto',
1247 payment_id => $payment_terms->id,
1250 my $bt = create_bank_transaction(record => $ap_transaction,
1251 bank_chart_id => $bank->id,
1254 amount => $ap_transaction->amount_less_skonto
1255 ) or die "Couldn't create bank_transaction";
1257 $::form->{invoice_ids} = {
1258 $bt->id => [ $ap_transaction->id ]
1260 $::form->{invoice_skontos} = {
1261 $bt->id => [ 'with_skonto_pt' ]
1264 save_btcontroller_to_string();
1266 $ap_transaction->load;
1268 is($ap_transaction->paid , '119.00000' , "$testname: ap transaction skonto was paid");
1269 is($ap_transaction->closed , 1 , "$testname: ap transaction skonto is closed");
1270 is($bt->invoice_amount , '113.05000' , "$testname: bt invoice amount was assigned");