test action
[kivitendo-erp.git] / t / bank / bank_transactions.t
1 use Test::More tests => 291;
2
3 use strict;
4
5 use lib 't';
6 use utf8;
7
8 use Carp;
9 use Support::TestSetup;
10 use Test::Exception;
11 use List::Util qw(sum);
12
13 use SL::DB::AccTransaction;
14 use SL::DB::BankTransactionAccTrans;
15 use SL::DB::Buchungsgruppe;
16 use SL::DB::Currency;
17 use SL::DB::Customer;
18 use SL::DB::Default;
19 use SL::DB::Vendor;
20 use SL::DB::Invoice;
21 use SL::DB::Unit;
22 use SL::DB::Part;
23 use SL::DB::TaxZone;
24 use SL::DB::BankAccount;
25 use SL::DB::PaymentTerm;
26 use SL::DB::PurchaseInvoice;
27 use SL::DB::BankTransaction;
28 use SL::Controller::BankTransaction;
29 use SL::Controller::Reconciliation;
30 use SL::Dev::ALL qw(:ALL);
31 use Data::Dumper;
32
33 my ($customer, $vendor, $currency_id, $unit, $tax, $tax0, $tax7, $tax_9, $payment_terms, $bank_account);
34 my ($currency);
35 my ($ar_chart,$bank,$ar_amount_chart, $ap_chart, $ap_amount_chart);
36 my ($ar_transaction, $ap_transaction);
37 my ($dt, $dt_5, $dt_10, $year);
38
39 sub clear_up {
40
41   SL::DB::Manager::BankTransactionAccTrans->delete_all(all => 1);
42   SL::DB::Manager::BankTransaction->delete_all(all => 1);
43   SL::DB::Manager::InvoiceItem->delete_all(all => 1);
44   SL::DB::Manager::InvoiceItem->delete_all(all => 1);
45   SL::DB::Manager::Invoice->delete_all(all => 1);
46   SL::DB::Manager::PurchaseInvoice->delete_all(all => 1);
47   SL::DB::Manager::Part->delete_all(all => 1);
48   SL::DB::Manager::Customer->delete_all(all => 1);
49   SL::DB::Manager::Vendor->delete_all(all => 1);
50   SL::DB::Manager::SepaExportItem->delete_all(all => 1);
51   SL::DB::Manager::SepaExport->delete_all(all => 1);
52   SL::DB::Manager::BankAccount->delete_all(all => 1);
53   SL::DB::Manager::PaymentTerm->delete_all(all => 1);
54   SL::DB::Manager::Currency->delete_all(where => [ name => 'CUR' ]);
55   # SL::DB::Manager::Default->delete_all(all => 1);
56 };
57
58 my $bt_controller;
59
60 sub save_btcontroller_to_string {
61   my $output;
62   open(my $outputFH, '>', \$output) or die;
63   my $oldFH = select $outputFH;
64
65   $bt_controller = SL::Controller::BankTransaction->new;
66   $bt_controller->action_save_invoices;
67
68   select $oldFH;
69   close $outputFH;
70   return $output;
71 }
72
73 # starting test:
74 Support::TestSetup::login();
75
76 clear_up();
77 reset_state(); # initialise customers/vendors/bank/currency/...
78
79 test1();
80
81 test_overpayment_with_partialpayment();
82 test_overpayment();
83 reset_state();
84 test_skonto_exact();
85 test_two_invoices();
86 test_partial_payment();
87 test_credit_note();
88 test_ap_transaction();
89 test_neg_ap_transaction(invoice => 0);
90 test_neg_ap_transaction(invoice => 1);
91 test_ap_payment_transaction();
92 test_ap_payment_part_transaction();
93 test_neg_sales_invoice();
94 test_two_neg_ap_transaction();
95 test_one_inv_and_two_invoices_with_skonto_exact();
96 test_bt_error();
97 test_full_workflow_ar_multiple_inv_skonto_reconciliate_and_undo();
98 reset_state();
99 test_sepa_export();
100
101 reset_state();
102 test_bt_rule1();
103 reset_state();
104 test_two_banktransactions();
105 # remove all created data at end of test
106 test_closedto();
107 clear_up();
108
109 done_testing();
110
111 ###### functions for setting up data
112
113 sub reset_state {
114   my %params = @_;
115
116   $params{$_} ||= {} for qw(unit customer tax vendor);
117
118   clear_up();
119
120   $year  = DateTime->today_local->year;
121   $year  = 2019 if $year == 2020; # use year 2019 in 2020, because of tax rate change in Germany
122   $dt    = DateTime->new(year => $year, month => 1, day => 12);
123   $dt_5  = $dt->clone->add(days => 5);
124   $dt_10 = $dt->clone->add(days => 10);
125
126   $tax             = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} }) || croak "No tax";
127   $tax7            = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07)                    || croak "No tax for 7\%";
128   $tax_9           = SL::DB::Manager::Tax->find_by(taxkey => 9, rate => 0.19, %{ $params{tax} }) || croak "No tax for 19\%";
129   $tax0            = SL::DB::Manager::Tax->find_by(taxkey => 0, rate => 0.0)                     || croak "No tax for 0\%";
130
131   $currency_id     = $::instance_conf->get_currency_id;
132
133   $bank_account     =  SL::DB::BankAccount->new(
134     account_number  => '123',
135     bank_code       => '123',
136     iban            => '123',
137     bic             => '123',
138     bank            => '123',
139     chart_id        => SL::DB::Manager::Chart->find_by(description => 'Bank')->id,
140     name            => SL::DB::Manager::Chart->find_by(description => 'Bank')->description,
141   )->save;
142
143   $customer = new_customer(
144     name                      => 'Test Customer OLÉ S.L. Årdbärg AB',
145     iban                      => 'DE12500105170648489890',
146     bic                       => 'TESTBIC',
147     account_number            => '648489890',
148     mandate_date_of_signature => $dt,
149     mandator_id               => 'foobar',
150     bank                      => 'Geizkasse',
151     bank_code                 => 'G1235',
152     depositor                 => 'Test Customer',
153     customernumber            => 'CUST1704',
154   )->save;
155
156   $payment_terms = create_payment_terms();
157
158   $vendor = new_vendor(
159     name           => 'Test Vendor',
160     payment_id     => $payment_terms->id,
161     iban           => 'DE12500105170648489890',
162     bic            => 'TESTBIC',
163     account_number => '648489890',
164     bank           => 'Geizkasse',
165     bank_code      => 'G1235',
166     depositor      => 'Test Vendor',
167     vendornumber   => 'VEND1704',
168   )->save;
169
170   $ar_chart        = SL::DB::Manager::Chart->find_by( accno => '1400' ); # Forderungen
171   $ap_chart        = SL::DB::Manager::Chart->find_by( accno => '1600' ); # Verbindlichkeiten
172   $bank            = SL::DB::Manager::Chart->find_by( accno => '1200' ); # Bank
173   $ar_amount_chart = SL::DB::Manager::Chart->find_by( accno => '8400' ); # Erlöse
174   $ap_amount_chart = SL::DB::Manager::Chart->find_by( accno => '3400' ); # Wareneingang 19%
175
176 }
177
178 sub test_ar_transaction {
179   my (%params) = @_;
180   my $netamount = $params{amount} || 100;
181   my $amount    = $::form->round_amount($netamount * 1.19,2);
182   my $invoice   = SL::DB::Invoice->new(
183       invoice      => 0,
184       invnumber    => $params{invnumber} || undef, # let it use its own invnumber
185       amount       => $amount,
186       netamount    => $netamount,
187       transdate    => $dt,
188       taxincluded  => $params{taxincluded } || 0,
189       customer_id  => $customer->id,
190       taxzone_id   => $customer->taxzone_id,
191       currency_id  => $currency_id,
192       transactions => [],
193       payment_id   => $params{payment_id} || undef,
194       notes        => 'test_ar_transaction',
195   );
196   $invoice->add_ar_amount_row(
197     amount => $invoice->netamount,
198     chart  => $ar_amount_chart,
199     tax_id => $params{tax_id} || $tax->id,
200   );
201
202   $invoice->create_ar_row(chart => $ar_chart);
203   $invoice->save;
204
205   is($invoice->currency_id , $currency_id , 'currency_id has been saved');
206   is($invoice->netamount   , $netamount   , 'ar amount has been converted');
207   is($invoice->amount      , $amount      , 'ar amount has been converted');
208   is($invoice->taxincluded , 0            , 'ar transaction doesn\'t have taxincluded');
209
210   if ( $netamount == 100 ) {
211     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');
212     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');
213   }
214   return $invoice;
215 };
216
217 sub test_ap_transaction {
218   my (%params) = @_;
219   my $testname = 'test_ap_transaction';
220
221   my $netamount = 100;
222   my $amount    = $::form->round_amount($netamount * 1.19,2);
223   my $invoice   = SL::DB::PurchaseInvoice->new(
224     invoice      => 0,
225     invnumber    => $params{invnumber} || $testname,
226     amount       => $amount,
227     netamount    => $netamount,
228     transdate    => $dt,
229     taxincluded  => 0,
230     vendor_id    => $vendor->id,
231     taxzone_id   => $vendor->taxzone_id,
232     currency_id  => $currency_id,
233     transactions => [],
234     notes        => 'test_ap_transaction',
235   );
236   $invoice->add_ap_amount_row(
237     amount     => $invoice->netamount,
238     chart      => $ap_amount_chart,
239     tax_id     => $params{tax_id} || $tax_9->id,
240   );
241
242   $invoice->create_ap_row(chart => $ap_chart);
243   $invoice->save;
244
245   is($invoice->currency_id , $currency_id , "$testname: currency_id has been saved");
246   is($invoice->netamount   , 100          , "$testname: ap amount has been converted");
247   is($invoice->amount      , 119          , "$testname: ap amount has been converted");
248   is($invoice->taxincluded , 0            , "$testname: ap transaction doesn\'t have taxincluded");
249
250   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');
251   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');
252
253   return $invoice;
254 };
255
256 ###### test cases
257
258 sub test1 {
259
260   my $testname = 'test1';
261
262   $ar_transaction = test_ar_transaction(invnumber => 'salesinv1');
263
264   my $bt = create_bank_transaction(record      => $ar_transaction,
265                                    transdate   => $dt,
266                                    valutadate  => $dt) or die "Couldn't create bank_transaction";
267
268   $::form->{invoice_ids} = {
269     $bt->id => [ $ar_transaction->id ]
270   };
271
272   save_btcontroller_to_string();
273
274   $ar_transaction->load;
275   $bt->load;
276   is($ar_transaction->paid   , '119.00000' , "$testname: salesinv1 was paid");
277   is($ar_transaction->closed , 1           , "$testname: salesinv1 is closed");
278   is($bt->invoice_amount     , '119.00000' , "$testname: bt invoice amount was assigned");
279
280 };
281
282 sub test_skonto_exact {
283
284   my $testname = 'test_skonto_exact';
285
286   $ar_transaction = test_ar_transaction(invnumber => 'salesinv skonto',
287                                         payment_id => $payment_terms->id,
288                                        );
289
290   my $bt = create_bank_transaction(record        => $ar_transaction,
291                                    bank_chart_id => $bank->id,
292                                    transdate     => $dt,
293                                    valutadate    => $dt,
294                                    amount        => $ar_transaction->amount_less_skonto
295                                   ) or die "Couldn't create bank_transaction";
296
297   $::form->{invoice_ids} = {
298     $bt->id => [ $ar_transaction->id ]
299   };
300   $::form->{invoice_skontos} = {
301     $bt->id => [ 'with_skonto_pt' ]
302   };
303
304   save_btcontroller_to_string();
305
306   $ar_transaction->load;
307   $bt->load;
308   is($ar_transaction->paid   , '119.00000' , "$testname: salesinv skonto was paid");
309   is($ar_transaction->closed , 1           , "$testname: salesinv skonto is closed");
310   is($bt->invoice_amount     , '113.05000' , "$testname: bt invoice amount was assigned");
311
312 };
313
314 sub test_bt_error {
315
316   my $testname = 'test_rollback_error';
317   # without type with_free_skonto the helper function (Payment.pm) looks ugly but not
318   # breakable
319
320   $ar_transaction = test_ar_transaction(invnumber   => 'salesinv skonto',
321                                         payment_id  => $payment_terms->id,
322                                         taxincluded => 0,
323                                         amount      => 168.58 / 1.19,
324                                        );
325
326   my $bt = create_bank_transaction(record        => $ar_transaction,
327                                    bank_chart_id => $bank->id,
328                                    transdate   => $dt,
329                                    valutadate  => $dt,
330                                    amount        => 160.15,
331                                   ) or die "Couldn't create bank_transaction";
332   $::form->{invoice_ids} = {
333     $bt->id => [ $ar_transaction->id ]
334   };
335   $::form->{invoice_skontos} = {
336     $bt->id => [ 'with_skonto_pt' ]
337   };
338
339   is($ar_transaction->paid   , '0' , "$testname: salesinv is not paid");
340
341   # generate an error for testing rollback mechanism
342   my $saved_skonto_sales_chart_id = $tax->skonto_sales_chart_id;
343   $tax->skonto_sales_chart_id(undef);
344   $tax->save;
345
346   save_btcontroller_to_string();
347   my @bt_errors = @{ $bt_controller->problems };
348   is(substr($bt_errors[0]->{message},0,38), 'Kein Skontokonto für Steuerschlüssel 3', "$testname: Fehlermeldung ok");
349   # set original value
350   $tax->skonto_sales_chart_id($saved_skonto_sales_chart_id);
351   $tax->save;
352
353   $ar_transaction->load;
354   $bt->load;
355   is($ar_transaction->paid   , '0.00000' , "$testname: salesinv was not paid");
356   is($bt->invoice_amount     , '0.00000' , "$testname: bt invoice amount was not assigned");
357
358 };
359
360 sub test_two_invoices {
361
362   my $testname = 'test_two_invoices';
363
364   my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv_1');
365   my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv_2');
366
367   my $bt = create_bank_transaction(record        => $ar_transaction_1,
368                                    amount        => ($ar_transaction_1->amount + $ar_transaction_2->amount),
369                                    purpose       => "Rechnungen " . $ar_transaction_1->invnumber . " und " . $ar_transaction_2->invnumber,
370                                    transdate     => $dt,
371                                    valutadate    => $dt,
372                                    bank_chart_id => $bank->id,
373                                   ) or die "Couldn't create bank_transaction";
374
375   my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($ar_transaction_1);
376   is($agreement, 16, "points for ar_transaction_1 in test_two_invoices ok");
377
378   $::form->{invoice_ids} = {
379     $bt->id => [ $ar_transaction_1->id, $ar_transaction_2->id ]
380   };
381
382   save_btcontroller_to_string();
383
384   $ar_transaction_1->load;
385   $ar_transaction_2->load;
386   $bt->load;
387
388   is($ar_transaction_1->paid   , '119.00000' , "$testname: salesinv_1 wcsv_import_reportsas paid");
389   is($ar_transaction_1->closed , 1           , "$testname: salesinv_1 is closed");
390   is($ar_transaction_2->paid   , '119.00000' , "$testname: salesinv_2 was paid");
391   is($ar_transaction_2->closed , 1           , "$testname: salesinv_2 is closed");
392   is($bt->invoice_amount       , '238.00000' , "$testname: bt invoice amount was assigned");
393
394 }
395
396 sub test_one_inv_and_two_invoices_with_skonto_exact {
397
398   my $testname = 'test_two_invoices_with_skonto_exact';
399
400   my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv 1 skonto',
401                                              payment_id => $payment_terms->id,
402                                             );
403   my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv 2 skonto',
404                                              payment_id => $payment_terms->id,
405                                             );
406   my $ar_transaction_3 = test_ar_transaction(invnumber => 'salesinv 3 no skonto');
407
408
409
410   my $bt = create_bank_transaction(record        => $ar_transaction_1,
411                                    bank_chart_id => $bank->id,
412                                    transdate     => $dt,
413                                    valutadate    => $dt,
414                                    amount        => $ar_transaction_1->amount_less_skonto * 2 + $ar_transaction_3->amount
415                                   ) or die "Couldn't create bank_transaction";
416
417   $::form->{invoice_ids} = {
418     $bt->id => [ $ar_transaction_1->id, $ar_transaction_3->id, $ar_transaction_2->id]
419   };
420   $::form->{invoice_skontos} = {
421     $bt->id => [ 'with_skonto_pt', 'without_skonto', 'with_skonto_pt' ]
422   };
423
424   save_btcontroller_to_string();
425
426   $ar_transaction_1->load;
427   $ar_transaction_2->load;
428   $ar_transaction_3->load;
429   my $skonto_1 = SL::DB::Manager::AccTransaction->find_by(trans_id => $ar_transaction_1->id, chart_id => 162);
430   my $skonto_2 = SL::DB::Manager::AccTransaction->find_by(trans_id => $ar_transaction_2->id, chart_id => 162);
431   $bt->load;
432   is($skonto_1->amount   , '-5.95000' , "$testname: salesinv 1 skonto was booked");
433   is($skonto_2->amount   , '-5.95000' , "$testname: salesinv 2 skonto was booked");
434   is($ar_transaction_1->paid   , '119.00000' , "$testname: salesinv 1 was paid");
435   is($ar_transaction_2->paid   , '119.00000' , "$testname: salesinv 2 was paid");
436   is($ar_transaction_3->paid   , '119.00000' , "$testname: salesinv 3 was paid");
437   is($ar_transaction_1->closed , 1           , "$testname: salesinv 1 skonto is closed");
438   is($ar_transaction_2->closed , 1           , "$testname: salesinv 2 skonto is closed");
439   is($ar_transaction_3->closed , 1           , "$testname: salesinv 2 skonto is closed");
440   is($bt->invoice_amount     , '345.10000' , "$testname: bt invoice amount was assigned");
441
442 }
443
444 sub test_overpayment {
445
446   my $testname = 'test_overpayment';
447
448   $ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid');
449
450   # amount 135 > 119
451   my $bt = create_bank_transaction(record        => $ar_transaction,
452                                    bank_chart_id => $bank->id,
453                                    transdate   => $dt,
454                                    valutadate  => $dt,
455                                    amount        => 135
456                                   ) or die "Couldn't create bank_transaction";
457
458   $::form->{invoice_ids} = {
459     $bt->id => [ $ar_transaction->id ]
460   };
461
462   save_btcontroller_to_string();
463
464   $ar_transaction->load;
465   $bt->load;
466
467   is($ar_transaction->paid                     , '119.00000' , "$testname: 'salesinv overpaid' was not overpaid");
468   is($bt->invoice_amount                       , '119.00000' , "$testname: bt invoice amount was not fully assigned with the overpaid amount");
469 { local $TODO = 'this currently fails because closed ignores over-payments, see commit d90966c7';
470   is($ar_transaction->closed                   , 0           , "$testname: 'salesinv overpaid' is open (via 'closed' method')");
471 }
472   is($ar_transaction->open_amount == 0 ? 1 : 0 , 1           , "$testname: 'salesinv overpaid is closed (via amount-paid)");
473
474 };
475
476 sub test_overpayment_with_partialpayment {
477
478   # two payments on different days, 10 and 119. If there is only one invoice we
479   # don't want it to be overpaid.
480   my $testname = 'test_overpayment_with_partialpayment';
481
482   $ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid partial');
483
484   my $bt_1 = create_bank_transaction(record        => $ar_transaction,
485                                      bank_chart_id => $bank->id,
486                                      transdate   => $dt,
487                                      valutadate  => $dt,
488                                      amount        =>  10
489                                     ) or die "Couldn't create bank_transaction";
490   my $bt_2 = create_bank_transaction(record        => $ar_transaction,
491                                      amount        => 119,
492                                      transdate     => $dt_5,
493                                      valutadate    => $dt_5,
494                                      bank_chart_id => $bank->id,
495                                     ) or die "Couldn't create bank_transaction";
496
497   $::form->{invoice_ids} = {
498     $bt_1->id => [ $ar_transaction->id ]
499   };
500   save_btcontroller_to_string();
501
502   $bt_1->load;
503   is($bt_1->invoice_amount ,  '10.00000' , "$testname: bt_1 invoice amount was fully assigned");
504   $::form->{invoice_ids} = {
505     $bt_2->id => [ $ar_transaction->id ]
506   };
507   save_btcontroller_to_string();
508
509   $ar_transaction->load;
510   $bt_2->load;
511
512   is($bt_1->invoice_amount ,  '10.00000' , "$testname: bt_1 invoice amount was fully assigned");
513   is($ar_transaction->paid , '119.00000' , "$testname: 'salesinv overpaid partial' was not overpaid");
514   is($bt_2->invoice_amount , '109.00000' , "$testname: bt_2 invoice amount was partly assigned");
515
516 };
517
518 sub test_partial_payment {
519
520   my $testname = 'test_partial_payment';
521
522   $ar_transaction = test_ar_transaction(invnumber => 'salesinv partial payment');
523
524   # amount 100 < 119
525   my $bt = create_bank_transaction(record        => $ar_transaction,
526                                    bank_chart_id => $bank->id,
527                                    transdate     => $dt,
528                                    valutadate    => $dt,
529                                    amount        => 100
530                                   ) or die "Couldn't create bank_transaction";
531
532   $::form->{invoice_ids} = {
533     $bt->id => [ $ar_transaction->id ]
534   };
535
536   save_btcontroller_to_string();
537
538   $ar_transaction->load;
539   $bt->load;
540
541   is($ar_transaction->paid , '100.00000' , "$testname: 'salesinv partial payment' was partially paid");
542   is($bt->invoice_amount   , '100.00000' , "$testname: bt invoice amount was assigned partially paid amount");
543
544   # addon test partial payment full point match
545   my $bt2 = create_bank_transaction(record        => $ar_transaction,
546                                     bank_chart_id => $bank->id,
547                                     transdate     => $dt,
548                                     valutadate    => $dt,
549                                     amount        => 19
550                                    ) or die "Couldn't create bank_transaction";
551
552   my ($agreement, $rule_matches) = $bt2->get_agreement_with_invoice($ar_transaction);
553   is($agreement, 15, "points for exact partial payment ok");
554   is($rule_matches, 'remote_account_number(3) exact_open_amount(4) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus0(3) ', "rules_matches for exact partial payment ok");
555 };
556
557 sub test_full_workflow_ar_multiple_inv_skonto_reconciliate_and_undo {
558
559   my $testname = 'test_partial_payment';
560
561   $ar_transaction = test_ar_transaction(invnumber => 'salesinv partial payment two');
562   my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv 2 22d2', amount => 22);
563
564   # amount 299.29 > 119
565   my $bt = create_bank_transaction(record        => $ar_transaction,
566                                    bank_chart_id => $bank->id,
567                                    amount        => 299.29
568                                   ) or die "Couldn't create bank_transaction";
569
570   $::form->{invoice_ids} = {
571     $bt->id => [ $ar_transaction->id ]
572   };
573
574   save_btcontroller_to_string();
575
576   $ar_transaction->load;
577   $bt->load;
578
579   is($ar_transaction->paid , '119.00000' , "$testname: 'salesinv partial payment' was fully paid");
580   is($bt->invoice_amount   , '119.00000' , "$testname: bt invoice amount was assigned partially paid amount");
581   is($bt->amount           , '299.29000' , "$testname: bt amount is stil there");
582   # next invoice, same bank transaction
583   $::form->{invoice_ids} = {
584     $bt->id => [ $ar_transaction_2->id ]
585   };
586
587   save_btcontroller_to_string();
588
589   $ar_transaction_2->load;
590   $bt->load;
591   is($ar_transaction_2->paid , '26.18000' , "$testname: 'salesinv partial payment' was fully paid");
592   is($bt->invoice_amount   , '145.18000' , "$testname: bt invoice amount was assigned partially paid amount");
593   is($bt->amount           , '299.29000' , "$testname: bt amount is stil there");
594   is(scalar @{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )}, 4, "$testname 4 acc_trans entries created");
595
596   #  now check all 4 entries done so far and save paid acc_trans_ids for later use with reconcile
597   foreach my $acc_trans_id_entry (@{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )}) {
598     isnt($acc_trans_id_entry->ar_id, undef, "$testname: bt linked with acc_trans and trans_id set");
599     my $rl = SL::DB::Manager::RecordLink->get_all(where => [ from_id => $bt->id, from_table => 'bank_transactions', to_id => $acc_trans_id_entry->ar_id ]);
600     is (ref $rl->[0], 'SL::DB::RecordLink', "$testname record link created");
601     my $acc_trans = SL::DB::Manager::AccTransaction->get_all(where => [acc_trans_id => $acc_trans_id_entry->acc_trans_id]);
602     foreach my $entry (@{ $acc_trans }) {
603       like(abs($entry->amount), qr/(119|26.18)/, "$testname: abs amount correct");
604       like($entry->chart_link, qr/(paid|AR)/, "$testname chart_link correct");
605       push @{ $::form->{bb_ids} }, $entry->acc_trans_id if $entry->chart_link =~ m/paid/;
606     }
607   }
608   # great we need one last booking to clear the whole bank transaction - we include skonto
609   my $ar_transaction_skonto = test_ar_transaction(invnumber  => 'salesinv skonto last case',
610                                                   payment_id => $payment_terms->id,
611                                                   amount     => 136.32,
612                                                  );
613
614   $::form->{invoice_ids} = {
615     $bt->id => [ $ar_transaction_skonto->id ]
616   };
617   $::form->{invoice_skontos} = {
618     $bt->id => [ 'with_skonto_pt' ]
619   };
620
621   save_btcontroller_to_string();
622
623   $ar_transaction_skonto->load;
624   $bt->load;
625   is($ar_transaction_skonto->paid , '162.22000' , "$testname: 'salesinv skonto fully paid");
626   is($bt->invoice_amount   , '299.29000' , "$testname: bt invoice amount was assigned partially paid amount");
627   is($bt->amount           , '299.29000' , "$testname: bt amount is stil there");
628   is(scalar @{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )},
629        7, "$testname 7 acc_trans entries created");
630
631   # same loop as above, but only for the 3rd ar_id
632   foreach my $acc_trans_id_entry (@{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [ar_id => $ar_transaction_skonto->id ] )}) {
633     isnt($acc_trans_id_entry->ar_id, '', "$testname: bt linked with acc_trans and trans_id set");
634     my $rl = SL::DB::Manager::RecordLink->get_all(where => [ from_id => $bt->id, from_table => 'bank_transactions', to_id => $acc_trans_id_entry->ar_id ]);
635     is (ref $rl->[0], 'SL::DB::RecordLink', "$testname record link created");
636     my $acc_trans = SL::DB::Manager::AccTransaction->get_all(where => [acc_trans_id => $acc_trans_id_entry->acc_trans_id]);
637     foreach my $entry (@{ $acc_trans }) {
638       like($entry->chart_link, qr/(paid|AR)/, "$testname chart_link correct");
639       is ($entry->amount, '162.22000', "$testname full amont") if $entry->chart_link eq 'AR'; # full amount
640       like(abs($entry->amount), qr/(154.11|8.11)/, "$testname: abs amount correct") if $entry->chart_link =~ m/paid/;
641       push @{ $::form->{bb_ids} }, $entry->acc_trans_id if ($entry->chart_link =~ m/paid/ && $entry->amount == -154.11);
642     }
643   }
644   # done, now reconciliate all bookings
645   $::form->{bt_ids} = [ $bt->id ];
646   my $rec_controller = SL::Controller::Reconciliation->new;
647   my @errors = $rec_controller->_get_elements_and_validate;
648
649   is (scalar @errors, 0, "$testname unsuccesfull reconciliation with error: " . Dumper(@errors));
650   $rec_controller->_reconcile;
651   $bt->load;
652
653   # and check the cleared state of bt and the acc_transactions
654   is($bt->cleared, '1' , "$testname: bt cleared");
655   foreach (@{ $::form->{bb_ids} }) {
656     my $acc_trans = SL::DB::Manager::AccTransaction->find_by(acc_trans_id => $_);
657     is($acc_trans->cleared, '1' , "$testname: acc_trans entry cleared");
658   }
659   # now, this was a really bad idea and in general a major mistake. better undo and redo the whole bank transactions
660
661   $::form->{ids} = [ $bt->id ];
662   $bt_controller = SL::Controller::BankTransaction->new;
663   $bt_controller->action_unlink_bank_transaction('testcase' => 1);
664
665   $bt->load;
666
667   # and check the cleared state of bt and the acc_transactions
668   is($bt->cleared, '0' , "$testname: bt undo cleared");
669   is($bt->invoice_amount, '0.00000' , "$testname: bt undo invoice amount");
670   foreach (@{ $::form->{bb_ids} }) {
671     my $acc_trans = SL::DB::Manager::AccTransaction->find_by(acc_trans_id => $_);
672     is($acc_trans, undef , "$testname: cleared acc_trans entry completely removed");
673   }
674   # this was for data integrity for reconcile, now all the other options
675   is(scalar @{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt->id ] )},
676        0, "$testname 7 acc_trans entries deleted");
677   my $rl = SL::DB::Manager::RecordLink->get_all(where => [ from_id => $bt->id, from_table => 'bank_transactions' ]);
678   is (ref $rl->[0], '', "$testname record link removed");
679   # double safety and check ar.paid
680   # load all three invoices and check for paid-link via acc_trans and paid in general
681
682   $ar_transaction->load;
683   $ar_transaction_2->load;
684   $ar_transaction_skonto->load;
685
686   is(scalar @{ SL::DB::Manager::AccTransaction->get_all(
687      where => [ trans_id => $ar_transaction->id, chart_link => { like => '%paid%' } ])},
688      0, "$testname no more paid entries in acc_trans for ar_transaction");
689   is(scalar @{ SL::DB::Manager::AccTransaction->get_all(
690      where => [ trans_id => $ar_transaction_2->id, chart_link => { like => '%paid%' } ])},
691      0, "$testname no more paid entries in acc_trans for ar_transaction_2");
692   is(scalar @{ SL::DB::Manager::AccTransaction->get_all(
693      where => [ trans_id => $ar_transaction_skonto->id, chart_link => { like => '%paid%' } ])},
694      0, "$testname no more paid entries in acc_trans for ar_transaction_skonto");
695
696   is($ar_transaction->paid , '0.00000' , "$testname: 'salesinv fully unpaid");
697   is($ar_transaction_2->paid , '0.00000' , "$testname: 'salesinv 2 fully unpaid");
698   is($ar_transaction_skonto->paid , '0.00000' , "$testname: 'salesinv skonto fully unpaid");
699
700   # whew. w(h)a(n)t a whole lotta test
701 }
702
703
704 sub test_credit_note {
705
706   my $testname = 'test_credit_note';
707
708   my $part1 = new_part(   partnumber => 'T4254')->save;
709   my $part2 = new_service(partnumber => 'Serv1')->save;
710   my $credit_note = create_credit_note(
711     invnumber    => 'cn 1',
712     customer     => $customer,
713     transdate    => $dt,
714     taxincluded  => 0,
715     invoiceitems => [ create_invoice_item(part => $part1, qty =>  3, sellprice => 70),
716                       create_invoice_item(part => $part2, qty => 10, sellprice => 50),
717                     ]
718   );
719   my $bt            = create_bank_transaction(record        => $credit_note,
720                                                                 amount        => $credit_note->amount,
721                                                                 bank_chart_id => $bank->id,
722                                                                 transdate     => $dt_10,
723                                                                );
724   my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($credit_note);
725   is($agreement, 14, "points for credit note ok");
726   is($rule_matches, 'remote_account_number(3) exact_amount(4) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus14(2) ', "rules_matches for credit note ok");
727
728   $::form->{invoice_ids} = {
729     $bt->id => [ $credit_note->id ]
730   };
731
732   save_btcontroller_to_string();
733
734   $credit_note->load;
735   $bt->load;
736   is($credit_note->amount   , '-844.90000', "$testname: amount ok");
737   is($credit_note->netamount, '-710.00000', "$testname: netamount ok");
738   is($credit_note->paid     , '-844.90000', "$testname: paid ok");
739 }
740
741 sub test_neg_ap_transaction {
742   my (%params) = @_;
743   my $testname = 'test_neg_ap_transaction' . $params{invoice} ? ' invoice booking' : ' credit booking';
744   my $netamount = -20;
745   my $amount    = $::form->round_amount($netamount * 1.19,2);
746   my $invoice   = SL::DB::PurchaseInvoice->new(
747     invoice      => $params{invoice} // 0,
748     invnumber    => $params{invnumber} || 'test_neg_ap_transaction',
749     amount       => $amount,
750     netamount    => $netamount,
751     transdate    => $dt,
752     taxincluded  => 0,
753     vendor_id    => $vendor->id,
754     taxzone_id   => $vendor->taxzone_id,
755     currency_id  => $currency_id,
756     transactions => [],
757     notes        => 'test_neg_ap_transaction',
758   );
759   $invoice->add_ap_amount_row(
760     amount     => $invoice->netamount,
761     chart      => $ap_amount_chart,
762     tax_id     => $tax_9->id,
763   );
764
765   $invoice->create_ap_row(chart => $ap_chart);
766   $invoice->save;
767
768   is($invoice->netamount, -20  , "$testname: netamount ok");
769   is($invoice->amount   , -23.8, "$testname: amount ok");
770
771   my $bt            = create_bank_transaction(record        => $invoice,
772                                               amount        => $invoice->amount,
773                                               bank_chart_id => $bank->id,
774                                               transdate     => $dt_10,
775                                                                );
776
777   my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($invoice);
778   is($agreement, 16, "points for negative ap transaction ok");
779
780   $::form->{invoice_ids} = {
781     $bt->id => [ $invoice->id ]
782   };
783
784   save_btcontroller_to_string();
785
786   $invoice->load;
787   $bt->load;
788
789   is($invoice->amount   , '-23.80000', "$testname: amount ok");
790   is($invoice->netamount, '-20.00000', "$testname: netamount ok");
791   is($invoice->paid     , '-23.80000', "$testname: paid ok");
792   is($bt->invoice_amount, '23.80000', "$testname: bt invoice amount for ap was assigned");
793   is($bt->amount,         '23.80000', "$testname: bt  amount for ap was assigned");
794
795   return $invoice;
796 };
797 sub test_two_neg_ap_transaction {
798   my $testname='test_two_neg_ap_transaction';
799   my $netamount = -20;
800   my $amount    = $::form->round_amount($netamount * 1.19,2);
801   my $invoice   = SL::DB::PurchaseInvoice->new(
802     invoice      =>  0,
803     invnumber    => 'test_neg_ap_transaction',
804     amount       => $amount,
805     netamount    => $netamount,
806     transdate    => $dt,
807     taxincluded  => 0,
808     vendor_id    => $vendor->id,
809     taxzone_id   => $vendor->taxzone_id,
810     currency_id  => $currency_id,
811     transactions => [],
812     notes        => 'test_neg_ap_transaction',
813   );
814   $invoice->add_ap_amount_row(
815     amount     => $invoice->netamount,
816     chart      => $ap_amount_chart,
817     tax_id     => $tax_9->id,
818   );
819
820   $invoice->create_ap_row(chart => $ap_chart);
821   $invoice->save;
822
823   is($invoice->netamount, -20  , "$testname: netamount ok");
824   is($invoice->amount   , -23.8, "$testname: amount ok");
825
826   my $netamount_two = -1.14;
827   my $amount_two    = $::form->round_amount($netamount_two * 1.19,2);
828   my $invoice_two   = SL::DB::PurchaseInvoice->new(
829     invoice      => 0,
830     invnumber    => 'test_neg_ap_transaction_two',
831     amount       => $amount_two,
832     netamount    => $netamount_two,
833     transdate    => $dt,
834     taxincluded  => 0,
835     vendor_id    => $vendor->id,
836     taxzone_id   => $vendor->taxzone_id,
837     currency_id  => $currency_id,
838     transactions => [],
839     notes        => 'test_neg_ap_transaction_two',
840   );
841   $invoice_two->add_ap_amount_row(
842     amount     => $invoice_two->netamount,
843     chart      => $ap_amount_chart,
844     tax_id     => $tax_9->id,
845   );
846
847   $invoice_two->create_ap_row(chart => $ap_chart);
848   $invoice_two->save;
849
850   is($invoice_two->netamount, -1.14  , "$testname: netamount ok");
851   is($invoice_two->amount   , -1.36, "$testname: amount ok");
852
853
854   my $bt            = create_bank_transaction(record        => $invoice_two,
855                                               amount        => $invoice_two->amount + $invoice->amount,
856                                               bank_chart_id => $bank->id,
857                                               transdate     => $dt_10,
858                                                                );
859   # my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($invoice_two);
860   # is($agreement, 15, "points for negative ap transaction ok");
861
862   $::form->{invoice_ids} = {
863     $bt->id => [ $invoice->id, $invoice_two->id ]
864   };
865
866   save_btcontroller_to_string();
867
868   $invoice->load;
869   $invoice_two->load;
870   $bt->load;
871
872   is($invoice->amount   , '-23.80000', "$testname: first inv amount ok");
873   is($invoice->netamount, '-20.00000', "$testname: first inv netamount ok");
874   is($invoice->paid     , '-23.80000', "$testname: first inv paid ok");
875   is($invoice_two->amount   , '-1.36000', "$testname: second inv amount ok");
876   is($invoice_two->netamount, '-1.14000', "$testname: second inv netamount ok");
877   is($invoice_two->paid     , '-1.36000', "$testname: second inv paid ok");
878   is($bt->invoice_amount, '25.16000', "$testname: bt invoice amount for both invoices were assigned");
879
880
881   return ($invoice, $invoice_two);
882 };
883
884 sub test_ap_payment_transaction {
885   my (%params) = @_;
886   my $testname = 'test_ap_payment_transaction';
887   my $netamount = 115;
888   my $amount    = $::form->round_amount($netamount * 1.19,2);
889   my $invoice   = SL::DB::PurchaseInvoice->new(
890     invoice      => 0,
891     invnumber    => $params{invnumber} || $testname,
892     amount       => $amount,
893     netamount    => $netamount,
894     transdate    => $dt,
895     taxincluded  => 0,
896     vendor_id    => $vendor->id,
897     taxzone_id   => $vendor->taxzone_id,
898     currency_id  => $currency_id,
899     transactions => [],
900     notes        => $testname,
901   );
902   $invoice->add_ap_amount_row(
903     amount     => $invoice->netamount,
904     chart      => $ap_amount_chart,
905     tax_id     => $tax_9->id,
906   );
907
908   $invoice->create_ap_row(chart => $ap_chart);
909   $invoice->save;
910
911   is($invoice->netamount, 115  , "$testname: netamount ok");
912   is($invoice->amount   , 136.85, "$testname: amount ok");
913
914   my $bt            = create_bank_transaction(record        => $invoice,
915                                               amount        => $invoice->amount,
916                                               bank_chart_id => $bank->id,
917                                               transdate     => $dt_10,
918                                              );
919   $::form->{invoice_ids} = {
920     $bt->id => [ $invoice->id ]
921   };
922
923   save_btcontroller_to_string();
924
925   $invoice->load;
926   $bt->load;
927
928   is($invoice->amount   , '136.85000', "$testname: amount ok");
929   is($invoice->netamount, '115.00000', "$testname: netamount ok");
930   is($bt->amount,         '-136.85000', "$testname: bt amount ok");
931   is($invoice->paid     , '136.85000', "$testname: paid ok");
932   is($bt->invoice_amount, '-136.85000', "$testname: bt invoice amount for ap was assigned");
933
934   return $invoice;
935 };
936
937 sub test_ap_payment_part_transaction {
938   my (%params) = @_;
939   my $testname = 'test_ap_payment_p_transaction';
940   my $netamount = 115;
941   my $amount    = $::form->round_amount($netamount * 1.19,2);
942   my $invoice   = SL::DB::PurchaseInvoice->new(
943     invoice      => 0,
944     invnumber    => $params{invnumber} || $testname,
945     amount       => $amount,
946     netamount    => $netamount,
947     transdate    => $dt,
948     taxincluded  => 0,
949     vendor_id    => $vendor->id,
950     taxzone_id   => $vendor->taxzone_id,
951     currency_id  => $currency_id,
952     transactions => [],
953     notes        => $testname,
954   );
955   $invoice->add_ap_amount_row(
956     amount     => $invoice->netamount,
957     chart      => $ap_amount_chart,
958     tax_id     => $tax_9->id,
959   );
960
961   $invoice->create_ap_row(chart => $ap_chart);
962   $invoice->save;
963
964   is($invoice->netamount, 115  , "$testname: netamount ok");
965   is($invoice->amount   , 136.85, "$testname: amount ok");
966
967   my $bt            = create_bank_transaction(record        => $invoice,
968                                               amount        => $invoice->amount-100,
969                                               bank_chart_id => $bank->id,
970                                               transdate     => $dt_10,
971                                              );
972   $::form->{invoice_ids} = {
973     $bt->id => [ $invoice->id ]
974   };
975
976   save_btcontroller_to_string();
977
978   $invoice->load;
979   $bt->load;
980
981   is($invoice->amount   , '136.85000', "$testname: amount ok");
982   is($invoice->netamount, '115.00000', "$testname: netamount ok");
983   is($bt->amount,         '-36.85000', "$testname: bt amount ok");
984   is($invoice->paid     ,  '36.85000', "$testname: paid ok");
985   is($bt->invoice_amount, '-36.85000', "$testname: bt invoice amount for ap was assigned");
986
987   my $bt2           = create_bank_transaction(record        => $invoice,
988                                               amount        => 100,
989                                               bank_chart_id => $bank->id,
990                                               transdate     => $dt_10,
991                                              );
992   $::form->{invoice_ids} = {
993     $bt2->id => [ $invoice->id ]
994   };
995
996   save_btcontroller_to_string();
997   $invoice->load;
998   $bt2->load;
999
1000   is($invoice->amount   , '136.85000', "$testname: amount ok");
1001   is($invoice->netamount, '115.00000', "$testname: netamount ok");
1002   is($bt2->amount,        '-100.00000',"$testname: bt amount ok");
1003   is($invoice->paid     , '136.85000', "$testname: paid ok");
1004   is($bt2->invoice_amount,'-100.00000', "$testname: bt invoice amount for ap was assigned");
1005
1006   return $invoice;
1007 };
1008
1009 sub test_neg_sales_invoice {
1010
1011   my $testname = 'test_neg_sales_invoice';
1012
1013   my $part1 = new_part(   partnumber => 'Funkenhaube öhm')->save;
1014   my $part2 = new_service(partnumber => 'Service-Pauschale Pasch!')->save;
1015
1016   my $neg_sales_inv = create_sales_invoice(
1017     invnumber    => '20172201',
1018     customer     => $customer,
1019     taxincluded  => 0,
1020     transdate     => $dt,
1021     invoiceitems => [ create_invoice_item(part => $part1, qty =>  3, sellprice => 70),
1022                       create_invoice_item(part => $part2, qty => 10, sellprice => -50),
1023                     ]
1024   );
1025   my $bt            = create_bank_transaction(record        => $neg_sales_inv,
1026                                                                 amount        => $neg_sales_inv->amount,
1027                                                                 bank_chart_id => $bank->id,
1028                                                                 transdate     => $dt,
1029                                                                 valutadate    => $dt,
1030                                                                );
1031   $::form->{invoice_ids} = {
1032     $bt->id => [ $neg_sales_inv->id ]
1033   };
1034
1035   save_btcontroller_to_string();
1036
1037   $neg_sales_inv->load;
1038   $bt->load;
1039   is($neg_sales_inv->amount   , '-345.10000', "$testname: amount ok");
1040   is($neg_sales_inv->netamount, '-290.00000', "$testname: netamount ok");
1041   is($neg_sales_inv->paid     , '-345.10000', "$testname: paid ok");
1042   is($bt->amount              , '-345.10000', "$testname: bt amount ok");
1043   is($bt->invoice_amount      , '-345.10000', "$testname: bt invoice_amount ok");
1044 }
1045
1046 sub test_bt_rule1 {
1047
1048   my $testname = 'test_bt_rule1';
1049
1050   $ar_transaction = test_ar_transaction(invnumber => 'bt_rule1', transdate => $dt);
1051
1052   my $bt = create_bank_transaction(record => $ar_transaction, transdate => $dt) or die "Couldn't create bank_transaction";
1053
1054   $ar_transaction->load;
1055   $bt->load;
1056   is($ar_transaction->paid   , '0.00000' , "$testname: not paid");
1057   is($bt->invoice_amount     , '0.00000' , "$testname: bt invoice amount was not assigned");
1058
1059   my $bt_controller = SL::Controller::BankTransaction->new;
1060   my ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
1061
1062   is(scalar(@$bt_transactions)         , 1  , "$testname: one bank_transaction");
1063   is($bt_transactions->[0]->{agreement}, 20 , "$testname: agreement == 20");
1064   my $match = join ( ' ',@{$bt_transactions->[0]->{rule_matches}});
1065   #print "rule_matches='".$match."'\n";
1066   is($match,
1067      "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) ",
1068      "$testname: rule_matches ok");
1069   $bt->invoice_amount($bt->amount);
1070   $bt->save;
1071   is($bt->invoice_amount     , '119.00000' , "$testname: bt invoice amount now set");
1072 };
1073
1074 sub test_sepa_export {
1075
1076   my $testname = 'test_sepa_export';
1077
1078   $ar_transaction = test_ar_transaction(invnumber => 'sepa1', transdate => $dt);
1079
1080   my $bt  = create_bank_transaction(record => $ar_transaction, transdate => $dt) or die "Couldn't create bank_transaction";
1081   my $se  = create_sepa_export();
1082   my $sei = create_sepa_export_item(
1083     chart_id       => $bank->id,
1084     ar_id          => $ar_transaction->id,
1085     sepa_export_id => $se->id,
1086     vc_iban        => $customer->iban,
1087     vc_bic         => $customer->bic,
1088     vc_mandator_id => $customer->mandator_id,
1089     vc_depositor   => $customer->depositor,
1090     amount         => $ar_transaction->amount,
1091   );
1092   require SL::SEPA::XML;
1093   my $sepa_xml   = SL::SEPA::XML->new('company'     => $customer->name,
1094                                       'creditor_id' => "id",
1095                                       'src_charset' => 'UTF-8',
1096                                       'message_id'  => "test",
1097                                       'grouped'     => 1,
1098                                       'collection'  => 1,
1099                                      );
1100   is($sepa_xml->{company}    , 'Test Customer OLE S.L. Ardbaerg AB');
1101
1102   $ar_transaction->load;
1103   $bt->load;
1104   $sei->load;
1105   is($ar_transaction->paid   , '0.00000' , "$testname: sepa1 not paid");
1106   is($bt->invoice_amount     , '0.00000' , "$testname: bt invoice amount was not assigned");
1107   is($bt->amount             , '119.00000' , "$testname: bt amount ok");
1108   is($sei->amount            , '119.00000' , "$testname: sepa export amount ok");
1109
1110   my $bt_controller = SL::Controller::BankTransaction->new;
1111   my ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
1112
1113   is(scalar(@$bt_transactions)         , 1  , "$testname: one bank_transaction");
1114   is($bt_transactions->[0]->{agreement}, 25 , "$testname: agreement == 25");
1115   my $match = join ( ' ',@{$bt_transactions->[0]->{rule_matches}});
1116   is($match,
1117      "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) ",
1118      "$testname: rule_matches ok");
1119 };
1120
1121 sub test_two_banktransactions {
1122
1123   my $testname = 'two_banktransactions';
1124
1125   my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv10000' , amount => 2912.00 );
1126   my $bt1 = create_bank_transaction(record        => $ar_transaction_1,
1127                                     amount        => $ar_transaction_1->amount,
1128                                     purpose       => "Rechnung10000 beinahe",
1129                                     transdate     => $dt,
1130                                     bank_chart_id => $bank->id,
1131                                   ) or die "Couldn't create bank_transaction";
1132
1133   my $bt2 = create_bank_transaction(record        => $ar_transaction_1,
1134                                     amount        => $ar_transaction_1->amount + 0.01,
1135                                     purpose       => "sicher salesinv20000 vielleicht",
1136                                     transdate     => $dt,
1137                                     bank_chart_id => $bank->id,
1138                                   ) or die "Couldn't create bank_transaction";
1139
1140   my ($agreement1, $rule_matches1) = $bt1->get_agreement_with_invoice($ar_transaction_1);
1141   is($agreement1, 19, "bt1 19 points for ar_transaction_1 in $testname ok");
1142   #print "rule_matches1=".$rule_matches1."\n";
1143   is($rule_matches1,
1144      "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) ",
1145      "$testname: rule_matches ok");
1146   my ($agreement2, $rule_matches2) = $bt2->get_agreement_with_invoice($ar_transaction_1);
1147   is($agreement2, 11, "bt2 11 points for ar_transaction_1 in $testname ok");
1148   is($rule_matches2,
1149      "remote_account_number(3) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus0(3) ",
1150      "$testname: rule_matches ok");
1151
1152   my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv20000' , amount => 2912.01 );
1153   my $ar_transaction_3 = test_ar_transaction(invnumber => 'zweitemit10000', amount => 2912.00 );
1154      ($agreement1, $rule_matches1) = $bt1->get_agreement_with_invoice($ar_transaction_2);
1155
1156   is($agreement1, 11, "bt1 11 points for ar_transaction_2 in $testname ok");
1157
1158      ($agreement2, $rule_matches2) = $bt2->get_agreement_with_invoice($ar_transaction_2);
1159   is($agreement2, 20, "bt2 20 points for ar_transaction_2 in $testname ok");
1160
1161      ($agreement2, $rule_matches2) = $bt2->get_agreement_with_invoice($ar_transaction_1);
1162   is($agreement2, 11, "bt2 11 points for ar_transaction_1 in $testname ok");
1163
1164   my $bt3 = create_bank_transaction(record        => $ar_transaction_3,
1165                                     amount        => $ar_transaction_3->amount,
1166                                     purpose       => "sicher Rechnung10000 vielleicht",
1167                                     transdate     => $dt,
1168                                     bank_chart_id => $bank->id,
1169                                   ) or die "Couldn't create bank_transaction";
1170
1171   my ($agreement3, $rule_matches3) = $bt3->get_agreement_with_invoice($ar_transaction_3);
1172   is($agreement3, 19, "bt3 19 points for ar_transaction_3 in $testname ok");
1173
1174   $bt2->delete;
1175   $ar_transaction_2->delete;
1176
1177   #nun sollten zwei gleichwertige Rechnungen $ar_transaction_1 und $ar_transaction_3 für $bt1 gefunden werden
1178   #aber es darf keine Proposals geben mit mehreren Rechnungen
1179   my $bt_controller = SL::Controller::BankTransaction->new;
1180   my ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
1181
1182   is(scalar(@$bt_transactions)   , 2  , "$testname: two bank_transaction");
1183   is(scalar(@$proposals)         , 0  , "$testname: no proposals");
1184
1185   $ar_transaction_3->delete;
1186
1187   # Jetzt gibt es zwei Kontobewegungen mit gleichen Punkten für eine Rechnung.
1188   # hier darf es auch keine Proposals geben
1189
1190   ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
1191
1192   is(scalar(@$bt_transactions)   , 2  , "$testname: two bank_transaction");
1193   # odyn testfall - anforderungen so (noch) nicht in kivi
1194   # is(scalar(@$proposals)         , 0  , "$testname: no proposals");
1195
1196   # Jetzt gibt es zwei Kontobewegungen für eine Rechnung.
1197   # eine Bewegung bekommt mehr Punkte
1198   # hier darf es auch keine Proposals geben
1199   $bt3->update_attributes( purpose => "fuer Rechnung salesinv10000");
1200
1201   ( $bt_transactions, $proposals ) = $bt_controller->gather_bank_transactions_and_proposals(bank_account => $bank_account);
1202
1203   is(scalar(@$bt_transactions)   , 2  , "$testname: two bank_transaction");
1204   # odyn testfall - anforderungen so (noch) nicht in kivi
1205   # is(scalar(@$proposals)         , 1  , "$testname: one proposal");
1206
1207 };
1208 sub test_closedto {
1209
1210   my $testname = 'closedto';
1211
1212   my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv10000' , amount => 2912.00 );
1213   my $bt1 = create_bank_transaction(record        => $ar_transaction_1,
1214                                     amount        => $ar_transaction_1->amount,
1215                                     purpose       => "Rechnung10000 beinahe",
1216                                     bank_chart_id => $bank->id,
1217                                   ) or die "Couldn't create bank_transaction";
1218
1219   $bt1->valutadate(DateTime->new(year => 2019, month => 12, day => 30));
1220   $bt1->save();
1221
1222   is($bt1->closed_period, 0, "$testname undefined closedto");
1223
1224   my $defaults = SL::DB::Manager::Default->get_all(limit => 1)->[0];
1225   $defaults->closedto(DateTime->new(year => 2019, month => 12, day => 31));
1226   $defaults->save();
1227   $::instance_conf->reload->data;
1228   $bt1->load();
1229   is($bt1->closed_period, 1, "$testname defined and next date closedto");
1230
1231   $bt1->valutadate(DateTime->new(year => 2020, month => 1, day => 1));
1232   $bt1->save();
1233   $bt1->load();
1234
1235   is($bt1->closed_period, 0, "$testname defined closedto and next date valuta");
1236   $defaults->closedto(undef);
1237   $defaults->save();
1238
1239 }
1240
1241 sub test_skonto_exact_ap_transaction {
1242
1243   my $testname = 'test_skonto_exact_ap_transaction';
1244
1245   $ap_transaction = test_ap_transaction(invnumber => 'ap transaction skonto',
1246                                         payment_id => $payment_terms->id,
1247                                        );
1248
1249   my $bt = create_bank_transaction(record        => $ap_transaction,
1250                                    bank_chart_id => $bank->id,
1251                                    transdate     => $dt,
1252                                    valutadate    => $dt,
1253                                    amount        => $ap_transaction->amount_less_skonto
1254                                   ) or die "Couldn't create bank_transaction";
1255
1256   $::form->{invoice_ids} = {
1257     $bt->id => [ $ap_transaction->id ]
1258   };
1259   $::form->{invoice_skontos} = {
1260     $bt->id => [ 'with_skonto_pt' ]
1261   };
1262
1263   save_btcontroller_to_string();
1264
1265   $ap_transaction->load;
1266   $bt->load;
1267   is($ap_transaction->paid   , '119.00000' , "$testname: ap transaction skonto was paid");
1268   is($ap_transaction->closed , 1           , "$testname: ap transaction skonto is closed");
1269   is($bt->invoice_amount     , '113.05000' , "$testname: bt invoice amount was assigned");
1270
1271 };
1272
1273 1;