From d5688e6ca08c0732f192207b9bb584bf7b672e90 Mon Sep 17 00:00:00 2001 From: Nans Lefebvre Date: Wed, 3 Jul 2019 13:10:41 +0000 Subject: [PATCH] [FIX] account: set bank account when creating invoices programmatically The bank account field is set up by the default_get of invoice. Similarly to the application of the onchange in the create, we add it in the create if it is not already part of the given values. This will set the bank account for invoices created e.g. from subscriptions. Note that we extract it in a function to avoid duplicating the logic. opw 2030562 closes odoo/odoo#34558 Signed-off-by: Nans Lefebvre (len) --- addons/account/models/account_invoice.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/addons/account/models/account_invoice.py b/addons/account/models/account_invoice.py index e4ec3fcbf01..f70b61bbe44 100644 --- a/addons/account/models/account_invoice.py +++ b/addons/account/models/account_invoice.py @@ -544,6 +544,9 @@ class AccountInvoice(models.Model): for field in changed_fields: if field not in vals and invoice[field]: vals[field] = invoice._fields[field].convert_to_write(invoice[field], invoice) + bank_account = self._get_default_bank_id(vals.get('type'), vals.get('company_id')) + if bank_account and not vals.get('partner_bank_id'): + vals['partner_bank_id'] = bank_account.id invoice = super(AccountInvoice, self.with_context(mail_create_nolog=True)).create(vals) @@ -579,14 +582,20 @@ class AccountInvoice(models.Model): """ res = super(AccountInvoice, self).default_get(default_fields) - if res.get('type', False) not in ('out_invoice', 'in_refund') or not 'company_id' in res: - return res - - partner_bank_result = self._get_partner_bank_id(res['company_id']) + partner_bank_result = self._get_default_bank_id(res.get('type'), res.get('company_id')) if partner_bank_result: res['partner_bank_id'] = partner_bank_result.id return res + def _get_default_bank_id(self, type, company_id): + """When setting the default bank account, we have two cases: + in the case of 'out_invoice', 'in_refund', we want the bank account of the company. + otherwise, we want the bank account of the partner. + """ + if type not in ('out_invoice', 'in_refund') or not company_id: + return False + return self._get_partner_bank_id(company_id) + def _get_partner_bank_id(self, company_id): company = self.env['res.company'].browse(company_id) if company.partner_id: