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