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