PseudoWithK
PseudoWithK

Reputation: 321

Create an automated action by date - Odoo 10

I have a date field (for closed an enrollment contract) that I created in a custom template. This field is not calculated but I would like to act on this field so that:

-> When the user chooses a date, I would like to retrieve this date to feed a CRON Odoo. This CRON will call a function to completely close the contract thanks to the date previously chosen by the user.

Do you have an idea ?

Here is my CRON:

    <record id="scheduler_synchronization_closed_contract_school_catering" model="ir.cron">
        <field name="name">Scheduler synchronization closed contract school catering</field>
        <field name="user_id" ref="base.user_root" />
        <field name="interval_number">1</field>
        <field name="interval_type">days</field>
        <field name="numbercall">1</field>
        <field name="args" eval=""/>
        <field name="nextcall" eval="" />
        <field name="doall" eval="False"/>
        <field name="model" eval="'ecole.partner.school'"/>
        <field name="function" eval="'closed_contract_school_catering'"/>
        <field name="active" eval="True"/>
    </record>

I guess I have to put something in args and nextcall but I can not find an example.

cordially

Upvotes: 1

Views: 858

Answers (1)

CZoellner
CZoellner

Reputation: 14778

You need a method in eg. ecole.partner.school which is doing the "closing contract" part. And then you can create a CronJob which is calling this method every hour or month.

In my example there will be a model school.catering.contract with a field date_close which can be set by a user in the client.

class SchoolCateringContract(models.Model):
    # _name, fields, ...

    @api.model
    def run_close_old_contracts(self):
        domain = [('date_close', '<=', fields.Date.today())]
        for contract in self.search(domain):
            # completely close contract whatever this means

The cron is nearly correct. nextcall and args aren't a must have. But numbercall is important. It means the number the cron should be called. So 1 means, that it will be called exactly one time and never again. If you need a cron which should be called every day, then set it to -1.

<record id="scheduler_synchronization_closed_contract_school_catering" model="ir.cron">
    <field name="name">Scheduler synchronization closed contract school catering</field>
    <field name="user_id" ref="base.user_root" />
    <field name="interval_number">1</field>
    <field name="interval_type">days</field>
    <field name="numbercall">-1</field>
    <field name="doall" eval="False"/>
    <field name="model" eval="'school.catering.contract'"/>
    <field name="function" eval="'run_close_old_contracts'"/>
    <field name="active" eval="True"/>
</record>

Upvotes: 2

Related Questions