To interact with the Odoo Immobilisation (Asset Management) module programmatically, developers often work with Odoo’s object-relational model (ORM) and Python code. Below are some key coding examples for managing assets, asset depreciation, and generating journal entries through the Odoo framework.
Also Read:
- Odoo Framework
- Odoo Editions
- ERPNext vs Odoo
- How to Create and Use a Slider in Odoo
- How to Create a Custom Revenue Report in Odoo
Odoo Immobilisation (Asset Management) – Complete Guidance
1. Creating an Asset Record
In Odoo, the account.asset.asset
model is used to manage fixed assets. Here’s an example of how to create an asset record programmatically using Odoo’s ORM.
from odoo import models, fields
class AssetCreation(models.Model):
_name = ‘asset.creation’
name = fields.Char(string=‘Asset Name’)
purchase_date = fields.Date(string=‘Purchase Date’)
value = fields.Float(string=‘Purchase Value’)
category_id = fields.Many2one(‘account.asset.category’, string=‘Asset Category’)
def create_asset(self):
# Create an asset record
asset_vals = {
‘name’: self.name,
‘purchase_date’: self.purchase_date,
‘value’: self.value,
‘category_id’: self.category_id.id,
}
# Create asset
asset = self.env[‘account.asset.asset’].create(asset_vals)
return asset
2. Depreciating the Asset Automatically
Once an asset is created, depreciation entries are automatically handled by Odoo, but you may want to programmatically adjust or force depreciation. Below is an example of how to trigger asset depreciation manually:
from odoo import models, fields
from odoo.exceptions import UserError
class AssetDepreciation(models.Model):_name = ‘asset.depreciation’
asset_id = fields.Many2one(‘account.asset.asset’, string=‘Asset’)
depreciation_period = fields.Integer(string=‘Depreciation Period (months)’, required=True)
def force_depreciation(self):
# Validate the asset
if not self.asset_id:
raise UserError(“No asset selected for depreciation.”)
# Force depreciation: Generate depreciation entries
for asset in self.asset_id:
# Trigger the asset’s depreciation for the specified period
asset.action_depreciation_confirm() # This action confirms depreciation
# You can also modify depreciation methods if needed
asset.write({
‘depreciation_period’: self.depreciation_period
})
return True
In this case, the action_depreciation_confirm()
method is used to confirm depreciation. You can adjust depreciation periods by modifying the asset’s depreciation_period
field or directly handling the depreciation using the provided methods in the account.asset.asset
model.
3. Creating a Journal Entry for Asset Purchase
When an asset is purchased, a journal entry is created automatically in Odoo. However, you might want to create journal entries manually for certain scenarios. Here’s an example of how to create a journal entry for the purchase of an asset.
from odoo import models, fields
class AssetJournalEntry(models.Model):
_name = ‘asset.journal.entry’
asset_id = fields.Many2one(‘account.asset.asset’, string=‘Asset’)
account_id = fields.Many2one(‘account.account’, string=‘Account’)
amount = fields.Float(string=‘Amount’)
def create_journal_entry(self):
# Creating a journal entry for the asset purchase
if not self.asset_id:
raise ValueError(“Asset not selected.”)
journal_entry_vals = {
‘name’: f”Asset Purchase: {self.asset_id.name}“,
‘journal_id’: self.asset_id.category_id.property_stock_account_input_categ.id, # Using the category’s account
‘line_ids’: [(0, 0, {
‘account_id’: self.account_id.id,
‘debit’: self.amount,
‘credit’: 0.0,
}),
(0, 0, {
‘account_id’: self.asset_id.category_id.property_account_asset.id,
‘debit’: 0.0,
‘credit’: self.amount,
})],
}
# Create the journal entry
journal_entry = self.env[‘account.move’].create(journal_entry_vals)
journal_entry.post() # Post the journal entry to confirm it
return journal_entry
4. Handling Asset Disposal (Write-off or Sale)
When an asset is disposed of, its residual value is calculated, and a journal entry is generated. Here’s an example of how to handle asset disposal:
from odoo import models, fields
from odoo.exceptions import UserError
class AssetDisposal(models.Model):_name = ‘asset.disposal’
asset_id = fields.Many2one(‘account.asset.asset’, string=‘Asset to Dispose’)
disposal_value = fields.Float(string=‘Disposal Value’)
def dispose_asset(self):
if not self.asset_id:
raise UserError(“No asset selected for disposal.”)
asset = self.asset_id
if asset.state != ‘open’:
raise UserError(“Asset must be in an open state to dispose of it.”)
# Calculate residual value (this may vary depending on how depreciation was calculated)
residual_value = asset.value – asset.depreciation
# Create a journal entry for the disposal
journal_entry_vals = {
‘name’: f”Asset Disposal: {asset.name}“,
‘journal_id’: asset.category_id.property_stock_account_input_categ.id,
‘line_ids’: [(0, 0, {
‘account_id’: asset.category_id.property_account_asset.id,
‘debit’: residual_value,
‘credit’: 0.0,
}),
(0, 0, {
‘account_id’: asset.category_id.property_account_expense.id,
‘debit’: 0.0,
‘credit’: residual_value,
})],
}
# Create and post the journal entry
journal_entry = self.env[‘account.move’].create(journal_entry_vals)
journal_entry.post()
# Set the asset to the disposed state
asset.write({‘state’: ‘disposed’})
return True
In this example, we assume that when an asset is disposed of, it creates an expense entry for the residual value (the value after depreciation), and it changes the asset’s state to “disposed.” This module can be expanded based on specific business rules for asset disposal.
5. Transferring Assets Between Locations
When an asset is transferred from one location to another, you can track the asset movement using the following code:
from odoo import models, fields
class AssetTransfer(models.Model):
_name = ‘asset.transfer’
asset_id = fields.Many2one(‘account.asset.asset’, string=‘Asset’)
location_from = fields.Char(string=‘From Location’)
location_to = fields.Char(string=‘To Location’)
def transfer_asset(self):
if not self.asset_id:
raise ValueError(“No asset selected for transfer.”)
# Transfer asset details
transfer_vals = {
‘asset_id’: self.asset_id.id,
‘location_from’: self.location_from,
‘location_to’: self.location_to,
}
# Record the transfer event (You can customize this part as per your business requirements)
self.env[‘asset.history’].create(transfer_vals)
# Update asset location (if you maintain location as a field on the asset)
self.asset_id.write({‘location’: self.location_to})
return True
This code records the movement of assets from one location to another and maintains a history of such transfers.
Conclusion
The Odoo framework and Odoo Immobilisation allows businesses to manage their assets efficiently by automating tasks such as depreciation, journal entry creation, and asset disposal. With Python code, developers can customize and extend the asset management features to fit their specific needs. The provided examples give you a foundation for handling asset creation, depreciation, journal entries, and transfers, making Odoo a powerful tool for managing Odoo Immobilisation and accounting. By building on these examples, developers can create a fully integrated and automated asset management solution within Odoo.
For more information about the Odoo Immobilisation (Asset Management), visit this link.
If you want to Free Trail Zoho, click on this link.