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, $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' ]);
52 sub save_btcontroller_to_string {
54 open(my $outputFH, '>', \$output) or die;
55 my $oldFH = select $outputFH;
57 my $bt_controller = SL::Controller::BankTransaction->new;
58 $bt_controller->action_save_invoices;
66 Support::TestSetup::login();
69 reset_state(); # initialise customers/vendors/bank/currency/...
73 test_overpayment_with_partialpayment();
77 test_partial_payment();
79 test_ap_transaction();
80 test_neg_ap_transaction(invoice => 0);
81 test_neg_ap_transaction(invoice => 1);
82 test_ap_payment_transaction();
83 test_ap_payment_part_transaction();
84 test_neg_sales_invoice();
85 test_two_neg_ap_transaction();
86 test_one_inv_and_two_invoices_with_skonto_exact();
91 test_two_banktransactions();
92 # remove all created data at end of test
97 ###### functions for setting up data
102 $params{$_} ||= {} for qw(unit customer tax vendor);
106 $transdate1 = DateTime->today;
107 $transdate2 = DateTime->today->add(days => 5);
109 $tax = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} }) || croak "No tax";
110 $tax7 = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07) || croak "No tax for 7\%";
111 $tax_9 = SL::DB::Manager::Tax->find_by(taxkey => 9, rate => 0.19, %{ $params{tax} }) || croak "No tax";
113 $currency_id = $::instance_conf->get_currency_id;
115 $bank_account = SL::DB::BankAccount->new(
116 account_number => '123',
121 chart_id => SL::DB::Manager::Chart->find_by(description => 'Bank')->id,
122 name => SL::DB::Manager::Chart->find_by(description => 'Bank')->description,
125 $customer = new_customer(
126 name => 'Test Customer OLÉ S.L. Årdbärg AB',
127 iban => 'DE12500105170648489890',
129 account_number => '648489890',
130 mandate_date_of_signature => $transdate1,
131 mandator_id => 'foobar',
133 bank_code => 'G1235',
134 depositor => 'Test Customer',
135 customernumber => 'CUST1704',
138 $payment_terms = create_payment_terms();
140 $vendor = new_vendor(
141 name => 'Test Vendor',
142 payment_id => $payment_terms->id,
143 iban => 'DE12500105170648489890',
145 account_number => '648489890',
147 bank_code => 'G1235',
148 depositor => 'Test Vendor',
149 vendornumber => 'VEND1704',
152 $ar_chart = SL::DB::Manager::Chart->find_by( accno => '1400' ); # Forderungen
153 $ap_chart = SL::DB::Manager::Chart->find_by( accno => '1600' ); # Verbindlichkeiten
154 $bank = SL::DB::Manager::Chart->find_by( accno => '1200' ); # Bank
155 $ar_amount_chart = SL::DB::Manager::Chart->find_by( accno => '8400' ); # Erlöse
156 $ap_amount_chart = SL::DB::Manager::Chart->find_by( accno => '3400' ); # Wareneingang 19%
160 sub test_ar_transaction {
162 my $netamount = $params{amount} || 100;
163 my $amount = $::form->round_amount($netamount * 1.19,2);
164 my $invoice = SL::DB::Invoice->new(
166 invnumber => $params{invnumber} || undef, # let it use its own invnumber
168 netamount => $netamount,
169 transdate => $transdate1,
171 customer_id => $customer->id,
172 taxzone_id => $customer->taxzone_id,
173 currency_id => $currency_id,
175 payment_id => $params{payment_id} || undef,
176 notes => 'test_ar_transaction',
178 $invoice->add_ar_amount_row(
179 amount => $invoice->netamount,
180 chart => $ar_amount_chart,
184 $invoice->create_ar_row(chart => $ar_chart);
187 is($invoice->currency_id , $currency_id , 'currency_id has been saved');
188 is($invoice->netamount , $netamount , 'ar amount has been converted');
189 is($invoice->amount , $amount , 'ar amount has been converted');
190 is($invoice->taxincluded , 0 , 'ar transaction doesn\'t have taxincluded');
192 if ( $netamount == 100 ) {
193 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');
194 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');
199 sub test_ap_transaction {
201 my $testname = 'test_ap_transaction';
204 my $amount = $::form->round_amount($netamount * 1.19,2);
205 my $invoice = SL::DB::PurchaseInvoice->new(
207 invnumber => $params{invnumber} || $testname,
209 netamount => $netamount,
210 transdate => $transdate1,
212 vendor_id => $vendor->id,
213 taxzone_id => $vendor->taxzone_id,
214 currency_id => $currency_id,
216 notes => 'test_ap_transaction',
218 $invoice->add_ap_amount_row(
219 amount => $invoice->netamount,
220 chart => $ap_amount_chart,
221 tax_id => $tax_9->id,
224 $invoice->create_ap_row(chart => $ap_chart);
227 is($invoice->currency_id , $currency_id , "$testname: currency_id has been saved");
228 is($invoice->netamount , 100 , "$testname: ap amount has been converted");
229 is($invoice->amount , 119 , "$testname: ap amount has been converted");
230 is($invoice->taxincluded , 0 , "$testname: ap transaction doesn\'t have taxincluded");
232 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');
233 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');
242 my $testname = 'test1';
244 $ar_transaction = test_ar_transaction(invnumber => 'salesinv1');
246 my $bt = create_bank_transaction(record => $ar_transaction) or die "Couldn't create bank_transaction";
248 $::form->{invoice_ids} = {
249 $bt->id => [ $ar_transaction->id ]
252 save_btcontroller_to_string();
254 $ar_transaction->load;
256 is($ar_transaction->paid , '119.00000' , "$testname: salesinv1 was paid");
257 is($ar_transaction->closed , 1 , "$testname: salesinv1 is closed");
258 is($bt->invoice_amount , '119.00000' , "$testname: bt invoice amount was assigned");
262 sub test_skonto_exact {
264 my $testname = 'test_skonto_exact';
266 $ar_transaction = test_ar_transaction(invnumber => 'salesinv skonto',
267 payment_id => $payment_terms->id,
270 my $bt = create_bank_transaction(record => $ar_transaction,
271 bank_chart_id => $bank->id,
272 amount => $ar_transaction->amount_less_skonto
273 ) or die "Couldn't create bank_transaction";
275 $::form->{invoice_ids} = {
276 $bt->id => [ $ar_transaction->id ]
278 $::form->{invoice_skontos} = {
279 $bt->id => [ 'with_skonto_pt' ]
282 save_btcontroller_to_string();
284 $ar_transaction->load;
286 is($ar_transaction->paid , '119.00000' , "$testname: salesinv skonto was paid");
287 is($ar_transaction->closed , 1 , "$testname: salesinv skonto is closed");
288 is($bt->invoice_amount , '113.05000' , "$testname: bt invoice amount was assigned");
292 sub test_two_invoices {
294 my $testname = 'test_two_invoices';
296 my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv_1');
297 my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv_2');
299 my $bt = create_bank_transaction(record => $ar_transaction_1,
300 amount => ($ar_transaction_1->amount + $ar_transaction_2->amount),
301 purpose => "Rechnungen " . $ar_transaction_1->invnumber . " und " . $ar_transaction_2->invnumber,
302 bank_chart_id => $bank->id,
303 ) or die "Couldn't create bank_transaction";
305 my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($ar_transaction_1);
306 is($agreement, 16, "points for ar_transaction_1 in test_two_invoices ok");
308 $::form->{invoice_ids} = {
309 $bt->id => [ $ar_transaction_1->id, $ar_transaction_2->id ]
312 save_btcontroller_to_string();
314 $ar_transaction_1->load;
315 $ar_transaction_2->load;
318 is($ar_transaction_1->paid , '119.00000' , "$testname: salesinv_1 wcsv_import_reportsas paid");
319 is($ar_transaction_1->closed , 1 , "$testname: salesinv_1 is closed");
320 is($ar_transaction_2->paid , '119.00000' , "$testname: salesinv_2 was paid");
321 is($ar_transaction_2->closed , 1 , "$testname: salesinv_2 is closed");
322 is($bt->invoice_amount , '238.00000' , "$testname: bt invoice amount was assigned");
326 sub test_one_inv_and_two_invoices_with_skonto_exact {
328 my $testname = 'test_two_invoices_with_skonto_exact';
330 my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv 1 skonto',
331 payment_id => $payment_terms->id,
333 my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv 2 skonto',
334 payment_id => $payment_terms->id,
336 my $ar_transaction_3 = test_ar_transaction(invnumber => 'salesinv 3 no skonto');
340 my $bt = create_bank_transaction(record => $ar_transaction_1,
341 bank_chart_id => $bank->id,
342 amount => $ar_transaction_1->amount_less_skonto * 2 + $ar_transaction_3->amount
343 ) or die "Couldn't create bank_transaction";
345 $::form->{invoice_ids} = {
346 $bt->id => [ $ar_transaction_1->id, $ar_transaction_3->id, $ar_transaction_2->id]
348 $::form->{invoice_skontos} = {
349 $bt->id => [ 'with_skonto_pt', 'without_skonto', 'with_skonto_pt' ]
352 save_btcontroller_to_string();
354 $ar_transaction_1->load;
355 $ar_transaction_2->load;
356 $ar_transaction_3->load;
357 my $skonto_1 = SL::DB::Manager::AccTransaction->find_by(trans_id => $ar_transaction_1->id, chart_id => 162);
358 my $skonto_2 = SL::DB::Manager::AccTransaction->find_by(trans_id => $ar_transaction_2->id, chart_id => 162);
360 is($skonto_1->amount , '-5.95000' , "$testname: salesinv 1 skonto was booked");
361 is($skonto_2->amount , '-5.95000' , "$testname: salesinv 2 skonto was booked");
362 is($ar_transaction_1->paid , '119.00000' , "$testname: salesinv 1 was paid");
363 is($ar_transaction_2->paid , '119.00000' , "$testname: salesinv 2 was paid");
364 is($ar_transaction_3->paid , '119.00000' , "$testname: salesinv 3 was paid");
365 is($ar_transaction_1->closed , 1 , "$testname: salesinv 1 skonto is closed");
366 is($ar_transaction_2->closed , 1 , "$testname: salesinv 2 skonto is closed");
367 is($ar_transaction_3->closed , 1 , "$testname: salesinv 2 skonto is closed");
368 is($bt->invoice_amount , '345.10000' , "$testname: bt invoice amount was assigned");
372 sub test_overpayment {
374 my $testname = 'test_overpayment';
376 $ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid');
379 my $bt = create_bank_transaction(record => $ar_transaction,
380 bank_chart_id => $bank->id,
382 ) or die "Couldn't create bank_transaction";
384 $::form->{invoice_ids} = {
385 $bt->id => [ $ar_transaction->id ]
388 save_btcontroller_to_string();
390 $ar_transaction->load;
393 is($ar_transaction->paid , '135.00000' , "$testname: 'salesinv overpaid' was overpaid");
394 is($bt->invoice_amount , '135.00000' , "$testname: bt invoice amount was assigned overpaid amount");
395 { local $TODO = 'this currently fails because closed ignores over-payments, see commit d90966c7';
396 is($ar_transaction->closed , 0 , "$testname: 'salesinv overpaid' is open (via 'closed' method')");
398 is($ar_transaction->open_amount == 0 ? 1 : 0 , 0 , "$testname: 'salesinv overpaid is open (via amount-paid)");
402 sub test_overpayment_with_partialpayment {
404 # two payments on different days, 10 and 119. If there is only one invoice we want it be overpaid.
405 my $testname = 'test_overpayment_with_partialpayment';
407 $ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid partial');
409 my $bt_1 = create_bank_transaction(record => $ar_transaction,
410 bank_chart_id => $bank->id,
412 ) or die "Couldn't create bank_transaction";
413 my $bt_2 = create_bank_transaction(record => $ar_transaction,
415 transdate => DateTime->today->add(days => 5),
416 bank_chart_id => $bank->id,
417 ) or die "Couldn't create bank_transaction";
419 $::form->{invoice_ids} = {
420 $bt_1->id => [ $ar_transaction->id ]
422 save_btcontroller_to_string();
424 $::form->{invoice_ids} = {
425 $bt_2->id => [ $ar_transaction->id ]
427 save_btcontroller_to_string();
429 $ar_transaction->load;
433 is($ar_transaction->paid , '129.00000' , "$testname: 'salesinv overpaid partial' was overpaid");
434 is($bt_1->invoice_amount , '10.00000' , "$testname: bt_1 invoice amount was assigned overpaid amount");
435 is($bt_2->invoice_amount , '119.00000' , "$testname: bt_2 invoice amount was assigned overpaid amount");
439 sub test_partial_payment {
441 my $testname = 'test_partial_payment';
443 $ar_transaction = test_ar_transaction(invnumber => 'salesinv partial payment');
446 my $bt = create_bank_transaction(record => $ar_transaction,
447 bank_chart_id => $bank->id,
449 ) or die "Couldn't create bank_transaction";
451 $::form->{invoice_ids} = {
452 $bt->id => [ $ar_transaction->id ]
455 save_btcontroller_to_string();
457 $ar_transaction->load;
460 is($ar_transaction->paid , '100.00000' , "$testname: 'salesinv partial payment' was partially paid");
461 is($bt->invoice_amount , '100.00000' , "$testname: bt invoice amount was assigned partially paid amount");
465 sub test_credit_note {
467 my $testname = 'test_credit_note';
469 my $part1 = new_part( partnumber => 'T4254')->save;
470 my $part2 = new_service(partnumber => 'Serv1')->save;
471 my $credit_note = create_credit_note(
473 customer => $customer,
475 invoiceitems => [ create_invoice_item(part => $part1, qty => 3, sellprice => 70),
476 create_invoice_item(part => $part2, qty => 10, sellprice => 50),
479 my $bt = create_bank_transaction(record => $credit_note,
480 amount => $credit_note->amount,
481 bank_chart_id => $bank->id,
482 transdate => DateTime->today->add(days => 10),
484 my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($credit_note);
485 is($agreement, 13, "points for credit note ok");
486 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");
488 $::form->{invoice_ids} = {
489 $bt->id => [ $credit_note->id ]
492 save_btcontroller_to_string();
496 is($credit_note->amount , '-844.90000', "$testname: amount ok");
497 is($credit_note->netamount, '-710.00000', "$testname: netamount ok");
498 is($credit_note->paid , '-844.90000', "$testname: paid ok");
501 sub test_neg_ap_transaction {
503 my $testname = 'test_neg_ap_transaction' . $params{invoice} ? ' invoice booking' : ' credit booking';
505 my $amount = $::form->round_amount($netamount * 1.19,2);
506 my $invoice = SL::DB::PurchaseInvoice->new(
507 invoice => $params{invoice} // 0,
508 invnumber => $params{invnumber} || 'test_neg_ap_transaction',
510 netamount => $netamount,
511 transdate => $transdate1,
513 vendor_id => $vendor->id,
514 taxzone_id => $vendor->taxzone_id,
515 currency_id => $currency_id,
517 notes => 'test_neg_ap_transaction',
519 $invoice->add_ap_amount_row(
520 amount => $invoice->netamount,
521 chart => $ap_amount_chart,
522 tax_id => $tax_9->id,
525 $invoice->create_ap_row(chart => $ap_chart);
528 is($invoice->netamount, -20 , "$testname: netamount ok");
529 is($invoice->amount , -23.8, "$testname: amount ok");
531 my $bt = create_bank_transaction(record => $invoice,
532 amount => $invoice->amount,
533 bank_chart_id => $bank->id,
534 transdate => DateTime->today->add(days => 10),
536 my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($invoice);
537 is($agreement, 15, "points for negative ap transaction ok");
539 $::form->{invoice_ids} = {
540 $bt->id => [ $invoice->id ]
543 save_btcontroller_to_string();
548 is($invoice->amount , '-23.80000', "$testname: amount ok");
549 is($invoice->netamount, '-20.00000', "$testname: netamount ok");
550 is($invoice->paid , '-23.80000', "$testname: paid ok");
551 is($bt->invoice_amount, '23.80000', "$testname: bt invoice amount for ap was assigned");
555 sub test_two_neg_ap_transaction {
556 my $testname='test_two_neg_ap_transaction';
558 my $amount = $::form->round_amount($netamount * 1.19,2);
559 my $invoice = SL::DB::PurchaseInvoice->new(
561 invnumber => 'test_neg_ap_transaction',
563 netamount => $netamount,
564 transdate => $transdate1,
566 vendor_id => $vendor->id,
567 taxzone_id => $vendor->taxzone_id,
568 currency_id => $currency_id,
570 notes => 'test_neg_ap_transaction',
572 $invoice->add_ap_amount_row(
573 amount => $invoice->netamount,
574 chart => $ap_amount_chart,
575 tax_id => $tax_9->id,
578 $invoice->create_ap_row(chart => $ap_chart);
581 is($invoice->netamount, -20 , "$testname: netamount ok");
582 is($invoice->amount , -23.8, "$testname: amount ok");
584 my $netamount_two = -1.14;
585 my $amount_two = $::form->round_amount($netamount_two * 1.19,2);
586 my $invoice_two = SL::DB::PurchaseInvoice->new(
588 invnumber => 'test_neg_ap_transaction_two',
589 amount => $amount_two,
590 netamount => $netamount_two,
591 transdate => $transdate1,
593 vendor_id => $vendor->id,
594 taxzone_id => $vendor->taxzone_id,
595 currency_id => $currency_id,
597 notes => 'test_neg_ap_transaction_two',
599 $invoice_two->add_ap_amount_row(
600 amount => $invoice_two->netamount,
601 chart => $ap_amount_chart,
602 tax_id => $tax_9->id,
605 $invoice_two->create_ap_row(chart => $ap_chart);
608 is($invoice_two->netamount, -1.14 , "$testname: netamount ok");
609 is($invoice_two->amount , -1.36, "$testname: amount ok");
612 my $bt = create_bank_transaction(record => $invoice_two,
613 amount => $invoice_two->amount + $invoice->amount,
614 bank_chart_id => $bank->id,
615 transdate => DateTime->today->add(days => 10),
617 # my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($invoice_two);
618 # is($agreement, 15, "points for negative ap transaction ok");
620 $::form->{invoice_ids} = {
621 $bt->id => [ $invoice->id, $invoice_two->id ]
624 save_btcontroller_to_string();
630 is($invoice->amount , '-23.80000', "$testname: first inv amount ok");
631 is($invoice->netamount, '-20.00000', "$testname: first inv netamount ok");
632 is($invoice->paid , '-23.80000', "$testname: first inv paid ok");
633 is($invoice_two->amount , '-1.36000', "$testname: second inv amount ok");
634 is($invoice_two->netamount, '-1.14000', "$testname: second inv netamount ok");
635 is($invoice_two->paid , '-1.36000', "$testname: second inv paid ok");
636 is($bt->invoice_amount, '25.16000', "$testname: bt invoice amount for both invoices were assigned");
639 return ($invoice, $invoice_two);
642 sub test_ap_payment_transaction {
644 my $testname = 'test_ap_payment_transaction';
646 my $amount = $::form->round_amount($netamount * 1.19,2);
647 my $invoice = SL::DB::PurchaseInvoice->new(
649 invnumber => $params{invnumber} || $testname,
651 netamount => $netamount,
652 transdate => $transdate1,
654 vendor_id => $vendor->id,
655 taxzone_id => $vendor->taxzone_id,
656 currency_id => $currency_id,
660 $invoice->add_ap_amount_row(
661 amount => $invoice->netamount,
662 chart => $ap_amount_chart,
663 tax_id => $tax_9->id,
666 $invoice->create_ap_row(chart => $ap_chart);
669 is($invoice->netamount, 115 , "$testname: netamount ok");
670 is($invoice->amount , 136.85, "$testname: amount ok");
672 my $bt = create_bank_transaction(record => $invoice,
673 amount => $invoice->amount,
674 bank_chart_id => $bank->id,
675 transdate => DateTime->today->add(days => 10),
677 $::form->{invoice_ids} = {
678 $bt->id => [ $invoice->id ]
681 save_btcontroller_to_string();
686 is($invoice->amount , '136.85000', "$testname: amount ok");
687 is($invoice->netamount, '115.00000', "$testname: netamount ok");
688 is($bt->amount, '-136.85000', "$testname: bt amount ok");
689 is($invoice->paid , '136.85000', "$testname: paid ok");
690 is($bt->invoice_amount, '-136.85000', "$testname: bt invoice amount for ap was assigned");
695 sub test_ap_payment_part_transaction {
697 my $testname = 'test_ap_payment_p_transaction';
699 my $amount = $::form->round_amount($netamount * 1.19,2);
700 my $invoice = SL::DB::PurchaseInvoice->new(
702 invnumber => $params{invnumber} || $testname,
704 netamount => $netamount,
705 transdate => $transdate1,
707 vendor_id => $vendor->id,
708 taxzone_id => $vendor->taxzone_id,
709 currency_id => $currency_id,
713 $invoice->add_ap_amount_row(
714 amount => $invoice->netamount,
715 chart => $ap_amount_chart,
716 tax_id => $tax_9->id,
719 $invoice->create_ap_row(chart => $ap_chart);
722 is($invoice->netamount, 115 , "$testname: netamount ok");
723 is($invoice->amount , 136.85, "$testname: amount ok");
725 my $bt = create_bank_transaction(record => $invoice,
726 amount => $invoice->amount-100,
727 bank_chart_id => $bank->id,
728 transdate => DateTime->today->add(days => 10),
730 $::form->{invoice_ids} = {
731 $bt->id => [ $invoice->id ]
734 save_btcontroller_to_string();
739 is($invoice->amount , '136.85000', "$testname: amount ok");
740 is($invoice->netamount, '115.00000', "$testname: netamount ok");
741 is($bt->amount, '-36.85000', "$testname: bt amount ok");
742 is($invoice->paid , '36.85000', "$testname: paid ok");
743 is($bt->invoice_amount, '-36.85000', "$testname: bt invoice amount for ap was assigned");
745 my $bt2 = create_bank_transaction(record => $invoice,
747 bank_chart_id => $bank->id,
748 transdate => DateTime->today->add(days => 10),
750 $::form->{invoice_ids} = {
751 $bt2->id => [ $invoice->id ]
754 save_btcontroller_to_string();
758 is($invoice->amount , '136.85000', "$testname: amount ok");
759 is($invoice->netamount, '115.00000', "$testname: netamount ok");
760 is($bt2->amount, '-100.00000',"$testname: bt amount ok");
761 is($invoice->paid , '136.85000', "$testname: paid ok");
762 is($bt2->invoice_amount,'-100.00000', "$testname: bt invoice amount for ap was assigned");
767 sub test_neg_sales_invoice {
769 my $testname = 'test_neg_sales_invoice';
771 my $part1 = new_part( partnumber => 'Funkenhaube öhm')->save;
772 my $part2 = new_service(partnumber => 'Service-Pauschale Pasch!')->save;
774 my $neg_sales_inv = create_sales_invoice(
775 invnumber => '20172201',
776 customer => $customer,
778 invoiceitems => [ create_invoice_item(part => $part1, qty => 3, sellprice => 70),
779 create_invoice_item(part => $part2, qty => 10, sellprice => -50),
782 my $bt = create_bank_transaction(record => $neg_sales_inv,
783 amount => $neg_sales_inv->amount,
784 bank_chart_id => $bank->id,
785 transdate => DateTime->today,
787 $::form->{invoice_ids} = {
788 $bt->id => [ $neg_sales_inv->id ]
791 save_btcontroller_to_string();
793 $neg_sales_inv->load;
795 is($neg_sales_inv->amount , '-345.10000', "$testname: amount ok");
796 is($neg_sales_inv->netamount, '-290.00000', "$testname: netamount ok");
797 is($neg_sales_inv->paid , '-345.10000', "$testname: paid ok");
798 is($bt->amount , '-345.10000', "$testname: bt amount ok");
799 is($bt->invoice_amount , '-345.10000', "$testname: bt invoice_amount ok");
804 my $testname = 'test_bt_rule1';
806 $ar_transaction = test_ar_transaction(invnumber => 'bt_rule1');
808 my $bt = create_bank_transaction(record => $ar_transaction) or die "Couldn't create bank_transaction";
810 $ar_transaction->load;
812 is($ar_transaction->paid , '0.00000' , "$testname: not paid");
813 is($bt->invoice_amount , '0.00000' , "$testname: bt invoice amount was not assigned");
815 my $bt_controller = SL::Controller::BankTransaction->new;
816 $::form->{dont_render_for_test} = 1;
817 $::form->{filter}{bank_account} = $bank_account->id;
818 my ( $bt_transactions, $proposals ) = $bt_controller->action_list;
820 is(scalar(@$bt_transactions) , 1 , "$testname: one bank_transaction");
821 is($bt_transactions->[0]->{agreement}, 20 , "$testname: agreement == 20");
822 my $match = join ( ' ',@{$bt_transactions->[0]->{rule_matches}});
823 #print "rule_matches='".$match."'\n";
825 "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) ",
826 "$testname: rule_matches ok");
827 $bt->invoice_amount($bt->amount);
829 is($bt->invoice_amount , '119.00000' , "$testname: bt invoice amount now set");
832 sub test_sepa_export {
834 my $testname = 'test_sepa_export';
836 $ar_transaction = test_ar_transaction(invnumber => 'sepa1');
838 my $bt = create_bank_transaction(record => $ar_transaction) or die "Couldn't create bank_transaction";
839 my $se = create_sepa_export();
840 my $sei = create_sepa_export_item(
841 chart_id => $bank->id,
842 ar_id => $ar_transaction->id,
843 sepa_export_id => $se->id,
844 vc_iban => $customer->iban,
845 vc_bic => $customer->bic,
846 vc_mandator_id => $customer->mandator_id,
847 vc_depositor => $customer->depositor,
848 amount => $ar_transaction->amount,
850 require SL::SEPA::XML;
851 my $sepa_xml = SL::SEPA::XML->new('company' => $customer->name,
852 'creditor_id' => "id",
853 'src_charset' => 'UTF-8',
854 'message_id' => "test",
858 is($sepa_xml->{company} , 'Test Customer OLE S.L. Ardbaerg AB');
860 $ar_transaction->load;
863 is($ar_transaction->paid , '0.00000' , "$testname: sepa1 not paid");
864 is($bt->invoice_amount , '0.00000' , "$testname: bt invoice amount was not assigned");
865 is($bt->amount , '119.00000' , "$testname: bt amount ok");
866 is($sei->amount , '119.00000' , "$testname: sepa export amount ok");
868 my $bt_controller = SL::Controller::BankTransaction->new;
869 $::form->{dont_render_for_test} = 1;
870 $::form->{filter}{bank_account} = $bank_account->id;
871 my ( $bt_transactions, $proposals ) = $bt_controller->action_list;
873 is(scalar(@$bt_transactions) , 1 , "$testname: one bank_transaction");
874 is($bt_transactions->[0]->{agreement}, 25 , "$testname: agreement == 25");
875 my $match = join ( ' ',@{$bt_transactions->[0]->{rule_matches}});
877 "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) ",
878 "$testname: rule_matches ok");
881 sub test_two_banktransactions {
883 my $testname = 'two_banktransactions';
885 my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv10000' , amount => 2912.00 );
886 my $bt1 = create_bank_transaction(record => $ar_transaction_1,
887 amount => $ar_transaction_1->amount,
888 purpose => "Rechnung10000 beinahe",
889 bank_chart_id => $bank->id,
890 ) or die "Couldn't create bank_transaction";
892 my $bt2 = create_bank_transaction(record => $ar_transaction_1,
893 amount => $ar_transaction_1->amount + 0.01,
894 purpose => "sicher salesinv20000 vielleicht",
895 bank_chart_id => $bank->id,
896 ) or die "Couldn't create bank_transaction";
898 my ($agreement1, $rule_matches1) = $bt1->get_agreement_with_invoice($ar_transaction_1);
899 is($agreement1, 19, "bt1 19 points for ar_transaction_1 in $testname ok");
900 #print "rule_matches1=".$rule_matches1."\n";
902 "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) ",
903 "$testname: rule_matches ok");
904 my ($agreement2, $rule_matches2) = $bt2->get_agreement_with_invoice($ar_transaction_1);
905 is($agreement2, 11, "bt2 11 points for ar_transaction_1 in $testname ok");
907 "remote_account_number(3) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus0(3) ",
908 "$testname: rule_matches ok");
910 my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv20000' , amount => 2912.01 );
911 my $ar_transaction_3 = test_ar_transaction(invnumber => 'zweitemit10000', amount => 2912.00 );
912 ($agreement1, $rule_matches1) = $bt1->get_agreement_with_invoice($ar_transaction_2);
914 is($agreement1, 11, "bt1 11 points for ar_transaction_2 in $testname ok");
916 ($agreement2, $rule_matches2) = $bt2->get_agreement_with_invoice($ar_transaction_2);
917 is($agreement2, 20, "bt2 20 points for ar_transaction_2 in $testname ok");
919 ($agreement2, $rule_matches2) = $bt2->get_agreement_with_invoice($ar_transaction_1);
920 is($agreement2, 11, "bt2 11 points for ar_transaction_1 in $testname ok");
922 my $bt3 = create_bank_transaction(record => $ar_transaction_3,
923 amount => $ar_transaction_3->amount,
924 purpose => "sicher Rechnung10000 vielleicht",
925 bank_chart_id => $bank->id,
926 ) or die "Couldn't create bank_transaction";
928 my ($agreement3, $rule_matches3) = $bt3->get_agreement_with_invoice($ar_transaction_3);
929 is($agreement3, 19, "bt3 19 points for ar_transaction_3 in $testname ok");
932 $ar_transaction_2->delete;
934 #nun sollten zwei gleichwertige Rechnungen $ar_transaction_1 und $ar_transaction_3 für $bt1 gefunden werden
935 #aber es darf keine Proposals geben mit mehreren Rechnungen
936 my $bt_controller = SL::Controller::BankTransaction->new;
937 $::form->{dont_render_for_test} = 1;
938 $::form->{filter}{bank_account} = $bank_account->id;
939 my ( $bt_transactions, $proposals ) = $bt_controller->action_list;
941 is(scalar(@$bt_transactions) , 2 , "$testname: two bank_transaction");
942 is(scalar(@$proposals) , 0 , "$testname: no proposals");
944 $ar_transaction_3->delete;
946 # Jetzt gibt es zwei Kontobewegungen mit gleichen Punkten für eine Rechnung.
947 # hier darf es auch keine Proposals geben
949 ( $bt_transactions, $proposals ) = $bt_controller->action_list;
951 is(scalar(@$bt_transactions) , 2 , "$testname: two bank_transaction");
952 is(scalar(@$proposals) , 0 , "$testname: no proposals");
954 # Jetzt gibt es zwei Kontobewegungen für eine Rechnung.
955 # eine Bewegung bekommt mehr Punkte
956 # hier darf es auch keine Proposals geben
957 $bt3->update_attributes( purpose => "fuer Rechnung salesinv10000");
959 ( $bt_transactions, $proposals ) = $bt_controller->action_list;
961 is(scalar(@$bt_transactions) , 2 , "$testname: two bank_transaction");
962 is(scalar(@$proposals) , 1 , "$testname: one proposal");