When updating assets massively the performance when searching for deprecation_lines_ids is very low.
It is a difficult problem to detect since massive asset updates are not common.
Indexing in this field dramatically improves the timing performance.
For example, to update 3000 records it took 30 sec. With this index it takes to 2 seconds.
To start, the context `company_id` is no longer used in v14. Instead, we use `with_company`.
OTOH, it doesn't make much sense because in case you have several companies enabled in your context, not necessarily your main one is gonna be the one that should be used for the asset. Instead, it should be the move company always, or consistency rules will fail.
Altered forward port of https://github.com/OCA/account-financial-tools/pull/1223.
@Tecnativa TT31311
2 related fixes:
- When removing an asset line depreciation move, we have to pass it
first to draft, or we won't be able to remove it even with the
context.
- When removing a move, the check for removing the linked asset should
be only for purchase documents, not for "not sale" documents.
If the assets are created before posting the invoice, they wouldn't have a name and the "code" field of the assets will be empty, but they would have the invoice name.
Steps to reproduce the problem:
* Go to assets view
* Group by profile
* Unfold a group and click on an asset
* Click on "Journal Entries" smart-button
* Go back to the asset list
* Click again on the same asset (or another).
* Click on "Journal Entries" smart-button
Current behavior:
Error saying "KeyError: 'profile_id'"
Expected behavior:
No error
The cause for this is that Odoo stores in the context the key `group_by` with the
value `profile_id` in the specified chain of steps. That context entry is used for
grouping records in the list, and system tries to group the journal entries also
by that field, which doesn't exists in the other model, and thus the error.
We avoided it copying the context to be passes and leaving out that entry.
tools/account_asset_management/models/account_asset.py", line 970, in _compute_entries
% (asset.name, asset.code) or asset.name
UnboundLocalError: local variable 'asset' referenced before assignment