Skip to content

Commit e97718b

Browse files
committed
Merge PR #2043 into 17.0
Signed-off-by BernatObrador
2 parents 0185a9f + 3a73f7e commit e97718b

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

account_asset_force_account/models/account_asset.py

+17-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class AccountAsset(models.Model):
1111
string="Asset Account",
1212
compute="_compute_account_asset_id",
1313
help="The account used to record the value of the asset.",
14+
store=True,
1415
)
1516

1617
account_depreciation_id = fields.Many2one(
@@ -37,6 +38,7 @@ def _onchange_profile_id(self):
3738
self.account_expense_depreciation_id = (
3839
self.profile_id.account_expense_depreciation_id
3940
)
41+
self._compute_account_asset_id()
4042

4143
@api.model_create_multi
4244
def create(self, vals_list):
@@ -51,10 +53,19 @@ def create(self, vals_list):
5153
] = profile.account_expense_depreciation_id.id
5254
return super().create(vals_list)
5355

56+
@api.depends("account_move_line_ids", "profile_id")
5457
def _compute_account_asset_id(self):
55-
if len(self.account_move_line_ids.account_id) != 0:
56-
self.account_asset_id = self.account_move_line_ids.sorted(
57-
lambda line: line.create_date
58-
).account_id[0]
59-
return
60-
self.account_asset_id = self.profile_id.account_asset_id
58+
for record in self:
59+
# Cannot update the account_asset_id if the asset is not in draft state
60+
if record.state != "draft":
61+
continue
62+
# Looks if the asset comes from an invoice, if so, takes the account from the invoice
63+
if len(record.account_move_line_ids.account_id) != 0:
64+
invoice_line = record.account_move_line_ids.filtered(
65+
lambda line: line.move_id.move_type == "in_invoice"
66+
)
67+
if invoice_line:
68+
record.account_asset_id = invoice_line.account_id
69+
continue
70+
# If not, takes the account from the profile
71+
record.account_asset_id = record.profile_id.account_asset_id

0 commit comments

Comments
 (0)