diff --git a/addons/account/models/account_invoice.py b/addons/account/models/account_invoice.py
index d6d7be96a27..dfa950d8ca9 100644
--- a/addons/account/models/account_invoice.py
+++ b/addons/account/models/account_invoice.py
@@ -475,7 +475,7 @@ class AccountInvoice(models.Model):
account_id = pay_account.id
payment_term_id = p.property_supplier_payment_term_id.id
fiscal_position = p.property_account_position_id.id
- bank_id = p.bank_ids and p.bank_ids.ids[0] or False
+
# If partner has no warning, check its company
if p.invoice_warn == 'no-message' and p.parent_id:
@@ -496,8 +496,12 @@ class AccountInvoice(models.Model):
self.payment_term_id = payment_term_id
self.fiscal_position_id = fiscal_position
- if type in ('in_invoice', 'in_refund'):
+ if type in ('in_invoice', 'out_refund'):
+ bank_ids = p.commercial_partner_id.bank_ids
+ bank_id = bank_ids[0].id if bank_ids else False
self.partner_bank_id = bank_id
+ return {'domain': {'partner_bank_id': [('id', 'in', bank_ids.ids)]}}
+ return {}
@api.onchange('journal_id')
diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py
index e039f31353a..365095cfc5e 100644
--- a/addons/account/models/account_move.py
+++ b/addons/account/models/account_move.py
@@ -695,22 +695,37 @@ class AccountMoveLine(models.Model):
line_currency = (line.currency_id and line.amount_currency) and line.currency_id or company_currency
amount_currency_str = ""
total_amount_currency_str = ""
- if line_currency != company_currency and target_currency != company_currency:
+ if line_currency != company_currency and target_currency == line_currency:
+ # The payment currency is the invoice currency, but they are different than the company currency
+ # We use the `amount_currency` computed during the invoice validation, at the invoice date
+ # to avoid exchange gain/loss
+ # e.g. an invoice of 100€ must be paid with 100€, whatever the company currency and the exchange rates
total_amount = line.amount_currency
actual_debit = debit > 0 and amount_currency or 0.0
actual_credit = credit > 0 and -amount_currency or 0.0
+ currency = line_currency
else:
+ # Either:
+ # - the invoice, payment, company currencies are all the same,
+ # - the payment currency is the company currency, but the invoice currency is different,
+ # - the invoice currency is the company currency, but the payment currency is different,
+ # - the invoice, payment and company currencies are all different.
+ # For the two first cases, we can simply use the debit/credit of the invoice move line, which are always in the company currency,
+ # and this is what the target need.
+ # For the two last cases, we can use the debit/credit which are in the company currency, and then change them to the target currency
total_amount = abs(debit - credit)
actual_debit = debit > 0 and amount or 0.0
actual_credit = credit > 0 and -amount or 0.0
- if line_currency != target_currency and target_currency != company_currency:
+ currency = company_currency
+ if line_currency != target_currency:
amount_currency_str = formatLang(self.env, abs(actual_debit or actual_credit), currency_obj=line_currency)
total_amount_currency_str = formatLang(self.env, total_amount, currency_obj=line_currency)
+ if currency != target_currency:
ctx = context.copy()
ctx.update({'date': target_date or line.date})
- total_amount = line_currency.with_context(ctx).compute(total_amount, target_currency)
- actual_debit = line_currency.with_context(ctx).compute(actual_debit, target_currency)
- actual_credit = line_currency.with_context(ctx).compute(actual_credit, target_currency)
+ total_amount = currency.with_context(ctx).compute(total_amount, target_currency)
+ actual_debit = currency.with_context(ctx).compute(actual_debit, target_currency)
+ actual_credit = currency.with_context(ctx).compute(actual_credit, target_currency)
amount_str = formatLang(self.env, abs(actual_debit or actual_credit), currency_obj=target_currency)
total_amount_str = formatLang(self.env, total_amount, currency_obj=target_currency)
diff --git a/addons/account/models/account_payment.py b/addons/account/models/account_payment.py
index dde50d41683..3a15fe24de8 100644
--- a/addons/account/models/account_payment.py
+++ b/addons/account/models/account_payment.py
@@ -388,7 +388,7 @@ class account_payment(models.Model):
# the writeoff debit and credit must be computed from the invoice residual in company currency
# minus the payment amount in company currency, and not from the payment difference in the payment currency
# to avoid loss of precision during the currency rate computations. See revision 20935462a0cabeb45480ce70114ff2f4e91eaf79 for a detailed example.
- total_residual_company_signed = self._compute_total_invoices_amount()
+ total_residual_company_signed = sum(invoice.residual_company_signed for invoice in self.invoice_ids)
total_payment_company_signed = self.currency_id.with_context(date=self.payment_date).compute(self.amount, self.company_id.currency_id)
if self.invoice_ids[0].type in ['in_invoice', 'out_refund']:
amount_wo = total_payment_company_signed - total_residual_company_signed
diff --git a/addons/account/views/account_view.xml b/addons/account/views/account_view.xml
index 2e4f132f27b..0658be752d7 100644
--- a/addons/account/views/account_view.xml
+++ b/addons/account/views/account_view.xml
@@ -1118,7 +1118,6 @@