Testfall test_ap_transaction auch ausführen
[kivitendo-erp.git] / t / bank / bank_transactions.t
1 use Test::More tests => 89;
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::Buchungsgruppe;
14 use SL::DB::Currency;
15 use SL::DB::Customer;
16 use SL::DB::Vendor;
17 use SL::DB::Invoice;
18 use SL::DB::Unit;
19 use SL::DB::Part;
20 use SL::DB::TaxZone;
21 use SL::DB::BankAccount;
22 use SL::DB::PaymentTerm;
23 use SL::DB::PurchaseInvoice;
24 use SL::DB::BankTransaction;
25 use SL::Controller::BankTransaction;
26 use SL::Dev::ALL;
27 use Data::Dumper;
28
29 my ($customer, $vendor, $currency_id, $unit, $tax, $tax7, $tax_9, $payment_terms, $bank_account);
30 my ($transdate1, $transdate2, $currency);
31 my ($ar_chart,$bank,$ar_amount_chart, $ap_chart, $ap_amount_chart);
32 my ($ar_transaction, $ap_transaction);
33
34 sub clear_up {
35
36   SL::DB::Manager::BankTransaction->delete_all(all => 1);
37   SL::DB::Manager::InvoiceItem->delete_all(all => 1);
38   SL::DB::Manager::InvoiceItem->delete_all(all => 1);
39   SL::DB::Manager::Invoice->delete_all(all => 1);
40   SL::DB::Manager::PurchaseInvoice->delete_all(all => 1);
41   SL::DB::Manager::Part->delete_all(all => 1);
42   SL::DB::Manager::Customer->delete_all(all => 1);
43   SL::DB::Manager::Vendor->delete_all(all => 1);
44   SL::DB::Manager::BankAccount->delete_all(all => 1);
45   SL::DB::Manager::PaymentTerm->delete_all(all => 1);
46   SL::DB::Manager::Currency->delete_all(where => [ name => 'CUR' ]);
47 };
48
49 sub save_btcontroller_to_string {
50   my $output;
51   open(my $outputFH, '>', \$output) or die;
52   my $oldFH = select $outputFH;
53
54   my $bt_controller = SL::Controller::BankTransaction->new;
55   $bt_controller->action_save_invoices;
56
57   select $oldFH;
58   close $outputFH;
59   return $output;
60 }
61
62 # starting test:
63 Support::TestSetup::login();
64
65 clear_up();
66 reset_state(); # initialise customers/vendors/bank/currency/...
67
68 test1();
69 test_overpayment_with_partialpayment();
70 test_overpayment();
71 test_skonto_exact();
72 test_two_invoices();
73 test_partial_payment();
74 test_credit_note();
75 test_neg_ap_transaction();
76 test_ap_transaction();
77 test_ap_payment_transaction();
78 # remove all created data at end of test
79 clear_up();
80
81 done_testing();
82
83 ###### functions for setting up data
84
85 sub reset_state {
86   my %params = @_;
87
88   $params{$_} ||= {} for qw(unit customer tax vendor);
89
90   clear_up();
91
92   $transdate1 = DateTime->today;
93   $transdate2 = DateTime->today->add(days => 5);
94
95   $tax             = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} }) || croak "No tax";
96   $tax7            = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07)                    || croak "No tax for 7\%";
97   $tax_9           = SL::DB::Manager::Tax->find_by(taxkey => 9, rate => 0.19, %{ $params{tax} }) || croak "No tax";
98
99   $currency_id     = $::instance_conf->get_currency_id;
100
101   $bank_account     =  SL::DB::BankAccount->new(
102     account_number  => '123',
103     bank_code       => '123',
104     iban            => '123',
105     bic             => '123',
106     bank            => '123',
107     chart_id        => SL::DB::Manager::Chart->find_by(description => 'Bank')->id,
108     name            => SL::DB::Manager::Chart->find_by(description => 'Bank')->description,
109   )->save;
110
111   $customer = SL::Dev::CustomerVendor::create_customer(
112     name                      => 'Test Customer',
113     iban                      => 'DE12500105170648489890',
114     bic                       => 'TESTBIC',
115     account_number            => '648489890',
116     mandate_date_of_signature => $transdate1,
117     mandator_id               => 'foobar',
118     bank                      => 'Geizkasse',
119     bank_code                 => 'G1235',
120     depositor                 => 'Test Customer',
121   )->save;
122
123   $payment_terms = SL::Dev::Payment::create_payment_terms;
124
125   $vendor = SL::Dev::CustomerVendor::create_vendor(
126     name           => 'Test Vendor',
127     payment_id     => $payment_terms->id,
128     iban           => 'DE12500105170648489890',
129     bic            => 'TESTBIC',
130     account_number => '648489890',
131     bank           => 'Geizkasse',
132     bank_code      => 'G1235',
133     depositor      => 'Test Vendor',
134   )->save;
135
136   $ar_chart        = SL::DB::Manager::Chart->find_by( accno => '1400' ); # Forderungen
137   $ap_chart        = SL::DB::Manager::Chart->find_by( accno => '1600' ); # Verbindlichkeiten
138   $bank            = SL::DB::Manager::Chart->find_by( accno => '1200' ); # Bank
139   $ar_amount_chart = SL::DB::Manager::Chart->find_by( accno => '8400' ); # Erlöse
140   $ap_amount_chart = SL::DB::Manager::Chart->find_by( accno => '3400' ); # Wareneingang 19%
141
142 }
143
144 sub test_ar_transaction {
145   my (%params) = @_;
146   my $netamount = 100;
147   my $amount    = $params{amount} || $::form->round_amount(100 * 1.19,2);
148   my $invoice   = SL::DB::Invoice->new(
149       invoice      => 0,
150       invnumber    => $params{invnumber} || undef, # let it use its own invnumber
151       amount       => $amount,
152       netamount    => $netamount,
153       transdate    => $transdate1,
154       taxincluded  => 0,
155       customer_id  => $customer->id,
156       taxzone_id   => $customer->taxzone_id,
157       currency_id  => $currency_id,
158       transactions => [],
159       payment_id   => $params{payment_id} || undef,
160       notes        => 'test_ar_transaction',
161   );
162   $invoice->add_ar_amount_row(
163     amount => $invoice->netamount,
164     chart  => $ar_amount_chart,
165     tax_id => $tax->id,
166   );
167
168   $invoice->create_ar_row(chart => $ar_chart);
169   $invoice->save;
170
171   is($invoice->currency_id , $currency_id , 'currency_id has been saved');
172   is($invoice->netamount   , 100          , 'ar amount has been converted');
173   is($invoice->amount      , 119          , 'ar amount has been converted');
174   is($invoice->taxincluded , 0            , 'ar transaction doesn\'t have taxincluded');
175
176   is(SL::DB::Manager::AccTransaction->find_by(chart_id => $ar_amount_chart->id , trans_id => $invoice->id)->amount , '100.00000'  , $ar_amount_chart->accno . ': has been converted for currency');
177   is(SL::DB::Manager::AccTransaction->find_by(chart_id => $ar_chart->id        , trans_id => $invoice->id)->amount , '-119.00000' , $ar_chart->accno . ': has been converted for currency');
178
179   return $invoice;
180 };
181
182 sub test_ap_transaction {
183   my (%params) = @_;
184   my $netamount = 100;
185   my $amount    = $::form->round_amount($netamount * 1.19,2);
186   my $invoice   = SL::DB::PurchaseInvoice->new(
187     invoice      => 0,
188     invnumber    => $params{invnumber} || 'test_ap_transaction',
189     amount       => $amount,
190     netamount    => $netamount,
191     transdate    => $transdate1,
192     taxincluded  => 0,
193     vendor_id    => $vendor->id,
194     taxzone_id   => $vendor->taxzone_id,
195     currency_id  => $currency_id,
196     transactions => [],
197     notes        => 'test_ap_transaction',
198   );
199   $invoice->add_ap_amount_row(
200     amount     => $invoice->netamount,
201     chart      => $ap_amount_chart,
202     tax_id     => $tax_9->id,
203   );
204
205   $invoice->create_ap_row(chart => $ap_chart);
206   $invoice->save;
207
208   is($invoice->currency_id , $currency_id , 'currency_id has been saved');
209   is($invoice->netamount   , 100          , 'ap amount has been converted');
210   is($invoice->amount      , 119          , 'ap amount has been converted');
211   is($invoice->taxincluded , 0            , 'ap transaction doesn\'t have taxincluded');
212
213   is(SL::DB::Manager::AccTransaction->find_by(chart_id => $ap_amount_chart->id , trans_id => $invoice->id)->amount , '-100.00000' , $ap_amount_chart->accno . ': has been converted for currency');
214   is(SL::DB::Manager::AccTransaction->find_by(chart_id => $ap_chart->id        , trans_id => $invoice->id)->amount , '119.00000'  , $ap_chart->accno . ': has been converted for currency');
215
216   return $invoice;
217 };
218
219 ###### test cases
220
221 sub test1 {
222
223   my $testname = 'test1';
224
225   $ar_transaction = test_ar_transaction(invnumber => 'salesinv1');
226
227   my $bt = SL::Dev::Payment::create_bank_transaction(record => $ar_transaction) or die "Couldn't create bank_transaction";
228
229   $::form->{invoice_ids} = {
230     $bt->id => [ $ar_transaction->id ]
231   };
232
233   save_btcontroller_to_string();
234
235   $ar_transaction->load;
236   $bt->load;
237   is($ar_transaction->paid   , '119.00000' , "$testname: salesinv1 was paid");
238   is($ar_transaction->closed , 1           , "$testname: salesinv1 is closed");
239   is($bt->invoice_amount     , '119.00000' , "$testname: bt invoice amount was assigned");
240
241 };
242
243 sub test_skonto_exact {
244
245   my $testname = 'test_skonto_exact';
246
247   $ar_transaction = test_ar_transaction(invnumber => 'salesinv skonto',
248                                         payment_id => $payment_terms->id,
249                                        );
250
251   my $bt = SL::Dev::Payment::create_bank_transaction(record        => $ar_transaction,
252                                                      bank_chart_id => $bank->id,
253                                                      amount        => $ar_transaction->amount_less_skonto
254                                                     ) or die "Couldn't create bank_transaction";
255
256   $::form->{invoice_ids} = {
257     $bt->id => [ $ar_transaction->id ]
258   };
259   $::form->{invoice_skontos} = {
260     $bt->id => [ 'with_skonto_pt' ]
261   };
262
263   save_btcontroller_to_string();
264
265   $ar_transaction->load;
266   $bt->load;
267   is($ar_transaction->paid   , '119.00000' , "$testname: salesinv skonto was paid");
268   is($ar_transaction->closed , 1           , "$testname: salesinv skonto is closed");
269   is($bt->invoice_amount     , '113.05000' , "$testname: bt invoice amount was assigned");
270
271 };
272
273 sub test_two_invoices {
274
275   my $testname = 'test_two_invoices';
276
277   my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv_1');
278   my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv_2');
279
280   my $bt = SL::Dev::Payment::create_bank_transaction(record        => $ar_transaction_1,
281                                                      amount        => ($ar_transaction_1->amount + $ar_transaction_2->amount),
282                                                      purpose       => "Rechnungen " . $ar_transaction_1->invnumber . " und " . $ar_transaction_2->invnumber,
283                                                      bank_chart_id => $bank->id,
284                                                     ) or die "Couldn't create bank_transaction";
285
286   my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($ar_transaction_1);
287   is($agreement, 16, "points for ar_transaction_1 in test_two_invoices ok");
288
289   $::form->{invoice_ids} = {
290     $bt->id => [ $ar_transaction_1->id, $ar_transaction_2->id ]
291   };
292
293   save_btcontroller_to_string();
294
295   $ar_transaction_1->load;
296   $ar_transaction_2->load;
297   $bt->load;
298
299   is($ar_transaction_1->paid   , '119.00000' , "$testname: salesinv_1 was paid");
300   is($ar_transaction_1->closed , 1           , "$testname: salesinv_1 is closed");
301   is($ar_transaction_2->paid   , '119.00000' , "$testname: salesinv_2 was paid");
302   is($ar_transaction_2->closed , 1           , "$testname: salesinv_2 is closed");
303   is($bt->invoice_amount       , '238.00000' , "$testname: bt invoice amount was assigned");
304
305 };
306
307 sub test_overpayment {
308
309   my $testname = 'test_overpayment';
310
311   $ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid');
312
313   # amount 135 > 119
314   my $bt = SL::Dev::Payment::create_bank_transaction(record        => $ar_transaction,
315                                                      bank_chart_id => $bank->id,
316                                                      amount        => 135
317                                                     ) or die "Couldn't create bank_transaction";
318
319   $::form->{invoice_ids} = {
320     $bt->id => [ $ar_transaction->id ]
321   };
322
323   save_btcontroller_to_string();
324
325   $ar_transaction->load;
326   $bt->load;
327
328   is($ar_transaction->paid                     , '135.00000' , "$testname: 'salesinv overpaid' was overpaid");
329   is($bt->invoice_amount                       , '135.00000' , "$testname: bt invoice amount was assigned overpaid amount");
330 { local $TODO = 'this currently fails because closed ignores over-payments, see commit d90966c7';
331   is($ar_transaction->closed                   , 0           , "$testname: 'salesinv overpaid' is open (via 'closed' method')");
332 }
333   is($ar_transaction->open_amount == 0 ? 1 : 0 , 0           , "$testname: 'salesinv overpaid is open (via amount-paid)");
334
335 };
336
337 sub test_overpayment_with_partialpayment {
338
339   # two payments on different days, 10 and 119. If there is only one invoice we want it be overpaid.
340   my $testname = 'test_overpayment_with_partialpayment';
341
342   $ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid partial');
343
344   my $bt_1 = SL::Dev::Payment::create_bank_transaction(record        => $ar_transaction,
345                                                        bank_chart_id => $bank->id,
346                                                        amount        =>  10
347                                                       ) or die "Couldn't create bank_transaction";
348   my $bt_2 = SL::Dev::Payment::create_bank_transaction(record        => $ar_transaction,
349                                                        amount        => 119,
350                                                        transdate     => DateTime->today->add(days => 5),
351                                                        bank_chart_id => $bank->id,
352                                                       ) or die "Couldn't create bank_transaction";
353
354   $::form->{invoice_ids} = {
355     $bt_1->id => [ $ar_transaction->id ]
356   };
357   save_btcontroller_to_string();
358
359   $::form->{invoice_ids} = {
360     $bt_2->id => [ $ar_transaction->id ]
361   };
362   save_btcontroller_to_string();
363
364   $ar_transaction->load;
365   $bt_1->load;
366   $bt_2->load;
367
368   is($ar_transaction->paid , '129.00000' , "$testname: 'salesinv overpaid partial' was overpaid");
369   is($bt_1->invoice_amount ,  '10.00000' , "$testname: bt_1 invoice amount was assigned overpaid amount");
370   is($bt_2->invoice_amount , '119.00000' , "$testname: bt_2 invoice amount was assigned overpaid amount");
371
372 };
373
374 sub test_partial_payment {
375
376   my $testname = 'test_partial_payment';
377
378   $ar_transaction = test_ar_transaction(invnumber => 'salesinv partial payment');
379
380   # amount 100 < 119
381   my $bt = SL::Dev::Payment::create_bank_transaction(record        => $ar_transaction,
382                                                      bank_chart_id => $bank->id,
383                                                      amount        => 100
384                                                     ) or die "Couldn't create bank_transaction";
385
386   $::form->{invoice_ids} = {
387     $bt->id => [ $ar_transaction->id ]
388   };
389
390   save_btcontroller_to_string();
391
392   $ar_transaction->load;
393   $bt->load;
394
395   is($ar_transaction->paid , '100.00000' , "$testname: 'salesinv partial payment' was partially paid");
396   is($bt->invoice_amount   , '100.00000' , "$testname: bt invoice amount was assigned partially paid amount");
397
398 };
399
400 sub test_credit_note {
401
402   my $testname = 'test_credit_note';
403
404   my $part1 = SL::Dev::Part::create_part(   partnumber => 'T4254')->save;
405   my $part2 = SL::Dev::Part::create_service(partnumber => 'Serv1')->save;
406   my $credit_note = SL::Dev::Record::create_credit_note(
407     invnumber    => 'cn 1',
408     customer     => $customer,
409     taxincluded  => 0,
410     invoiceitems => [ SL::Dev::Record::create_invoice_item(part => $part1, qty =>  3, sellprice => 70),
411                       SL::Dev::Record::create_invoice_item(part => $part2, qty => 10, sellprice => 50),
412                     ]
413   );
414   my $bt            = SL::Dev::Payment::create_bank_transaction(record        => $credit_note,
415                                                                 amount        => $credit_note->amount,
416                                                                 bank_chart_id => $bank->id,
417                                                                 transdate     => DateTime->today->add(days => 10),
418                                                                );
419   my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($credit_note);
420   is($agreement, 13, "points for credit note ok");
421   is($rule_matches, 'remote_account_number(3) exact_amount(4) wrong_sign(-1) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus14(2) ', "rules_matches for credit note ok");
422
423   $::form->{invoice_ids} = {
424     $bt->id => [ $credit_note->id ]
425   };
426
427   save_btcontroller_to_string();
428
429   $credit_note->load;
430   $bt->load;
431   is($credit_note->amount   , '-844.90000', "$testname: amount ok");
432   is($credit_note->netamount, '-710.00000', "$testname: netamount ok");
433   is($credit_note->paid     , '-844.90000', "$testname: paid ok");
434 }
435
436 sub test_neg_ap_transaction {
437   my (%params) = @_;
438   my $testname = 'test_neg_ap_transaction';
439   my $netamount = -20;
440   my $amount    = $::form->round_amount($netamount * 1.19,2);
441   my $invoice   = SL::DB::PurchaseInvoice->new(
442     invoice      => 0,
443     invnumber    => $params{invnumber} || 'test_neg_ap_transaction',
444     amount       => $amount,
445     netamount    => $netamount,
446     transdate    => $transdate1,
447     taxincluded  => 0,
448     vendor_id    => $vendor->id,
449     taxzone_id   => $vendor->taxzone_id,
450     currency_id  => $currency_id,
451     transactions => [],
452     notes        => 'test_neg_ap_transaction',
453   );
454   $invoice->add_ap_amount_row(
455     amount     => $invoice->netamount,
456     chart      => $ap_amount_chart,
457     tax_id     => $tax_9->id,
458   );
459
460   $invoice->create_ap_row(chart => $ap_chart);
461   $invoice->save;
462
463   is($invoice->netamount, -20  , "$testname: netamount ok");
464   is($invoice->amount   , -23.8, "$testname: amount ok");
465
466   my $bt            = SL::Dev::Payment::create_bank_transaction(record        => $invoice,
467                                                                 amount        => $invoice->amount,
468                                                                 bank_chart_id => $bank->id,
469                                                                 transdate     => DateTime->today->add(days => 10),
470                                                                );
471   my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($invoice);
472   is($agreement, 15, "points for negative ap transaction ok");
473
474   $::form->{invoice_ids} = {
475     $bt->id => [ $invoice->id ]
476   };
477
478   save_btcontroller_to_string();
479
480   $invoice->load;
481   $bt->load;
482
483   is($invoice->amount   , '-23.80000', "$testname: amount ok");
484   is($invoice->netamount, '-20.00000', "$testname: netamount ok");
485   is($invoice->paid     , '-23.80000', "$testname: paid ok");
486   is($bt->invoice_amount, '23.80000', "$testname: bt invoice amount for ap was assigned");
487
488   return $invoice;
489 };
490
491 sub test_ap_payment_transaction {
492   my (%params) = @_;
493   my $testname = 'test_ap_two_transaction';
494   my $netamount = 115;
495   my $amount    = $::form->round_amount($netamount * 1.19,2);
496   my $invoice   = SL::DB::PurchaseInvoice->new(
497     invoice      => 0,
498     invnumber    => $params{invnumber} || 'test_ap_two_transaction',
499     amount       => $amount,
500     netamount    => $netamount,
501     transdate    => $transdate1,
502     taxincluded  => 0,
503     vendor_id    => $vendor->id,
504     taxzone_id   => $vendor->taxzone_id,
505     currency_id  => $currency_id,
506     transactions => [],
507     notes        => 'test_ap_transaction',
508   );
509   $invoice->add_ap_amount_row(
510     amount     => $invoice->netamount,
511     chart      => $ap_amount_chart,
512     tax_id     => $tax_9->id,
513   );
514
515   $invoice->create_ap_row(chart => $ap_chart);
516   $invoice->save;
517
518   is($invoice->netamount, 115  , "$testname: netamount ok");
519   is($invoice->amount   , 136.85, "$testname: amount ok");
520
521   my $bt            = SL::Dev::Payment::create_bank_transaction(record        => $invoice,
522                                                                 amount        => $invoice->amount,
523                                                                 bank_chart_id => $bank->id,
524                                                                 transdate     => DateTime->today->add(days => 10),
525                                                                );
526   $::form->{invoice_ids} = {
527     $bt->id => [ $invoice->id ]
528   };
529
530   save_btcontroller_to_string();
531
532   $invoice->load;
533   $bt->load;
534
535   is($invoice->amount   , '136.85000', "$testname: amount ok");
536   is($invoice->netamount, '115.00000', "$testname: netamount ok");
537   is($bt->amount, '-136.85000', "$testname: bt amount ok");
538   is($invoice->paid     , '136.85000', "$testname: paid ok");
539   is($bt->invoice_amount, '-136.85000', "$testname: bt invoice amount for ap was assigned");
540
541   return $invoice;
542 };
543 1;