Ware/Erzeugnis/Dienstleistung per parts.part_type unterscheiden 2
[kivitendo-erp.git] / t / db_helper / payment.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::Exchangerate;
16 use SL::DB::Customer;
17 use SL::DB::Vendor;
18 use SL::DB::Employee;
19 use SL::DB::Invoice;
20 use SL::DB::Part;
21 use SL::DB::Unit;
22 use SL::DB::TaxZone;
23 use SL::DB::BankAccount;
24 use SL::DB::PaymentTerm;
25 use SL::DBUtils qw(selectfirst_array_query);
26 use Data::Dumper;
27
28 my ($customer, $vendor, $currency_id, @parts, $buchungsgruppe, $buchungsgruppe7, $unit, $employee, $tax, $tax7, $tax_9, $taxzone, $payment_terms, $bank_account);
29 my ($transdate1, $transdate2, $transdate3, $transdate4, $currency, $exchangerate, $exchangerate2, $exchangerate3, $exchangerate4);
30 my ($ar_chart,$bank,$ar_amount_chart, $ap_chart, $ap_amount_chart, $fxloss_chart, $fxgain_chart);
31
32 my $ALWAYS_RESET = 1;
33
34 my $reset_state_counter = 0;
35
36 my $purchase_invoice_counter = 0; # used for generating purchase invnumber
37
38 sub clear_up {
39   SL::DB::Manager::InvoiceItem->delete_all(all => 1);
40   SL::DB::Manager::Invoice->delete_all(all => 1);
41   SL::DB::Manager::PurchaseInvoice->delete_all(all => 1);
42   SL::DB::Manager::Part->delete_all(all => 1);
43   SL::DB::Manager::Customer->delete_all(all => 1);
44   SL::DB::Manager::Vendor->delete_all(all => 1);
45   SL::DB::Manager::BankAccount->delete_all(all => 1);
46   SL::DB::Manager::PaymentTerm->delete_all(all => 1);
47   SL::DB::Manager::Exchangerate->delete_all(all => 1);
48   SL::DB::Manager::Currency->delete_all(where => [ name => 'CUR' ]);
49 };
50
51 sub reset_state {
52   my %params = @_;
53
54   return if $reset_state_counter;
55
56   $params{$_} ||= {} for qw(buchungsgruppe unit customer part tax vendor);
57
58   clear_up();
59
60   $transdate1 = DateTime->today;
61   $transdate2 = DateTime->today->add(days => 1);
62   $transdate3 = DateTime->today->add(days => 2);
63   $transdate4 = DateTime->today->add(days => 3);
64
65   $buchungsgruppe  = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 19%', %{ $params{buchungsgruppe} }) || croak "No accounting group";
66   $buchungsgruppe7 = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 7%')                                || croak "No accounting group for 7\%";
67   $unit            = SL::DB::Manager::Unit->find_by(name => 'kg', %{ $params{unit} })                                      || croak "No unit";
68   $employee        = SL::DB::Manager::Employee->current                                                                    || croak "No employee";
69   $tax             = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} })                           || croak "No tax";
70   $tax7            = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07)                                              || croak "No tax for 7\%";
71   $taxzone         = SL::DB::Manager::TaxZone->find_by( description => 'Inland')                                           || croak "No taxzone";
72   $tax_9           = SL::DB::Manager::Tax->find_by(taxkey => 9, rate => 0.19, %{ $params{tax} })                           || croak "No tax";
73   # $tax7            = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07)                                              || croak "No tax for 7\%";
74
75   $currency_id     = $::instance_conf->get_currency_id;
76
77   $currency = SL::DB::Currency->new(name => 'CUR')->save;
78
79   $fxgain_chart = SL::DB::Manager::Chart->find_by(accno => '2660') or die "Can't find fxgain_chart in test";
80   $fxloss_chart = SL::DB::Manager::Chart->find_by(accno => '2150') or die "Can't find fxloss_chart in test";
81
82   $currency->db->dbh->do('UPDATE defaults SET fxgain_accno_id = ' . $fxgain_chart->id);
83   $currency->db->dbh->do('UPDATE defaults SET fxloss_accno_id = ' . $fxloss_chart->id);
84   $::instance_conf->reload->data;
85   is($fxgain_chart->id,  $::instance_conf->get_fxgain_accno_id, "fxgain_chart was updated in defaults");
86   is($fxloss_chart->id,  $::instance_conf->get_fxloss_accno_id, "fxloss_chart was updated in defaults");
87
88   $exchangerate  = SL::DB::Exchangerate->new(transdate   => $transdate1,
89                                              buy         => '1.33333',
90                                              sell        => '1.33333',
91                                              currency_id => $currency->id,
92                                             )->save;
93   $exchangerate2 = SL::DB::Exchangerate->new(transdate   => $transdate2,
94                                              buy         => '0.8',
95                                              sell        => '0.8',
96                                              currency_id => $currency->id,
97                                             )->save;
98   $exchangerate3 = SL::DB::Exchangerate->new(transdate   => $transdate3,
99                                              buy         => '1.55557',
100                                              sell        => '1.55557',
101                                              currency_id => $currency->id,
102                                             )->save;
103   $exchangerate4 = SL::DB::Exchangerate->new(transdate   => $transdate4,
104                                              buy         => '0.77777',
105                                              sell        => '0.77777',
106                                              currency_id => $currency->id,
107                                             )->save;
108
109   $customer     = SL::DB::Customer->new(
110     name        => 'Test Customer',
111     currency_id => $currency_id,
112     taxzone_id  => $taxzone->id,
113     %{ $params{customer} }
114   )->save;
115
116   $bank_account     =  SL::DB::BankAccount->new(
117     account_number  => '123',
118     bank_code       => '123',
119     iban            => '123',
120     bic             => '123',
121     bank            => '123',
122     chart_id        => SL::DB::Manager::Chart->find_by( description => 'Bank' )->id,
123     name            => SL::DB::Manager::Chart->find_by( description => 'Bank' )->description,
124   )->save;
125
126   $payment_terms     =  SL::DB::PaymentTerm->new(
127     description      => 'payment',
128     description_long => 'payment',
129     terms_netto      => '30',
130     terms_skonto     => '5',
131     percent_skonto   => '0.05',
132     auto_calculation => 1,
133   )->save;
134
135   $vendor       = SL::DB::Vendor->new(
136     name        => 'Test Vendor',
137     currency_id => $currency_id,
138     taxzone_id  => $taxzone->id,
139     payment_id  => $payment_terms->id,
140     %{ $params{vendor} }
141   )->save;
142
143
144   @parts = ();
145   push @parts, SL::DB::Part->new(
146     partnumber         => 'T4254',
147     description        => 'Fourty-two fifty-four',
148     lastcost           => 1.93,
149     sellprice          => 2.34,
150     part_type          => 'part',
151     buchungsgruppen_id => $buchungsgruppe->id,
152     unit               => $unit->name,
153     %{ $params{part1} }
154   )->save;
155
156   push @parts, SL::DB::Part->new(
157     partnumber         => 'T0815',
158     description        => 'Zero EIGHT fifteeN @ 7%',
159     lastcost           => 5.473,
160     sellprice          => 9.714,
161     part_type          => 'part',
162     buchungsgruppen_id => $buchungsgruppe7->id,
163     unit               => $unit->name,
164     %{ $params{part2} }
165   )->save;
166   push @parts, SL::DB::Part->new(
167     partnumber         => '19%',
168     description        => 'Testware 19%',
169     lastcost           => 0,
170     sellprice          => 50,
171     part_type          => 'part',
172     buchungsgruppen_id => $buchungsgruppe->id,
173     unit               => $unit->name,
174     %{ $params{part3} }
175   )->save;
176   push @parts, SL::DB::Part->new(
177     partnumber         => '7%',
178     description        => 'Testware 7%',
179     lastcost           => 0,
180     sellprice          => 50,
181     part_type          => 'part',
182     buchungsgruppen_id => $buchungsgruppe7->id,
183     unit               => $unit->name,
184     %{ $params{part4} }
185   )->save;
186
187   $ar_chart        = SL::DB::Manager::Chart->find_by( accno => '1400' ); # Forderungen
188   $ap_chart        = SL::DB::Manager::Chart->find_by( accno => '1600' ); # Verbindlichkeiten
189   $bank            = SL::DB::Manager::Chart->find_by( accno => '1200' ); # Bank
190   $ar_amount_chart = SL::DB::Manager::Chart->find_by( accno => '8400' ); # Erlöse
191   $ap_amount_chart = SL::DB::Manager::Chart->find_by( accno => '3400' ); # Wareneingang 19%
192
193   $reset_state_counter++;
194 }
195
196 sub new_invoice {
197   my %params  = @_;
198
199   return SL::DB::Invoice->new(
200     customer_id => $customer->id,
201     currency_id => $currency_id,
202     employee_id => $employee->id,
203     salesman_id => $employee->id,
204     gldate      => $transdate1,
205     taxzone_id  => $taxzone->id,
206     transdate   => $transdate1,
207     invoice     => 1,
208     type        => 'invoice',
209     %params,
210   );
211
212 }
213
214 sub new_purchase_invoice {
215   # my %params  = @_;
216   # manually create a Kreditorenbuchung from scratch, ap + acc_trans bookings, as no helper exists yet, like $invoice->post.
217   # arap-Booking must come last in the acc_trans order
218   $purchase_invoice_counter++;
219
220   my $purchase_invoice = SL::DB::PurchaseInvoice->new(
221     vendor_id   => $vendor->id,
222     invnumber   => 'newap ' . $purchase_invoice_counter ,
223     currency_id => $currency_id,
224     employee_id => $employee->id,
225     gldate      => $transdate1,
226     taxzone_id  => $taxzone->id,
227     transdate   => $transdate1,
228     invoice     => 0,
229     type        => 'invoice',
230     taxincluded => 0,
231     amount      => '226',
232     netamount   => '200',
233     paid        => '0',
234     # %params,
235   )->save;
236
237   my $expense_chart  = SL::DB::Manager::Chart->find_by(accno => '3400');
238   my $expense_chart_booking= SL::DB::AccTransaction->new(
239                                         trans_id   => $purchase_invoice->id,
240                                         chart_id   => $expense_chart->id,
241                                         chart_link => $expense_chart->link,
242                                         amount     => '-100',
243                                         transdate  => $transdate1,
244                                         source     => '',
245                                         taxkey     => 9,
246                                         tax_id     => SL::DB::Manager::Tax->find_by(taxkey => 9)->id);
247   $expense_chart_booking->save;
248
249   my $tax_chart  = SL::DB::Manager::Chart->find_by(accno => '1576');
250   my $tax_chart_booking= SL::DB::AccTransaction->new(
251                                         trans_id   => $purchase_invoice->id,
252                                         chart_id   => $tax_chart->id,
253                                         chart_link => $tax_chart->link,
254                                         amount     => '-19',
255                                         transdate  => $transdate1,
256                                         source     => '',
257                                         taxkey     => 0,
258                                         tax_id     => SL::DB::Manager::Tax->find_by(taxkey => 9)->id);
259   $tax_chart_booking->save;
260   $expense_chart  = SL::DB::Manager::Chart->find_by(accno => '3300');
261   $expense_chart_booking= SL::DB::AccTransaction->new(
262                                         trans_id   => $purchase_invoice->id,
263                                         chart_id   => $expense_chart->id,
264                                         chart_link => $expense_chart->link,
265                                         amount     => '-100',
266                                         transdate  => $transdate1,
267                                         source     => '',
268                                         taxkey     => 8,
269                                         tax_id     => SL::DB::Manager::Tax->find_by(taxkey => 8)->id);
270   $expense_chart_booking->save;
271
272
273   $tax_chart  = SL::DB::Manager::Chart->find_by(accno => '1571');
274   $tax_chart_booking= SL::DB::AccTransaction->new(
275                                          trans_id   => $purchase_invoice->id,
276                                          chart_id   => $tax_chart->id,
277                                          chart_link => $tax_chart->link,
278                                          amount     => '-7',
279                                          transdate  => $transdate1,
280                                          source     => '',
281                                          taxkey     => 0,
282                                          tax_id     => SL::DB::Manager::Tax->find_by(taxkey => 8)->id);
283   $tax_chart_booking->save;
284   my $arap_chart  = SL::DB::Manager::Chart->find_by(accno => '1600');
285   my $arap_booking= SL::DB::AccTransaction->new(trans_id   => $purchase_invoice->id,
286                                                 chart_id   => $arap_chart->id,
287                                                 chart_link => $arap_chart->link,
288                                                 amount     => '226',
289                                                 transdate  => $transdate1,
290                                                 source     => '',
291                                                 taxkey     => 0,
292                                                 tax_id     => SL::DB::Manager::Tax->find_by(taxkey => 0)->id);
293   $arap_booking->save;
294
295   return $purchase_invoice;
296 }
297
298 sub new_item {
299   my (%params) = @_;
300
301   my $part = delete($params{part}) || $parts[0];
302
303   return SL::DB::InvoiceItem->new(
304     parts_id    => $part->id,
305     lastcost    => $part->lastcost,
306     sellprice   => $part->sellprice,
307     description => $part->description,
308     unit        => $part->unit,
309     %params,
310   );
311 }
312
313 sub number_of_payments {
314   my $invoice = shift;
315
316   my $number_of_payments;
317   my $paid_amount;
318   foreach my $transaction ( @{ $invoice->transactions } ) {
319     if ( $transaction->chart_link =~ /(AR_paid|AP_paid)/ ) {
320       $paid_amount += $transaction->amount ;
321       $number_of_payments++;
322     };
323   };
324   return ($number_of_payments, $paid_amount);
325 };
326
327 sub total_amount {
328   my $invoice = shift;
329
330   my $total = sum map { $_->amount } @{ $invoice->transactions };
331
332   return $::form->round_amount($total, 5);
333
334 };
335
336
337 # test 1
338 sub test_default_invoice_one_item_19_without_skonto() {
339   reset_state() if $ALWAYS_RESET;
340
341   my $item    = new_item(qty => 2.5);
342   my $invoice = new_invoice(
343     taxincluded  => 0,
344     invoiceitems => [ $item ],
345     payment_id   => $payment_terms->id,
346   );
347   $invoice->post;
348
349   my $purchase_invoice = new_purchase_invoice();
350
351
352   # default values
353   my %params = ( chart_id => $bank_account->chart_id,
354                  transdate => DateTime->today_local->to_kivitendo
355                );
356
357   $params{amount} = '6.96';
358   $params{payment_type} = 'without_skonto';
359
360   $invoice->pay_invoice( %params );
361
362   my ($number_of_payments, $paid_amount) = number_of_payments($invoice);
363   my $total = total_amount($invoice);
364
365   my $title = 'default invoice, one item, 19% tax, without_skonto';
366
367   is($invoice->netamount,   5.85,      "${title}: netamount");
368   is($invoice->amount,      6.96,      "${title}: amount");
369   is($paid_amount,         -6.96,      "${title}: paid amount");
370   is($number_of_payments,      1,      "${title}: 1 AR_paid booking");
371   is($invoice->paid,        6.96,      "${title}: paid");
372   is($total,                   0,      "${title}: even balance");
373
374 }
375
376 sub test_default_invoice_one_item_19_without_skonto_overpaid() {
377   reset_state() if $ALWAYS_RESET;
378
379   my $item    = new_item(qty => 2.5);
380   my $invoice = new_invoice(
381     taxincluded  => 0,
382     invoiceitems => [ $item ],
383     payment_id   => $payment_terms->id,
384   );
385   $invoice->post;
386
387   my $purchase_invoice = new_purchase_invoice();
388
389
390   # default values
391   my %params = ( chart_id => $bank_account->chart_id,
392                  transdate => DateTime->today_local->to_kivitendo
393                );
394
395   $params{amount} = '16.96';
396   $params{payment_type} = 'without_skonto';
397   $invoice->pay_invoice( %params );
398
399   $params{amount} = '-10.00';
400   $invoice->pay_invoice( %params );
401
402   my ($number_of_payments, $paid_amount) = number_of_payments($invoice);
403   my $total = total_amount($invoice);
404
405   my $title = 'default invoice, one item, 19% tax, without_skonto';
406
407   is($invoice->netamount,   5.85,      "${title}: netamount");
408   is($invoice->amount,      6.96,      "${title}: amount");
409   is($paid_amount,         -6.96,      "${title}: paid amount");
410   is($number_of_payments,      2,      "${title}: 1 AR_paid booking");
411   is($invoice->paid,        6.96,      "${title}: paid");
412   is($total,                   0,      "${title}: even balance");
413
414 }
415
416
417 # test 2
418 sub test_default_invoice_two_items_19_7_tax_with_skonto() {
419   reset_state() if $ALWAYS_RESET;
420
421   my $item1   = new_item(qty => 2.5);
422   my $item2   = new_item(qty => 1.2, part => $parts[1]);
423   my $invoice = new_invoice(
424     taxincluded  => 0,
425     invoiceitems => [ $item1, $item2 ],
426     payment_id  => $payment_terms->id,
427   );
428   $invoice->post;
429
430   # default values
431   my %params = ( chart_id => $bank_account->chart_id,
432                  transdate => DateTime->today_local->to_kivitendo
433                );
434
435   $params{payment_type} = 'with_skonto_pt';
436   $params{amount}       = $invoice->amount_less_skonto;
437
438   $invoice->pay_invoice( %params );
439
440   my ($number_of_payments, $paid_amount) = number_of_payments($invoice);
441   my $total = total_amount($invoice);
442
443   my $title = 'default invoice, two items, 19/7% tax with_skonto_pt';
444
445   is($invoice->netamount,  5.85 + 11.66,   "${title}: netamount");
446   is($invoice->amount,     6.96 + 12.48,   "${title}: amount");
447   is($paid_amount,               -19.44,   "${title}: paid amount");
448   is($invoice->paid,              19.44,   "${title}: paid");
449   is($number_of_payments,             3,   "${title}: 3 AR_paid bookings");
450   is($total,                          0,   "${title}: even balance");
451 }
452
453 sub test_default_invoice_two_items_19_7_tax_with_skonto_tax_included() {
454   reset_state() if $ALWAYS_RESET;
455
456   my $item1   = new_item(qty => 2.5);
457   my $item2   = new_item(qty => 1.2, part => $parts[1]);
458   my $invoice = new_invoice(
459     taxincluded  => 1,
460     invoiceitems => [ $item1, $item2 ],
461     payment_id  => $payment_terms->id,
462   );
463   $invoice->post;
464
465   # default values
466   my %params = ( chart_id => $bank_account->chart_id,
467                  transdate => DateTime->today_local->to_kivitendo
468                );
469
470   $params{payment_type} = 'with_skonto_pt';
471   $params{amount}       = $invoice->amount_less_skonto;
472
473   $invoice->pay_invoice( %params );
474
475   my ($number_of_payments, $paid_amount) = number_of_payments($invoice);
476   my $total = total_amount($invoice);
477
478   my $title = 'default invoice, two items, 19/7% tax with_skonto_pt';
479
480   is($invoice->netamount,         15.82,   "${title}: netamount");
481   is($invoice->amount,            17.51,   "${title}: amount");
482   is($paid_amount,               -17.51,   "${title}: paid amount");
483   is($invoice->paid,              17.51,   "${title}: paid");
484   is($number_of_payments,             3,   "${title}: 3 AR_paid bookings");
485   { local $TODO = "currently this test fails because the code writing the invoice is buggy, the calculation of skonto is correct";
486   is($total,                          0,   "${title}: even balance");
487   }
488 }
489
490 # test 3 : two items, without skonto
491 sub test_default_invoice_two_items_19_7_without_skonto() {
492   reset_state() if $ALWAYS_RESET;
493
494   my $item1   = new_item(qty => 2.5);
495   my $item2   = new_item(qty => 1.2, part => $parts[1]);
496   my $invoice = new_invoice(
497     taxincluded  => 0,
498     invoiceitems => [ $item1, $item2 ],
499     payment_id  => $payment_terms->id,
500   );
501   $invoice->post;
502
503   # default values
504   my %params = ( chart_id => $bank_account->chart_id,
505                  transdate => DateTime->today_local->to_kivitendo
506                );
507
508   $params{amount} = '19.44'; # pass full amount
509   $params{payment_type} = 'without_skonto';
510
511   $invoice->pay_invoice( %params );
512
513   my ($number_of_payments, $paid_amount) = number_of_payments($invoice);
514   my $total = total_amount($invoice);
515
516   my $title = 'default invoice, two items, 19/7% tax without skonto';
517
518   is($invoice->netamount,     5.85 + 11.66,     "${title}: netamount");
519   is($invoice->amount,        6.96 + 12.48,     "${title}: amount");
520   is($paid_amount,                  -19.44,     "${title}: paid amount");
521   is($invoice->paid,                 19.44,     "${title}: paid");
522   is($number_of_payments,                1,     "${title}: 1 AR_paid bookings");
523   is($total,                             0,     "${title}: even balance");
524 }
525
526 # test 4
527 sub test_default_invoice_two_items_19_7_without_skonto_incomplete_payment() {
528   reset_state() if $ALWAYS_RESET;
529
530   my $item1   = new_item(qty => 2.5);
531   my $item2   = new_item(qty => 1.2, part => $parts[1]);
532   my $invoice = new_invoice(
533     taxincluded  => 0,
534     invoiceitems => [ $item1, $item2 ],
535     payment_id  => $payment_terms->id,
536   );
537   $invoice->post;
538
539   $invoice->pay_invoice( amount       => '9.44',
540                          payment_type => 'without_skonto',
541                          chart_id     => $bank_account->chart_id,
542                          transdate    => DateTime->today_local->to_kivitendo,
543                        );
544
545   my ($number_of_payments, $paid_amount) = number_of_payments($invoice);
546   my $total = total_amount($invoice);
547
548   my $title = 'default invoice, two items, 19/7% tax without skonto incomplete payment';
549
550   is($invoice->netamount,        5.85 + 11.66,     "${title}: netamount");
551   is($invoice->amount,           6.96 + 12.48,     "${title}: amount");
552   is($paid_amount,              -9.44,             "${title}: paid amount");
553   is($invoice->paid,             9.44,            "${title}: paid");
554   is($number_of_payments,   1,                "${title}: 1 AR_paid bookings");
555   is($total,                    0,                "${title}: even balance");
556 }
557
558 # test 5
559 sub test_default_invoice_two_items_19_7_tax_without_skonto_multiple_payments() {
560   reset_state() if $ALWAYS_RESET;
561
562   my $item1   = new_item(qty => 2.5);
563   my $item2   = new_item(qty => 1.2, part => $parts[1]);
564   my $invoice = new_invoice(
565     taxincluded  => 0,
566     invoiceitems => [ $item1, $item2 ],
567     payment_id  => $payment_terms->id,
568   );
569   $invoice->post;
570
571   $invoice->pay_invoice( amount       => '9.44',
572                          payment_type => 'without_skonto',
573                          chart_id     => $bank_account->chart_id,
574                          transdate    => DateTime->today_local->to_kivitendo
575                        );
576   $invoice->pay_invoice( amount       => '10.00',
577                          chart_id     => $bank_account->chart_id,
578                          transdate    => DateTime->today_local->to_kivitendo
579                        );
580
581   my ($number_of_payments, $paid_amount) = number_of_payments($invoice);
582   my $total = total_amount($invoice);
583
584   my $title = 'default invoice, two items, 19/7% tax not included';
585
586   is($invoice->netamount,        5.85 + 11.66,     "${title}: netamount");
587   is($invoice->amount,           6.96 + 12.48,     "${title}: amount");
588   is($paid_amount,                     -19.44,     "${title}: paid amount");
589   is($invoice->paid,                    19.44,     "${title}: paid");
590   is($number_of_payments,                   2,     "${title}: 2 AR_paid bookings");
591   is($total,                                0,     "${title}: even balance");
592
593 }
594
595 # test 6
596 sub test_default_invoice_two_items_19_7_tax_without_skonto_multiple_payments_final_difference_as_skonto() {
597   reset_state() if $ALWAYS_RESET;
598
599   my $item1   = new_item(qty => 2.5);
600   my $item2   = new_item(qty => 1.2, part => $parts[1]);
601   my $invoice = new_invoice(
602     taxincluded  => 0,
603     invoiceitems => [ $item1, $item2 ],
604     payment_id  => $payment_terms->id,
605   );
606   $invoice->post;
607
608   $invoice->pay_invoice( amount       => '9.44',
609                          payment_type => 'without_skonto',
610                          chart_id     => $bank_account->chart_id,
611                          transdate    => DateTime->today_local->to_kivitendo
612                        );
613   $invoice->pay_invoice( amount       => '8.73',
614                          payment_type => 'without_skonto',
615                          chart_id     => $bank_account->chart_id,
616                          transdate    => DateTime->today_local->to_kivitendo
617                        );
618   $invoice->pay_invoice( amount       => $invoice->open_amount,
619                          payment_type => 'difference_as_skonto',
620                          chart_id     => $bank_account->chart_id,
621                          transdate    => DateTime->today_local->to_kivitendo
622                        );
623
624   my ($number_of_payments, $paid_amount) = number_of_payments($invoice);
625   my $total = total_amount($invoice);
626
627   my $title = 'default invoice, two items, 19/7% tax not included';
628
629   is($invoice->netamount,        5.85 + 11.66,     "${title}: netamount");
630   is($invoice->amount,           6.96 + 12.48,     "${title}: amount");
631   is($paid_amount,                     -19.44,     "${title}: paid amount");
632   is($invoice->paid,                    19.44,     "${title}: paid");
633   is($number_of_payments,                   4,     "${title}: 4 AR_paid bookings");
634   is($total,                                0,     "${title}: even balance");
635
636 }
637
638 sub  test_default_invoice_two_items_19_7_tax_without_skonto_multiple_payments_final_difference_as_skonto_1cent() {
639   reset_state() if $ALWAYS_RESET;
640
641   # if there is only one cent left there can only be one skonto booking, the
642   # error handling should choose the highest amount, which is the 7% account
643   # (11.66) rather than the 19% account (5.85).  The actual tax amount is
644   # higher for the 19% case, though (1.11 compared to 0.82)
645
646   my $item1   = new_item(qty => 2.5);
647   my $item2   = new_item(qty => 1.2, part => $parts[1]);
648   my $invoice = new_invoice(
649     taxincluded  => 0,
650     invoiceitems => [ $item1, $item2 ],
651     payment_id  => $payment_terms->id,
652   );
653   $invoice->post;
654
655   $invoice->pay_invoice( amount       => '19.42',
656                          payment_type => 'without_skonto',
657                          chart_id     => $bank_account->chart_id,
658                          transdate    => DateTime->today_local->to_kivitendo
659                        );
660   $invoice->pay_invoice( amount       => $invoice->open_amount,
661                          payment_type => 'difference_as_skonto',
662                          chart_id     => $bank_account->chart_id,
663                          transdate    => DateTime->today_local->to_kivitendo
664                        );
665
666   my ($number_of_payments, $paid_amount) = number_of_payments($invoice);
667   my $total = total_amount($invoice);
668
669   my $title = 'default invoice, two items, 19/7% tax not included';
670
671   is($invoice->netamount,        5.85 + 11.66,     "${title}: netamount");
672   is($invoice->amount,           6.96 + 12.48,     "${title}: amount");
673   is($paid_amount,                     -19.44,     "${title}: paid amount");
674   is($invoice->paid,                    19.44,     "${title}: paid");
675   is($number_of_payments,                   3,     "${title}: 2 AR_paid bookings");
676   is($total,                                0,     "${title}: even balance");
677
678 }
679
680 sub test_default_invoice_two_items_19_7_tax_without_skonto_multiple_payments_final_difference_as_skonto_2cent() {
681   reset_state() if $ALWAYS_RESET;
682
683   # if there are two cents left there will be two skonto bookings, 1 cent each
684   my $item1   = new_item(qty => 2.5);
685   my $item2   = new_item(qty => 1.2, part => $parts[1]);
686   my $invoice = new_invoice(
687     taxincluded  => 0,
688     invoiceitems => [ $item1, $item2 ],
689     payment_id  => $payment_terms->id,
690   );
691   $invoice->post;
692
693   $invoice->pay_invoice( amount       => '19.42',
694                          payment_type => 'without_skonto',
695                          chart_id     => $bank_account->chart_id,
696                          transdate    => DateTime->today_local->to_kivitendo
697                        );
698   $invoice->pay_invoice( amount       => $invoice->open_amount,
699                          payment_type => 'difference_as_skonto',
700                          chart_id     => $bank_account->chart_id,
701                          transdate    => DateTime->today_local->to_kivitendo
702                        );
703
704   my ($number_of_payments, $paid_amount) = number_of_payments($invoice);
705   my $total = total_amount($invoice);
706
707   my $title = 'default invoice, two items, 19/7% tax not included';
708
709   is($invoice->netamount,        5.85 + 11.66,     "${title}: netamount");
710   is($invoice->amount,           6.96 + 12.48,     "${title}: amount");
711   is($paid_amount,                     -19.44,     "${title}: paid amount");
712   is($invoice->paid,                    19.44,     "${title}: paid");
713   is($number_of_payments,                   3,     "${title}: 3 AR_paid bookings");
714   is($total,                                0,     "${title}: even balance");
715
716 }
717
718 sub test_default_invoice_one_item_19_multiple_payment_final_difference_as_skonto() {
719   reset_state() if $ALWAYS_RESET;
720
721   my $item    = new_item(qty => 2.5);
722   my $invoice = new_invoice(
723     taxincluded  => 0,
724     invoiceitems => [ $item ],
725     payment_id   => $payment_terms->id,
726   );
727   $invoice->post;
728
729   # default values
730   my %params = ( chart_id  => $bank_account->chart_id,
731                  transdate => DateTime->today_local->to_kivitendo
732                );
733
734   $params{amount}       = '2.32';
735   $params{payment_type} = 'without_skonto';
736   $invoice->pay_invoice( %params );
737
738   $params{amount}       = '3.81';
739   $params{payment_type} = 'without_skonto';
740   $invoice->pay_invoice( %params );
741
742   $params{amount}       = $invoice->open_amount; # set amount, otherwise previous 3.81 is used
743   $params{payment_type} = 'difference_as_skonto';
744   $invoice->pay_invoice( %params );
745
746   my ($number_of_payments, $paid_amount) = number_of_payments($invoice);
747   my $total = total_amount($invoice);
748
749   my $title = 'default invoice, one item, 19% tax, without_skonto';
750
751   is($invoice->netamount,       5.85,     "${title}: netamount");
752   is($invoice->amount,          6.96,     "${title}: amount");
753   is($paid_amount,             -6.96,     "${title}: paid amount");
754   is($number_of_payments,          3,     "${title}: 3 AR_paid booking");
755   is($invoice->paid,            6.96,     "${title}: paid");
756   is($total,                       0,     "${title}: even balance");
757
758 }
759
760 sub test_default_invoice_one_item_19_multiple_payment_final_difference_as_skonto_1cent() {
761   reset_state() if $ALWAYS_RESET;
762
763   my $item    = new_item(qty => 2.5);
764   my $invoice = new_invoice(
765     taxincluded  => 0,
766     invoiceitems => [ $item ],
767     payment_id   => $payment_terms->id,
768   );
769   $invoice->post;
770
771   # default values
772   my %params = ( chart_id  => $bank_account->chart_id,
773                  transdate => DateTime->today_local->to_kivitendo
774                );
775
776   $params{amount}       = '6.95';
777   $params{payment_type} = 'without_skonto';
778   $invoice->pay_invoice( %params );
779
780   $params{amount}       = $invoice->open_amount; # set amount, otherwise previous value 6.95 is used
781   $params{payment_type} = 'difference_as_skonto';
782   $invoice->pay_invoice( %params );
783
784   my ($number_of_payments, $paid_amount) = number_of_payments($invoice);
785   my $total = total_amount($invoice);
786
787   my $title = 'default invoice, one item, 19% tax, without_skonto';
788
789   is($invoice->netamount,       5.85,     "${title}: netamount");
790   is($invoice->amount,          6.96,     "${title}: amount");
791   is($paid_amount,             -6.96,     "${title}: paid amount");
792   is($number_of_payments,          2,     "${title}: 3 AR_paid booking");
793   is($invoice->paid,            6.96,     "${title}: paid");
794   is($total,                       0,     "${title}: even balance");
795
796 }
797
798 # test 3 : two items, without skonto
799 sub test_default_purchase_invoice_two_charts_19_7_without_skonto() {
800   reset_state() if $ALWAYS_RESET;
801
802   my $purchase_invoice = new_purchase_invoice();
803
804   my %params = ( chart_id => $bank_account->chart_id,
805                  transdate => DateTime->today_local->to_kivitendo
806                );
807
808   $params{amount} = '226'; # pass full amount
809   $params{payment_type} = 'without_skonto';
810
811   $purchase_invoice->pay_invoice( %params );
812
813   my ($number_of_payments, $paid_amount) = number_of_payments($purchase_invoice);
814   my $total = total_amount($purchase_invoice);
815
816   my $title = 'default invoice, two items, 19/7% tax without skonto';
817
818   is($paid_amount,         226,     "${title}: paid amount");
819   is($number_of_payments,    1,     "${title}: 1 AP_paid bookings");
820   is($total,                 0,     "${title}: even balance");
821
822 }
823
824 sub test_default_purchase_invoice_two_charts_19_7_with_skonto() {
825   reset_state() if $ALWAYS_RESET;
826
827   my $purchase_invoice = new_purchase_invoice();
828
829   my %params = ( chart_id => $bank_account->chart_id,
830                  transdate => DateTime->today_local->to_kivitendo
831                );
832
833   # $params{amount} = '226'; # pass full amount
834   $params{payment_type} = 'with_skonto_pt';
835
836   $purchase_invoice->pay_invoice( %params );
837
838   my ($number_of_payments, $paid_amount) = number_of_payments($purchase_invoice);
839   my $total = total_amount($purchase_invoice);
840
841   my $title = 'default invoice, two items, 19/7% tax without skonto';
842
843   is($paid_amount,         226,     "${title}: paid amount");
844   is($number_of_payments,    3,     "${title}: 1 AP_paid bookings");
845   is($total,                 0,     "${title}: even balance");
846
847 }
848
849 sub test_default_purchase_invoice_two_charts_19_7_tax_partial_unrounded_payment_without_skonto() {
850   # check whether unrounded amounts passed via $params{amount} are rounded for without_skonto case
851   reset_state() if $ALWAYS_RESET;
852   my $purchase_invoice = new_purchase_invoice();
853   $purchase_invoice->pay_invoice(
854                           amount       => ( $purchase_invoice->amount / 3 * 2),
855                           payment_type => 'without_skonto',
856                           chart_id     => $bank_account->chart_id,
857                           transdate    => DateTime->today_local->to_kivitendo
858                          );
859   my ($number_of_payments, $paid_amount) = number_of_payments($purchase_invoice);
860   my $total = total_amount($purchase_invoice);
861
862   my $title = 'default purchase_invoice, two charts, 19/7% tax multiple payments with final difference as skonto';
863
864   is($paid_amount,         150.67,   "${title}: paid amount");
865   is($number_of_payments,       1,   "${title}: 1 AP_paid bookings");
866   is($total,                    0,   "${title}: even balance");
867 };
868
869
870 sub test_default_purchase_invoice_two_charts_19_7_tax_without_skonto_multiple_payments_final_difference_as_skonto() {
871   reset_state() if $ALWAYS_RESET;
872
873   my $purchase_invoice = new_purchase_invoice();
874
875   # pay 2/3 and 1/5, leaves 3.83% to be used as Skonto
876   $purchase_invoice->pay_invoice(
877                           amount       => ( $purchase_invoice->amount / 3 * 2),
878                           payment_type => 'without_skonto',
879                           chart_id     => $bank_account->chart_id,
880                           transdate    => DateTime->today_local->to_kivitendo
881                          );
882   $purchase_invoice->pay_invoice(
883                           amount       => ( $purchase_invoice->amount / 5 ),
884                           payment_type => 'without_skonto',
885                           chart_id     => $bank_account->chart_id,
886                           transdate    => DateTime->today_local->to_kivitendo
887                          );
888   $purchase_invoice->pay_invoice(
889                           payment_type => 'difference_as_skonto',
890                           chart_id     => $bank_account->chart_id,
891                           transdate    => DateTime->today_local->to_kivitendo
892                          );
893
894   my ($number_of_payments, $paid_amount) = number_of_payments($purchase_invoice);
895   my $total = total_amount($purchase_invoice);
896
897   my $title = 'default purchase_invoice, two charts, 19/7% tax multiple payments with final difference as skonto';
898
899   is($paid_amount,         226, "${title}: paid amount");
900   is($number_of_payments,    4, "${title}: 1 AP_paid bookings");
901   is($total,                 0, "${title}: even balance");
902
903 }
904
905 # test
906 sub test_default_invoice_two_items_19_7_tax_with_skonto_50_50() {
907   reset_state() if $ALWAYS_RESET;
908
909   my $item1   = new_item(qty => 1, part => $parts[2]);
910   my $item2   = new_item(qty => 1, part => $parts[3]);
911   my $invoice = new_invoice(
912     taxincluded  => 0,
913     invoiceitems => [ $item1, $item2 ],
914     payment_id  => $payment_terms->id,
915   );
916   $invoice->post;
917
918   # default values
919   my %params = ( chart_id => $bank_account->chart_id,
920                  transdate => DateTime->today_local->to_kivitendo
921                );
922
923   $params{amount} = $invoice->amount_less_skonto;
924   $params{payment_type} = 'with_skonto_pt';
925
926   $invoice->pay_invoice( %params );
927
928   my ($number_of_payments, $paid_amount) = number_of_payments($invoice);
929   my $total = total_amount($invoice);
930
931   my $title = 'default invoice, two items, 19/7% tax with_skonto_pt 50/50';
932
933   is($invoice->netamount,        100,     "${title}: netamount");
934   is($invoice->amount,           113,     "${title}: amount");
935   is($paid_amount,              -113,     "${title}: paid amount");
936   is($invoice->paid,             113,     "${title}: paid");
937   is($number_of_payments,          3,     "${title}: 3 AR_paid bookings");
938   is($total,                       0,     "${title}: even balance");
939 }
940
941 # test
942 sub test_default_invoice_four_items_19_7_tax_with_skonto_4x_25() {
943   reset_state() if $ALWAYS_RESET;
944
945   my $item1   = new_item(qty => 0.5, part => $parts[2]);
946   my $item2   = new_item(qty => 0.5, part => $parts[3]);
947   my $item3   = new_item(qty => 0.5, part => $parts[2]);
948   my $item4   = new_item(qty => 0.5, part => $parts[3]);
949   my $invoice = new_invoice(
950     taxincluded  => 0,
951     invoiceitems => [ $item1, $item2, $item3, $item4 ],
952     payment_id  => $payment_terms->id,
953   );
954   $invoice->post;
955
956   # default values
957   my %params = ( chart_id => $bank_account->chart_id,
958                  transdate => DateTime->today_local->to_kivitendo
959                );
960
961   $params{amount} = $invoice->amount_less_skonto;
962   $params{payment_type} = 'with_skonto_pt';
963
964   $invoice->pay_invoice( %params );
965
966   my ($number_of_payments, $paid_amount) = number_of_payments($invoice);
967   my $total = total_amount($invoice);
968
969   my $title = 'default invoice, four items, 19/7% tax with_skonto_pt 4x25';
970
971   is($invoice->netamount , 100  , "${title}: netamount");
972   is($invoice->amount    , 113  , "${title}: amount");
973   is($paid_amount        , -113 , "${title}: paid amount");
974   is($invoice->paid      , 113  , "${title}: paid");
975   is($number_of_payments , 3    , "${title}: 3 AR_paid bookings");
976   is($total              , 0    , "${title}: even balance");
977 }
978
979 sub test_default_invoice_four_items_19_7_tax_with_skonto_4x_25_tax_included() {
980   reset_state() if $ALWAYS_RESET;
981
982   my $item1   = new_item(qty => 0.5, part => $parts[2]);
983   my $item2   = new_item(qty => 0.5, part => $parts[3]);
984   my $item3   = new_item(qty => 0.5, part => $parts[2]);
985   my $item4   = new_item(qty => 0.5, part => $parts[3]);
986   my $invoice = new_invoice(
987     taxincluded  => 1,
988     invoiceitems => [ $item1, $item2, $item3, $item4 ],
989     payment_id  => $payment_terms->id,
990   );
991   $invoice->post;
992
993   # default values
994   my %params = ( chart_id => $bank_account->chart_id,
995                  transdate => DateTime->today_local->to_kivitendo
996                );
997
998   $params{amount} = $invoice->amount_less_skonto;
999   $params{payment_type} = 'with_skonto_pt';
1000
1001   $invoice->pay_invoice( %params );
1002
1003   my ($number_of_payments, $paid_amount) = number_of_payments($invoice);
1004   my $total = total_amount($invoice);
1005
1006   my $title = 'default invoice, four items, 19/7% tax with_skonto_pt 4x25';
1007
1008   is($invoice->netamount,   88.75,    "${title}: netamount");
1009   is($invoice->amount,        100,    "${title}: amount");
1010   is($paid_amount,           -100,    "${title}: paid amount");
1011   is($invoice->paid,          100,    "${title}: paid");
1012   is($number_of_payments,       3,    "${title}: 3 AR_paid bookings");
1013   { local $TODO = "currently this test fails because the code writing the invoice is buggy, the calculation of skonto is correct";
1014   is($total,                    0,    "${title}: even balance");
1015   }
1016 }
1017
1018 sub test_default_invoice_four_items_19_7_tax_with_skonto_4x_25_multiple() {
1019   reset_state() if $ALWAYS_RESET;
1020
1021   my $item1   = new_item(qty => 0.5, part => $parts[2]);
1022   my $item2   = new_item(qty => 0.5, part => $parts[3]);
1023   my $item3   = new_item(qty => 0.5, part => $parts[2]);
1024   my $item4   = new_item(qty => 0.5, part => $parts[3]);
1025   my $invoice = new_invoice(
1026     taxincluded  => 0,
1027     invoiceitems => [ $item1, $item2, $item3, $item4 ],
1028     payment_id  => $payment_terms->id,
1029   );
1030   $invoice->post;
1031
1032   $invoice->pay_invoice( amount       => '90',
1033                          payment_type => 'without_skonto',
1034                          chart_id     => $bank_account->chart_id,
1035                          transdate => DateTime->today_local->to_kivitendo
1036                        );
1037   $invoice->pay_invoice( payment_type => 'difference_as_skonto',
1038                          chart_id     => $bank_account->chart_id,
1039                          transdate    => DateTime->today_local->to_kivitendo
1040                        );
1041
1042   my ($number_of_payments, $paid_amount) = number_of_payments($invoice);
1043   my $total = total_amount($invoice);
1044
1045   my $title = 'default invoice, four items, 19/7% tax with_skonto_pt 4x25';
1046
1047   is($invoice->netamount,  100,     "${title}: netamount");
1048   is($invoice->amount,     113,     "${title}: amount");
1049   is($paid_amount,        -113,     "${title}: paid amount");
1050   is($invoice->paid,       113,     "${title}: paid");
1051   is($number_of_payments,    3,     "${title}: 3 AR_paid bookings");
1052   is($total,                 0,     "${title}: even balance: this will fail due to rounding error in invoice post, not the skonto");
1053 }
1054
1055 sub test_ar_currency_tax_not_included_and_payment {
1056   my $netamount = $::form->round_amount(75 * $exchangerate->sell,2); #  75 in CUR, 100.00 in EUR
1057   my $amount    = $::form->round_amount($netamount * 1.19,2);        # 100 in CUR, 119.00 in EUR
1058   my $invoice   = SL::DB::Invoice->new(
1059       invoice      => 0,
1060       amount       => $amount,
1061       netamount    => $netamount,
1062       transdate    => $transdate1,
1063       taxincluded  => 0,
1064       customer_id  => $customer->id,
1065       taxzone_id   => $customer->taxzone_id,
1066       currency_id  => $currency->id,
1067       transactions => [],
1068       notes        => 'test_ar_currency_tax_not_included_and_payment',
1069   );
1070   $invoice->add_ar_amount_row(
1071     amount     => $invoice->netamount,
1072     chart      => $ar_amount_chart,
1073     tax_id     => $tax->id,
1074   );
1075
1076   $invoice->create_ar_row(chart => $ar_chart);
1077   $invoice->save;
1078
1079   is(SL::DB::Manager::Invoice->get_all_count(where => [ invoice => 0 ]), 1, 'there is one ar transaction');
1080   is($invoice->currency_id , $currency->id , 'currency_id has been saved');
1081   is($invoice->netamount   , 100           , 'ar amount has been converted');
1082   is($invoice->amount      , 119           , 'ar amount has been converted');
1083   is($invoice->taxincluded ,   0           , 'ar transaction doesn\'t have taxincluded');
1084   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');
1085   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');
1086
1087   $invoice->pay_invoice(chart_id   => $bank->id,
1088                         amount     => 50,
1089                         currency   => 'CUR',
1090                         transdate  => $transdate1->to_kivitendo,
1091                        );
1092   $invoice->pay_invoice(chart_id   => $bank->id,
1093                         amount     => 39.25,
1094                         currency   => 'CUR',
1095                         transdate  => $transdate1->to_kivitendo,
1096                        );
1097   # $invoice->pay_invoice(chart_id   => $bank->id,
1098   #                       amount     => 30,
1099   #                       transdate  => $transdate2->to_kivitendo,
1100   #                      );
1101   is(scalar @{$invoice->transactions}, 9, 'ar transaction has 9 transactions (incl. fxtransactions)');
1102   is($invoice->paid, $invoice->amount, 'ar transaction paid = amount in default currency');
1103 };
1104
1105 sub test_ar_currency_tax_included {
1106   # we want the acc_trans amount to be 100
1107   my $amount    = $::form->round_amount(75 * $exchangerate->sell * 1.19);
1108   my $netamount = $::form->round_amount($amount / 1.19,2);
1109   my $invoice = SL::DB::Invoice->new(
1110       invoice      => 0,
1111       amount       => 119,
1112       netamount    => 100,
1113       transdate    => $transdate1,
1114       taxincluded  => 1,
1115       customer_id  => $customer->id,
1116       taxzone_id   => $customer->taxzone_id,
1117       currency_id  => $currency->id,
1118       notes        => 'test_ar_currency_tax_included',
1119       transactions => [],
1120   );
1121   $invoice->add_ar_amount_row( # should take care of taxincluded
1122     amount     => $invoice->amount, # tax included in local currency
1123     chart      => $ar_amount_chart,
1124     tax_id     => $tax->id,
1125   );
1126
1127   $invoice->create_ar_row( chart => $ar_chart );
1128   $invoice->save;
1129   is(SL::DB::Manager::Invoice->get_all_count(where => [ invoice => 0 ]), 2, 'there are now two ar transactions');
1130   is($invoice->currency_id , $currency->id , 'currency_id has been saved');
1131   is($invoice->amount      , $amount       , 'amount ok');
1132   is($invoice->netamount   , $netamount    , 'netamount ok');
1133   is($invoice->taxincluded , 1             , 'ar transaction has taxincluded');
1134   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');
1135   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');
1136   $invoice->pay_invoice(chart_id   => $bank->id,
1137                         amount     => 89.25,
1138                         currency   => 'CUR',
1139                         transdate  => $transdate1->to_kivitendo,
1140                        );
1141
1142 };
1143
1144 sub test_ap_currency_tax_not_included_and_payment {
1145   my $netamount = $::form->round_amount(75 * $exchangerate->buy,2); #  75 in CUR, 100.00 in EUR
1146   my $amount    = $::form->round_amount($netamount * 1.19,2);        # 100 in CUR, 119.00 in EUR
1147   my $invoice   = SL::DB::PurchaseInvoice->new(
1148       invoice      => 0,
1149       invnumber    => 'test_ap_currency_tax_not_included_and_payment',
1150       amount       => $amount,
1151       netamount    => $netamount,
1152       transdate    => $transdate1,
1153       taxincluded  => 0,
1154       vendor_id    => $vendor->id,
1155       taxzone_id   => $vendor->taxzone_id,
1156       currency_id  => $currency->id,
1157       transactions => [],
1158       notes        => 'test_ap_currency_tax_not_included_and_payment',
1159   );
1160   $invoice->add_ap_amount_row(
1161     amount     => $invoice->netamount,
1162     chart      => $ap_amount_chart,
1163     tax_id     => $tax_9->id,
1164   );
1165
1166   $invoice->create_ap_row(chart => $ap_chart);
1167   $invoice->save;
1168
1169   is($invoice->currency_id, $currency->id, 'currency_id has been saved');
1170   is($invoice->netamount, 100, 'ap amount has been converted');
1171   is($invoice->amount, 119, 'ap amount has been converted');
1172   is($invoice->taxincluded, 0, 'ap transaction doesn\'t have taxincluded');
1173   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');
1174   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');
1175
1176   $invoice->pay_invoice(chart_id   => $bank->id,
1177                         amount     => 50,
1178                         currency   => 'CUR',
1179                         transdate  => $transdate1->to_kivitendo,
1180                        );
1181   $invoice->pay_invoice(chart_id   => $bank->id,
1182                         amount     => 39.25,
1183                         currency   => 'CUR',
1184                         transdate  => $transdate1->to_kivitendo,
1185                        );
1186   is(scalar @{$invoice->transactions}, 9, 'ap transaction has 9 transactions (incl. fxtransactions)');
1187   is($invoice->paid, $invoice->amount, 'ap transaction paid = amount in default currency');
1188 };
1189
1190 sub test_ap_currency_tax_included {
1191   # we want the acc_trans amount to be 100
1192   my $amount    = $::form->round_amount(75 * $exchangerate->buy * 1.19);
1193   my $netamount = $::form->round_amount($amount / 1.19,2);
1194   my $invoice = SL::DB::PurchaseInvoice->new(
1195       invoice      => 0,
1196       amount       => 119, #$amount,
1197       netamount    => 100, #$netamount,
1198       transdate    => $transdate1,
1199       taxincluded  => 1,
1200       vendor_id    => $vendor->id,
1201       taxzone_id   => $vendor->taxzone_id,
1202       currency_id  => $currency->id,
1203       notes        => 'test_ap_currency_tax_included',
1204       invnumber    => 'test_ap_currency_tax_included',
1205       transactions => [],
1206   );
1207   $invoice->add_ap_amount_row( # should take care of taxincluded
1208     amount     => $invoice->amount, # tax included in local currency
1209     chart      => $ap_amount_chart,
1210     tax_id     => $tax_9->id,
1211   );
1212
1213   $invoice->create_ap_row( chart => $ap_chart );
1214   $invoice->save;
1215   is($invoice->currency_id , $currency->id , 'currency_id has been saved');
1216   is($invoice->amount      , $amount       , 'amount ok');
1217   is($invoice->netamount   , $netamount    , 'netamount ok');
1218   is($invoice->taxincluded , 1             , 'ap transaction has taxincluded');
1219   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');
1220   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');
1221
1222   $invoice->pay_invoice(chart_id   => $bank->id,
1223                         amount     => 89.25,
1224                         currency   => 'CUR',
1225                         transdate  => $transdate1->to_kivitendo,
1226                        );
1227
1228 };
1229
1230 sub test_ar_currency_tax_not_included_and_payment_2 {
1231   my $title = 'test_ar_currency_tax_not_included_and_payment_2';
1232   my $netamount = $::form->round_amount(125 * $exchangerate2->sell,2); # 125.00 in CUR, 100.00 in EUR
1233   my $amount    = $::form->round_amount($netamount * 1.19,2);          # 148.75 in CUR, 119.00 in EUR
1234   my $invoice   = SL::DB::Invoice->new(
1235       invoice      => 0,
1236       amount       => $amount,
1237       netamount    => $netamount,
1238       transdate    => $transdate2,
1239       taxincluded  => 0,
1240       customer_id  => $customer->id,
1241       taxzone_id   => $customer->taxzone_id,
1242       currency_id  => $currency->id,
1243       transactions => [],
1244       notes        => 'test_ar_currency_tax_not_included_and_payment 0.8',
1245       invnumber    => 'test_ar_currency_tax_not_included_and_payment 0.8',
1246   );
1247   $invoice->add_ar_amount_row(
1248     amount     => $invoice->netamount,
1249     chart      => $ar_amount_chart,
1250     tax_id     => $tax->id,
1251   );
1252
1253   $invoice->create_ar_row(chart => $ar_chart);
1254   $invoice->save;
1255
1256   is($invoice->currency_id , $currency->id , "$title: currency_id has been saved");
1257   is($invoice->netamount   , 100           , "$title: ar amount has been converted");
1258   is($invoice->amount      , 119           , "$title: ar amount has been converted");
1259   is($invoice->taxincluded ,   0           , "$title: ar transaction doesn\"t have taxincluded");
1260   is(SL::DB::Manager::AccTransaction->find_by(chart_id => $ar_amount_chart->id, trans_id => $invoice->id)->amount, '100.00000', $title . " " . $ar_amount_chart->accno . ": has been converted for currency");
1261   is(SL::DB::Manager::AccTransaction->find_by(chart_id => $ar_chart->id, trans_id => $invoice->id)->amount, '-119.00000', $title  . " " . $ar_chart->accno . ': has been converted for currency');
1262
1263   $invoice->pay_invoice(chart_id   => $bank->id,
1264                         amount     => 123.45,
1265                         currency   => 'CUR',
1266                         transdate  => $transdate2->to_kivitendo,
1267                        );
1268   $invoice->pay_invoice(chart_id   => $bank->id,
1269                         amount     => 15.30,
1270                         currency   => 'CUR',
1271                         transdate  => $transdate3->to_kivitendo,
1272                        );
1273   $invoice->pay_invoice(chart_id   => $bank->id,
1274                         amount     => 10.00,
1275                         currency   => 'CUR',
1276                         transdate  => $transdate4->to_kivitendo,
1277                        );
1278   # $invoice->pay_invoice(chart_id   => $bank->id,
1279   #                       amount     => 30,
1280   #                       transdate  => $transdate2->to_kivitendo,
1281   #                      );
1282   my $fx_transactions = SL::DB::Manager::AccTransaction->get_all(where => [ trans_id => $invoice->id, fx_transaction => 1 ], sort_by => ('acc_trans_id'));
1283   is(scalar @{$fx_transactions}, 3, "$title: ar transaction has 3 fx transactions");
1284   is($fx_transactions->[0]->amount, '24.69000', "$title fx transactions 1: 123.45-(123.45*0.8) = 24.69");
1285
1286   is(scalar @{$invoice->transactions}, 14, "$title ar transaction has 14 transactions (incl. fxtransactions and fx_gain)");
1287   is($invoice->paid, $invoice->amount, "$title ar transaction paid = amount in default currency");
1288 };
1289
1290 sub test_ar_currency_tax_not_included_and_payment_2_credit_note {
1291   my $netamount = $::form->round_amount(-125 * $exchangerate2->sell,2); # 125.00 in CUR, 100.00 in EUR
1292   my $amount    = $::form->round_amount($netamount * 1.19,2);          # 148.75 in CUR, 119.00 in EUR
1293   my $invoice   = SL::DB::Invoice->new(
1294       invoice      => 0,
1295       amount       => $amount,
1296       netamount    => $netamount,
1297       transdate    => $transdate2,
1298       taxincluded  => 0,
1299       customer_id  => $customer->id,
1300       taxzone_id   => $customer->taxzone_id,
1301       currency_id  => $currency->id,
1302       transactions => [],
1303       notes        => 'test_ar_currency_tax_not_included_and_payment credit note 0.8',
1304       invnumber    => 'test_ar_currency_tax_not_included_and_payment credit note 0.8',
1305   );
1306   $invoice->add_ar_amount_row(
1307     amount     => $invoice->netamount,
1308     chart      => $ar_amount_chart,
1309     tax_id     => $tax->id,
1310   );
1311
1312   $invoice->create_ar_row(chart => $ar_chart);
1313   $invoice->save;
1314
1315   is($invoice->currency_id , $currency->id , 'currency_id has been saved');
1316   is($invoice->netamount   , -100          , 'ar amount has been converted');
1317   is($invoice->amount      , -119          , 'ar amount has been converted');
1318   is($invoice->taxincluded ,   0           , 'ar transaction doesn\'t have taxincluded');
1319   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');
1320   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');
1321
1322   $invoice->pay_invoice(chart_id   => $bank->id,
1323                         amount     => -123.45,
1324                         currency   => 'CUR',
1325                         transdate  => $transdate2->to_kivitendo,
1326                        );
1327   $invoice->pay_invoice(chart_id   => $bank->id,
1328                         amount     => -25.30,
1329                         currency   => 'CUR',
1330                         transdate  => $transdate2->to_kivitendo,
1331                        );
1332   my $fx_transactions = SL::DB::Manager::AccTransaction->get_all(where => [ trans_id => $invoice->id, fx_transaction => 1 ], sort_by => ('acc_trans_id'));
1333   is(scalar @{$fx_transactions}, 2, 'ar transaction has 2 fx transactions');
1334   is($fx_transactions->[0]->amount, '-24.69000', 'fx transactions 1: 123.45-(123.45*0.8) = 24.69');
1335
1336   is(scalar @{$invoice->transactions}, 9, 'ar transaction has 9 transactions (incl. fxtransactions)');
1337   is($invoice->paid, $invoice->amount, 'ar transaction paid = amount in default currency');
1338 };
1339
1340 sub test_ap_currency_tax_not_included_and_payment_2 {
1341   my $title = 'test_ap_currency_tax_not_included_and_payment_2';
1342   my $netamount = $::form->round_amount(125 * $exchangerate2->sell,2); # 125.00 in CUR, 100.00 in EUR
1343   my $amount    = $::form->round_amount($netamount * 1.19,2);          # 148.75 in CUR, 119.00 in EUR
1344   my $invoice   = SL::DB::PurchaseInvoice->new(
1345       invoice      => 0,
1346       amount       => $amount,
1347       netamount    => $netamount,
1348       transdate    => $transdate2,
1349       taxincluded  => 0,
1350       vendor_id    => $vendor->id,
1351       taxzone_id   => $vendor->taxzone_id,
1352       currency_id  => $currency->id,
1353       transactions => [],
1354       notes        => 'test_ap_currency_tax_not_included_and_payment_2 0.8 + 1.33333',
1355       invnumber    => 'test_ap_currency_tax_not_included_and_payment_2 0.8 + 1.33333',
1356   );
1357   $invoice->add_ap_amount_row(
1358     amount     => $invoice->netamount,
1359     chart      => $ap_amount_chart,
1360     tax_id     => $tax_9->id,
1361   );
1362
1363   $invoice->create_ap_row(chart => $ap_chart);
1364   $invoice->save;
1365
1366   is($invoice->currency_id , $currency->id , "$title: currency_id has been saved");
1367   is($invoice->netamount   ,  100          , "$title: ap amount has been converted");
1368   is($invoice->amount      ,  119          , "$title: ap amount has been converted");
1369   is($invoice->taxincluded ,    0          , "$title: ap transaction doesn\'t have taxincluded");
1370   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');
1371   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');
1372
1373   $invoice->pay_invoice(chart_id   => $bank->id,
1374                         amount     => 10,
1375                         currency   => 'CUR',
1376                         transdate  => $transdate2->to_kivitendo,
1377                        );
1378   $invoice->pay_invoice(chart_id   => $bank->id,
1379                         amount     => 123.45,
1380                         currency   => 'CUR',
1381                         transdate  => $transdate3->to_kivitendo,
1382                        );
1383   $invoice->pay_invoice(chart_id   => $bank->id,
1384                         amount     => 15.30,
1385                         currency   => 'CUR',
1386                         transdate  => $transdate4->to_kivitendo,
1387                        );
1388   my $fx_transactions = SL::DB::Manager::AccTransaction->get_all(where => [ trans_id => $invoice->id, fx_transaction => 1 ], sort_by => ('acc_trans_id'));
1389   is(scalar @{$fx_transactions}, 3, "$title: ap transaction has 3 fx transactions");
1390   is($fx_transactions->[0]->amount,  '-2.00000', "$title: fx transaction 1:  10.00-( 10.00*0.80000) =   2.00000");
1391   is($fx_transactions->[1]->amount,  '68.59000', "$title: fx transaction 2: 123.45-(123.45*1.55557) = -68.58511");
1392   is($fx_transactions->[2]->amount,  '-3.40000', "$title: fx transaction 3:  15.30-(15.30 *0.77777) =   3.40012");
1393
1394   my $fx_loss_transactions = SL::DB::Manager::AccTransaction->get_all(where => [ trans_id => $invoice->id, chart_id => $fxloss_chart->id ], sort_by => ('acc_trans_id'));
1395   my $fx_gain_transactions = SL::DB::Manager::AccTransaction->get_all(where => [ trans_id => $invoice->id, chart_id => $fxgain_chart->id ], sort_by => ('acc_trans_id'));
1396   is($fx_gain_transactions->[0]->amount,   '0.34000', "$title: fx gain amount ok");
1397   is($fx_loss_transactions->[0]->amount, '-93.28000', "$title: fx loss amount ok");
1398
1399   is(scalar @{$invoice->transactions}, 14, "$title: ap transaction has 14 transactions (incl. fxtransactions and gain_loss)");
1400   is($invoice->paid, $invoice->amount, "$title: ap transaction paid = amount in default currency");
1401   is(total_amount($invoice), 0,   "$title: even balance");
1402 };
1403
1404 sub test_ap_currency_tax_not_included_and_payment_2_credit_note {
1405   my $title = 'test_ap_currency_tax_not_included_and_payment_2_credit_note';
1406   my $netamount = $::form->round_amount(-125 * $exchangerate2->sell,2); # 125.00 in CUR, 100.00 in EUR
1407   my $amount    = $::form->round_amount($netamount * 1.19,2);          # 148.75 in CUR, 119.00 in EUR
1408   my $invoice   = SL::DB::PurchaseInvoice->new(
1409       invoice      => 0,
1410       amount       => $amount,
1411       netamount    => $netamount,
1412       transdate    => $transdate2,
1413       taxincluded  => 0,
1414       vendor_id    => $vendor->id,
1415       taxzone_id   => $vendor->taxzone_id,
1416       currency_id  => $currency->id,
1417       transactions => [],
1418       notes        => 'test_ap_currency_tax_not_included_and_payment credit note 0.8 + 1.33333',
1419       invnumber    => 'test_ap_currency_tax_not_included_and_payment credit note 0.8 + 1.33333',
1420   );
1421   $invoice->add_ap_amount_row(
1422     amount     => $invoice->netamount,
1423     chart      => $ap_amount_chart,
1424     tax_id     => $tax_9->id,
1425   );
1426
1427   $invoice->create_ap_row(chart => $ap_chart);
1428   $invoice->save;
1429
1430   is($invoice->currency_id , $currency->id , "$title: currency_id has been saved");
1431   is($invoice->netamount   , -100          , "$title: ap amount has been converted");
1432   is($invoice->amount      , -119          , "$title: ap amount has been converted");
1433   is($invoice->taxincluded ,   0           , "$title: ap transaction doesn\'t have taxincluded");
1434   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');
1435   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');
1436
1437   $invoice->pay_invoice(chart_id   => $bank->id,
1438                         amount     => -10,
1439                         currency   => 'CUR',
1440                         transdate  => $transdate2->to_kivitendo,
1441                        );
1442   $invoice->pay_invoice(chart_id   => $bank->id,
1443                         amount     => -123.45,
1444                         currency   => 'CUR',
1445                         transdate  => $transdate3->to_kivitendo,
1446                        );
1447   $invoice->pay_invoice(chart_id   => $bank->id,
1448                         amount     => -15.30,
1449                         currency   => 'CUR',
1450                         transdate  => $transdate4->to_kivitendo,
1451                        );
1452   my $fx_transactions = SL::DB::Manager::AccTransaction->get_all(where => [ trans_id => $invoice->id, fx_transaction => 1 ], sort_by => ('acc_trans_id'));
1453   is(scalar @{$fx_transactions}, 3, "$title: ap transaction has 3 fx transactions");
1454   is($fx_transactions->[0]->amount,   '2.00000', "$title: fx transaction 1:  10.00-( 10.00*0.80000) =   2.00000");
1455   is($fx_transactions->[1]->amount, '-68.59000', "$title: fx transaction 2: 123.45-(123.45*1.55557) = -68.58511");
1456   is($fx_transactions->[2]->amount,   '3.40000', "$title: fx transaction 3:  15.30-(15.30 *0.77777) =   3.40012");
1457
1458   my $fx_gain_loss_transactions = SL::DB::Manager::AccTransaction->get_all(where => [ trans_id => $invoice->id, chart_id => $fxgain_chart->id ], sort_by => ('acc_trans_id'));
1459   is($fx_gain_loss_transactions->[0]->amount, '93.28000', "$title: fx gain loss amount ok");
1460
1461   is(scalar @{$invoice->transactions}, 14, "$title: ap transaction has 14 transactions (incl. fxtransactions and gain_loss)");
1462   is($invoice->paid, $invoice->amount, "$title: ap transaction paid = amount in default currency");
1463   is(total_amount($invoice), 0,   "$title: even balance");
1464 };
1465
1466 Support::TestSetup::login();
1467
1468 # test cases: without_skonto
1469 test_default_invoice_one_item_19_without_skonto();
1470 test_default_invoice_two_items_19_7_tax_with_skonto();
1471 test_default_invoice_two_items_19_7_without_skonto();
1472 test_default_invoice_two_items_19_7_without_skonto_incomplete_payment();
1473 test_default_invoice_two_items_19_7_tax_without_skonto_multiple_payments();
1474 test_default_purchase_invoice_two_charts_19_7_without_skonto();
1475 test_default_purchase_invoice_two_charts_19_7_tax_partial_unrounded_payment_without_skonto();
1476 test_default_invoice_one_item_19_without_skonto_overpaid();
1477
1478 # test cases: difference_as_skonto
1479 test_default_invoice_two_items_19_7_tax_without_skonto_multiple_payments_final_difference_as_skonto();
1480 test_default_invoice_two_items_19_7_tax_without_skonto_multiple_payments_final_difference_as_skonto_1cent();
1481 test_default_invoice_two_items_19_7_tax_without_skonto_multiple_payments_final_difference_as_skonto_2cent();
1482 test_default_invoice_one_item_19_multiple_payment_final_difference_as_skonto();
1483 test_default_invoice_one_item_19_multiple_payment_final_difference_as_skonto_1cent();
1484 test_default_purchase_invoice_two_charts_19_7_tax_without_skonto_multiple_payments_final_difference_as_skonto();
1485
1486 # test cases: with_skonto_pt
1487 test_default_invoice_two_items_19_7_tax_with_skonto_50_50();
1488 test_default_invoice_four_items_19_7_tax_with_skonto_4x_25();
1489 test_default_invoice_four_items_19_7_tax_with_skonto_4x_25_multiple();
1490 test_default_purchase_invoice_two_charts_19_7_with_skonto();
1491 test_default_invoice_four_items_19_7_tax_with_skonto_4x_25_tax_included();
1492 test_default_invoice_two_items_19_7_tax_with_skonto_tax_included();
1493
1494 # test payment of ar and ap transactions with currency and tax included/not included
1495 # exchangerate = 1.33333
1496 test_ar_currency_tax_not_included_and_payment();
1497 test_ar_currency_tax_included();
1498 test_ap_currency_tax_not_included_and_payment();
1499 test_ap_currency_tax_included();
1500
1501 test_ar_currency_tax_not_included_and_payment_2();              # exchangerate 0.8
1502 test_ar_currency_tax_not_included_and_payment_2_credit_note();  # exchangerate 0.8
1503
1504 test_ap_currency_tax_not_included_and_payment_2();             # two exchangerates, with fx_gain_loss
1505 test_ap_currency_tax_not_included_and_payment_2_credit_note(); # two exchangerates, with fx_gain_loss
1506
1507 { local $TODO = "currently this test fails because the code writing the invoice is buggy, the calculation of skonto is correct";
1508   my ($acc_trans_sum)  = selectfirst_array_query($::form, $currency->db->dbh, 'SELECT SUM(amount) FROM acc_trans'); is($acc_trans_sum, '0.00000', "sum of all acc_trans is 0");
1509 }
1510
1511 # remove all created data at end of test
1512 clear_up();
1513
1514 done_testing();
1515
1516 1;