+ transdate => $transdate1->to_kivitendo,
+ );
+
+};
+
+sub test_ar_currency_tax_not_included_and_payment_2 {
+ my $title = 'test_ar_currency_tax_not_included_and_payment_2';
+ my $netamount = $::form->round_amount(125 * $exchangerate2->sell,2); # 125.00 in CUR, 100.00 in EUR
+ my $amount = $::form->round_amount($netamount * 1.19,2); # 148.75 in CUR, 119.00 in EUR
+ my $invoice = SL::DB::Invoice->new(
+ invoice => 0,
+ amount => $amount,
+ netamount => $netamount,
+ transdate => $transdate2,
+ taxincluded => 0,
+ customer_id => $customer->id,
+ taxzone_id => $customer->taxzone_id,
+ currency_id => $currency->id,
+ transactions => [],
+ notes => 'test_ar_currency_tax_not_included_and_payment 0.8',
+ invnumber => 'test_ar_currency_tax_not_included_and_payment 0.8',
+ );
+ $invoice->add_ar_amount_row(
+ amount => $invoice->netamount,
+ chart => $ar_amount_chart,
+ tax_id => $tax->id,
+ );
+
+ $invoice->create_ar_row(chart => $ar_chart);
+ $invoice->save;
+
+ is($invoice->currency_id , $currency->id , "$title: currency_id has been saved");
+ is($invoice->netamount , 100 , "$title: ar amount has been converted");
+ is($invoice->amount , 119 , "$title: ar amount has been converted");
+ is($invoice->taxincluded , 0 , "$title: ar transaction doesn\"t have taxincluded");
+ 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");
+ 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');
+
+ $invoice->pay_invoice(chart_id => $bank->id,
+ amount => 123.45,
+ currency => 'CUR',
+ transdate => $transdate2->to_kivitendo,
+ );
+ $invoice->pay_invoice(chart_id => $bank->id,
+ amount => 15.30,
+ currency => 'CUR',
+ transdate => $transdate3->to_kivitendo,
+ );
+ $invoice->pay_invoice(chart_id => $bank->id,
+ amount => 10.00,
+ currency => 'CUR',
+ transdate => $transdate4->to_kivitendo,
+ );
+ # $invoice->pay_invoice(chart_id => $bank->id,
+ # amount => 30,
+ # transdate => $transdate2->to_kivitendo,
+ # );
+ my $fx_transactions = SL::DB::Manager::AccTransaction->get_all(where => [ trans_id => $invoice->id, fx_transaction => 1 ], sort_by => ('acc_trans_id'));
+ is(scalar @{$fx_transactions}, 3, "$title: ar transaction has 3 fx transactions");
+ is($fx_transactions->[0]->amount, '24.69000', "$title fx transactions 1: 123.45-(123.45*0.8) = 24.69");
+
+ is(scalar @{$invoice->transactions}, 14, "$title ar transaction has 14 transactions (incl. fxtransactions and fx_gain)");
+ is($invoice->paid, $invoice->amount, "$title ar transaction paid = amount in default currency");
+};
+
+sub test_ar_currency_tax_not_included_and_payment_2_credit_note {
+ my $netamount = $::form->round_amount(-125 * $exchangerate2->sell,2); # 125.00 in CUR, 100.00 in EUR
+ my $amount = $::form->round_amount($netamount * 1.19,2); # 148.75 in CUR, 119.00 in EUR
+ my $invoice = SL::DB::Invoice->new(
+ invoice => 0,
+ amount => $amount,
+ netamount => $netamount,
+ transdate => $transdate2,
+ taxincluded => 0,
+ customer_id => $customer->id,
+ taxzone_id => $customer->taxzone_id,
+ currency_id => $currency->id,
+ transactions => [],
+ notes => 'test_ar_currency_tax_not_included_and_payment credit note 0.8',
+ invnumber => 'test_ar_currency_tax_not_included_and_payment credit note 0.8',
+ );
+ $invoice->add_ar_amount_row(
+ amount => $invoice->netamount,
+ chart => $ar_amount_chart,
+ tax_id => $tax->id,
+ );
+
+ $invoice->create_ar_row(chart => $ar_chart);
+ $invoice->save;
+
+ is($invoice->currency_id , $currency->id , 'currency_id has been saved');
+ is($invoice->netamount , -100 , 'ar amount has been converted');
+ is($invoice->amount , -119 , 'ar amount has been converted');
+ is($invoice->taxincluded , 0 , 'ar transaction doesn\'t have taxincluded');
+ 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');
+ 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');
+
+ $invoice->pay_invoice(chart_id => $bank->id,
+ amount => -123.45,
+ currency => 'CUR',
+ transdate => $transdate2->to_kivitendo,
+ );
+ $invoice->pay_invoice(chart_id => $bank->id,
+ amount => -25.30,
+ currency => 'CUR',
+ transdate => $transdate2->to_kivitendo,
+ );
+ my $fx_transactions = SL::DB::Manager::AccTransaction->get_all(where => [ trans_id => $invoice->id, fx_transaction => 1 ], sort_by => ('acc_trans_id'));
+ is(scalar @{$fx_transactions}, 2, 'ar transaction has 2 fx transactions');
+ is($fx_transactions->[0]->amount, '-24.69000', 'fx transactions 1: 123.45-(123.45*0.8) = 24.69');
+
+ is(scalar @{$invoice->transactions}, 9, 'ar transaction has 9 transactions (incl. fxtransactions)');
+ is($invoice->paid, $invoice->amount, 'ar transaction paid = amount in default currency');
+};
+
+sub test_ap_currency_tax_not_included_and_payment_2 {
+ my $title = 'test_ap_currency_tax_not_included_and_payment_2';
+ my $netamount = $::form->round_amount(125 * $exchangerate2->sell,2); # 125.00 in CUR, 100.00 in EUR
+ my $amount = $::form->round_amount($netamount * 1.19,2); # 148.75 in CUR, 119.00 in EUR
+ my $invoice = SL::DB::PurchaseInvoice->new(
+ invoice => 0,
+ amount => $amount,
+ netamount => $netamount,
+ transdate => $transdate2,
+ taxincluded => 0,
+ vendor_id => $vendor->id,
+ taxzone_id => $vendor->taxzone_id,
+ currency_id => $currency->id,
+ transactions => [],
+ notes => 'test_ap_currency_tax_not_included_and_payment_2 0.8 + 1.33333',
+ invnumber => 'test_ap_currency_tax_not_included_and_payment_2 0.8 + 1.33333',
+ );
+ $invoice->add_ap_amount_row(
+ amount => $invoice->netamount,
+ chart => $ap_amount_chart,
+ tax_id => $tax_9->id,
+ );
+
+ $invoice->create_ap_row(chart => $ap_chart);
+ $invoice->save;
+
+ is($invoice->currency_id , $currency->id , "$title: currency_id has been saved");
+ is($invoice->netamount , 100 , "$title: ap amount has been converted");
+ is($invoice->amount , 119 , "$title: ap amount has been converted");
+ is($invoice->taxincluded , 0 , "$title: ap transaction doesn\'t have taxincluded");
+ 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');
+ 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');
+
+ $invoice->pay_invoice(chart_id => $bank->id,
+ amount => 10,
+ currency => 'CUR',
+ transdate => $transdate2->to_kivitendo,
+ );
+ $invoice->pay_invoice(chart_id => $bank->id,
+ amount => 123.45,
+ currency => 'CUR',
+ transdate => $transdate3->to_kivitendo,