Home > OS >  How can I increment sequence number in a conditional sequence odoo
How can I increment sequence number in a conditional sequence odoo

Time:12-16

this function

@api.model
    def create(self, vals):
        if vals.get('intervention_category') == ('qualityassured'):
            vals['project_ref'] = self.env['ir.sequence'].next_by_code(
                'mis.project.qualityassured') or ('/')
        if vals.get('intervention_category') == ('nonqualityassured'):
            vals['project_ref'] = self.env['ir.sequence'].next_by_code(
                'mis.project.sequence') or ('/')
        result = super().create(vals)
        return result

generates a different sequence sequence for the two different intervention category. here is the xml:

 <record id="qualityassured_seq" model="ir.sequence">
            <field name="name">PROJECTS</field>
            <field name="code">mis.project.qualityassured</field>
            <field name="active">TRUE</field>
            <field name="prefix">WBLA/%(year)s/</field>
            <field name="padding">3</field>
            <field name="number_next">1</field>
            <field name="number_increment">1</field>
        </record>
        
        <record id="project_seq" model="ir.sequence">
            <field name="name">PROJECTS</field>
            <field name="code">mis.project.sequence</field>
            <field name="active">TRUE</field>
            <field name="prefix">P/%(year)s/</field>
            <field name="padding">3</field>
            <field name="number_next">1</field>
            <field name="number_increment">1</field>
        </record>

the problem I am facing is, If I select "qualityassured" as intervention category for instance, the sequence will save as WBLA/2022/001. if I select the same intervention category as earlier and create a project it will increment to WBLA/2022/02. but when I select nonqualityassured as intervention category and create a project, if on another project I am creating I select "qualityassured" the sequence will start from WBLA/2022/01 instead of starting from WBLA/2022/003. same problem with when I select "nonqualityassured" vice versa. I'd appreciate any help I can get to improve this code. thanks in advance.

CodePudding user response:

You can define only 1 sequence and omit the prefix part of the sequence, so that whatever the condition, the sequence will increment and it is the only sequence; therefore the problem will be fixed.

Next, in the condition parts where you want your project_ref to have different prefix depending on different options you have. You still use the same if codes, but you manually prefix the sequence by the constant characters that you desire. For example,

if vals.get('intervention_category') == ('qualityassured'):
    vals['project_ref'] = 'WBLA'
if vals.get('intervention_category') == ('nonqualityassured'):
    vals['project_ref'] = 'P'
vals['project_ref']  = self.env['ir.sequence'].next_by_code('mis.project') or ('/')
  • Related