This question has been flagged
2 Replies
3017 Views

Hello,

I am looking to calculate a field automatically. This field is a business reference and consists of 2 letters and 5 digits. The first letter is fixed and the second is incremented each year. For the first two digits it corresponds to the current month and the last three are incremented by new creation and reinialise each year.

RA05003 = First year, mounth may, and 3rd project

Do you think that it is possible to do this with a caluted field?

thanks a lot for your help

Avatar
Discard
Best Answer

Yes this is possible.

First you have to create a sequence for last three digit. Then after make a compute field which computes the string as per your need. So follow these steps.

Create data.xml under data folder add in manifest file.

<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">
<!--
ID Sequences
-->
<record id="sequence_temp+id" model="ir.sequence">
<field name="name">Sequence</field>
<field name="code">your model name</field>
<field name="prefix"></field>
<field eval="1" name="number_next"/>
<field eval="1" name="number_increment"/>
<field eval="False" name="company_id"/>
<field name="padding">3</field>
</record>
</data>
</odoo>

model.py

seq_id = fields.Char(string="string"  help="ID used for employee identification.",
default=lambda self: self.env['ir.sequence'].next_by_code('your model name'), invisible=True, copy=False)
computed_field = fields.Char(compute="_compute_id_with_month_year")

import datetime
current_month = datetime.datetime.today().month # 5 eg

@api.depends('seq_id')
def _compute_id_with_month_year(self):
    for rec in self:
        if rec.seq_id:
            rec.computed_field = 'RA' + str(current_month) + rec.seq_id
        else:
            pass

Replace the string, fields names with your own needs.

Thanks




Avatar
Discard
Author Best Answer

Hello, Thank you so much for make time to answer me. I try this night , i am see if it's work thank :)

Avatar
Discard