63 lines
2.7 KiB
Python
63 lines
2.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')
|
|
|
|
@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}"
|
|
|
|
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,
|
|
}
|
|
}
|