From 69b1fc0e4fcd15b91a601c4f47a0c3d8dc95cee3 Mon Sep 17 00:00:00 2001 From: eLBati Date: Tue, 26 Apr 2022 10:48:59 +0200 Subject: [PATCH] REF account_invoice_line_default_account using _onchange_product_id, which is always executed while adding invoice lines --- .../__manifest__.py | 4 --- .../models/account_invoice_line.py | 33 ++++++++----------- .../readme/CONTRIBUTORS.rst | 1 + .../views/account_invoice.xml | 30 ----------------- 4 files changed, 15 insertions(+), 53 deletions(-) delete mode 100644 account_invoice_line_default_account/views/account_invoice.xml diff --git a/account_invoice_line_default_account/__manifest__.py b/account_invoice_line_default_account/__manifest__.py index 290831390eb..b0c88ada966 100644 --- a/account_invoice_line_default_account/__manifest__.py +++ b/account_invoice_line_default_account/__manifest__.py @@ -9,15 +9,11 @@ 'depends': [ 'account' ], - 'excludes': [ - 'account_invoice_line_sequence', - ], 'author': 'Therp BV,BCIM,Odoo Community Association (OCA)', 'license': 'AGPL-3', 'category': 'Accounting', 'data': [ 'views/res_partner.xml', - 'views/account_invoice.xml', ], 'installable': True, } diff --git a/account_invoice_line_default_account/models/account_invoice_line.py b/account_invoice_line_default_account/models/account_invoice_line.py index 93da9bbb445..afef65ef814 100644 --- a/account_invoice_line_default_account/models/account_invoice_line.py +++ b/account_invoice_line_default_account/models/account_invoice_line.py @@ -3,30 +3,25 @@ # Copyright 2022 Simone Rubino - TAKOBI # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from odoo import api, fields, models, _ +from odoo import api, models, _ class AccountInvoiceLine(models.Model): _inherit = 'account.invoice.line' - def _account_id_default(self): - partner_id = self.env.context.get('partner_id') - if not partner_id: - return self._default_account() - assert isinstance(partner_id, int), ( - _('No valid id for context partner_id %d') % partner_id) - invoice_type = self.env.context.get('type') - if invoice_type in ['in_invoice', 'in_refund']: - partner = self.env['res.partner'].browse(partner_id) - if partner.property_account_expense: - return partner.property_account_expense.id - elif invoice_type in ['out_invoice', 'out_refund']: - partner = self.env['res.partner'].browse(partner_id) - if partner.property_account_income: - return partner.property_account_income.id - return self._default_account() - - account_id = fields.Many2one(default=_account_id_default) + @api.onchange('product_id') + def _onchange_product_id(self): + res = super(AccountInvoiceLine, self)._onchange_product_id() + partner = self.invoice_id.partner_id + invoice_type = self.invoice_id.type + if partner and invoice_type: + if invoice_type in ['in_invoice', 'in_refund']: + if partner.property_account_expense: + self.account_id = partner.property_account_expense.id + elif invoice_type in ['out_invoice', 'out_refund']: + if partner.property_account_income: + self.account_id = partner.property_account_income.id + return res @api.onchange('account_id') def _onchange_default_partner_account(self): diff --git a/account_invoice_line_default_account/readme/CONTRIBUTORS.rst b/account_invoice_line_default_account/readme/CONTRIBUTORS.rst index cb2554413ee..3800d985d0c 100644 --- a/account_invoice_line_default_account/readme/CONTRIBUTORS.rst +++ b/account_invoice_line_default_account/readme/CONTRIBUTORS.rst @@ -3,3 +3,4 @@ * `TAKOBI `_: * Simone Rubino + * Lorenzo Battistini diff --git a/account_invoice_line_default_account/views/account_invoice.xml b/account_invoice_line_default_account/views/account_invoice.xml deleted file mode 100644 index 7f7f1259e4a..00000000000 --- a/account_invoice_line_default_account/views/account_invoice.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - invoice_line_ids partner context - account.invoice - form - - - - {'partner_id': partner_id, 'type': type, 'journal_id': journal_id} - - - - - invoice_line_ids partner context - account.invoice - form - - - - {'partner_id': partner_id, 'type': type, 'journal_id': journal_id} - - - -