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