Skip to content

Commit

Permalink
[FIX] account,purchase: do not override analytic values
Browse files Browse the repository at this point in the history
Editable computed fields need to have their own compute
method.

Otherwise, when providing one of the two fields at create/write time
will not be taken into account because the compute method will be
triggered for the other field.

closes odoo#77900

X-original-commit: e35dc4c
Signed-off-by: William André (wan) <wan@odoo.com>
  • Loading branch information
simongoffin authored and william-andre committed Oct 5, 2021
1 parent 659c6a8 commit 676c568
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
20 changes: 17 additions & 3 deletions addons/account/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -3537,9 +3537,9 @@ class AccountMoveLine(models.Model):
# ==== Analytic fields ====
analytic_line_ids = fields.One2many('account.analytic.line', 'move_id', string='Analytic lines')
analytic_account_id = fields.Many2one('account.analytic.account', string='Analytic Account',
index=True, compute="_compute_analytic_account", store=True, readonly=False, check_company=True, copy=True)
index=True, compute="_compute_analytic_account_id", store=True, readonly=False, check_company=True, copy=True)
analytic_tag_ids = fields.Many2many('account.analytic.tag', string='Analytic Tags',
compute="_compute_analytic_account", store=True, readonly=False, check_company=True, copy=True)
compute="_compute_analytic_tag_ids", store=True, readonly=False, check_company=True, copy=True)

# ==== Onchange / display purpose fields ====
recompute_tax_line = fields.Boolean(store=False, readonly=True,
Expand Down Expand Up @@ -3792,7 +3792,7 @@ def _set_price_and_tax_after_fpos(self):
self.price_unit = business_vals['price_unit']

@api.depends('product_id', 'account_id', 'partner_id', 'date')
def _compute_analytic_account(self):
def _compute_analytic_account_id(self):
for record in self:
if not record.exclude_from_invoice_tab or not record.move_id.is_invoice(include_receipts=True):
rec = self.env['account.analytic.default'].account_get(
Expand All @@ -3805,6 +3805,20 @@ def _compute_analytic_account(self):
)
if rec:
record.analytic_account_id = rec.analytic_id

@api.depends('product_id', 'account_id', 'partner_id', 'date')
def _compute_analytic_tag_ids(self):
for record in self:
if not record.exclude_from_invoice_tab or not record.move_id.is_invoice(include_receipts=True):
rec = self.env['account.analytic.default'].account_get(
product_id=record.product_id.id,
partner_id=record.partner_id.commercial_partner_id.id or record.move_id.partner_id.commercial_partner_id.id,
account_id=record.account_id.id,
user_id=record.env.uid,
date=record.date,
company_id=record.move_id.company_id.id
)
if rec:
record.analytic_tag_ids = rec.analytic_tag_ids

def _get_price_total_and_subtotal(self, price_unit=None, quantity=None, discount=None, currency=None, product=None, partner=None, taxes=None, move_type=None):
Expand Down
37 changes: 25 additions & 12 deletions addons/purchase/models/purchase.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,8 +876,8 @@ class PurchaseOrderLine(models.Model):
price_tax = fields.Float(compute='_compute_amount', string='Tax', store=True)

order_id = fields.Many2one('purchase.order', string='Order Reference', index=True, required=True, ondelete='cascade')
account_analytic_id = fields.Many2one('account.analytic.account', store=True, string='Analytic Account', compute='_compute_analytic_id_and_tag_ids', readonly=False)
analytic_tag_ids = fields.Many2many('account.analytic.tag', store=True, string='Analytic Tags', compute='_compute_analytic_id_and_tag_ids', readonly=False)
account_analytic_id = fields.Many2one('account.analytic.account', store=True, string='Analytic Account', compute='_compute_account_analytic_id', readonly=False)
analytic_tag_ids = fields.Many2many('account.analytic.tag', store=True, string='Analytic Tags', compute='_compute_analytic_tag_ids', readonly=False)
company_id = fields.Many2one('res.company', related='order_id.company_id', string='Company', store=True, readonly=True)
state = fields.Selection(related='order_id.state', store=True)

Expand Down Expand Up @@ -1064,17 +1064,30 @@ def _get_date_planned(self, seller, po=False):
return self._convert_to_middle_of_day(date_planned)

@api.depends('product_id', 'date_order')
def _compute_analytic_id_and_tag_ids(self):
def _compute_account_analytic_id(self):
for rec in self:
default_analytic_account = rec.env['account.analytic.default'].sudo().account_get(
product_id=rec.product_id.id,
partner_id=rec.order_id.partner_id.id,
user_id=rec.env.uid,
date=rec.date_order,
company_id=rec.company_id.id,
)
rec.account_analytic_id = rec.account_analytic_id or default_analytic_account.analytic_id
rec.analytic_tag_ids = rec.analytic_tag_ids or default_analytic_account.analytic_tag_ids
if not rec.account_analytic_id:
default_analytic_account = rec.env['account.analytic.default'].sudo().account_get(
product_id=rec.product_id.id,
partner_id=rec.order_id.partner_id.id,
user_id=rec.env.uid,
date=rec.date_order,
company_id=rec.company_id.id,
)
rec.account_analytic_id = default_analytic_account.analytic_id

@api.depends('product_id', 'date_order')
def _compute_analytic_tag_ids(self):
for rec in self:
if not rec.analytic_tag_ids:
default_analytic_account = rec.env['account.analytic.default'].sudo().account_get(
product_id=rec.product_id.id,
partner_id=rec.order_id.partner_id.id,
user_id=rec.env.uid,
date=rec.date_order,
company_id=rec.company_id.id,
)
rec.analytic_tag_ids = default_analytic_account.analytic_tag_ids

@api.onchange('product_id')
def onchange_product_id(self):
Expand Down

0 comments on commit 676c568

Please sign in to comment.