1 use Test::More tests => 100;
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;
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::BankAccount->delete_all(all => 1);
45 SL::DB::Manager::PaymentTerm->delete_all(all => 1);
46 SL::DB::Manager::Currency->delete_all(where => [ name => 'CUR' ]);
49 sub save_btcontroller_to_string {
51 open(my $outputFH, '>', \$output) or die;
52 my $oldFH = select $outputFH;
54 my $bt_controller = SL::Controller::BankTransaction->new;
55 $bt_controller->action_save_invoices;
63 Support::TestSetup::login();
66 reset_state(); # initialise customers/vendors/bank/currency/...
70 test_overpayment_with_partialpayment();
74 test_partial_payment();
76 test_ap_transaction();
77 test_neg_ap_transaction();
78 test_ap_payment_transaction();
79 test_ap_payment_part_transaction();
81 # remove all created data at end of test
86 ###### functions for setting up data
91 $params{$_} ||= {} for qw(unit customer tax vendor);
95 $transdate1 = DateTime->today;
96 $transdate2 = DateTime->today->add(days => 5);
98 $tax = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} }) || croak "No tax";
99 $tax7 = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07) || croak "No tax for 7\%";
100 $tax_9 = SL::DB::Manager::Tax->find_by(taxkey => 9, rate => 0.19, %{ $params{tax} }) || croak "No tax";
102 $currency_id = $::instance_conf->get_currency_id;
104 $bank_account = SL::DB::BankAccount->new(
105 account_number => '123',
110 chart_id => SL::DB::Manager::Chart->find_by(description => 'Bank')->id,
111 name => SL::DB::Manager::Chart->find_by(description => 'Bank')->description,
114 $customer = SL::Dev::CustomerVendor::create_customer(
115 name => 'Test Customer',
116 iban => 'DE12500105170648489890',
118 account_number => '648489890',
119 mandate_date_of_signature => $transdate1,
120 mandator_id => 'foobar',
122 bank_code => 'G1235',
123 depositor => 'Test Customer',
124 customernumber => 'CUST1704',
127 $payment_terms = SL::Dev::Payment::create_payment_terms;
129 $vendor = SL::Dev::CustomerVendor::create_vendor(
130 name => 'Test Vendor',
131 payment_id => $payment_terms->id,
132 iban => 'DE12500105170648489890',
134 account_number => '648489890',
136 bank_code => 'G1235',
137 depositor => 'Test Vendor',
138 vendornumber => 'VEND1704',
141 $ar_chart = SL::DB::Manager::Chart->find_by( accno => '1400' ); # Forderungen
142 $ap_chart = SL::DB::Manager::Chart->find_by( accno => '1600' ); # Verbindlichkeiten
143 $bank = SL::DB::Manager::Chart->find_by( accno => '1200' ); # Bank
144 $ar_amount_chart = SL::DB::Manager::Chart->find_by( accno => '8400' ); # Erlöse
145 $ap_amount_chart = SL::DB::Manager::Chart->find_by( accno => '3400' ); # Wareneingang 19%
149 sub test_ar_transaction {
152 my $amount = $params{amount} || $::form->round_amount(100 * 1.19,2);
153 my $invoice = SL::DB::Invoice->new(
155 invnumber => $params{invnumber} || undef, # let it use its own invnumber
157 netamount => $netamount,
158 transdate => $transdate1,
160 customer_id => $customer->id,
161 taxzone_id => $customer->taxzone_id,
162 currency_id => $currency_id,
164 payment_id => $params{payment_id} || undef,
165 notes => 'test_ar_transaction',
167 $invoice->add_ar_amount_row(
168 amount => $invoice->netamount,
169 chart => $ar_amount_chart,
173 $invoice->create_ar_row(chart => $ar_chart);
176 is($invoice->currency_id , $currency_id , 'currency_id has been saved');
177 is($invoice->netamount , 100 , 'ar amount has been converted');
178 is($invoice->amount , 119 , 'ar amount has been converted');
179 is($invoice->taxincluded , 0 , 'ar transaction doesn\'t have taxincluded');
181 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');
182 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');
187 sub test_ap_transaction {
189 my $testname = 'test_ap_transaction';
192 my $amount = $::form->round_amount($netamount * 1.19,2);
193 my $invoice = SL::DB::PurchaseInvoice->new(
195 invnumber => $params{invnumber} || $testname,
197 netamount => $netamount,
198 transdate => $transdate1,
200 vendor_id => $vendor->id,
201 taxzone_id => $vendor->taxzone_id,
202 currency_id => $currency_id,
204 notes => 'test_ap_transaction',
206 $invoice->add_ap_amount_row(
207 amount => $invoice->netamount,
208 chart => $ap_amount_chart,
209 tax_id => $tax_9->id,
212 $invoice->create_ap_row(chart => $ap_chart);
215 is($invoice->currency_id , $currency_id , "$testname: currency_id has been saved");
216 is($invoice->netamount , 100 , "$testname: ap amount has been converted");
217 is($invoice->amount , 119 , "$testname: ap amount has been converted");
218 is($invoice->taxincluded , 0 , "$testname: ap transaction doesn\'t have taxincluded");
220 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');
221 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');
230 my $testname = 'test1';
232 $ar_transaction = test_ar_transaction(invnumber => 'salesinv1');
234 my $bt = SL::Dev::Payment::create_bank_transaction(record => $ar_transaction) or die "Couldn't create bank_transaction";
236 $::form->{invoice_ids} = {
237 $bt->id => [ $ar_transaction->id ]
240 save_btcontroller_to_string();
242 $ar_transaction->load;
244 is($ar_transaction->paid , '119.00000' , "$testname: salesinv1 was paid");
245 is($ar_transaction->closed , 1 , "$testname: salesinv1 is closed");
246 is($bt->invoice_amount , '119.00000' , "$testname: bt invoice amount was assigned");
250 sub test_skonto_exact {
252 my $testname = 'test_skonto_exact';
254 $ar_transaction = test_ar_transaction(invnumber => 'salesinv skonto',
255 payment_id => $payment_terms->id,
258 my $bt = SL::Dev::Payment::create_bank_transaction(record => $ar_transaction,
259 bank_chart_id => $bank->id,
260 amount => $ar_transaction->amount_less_skonto
261 ) or die "Couldn't create bank_transaction";
263 $::form->{invoice_ids} = {
264 $bt->id => [ $ar_transaction->id ]
266 $::form->{invoice_skontos} = {
267 $bt->id => [ 'with_skonto_pt' ]
270 save_btcontroller_to_string();
272 $ar_transaction->load;
274 is($ar_transaction->paid , '119.00000' , "$testname: salesinv skonto was paid");
275 is($ar_transaction->closed , 1 , "$testname: salesinv skonto is closed");
276 is($bt->invoice_amount , '113.05000' , "$testname: bt invoice amount was assigned");
280 sub test_two_invoices {
282 my $testname = 'test_two_invoices';
284 my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv_1');
285 my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv_2');
287 my $bt = SL::Dev::Payment::create_bank_transaction(record => $ar_transaction_1,
288 amount => ($ar_transaction_1->amount + $ar_transaction_2->amount),
289 purpose => "Rechnungen " . $ar_transaction_1->invnumber . " und " . $ar_transaction_2->invnumber,
290 bank_chart_id => $bank->id,
291 ) or die "Couldn't create bank_transaction";
293 my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($ar_transaction_1);
294 is($agreement, 16, "points for ar_transaction_1 in test_two_invoices ok");
296 $::form->{invoice_ids} = {
297 $bt->id => [ $ar_transaction_1->id, $ar_transaction_2->id ]
300 save_btcontroller_to_string();
302 $ar_transaction_1->load;
303 $ar_transaction_2->load;
306 is($ar_transaction_1->paid , '119.00000' , "$testname: salesinv_1 was paid");
307 is($ar_transaction_1->closed , 1 , "$testname: salesinv_1 is closed");
308 is($ar_transaction_2->paid , '119.00000' , "$testname: salesinv_2 was paid");
309 is($ar_transaction_2->closed , 1 , "$testname: salesinv_2 is closed");
310 is($bt->invoice_amount , '238.00000' , "$testname: bt invoice amount was assigned");
314 sub test_overpayment {
316 my $testname = 'test_overpayment';
318 $ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid');
321 my $bt = SL::Dev::Payment::create_bank_transaction(record => $ar_transaction,
322 bank_chart_id => $bank->id,
324 ) or die "Couldn't create bank_transaction";
326 $::form->{invoice_ids} = {
327 $bt->id => [ $ar_transaction->id ]
330 save_btcontroller_to_string();
332 $ar_transaction->load;
335 is($ar_transaction->paid , '135.00000' , "$testname: 'salesinv overpaid' was overpaid");
336 is($bt->invoice_amount , '135.00000' , "$testname: bt invoice amount was assigned overpaid amount");
337 { local $TODO = 'this currently fails because closed ignores over-payments, see commit d90966c7';
338 is($ar_transaction->closed , 0 , "$testname: 'salesinv overpaid' is open (via 'closed' method')");
340 is($ar_transaction->open_amount == 0 ? 1 : 0 , 0 , "$testname: 'salesinv overpaid is open (via amount-paid)");
344 sub test_overpayment_with_partialpayment {
346 # two payments on different days, 10 and 119. If there is only one invoice we want it be overpaid.
347 my $testname = 'test_overpayment_with_partialpayment';
349 $ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid partial');
351 my $bt_1 = SL::Dev::Payment::create_bank_transaction(record => $ar_transaction,
352 bank_chart_id => $bank->id,
354 ) or die "Couldn't create bank_transaction";
355 my $bt_2 = SL::Dev::Payment::create_bank_transaction(record => $ar_transaction,
357 transdate => DateTime->today->add(days => 5),
358 bank_chart_id => $bank->id,
359 ) or die "Couldn't create bank_transaction";
361 $::form->{invoice_ids} = {
362 $bt_1->id => [ $ar_transaction->id ]
364 save_btcontroller_to_string();
366 $::form->{invoice_ids} = {
367 $bt_2->id => [ $ar_transaction->id ]
369 save_btcontroller_to_string();
371 $ar_transaction->load;
375 is($ar_transaction->paid , '129.00000' , "$testname: 'salesinv overpaid partial' was overpaid");
376 is($bt_1->invoice_amount , '10.00000' , "$testname: bt_1 invoice amount was assigned overpaid amount");
377 is($bt_2->invoice_amount , '119.00000' , "$testname: bt_2 invoice amount was assigned overpaid amount");
381 sub test_partial_payment {
383 my $testname = 'test_partial_payment';
385 $ar_transaction = test_ar_transaction(invnumber => 'salesinv partial payment');
388 my $bt = SL::Dev::Payment::create_bank_transaction(record => $ar_transaction,
389 bank_chart_id => $bank->id,
391 ) or die "Couldn't create bank_transaction";
393 $::form->{invoice_ids} = {
394 $bt->id => [ $ar_transaction->id ]
397 save_btcontroller_to_string();
399 $ar_transaction->load;
402 is($ar_transaction->paid , '100.00000' , "$testname: 'salesinv partial payment' was partially paid");
403 is($bt->invoice_amount , '100.00000' , "$testname: bt invoice amount was assigned partially paid amount");
407 sub test_credit_note {
409 my $testname = 'test_credit_note';
411 my $part1 = SL::Dev::Part::create_part( partnumber => 'T4254')->save;
412 my $part2 = SL::Dev::Part::create_service(partnumber => 'Serv1')->save;
413 my $credit_note = SL::Dev::Record::create_credit_note(
415 customer => $customer,
417 invoiceitems => [ SL::Dev::Record::create_invoice_item(part => $part1, qty => 3, sellprice => 70),
418 SL::Dev::Record::create_invoice_item(part => $part2, qty => 10, sellprice => 50),
421 my $bt = SL::Dev::Payment::create_bank_transaction(record => $credit_note,
422 amount => $credit_note->amount,
423 bank_chart_id => $bank->id,
424 transdate => DateTime->today->add(days => 10),
426 my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($credit_note);
427 is($agreement, 13, "points for credit note ok");
428 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");
430 $::form->{invoice_ids} = {
431 $bt->id => [ $credit_note->id ]
434 save_btcontroller_to_string();
438 is($credit_note->amount , '-844.90000', "$testname: amount ok");
439 is($credit_note->netamount, '-710.00000', "$testname: netamount ok");
440 is($credit_note->paid , '-844.90000', "$testname: paid ok");
443 sub test_neg_ap_transaction {
445 my $testname = 'test_neg_ap_transaction';
447 my $amount = $::form->round_amount($netamount * 1.19,2);
448 my $invoice = SL::DB::PurchaseInvoice->new(
450 invnumber => $params{invnumber} || 'test_neg_ap_transaction',
452 netamount => $netamount,
453 transdate => $transdate1,
455 vendor_id => $vendor->id,
456 taxzone_id => $vendor->taxzone_id,
457 currency_id => $currency_id,
459 notes => 'test_neg_ap_transaction',
461 $invoice->add_ap_amount_row(
462 amount => $invoice->netamount,
463 chart => $ap_amount_chart,
464 tax_id => $tax_9->id,
467 $invoice->create_ap_row(chart => $ap_chart);
470 is($invoice->netamount, -20 , "$testname: netamount ok");
471 is($invoice->amount , -23.8, "$testname: amount ok");
473 my $bt = SL::Dev::Payment::create_bank_transaction(record => $invoice,
474 amount => $invoice->amount,
475 bank_chart_id => $bank->id,
476 transdate => DateTime->today->add(days => 10),
478 my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($invoice);
479 is($agreement, 15, "points for negative ap transaction ok");
481 $::form->{invoice_ids} = {
482 $bt->id => [ $invoice->id ]
485 save_btcontroller_to_string();
490 is($invoice->amount , '-23.80000', "$testname: amount ok");
491 is($invoice->netamount, '-20.00000', "$testname: netamount ok");
492 is($invoice->paid , '-23.80000', "$testname: paid ok");
493 is($bt->invoice_amount, '23.80000', "$testname: bt invoice amount for ap was assigned");
498 sub test_ap_payment_transaction {
500 my $testname = 'test_ap_payment_transaction';
502 my $amount = $::form->round_amount($netamount * 1.19,2);
503 my $invoice = SL::DB::PurchaseInvoice->new(
505 invnumber => $params{invnumber} || $testname,
507 netamount => $netamount,
508 transdate => $transdate1,
510 vendor_id => $vendor->id,
511 taxzone_id => $vendor->taxzone_id,
512 currency_id => $currency_id,
516 $invoice->add_ap_amount_row(
517 amount => $invoice->netamount,
518 chart => $ap_amount_chart,
519 tax_id => $tax_9->id,
522 $invoice->create_ap_row(chart => $ap_chart);
525 is($invoice->netamount, 115 , "$testname: netamount ok");
526 is($invoice->amount , 136.85, "$testname: amount ok");
528 my $bt = SL::Dev::Payment::create_bank_transaction(record => $invoice,
529 amount => $invoice->amount,
530 bank_chart_id => $bank->id,
531 transdate => DateTime->today->add(days => 10),
533 $::form->{invoice_ids} = {
534 $bt->id => [ $invoice->id ]
537 save_btcontroller_to_string();
542 is($invoice->amount , '136.85000', "$testname: amount ok");
543 is($invoice->netamount, '115.00000', "$testname: netamount ok");
544 is($bt->amount, '-136.85000', "$testname: bt amount ok");
545 is($invoice->paid , '136.85000', "$testname: paid ok");
546 is($bt->invoice_amount, '-136.85000', "$testname: bt invoice amount for ap was assigned");
551 sub test_ap_payment_part_transaction {
553 my $testname = 'test_ap_payment_p_transaction';
555 my $amount = $::form->round_amount($netamount * 1.19,2);
556 my $invoice = SL::DB::PurchaseInvoice->new(
558 invnumber => $params{invnumber} || $testname,
560 netamount => $netamount,
561 transdate => $transdate1,
563 vendor_id => $vendor->id,
564 taxzone_id => $vendor->taxzone_id,
565 currency_id => $currency_id,
569 $invoice->add_ap_amount_row(
570 amount => $invoice->netamount,
571 chart => $ap_amount_chart,
572 tax_id => $tax_9->id,
575 $invoice->create_ap_row(chart => $ap_chart);
578 is($invoice->netamount, 115 , "$testname: netamount ok");
579 is($invoice->amount , 136.85, "$testname: amount ok");
581 my $bt = SL::Dev::Payment::create_bank_transaction(record => $invoice,
582 amount => $invoice->amount-100,
583 bank_chart_id => $bank->id,
584 transdate => DateTime->today->add(days => 10),
586 $::form->{invoice_ids} = {
587 $bt->id => [ $invoice->id ]
590 save_btcontroller_to_string();
595 is($invoice->amount , '136.85000', "$testname: amount ok");
596 is($invoice->netamount, '115.00000', "$testname: netamount ok");
597 is($bt->amount, '-36.85000', "$testname: bt amount ok");
598 is($invoice->paid , '36.85000', "$testname: paid ok");
599 is($bt->invoice_amount, '-36.85000', "$testname: bt invoice amount for ap was assigned");
601 my $bt2 = SL::Dev::Payment::create_bank_transaction(record => $invoice,
603 bank_chart_id => $bank->id,
604 transdate => DateTime->today->add(days => 10),
606 $::form->{invoice_ids} = {
607 $bt2->id => [ $invoice->id ]
610 save_btcontroller_to_string();
614 is($invoice->amount , '136.85000', "$testname: amount ok");
615 is($invoice->netamount, '115.00000', "$testname: netamount ok");
616 is($bt2->amount, '-100.00000',"$testname: bt amount ok");
617 is($invoice->paid , '136.85000', "$testname: paid ok");
618 is($bt2->invoice_amount,'-100.00000', "$testname: bt invoice amount for ap was assigned");