@@ -11,6 +11,7 @@ class AccountAsset(models.Model):
11
11
string = "Asset Account" ,
12
12
compute = "_compute_account_asset_id" ,
13
13
help = "The account used to record the value of the asset." ,
14
+ store = True ,
14
15
)
15
16
16
17
account_depreciation_id = fields .Many2one (
@@ -37,6 +38,7 @@ def _onchange_profile_id(self):
37
38
self .account_expense_depreciation_id = (
38
39
self .profile_id .account_expense_depreciation_id
39
40
)
41
+ self ._compute_account_asset_id ()
40
42
41
43
@api .model_create_multi
42
44
def create (self , vals_list ):
@@ -51,10 +53,19 @@ def create(self, vals_list):
51
53
] = profile .account_expense_depreciation_id .id
52
54
return super ().create (vals_list )
53
55
56
+ @api .depends ("account_move_line_ids" , "profile_id" )
54
57
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