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