bank_transactions.t - Punktetest für test_two_invoices
[kivitendo-erp.git] / t / bank / bank_transactions.t
1 use Test::More;
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
50 # starting test:
51 Support::TestSetup::login();
52
53 reset_state(); # initialise customers/vendors/bank/currency/...
54
55 test1();
56 test_overpayment_with_partialpayment();
57 test_overpayment();
58 test_skonto_exact();
59 test_two_invoices();
60 test_partial_payment();
61 test_credit_note();
62
63 # remove all created data at end of test
64 clear_up();
65
66 done_testing();
67
68 ###### functions for setting up data
69
70 sub reset_state {
71   my %params = @_;
72
73   $params{$_} ||= {} for qw(unit customer tax vendor);
74
75   clear_up();
76
77   $transdate1 = DateTime->today;
78   $transdate2 = DateTime->today->add(days => 5);
79
80   $tax             = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} }) || croak "No tax";
81   $tax7            = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07)                    || croak "No tax for 7\%";
82   $tax_9           = SL::DB::Manager::Tax->find_by(taxkey => 9, rate => 0.19, %{ $params{tax} }) || croak "No tax";
83
84   $currency_id     = $::instance_conf->get_currency_id;
85
86   $bank_account     =  SL::DB::BankAccount->new(
87     account_number  => '123',
88     bank_code       => '123',
89     iban            => '123',
90     bic             => '123',
91     bank            => '123',
92     chart_id        => SL::DB::Manager::Chart->find_by(description => 'Bank')->id,
93     name            => SL::DB::Manager::Chart->find_by(description => 'Bank')->description,
94   )->save;
95
96   $customer = SL::Dev::CustomerVendor::create_customer(
97     name                      => 'Test Customer',
98     iban                      => 'DE12500105170648489890',
99     bic                       => 'TESTBIC',
100     account_number            => '648489890',
101     mandate_date_of_signature => $transdate1,
102     mandator_id               => 'foobar',
103     bank                      => 'Geizkasse',
104     bank_code                 => 'G1235',
105     depositor                 => 'Test Customer',
106   )->save;
107
108   $payment_terms = SL::Dev::Payment::create_payment_terms;
109
110   $vendor = SL::Dev::CustomerVendor::create_vendor(
111     name           => 'Test Vendor',
112     payment_id     => $payment_terms->id,
113     iban           => 'DE12500105170648489890',
114     bic            => 'TESTBIC',
115     account_number => '648489890',
116     bank           => 'Geizkasse',
117     bank_code      => 'G1235',
118     depositor      => 'Test Vendor',
119   )->save;
120
121   $ar_chart        = SL::DB::Manager::Chart->find_by( accno => '1400' ); # Forderungen
122   $ap_chart        = SL::DB::Manager::Chart->find_by( accno => '1600' ); # Verbindlichkeiten
123   $bank            = SL::DB::Manager::Chart->find_by( accno => '1200' ); # Bank
124   $ar_amount_chart = SL::DB::Manager::Chart->find_by( accno => '8400' ); # Erlöse
125   $ap_amount_chart = SL::DB::Manager::Chart->find_by( accno => '3400' ); # Wareneingang 19%
126
127 }
128
129 sub test_ar_transaction {
130   my (%params) = @_;
131   my $netamount = 100;
132   my $amount    = $params{amount} || $::form->round_amount(100 * 1.19,2);
133   my $invoice   = SL::DB::Invoice->new(
134       invoice      => 0,
135       invnumber    => $params{invnumber} || undef, # let it use its own invnumber
136       amount       => $amount,
137       netamount    => $netamount,
138       transdate    => $transdate1,
139       taxincluded  => 0,
140       customer_id  => $customer->id,
141       taxzone_id   => $customer->taxzone_id,
142       currency_id  => $currency_id,
143       transactions => [],
144       payment_id   => $params{payment_id} || undef,
145       notes        => 'test_ar_transaction',
146   );
147   $invoice->add_ar_amount_row(
148     amount => $invoice->netamount,
149     chart  => $ar_amount_chart,
150     tax_id => $tax->id,
151   );
152
153   $invoice->create_ar_row(chart => $ar_chart);
154   $invoice->save;
155
156   is($invoice->currency_id , $currency_id , 'currency_id has been saved');
157   is($invoice->netamount   , 100          , 'ar amount has been converted');
158   is($invoice->amount      , 119          , 'ar amount has been converted');
159   is($invoice->taxincluded , 0            , 'ar transaction doesn\'t have taxincluded');
160
161   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');
162   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');
163
164   return $invoice;
165 };
166
167 sub test_ap_transaction {
168   my (%params) = @_;
169   my $netamount = 100;
170   my $amount    = $::form->round_amount($netamount * 1.19,2);
171   my $invoice   = SL::DB::PurchaseInvoice->new(
172       invoice      => 0,
173       invnumber    => $params{invnumber} || 'test_ap_transaction',
174       amount       => $amount,
175       netamount    => $netamount,
176       transdate    => $transdate1,
177       taxincluded  => 0,
178       vendor_id    => $vendor->id,
179       taxzone_id   => $vendor->taxzone_id,
180       currency_id  => $currency_id,
181       transactions => [],
182       notes        => 'test_ap_transaction',
183   );
184   $invoice->add_ap_amount_row(
185     amount     => $invoice->netamount,
186     chart      => $ap_amount_chart,
187     tax_id     => $tax_9->id,
188   );
189
190   $invoice->create_ap_row(chart => $ap_chart);
191   $invoice->save;
192
193   is($invoice->currency_id , $currency_id , 'currency_id has been saved');
194   is($invoice->netamount   , 100          , 'ap amount has been converted');
195   is($invoice->amount      , 119          , 'ap amount has been converted');
196   is($invoice->taxincluded , 0            , 'ap transaction doesn\'t have taxincluded');
197
198   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');
199   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');
200
201   return $invoice;
202 };
203
204 ###### test cases
205
206 sub test1 {
207
208   my $testname = 'test1';
209
210   $ar_transaction = test_ar_transaction(invnumber => 'salesinv1');
211
212   my $bt = SL::Dev::Payment::create_bank_transaction(record => $ar_transaction) or die "Couldn't create bank_transaction";
213
214   $::form->{invoice_ids} = {
215           $bt->id => [ $ar_transaction->id ]
216         };
217
218   my $bt_controller = SL::Controller::BankTransaction->new;
219   $bt_controller->action_save_invoices;
220
221   $ar_transaction->load;
222   $bt->load;
223   is($ar_transaction->paid   , '119.00000' , "$testname: salesinv1 was paid");
224   is($ar_transaction->closed , 1           , "$testname: salesinv1 is closed");
225   is($bt->invoice_amount     , '119.00000' , "$testname: bt invoice amount was assigned");
226
227 };
228
229 sub test_skonto_exact {
230
231   my $testname = 'test_skonto_exact';
232
233   $ar_transaction = test_ar_transaction(invnumber => 'salesinv skonto',
234                                         payment_id => $payment_terms->id,
235                                        );
236
237   my $bt = SL::Dev::Payment::create_bank_transaction(record        => $ar_transaction,
238                                                      bank_chart_id => $bank->id,
239                                                      amount        => $ar_transaction->amount_less_skonto
240                                                     ) or die "Couldn't create bank_transaction";
241
242   $::form->{invoice_ids} = {
243           $bt->id => [ $ar_transaction->id ]
244         };
245   $::form->{invoice_skontos} = {
246           $bt->id => [ 'with_skonto_pt' ]
247         };
248
249   my $bt_controller = SL::Controller::BankTransaction->new;
250   $bt_controller->action_save_invoices;
251
252   $ar_transaction->load;
253   $bt->load;
254   is($ar_transaction->paid   , '119.00000' , "$testname: salesinv skonto was paid");
255   is($ar_transaction->closed , 1           , "$testname: salesinv skonto is closed");
256   is($bt->invoice_amount     , '113.05000' , "$testname: bt invoice amount was assigned");
257
258 };
259
260 sub test_two_invoices {
261
262   my $testname = 'test_two_invoices';
263
264   my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv_1');
265   my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv_2');
266
267   my $bt = SL::Dev::Payment::create_bank_transaction(record        => $ar_transaction_1,
268                                                      amount        => ($ar_transaction_1->amount + $ar_transaction_2->amount),
269                                                      purpose       => "Rechnungen " . $ar_transaction_1->invnumber . " und " . $ar_transaction_2->invnumber,
270                                                      bank_chart_id => $bank->id,
271                                                     ) or die "Couldn't create bank_transaction";
272
273   my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($ar_transaction_1);
274   is($agreement, 16, "points for ar_transaction_1 in test_two_invoices ok");
275
276   $::form->{invoice_ids} = {
277           $bt->id => [ $ar_transaction_1->id, $ar_transaction_2->id ]
278         };
279
280   my $bt_controller = SL::Controller::BankTransaction->new;
281   $bt_controller->action_save_invoices;
282
283   $ar_transaction_1->load;
284   $ar_transaction_2->load;
285   $bt->load;
286
287   is($ar_transaction_1->paid   , '119.00000' , "$testname: salesinv_1 was paid");
288   is($ar_transaction_1->closed , 1           , "$testname: salesinv_1 is closed");
289   is($ar_transaction_2->paid   , '119.00000' , "$testname: salesinv_2 was paid");
290   is($ar_transaction_2->closed , 1           , "$testname: salesinv_2 is closed");
291   is($bt->invoice_amount       , '238.00000' , "$testname: bt invoice amount was assigned");
292
293 };
294
295 sub test_overpayment {
296
297   my $testname = 'test_overpayment';
298
299   $ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid');
300
301   # amount 135 > 119
302   my $bt = SL::Dev::Payment::create_bank_transaction(record        => $ar_transaction,
303                                                      bank_chart_id => $bank->id,
304                                                      amount        => 135
305                                                     ) or die "Couldn't create bank_transaction";
306
307   $::form->{invoice_ids} = {
308           $bt->id => [ $ar_transaction->id ]
309         };
310
311   my $bt_controller = SL::Controller::BankTransaction->new;
312   $bt_controller->action_save_invoices;
313
314   $ar_transaction->load;
315   $bt->load;
316
317   is($ar_transaction->paid                     , '135.00000' , "$testname: 'salesinv overpaid' was overpaid");
318   is($bt->invoice_amount                       , '135.00000' , "$testname: bt invoice amount was assigned overpaid amount");
319 { local $TODO = 'this currently fails because closed ignores over-payments, see commit d90966c7';
320   is($ar_transaction->closed                   , 0           , "$testname: 'salesinv overpaid' is open (via 'closed' method')");
321 }
322   is($ar_transaction->open_amount == 0 ? 1 : 0 , 0           , "$testname: 'salesinv overpaid is open (via amount-paid)");
323
324 };
325
326 sub test_overpayment_with_partialpayment {
327
328   # two payments on different days, 10 and 119. If there is only one invoice we want it be overpaid.
329   my $testname = 'test_overpayment_with_partialpayment';
330
331   $ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid partial');
332
333   my $bt_1 = SL::Dev::Payment::create_bank_transaction(record        => $ar_transaction,
334                                                        bank_chart_id => $bank->id,
335                                                        amount        =>  10
336                                                       ) or die "Couldn't create bank_transaction";
337   my $bt_2 = SL::Dev::Payment::create_bank_transaction(record        => $ar_transaction,
338                                                        amount        => 119,
339                                                        transdate     => DateTime->today->add(days => 5),
340                                                        bank_chart_id => $bank->id,
341                                                       ) or die "Couldn't create bank_transaction";
342
343   $::form->{invoice_ids} = {
344           $bt_1->id => [ $ar_transaction->id ]
345         };
346   my $bt_controller_1 = SL::Controller::BankTransaction->new;
347   $bt_controller_1->action_save_invoices;
348
349   $::form->{invoice_ids} = {
350           $bt_2->id => [ $ar_transaction->id ]
351         };
352   my $bt_controller_2 = SL::Controller::BankTransaction->new;
353   $bt_controller_2->action_save_invoices;
354
355   $ar_transaction->load;
356   $bt_1->load;
357   $bt_2->load;
358
359   is($ar_transaction->paid , '129.00000' , "$testname: 'salesinv overpaid partial' was overpaid");
360   is($bt_1->invoice_amount ,  '10.00000' , "$testname: bt_1 invoice amount was assigned overpaid amount");
361   is($bt_2->invoice_amount , '119.00000' , "$testname: bt_2 invoice amount was assigned overpaid amount");
362
363 };
364
365 sub test_partial_payment {
366
367   my $testname = 'test_partial_payment';
368
369   $ar_transaction = test_ar_transaction(invnumber => 'salesinv partial payment');
370
371   # amount 100 < 119
372   my $bt = SL::Dev::Payment::create_bank_transaction(record        => $ar_transaction,
373                                                      bank_chart_id => $bank->id,
374                                                      amount        => 100
375                                                     ) or die "Couldn't create bank_transaction";
376
377   $::form->{invoice_ids} = {
378           $bt->id => [ $ar_transaction->id ]
379         };
380
381   my $bt_controller = SL::Controller::BankTransaction->new;
382   $bt_controller->action_save_invoices;
383
384   $ar_transaction->load;
385   $bt->load;
386
387   is($ar_transaction->paid , '100.00000' , "$testname: 'salesinv partial payment' was partially paid");
388   is($bt->invoice_amount   , '100.00000' , "$testname: bt invoice amount was assigned partially paid amount");
389
390 };
391
392 sub test_credit_note {
393
394   my $testname = 'test_credit_note';
395
396   my $part1 = SL::Dev::Part::create_part(   partnumber => 'T4254')->save;
397   my $part2 = SL::Dev::Part::create_service(partnumber => 'Serv1')->save;
398   my $credit_note = SL::Dev::Record::create_credit_note(
399     invnumber    => 'cn 1',
400     customer     => $customer,
401     taxincluded  => 0,
402     invoiceitems => [ SL::Dev::Record::create_invoice_item(part => $part1, qty =>  3, sellprice => 70),
403                       SL::Dev::Record::create_invoice_item(part => $part2, qty => 10, sellprice => 50),
404                     ]
405   );
406   my $bt            = SL::Dev::Payment::create_bank_transaction(record        => $credit_note,
407                                                                 amount        => $credit_note->amount,
408                                                                 bank_chart_id => $bank->id,
409                                                                 transdate     => DateTime->today->add(days => 10),
410                                                                );
411   my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($credit_note);
412   is($agreement, 13, "points for credit note ok");
413   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");
414
415   $::form->{invoice_ids} = {
416           $bt->id => [ $credit_note->id ]
417   };
418
419   my $bt_controller = SL::Controller::BankTransaction->new;
420   $bt_controller->action_save_invoices;
421
422   $credit_note->load;
423   $bt->load;
424   is($credit_note->amount   , '-844.90000', "$testname: amount ok");
425   is($credit_note->netamount, '-710.00000', "$testname: netamount ok");
426   is($credit_note->paid     , '-844.90000', "$testname: paid ok");
427 }
428
429 1;