1 use Test::More tests => 130;
9 use Support::TestSetup;
11 use List::Util qw(sum);
13 use SL::DB::Buchungsgruppe;
21 use SL::DB::BankAccount;
22 use SL::DB::PaymentTerm;
23 use SL::DB::PurchaseInvoice;
24 use SL::DB::BankTransaction;
25 use SL::Controller::BankTransaction;
26 use SL::Dev::ALL qw(:ALL);
29 my ($customer, $vendor, $currency_id, $unit, $tax, $tax7, $tax_9, $payment_terms, $bank_account);
30 my ($transdate1, $transdate2, $currency);
31 my ($ar_chart,$bank,$ar_amount_chart, $ap_chart, $ap_amount_chart);
32 my ($ar_transaction, $ap_transaction);
36 SL::DB::Manager::BankTransaction->delete_all(all => 1);
37 SL::DB::Manager::InvoiceItem->delete_all(all => 1);
38 SL::DB::Manager::InvoiceItem->delete_all(all => 1);
39 SL::DB::Manager::Invoice->delete_all(all => 1);
40 SL::DB::Manager::PurchaseInvoice->delete_all(all => 1);
41 SL::DB::Manager::Part->delete_all(all => 1);
42 SL::DB::Manager::Customer->delete_all(all => 1);
43 SL::DB::Manager::Vendor->delete_all(all => 1);
44 SL::DB::Manager::SepaExportItem->delete_all(all => 1);
45 SL::DB::Manager::SepaExport->delete_all(all => 1);
46 SL::DB::Manager::BankAccount->delete_all(all => 1);
47 SL::DB::Manager::PaymentTerm->delete_all(all => 1);
48 SL::DB::Manager::Currency->delete_all(where => [ name => 'CUR' ]);
51 sub save_btcontroller_to_string {
53 open(my $outputFH, '>', \$output) or die;
54 my $oldFH = select $outputFH;
56 my $bt_controller = SL::Controller::BankTransaction->new;
57 $bt_controller->action_save_invoices;
65 Support::TestSetup::login();
68 reset_state(); # initialise customers/vendors/bank/currency/...
72 test_overpayment_with_partialpayment();
76 test_partial_payment();
78 test_ap_transaction();
79 test_neg_ap_transaction();
80 test_ap_payment_transaction();
81 test_ap_payment_part_transaction();
82 test_neg_sales_invoice();
87 # remove all created data at end of test
92 ###### functions for setting up data
97 $params{$_} ||= {} for qw(unit customer tax vendor);
101 $transdate1 = DateTime->today;
102 $transdate2 = DateTime->today->add(days => 5);
104 $tax = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} }) || croak "No tax";
105 $tax7 = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07) || croak "No tax for 7\%";
106 $tax_9 = SL::DB::Manager::Tax->find_by(taxkey => 9, rate => 0.19, %{ $params{tax} }) || croak "No tax";
108 $currency_id = $::instance_conf->get_currency_id;
110 $bank_account = SL::DB::BankAccount->new(
111 account_number => '123',
116 chart_id => SL::DB::Manager::Chart->find_by(description => 'Bank')->id,
117 name => SL::DB::Manager::Chart->find_by(description => 'Bank')->description,
120 $customer = new_customer(
121 name => 'Test Customer',
122 iban => 'DE12500105170648489890',
124 account_number => '648489890',
125 mandate_date_of_signature => $transdate1,
126 mandator_id => 'foobar',
128 bank_code => 'G1235',
129 depositor => 'Test Customer',
130 customernumber => 'CUST1704',
133 $payment_terms = create_payment_terms();
135 $vendor = new_vendor(
136 name => 'Test Vendor',
137 payment_id => $payment_terms->id,
138 iban => 'DE12500105170648489890',
140 account_number => '648489890',
142 bank_code => 'G1235',
143 depositor => 'Test Vendor',
144 vendornumber => 'VEND1704',
147 $ar_chart = SL::DB::Manager::Chart->find_by( accno => '1400' ); # Forderungen
148 $ap_chart = SL::DB::Manager::Chart->find_by( accno => '1600' ); # Verbindlichkeiten
149 $bank = SL::DB::Manager::Chart->find_by( accno => '1200' ); # Bank
150 $ar_amount_chart = SL::DB::Manager::Chart->find_by( accno => '8400' ); # Erlöse
151 $ap_amount_chart = SL::DB::Manager::Chart->find_by( accno => '3400' ); # Wareneingang 19%
155 sub test_ar_transaction {
158 my $amount = $params{amount} || $::form->round_amount(100 * 1.19,2);
159 my $invoice = SL::DB::Invoice->new(
161 invnumber => $params{invnumber} || undef, # let it use its own invnumber
163 netamount => $netamount,
164 transdate => $transdate1,
166 customer_id => $customer->id,
167 taxzone_id => $customer->taxzone_id,
168 currency_id => $currency_id,
170 payment_id => $params{payment_id} || undef,
171 notes => 'test_ar_transaction',
173 $invoice->add_ar_amount_row(
174 amount => $invoice->netamount,
175 chart => $ar_amount_chart,
179 $invoice->create_ar_row(chart => $ar_chart);
182 is($invoice->currency_id , $currency_id , 'currency_id has been saved');
183 is($invoice->netamount , 100 , 'ar amount has been converted');
184 is($invoice->amount , 119 , 'ar amount has been converted');
185 is($invoice->taxincluded , 0 , 'ar transaction doesn\'t have taxincluded');
187 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');
188 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');
193 sub test_ap_transaction {
195 my $testname = 'test_ap_transaction';
198 my $amount = $::form->round_amount($netamount * 1.19,2);
199 my $invoice = SL::DB::PurchaseInvoice->new(
201 invnumber => $params{invnumber} || $testname,
203 netamount => $netamount,
204 transdate => $transdate1,
206 vendor_id => $vendor->id,
207 taxzone_id => $vendor->taxzone_id,
208 currency_id => $currency_id,
210 notes => 'test_ap_transaction',
212 $invoice->add_ap_amount_row(
213 amount => $invoice->netamount,
214 chart => $ap_amount_chart,
215 tax_id => $tax_9->id,
218 $invoice->create_ap_row(chart => $ap_chart);
221 is($invoice->currency_id , $currency_id , "$testname: currency_id has been saved");
222 is($invoice->netamount , 100 , "$testname: ap amount has been converted");
223 is($invoice->amount , 119 , "$testname: ap amount has been converted");
224 is($invoice->taxincluded , 0 , "$testname: ap transaction doesn\'t have taxincluded");
226 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');
227 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');
236 my $testname = 'test1';
238 $ar_transaction = test_ar_transaction(invnumber => 'salesinv1');
240 my $bt = create_bank_transaction(record => $ar_transaction) or die "Couldn't create bank_transaction";
242 $::form->{invoice_ids} = {
243 $bt->id => [ $ar_transaction->id ]
246 save_btcontroller_to_string();
248 $ar_transaction->load;
250 is($ar_transaction->paid , '119.00000' , "$testname: salesinv1 was paid");
251 is($ar_transaction->closed , 1 , "$testname: salesinv1 is closed");
252 is($bt->invoice_amount , '119.00000' , "$testname: bt invoice amount was assigned");
256 sub test_skonto_exact {
258 my $testname = 'test_skonto_exact';
260 $ar_transaction = test_ar_transaction(invnumber => 'salesinv skonto',
261 payment_id => $payment_terms->id,
264 my $bt = create_bank_transaction(record => $ar_transaction,
265 bank_chart_id => $bank->id,
266 amount => $ar_transaction->amount_less_skonto
267 ) or die "Couldn't create bank_transaction";
269 $::form->{invoice_ids} = {
270 $bt->id => [ $ar_transaction->id ]
272 $::form->{invoice_skontos} = {
273 $bt->id => [ 'with_skonto_pt' ]
276 save_btcontroller_to_string();
278 $ar_transaction->load;
280 is($ar_transaction->paid , '119.00000' , "$testname: salesinv skonto was paid");
281 is($ar_transaction->closed , 1 , "$testname: salesinv skonto is closed");
282 is($bt->invoice_amount , '113.05000' , "$testname: bt invoice amount was assigned");
286 sub test_two_invoices {
288 my $testname = 'test_two_invoices';
290 my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv_1');
291 my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv_2');
293 my $bt = create_bank_transaction(record => $ar_transaction_1,
294 amount => ($ar_transaction_1->amount + $ar_transaction_2->amount),
295 purpose => "Rechnungen " . $ar_transaction_1->invnumber . " und " . $ar_transaction_2->invnumber,
296 bank_chart_id => $bank->id,
297 ) or die "Couldn't create bank_transaction";
299 my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($ar_transaction_1);
300 is($agreement, 16, "points for ar_transaction_1 in test_two_invoices ok");
302 $::form->{invoice_ids} = {
303 $bt->id => [ $ar_transaction_1->id, $ar_transaction_2->id ]
306 save_btcontroller_to_string();
308 $ar_transaction_1->load;
309 $ar_transaction_2->load;
312 is($ar_transaction_1->paid , '119.00000' , "$testname: salesinv_1 was paid");
313 is($ar_transaction_1->closed , 1 , "$testname: salesinv_1 is closed");
314 is($ar_transaction_2->paid , '119.00000' , "$testname: salesinv_2 was paid");
315 is($ar_transaction_2->closed , 1 , "$testname: salesinv_2 is closed");
316 is($bt->invoice_amount , '238.00000' , "$testname: bt invoice amount was assigned");
320 sub test_overpayment {
322 my $testname = 'test_overpayment';
324 $ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid');
327 my $bt = create_bank_transaction(record => $ar_transaction,
328 bank_chart_id => $bank->id,
330 ) or die "Couldn't create bank_transaction";
332 $::form->{invoice_ids} = {
333 $bt->id => [ $ar_transaction->id ]
336 save_btcontroller_to_string();
338 $ar_transaction->load;
341 is($ar_transaction->paid , '135.00000' , "$testname: 'salesinv overpaid' was overpaid");
342 is($bt->invoice_amount , '135.00000' , "$testname: bt invoice amount was assigned overpaid amount");
343 { local $TODO = 'this currently fails because closed ignores over-payments, see commit d90966c7';
344 is($ar_transaction->closed , 0 , "$testname: 'salesinv overpaid' is open (via 'closed' method')");
346 is($ar_transaction->open_amount == 0 ? 1 : 0 , 0 , "$testname: 'salesinv overpaid is open (via amount-paid)");
350 sub test_overpayment_with_partialpayment {
352 # two payments on different days, 10 and 119. If there is only one invoice we want it be overpaid.
353 my $testname = 'test_overpayment_with_partialpayment';
355 $ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid partial');
357 my $bt_1 = create_bank_transaction(record => $ar_transaction,
358 bank_chart_id => $bank->id,
360 ) or die "Couldn't create bank_transaction";
361 my $bt_2 = create_bank_transaction(record => $ar_transaction,
363 transdate => DateTime->today->add(days => 5),
364 bank_chart_id => $bank->id,
365 ) or die "Couldn't create bank_transaction";
367 $::form->{invoice_ids} = {
368 $bt_1->id => [ $ar_transaction->id ]
370 save_btcontroller_to_string();
372 $::form->{invoice_ids} = {
373 $bt_2->id => [ $ar_transaction->id ]
375 save_btcontroller_to_string();
377 $ar_transaction->load;
381 is($ar_transaction->paid , '129.00000' , "$testname: 'salesinv overpaid partial' was overpaid");
382 is($bt_1->invoice_amount , '10.00000' , "$testname: bt_1 invoice amount was assigned overpaid amount");
383 is($bt_2->invoice_amount , '119.00000' , "$testname: bt_2 invoice amount was assigned overpaid amount");
387 sub test_partial_payment {
389 my $testname = 'test_partial_payment';
391 $ar_transaction = test_ar_transaction(invnumber => 'salesinv partial payment');
394 my $bt = create_bank_transaction(record => $ar_transaction,
395 bank_chart_id => $bank->id,
397 ) or die "Couldn't create bank_transaction";
399 $::form->{invoice_ids} = {
400 $bt->id => [ $ar_transaction->id ]
403 save_btcontroller_to_string();
405 $ar_transaction->load;
408 is($ar_transaction->paid , '100.00000' , "$testname: 'salesinv partial payment' was partially paid");
409 is($bt->invoice_amount , '100.00000' , "$testname: bt invoice amount was assigned partially paid amount");
413 sub test_credit_note {
415 my $testname = 'test_credit_note';
417 my $part1 = new_part( partnumber => 'T4254')->save;
418 my $part2 = new_service(partnumber => 'Serv1')->save;
419 my $credit_note = create_credit_note(
421 customer => $customer,
423 invoiceitems => [ create_invoice_item(part => $part1, qty => 3, sellprice => 70),
424 create_invoice_item(part => $part2, qty => 10, sellprice => 50),
427 my $bt = create_bank_transaction(record => $credit_note,
428 amount => $credit_note->amount,
429 bank_chart_id => $bank->id,
430 transdate => DateTime->today->add(days => 10),
432 my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($credit_note);
433 is($agreement, 13, "points for credit note ok");
434 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");
436 $::form->{invoice_ids} = {
437 $bt->id => [ $credit_note->id ]
440 save_btcontroller_to_string();
444 is($credit_note->amount , '-844.90000', "$testname: amount ok");
445 is($credit_note->netamount, '-710.00000', "$testname: netamount ok");
446 is($credit_note->paid , '-844.90000', "$testname: paid ok");
449 sub test_neg_ap_transaction {
451 my $testname = 'test_neg_ap_transaction';
453 my $amount = $::form->round_amount($netamount * 1.19,2);
454 my $invoice = SL::DB::PurchaseInvoice->new(
456 invnumber => $params{invnumber} || 'test_neg_ap_transaction',
458 netamount => $netamount,
459 transdate => $transdate1,
461 vendor_id => $vendor->id,
462 taxzone_id => $vendor->taxzone_id,
463 currency_id => $currency_id,
465 notes => 'test_neg_ap_transaction',
467 $invoice->add_ap_amount_row(
468 amount => $invoice->netamount,
469 chart => $ap_amount_chart,
470 tax_id => $tax_9->id,
473 $invoice->create_ap_row(chart => $ap_chart);
476 is($invoice->netamount, -20 , "$testname: netamount ok");
477 is($invoice->amount , -23.8, "$testname: amount ok");
479 my $bt = create_bank_transaction(record => $invoice,
480 amount => $invoice->amount,
481 bank_chart_id => $bank->id,
482 transdate => DateTime->today->add(days => 10),
484 my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($invoice);
485 is($agreement, 15, "points for negative ap transaction ok");
487 $::form->{invoice_ids} = {
488 $bt->id => [ $invoice->id ]
491 save_btcontroller_to_string();
496 is($invoice->amount , '-23.80000', "$testname: amount ok");
497 is($invoice->netamount, '-20.00000', "$testname: netamount ok");
498 is($invoice->paid , '-23.80000', "$testname: paid ok");
499 is($bt->invoice_amount, '23.80000', "$testname: bt invoice amount for ap was assigned");
504 sub test_ap_payment_transaction {
506 my $testname = 'test_ap_payment_transaction';
508 my $amount = $::form->round_amount($netamount * 1.19,2);
509 my $invoice = SL::DB::PurchaseInvoice->new(
511 invnumber => $params{invnumber} || $testname,
513 netamount => $netamount,
514 transdate => $transdate1,
516 vendor_id => $vendor->id,
517 taxzone_id => $vendor->taxzone_id,
518 currency_id => $currency_id,
522 $invoice->add_ap_amount_row(
523 amount => $invoice->netamount,
524 chart => $ap_amount_chart,
525 tax_id => $tax_9->id,
528 $invoice->create_ap_row(chart => $ap_chart);
531 is($invoice->netamount, 115 , "$testname: netamount ok");
532 is($invoice->amount , 136.85, "$testname: amount ok");
534 my $bt = create_bank_transaction(record => $invoice,
535 amount => $invoice->amount,
536 bank_chart_id => $bank->id,
537 transdate => DateTime->today->add(days => 10),
539 $::form->{invoice_ids} = {
540 $bt->id => [ $invoice->id ]
543 save_btcontroller_to_string();
548 is($invoice->amount , '136.85000', "$testname: amount ok");
549 is($invoice->netamount, '115.00000', "$testname: netamount ok");
550 is($bt->amount, '-136.85000', "$testname: bt amount ok");
551 is($invoice->paid , '136.85000', "$testname: paid ok");
552 is($bt->invoice_amount, '-136.85000', "$testname: bt invoice amount for ap was assigned");
557 sub test_ap_payment_part_transaction {
559 my $testname = 'test_ap_payment_p_transaction';
561 my $amount = $::form->round_amount($netamount * 1.19,2);
562 my $invoice = SL::DB::PurchaseInvoice->new(
564 invnumber => $params{invnumber} || $testname,
566 netamount => $netamount,
567 transdate => $transdate1,
569 vendor_id => $vendor->id,
570 taxzone_id => $vendor->taxzone_id,
571 currency_id => $currency_id,
575 $invoice->add_ap_amount_row(
576 amount => $invoice->netamount,
577 chart => $ap_amount_chart,
578 tax_id => $tax_9->id,
581 $invoice->create_ap_row(chart => $ap_chart);
584 is($invoice->netamount, 115 , "$testname: netamount ok");
585 is($invoice->amount , 136.85, "$testname: amount ok");
587 my $bt = create_bank_transaction(record => $invoice,
588 amount => $invoice->amount-100,
589 bank_chart_id => $bank->id,
590 transdate => DateTime->today->add(days => 10),
592 $::form->{invoice_ids} = {
593 $bt->id => [ $invoice->id ]
596 save_btcontroller_to_string();
601 is($invoice->amount , '136.85000', "$testname: amount ok");
602 is($invoice->netamount, '115.00000', "$testname: netamount ok");
603 is($bt->amount, '-36.85000', "$testname: bt amount ok");
604 is($invoice->paid , '36.85000', "$testname: paid ok");
605 is($bt->invoice_amount, '-36.85000', "$testname: bt invoice amount for ap was assigned");
607 my $bt2 = create_bank_transaction(record => $invoice,
609 bank_chart_id => $bank->id,
610 transdate => DateTime->today->add(days => 10),
612 $::form->{invoice_ids} = {
613 $bt2->id => [ $invoice->id ]
616 save_btcontroller_to_string();
620 is($invoice->amount , '136.85000', "$testname: amount ok");
621 is($invoice->netamount, '115.00000', "$testname: netamount ok");
622 is($bt2->amount, '-100.00000',"$testname: bt amount ok");
623 is($invoice->paid , '136.85000', "$testname: paid ok");
624 is($bt2->invoice_amount,'-100.00000', "$testname: bt invoice amount for ap was assigned");
629 sub test_neg_sales_invoice {
631 my $testname = 'test_neg_sales_invoice';
633 my $part1 = new_part( partnumber => 'Funkenhaube öhm')->save;
634 my $part2 = new_service(partnumber => 'Service-Pauschale Pasch!')->save;
636 my $neg_sales_inv = create_sales_invoice(
637 invnumber => '20172201',
638 customer => $customer,
640 invoiceitems => [ create_invoice_item(part => $part1, qty => 3, sellprice => 70),
641 create_invoice_item(part => $part2, qty => 10, sellprice => -50),
644 my $bt = create_bank_transaction(record => $neg_sales_inv,
645 amount => $neg_sales_inv->amount,
646 bank_chart_id => $bank->id,
647 transdate => DateTime->today,
649 $::form->{invoice_ids} = {
650 $bt->id => [ $neg_sales_inv->id ]
653 save_btcontroller_to_string();
655 $neg_sales_inv->load;
657 is($neg_sales_inv->amount , '-345.10000', "$testname: amount ok");
658 is($neg_sales_inv->netamount, '-290.00000', "$testname: netamount ok");
659 is($neg_sales_inv->paid , '-345.10000', "$testname: paid ok");
660 is($bt->amount , '-345.10000', "$testname: bt amount ok");
661 is($bt->invoice_amount , '-345.10000', "$testname: bt invoice_amount ok");
666 my $testname = 'test_bt_rule1';
668 $ar_transaction = test_ar_transaction(invnumber => 'bt_rule1');
670 my $bt = create_bank_transaction(record => $ar_transaction) or die "Couldn't create bank_transaction";
672 $ar_transaction->load;
674 is($ar_transaction->paid , '0.00000' , "$testname: not paid");
675 is($bt->invoice_amount , '0.00000' , "$testname: bt invoice amount was not assigned");
677 my $bt_controller = SL::Controller::BankTransaction->new;
678 $::form->{dont_render_for_test} = 1;
679 $::form->{filter}{bank_account} = $bank_account->id;
680 my $bt_transactions = $bt_controller->action_list;
682 is(scalar(@$bt_transactions) , 1 , "$testname: one bank_transaction");
683 is($bt_transactions->[0]->{agreement}, 20 , "$testname: agreement == 20");
684 my $match = join ( ' ',@{$bt_transactions->[0]->{rule_matches}});
685 #print "rule_matches='".$match."'\n";
687 "remote_account_number(3) exact_amount(4) own_invnumber_in_purpose(5) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus0(3) ",
688 "$testname: rule_matches ok");
689 $bt->invoice_amount($bt->amount);
691 is($bt->invoice_amount , '119.00000' , "$testname: bt invoice amount now set");
694 sub test_sepa_export {
696 my $testname = 'test_sepa_export';
698 $ar_transaction = test_ar_transaction(invnumber => 'sepa1');
700 my $bt = create_bank_transaction(record => $ar_transaction) or die "Couldn't create bank_transaction";
701 my $se = create_sepa_export();
702 my $sei = create_sepa_export_item(
703 chart_id => $bank->id,
704 ar_id => $ar_transaction->id,
705 sepa_export_id => $se->id,
706 vc_iban => $customer->iban,
707 vc_bic => $customer->bic,
708 vc_mandator_id => $customer->mandator_id,
709 vc_depositor => $customer->depositor,
710 amount => $ar_transaction->amount,
713 $ar_transaction->load;
716 is($ar_transaction->paid , '0.00000' , "$testname: sepa1 not paid");
717 is($bt->invoice_amount , '0.00000' , "$testname: bt invoice amount was not assigned");
718 is($bt->amount , '119.00000' , "$testname: bt amount ok");
719 is($sei->amount , '119.00000' , "$testname: sepa export amount ok");
721 my $bt_controller = SL::Controller::BankTransaction->new;
722 $::form->{dont_render_for_test} = 1;
723 $::form->{filter}{bank_account} = $bank_account->id;
724 my $bt_transactions = $bt_controller->action_list;
726 is(scalar(@$bt_transactions) , 1 , "$testname: one bank_transaction");
727 is($bt_transactions->[0]->{agreement}, 25 , "$testname: agreement == 25");
728 my $match = join ( ' ',@{$bt_transactions->[0]->{rule_matches}});
730 "remote_account_number(3) exact_amount(4) own_invnumber_in_purpose(5) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus0(3) sepa_export_item(5) ",
731 "$testname: rule_matches ok");