Search code examples
pythonodooodoo-12erp

How to daily reset a sequence (ir.sequence object) in Odoo 12?


I have a custom module in Odoo 12 and I need to create sequences for model records every day so, I need to reset it daily at 00:00 pm. I tried to use scheduler but I can not find which field should I override or reset the sequence I want. As I reviewed the ir_sequence table structure in the database, no values of this table fields are stored in the database, so how we can reset this sequence?

Here is the code I used as scheduler:

<odoo>
    <data noupdate="1">
        <record id="ir_cron_token_no_sequence_daily_restart_scheduler" model="ir.cron">
            <field name="name">Token Nummber Sequence Daily Restart Scheduler</field>
            <field name="user_id" ref="base.user_root"/>
            <field name="model_id" ref="acs_hms.model_hms_appointment"/>
            <field name="interval_number">1</field>
            <field name="interval_type">days</field>
            <field name="numbercall">-1</field>
            <field name="doall" eval="False"/>
            <field name="code">model._reset_token_number_sequences()</field>
            <field name="state">code</field>
        </record>
    </data>
</odoo>

This is my _reset_token_number_sequences() method:

def _reset_token_number_sequences(self):
        sequences = self.env['ir.sequence'].search([('name', '=like', '%nl_department_%')])
        for sequence in sequences:
            print('Sequence is : ', sequence)
            sequence.write({
                'number_next' : 1,
            })

But it's not working in my case, please help me on which field should I override it from here instead of number_next.


Solution

  • The field which holds next number value in odoo v12 is number_next_actual. So you will have to update number_next_actual field value through cron. Your python code should look like:

        def _reset_token_number_sequences(self):
            # just use write directly on the result this will execute one update query
            sequences = self.env['ir.sequence'].search([('name', '=like', '%nl_department_%')])
            sequences.write({'number_next_actual': 1}) 
    

    Hope this code help you.