1 use Test::More tests => 88;
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/...
69 test_overpayment_with_partialpayment();
73 test_partial_payment();
75 test_neg_ap_transaction();
76 test_ap_transaction();
77 test_ap_payment_transaction();
78 # remove all created data at end of test
83 ###### functions for setting up data
88 $params{$_} ||= {} for qw(unit customer tax vendor);
92 $transdate1 = DateTime->today;
93 $transdate2 = DateTime->today->add(days => 5);
95 $tax = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} }) || croak "No tax";
96 $tax7 = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07) || croak "No tax for 7\%";
97 $tax_9 = SL::DB::Manager::Tax->find_by(taxkey => 9, rate => 0.19, %{ $params{tax} }) || croak "No tax";
99 $currency_id = $::instance_conf->get_currency_id;
101 $bank_account = SL::DB::BankAccount->new(
102 account_number => '123',
107 chart_id => SL::DB::Manager::Chart->find_by(description => 'Bank')->id,
108 name => SL::DB::Manager::Chart->find_by(description => 'Bank')->description,
111 $customer = SL::Dev::CustomerVendor::create_customer(
112 name => 'Test Customer',
113 iban => 'DE12500105170648489890',
115 account_number => '648489890',
116 mandate_date_of_signature => $transdate1,
117 mandator_id => 'foobar',
119 bank_code => 'G1235',
120 depositor => 'Test Customer',
123 $payment_terms = SL::Dev::Payment::create_payment_terms;
125 $vendor = SL::Dev::CustomerVendor::create_vendor(
126 name => 'Test Vendor',
127 payment_id => $payment_terms->id,
128 iban => 'DE12500105170648489890',
130 account_number => '648489890',
132 bank_code => 'G1235',
133 depositor => 'Test Vendor',
136 $ar_chart = SL::DB::Manager::Chart->find_by( accno => '1400' ); # Forderungen
137 $ap_chart = SL::DB::Manager::Chart->find_by( accno => '1600' ); # Verbindlichkeiten
138 $bank = SL::DB::Manager::Chart->find_by( accno => '1200' ); # Bank
139 $ar_amount_chart = SL::DB::Manager::Chart->find_by( accno => '8400' ); # Erlöse
140 $ap_amount_chart = SL::DB::Manager::Chart->find_by( accno => '3400' ); # Wareneingang 19%
144 sub test_ar_transaction {
147 my $amount = $params{amount} || $::form->round_amount(100 * 1.19,2);
148 my $invoice = SL::DB::Invoice->new(
150 invnumber => $params{invnumber} || undef, # let it use its own invnumber
152 netamount => $netamount,
153 transdate => $transdate1,
155 customer_id => $customer->id,
156 taxzone_id => $customer->taxzone_id,
157 currency_id => $currency_id,
159 payment_id => $params{payment_id} || undef,
160 notes => 'test_ar_transaction',
162 $invoice->add_ar_amount_row(
163 amount => $invoice->netamount,
164 chart => $ar_amount_chart,
168 $invoice->create_ar_row(chart => $ar_chart);
171 is($invoice->currency_id , $currency_id , 'currency_id has been saved');
172 is($invoice->netamount , 100 , 'ar amount has been converted');
173 is($invoice->amount , 119 , 'ar amount has been converted');
174 is($invoice->taxincluded , 0 , 'ar transaction doesn\'t have taxincluded');
176 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');
177 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');
182 sub test_ap_transaction {
185 my $amount = $::form->round_amount($netamount * 1.19,2);
186 my $invoice = SL::DB::PurchaseInvoice->new(
188 invnumber => $params{invnumber} || 'test_ap_transaction',
190 netamount => $netamount,
191 transdate => $transdate1,
193 vendor_id => $vendor->id,
194 taxzone_id => $vendor->taxzone_id,
195 currency_id => $currency_id,
197 notes => 'test_ap_transaction',
199 $invoice->add_ap_amount_row(
200 amount => $invoice->netamount,
201 chart => $ap_amount_chart,
202 tax_id => $tax_9->id,
205 $invoice->create_ap_row(chart => $ap_chart);
208 is($invoice->currency_id , $currency_id , 'currency_id has been saved');
209 is($invoice->netamount , 100 , 'ap amount has been converted');
210 is($invoice->amount , 119 , 'ap amount has been converted');
211 is($invoice->taxincluded , 0 , 'ap transaction doesn\'t have taxincluded');
213 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');
214 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');
223 my $testname = 'test1';
225 $ar_transaction = test_ar_transaction(invnumber => 'salesinv1');
227 my $bt = SL::Dev::Payment::create_bank_transaction(record => $ar_transaction) or die "Couldn't create bank_transaction";
229 $::form->{invoice_ids} = {
230 $bt->id => [ $ar_transaction->id ]
233 save_btcontroller_to_string();
235 $ar_transaction->load;
237 is($ar_transaction->paid , '119.00000' , "$testname: salesinv1 was paid");
238 is($ar_transaction->closed , 1 , "$testname: salesinv1 is closed");
239 is($bt->invoice_amount , '119.00000' , "$testname: bt invoice amount was assigned");
243 sub test_skonto_exact {
245 my $testname = 'test_skonto_exact';
247 $ar_transaction = test_ar_transaction(invnumber => 'salesinv skonto',
248 payment_id => $payment_terms->id,
251 my $bt = SL::Dev::Payment::create_bank_transaction(record => $ar_transaction,
252 bank_chart_id => $bank->id,
253 amount => $ar_transaction->amount_less_skonto
254 ) or die "Couldn't create bank_transaction";
256 $::form->{invoice_ids} = {
257 $bt->id => [ $ar_transaction->id ]
259 $::form->{invoice_skontos} = {
260 $bt->id => [ 'with_skonto_pt' ]
263 save_btcontroller_to_string();
265 $ar_transaction->load;
267 is($ar_transaction->paid , '119.00000' , "$testname: salesinv skonto was paid");
268 is($ar_transaction->closed , 1 , "$testname: salesinv skonto is closed");
269 is($bt->invoice_amount , '113.05000' , "$testname: bt invoice amount was assigned");
273 sub test_two_invoices {
275 my $testname = 'test_two_invoices';
277 my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv_1');
278 my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv_2');
280 my $bt = SL::Dev::Payment::create_bank_transaction(record => $ar_transaction_1,
281 amount => ($ar_transaction_1->amount + $ar_transaction_2->amount),
282 purpose => "Rechnungen " . $ar_transaction_1->invnumber . " und " . $ar_transaction_2->invnumber,
283 bank_chart_id => $bank->id,
284 ) or die "Couldn't create bank_transaction";
286 my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($ar_transaction_1);
287 is($agreement, 16, "points for ar_transaction_1 in test_two_invoices ok");
289 $::form->{invoice_ids} = {
290 $bt->id => [ $ar_transaction_1->id, $ar_transaction_2->id ]
293 save_btcontroller_to_string();
295 $ar_transaction_1->load;
296 $ar_transaction_2->load;
299 is($ar_transaction_1->paid , '119.00000' , "$testname: salesinv_1 was paid");
300 is($ar_transaction_1->closed , 1 , "$testname: salesinv_1 is closed");
301 is($ar_transaction_2->paid , '119.00000' , "$testname: salesinv_2 was paid");
302 is($ar_transaction_2->closed , 1 , "$testname: salesinv_2 is closed");
303 is($bt->invoice_amount , '238.00000' , "$testname: bt invoice amount was assigned");
307 sub test_overpayment {
309 my $testname = 'test_overpayment';
311 $ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid');
314 my $bt = SL::Dev::Payment::create_bank_transaction(record => $ar_transaction,
315 bank_chart_id => $bank->id,
317 ) or die "Couldn't create bank_transaction";
319 $::form->{invoice_ids} = {
320 $bt->id => [ $ar_transaction->id ]
323 save_btcontroller_to_string();
325 $ar_transaction->load;
328 is($ar_transaction->paid , '135.00000' , "$testname: 'salesinv overpaid' was overpaid");
329 is($bt->invoice_amount , '135.00000' , "$testname: bt invoice amount was assigned overpaid amount");
330 { local $TODO = 'this currently fails because closed ignores over-payments, see commit d90966c7';
331 is($ar_transaction->closed , 0 , "$testname: 'salesinv overpaid' is open (via 'closed' method')");
333 is($ar_transaction->open_amount == 0 ? 1 : 0 , 0 , "$testname: 'salesinv overpaid is open (via amount-paid)");
337 sub test_overpayment_with_partialpayment {
339 # two payments on different days, 10 and 119. If there is only one invoice we want it be overpaid.
340 my $testname = 'test_overpayment_with_partialpayment';
342 $ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid partial');
344 my $bt_1 = SL::Dev::Payment::create_bank_transaction(record => $ar_transaction,
345 bank_chart_id => $bank->id,
347 ) or die "Couldn't create bank_transaction";
348 my $bt_2 = SL::Dev::Payment::create_bank_transaction(record => $ar_transaction,
350 transdate => DateTime->today->add(days => 5),
351 bank_chart_id => $bank->id,
352 ) or die "Couldn't create bank_transaction";
354 $::form->{invoice_ids} = {
355 $bt_1->id => [ $ar_transaction->id ]
357 save_btcontroller_to_string();
359 $::form->{invoice_ids} = {
360 $bt_2->id => [ $ar_transaction->id ]
362 save_btcontroller_to_string();
364 $ar_transaction->load;
368 is($ar_transaction->paid , '129.00000' , "$testname: 'salesinv overpaid partial' was overpaid");
369 is($bt_1->invoice_amount , '10.00000' , "$testname: bt_1 invoice amount was assigned overpaid amount");
370 is($bt_2->invoice_amount , '119.00000' , "$testname: bt_2 invoice amount was assigned overpaid amount");
374 sub test_partial_payment {
376 my $testname = 'test_partial_payment';
378 $ar_transaction = test_ar_transaction(invnumber => 'salesinv partial payment');
381 my $bt = SL::Dev::Payment::create_bank_transaction(record => $ar_transaction,
382 bank_chart_id => $bank->id,
384 ) or die "Couldn't create bank_transaction";
386 $::form->{invoice_ids} = {
387 $bt->id => [ $ar_transaction->id ]
390 save_btcontroller_to_string();
392 $ar_transaction->load;
395 is($ar_transaction->paid , '100.00000' , "$testname: 'salesinv partial payment' was partially paid");
396 is($bt->invoice_amount , '100.00000' , "$testname: bt invoice amount was assigned partially paid amount");
400 sub test_credit_note {
402 my $testname = 'test_credit_note';
404 my $part1 = SL::Dev::Part::create_part( partnumber => 'T4254')->save;
405 my $part2 = SL::Dev::Part::create_service(partnumber => 'Serv1')->save;
406 my $credit_note = SL::Dev::Record::create_credit_note(
408 customer => $customer,
410 invoiceitems => [ SL::Dev::Record::create_invoice_item(part => $part1, qty => 3, sellprice => 70),
411 SL::Dev::Record::create_invoice_item(part => $part2, qty => 10, sellprice => 50),
414 my $bt = SL::Dev::Payment::create_bank_transaction(record => $credit_note,
415 amount => $credit_note->amount,
416 bank_chart_id => $bank->id,
417 transdate => DateTime->today->add(days => 10),
419 my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($credit_note);
420 is($agreement, 13, "points for credit note ok");
421 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");
423 $::form->{invoice_ids} = {
424 $bt->id => [ $credit_note->id ]
427 save_btcontroller_to_string();
431 is($credit_note->amount , '-844.90000', "$testname: amount ok");
432 is($credit_note->netamount, '-710.00000', "$testname: netamount ok");
433 is($credit_note->paid , '-844.90000', "$testname: paid ok");
436 sub test_neg_ap_transaction {
438 my $testname = 'test_neg_ap_transaction';
440 my $amount = $::form->round_amount($netamount * 1.19,2);
441 my $invoice = SL::DB::PurchaseInvoice->new(
443 invnumber => $params{invnumber} || 'test_neg_ap_transaction',
445 netamount => $netamount,
446 transdate => $transdate1,
448 vendor_id => $vendor->id,
449 taxzone_id => $vendor->taxzone_id,
450 currency_id => $currency_id,
452 notes => 'test_neg_ap_transaction',
454 $invoice->add_ap_amount_row(
455 amount => $invoice->netamount,
456 chart => $ap_amount_chart,
457 tax_id => $tax_9->id,
460 $invoice->create_ap_row(chart => $ap_chart);
463 is($invoice->netamount, -20 , "$testname: netamount ok");
464 is($invoice->amount , -23.8, "$testname: amount ok");
466 my $bt = SL::Dev::Payment::create_bank_transaction(record => $invoice,
467 amount => $invoice->amount,
468 bank_chart_id => $bank->id,
469 transdate => DateTime->today->add(days => 10),
471 my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($invoice);
472 is($agreement, 15, "points for negative ap transaction ok");
474 $::form->{invoice_ids} = {
475 $bt->id => [ $invoice->id ]
478 save_btcontroller_to_string();
483 is($invoice->amount , '-23.80000', "$testname: amount ok");
484 is($invoice->netamount, '-20.00000', "$testname: netamount ok");
485 is($invoice->paid , '-23.80000', "$testname: paid ok");
486 is($bt->invoice_amount, '23.80000', "$testname: bt invoice amount for ap was assigned");
491 sub test_ap_payment_transaction {
493 my $testname = 'test_ap_two_transaction';
495 my $amount = $::form->round_amount($netamount * 1.19,2);
496 my $invoice = SL::DB::PurchaseInvoice->new(
498 invnumber => $params{invnumber} || 'test_ap_two_transaction',
500 netamount => $netamount,
501 transdate => $transdate1,
503 vendor_id => $vendor->id,
504 taxzone_id => $vendor->taxzone_id,
505 currency_id => $currency_id,
507 notes => 'test_ap_transaction',
509 $invoice->add_ap_amount_row(
510 amount => $invoice->netamount,
511 chart => $ap_amount_chart,
512 tax_id => $tax_9->id,
515 $invoice->create_ap_row(chart => $ap_chart);
518 is($invoice->netamount, 115 , "$testname: netamount ok");
519 is($invoice->amount , 136.85, "$testname: amount ok");
521 my $bt = SL::Dev::Payment::create_bank_transaction(record => $invoice,
522 amount => $invoice->amount,
523 bank_chart_id => $bank->id,
524 transdate => DateTime->today->add(days => 10),
526 $::form->{invoice_ids} = {
527 $bt->id => [ $invoice->id ]
530 save_btcontroller_to_string();
535 is($invoice->amount , '136.85000', "$testname: amount ok");
536 is($invoice->netamount, '115.00000', "$testname: netamount ok");
537 is($bt->amount, '-136.85000', "$testname: bt amount ok");
538 is($invoice->paid , '136.85000', "$testname: paid ok");
539 is($bt->invoice_amount, '-136.85000', "$testname: bt invoice amount for ap was assigned");