f165b476a497c94a7525b59f5d7d0bdf1be378d7
[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->pay_invoice( %params );
767
768   my ($number_of_payments, $paid_amount) = number_of_payments($ap_transaction);
769   my $total = total_amount($ap_transaction);
770
771   is($paid_amount,         226,     "${title}: paid amount");
772   is($number_of_payments,    3,     "${title}: 1 AP_paid bookings");
773   is($total,                 0,     "${title}: even balance");
774
775 }
776
777 sub test_default_ap_transaction_two_charts_19_7_tax_partial_unrounded_payment_without_skonto {
778   my $title = 'default ap_transaction, two charts, 19/7% tax multiple payments with final difference as skonto';
779
780   # check whether unrounded amounts passed via $params{amount} are rounded for without_skonto case
781   my $ap_transaction = new_ap_transaction();
782   $ap_transaction->pay_invoice(
783                           amount       => ( $ap_transaction->amount / 3 * 2),
784                           payment_type => 'without_skonto',
785                           chart_id     => $bank_account->chart_id,
786                           transdate    => $transdate1,
787                          );
788   my ($number_of_payments, $paid_amount) = number_of_payments($ap_transaction);
789   my $total = total_amount($ap_transaction);
790
791   is($paid_amount,         150.67,   "${title}: paid amount");
792   is($number_of_payments,       1,   "${title}: 1 AP_paid bookings");
793   is($total,                    0,   "${title}: even balance");
794 };
795
796
797 sub test_default_ap_transaction_two_charts_19_7_tax_without_skonto_multiple_payments_final_difference_as_skonto {
798   my $title = 'default ap_transaction, two charts, 19/7% tax multiple payments with final difference as skonto';
799
800   my $ap_transaction = new_ap_transaction();
801
802   # pay 2/3 and 1/5, leaves 3.83% to be used as Skonto
803   $ap_transaction->pay_invoice(
804                           amount       => ( $ap_transaction->amount / 3 * 2),
805                           payment_type => 'without_skonto',
806                           chart_id     => $bank_account->chart_id,
807                           transdate    => $transdate1,
808                          );
809   $ap_transaction->pay_invoice(
810                           amount       => ( $ap_transaction->amount / 5 ),
811                           payment_type => 'without_skonto',
812                           chart_id     => $bank_account->chart_id,
813                           transdate    => $transdate1,
814                          );
815   $ap_transaction->pay_invoice(
816                           payment_type => 'difference_as_skonto',
817                           chart_id     => $bank_account->chart_id,
818                           transdate    => $transdate1,
819                          );
820
821   my ($number_of_payments, $paid_amount) = number_of_payments($ap_transaction);
822   my $total = total_amount($ap_transaction);
823
824   is($paid_amount,         226, "${title}: paid amount");
825   is($number_of_payments,    4, "${title}: 1 AP_paid bookings");
826   is($total,                 0, "${title}: even balance");
827
828 }
829
830 # test
831 sub test_default_invoice_two_items_19_7_tax_with_skonto_50_50 {
832   my $title = 'default invoice, two items, 19/7% tax with_skonto_pt 50/50';
833
834   my $item1   = create_invoice_item(part => $parts[2], qty => 1);
835   my $item2   = create_invoice_item(part => $parts[3], qty => 1);
836   my $invoice = create_sales_invoice(
837     taxincluded  => 0,
838     transdate    => $transdate1,
839     invoiceitems => [ $item1, $item2 ],
840     payment_id   => $payment_terms->id,
841   );
842
843   # default values
844   my %params = ( chart_id => $bank_account->chart_id,
845                  transdate => $transdate1,
846                );
847
848   $params{amount} = $invoice->amount_less_skonto;
849   $params{payment_type} = 'with_skonto_pt';
850
851   $invoice->pay_invoice( %params );
852
853   my ($number_of_payments, $paid_amount) = number_of_payments($invoice);
854   my $total = total_amount($invoice);
855
856   is($invoice->netamount,        100,     "${title}: netamount");
857   is($invoice->amount,           113,     "${title}: amount");
858   is($paid_amount,              -113,     "${title}: paid amount");
859   is($invoice->paid,             113,     "${title}: paid");
860   is($number_of_payments,          3,     "${title}: 3 AR_paid bookings");
861   is($total,                       0,     "${title}: even balance");
862 }
863
864 # test
865 sub test_default_invoice_four_items_19_7_tax_with_skonto_4x_25 {
866   my $title = 'default invoice, four items, 19/7% tax with_skonto_pt 4x25';
867
868   my $item1   = create_invoice_item(part => $parts[2], qty => 0.5);
869   my $item2   = create_invoice_item(part => $parts[3], qty => 0.5);
870   my $item3   = create_invoice_item(part => $parts[2], qty => 0.5);
871   my $item4   = create_invoice_item(part => $parts[3], qty => 0.5);
872   my $invoice = create_sales_invoice(
873     taxincluded  => 0,
874     transdate    => $transdate1,
875     invoiceitems => [ $item1, $item2, $item3, $item4 ],
876     payment_id   => $payment_terms->id,
877   );
878
879   # default values
880   my %params = ( chart_id => $bank_account->chart_id,
881                  transdate => $transdate1,
882                );
883
884   $params{amount} = $invoice->amount_less_skonto;
885   $params{payment_type} = 'with_skonto_pt';
886
887   $invoice->pay_invoice( %params );
888
889   my ($number_of_payments, $paid_amount) = number_of_payments($invoice);
890   my $total = total_amount($invoice);
891
892   is($invoice->netamount , 100  , "${title}: netamount");
893   is($invoice->amount    , 113  , "${title}: amount");
894   is($paid_amount        , -113 , "${title}: paid amount");
895   is($invoice->paid      , 113  , "${title}: paid");
896   is($number_of_payments , 3    , "${title}: 3 AR_paid bookings");
897   is($total              , 0    , "${title}: even balance");
898 }
899
900 sub test_default_invoice_four_items_19_7_tax_with_skonto_4x_25_tax_included {
901   my $title = 'default invoice, four items, 19/7% tax with_skonto_pt 4x25';
902
903   my $item1   = create_invoice_item(part => $parts[2], qty => 0.5);
904   my $item2   = create_invoice_item(part => $parts[3], qty => 0.5);
905   my $item3   = create_invoice_item(part => $parts[2], qty => 0.5);
906   my $item4   = create_invoice_item(part => $parts[3], qty => 0.5);
907   my $invoice = create_sales_invoice(
908     taxincluded  => 1,
909     transdate    => $transdate1,
910     invoiceitems => [ $item1, $item2, $item3, $item4 ],
911     payment_id   => $payment_terms->id,
912   );
913
914   # default values
915   my %params = ( chart_id => $bank_account->chart_id,
916                  transdate => $transdate1,
917                );
918
919   $params{amount} = $invoice->amount_less_skonto;
920   $params{payment_type} = 'with_skonto_pt';
921
922   $invoice->pay_invoice( %params );
923
924   my ($number_of_payments, $paid_amount) = number_of_payments($invoice);
925   my $total = total_amount($invoice);
926
927   is($invoice->netamount,   88.75,    "${title}: netamount");
928   is($invoice->amount,        100,    "${title}: amount");
929   is($paid_amount,           -100,    "${title}: paid amount");
930   is($invoice->paid,          100,    "${title}: paid");
931   is($number_of_payments,       3,    "${title}: 3 AR_paid bookings");
932 TODO: {
933   local $TODO = "currently this test fails because the code writing the invoice is buggy, the calculation of skonto is correct";
934   is($total,                    0,    "${title}: even balance");
935   }
936 }
937
938 sub test_default_invoice_four_items_19_7_tax_with_skonto_4x_25_multiple {
939   my $title = 'default invoice, four items, 19/7% tax with_skonto_pt 4x25';
940
941   my $item1   = create_invoice_item(part => $parts[2], qty => 0.5);
942   my $item2   = create_invoice_item(part => $parts[3], qty => 0.5);
943   my $item3   = create_invoice_item(part => $parts[2], qty => 0.5);
944   my $item4   = create_invoice_item(part => $parts[3], qty => 0.5);
945   my $invoice = create_sales_invoice(
946     taxincluded  => 0,
947     transdate    => $transdate1,
948     invoiceitems => [ $item1, $item2, $item3, $item4 ],
949     payment_id   => $payment_terms->id,
950   );
951
952   $invoice->pay_invoice( amount       => '90',
953                          payment_type => 'without_skonto',
954                          chart_id     => $bank_account->chart_id,
955                          transdate => $transdate1,
956                        );
957   $invoice->pay_invoice( payment_type => 'difference_as_skonto',
958                          chart_id     => $bank_account->chart_id,
959                          transdate    => $transdate1,
960                        );
961
962   my ($number_of_payments, $paid_amount) = number_of_payments($invoice);
963   my $total = total_amount($invoice);
964
965   is($invoice->netamount,  100,     "${title}: netamount");
966   is($invoice->amount,     113,     "${title}: amount");
967   is($paid_amount,        -113,     "${title}: paid amount");
968   is($invoice->paid,       113,     "${title}: paid");
969   is($number_of_payments,    3,     "${title}: 3 AR_paid bookings");
970   is($total,                 0,     "${title}: even balance: this will fail due to rounding error in invoice post, not the skonto");
971 }
972
973 sub test_ar_currency_tax_not_included_and_payment {
974   my $title = 'test_ar_currency_tax_not_included_and_payment_2';
975
976   my $netamount = $::form->round_amount(75 * $exchangerate->sell,2); #  75 in CUR, 100.00 in EUR
977   my $amount    = $::form->round_amount($netamount * 1.19,2);        # 100 in CUR, 119.00 in EUR
978   my $invoice   = SL::DB::Invoice->new(
979       invoice      => 0,
980       amount       => $amount,
981       netamount    => $netamount,
982       transdate    => $transdate1,
983       taxincluded  => 0,
984       customer_id  => $customer->id,
985       taxzone_id   => $customer->taxzone_id,
986       currency_id  => $currency->id,
987       transactions => [],
988       notes        => 'test_ar_currency_tax_not_included_and_payment',
989   );
990   $invoice->add_ar_amount_row(
991     amount     => $invoice->netamount,
992     chart      => $ar_amount_chart,
993     tax_id     => $tax->id,
994   );
995
996   $invoice->create_ar_row(chart => $ar_chart);
997   $invoice->save;
998
999   is(SL::DB::Manager::Invoice->get_all_count(where => [ invoice => 0 ]), 1, 'there is one ar transaction');
1000   is($invoice->currency_id , $currency->id , 'currency_id has been saved');
1001   is($invoice->netamount   , 100           , 'ar amount has been converted');
1002   is($invoice->amount      , 119           , 'ar amount has been converted');
1003   is($invoice->taxincluded ,   0           , 'ar transaction doesn\'t have taxincluded');
1004   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');
1005   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');
1006
1007   $invoice->pay_invoice(chart_id   => $bank->id,
1008                         amount     => 50,
1009                         currency   => 'CUR',
1010                         transdate  => $transdate1->to_kivitendo,
1011                        );
1012   $invoice->pay_invoice(chart_id   => $bank->id,
1013                         amount     => 39.25,
1014                         currency   => 'CUR',
1015                         transdate  => $transdate1->to_kivitendo,
1016                        );
1017   # $invoice->pay_invoice(chart_id   => $bank->id,
1018   #                       amount     => 30,
1019   #                       transdate  => $transdate2->to_kivitendo,
1020   #                      );
1021   is(scalar @{$invoice->transactions}, 9, 'ar transaction has 9 transactions (incl. fxtransactions)');
1022   is($invoice->paid, $invoice->amount, 'ar transaction paid = amount in default currency');
1023 };
1024
1025 sub test_ar_currency_tax_included {
1026   my $title = 'test_ar_currency_tax_included';
1027
1028   # we want the acc_trans amount to be 100
1029   my $amount    = $::form->round_amount(75 * $exchangerate->sell * 1.19);
1030   my $netamount = $::form->round_amount($amount / 1.19,2);
1031   my $invoice = SL::DB::Invoice->new(
1032       invoice      => 0,
1033       amount       => 119,
1034       netamount    => 100,
1035       transdate    => $transdate1,
1036       taxincluded  => 1,
1037       customer_id  => $customer->id,
1038       taxzone_id   => $customer->taxzone_id,
1039       currency_id  => $currency->id,
1040       notes        => 'test_ar_currency_tax_included',
1041       transactions => [],
1042   );
1043   $invoice->add_ar_amount_row( # should take care of taxincluded
1044     amount     => $invoice->amount, # tax included in local currency
1045     chart      => $ar_amount_chart,
1046     tax_id     => $tax->id,
1047   );
1048
1049   $invoice->create_ar_row( chart => $ar_chart );
1050   $invoice->save;
1051   is(SL::DB::Manager::Invoice->get_all_count(where => [ invoice => 0 ]), 2, 'there are now two ar transactions');
1052   is($invoice->currency_id , $currency->id , 'currency_id has been saved');
1053   is($invoice->amount      , $amount       , 'amount ok');
1054   is($invoice->netamount   , $netamount    , 'netamount ok');
1055   is($invoice->taxincluded , 1             , 'ar transaction has taxincluded');
1056   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');
1057   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');
1058   $invoice->pay_invoice(chart_id   => $bank->id,
1059                         amount     => 89.25,
1060                         currency   => 'CUR',
1061                         transdate  => $transdate1->to_kivitendo,
1062                        );
1063
1064 };
1065
1066 sub test_ap_currency_tax_not_included_and_payment {
1067   my $title = 'test_ap_currency_tax_not_included_and_payment';
1068
1069   my $netamount = $::form->round_amount(75 * $exchangerate->buy,2); #  75 in CUR, 100.00 in EUR
1070   my $amount    = $::form->round_amount($netamount * 1.19,2);        # 100 in CUR, 119.00 in EUR
1071   my $invoice   = SL::DB::PurchaseInvoice->new(
1072       invoice      => 0,
1073       invnumber    => 'test_ap_currency_tax_not_included_and_payment',
1074       amount       => $amount,
1075       netamount    => $netamount,
1076       transdate    => $transdate1,
1077       taxincluded  => 0,
1078       vendor_id    => $vendor->id,
1079       taxzone_id   => $vendor->taxzone_id,
1080       currency_id  => $currency->id,
1081       transactions => [],
1082       notes        => 'test_ap_currency_tax_not_included_and_payment',
1083   );
1084   $invoice->add_ap_amount_row(
1085     amount     => $invoice->netamount,
1086     chart      => $ap_amount_chart,
1087     tax_id     => $tax_9->id,
1088   );
1089
1090   $invoice->create_ap_row(chart => $ap_chart);
1091   $invoice->save;
1092
1093   is($invoice->currency_id, $currency->id, 'currency_id has been saved');
1094   is($invoice->netamount, 100, 'ap amount has been converted');
1095   is($invoice->amount, 119, 'ap amount has been converted');
1096   is($invoice->taxincluded, 0, 'ap transaction doesn\'t have taxincluded');
1097   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');
1098   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');
1099
1100   $invoice->pay_invoice(chart_id   => $bank->id,
1101                         amount     => 50,
1102                         currency   => 'CUR',
1103                         transdate  => $transdate1->to_kivitendo,
1104                        );
1105   $invoice->pay_invoice(chart_id   => $bank->id,
1106                         amount     => 39.25,
1107                         currency   => 'CUR',
1108                         transdate  => $transdate1->to_kivitendo,
1109                        );
1110   is(scalar @{$invoice->transactions}, 9, 'ap transaction has 9 transactions (incl. fxtransactions)');
1111   is($invoice->paid, $invoice->amount, 'ap transaction paid = amount in default currency');
1112 };
1113
1114 sub test_ap_currency_tax_included {
1115   my $title = 'test_ap_currency_tax_included';
1116
1117   # we want the acc_trans amount to be 100
1118   my $amount    = $::form->round_amount(75 * $exchangerate->buy * 1.19);
1119   my $netamount = $::form->round_amount($amount / 1.19,2);
1120   my $invoice = SL::DB::PurchaseInvoice->new(
1121       invoice      => 0,
1122       amount       => 119, #$amount,
1123       netamount    => 100, #$netamount,
1124       transdate    => $transdate1,
1125       taxincluded  => 1,
1126       vendor_id    => $vendor->id,
1127       taxzone_id   => $vendor->taxzone_id,
1128       currency_id  => $currency->id,
1129       notes        => 'test_ap_currency_tax_included',
1130       invnumber    => 'test_ap_currency_tax_included',
1131       transactions => [],
1132   );
1133   $invoice->add_ap_amount_row( # should take care of taxincluded
1134     amount     => $invoice->amount, # tax included in local currency
1135     chart      => $ap_amount_chart,
1136     tax_id     => $tax_9->id,
1137   );
1138
1139   $invoice->create_ap_row( chart => $ap_chart );
1140   $invoice->save;
1141   is($invoice->currency_id , $currency->id , 'currency_id has been saved');
1142   is($invoice->amount      , $amount       , 'amount ok');
1143   is($invoice->netamount   , $netamount    , 'netamount ok');
1144   is($invoice->taxincluded , 1             , 'ap transaction has taxincluded');
1145   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');
1146   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');
1147
1148   $invoice->pay_invoice(chart_id   => $bank->id,
1149                         amount     => 89.25,
1150                         currency   => 'CUR',
1151                         transdate  => $transdate1->to_kivitendo,
1152                        );
1153
1154 };
1155
1156 sub test_ar_currency_tax_not_included_and_payment_2 {
1157   my $title = 'test_ar_currency_tax_not_included_and_payment_2';
1158
1159   my $netamount = $::form->round_amount(125 * $exchangerate2->sell,2); # 125.00 in CUR, 100.00 in EUR
1160   my $amount    = $::form->round_amount($netamount * 1.19,2);          # 148.75 in CUR, 119.00 in EUR
1161   my $invoice   = SL::DB::Invoice->new(
1162       invoice      => 0,
1163       amount       => $amount,
1164       netamount    => $netamount,
1165       transdate    => $transdate2,
1166       taxincluded  => 0,
1167       customer_id  => $customer->id,
1168       taxzone_id   => $customer->taxzone_id,
1169       currency_id  => $currency->id,
1170       transactions => [],
1171       notes        => 'test_ar_currency_tax_not_included_and_payment 0.8',
1172       invnumber    => 'test_ar_currency_tax_not_included_and_payment 0.8',
1173   );
1174   $invoice->add_ar_amount_row(
1175     amount     => $invoice->netamount,
1176     chart      => $ar_amount_chart,
1177     tax_id     => $tax->id,
1178   );
1179
1180   $invoice->create_ar_row(chart => $ar_chart);
1181   $invoice->save;
1182
1183   is($invoice->currency_id , $currency->id , "$title: currency_id has been saved");
1184   is($invoice->netamount   , 100           , "$title: ar amount has been converted");
1185   is($invoice->amount      , 119           , "$title: ar amount has been converted");
1186   is($invoice->taxincluded ,   0           , "$title: ar transaction doesn\"t have taxincluded");
1187   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");
1188   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');
1189
1190   $invoice->pay_invoice(chart_id   => $bank->id,
1191                         amount     => 123.45,
1192                         currency   => 'CUR',
1193                         transdate  => $transdate2->to_kivitendo,
1194                        );
1195   $invoice->pay_invoice(chart_id   => $bank->id,
1196                         amount     => 15.30,
1197                         currency   => 'CUR',
1198                         transdate  => $transdate3->to_kivitendo,
1199                        );
1200   $invoice->pay_invoice(chart_id   => $bank->id,
1201                         amount     => 10.00,
1202                         currency   => 'CUR',
1203                         transdate  => $transdate4->to_kivitendo,
1204                        );
1205   # $invoice->pay_invoice(chart_id   => $bank->id,
1206   #                       amount     => 30,
1207   #                       transdate  => $transdate2->to_kivitendo,
1208   #                      );
1209   my $fx_transactions = SL::DB::Manager::AccTransaction->get_all(where => [ trans_id => $invoice->id, fx_transaction => 1 ], sort_by => ('acc_trans_id'));
1210   is(scalar @{$fx_transactions}, 3, "$title: ar transaction has 3 fx transactions");
1211   is($fx_transactions->[0]->amount, '24.69000', "$title fx transactions 1: 123.45-(123.45*0.8) = 24.69");
1212
1213   is(scalar @{$invoice->transactions}, 14, "$title ar transaction has 14 transactions (incl. fxtransactions and fx_gain)");
1214   is($invoice->paid, $invoice->amount, "$title ar transaction paid = amount in default currency");
1215 };
1216
1217 sub test_ar_currency_tax_not_included_and_payment_2_credit_note {
1218   my $title = 'test_ar_currency_tax_not_included_and_payment_2_credit_note';
1219
1220   my $netamount = $::form->round_amount(-125 * $exchangerate2->sell,2); # 125.00 in CUR, 100.00 in EUR
1221   my $amount    = $::form->round_amount($netamount * 1.19,2);          # 148.75 in CUR, 119.00 in EUR
1222   my $invoice   = SL::DB::Invoice->new(
1223       invoice      => 0,
1224       amount       => $amount,
1225       netamount    => $netamount,
1226       transdate    => $transdate2,
1227       taxincluded  => 0,
1228       customer_id  => $customer->id,
1229       taxzone_id   => $customer->taxzone_id,
1230       currency_id  => $currency->id,
1231       transactions => [],
1232       notes        => 'test_ar_currency_tax_not_included_and_payment credit note 0.8',
1233       invnumber    => 'test_ar_currency_tax_not_included_and_payment credit note 0.8',
1234   );
1235   $invoice->add_ar_amount_row(
1236     amount     => $invoice->netamount,
1237     chart      => $ar_amount_chart,
1238     tax_id     => $tax->id,
1239   );
1240
1241   $invoice->create_ar_row(chart => $ar_chart);
1242   $invoice->save;
1243
1244   is($invoice->currency_id , $currency->id , 'currency_id has been saved');
1245   is($invoice->netamount   , -100          , 'ar amount has been converted');
1246   is($invoice->amount      , -119          , 'ar amount has been converted');
1247   is($invoice->taxincluded ,   0           , 'ar transaction doesn\'t have taxincluded');
1248   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');
1249   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');
1250
1251   $invoice->pay_invoice(chart_id   => $bank->id,
1252                         amount     => -123.45,
1253                         currency   => 'CUR',
1254                         transdate  => $transdate2->to_kivitendo,
1255                        );
1256   $invoice->pay_invoice(chart_id   => $bank->id,
1257                         amount     => -25.30,
1258                         currency   => 'CUR',
1259                         transdate  => $transdate2->to_kivitendo,
1260                        );
1261   my $fx_transactions = SL::DB::Manager::AccTransaction->get_all(where => [ trans_id => $invoice->id, fx_transaction => 1 ], sort_by => ('acc_trans_id'));
1262   is(scalar @{$fx_transactions}, 2, 'ar transaction has 2 fx transactions');
1263   is($fx_transactions->[0]->amount, '-24.69000', 'fx transactions 1: 123.45-(123.45*0.8) = 24.69');
1264
1265   is(scalar @{$invoice->transactions}, 9, 'ar transaction has 9 transactions (incl. fxtransactions)');
1266   is($invoice->paid, $invoice->amount, 'ar transaction paid = amount in default currency');
1267 };
1268
1269 sub test_ap_currency_tax_not_included_and_payment_2 {
1270   my $title = 'test_ap_currency_tax_not_included_and_payment_2';
1271
1272   my $netamount = $::form->round_amount(125 * $exchangerate2->sell,2); # 125.00 in CUR, 100.00 in EUR
1273   my $amount    = $::form->round_amount($netamount * 1.19,2);          # 148.75 in CUR, 119.00 in EUR
1274   my $invoice   = SL::DB::PurchaseInvoice->new(
1275       invoice      => 0,
1276       amount       => $amount,
1277       netamount    => $netamount,
1278       transdate    => $transdate2,
1279       taxincluded  => 0,
1280       vendor_id    => $vendor->id,
1281       taxzone_id   => $vendor->taxzone_id,
1282       currency_id  => $currency->id,
1283       transactions => [],
1284       notes        => 'test_ap_currency_tax_not_included_and_payment_2 0.8 + 1.33333',
1285       invnumber    => 'test_ap_currency_tax_not_included_and_payment_2 0.8 + 1.33333',
1286   );
1287   $invoice->add_ap_amount_row(
1288     amount     => $invoice->netamount,
1289     chart      => $ap_amount_chart,
1290     tax_id     => $tax_9->id,
1291   );
1292
1293   $invoice->create_ap_row(chart => $ap_chart);
1294   $invoice->save;
1295
1296   is($invoice->currency_id , $currency->id , "$title: currency_id has been saved");
1297   is($invoice->netamount   ,  100          , "$title: ap amount has been converted");
1298   is($invoice->amount      ,  119          , "$title: ap amount has been converted");
1299   is($invoice->taxincluded ,    0          , "$title: ap transaction doesn\'t have taxincluded");
1300   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');
1301   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');
1302
1303   $invoice->pay_invoice(chart_id   => $bank->id,
1304                         amount     => 10,
1305                         currency   => 'CUR',
1306                         transdate  => $transdate2->to_kivitendo,
1307                        );
1308   $invoice->pay_invoice(chart_id   => $bank->id,
1309                         amount     => 123.45,
1310                         currency   => 'CUR',
1311                         transdate  => $transdate3->to_kivitendo,
1312                        );
1313   $invoice->pay_invoice(chart_id   => $bank->id,
1314                         amount     => 15.30,
1315                         currency   => 'CUR',
1316                         transdate  => $transdate4->to_kivitendo,
1317                        );
1318   my $fx_transactions = SL::DB::Manager::AccTransaction->get_all(where => [ trans_id => $invoice->id, fx_transaction => 1 ], sort_by => ('acc_trans_id'));
1319   is(scalar @{$fx_transactions}, 3, "$title: ap transaction has 3 fx transactions");
1320   is($fx_transactions->[0]->amount,  '-2.00000', "$title: fx transaction 1:  10.00-( 10.00*0.80000) =   2.00000");
1321   is($fx_transactions->[1]->amount,  '68.59000', "$title: fx transaction 2: 123.45-(123.45*1.55557) = -68.58511");
1322   is($fx_transactions->[2]->amount,  '-3.40000', "$title: fx transaction 3:  15.30-(15.30 *0.77777) =   3.40012");
1323
1324   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'));
1325   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'));
1326   is($fx_gain_transactions->[0]->amount,   '0.34000', "$title: fx gain amount ok");
1327   is($fx_loss_transactions->[0]->amount, '-93.28000', "$title: fx loss amount ok");
1328
1329   is(scalar @{$invoice->transactions}, 14, "$title: ap transaction has 14 transactions (incl. fxtransactions and gain_loss)");
1330   is($invoice->paid, $invoice->amount, "$title: ap transaction paid = amount in default currency");
1331   is(total_amount($invoice), 0,   "$title: even balance");
1332 };
1333
1334 sub test_ap_currency_tax_not_included_and_payment_2_credit_note {
1335   my $title = 'test_ap_currency_tax_not_included_and_payment_2_credit_note';
1336
1337   my $netamount = $::form->round_amount(-125 * $exchangerate2->sell,2); # 125.00 in CUR, 100.00 in EUR
1338   my $amount    = $::form->round_amount($netamount * 1.19,2);          # 148.75 in CUR, 119.00 in EUR
1339   my $invoice   = SL::DB::PurchaseInvoice->new(
1340       invoice      => 0,
1341       amount       => $amount,
1342       netamount    => $netamount,
1343       transdate    => $transdate2,
1344       taxincluded  => 0,
1345       vendor_id    => $vendor->id,
1346       taxzone_id   => $vendor->taxzone_id,
1347       currency_id  => $currency->id,
1348       transactions => [],
1349       notes        => 'test_ap_currency_tax_not_included_and_payment credit note 0.8 + 1.33333',
1350       invnumber    => 'test_ap_currency_tax_not_included_and_payment credit note 0.8 + 1.33333',
1351   );
1352   $invoice->add_ap_amount_row(
1353     amount     => $invoice->netamount,
1354     chart      => $ap_amount_chart,
1355     tax_id     => $tax_9->id,
1356   );
1357
1358   $invoice->create_ap_row(chart => $ap_chart);
1359   $invoice->save;
1360
1361   is($invoice->currency_id , $currency->id , "$title: currency_id has been saved");
1362   is($invoice->netamount   , -100          , "$title: ap amount has been converted");
1363   is($invoice->amount      , -119          , "$title: ap amount has been converted");
1364   is($invoice->taxincluded ,   0           , "$title: ap transaction doesn\'t have taxincluded");
1365   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');
1366   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');
1367
1368   $invoice->pay_invoice(chart_id   => $bank->id,
1369                         amount     => -10,
1370                         currency   => 'CUR',
1371                         transdate  => $transdate2->to_kivitendo,
1372                        );
1373   $invoice->pay_invoice(chart_id   => $bank->id,
1374                         amount     => -123.45,
1375                         currency   => 'CUR',
1376                         transdate  => $transdate3->to_kivitendo,
1377                        );
1378   $invoice->pay_invoice(chart_id   => $bank->id,
1379                         amount     => -15.30,
1380                         currency   => 'CUR',
1381                         transdate  => $transdate4->to_kivitendo,
1382                        );
1383   my $fx_transactions = SL::DB::Manager::AccTransaction->get_all(where => [ trans_id => $invoice->id, fx_transaction => 1 ], sort_by => ('acc_trans_id'));
1384   is(scalar @{$fx_transactions}, 3, "$title: ap transaction has 3 fx transactions");
1385   is($fx_transactions->[0]->amount,   '2.00000', "$title: fx transaction 1:  10.00-( 10.00*0.80000) =   2.00000");
1386   is($fx_transactions->[1]->amount, '-68.59000', "$title: fx transaction 2: 123.45-(123.45*1.55557) = -68.58511");
1387   is($fx_transactions->[2]->amount,   '3.40000', "$title: fx transaction 3:  15.30-(15.30 *0.77777) =   3.40012");
1388
1389   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'));
1390   is($fx_gain_loss_transactions->[0]->amount, '93.28000', "$title: fx gain loss amount ok");
1391
1392   is(scalar @{$invoice->transactions}, 14, "$title: ap transaction has 14 transactions (incl. fxtransactions and gain_loss)");
1393   is($invoice->paid, $invoice->amount, "$title: ap transaction paid = amount in default currency");
1394   is(total_amount($invoice), 0,   "$title: even balance");
1395 };
1396
1397 sub test_credit_note_two_items_19_7_tax_tax_not_included {
1398   my $title = 'test_credit_note_two_items_19_7_tax_tax_not_included';
1399
1400   my $item1   = create_invoice_item(part => $parts[0], qty => 5);
1401   my $item2   = create_invoice_item(part => $parts[1], qty => 3);
1402   my $invoice = create_credit_note(
1403     invnumber    => 'cn1',
1404     transdate    => $transdate1,
1405     taxincluded  => 0,
1406     invoiceitems => [ $item1, $item2 ],
1407   );
1408
1409   # default values
1410   my %params = ( chart_id => $bank_account->chart_id,
1411                  transdate => $transdate1,
1412                );
1413
1414   $params{amount}       = $invoice->amount,
1415
1416   $invoice->pay_invoice( %params );
1417
1418   my ($number_of_payments, $paid_amount) = number_of_payments($invoice);
1419   my $total = total_amount($invoice);
1420
1421   is($invoice->netamount,        -40.84,   "${title}: netamount");
1422   is($invoice->amount,           -45.10,   "${title}: amount");
1423   is($paid_amount,                45.10,   "${title}: paid amount according to acc_trans is positive (Haben)");
1424   is($invoice->paid,             -45.10,   "${title}: paid");
1425   is($number_of_payments,             1,   "${title}: 1 AR_paid bookings");
1426   is($total,                          0,   "${title}: even balance");
1427 }
1428
1429 1;