ga_asset_management/models/account_asset.py

95 lines
4.7 KiB
Python

from odoo import models, fields, api
import datetime
class AccountAsset(models.Model):
_inherit = 'account.asset'
product_id = fields.Many2one('product.product', string='Product')
description = fields.Text(string='Description')
location_id = fields.Many2one('stock.location', string='Location', domain="[('company_id', '=', company_id)]")
employee_id = fields.Many2one('hr.employee', string='Employee', domain="[('company_id', '=', company_id)]")
in_transit = fields.Boolean(string='In Transit', default=False, help="Asset is currently being transferred between companies.")
@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
if not vals.get('asset_code'):
product_id = vals.get('product_id')
sequence = self.env['ir.sequence'].next_by_code('ga.asset.code') or '00000'
year = datetime.datetime.now().year
prefix = 'AST'
if product_id:
product = self.env['product.product'].browse(product_id)
if product.barcode:
prefix = product.barcode
# Format: [Barcode/AST]/[Year]/[Sequence]
# Note: Sequence defined in XML has prefix '/' so we just append it
# Wait, sequence next_by_code returns the full string including prefix/suffix if defined
# My XML defines prefix '/' and padding 5. So it returns '/00001'
# Let's construct manually to control the format exactly as requested:
# [Barcode]/[Year]/[Sequence_Number]
# If I use next_by_code, I get what is configured.
# To get just number, checking implementation... standard next_by_code returns full string.
# Let's simple use sequence for the number part only.
# I will change sequence prefix to empty in XML or just use it here.
# Actually, standard way is to have sequence configured with year but here we have dynamic prefix (Product Barcode).
# Revised Logic:
# 1. Get raw sequence number? No, next_by_code gives formatted string.
# Let's use a sequence with NO prefix in XML, and build format here.
# Re-reading XML I created: prefix='/'
# So `self.env['ir.sequence'].next_by_code('ga.asset.code')` returns `/00001`.
vals['asset_code'] = f"{prefix}/{year}{sequence}"
# Remove original_value if it is 0.0 to allow computation from lines later
if 'original_value' in vals and vals['original_value'] == 0.0:
del vals['original_value']
# Populate accounting fields from Asset Model if not already set
if vals.get('model_id'):
model = self.env['account.asset'].browse(vals['model_id'])
if model:
if not vals.get('method'):
vals['method'] = model.method
if not vals.get('method_number'):
vals['method_number'] = model.method_number
if not vals.get('method_period'):
vals['method_period'] = model.method_period
if not vals.get('prorata_computation_type'):
vals['prorata_computation_type'] = model.prorata_computation_type
if not vals.get('account_asset_id'):
vals['account_asset_id'] = model.account_asset_id.id
if not vals.get('account_depreciation_id'):
vals['account_depreciation_id'] = model.account_depreciation_id.id
if not vals.get('account_depreciation_expense_id'):
vals['account_depreciation_expense_id'] = model.account_depreciation_expense_id.id
if not vals.get('journal_id'):
vals['journal_id'] = model.journal_id.id
if not vals.get('analytic_distribution') and model.analytic_distribution:
vals['analytic_distribution'] = model.analytic_distribution
return super(AccountAsset, self).create(vals_list)
def action_open_transfer_wizard(self):
self.ensure_one()
return {
'name': 'Transfer Asset',
'type': 'ir.actions.act_window',
'res_model': 'ga.asset.transfer.wizard',
'view_mode': 'form',
'target': 'new',
'context': {
'default_asset_id': self.id,
'default_current_company_id': self.company_id.id,
}
}