Skip to content

Commit

Permalink
REF account_invoice_line_default_account using _onchange_product_id, …
Browse files Browse the repository at this point in the history
…which is always executed while adding invoice lines
  • Loading branch information
eLBati authored and SirTakobi committed May 17, 2022
1 parent cf3cc2c commit 69b1fc0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 53 deletions.
4 changes: 0 additions & 4 deletions account_invoice_line_default_account/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
33 changes: 14 additions & 19 deletions account_invoice_line_default_account/models/account_invoice_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
* `TAKOBI <https://takobi.online>`_:

* Simone Rubino <[email protected]>
* Lorenzo Battistini
30 changes: 0 additions & 30 deletions account_invoice_line_default_account/views/account_invoice.xml

This file was deleted.

0 comments on commit 69b1fc0

Please sign in to comment.