+ 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 $title = '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,
+ );
+ $invoice->pay_invoice(chart_id => $bank->id,
+ amount => 15.30,
+ currency => 'CUR',
+ transdate => $transdate4->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: ap transaction has 3 fx transactions");
+ is($fx_transactions->[0]->amount, '-2.00000', "$title: fx transaction 1: 10.00-( 10.00*0.80000) = 2.00000");
+ is($fx_transactions->[1]->amount, '68.59000', "$title: fx transaction 2: 123.45-(123.45*1.55557) = -68.58511");
+ is($fx_transactions->[2]->amount, '-3.40000', "$title: fx transaction 3: 15.30-(15.30 *0.77777) = 3.40012");
+
+ 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'));
+ 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'));
+ is($fx_gain_transactions->[0]->amount, '0.34000', "$title: fx gain amount ok");
+ is($fx_loss_transactions->[0]->amount, '-93.28000', "$title: fx loss amount ok");
+
+ is(scalar @{$invoice->transactions}, 14, "$title: ap transaction has 14 transactions (incl. fxtransactions and gain_loss)");
+ is($invoice->paid, $invoice->amount, "$title: ap transaction paid = amount in default currency");
+ is(total_amount($invoice), 0, "$title: even balance");
+};
+
+sub test_ap_currency_tax_not_included_and_payment_2_credit_note {
+ my $title = 'test_ap_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::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 credit note 0.8 + 1.33333',
+ invnumber => 'test_ap_currency_tax_not_included_and_payment credit note 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,
+ );
+ $invoice->pay_invoice(chart_id => $bank->id,
+ amount => -15.30,
+ currency => 'CUR',
+ transdate => $transdate4->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: ap transaction has 3 fx transactions");
+ is($fx_transactions->[0]->amount, '2.00000', "$title: fx transaction 1: 10.00-( 10.00*0.80000) = 2.00000");
+ is($fx_transactions->[1]->amount, '-68.59000', "$title: fx transaction 2: 123.45-(123.45*1.55557) = -68.58511");
+ is($fx_transactions->[2]->amount, '3.40000', "$title: fx transaction 3: 15.30-(15.30 *0.77777) = 3.40012");
+
+ 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'));
+ is($fx_gain_loss_transactions->[0]->amount, '93.28000', "$title: fx gain loss amount ok");
+
+ is(scalar @{$invoice->transactions}, 14, "$title: ap transaction has 14 transactions (incl. fxtransactions and gain_loss)");
+ is($invoice->paid, $invoice->amount, "$title: ap transaction paid = amount in default currency");
+ is(total_amount($invoice), 0, "$title: even balance");
+};
+
+sub test_credit_note_two_items_19_7_tax_tax_not_included {
+ my $title = 'test_credit_note_two_items_19_7_tax_tax_not_included';
+
+ my $item1 = create_invoice_item(part => $parts[0], qty => 5);
+ my $item2 = create_invoice_item(part => $parts[1], qty => 3);
+ my $invoice = create_credit_note(
+ invnumber => 'cn1',
+ transdate => $transdate1,
+ taxincluded => 0,
+ invoiceitems => [ $item1, $item2 ],
+ );
+
+ # default values
+ my %params = ( chart_id => $bank_account->chart_id,
+ transdate => $transdate1,
+ );
+
+ $params{amount} = $invoice->amount,
+
+ $invoice->pay_invoice( %params );
+
+ my ($number_of_payments, $paid_amount) = number_of_payments($invoice);
+ my $total = total_amount($invoice);
+
+ is($invoice->netamount, -40.84, "${title}: netamount");
+ is($invoice->amount, -45.10, "${title}: amount");
+ is($paid_amount, 45.10, "${title}: paid amount according to acc_trans is positive (Haben)");
+ is($invoice->paid, -45.10, "${title}: paid");
+ is($number_of_payments, 1, "${title}: 1 AR_paid bookings");
+ is($total, 0, "${title}: even balance");
+}