From 9ca30734acc00078806ae02017a244f140ac09cf Mon Sep 17 00:00:00 2001 From: mariadforgeflow Date: Wed, 9 Feb 2022 15:57:14 +0100 Subject: [PATCH] [IMP] account_asset_management: set asset_profile_id on invoice creation --- .../models/account_move.py | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/account_asset_management/models/account_move.py b/account_asset_management/models/account_move.py index 3c792175..15ff6de8 100644 --- a/account_asset_management/models/account_move.py +++ b/account_asset_management/models/account_move.py @@ -93,6 +93,8 @@ class AccountMove(models.Model): raise UserError( _("Asset name must be set in the label of the line.") ) + if aml.asset_id: + continue asset_form = Form( self.env["account.asset"] .with_company(move.company_id) @@ -102,7 +104,9 @@ class AccountMove(models.Model): setattr(asset_form, key, val) asset = asset_form.save() asset.analytic_tag_ids = aml.analytic_tag_ids - aml.with_context(allow_asset=True).asset_id = asset.id + aml.with_context( + allow_asset=True, allow_asset_removal=True + ).asset_id = asset.id refs = [ "%s" % tuple(name_get) @@ -157,6 +161,9 @@ class AccountMoveLine(models.Model): asset_profile_id = fields.Many2one( comodel_name="account.asset.profile", string="Asset Profile", + compute="_compute_asset_profile", + store=True, + readonly=False, ) asset_id = fields.Many2one( comodel_name="account.asset", @@ -164,11 +171,13 @@ class AccountMoveLine(models.Model): ondelete="restrict", ) - @api.onchange("account_id") - def _onchange_account_id(self): - if self.account_id.asset_profile_id: - self.asset_profile_id = self.account_id.asset_profile_id - super()._onchange_account_id() + @api.depends("account_id", "asset_id") + def _compute_asset_profile(self): + for rec in self: + if rec.account_id.asset_profile_id and not rec.asset_id: + rec.asset_profile_id = rec.account_id.asset_profile_id + elif rec.asset_id: + rec.asset_profile_id = rec.asset_id.profile_id @api.onchange("asset_profile_id") def _onchange_asset_profile_id(self):