1 use Test::More tests => 197;
9 use Support::TestSetup;
11 use List::Util qw(sum);
13 use SL::DB::AccTransaction;
14 use SL::DB::Buchungsgruppe;
22 use SL::DB::BankAccount;
23 use SL::DB::PaymentTerm;
24 use SL::DB::PurchaseInvoice;
25 use SL::DB::BankTransaction;
26 use SL::Controller::BankTransaction;
27 use SL::Dev::ALL qw(:ALL);
30 my ($customer, $vendor, $currency_id, $unit, $tax, $tax0, $tax7, $tax_9, $payment_terms, $bank_account);
31 my ($transdate1, $transdate2, $currency);
32 my ($ar_chart,$bank,$ar_amount_chart, $ap_chart, $ap_amount_chart);
33 my ($ar_transaction, $ap_transaction);
37 SL::DB::Manager::BankTransaction->delete_all(all => 1);
38 SL::DB::Manager::InvoiceItem->delete_all(all => 1);
39 SL::DB::Manager::InvoiceItem->delete_all(all => 1);
40 SL::DB::Manager::Invoice->delete_all(all => 1);
41 SL::DB::Manager::PurchaseInvoice->delete_all(all => 1);
42 SL::DB::Manager::Part->delete_all(all => 1);
43 SL::DB::Manager::Customer->delete_all(all => 1);
44 SL::DB::Manager::Vendor->delete_all(all => 1);
45 SL::DB::Manager::SepaExportItem->delete_all(all => 1);
46 SL::DB::Manager::SepaExport->delete_all(all => 1);
47 SL::DB::Manager::BankAccount->delete_all(all => 1);
48 SL::DB::Manager::PaymentTerm->delete_all(all => 1);
49 SL::DB::Manager::Currency->delete_all(where => [ name => 'CUR' ]);
54 sub save_btcontroller_to_string {
56 open(my $outputFH, '>', \$output) or die;
57 my $oldFH = select $outputFH;
59 $bt_controller = SL::Controller::BankTransaction->new;
60 $bt_controller->action_save_invoices;
68 Support::TestSetup::login();
71 reset_state(); # initialise customers/vendors/bank/currency/...
75 test_overpayment_with_partialpayment();
80 test_partial_payment();
82 test_ap_transaction();
83 test_neg_ap_transaction(invoice => 0);
84 test_neg_ap_transaction(invoice => 1);
85 test_ap_payment_transaction();
86 test_ap_payment_part_transaction();
87 test_neg_sales_invoice();
88 test_two_neg_ap_transaction();
89 test_one_inv_and_two_invoices_with_skonto_exact();
98 test_two_banktransactions();
99 # remove all created data at end of test
104 ###### functions for setting up data
109 $params{$_} ||= {} for qw(unit customer tax vendor);
113 $transdate1 = DateTime->today;
114 $transdate2 = DateTime->today->add(days => 5);
116 $tax = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} }) || croak "No tax";
117 $tax7 = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07) || croak "No tax for 7\%";
118 $tax_9 = SL::DB::Manager::Tax->find_by(taxkey => 9, rate => 0.19, %{ $params{tax} }) || croak "No tax for 19\%";
119 $tax0 = SL::DB::Manager::Tax->find_by(taxkey => 0, rate => 0.0) || croak "No tax for 0\%";
121 $currency_id = $::instance_conf->get_currency_id;
123 $bank_account = SL::DB::BankAccount->new(
124 account_number => '123',
129 chart_id => SL::DB::Manager::Chart->find_by(description => 'Bank')->id,
130 name => SL::DB::Manager::Chart->find_by(description => 'Bank')->description,
133 $customer = new_customer(
134 name => 'Test Customer OLÉ S.L. Årdbärg AB',
135 iban => 'DE12500105170648489890',
137 account_number => '648489890',
138 mandate_date_of_signature => $transdate1,
139 mandator_id => 'foobar',
141 bank_code => 'G1235',
142 depositor => 'Test Customer',
143 customernumber => 'CUST1704',
146 $payment_terms = create_payment_terms();
148 $vendor = new_vendor(
149 name => 'Test Vendor',
150 payment_id => $payment_terms->id,
151 iban => 'DE12500105170648489890',
153 account_number => '648489890',
155 bank_code => 'G1235',
156 depositor => 'Test Vendor',
157 vendornumber => 'VEND1704',
160 $ar_chart = SL::DB::Manager::Chart->find_by( accno => '1400' ); # Forderungen
161 $ap_chart = SL::DB::Manager::Chart->find_by( accno => '1600' ); # Verbindlichkeiten
162 $bank = SL::DB::Manager::Chart->find_by( accno => '1200' ); # Bank
163 $ar_amount_chart = SL::DB::Manager::Chart->find_by( accno => '8400' ); # Erlöse
164 $ap_amount_chart = SL::DB::Manager::Chart->find_by( accno => '3400' ); # Wareneingang 19%
168 sub test_ar_transaction {
170 my $netamount = $params{amount} || 100;
171 my $amount = $::form->round_amount($netamount * 1.19,2);
172 my $invoice = SL::DB::Invoice->new(
174 invnumber => $params{invnumber} || undef, # let it use its own invnumber
176 netamount => $netamount,
177 transdate => $transdate1,
178 taxincluded => $params{taxincluded } || 0,
179 customer_id => $customer->id,
180 taxzone_id => $customer->taxzone_id,
181 currency_id => $currency_id,
183 payment_id => $params{payment_id} || undef,
184 notes => 'test_ar_transaction',
186 $invoice->add_ar_amount_row(
187 amount => $invoice->netamount,
188 chart => $ar_amount_chart,
189 tax_id => $params{tax_id} || $tax->id,
192 $invoice->create_ar_row(chart => $ar_chart);
195 is($invoice->currency_id , $currency_id , 'currency_id has been saved');
196 is($invoice->netamount , $netamount , 'ar amount has been converted');
197 is($invoice->amount , $amount , 'ar amount has been converted');
198 is($invoice->taxincluded , 0 , 'ar transaction doesn\'t have taxincluded');
200 if ( $netamount == 100 ) {
201 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');
202 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');
207 sub test_ap_transaction {
209 my $testname = 'test_ap_transaction';
212 my $amount = $::form->round_amount($netamount * 1.19,2);
213 my $invoice = SL::DB::PurchaseInvoice->new(
215 invnumber => $params{invnumber} || $testname,
217 netamount => $netamount,
218 transdate => $transdate1,
220 vendor_id => $vendor->id,
221 taxzone_id => $vendor->taxzone_id,
222 currency_id => $currency_id,
224 notes => 'test_ap_transaction',
226 $invoice->add_ap_amount_row(
227 amount => $invoice->netamount,
228 chart => $ap_amount_chart,
229 tax_id => $params{tax_id} || $tax_9->id,
232 $invoice->create_ap_row(chart => $ap_chart);
235 is($invoice->currency_id , $currency_id , "$testname: currency_id has been saved");
236 is($invoice->netamount , 100 , "$testname: ap amount has been converted");
237 is($invoice->amount , 119 , "$testname: ap amount has been converted");
238 is($invoice->taxincluded , 0 , "$testname: ap transaction doesn\'t have taxincluded");
240 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');
241 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');
250 my $testname = 'test1';
252 $ar_transaction = test_ar_transaction(invnumber => 'salesinv1');
254 my $bt = create_bank_transaction(record => $ar_transaction) or die "Couldn't create bank_transaction";
256 $::form->{invoice_ids} = {
257 $bt->id => [ $ar_transaction->id ]
260 save_btcontroller_to_string();
262 $ar_transaction->load;
264 is($ar_transaction->paid , '119.00000' , "$testname: salesinv1 was paid");
265 is($ar_transaction->closed , 1 , "$testname: salesinv1 is closed");
266 is($bt->invoice_amount , '119.00000' , "$testname: bt invoice amount was assigned");
270 sub test_skonto_exact {
272 my $testname = 'test_skonto_exact';
274 $ar_transaction = test_ar_transaction(invnumber => 'salesinv skonto',
275 payment_id => $payment_terms->id,
278 my $bt = create_bank_transaction(record => $ar_transaction,
279 bank_chart_id => $bank->id,
280 amount => $ar_transaction->amount_less_skonto
281 ) or die "Couldn't create bank_transaction";
283 $::form->{invoice_ids} = {
284 $bt->id => [ $ar_transaction->id ]
286 $::form->{invoice_skontos} = {
287 $bt->id => [ 'with_skonto_pt' ]
290 save_btcontroller_to_string();
292 $ar_transaction->load;
294 is($ar_transaction->paid , '119.00000' , "$testname: salesinv skonto was paid");
295 is($ar_transaction->closed , 1 , "$testname: salesinv skonto is closed");
296 is($bt->invoice_amount , '113.05000' , "$testname: bt invoice amount was assigned");
302 my $testname = 'test_free_skonto';
304 $ar_transaction = test_ar_transaction(invnumber => 'salesinv skonto',
305 payment_id => $payment_terms->id,
307 amount => 168.58 / 1.19,
310 my $bt = create_bank_transaction(record => $ar_transaction,
311 bank_chart_id => $bank->id,
313 ) or die "Couldn't create bank_transaction";
315 $::form->{invoice_ids} = {
316 $bt->id => [ $ar_transaction->id ]
318 $::form->{invoice_skontos} = {
319 $bt->id => [ 'with_free_skonto' ]
322 is($ar_transaction->paid , '0' , "$testname: salesinv is not paid");
324 # generate an error for testing rollback mechanism
325 my $saved_skonto_sales_chart_id = $tax->skonto_sales_chart_id;
326 $tax->skonto_sales_chart_id(undef);
329 save_btcontroller_to_string();
330 my @bt_errors = @{ $bt_controller->problems };
331 is(substr($bt_errors[0]->{message},0,38), 'Kein Skontokonto für Steuerschlüssel 3', "$testname: Fehlermeldung ok");
333 $tax->skonto_sales_chart_id($saved_skonto_sales_chart_id);
336 $ar_transaction->load;
338 is($ar_transaction->paid , '0.00000' , "$testname: salesinv was not paid");
339 is($bt->invoice_amount , '0.00000' , "$testname: bt invoice amount was not assigned");
343 sub test_two_invoices {
345 my $testname = 'test_two_invoices';
347 my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv_1');
348 my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv_2');
350 my $bt = create_bank_transaction(record => $ar_transaction_1,
351 amount => ($ar_transaction_1->amount + $ar_transaction_2->amount),
352 purpose => "Rechnungen " . $ar_transaction_1->invnumber . " und " . $ar_transaction_2->invnumber,
353 bank_chart_id => $bank->id,
354 ) or die "Couldn't create bank_transaction";
356 my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($ar_transaction_1);
357 is($agreement, 16, "points for ar_transaction_1 in test_two_invoices ok");
359 $::form->{invoice_ids} = {
360 $bt->id => [ $ar_transaction_1->id, $ar_transaction_2->id ]
363 save_btcontroller_to_string();
365 $ar_transaction_1->load;
366 $ar_transaction_2->load;
369 is($ar_transaction_1->paid , '119.00000' , "$testname: salesinv_1 wcsv_import_reportsas paid");
370 is($ar_transaction_1->closed , 1 , "$testname: salesinv_1 is closed");
371 is($ar_transaction_2->paid , '119.00000' , "$testname: salesinv_2 was paid");
372 is($ar_transaction_2->closed , 1 , "$testname: salesinv_2 is closed");
373 is($bt->invoice_amount , '238.00000' , "$testname: bt invoice amount was assigned");
377 sub test_one_inv_and_two_invoices_with_skonto_exact {
379 my $testname = 'test_two_invoices_with_skonto_exact';
381 my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv 1 skonto',
382 payment_id => $payment_terms->id,
384 my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv 2 skonto',
385 payment_id => $payment_terms->id,
387 my $ar_transaction_3 = test_ar_transaction(invnumber => 'salesinv 3 no skonto');
391 my $bt = create_bank_transaction(record => $ar_transaction_1,
392 bank_chart_id => $bank->id,
393 amount => $ar_transaction_1->amount_less_skonto * 2 + $ar_transaction_3->amount
394 ) or die "Couldn't create bank_transaction";
396 $::form->{invoice_ids} = {
397 $bt->id => [ $ar_transaction_1->id, $ar_transaction_3->id, $ar_transaction_2->id]
399 $::form->{invoice_skontos} = {
400 $bt->id => [ 'with_skonto_pt', 'without_skonto', 'with_skonto_pt' ]
403 save_btcontroller_to_string();
405 $ar_transaction_1->load;
406 $ar_transaction_2->load;
407 $ar_transaction_3->load;
408 my $skonto_1 = SL::DB::Manager::AccTransaction->find_by(trans_id => $ar_transaction_1->id, chart_id => 162);
409 my $skonto_2 = SL::DB::Manager::AccTransaction->find_by(trans_id => $ar_transaction_2->id, chart_id => 162);
411 is($skonto_1->amount , '-5.95000' , "$testname: salesinv 1 skonto was booked");
412 is($skonto_2->amount , '-5.95000' , "$testname: salesinv 2 skonto was booked");
413 is($ar_transaction_1->paid , '119.00000' , "$testname: salesinv 1 was paid");
414 is($ar_transaction_2->paid , '119.00000' , "$testname: salesinv 2 was paid");
415 is($ar_transaction_3->paid , '119.00000' , "$testname: salesinv 3 was paid");
416 is($ar_transaction_1->closed , 1 , "$testname: salesinv 1 skonto is closed");
417 is($ar_transaction_2->closed , 1 , "$testname: salesinv 2 skonto is closed");
418 is($ar_transaction_3->closed , 1 , "$testname: salesinv 2 skonto is closed");
419 is($bt->invoice_amount , '345.10000' , "$testname: bt invoice amount was assigned");
423 sub test_overpayment {
425 my $testname = 'test_overpayment';
427 $ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid');
430 my $bt = create_bank_transaction(record => $ar_transaction,
431 bank_chart_id => $bank->id,
433 ) or die "Couldn't create bank_transaction";
435 $::form->{invoice_ids} = {
436 $bt->id => [ $ar_transaction->id ]
439 save_btcontroller_to_string();
441 $ar_transaction->load;
444 is($ar_transaction->paid , '135.00000' , "$testname: 'salesinv overpaid' was overpaid");
445 is($bt->invoice_amount , '135.00000' , "$testname: bt invoice amount was assigned overpaid amount");
446 { local $TODO = 'this currently fails because closed ignores over-payments, see commit d90966c7';
447 is($ar_transaction->closed , 0 , "$testname: 'salesinv overpaid' is open (via 'closed' method')");
449 is($ar_transaction->open_amount == 0 ? 1 : 0 , 0 , "$testname: 'salesinv overpaid is open (via amount-paid)");
453 sub test_overpayment_with_partialpayment {
455 # two payments on different days, 10 and 119. If there is only one invoice we want it be overpaid.
456 my $testname = 'test_overpayment_with_partialpayment';
458 $ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid partial');
460 my $bt_1 = create_bank_transaction(record => $ar_transaction,
461 bank_chart_id => $bank->id,
463 ) or die "Couldn't create bank_transaction";
464 my $bt_2 = create_bank_transaction(record => $ar_transaction,
466 transdate => DateTime->today->add(days => 5),
467 bank_chart_id => $bank->id,
468 ) or die "Couldn't create bank_transaction";
470 $::form->{invoice_ids} = {
471 $bt_1->id => [ $ar_transaction->id ]
473 save_btcontroller_to_string();
475 $::form->{invoice_ids} = {
476 $bt_2->id => [ $ar_transaction->id ]
478 save_btcontroller_to_string();
480 $ar_transaction->load;
484 is($ar_transaction->paid , '129.00000' , "$testname: 'salesinv overpaid partial' was overpaid");
485 is($bt_1->invoice_amount , '10.00000' , "$testname: bt_1 invoice amount was assigned overpaid amount");
486 is($bt_2->invoice_amount , '119.00000' , "$testname: bt_2 invoice amount was assigned overpaid amount");
490 sub test_partial_payment {
492 my $testname = 'test_partial_payment';
494 $ar_transaction = test_ar_transaction(invnumber => 'salesinv partial payment');
497 my $bt = create_bank_transaction(record => $ar_transaction,
498 bank_chart_id => $bank->id,
500 ) or die "Couldn't create bank_transaction";
502 $::form->{invoice_ids} = {
503 $bt->id => [ $ar_transaction->id ]
506 save_btcontroller_to_string();
508 $ar_transaction->load;
511 is($ar_transaction->paid , '100.00000' , "$testname: 'salesinv partial payment' was partially paid");
512 is($bt->invoice_amount , '100.00000' , "$testname: bt invoice amount was assigned partially paid amount");
516 sub test_credit_note {
518 my $testname = 'test_credit_note';
520 my $part1 = new_part( partnumber => 'T4254')->save;
521 my $part2 = new_service(partnumber => 'Serv1')->save;
522 my $credit_note = create_credit_note(
524 customer => $customer,
526 invoiceitems => [ create_invoice_item(part => $part1, qty => 3, sellprice => 70),
527 create_invoice_item(part => $part2, qty => 10, sellprice => 50),
530 my $bt = create_bank_transaction(record => $credit_note,
531 amount => $credit_note->amount,
532 bank_chart_id => $bank->id,
533 transdate => DateTime->today->add(days => 10),
535 my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($credit_note);
536 is($agreement, 13, "points for credit note ok");
537 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");
539 $::form->{invoice_ids} = {
540 $bt->id => [ $credit_note->id ]
543 save_btcontroller_to_string();
547 is($credit_note->amount , '-844.90000', "$testname: amount ok");
548 is($credit_note->netamount, '-710.00000', "$testname: netamount ok");
549 is($credit_note->paid , '-844.90000', "$testname: paid ok");
552 sub test_neg_ap_transaction {
554 my $testname = 'test_neg_ap_transaction' . $params{invoice} ? ' invoice booking' : ' credit booking';
556 my $amount = $::form->round_amount($netamount * 1.19,2);
557 my $invoice = SL::DB::PurchaseInvoice->new(
558 invoice => $params{invoice} // 0,
559 invnumber => $params{invnumber} || 'test_neg_ap_transaction',
561 netamount => $netamount,
562 transdate => $transdate1,
564 vendor_id => $vendor->id,
565 taxzone_id => $vendor->taxzone_id,
566 currency_id => $currency_id,
568 notes => 'test_neg_ap_transaction',
570 $invoice->add_ap_amount_row(
571 amount => $invoice->netamount,
572 chart => $ap_amount_chart,
573 tax_id => $tax_9->id,
576 $invoice->create_ap_row(chart => $ap_chart);
579 is($invoice->netamount, -20 , "$testname: netamount ok");
580 is($invoice->amount , -23.8, "$testname: amount ok");
582 my $bt = create_bank_transaction(record => $invoice,
583 amount => $invoice->amount,
584 bank_chart_id => $bank->id,
585 transdate => DateTime->today->add(days => 10),
587 my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($invoice);
588 is($agreement, 15, "points for negative ap transaction ok");
590 $::form->{invoice_ids} = {
591 $bt->id => [ $invoice->id ]
594 save_btcontroller_to_string();
599 is($invoice->amount , '-23.80000', "$testname: amount ok");
600 is($invoice->netamount, '-20.00000', "$testname: netamount ok");
601 is($invoice->paid , '-23.80000', "$testname: paid ok");
602 is($bt->invoice_amount, '23.80000', "$testname: bt invoice amount for ap was assigned");
606 sub test_two_neg_ap_transaction {
607 my $testname='test_two_neg_ap_transaction';
609 my $amount = $::form->round_amount($netamount * 1.19,2);
610 my $invoice = SL::DB::PurchaseInvoice->new(
612 invnumber => 'test_neg_ap_transaction',
614 netamount => $netamount,
615 transdate => $transdate1,
617 vendor_id => $vendor->id,
618 taxzone_id => $vendor->taxzone_id,
619 currency_id => $currency_id,
621 notes => 'test_neg_ap_transaction',
623 $invoice->add_ap_amount_row(
624 amount => $invoice->netamount,
625 chart => $ap_amount_chart,
626 tax_id => $tax_9->id,
629 $invoice->create_ap_row(chart => $ap_chart);
632 is($invoice->netamount, -20 , "$testname: netamount ok");
633 is($invoice->amount , -23.8, "$testname: amount ok");
635 my $netamount_two = -1.14;
636 my $amount_two = $::form->round_amount($netamount_two * 1.19,2);
637 my $invoice_two = SL::DB::PurchaseInvoice->new(
639 invnumber => 'test_neg_ap_transaction_two',
640 amount => $amount_two,
641 netamount => $netamount_two,
642 transdate => $transdate1,
644 vendor_id => $vendor->id,
645 taxzone_id => $vendor->taxzone_id,
646 currency_id => $currency_id,
648 notes => 'test_neg_ap_transaction_two',
650 $invoice_two->add_ap_amount_row(
651 amount => $invoice_two->netamount,
652 chart => $ap_amount_chart,
653 tax_id => $tax_9->id,
656 $invoice_two->create_ap_row(chart => $ap_chart);
659 is($invoice_two->netamount, -1.14 , "$testname: netamount ok");
660 is($invoice_two->amount , -1.36, "$testname: amount ok");
663 my $bt = create_bank_transaction(record => $invoice_two,
664 amount => $invoice_two->amount + $invoice->amount,
665 bank_chart_id => $bank->id,
666 transdate => DateTime->today->add(days => 10),
668 # my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($invoice_two);
669 # is($agreement, 15, "points for negative ap transaction ok");
671 $::form->{invoice_ids} = {
672 $bt->id => [ $invoice->id, $invoice_two->id ]
675 save_btcontroller_to_string();
681 is($invoice->amount , '-23.80000', "$testname: first inv amount ok");
682 is($invoice->netamount, '-20.00000', "$testname: first inv netamount ok");
683 is($invoice->paid , '-23.80000', "$testname: first inv paid ok");
684 is($invoice_two->amount , '-1.36000', "$testname: second inv amount ok");
685 is($invoice_two->netamount, '-1.14000', "$testname: second inv netamount ok");
686 is($invoice_two->paid , '-1.36000', "$testname: second inv paid ok");
687 is($bt->invoice_amount, '25.16000', "$testname: bt invoice amount for both invoices were assigned");
690 return ($invoice, $invoice_two);
693 sub test_ap_payment_transaction {
695 my $testname = 'test_ap_payment_transaction';
697 my $amount = $::form->round_amount($netamount * 1.19,2);
698 my $invoice = SL::DB::PurchaseInvoice->new(
700 invnumber => $params{invnumber} || $testname,
702 netamount => $netamount,
703 transdate => $transdate1,
705 vendor_id => $vendor->id,
706 taxzone_id => $vendor->taxzone_id,
707 currency_id => $currency_id,
711 $invoice->add_ap_amount_row(
712 amount => $invoice->netamount,
713 chart => $ap_amount_chart,
714 tax_id => $tax_9->id,
717 $invoice->create_ap_row(chart => $ap_chart);
720 is($invoice->netamount, 115 , "$testname: netamount ok");
721 is($invoice->amount , 136.85, "$testname: amount ok");
723 my $bt = create_bank_transaction(record => $invoice,
724 amount => $invoice->amount,
725 bank_chart_id => $bank->id,
726 transdate => DateTime->today->add(days => 10),
728 $::form->{invoice_ids} = {
729 $bt->id => [ $invoice->id ]
732 save_btcontroller_to_string();
737 is($invoice->amount , '136.85000', "$testname: amount ok");
738 is($invoice->netamount, '115.00000', "$testname: netamount ok");
739 is($bt->amount, '-136.85000', "$testname: bt amount ok");
740 is($invoice->paid , '136.85000', "$testname: paid ok");
741 is($bt->invoice_amount, '-136.85000', "$testname: bt invoice amount for ap was assigned");
746 sub test_ap_payment_part_transaction {
748 my $testname = 'test_ap_payment_p_transaction';
750 my $amount = $::form->round_amount($netamount * 1.19,2);
751 my $invoice = SL::DB::PurchaseInvoice->new(
753 invnumber => $params{invnumber} || $testname,
755 netamount => $netamount,
756 transdate => $transdate1,
758 vendor_id => $vendor->id,
759 taxzone_id => $vendor->taxzone_id,
760 currency_id => $currency_id,
764 $invoice->add_ap_amount_row(
765 amount => $invoice->netamount,
766 chart => $ap_amount_chart,
767 tax_id => $tax_9->id,
770 $invoice->create_ap_row(chart => $ap_chart);
773 is($invoice->netamount, 115 , "$testname: netamount ok");
774 is($invoice->amount , 136.85, "$testname: amount ok");
776 my $bt = create_bank_transaction(record => $invoice,
777 amount => $invoice->amount-100,
778 bank_chart_id => $bank->id,
779 transdate => DateTime->today->add(days => 10),
781 $::form->{invoice_ids} = {
782 $bt->id => [ $invoice->id ]
785 save_btcontroller_to_string();
790 is($invoice->amount , '136.85000', "$testname: amount ok");
791 is($invoice->netamount, '115.00000', "$testname: netamount ok");
792 is($bt->amount, '-36.85000', "$testname: bt amount ok");
793 is($invoice->paid , '36.85000', "$testname: paid ok");
794 is($bt->invoice_amount, '-36.85000', "$testname: bt invoice amount for ap was assigned");
796 my $bt2 = create_bank_transaction(record => $invoice,
798 bank_chart_id => $bank->id,
799 transdate => DateTime->today->add(days => 10),
801 $::form->{invoice_ids} = {
802 $bt2->id => [ $invoice->id ]
805 save_btcontroller_to_string();
809 is($invoice->amount , '136.85000', "$testname: amount ok");
810 is($invoice->netamount, '115.00000', "$testname: netamount ok");
811 is($bt2->amount, '-100.00000',"$testname: bt amount ok");
812 is($invoice->paid , '136.85000', "$testname: paid ok");
813 is($bt2->invoice_amount,'-100.00000', "$testname: bt invoice amount for ap was assigned");
818 sub test_neg_sales_invoice {
820 my $testname = 'test_neg_sales_invoice';
822 my $part1 = new_part( partnumber => 'Funkenhaube öhm')->save;
823 my $part2 = new_service(partnumber => 'Service-Pauschale Pasch!')->save;
825 my $neg_sales_inv = create_sales_invoice(
826 invnumber => '20172201',
827 customer => $customer,
829 invoiceitems => [ create_invoice_item(part => $part1, qty => 3, sellprice => 70),
830 create_invoice_item(part => $part2, qty => 10, sellprice => -50),
833 my $bt = create_bank_transaction(record => $neg_sales_inv,
834 amount => $neg_sales_inv->amount,
835 bank_chart_id => $bank->id,
836 transdate => DateTime->today,
838 $::form->{invoice_ids} = {
839 $bt->id => [ $neg_sales_inv->id ]
842 save_btcontroller_to_string();
844 $neg_sales_inv->load;
846 is($neg_sales_inv->amount , '-345.10000', "$testname: amount ok");
847 is($neg_sales_inv->netamount, '-290.00000', "$testname: netamount ok");
848 is($neg_sales_inv->paid , '-345.10000', "$testname: paid ok");
849 is($bt->amount , '-345.10000', "$testname: bt amount ok");
850 is($bt->invoice_amount , '-345.10000', "$testname: bt invoice_amount ok");
855 my $testname = 'test_bt_rule1';
857 $ar_transaction = test_ar_transaction(invnumber => 'bt_rule1');
859 my $bt = create_bank_transaction(record => $ar_transaction) or die "Couldn't create bank_transaction";
861 $ar_transaction->load;
863 is($ar_transaction->paid , '0.00000' , "$testname: not paid");
864 is($bt->invoice_amount , '0.00000' , "$testname: bt invoice amount was not assigned");
866 my $bt_controller = SL::Controller::BankTransaction->new;
867 $::form->{dont_render_for_test} = 1;
868 $::form->{filter}{bank_account} = $bank_account->id;
869 my ( $bt_transactions, $proposals ) = $bt_controller->action_list;
871 is(scalar(@$bt_transactions) , 1 , "$testname: one bank_transaction");
872 is($bt_transactions->[0]->{agreement}, 20 , "$testname: agreement == 20");
873 my $match = join ( ' ',@{$bt_transactions->[0]->{rule_matches}});
874 #print "rule_matches='".$match."'\n";
876 "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) ",
877 "$testname: rule_matches ok");
878 $bt->invoice_amount($bt->amount);
880 is($bt->invoice_amount , '119.00000' , "$testname: bt invoice amount now set");
883 sub test_sepa_export {
885 my $testname = 'test_sepa_export';
887 $ar_transaction = test_ar_transaction(invnumber => 'sepa1');
889 my $bt = create_bank_transaction(record => $ar_transaction) or die "Couldn't create bank_transaction";
890 my $se = create_sepa_export();
891 my $sei = create_sepa_export_item(
892 chart_id => $bank->id,
893 ar_id => $ar_transaction->id,
894 sepa_export_id => $se->id,
895 vc_iban => $customer->iban,
896 vc_bic => $customer->bic,
897 vc_mandator_id => $customer->mandator_id,
898 vc_depositor => $customer->depositor,
899 amount => $ar_transaction->amount,
901 require SL::SEPA::XML;
902 my $sepa_xml = SL::SEPA::XML->new('company' => $customer->name,
903 'creditor_id' => "id",
904 'src_charset' => 'UTF-8',
905 'message_id' => "test",
909 is($sepa_xml->{company} , 'Test Customer OLE S.L. Ardbaerg AB');
911 $ar_transaction->load;
914 is($ar_transaction->paid , '0.00000' , "$testname: sepa1 not paid");
915 is($bt->invoice_amount , '0.00000' , "$testname: bt invoice amount was not assigned");
916 is($bt->amount , '119.00000' , "$testname: bt amount ok");
917 is($sei->amount , '119.00000' , "$testname: sepa export amount ok");
919 my $bt_controller = SL::Controller::BankTransaction->new;
920 $::form->{dont_render_for_test} = 1;
921 $::form->{filter}{bank_account} = $bank_account->id;
922 my ( $bt_transactions, $proposals ) = $bt_controller->action_list;
924 is(scalar(@$bt_transactions) , 1 , "$testname: one bank_transaction");
925 is($bt_transactions->[0]->{agreement}, 25 , "$testname: agreement == 25");
926 my $match = join ( ' ',@{$bt_transactions->[0]->{rule_matches}});
928 "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) ",
929 "$testname: rule_matches ok");
932 sub test_two_banktransactions {
934 my $testname = 'two_banktransactions';
936 my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv10000' , amount => 2912.00 );
937 my $bt1 = create_bank_transaction(record => $ar_transaction_1,
938 amount => $ar_transaction_1->amount,
939 purpose => "Rechnung10000 beinahe",
940 bank_chart_id => $bank->id,
941 ) or die "Couldn't create bank_transaction";
943 my $bt2 = create_bank_transaction(record => $ar_transaction_1,
944 amount => $ar_transaction_1->amount + 0.01,
945 purpose => "sicher salesinv20000 vielleicht",
946 bank_chart_id => $bank->id,
947 ) or die "Couldn't create bank_transaction";
949 my ($agreement1, $rule_matches1) = $bt1->get_agreement_with_invoice($ar_transaction_1);
950 is($agreement1, 19, "bt1 19 points for ar_transaction_1 in $testname ok");
951 #print "rule_matches1=".$rule_matches1."\n";
953 "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) ",
954 "$testname: rule_matches ok");
955 my ($agreement2, $rule_matches2) = $bt2->get_agreement_with_invoice($ar_transaction_1);
956 is($agreement2, 11, "bt2 11 points for ar_transaction_1 in $testname ok");
958 "remote_account_number(3) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus0(3) ",
959 "$testname: rule_matches ok");
961 my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv20000' , amount => 2912.01 );
962 my $ar_transaction_3 = test_ar_transaction(invnumber => 'zweitemit10000', amount => 2912.00 );
963 ($agreement1, $rule_matches1) = $bt1->get_agreement_with_invoice($ar_transaction_2);
965 is($agreement1, 11, "bt1 11 points for ar_transaction_2 in $testname ok");
967 ($agreement2, $rule_matches2) = $bt2->get_agreement_with_invoice($ar_transaction_2);
968 is($agreement2, 20, "bt2 20 points for ar_transaction_2 in $testname ok");
970 ($agreement2, $rule_matches2) = $bt2->get_agreement_with_invoice($ar_transaction_1);
971 is($agreement2, 11, "bt2 11 points for ar_transaction_1 in $testname ok");
973 my $bt3 = create_bank_transaction(record => $ar_transaction_3,
974 amount => $ar_transaction_3->amount,
975 purpose => "sicher Rechnung10000 vielleicht",
976 bank_chart_id => $bank->id,
977 ) or die "Couldn't create bank_transaction";
979 my ($agreement3, $rule_matches3) = $bt3->get_agreement_with_invoice($ar_transaction_3);
980 is($agreement3, 19, "bt3 19 points for ar_transaction_3 in $testname ok");
983 $ar_transaction_2->delete;
985 #nun sollten zwei gleichwertige Rechnungen $ar_transaction_1 und $ar_transaction_3 für $bt1 gefunden werden
986 #aber es darf keine Proposals geben mit mehreren Rechnungen
987 my $bt_controller = SL::Controller::BankTransaction->new;
988 $::form->{dont_render_for_test} = 1;
989 $::form->{filter}{bank_account} = $bank_account->id;
990 my ( $bt_transactions, $proposals ) = $bt_controller->action_list;
992 is(scalar(@$bt_transactions) , 2 , "$testname: two bank_transaction");
993 is(scalar(@$proposals) , 0 , "$testname: no proposals");
995 $ar_transaction_3->delete;
997 # Jetzt gibt es zwei Kontobewegungen mit gleichen Punkten für eine Rechnung.
998 # hier darf es auch keine Proposals geben
1000 ( $bt_transactions, $proposals ) = $bt_controller->action_list;
1002 is(scalar(@$bt_transactions) , 2 , "$testname: two bank_transaction");
1003 is(scalar(@$proposals) , 0 , "$testname: no proposals");
1005 # Jetzt gibt es zwei Kontobewegungen für eine Rechnung.
1006 # eine Bewegung bekommt mehr Punkte
1007 # hier darf es auch keine Proposals geben
1008 $bt3->update_attributes( purpose => "fuer Rechnung salesinv10000");
1010 ( $bt_transactions, $proposals ) = $bt_controller->action_list;
1012 is(scalar(@$bt_transactions) , 2 , "$testname: two bank_transaction");
1013 is(scalar(@$proposals) , 1 , "$testname: one proposal");